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 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1142 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1143 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1144 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1145 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1146 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1147 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1148 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1149 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1150 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1151 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1152 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1153 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1157 | 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] | 1158 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1159 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1160 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1161 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1162 | op = iff_getop(expr->expr, *index_e); |
| 1163 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1164 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1165 | switch (op) { |
| 1166 | case LYS_IFF_F: |
| 1167 | /* resolve feature */ |
| 1168 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1169 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1170 | /* invert result */ |
| 1171 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1172 | case LYS_IFF_AND: |
| 1173 | case LYS_IFF_OR: |
| 1174 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1175 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1176 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1177 | return a && b; |
| 1178 | } else { /* LYS_IFF_OR */ |
| 1179 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1183 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | int |
| 1187 | resolve_iffeature(struct lys_iffeature *expr) |
| 1188 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1189 | int index_e = 0, index_f = 0; |
| 1190 | |
| 1191 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1192 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1193 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1194 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | struct iff_stack { |
| 1198 | int size; |
| 1199 | int index; /* first empty item */ |
| 1200 | uint8_t *stack; |
| 1201 | }; |
| 1202 | |
| 1203 | static int |
| 1204 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1205 | { |
| 1206 | if (stack->index == stack->size) { |
| 1207 | stack->size += 4; |
| 1208 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 1209 | if (!stack->stack) { |
| 1210 | LOGMEM; |
| 1211 | stack->size = 0; |
| 1212 | return EXIT_FAILURE; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | stack->stack[stack->index++] = value; |
| 1217 | return EXIT_SUCCESS; |
| 1218 | } |
| 1219 | |
| 1220 | static uint8_t |
| 1221 | iff_stack_pop(struct iff_stack *stack) |
| 1222 | { |
| 1223 | stack->index--; |
| 1224 | return stack->stack[stack->index]; |
| 1225 | } |
| 1226 | |
| 1227 | static void |
| 1228 | iff_stack_clean(struct iff_stack *stack) |
| 1229 | { |
| 1230 | stack->size = 0; |
| 1231 | free(stack->stack); |
| 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1236 | { |
| 1237 | uint8_t *item; |
| 1238 | uint8_t mask = 3; |
| 1239 | |
| 1240 | assert(pos >= 0); |
| 1241 | assert(op <= 3); /* max 2 bits */ |
| 1242 | |
| 1243 | item = &list[pos / 4]; |
| 1244 | mask = mask << 2 * (pos % 4); |
| 1245 | *item = (*item) & ~mask; |
| 1246 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1247 | } |
| 1248 | |
| 1249 | uint8_t |
| 1250 | iff_getop(uint8_t *list, int pos) |
| 1251 | { |
| 1252 | uint8_t *item; |
| 1253 | uint8_t mask = 3, result; |
| 1254 | |
| 1255 | assert(pos >= 0); |
| 1256 | |
| 1257 | item = &list[pos / 4]; |
| 1258 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1259 | return result >> 2 * (pos % 4); |
| 1260 | } |
| 1261 | |
| 1262 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1263 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1264 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1265 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1266 | struct unres_iffeat_data { |
| 1267 | struct lys_node *node; |
| 1268 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1269 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1270 | }; |
| 1271 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1272 | void |
| 1273 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1274 | { |
| 1275 | unsigned int e = 0, f = 0, r = 0; |
| 1276 | uint8_t op; |
| 1277 | |
| 1278 | assert(iffeat); |
| 1279 | |
| 1280 | if (!iffeat->expr) { |
| 1281 | goto result; |
| 1282 | } |
| 1283 | |
| 1284 | do { |
| 1285 | op = iff_getop(iffeat->expr, e++); |
| 1286 | switch (op) { |
| 1287 | case LYS_IFF_NOT: |
| 1288 | if (!r) { |
| 1289 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1290 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1291 | break; |
| 1292 | case LYS_IFF_AND: |
| 1293 | case LYS_IFF_OR: |
| 1294 | if (!r) { |
| 1295 | r += 2; |
| 1296 | } else { |
| 1297 | r += 1; |
| 1298 | } |
| 1299 | break; |
| 1300 | case LYS_IFF_F: |
| 1301 | f++; |
| 1302 | if (r) { |
| 1303 | r--; |
| 1304 | } |
| 1305 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1306 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1307 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1308 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1309 | result: |
| 1310 | if (expr_size) { |
| 1311 | *expr_size = e; |
| 1312 | } |
| 1313 | if (feat_size) { |
| 1314 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1319 | 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] | 1320 | int infeature, struct unres_schema *unres) |
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 | const char *c = value; |
| 1323 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1324 | int i, j, last_not, checkversion = 0; |
| 1325 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1326 | uint8_t op; |
| 1327 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1328 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1329 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1330 | assert(c); |
| 1331 | |
| 1332 | if (isspace(c[0])) { |
| 1333 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1334 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1337 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1338 | for (i = j = last_not = 0; c[i]; i++) { |
| 1339 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1340 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1341 | j++; |
| 1342 | continue; |
| 1343 | } else if (c[i] == ')') { |
| 1344 | j--; |
| 1345 | continue; |
| 1346 | } else if (isspace(c[i])) { |
| 1347 | continue; |
| 1348 | } |
| 1349 | |
| 1350 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1351 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1352 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1353 | return EXIT_FAILURE; |
| 1354 | } else if (!isspace(c[i + r])) { |
| 1355 | /* feature name starting with the not/and/or */ |
| 1356 | last_not = 0; |
| 1357 | f_size++; |
| 1358 | } else if (c[i] == 'n') { /* not operation */ |
| 1359 | if (last_not) { |
| 1360 | /* double not */ |
| 1361 | expr_size = expr_size - 2; |
| 1362 | last_not = 0; |
| 1363 | } else { |
| 1364 | last_not = 1; |
| 1365 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1366 | } else { /* and, or */ |
| 1367 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1368 | /* not a not operation */ |
| 1369 | last_not = 0; |
| 1370 | } |
| 1371 | i += r; |
| 1372 | } else { |
| 1373 | f_size++; |
| 1374 | last_not = 0; |
| 1375 | } |
| 1376 | expr_size++; |
| 1377 | |
| 1378 | while (!isspace(c[i])) { |
| 1379 | if (!c[i] || c[i] == ')') { |
| 1380 | i--; |
| 1381 | break; |
| 1382 | } |
| 1383 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1384 | } |
| 1385 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1386 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1387 | /* not matching count of ( and ) */ |
| 1388 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1389 | return EXIT_FAILURE; |
| 1390 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1391 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1392 | if (checkversion || expr_size > 1) { |
| 1393 | /* check that we have 1.1 module */ |
| 1394 | if (node->module->version != 2) { |
| 1395 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1396 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1397 | return EXIT_FAILURE; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1401 | /* allocate the memory */ |
| 1402 | 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] | 1403 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1404 | stack.size = expr_size; |
| 1405 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1406 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1407 | LOGMEM; |
| 1408 | goto error; |
| 1409 | } |
| 1410 | f_size--; expr_size--; /* used as indexes from now */ |
| 1411 | |
| 1412 | for (i--; i >= 0; i--) { |
| 1413 | if (c[i] == ')') { |
| 1414 | /* push it on stack */ |
| 1415 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1416 | continue; |
| 1417 | } else if (c[i] == '(') { |
| 1418 | /* pop from the stack into result all operators until ) */ |
| 1419 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1420 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1421 | } |
| 1422 | continue; |
| 1423 | } else if (isspace(c[i])) { |
| 1424 | continue; |
| 1425 | } |
| 1426 | |
| 1427 | /* end operator or operand -> find beginning and get what is it */ |
| 1428 | j = i + 1; |
| 1429 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1430 | i--; |
| 1431 | } |
| 1432 | i++; /* get back by one step */ |
| 1433 | |
| 1434 | if (!strncmp(&c[i], "not ", 4)) { |
| 1435 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1436 | /* double not */ |
| 1437 | iff_stack_pop(&stack); |
| 1438 | } else { |
| 1439 | /* not has the highest priority, so do not pop from the stack |
| 1440 | * as in case of AND and OR */ |
| 1441 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1442 | } |
| 1443 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1444 | /* as for OR - pop from the stack all operators with the same or higher |
| 1445 | * priority and store them to the result, then push the AND to the stack */ |
| 1446 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1447 | op = iff_stack_pop(&stack); |
| 1448 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1449 | } |
| 1450 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1451 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1452 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1453 | op = iff_stack_pop(&stack); |
| 1454 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1455 | } |
| 1456 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1457 | } else { |
| 1458 | /* feature name, length is j - i */ |
| 1459 | |
| 1460 | /* add it to the result */ |
| 1461 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1462 | |
| 1463 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1464 | * forward referenced, we have to keep the feature name in auxiliary |
| 1465 | * structure passed into unres */ |
| 1466 | iff_data = malloc(sizeof *iff_data); |
| 1467 | iff_data->node = node; |
| 1468 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1469 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1470 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1471 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1472 | f_size--; |
| 1473 | |
| 1474 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1475 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1476 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1479 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1480 | while (stack.index) { |
| 1481 | op = iff_stack_pop(&stack); |
| 1482 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1483 | } |
| 1484 | |
| 1485 | if (++expr_size || ++f_size) { |
| 1486 | /* not all expected operators and operands found */ |
| 1487 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1488 | rc = EXIT_FAILURE; |
| 1489 | } else { |
| 1490 | rc = EXIT_SUCCESS; |
| 1491 | } |
| 1492 | |
| 1493 | error: |
| 1494 | /* cleanup */ |
| 1495 | iff_stack_clean(&stack); |
| 1496 | |
| 1497 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1501 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1502 | * |
| 1503 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1504 | * module). |
| 1505 | * |
| 1506 | */ |
| 1507 | struct lyd_node * |
| 1508 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1509 | { |
| 1510 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1511 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1512 | const struct lys_node *schema = NULL; |
| 1513 | |
| 1514 | assert(nodeid && start); |
| 1515 | |
| 1516 | if (nodeid[0] == '/') { |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | |
| 1520 | str = p = strdup(nodeid); |
| 1521 | if (!str) { |
| 1522 | LOGMEM; |
| 1523 | return NULL; |
| 1524 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1525 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1526 | while (p) { |
| 1527 | token = p; |
| 1528 | p = strchr(p, '/'); |
| 1529 | if (p) { |
| 1530 | *p = '\0'; |
| 1531 | p++; |
| 1532 | } |
| 1533 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1534 | if (p) { |
| 1535 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1536 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1537 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1538 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1539 | result = NULL; |
| 1540 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1541 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1542 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1543 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1544 | continue; |
| 1545 | } |
| 1546 | } else { |
| 1547 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1548 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1549 | || !schema) { |
| 1550 | result = NULL; |
| 1551 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1552 | } |
| 1553 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1554 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1555 | if (iter->schema == schema) { |
| 1556 | /* move in data tree according to returned schema */ |
| 1557 | result = iter; |
| 1558 | break; |
| 1559 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1560 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1561 | if (!iter) { |
| 1562 | /* instance not found */ |
| 1563 | result = NULL; |
| 1564 | break; |
| 1565 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1566 | } |
| 1567 | free(str); |
| 1568 | |
| 1569 | return result; |
| 1570 | } |
| 1571 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1572 | /* |
| 1573 | * 0 - ok (done) |
| 1574 | * 1 - continue |
| 1575 | * 2 - break |
| 1576 | * -1 - error |
| 1577 | */ |
| 1578 | static int |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1579 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const char *id, const struct lys_module *module, |
| 1580 | const char *mod_name, int mod_name_len, int implemented_mod, |
| 1581 | const struct lys_node **start_parent) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1582 | { |
| 1583 | const struct lys_module *prefix_mod; |
| 1584 | |
| 1585 | /* module check */ |
| 1586 | 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] | 1587 | if (prefix_mod && implemented_mod) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1588 | prefix_mod = lys_implemented_module(prefix_mod); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1589 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1590 | if (!prefix_mod) { |
| 1591 | return -1; |
| 1592 | } |
| 1593 | if (prefix_mod != lys_node_module(sibling)) { |
| 1594 | return 1; |
| 1595 | } |
| 1596 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1597 | /* the result node? */ |
| 1598 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1602 | /* move down the tree, if possible */ |
| 1603 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1604 | return -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1605 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1606 | *start_parent = sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1607 | |
| 1608 | return 2; |
| 1609 | } |
| 1610 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1611 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
| 1612 | * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules |
| 1613 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1614 | int |
| 1615 | 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] | 1616 | int implement, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1617 | { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1618 | const char *name, *mod_name, *mod_name_prev, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1619 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1620 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1621 | /* 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] | 1622 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1623 | |
| 1624 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1625 | |
| 1626 | id = nodeid; |
| 1627 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1628 | 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] | 1629 | return ((id - nodeid) - r) + 1; |
| 1630 | } |
| 1631 | id += r; |
| 1632 | |
| 1633 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1634 | return -1; |
| 1635 | } |
| 1636 | |
| 1637 | /* descendant-schema-nodeid */ |
| 1638 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1639 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1640 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1641 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1642 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1643 | /* absolute-schema-nodeid */ |
| 1644 | } else { |
| 1645 | 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] | 1646 | if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1647 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1648 | * itself, we don't want to search for the implemented module but augments |
| 1649 | * the module anyway. But when augmenting another module, we need the implemented |
| 1650 | * revision of the module if any */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1651 | aux_mod = lys_implemented_module(start_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1652 | if (!aux_mod->implemented && implement) { |
| 1653 | /* make the found module implemented */ |
| 1654 | if (lys_set_implemented(aux_mod)) { |
| 1655 | return -1; |
| 1656 | } |
| 1657 | } |
| 1658 | start_mod = aux_mod; |
| 1659 | implement++; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1660 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1661 | if (!start_mod) { |
| 1662 | return -1; |
| 1663 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1664 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | while (1) { |
| 1668 | sibling = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1669 | mod_name_prev = mod_name; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1670 | while ((sibling = lys_getnext(sibling, start_parent, start_mod, |
Michal Vasko | 5b99790 | 2017-04-03 14:16:22 +0200 | [diff] [blame] | 1671 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1672 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1673 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1674 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, implement, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1675 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1676 | *ret = sibling; |
| 1677 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1678 | } else if (r == 1) { |
| 1679 | continue; |
| 1680 | } else if (r == 2) { |
| 1681 | break; |
| 1682 | } else { |
| 1683 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1684 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* no match */ |
| 1689 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1690 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1691 | return EXIT_SUCCESS; |
| 1692 | } |
| 1693 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1694 | 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] | 1695 | return ((id - nodeid) - r) + 1; |
| 1696 | } |
| 1697 | id += r; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1698 | |
| 1699 | if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) || |
| 1700 | (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) { |
| 1701 | /* we are getting into another module (augment) */ |
| 1702 | if (implement) { |
| 1703 | /* we have to check that also target modules are implemented, if not, we have to change it */ |
| 1704 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1705 | if (!aux_mod) { |
| 1706 | return -1; |
| 1707 | } |
| 1708 | if (!aux_mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1709 | aux_mod = lys_implemented_module(aux_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1710 | if (!aux_mod->implemented) { |
| 1711 | /* make the found module implemented */ |
| 1712 | if (lys_set_implemented(aux_mod)) { |
| 1713 | return -1; |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | } else { |
| 1718 | /* we are not implementing the module itself, so the augments outside the module are ignored */ |
| 1719 | *ret = NULL; |
| 1720 | return EXIT_SUCCESS; |
| 1721 | } |
| 1722 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* cannot get here */ |
| 1726 | LOGINT; |
| 1727 | return -1; |
| 1728 | } |
| 1729 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1730 | /* unique, refine, |
| 1731 | * >0 - unexpected char on position (ret - 1), |
| 1732 | * 0 - ok (but ret can still be NULL), |
| 1733 | * -1 - error, |
| 1734 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1735 | int |
| 1736 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1737 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1738 | { |
| 1739 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1740 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1741 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1742 | /* 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] | 1743 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1744 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1745 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1746 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1747 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1748 | if (!start) { |
| 1749 | /* leaf not found */ |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1753 | id = nodeid; |
| 1754 | module = start->module; |
| 1755 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1756 | 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] | 1757 | return ((id - nodeid) - r) + 1; |
| 1758 | } |
| 1759 | id += r; |
| 1760 | |
| 1761 | if (!is_relative) { |
| 1762 | return -1; |
| 1763 | } |
| 1764 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1765 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1766 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1767 | start_parent = lys_parent(start_parent); |
| 1768 | } |
| 1769 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1770 | while (1) { |
| 1771 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1772 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1773 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1774 | /* name match */ |
| 1775 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1776 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, 0, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1777 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1778 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1779 | /* wrong node type, too bad */ |
| 1780 | continue; |
| 1781 | } |
| 1782 | *ret = sibling; |
| 1783 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1784 | } else if (r == 1) { |
| 1785 | continue; |
| 1786 | } else if (r == 2) { |
| 1787 | break; |
| 1788 | } else { |
| 1789 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1790 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* no match */ |
| 1795 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1796 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1797 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1798 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1799 | *ret = NULL; |
| 1800 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1803 | 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] | 1804 | return ((id - nodeid) - r) + 1; |
| 1805 | } |
| 1806 | id += r; |
| 1807 | } |
| 1808 | |
| 1809 | /* cannot get here */ |
| 1810 | LOGINT; |
| 1811 | return -1; |
| 1812 | } |
| 1813 | |
| 1814 | /* choice default */ |
| 1815 | int |
| 1816 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1817 | { |
| 1818 | /* cannot actually be a path */ |
| 1819 | if (strchr(nodeid, '/')) { |
| 1820 | return -1; |
| 1821 | } |
| 1822 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1823 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1827 | static int |
| 1828 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1829 | { |
| 1830 | const struct lys_module *module; |
| 1831 | const char *mod_prefix, *name; |
| 1832 | int i, mod_prefix_len, nam_len; |
| 1833 | |
| 1834 | /* parse the identifier, it must be parsed on one call */ |
| 1835 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1836 | return -i + 1; |
| 1837 | } |
| 1838 | |
| 1839 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1840 | if (!module) { |
| 1841 | return -1; |
| 1842 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 1843 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1844 | start = module->data; |
| 1845 | } |
| 1846 | |
| 1847 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1848 | |
| 1849 | return EXIT_SUCCESS; |
| 1850 | } |
| 1851 | |
| 1852 | int |
| 1853 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1854 | const struct lys_node **ret) |
| 1855 | { |
| 1856 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1857 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1858 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1859 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1860 | |
| 1861 | assert(nodeid && module && ret); |
| 1862 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1863 | |
| 1864 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1865 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1866 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1867 | 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] | 1868 | return ((id - nodeid) - r) + 1; |
| 1869 | } |
| 1870 | id += r; |
| 1871 | |
| 1872 | if (is_relative) { |
| 1873 | return -1; |
| 1874 | } |
| 1875 | |
| 1876 | 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] | 1877 | if (!abs_start_mod) { |
| 1878 | return -1; |
| 1879 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1880 | |
| 1881 | while (1) { |
| 1882 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1883 | while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1884 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1885 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1886 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1887 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, 0, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1888 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1889 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1890 | /* wrong node type, too bad */ |
| 1891 | continue; |
| 1892 | } |
| 1893 | *ret = sibling; |
| 1894 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1895 | } else if (r == 1) { |
| 1896 | continue; |
| 1897 | } else if (r == 2) { |
| 1898 | break; |
| 1899 | } else { |
| 1900 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1901 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | /* no match */ |
| 1906 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1907 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1908 | return EXIT_SUCCESS; |
| 1909 | } |
| 1910 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1911 | 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] | 1912 | return ((id - nodeid) - r) + 1; |
| 1913 | } |
| 1914 | id += r; |
| 1915 | } |
| 1916 | |
| 1917 | /* cannot get here */ |
| 1918 | LOGINT; |
| 1919 | return -1; |
| 1920 | } |
| 1921 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1922 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1923 | 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] | 1924 | { |
| 1925 | const char *name; |
| 1926 | int nam_len, has_predicate, i; |
| 1927 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1928 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1929 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1930 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1931 | return -1; |
| 1932 | } |
| 1933 | |
| 1934 | predicate += i; |
| 1935 | *parsed += i; |
| 1936 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1937 | if (!isdigit(name[0])) { |
| 1938 | for (i = 0; i < list->keys_size; ++i) { |
| 1939 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1940 | break; |
| 1941 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1942 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1943 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1944 | if (i == list->keys_size) { |
| 1945 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 1946 | return -1; |
| 1947 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* more predicates? */ |
| 1951 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1952 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1958 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1959 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1960 | 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] | 1961 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1962 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1963 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1964 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1965 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1966 | /* 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] | 1967 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1968 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1969 | assert(nodeid && (ctx || start)); |
| 1970 | if (!ctx) { |
| 1971 | ctx = start->module->ctx; |
| 1972 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1973 | |
| 1974 | id = nodeid; |
| 1975 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1976 | 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] | 1977 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1978 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1979 | } |
| 1980 | id += r; |
| 1981 | |
| 1982 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1983 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1984 | start_parent = start; |
| 1985 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 1986 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1987 | } |
| 1988 | module = start->module; |
| 1989 | } else { |
| 1990 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1991 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1992 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1993 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1994 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1995 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1996 | LOGINT; |
| 1997 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2000 | if (ly_buf_used && module_name[0]) { |
| 2001 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2002 | } |
| 2003 | ly_buf_used++; |
| 2004 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2005 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2006 | module_name[mod_name_len] = '\0'; |
| 2007 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 2008 | |
| 2009 | if (buf_backup) { |
| 2010 | /* return previous internal buffer content */ |
| 2011 | strcpy(module_name, buf_backup); |
| 2012 | free(buf_backup); |
| 2013 | buf_backup = NULL; |
| 2014 | } |
| 2015 | ly_buf_used--; |
| 2016 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2017 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2018 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2019 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2020 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2021 | return NULL; |
| 2022 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2023 | start_parent = NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2024 | |
| 2025 | /* now it's as if there was no module name */ |
| 2026 | mod_name = NULL; |
| 2027 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2030 | prev_mod = module; |
| 2031 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2032 | while (1) { |
| 2033 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2034 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2035 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2036 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2037 | 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] | 2038 | /* module check */ |
| 2039 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2040 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2041 | LOGINT; |
| 2042 | return NULL; |
| 2043 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2044 | |
| 2045 | if (ly_buf_used && module_name[0]) { |
| 2046 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2047 | } |
| 2048 | ly_buf_used++; |
| 2049 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2050 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2051 | module_name[mod_name_len] = '\0'; |
| 2052 | /* will also find an augment module */ |
| 2053 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2054 | |
| 2055 | if (buf_backup) { |
| 2056 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2057 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2058 | free(buf_backup); |
| 2059 | buf_backup = NULL; |
| 2060 | } |
| 2061 | ly_buf_used--; |
| 2062 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2063 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2064 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2065 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2066 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2067 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2068 | } |
| 2069 | } else { |
| 2070 | prefix_mod = prev_mod; |
| 2071 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2072 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2073 | continue; |
| 2074 | } |
| 2075 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2076 | /* do we have some predicates on it? */ |
| 2077 | if (has_predicate) { |
| 2078 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2079 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2080 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2081 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | } else if (sibling->nodetype == LYS_LIST) { |
| 2085 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2086 | return NULL; |
| 2087 | } |
| 2088 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2089 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2090 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2091 | } |
| 2092 | id += r; |
| 2093 | } |
| 2094 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2095 | /* the result node? */ |
| 2096 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2097 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2100 | /* move down the tree, if possible */ |
| 2101 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2102 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2103 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2104 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2105 | start_parent = sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* update prev mod */ |
Michal Vasko | d09a502 | 2017-03-08 13:14:40 +0100 | [diff] [blame] | 2108 | prev_mod = (start_parent ? start_parent->child->module : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | /* no match */ |
| 2114 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2115 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2116 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2117 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2118 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2119 | } |
| 2120 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2121 | 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] | 2122 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2123 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2124 | } |
| 2125 | id += r; |
| 2126 | } |
| 2127 | |
| 2128 | /* cannot get here */ |
| 2129 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2130 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2133 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2134 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
| 2135 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2136 | { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2137 | const char *name, *value, *key_val; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2138 | int nam_len, val_len, has_predicate = 1, r; |
| 2139 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2140 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2141 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2142 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2143 | assert(node->schema->nodetype == LYS_LIST); |
| 2144 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2145 | /* is the predicate a number? */ |
| 2146 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2147 | || !strncmp(name, ".", nam_len)) { |
| 2148 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2149 | return -1; |
| 2150 | } |
| 2151 | |
| 2152 | if (isdigit(name[0])) { |
| 2153 | if (position == atoi(name)) { |
| 2154 | /* match */ |
| 2155 | *parsed += r; |
| 2156 | return 0; |
| 2157 | } else { |
| 2158 | /* not a match */ |
| 2159 | return 1; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2164 | /* no keys in schema - causes an error later */ |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2168 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2169 | if (!key) { |
| 2170 | /* it is not a position, so we need a key for it to be a match */ |
| 2171 | return 1; |
| 2172 | } |
| 2173 | |
| 2174 | /* go through all the keys */ |
| 2175 | i = 0; |
| 2176 | goto check_parsed_values; |
| 2177 | |
| 2178 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2179 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2180 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2181 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2184 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2185 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2186 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2187 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2190 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2191 | predicate += r; |
| 2192 | *parsed += r; |
| 2193 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2194 | 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] | 2195 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2196 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2197 | } |
| 2198 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2199 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2200 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2201 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2202 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2203 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2204 | } else { |
| 2205 | key_val = key->value_str; |
| 2206 | } |
| 2207 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2208 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2209 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2210 | return 1; |
| 2211 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2212 | |
| 2213 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2217 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2218 | return -1; |
| 2219 | } |
| 2220 | |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2224 | /** |
| 2225 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2226 | * |
| 2227 | * @param[in] nodeid Node data path to find |
| 2228 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2229 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2230 | * @param[out] parsed Number of characters processed in \p id |
| 2231 | * @return The closes parent (or the node itself) from the path |
| 2232 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2233 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2234 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2235 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2236 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2237 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2238 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2239 | 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] | 2240 | 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] | 2241 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2242 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2243 | const struct lys_module *prefix_mod, *prev_mod; |
| 2244 | struct ly_ctx *ctx; |
| 2245 | |
| 2246 | assert(nodeid && start && parsed); |
| 2247 | |
| 2248 | ctx = start->schema->module->ctx; |
| 2249 | id = nodeid; |
| 2250 | |
| 2251 | 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] | 2252 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2253 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
| 2256 | id += r; |
| 2257 | /* add it to parsed only after the data node was actually found */ |
| 2258 | last_parsed = r; |
| 2259 | |
| 2260 | if (is_relative) { |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2261 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2262 | start = start->child; |
| 2263 | } else { |
| 2264 | for (; start->parent; start = start->parent); |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2265 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2269 | list_instance_position = 0; |
| 2270 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2271 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2272 | /* 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] | 2273 | if (lys_parent(sibling->schema)) { |
| 2274 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2275 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2276 | 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] | 2277 | *parsed = -1; |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | } else { |
| 2281 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2282 | 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] | 2283 | *parsed = -1; |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2289 | /* name match */ |
| 2290 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2291 | |
| 2292 | /* module check */ |
| 2293 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2294 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2295 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2296 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2297 | return NULL; |
| 2298 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2299 | |
| 2300 | if (ly_buf_used && module_name[0]) { |
| 2301 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2302 | } |
| 2303 | ly_buf_used++; |
| 2304 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2305 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2306 | module_name[mod_name_len] = '\0'; |
| 2307 | /* will also find an augment module */ |
| 2308 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2309 | |
| 2310 | if (buf_backup) { |
| 2311 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2312 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2313 | free(buf_backup); |
| 2314 | buf_backup = NULL; |
| 2315 | } |
| 2316 | ly_buf_used--; |
| 2317 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2318 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2319 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2320 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2321 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2322 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2323 | return NULL; |
| 2324 | } |
| 2325 | } else { |
| 2326 | prefix_mod = prev_mod; |
| 2327 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2328 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2329 | continue; |
| 2330 | } |
| 2331 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2332 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2333 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2334 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2335 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2336 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2337 | if (has_predicate) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2338 | 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] | 2339 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2340 | *parsed = -1; |
| 2341 | return NULL; |
| 2342 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2343 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2344 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2345 | *parsed = -1; |
| 2346 | return NULL; |
| 2347 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2348 | } else { |
| 2349 | r = 0; |
| 2350 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2351 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2355 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2356 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2357 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2358 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2359 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2360 | } else { |
| 2361 | data_val = llist->value_str; |
| 2362 | } |
| 2363 | |
| 2364 | if ((!llist_value && data_val && data_val[0]) |
| 2365 | || (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] | 2366 | continue; |
| 2367 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2368 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2369 | id += r; |
| 2370 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2371 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2372 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2373 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2374 | /* 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] | 2375 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2376 | /* none match */ |
| 2377 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2378 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2379 | |
| 2380 | ++list_instance_position; |
| 2381 | r = 0; |
| 2382 | 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] | 2383 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2384 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2385 | return NULL; |
| 2386 | } else if (ret == 1) { |
| 2387 | /* this list instance does not match */ |
| 2388 | continue; |
| 2389 | } |
| 2390 | id += r; |
| 2391 | last_parsed += r; |
| 2392 | } |
| 2393 | |
| 2394 | *parsed += last_parsed; |
| 2395 | |
| 2396 | /* the result node? */ |
| 2397 | if (!id[0]) { |
| 2398 | return sibling; |
| 2399 | } |
| 2400 | |
| 2401 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2402 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2403 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2404 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2405 | return NULL; |
| 2406 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2407 | last_match = sibling; |
Michal Vasko | fc11b68 | 2016-11-18 09:52:41 +0100 | [diff] [blame] | 2408 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2409 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | /* no match, return last match */ |
| 2415 | if (!sibling) { |
| 2416 | return last_match; |
| 2417 | } |
| 2418 | |
| 2419 | 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] | 2420 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2421 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2422 | return NULL; |
| 2423 | } |
| 2424 | id += r; |
| 2425 | last_parsed = r; |
| 2426 | } |
| 2427 | |
| 2428 | /* cannot get here */ |
| 2429 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2430 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2431 | return NULL; |
| 2432 | } |
| 2433 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2434 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2435 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2436 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2437 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2438 | * @param[in] str_restr Restriction as a string. |
| 2439 | * @param[in] type Type of the restriction. |
| 2440 | * @param[out] ret Final interval structure that starts with |
| 2441 | * the interval of the initial type, continues with intervals |
| 2442 | * of any superior types derived from the initial one, and |
| 2443 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2444 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2445 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2446 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2447 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2448 | 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] | 2449 | { |
| 2450 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2451 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2452 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2453 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2454 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2455 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2456 | 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] | 2457 | |
| 2458 | switch (type->base) { |
| 2459 | case LY_TYPE_BINARY: |
| 2460 | kind = 0; |
| 2461 | local_umin = 0; |
| 2462 | local_umax = 18446744073709551615UL; |
| 2463 | |
| 2464 | if (!str_restr && type->info.binary.length) { |
| 2465 | str_restr = type->info.binary.length->expr; |
| 2466 | } |
| 2467 | break; |
| 2468 | case LY_TYPE_DEC64: |
| 2469 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2470 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2471 | local_fmax = __INT64_C(9223372036854775807); |
| 2472 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2473 | |
| 2474 | if (!str_restr && type->info.dec64.range) { |
| 2475 | str_restr = type->info.dec64.range->expr; |
| 2476 | } |
| 2477 | break; |
| 2478 | case LY_TYPE_INT8: |
| 2479 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2480 | local_smin = __INT64_C(-128); |
| 2481 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2482 | |
| 2483 | if (!str_restr && type->info.num.range) { |
| 2484 | str_restr = type->info.num.range->expr; |
| 2485 | } |
| 2486 | break; |
| 2487 | case LY_TYPE_INT16: |
| 2488 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2489 | local_smin = __INT64_C(-32768); |
| 2490 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (!str_restr && type->info.num.range) { |
| 2493 | str_restr = type->info.num.range->expr; |
| 2494 | } |
| 2495 | break; |
| 2496 | case LY_TYPE_INT32: |
| 2497 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2498 | local_smin = __INT64_C(-2147483648); |
| 2499 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2500 | |
| 2501 | if (!str_restr && type->info.num.range) { |
| 2502 | str_restr = type->info.num.range->expr; |
| 2503 | } |
| 2504 | break; |
| 2505 | case LY_TYPE_INT64: |
| 2506 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2507 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2508 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2509 | |
| 2510 | if (!str_restr && type->info.num.range) { |
| 2511 | str_restr = type->info.num.range->expr; |
| 2512 | } |
| 2513 | break; |
| 2514 | case LY_TYPE_UINT8: |
| 2515 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2516 | local_umin = __UINT64_C(0); |
| 2517 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2518 | |
| 2519 | if (!str_restr && type->info.num.range) { |
| 2520 | str_restr = type->info.num.range->expr; |
| 2521 | } |
| 2522 | break; |
| 2523 | case LY_TYPE_UINT16: |
| 2524 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2525 | local_umin = __UINT64_C(0); |
| 2526 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2527 | |
| 2528 | if (!str_restr && type->info.num.range) { |
| 2529 | str_restr = type->info.num.range->expr; |
| 2530 | } |
| 2531 | break; |
| 2532 | case LY_TYPE_UINT32: |
| 2533 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2534 | local_umin = __UINT64_C(0); |
| 2535 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2536 | |
| 2537 | if (!str_restr && type->info.num.range) { |
| 2538 | str_restr = type->info.num.range->expr; |
| 2539 | } |
| 2540 | break; |
| 2541 | case LY_TYPE_UINT64: |
| 2542 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2543 | local_umin = __UINT64_C(0); |
| 2544 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2545 | |
| 2546 | if (!str_restr && type->info.num.range) { |
| 2547 | str_restr = type->info.num.range->expr; |
| 2548 | } |
| 2549 | break; |
| 2550 | case LY_TYPE_STRING: |
| 2551 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2552 | local_umin = __UINT64_C(0); |
| 2553 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2554 | |
| 2555 | if (!str_restr && type->info.str.length) { |
| 2556 | str_restr = type->info.str.length->expr; |
| 2557 | } |
| 2558 | break; |
| 2559 | default: |
| 2560 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2561 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2565 | if (type->der) { |
| 2566 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2567 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2568 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2569 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2570 | assert(!intv || (intv->kind == kind)); |
| 2571 | } |
| 2572 | |
| 2573 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2574 | /* we do not have any restriction, return superior ones */ |
| 2575 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2576 | return EXIT_SUCCESS; |
| 2577 | } |
| 2578 | |
| 2579 | /* adjust local min and max */ |
| 2580 | if (intv) { |
| 2581 | tmp_intv = intv; |
| 2582 | |
| 2583 | if (kind == 0) { |
| 2584 | local_umin = tmp_intv->value.uval.min; |
| 2585 | } else if (kind == 1) { |
| 2586 | local_smin = tmp_intv->value.sval.min; |
| 2587 | } else if (kind == 2) { |
| 2588 | local_fmin = tmp_intv->value.fval.min; |
| 2589 | } |
| 2590 | |
| 2591 | while (tmp_intv->next) { |
| 2592 | tmp_intv = tmp_intv->next; |
| 2593 | } |
| 2594 | |
| 2595 | if (kind == 0) { |
| 2596 | local_umax = tmp_intv->value.uval.max; |
| 2597 | } else if (kind == 1) { |
| 2598 | local_smax = tmp_intv->value.sval.max; |
| 2599 | } else if (kind == 2) { |
| 2600 | local_fmax = tmp_intv->value.fval.max; |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | /* finally parse our restriction */ |
| 2605 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2606 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2607 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2608 | if (!tmp_local_intv) { |
| 2609 | assert(!local_intv); |
| 2610 | local_intv = malloc(sizeof *local_intv); |
| 2611 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2612 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2613 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2614 | tmp_local_intv = tmp_local_intv->next; |
| 2615 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2616 | if (!tmp_local_intv) { |
| 2617 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2618 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2619 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2620 | |
| 2621 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2622 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2623 | tmp_local_intv->next = NULL; |
| 2624 | |
| 2625 | /* min */ |
| 2626 | ptr = seg_ptr; |
| 2627 | while (isspace(ptr[0])) { |
| 2628 | ++ptr; |
| 2629 | } |
| 2630 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2631 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2632 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2633 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2634 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2635 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2636 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2637 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2638 | goto error; |
| 2639 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2640 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2641 | } else if (!strncmp(ptr, "min", 3)) { |
| 2642 | if (kind == 0) { |
| 2643 | tmp_local_intv->value.uval.min = local_umin; |
| 2644 | } else if (kind == 1) { |
| 2645 | tmp_local_intv->value.sval.min = local_smin; |
| 2646 | } else if (kind == 2) { |
| 2647 | tmp_local_intv->value.fval.min = local_fmin; |
| 2648 | } |
| 2649 | |
| 2650 | ptr += 3; |
| 2651 | } else if (!strncmp(ptr, "max", 3)) { |
| 2652 | if (kind == 0) { |
| 2653 | tmp_local_intv->value.uval.min = local_umax; |
| 2654 | } else if (kind == 1) { |
| 2655 | tmp_local_intv->value.sval.min = local_smax; |
| 2656 | } else if (kind == 2) { |
| 2657 | tmp_local_intv->value.fval.min = local_fmax; |
| 2658 | } |
| 2659 | |
| 2660 | ptr += 3; |
| 2661 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2662 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2663 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | while (isspace(ptr[0])) { |
| 2667 | ptr++; |
| 2668 | } |
| 2669 | |
| 2670 | /* no interval or interval */ |
| 2671 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2672 | if (kind == 0) { |
| 2673 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2674 | } else if (kind == 1) { |
| 2675 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2676 | } else if (kind == 2) { |
| 2677 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2678 | } |
| 2679 | } else if (!strncmp(ptr, "..", 2)) { |
| 2680 | /* skip ".." */ |
| 2681 | ptr += 2; |
| 2682 | while (isspace(ptr[0])) { |
| 2683 | ++ptr; |
| 2684 | } |
| 2685 | |
| 2686 | /* max */ |
| 2687 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2688 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2689 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2690 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2691 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2692 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2693 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2694 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2695 | goto error; |
| 2696 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2697 | } |
| 2698 | } else if (!strncmp(ptr, "max", 3)) { |
| 2699 | if (kind == 0) { |
| 2700 | tmp_local_intv->value.uval.max = local_umax; |
| 2701 | } else if (kind == 1) { |
| 2702 | tmp_local_intv->value.sval.max = local_smax; |
| 2703 | } else if (kind == 2) { |
| 2704 | tmp_local_intv->value.fval.max = local_fmax; |
| 2705 | } |
| 2706 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2707 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2708 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2709 | } |
| 2710 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2711 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2712 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2715 | /* check min and max in correct order*/ |
| 2716 | if (kind == 0) { |
| 2717 | /* current segment */ |
| 2718 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2719 | goto error; |
| 2720 | } |
| 2721 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2722 | goto error; |
| 2723 | } |
| 2724 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2725 | 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] | 2726 | goto error; |
| 2727 | } |
| 2728 | } else if (kind == 1) { |
| 2729 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2730 | goto error; |
| 2731 | } |
| 2732 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2733 | goto error; |
| 2734 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2735 | 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] | 2736 | goto error; |
| 2737 | } |
| 2738 | } else if (kind == 2) { |
| 2739 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2740 | goto error; |
| 2741 | } |
| 2742 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2743 | goto error; |
| 2744 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2745 | 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] | 2746 | /* 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] | 2747 | goto error; |
| 2748 | } |
| 2749 | } |
| 2750 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2751 | /* next segment (next OR) */ |
| 2752 | seg_ptr = strchr(seg_ptr, '|'); |
| 2753 | if (!seg_ptr) { |
| 2754 | break; |
| 2755 | } |
| 2756 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2757 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | /* check local restrictions against superior ones */ |
| 2761 | if (intv) { |
| 2762 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2763 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2764 | |
| 2765 | while (tmp_local_intv && tmp_intv) { |
| 2766 | /* reuse local variables */ |
| 2767 | if (kind == 0) { |
| 2768 | local_umin = tmp_local_intv->value.uval.min; |
| 2769 | local_umax = tmp_local_intv->value.uval.max; |
| 2770 | |
| 2771 | /* it must be in this interval */ |
| 2772 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2773 | /* this interval is covered, next one */ |
| 2774 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2775 | tmp_local_intv = tmp_local_intv->next; |
| 2776 | continue; |
| 2777 | /* ascending order of restrictions -> fail */ |
| 2778 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2779 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | } else if (kind == 1) { |
| 2783 | local_smin = tmp_local_intv->value.sval.min; |
| 2784 | local_smax = tmp_local_intv->value.sval.max; |
| 2785 | |
| 2786 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2787 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2788 | tmp_local_intv = tmp_local_intv->next; |
| 2789 | continue; |
| 2790 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2791 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2792 | } |
| 2793 | } |
| 2794 | } else if (kind == 2) { |
| 2795 | local_fmin = tmp_local_intv->value.fval.min; |
| 2796 | local_fmax = tmp_local_intv->value.fval.max; |
| 2797 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2798 | 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] | 2799 | && (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] | 2800 | 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] | 2801 | tmp_local_intv = tmp_local_intv->next; |
| 2802 | continue; |
| 2803 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2804 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | tmp_intv = tmp_intv->next; |
| 2810 | } |
| 2811 | |
| 2812 | /* some interval left uncovered -> fail */ |
| 2813 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2814 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2815 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2816 | } |
| 2817 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2818 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2819 | if (intv) { |
| 2820 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2821 | tmp_intv->next = local_intv; |
| 2822 | } else { |
| 2823 | intv = local_intv; |
| 2824 | } |
| 2825 | *ret = intv; |
| 2826 | |
| 2827 | return EXIT_SUCCESS; |
| 2828 | |
| 2829 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2830 | while (intv) { |
| 2831 | tmp_intv = intv->next; |
| 2832 | free(intv); |
| 2833 | intv = tmp_intv; |
| 2834 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2835 | while (local_intv) { |
| 2836 | tmp_local_intv = local_intv->next; |
| 2837 | free(local_intv); |
| 2838 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2841 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2844 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2845 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2846 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2847 | * |
| 2848 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2849 | * @param[in] mod_name Typedef name module name. |
| 2850 | * @param[in] module Main module. |
| 2851 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2852 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2853 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2854 | * @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] | 2855 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2856 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2857 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2858 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2859 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2860 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2861 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2862 | int tpdf_size; |
| 2863 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2864 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2865 | /* no prefix, try built-in types */ |
| 2866 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2867 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2868 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2869 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2870 | } |
| 2871 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2875 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2876 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2877 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2881 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2882 | /* search in local typedefs */ |
| 2883 | while (parent) { |
| 2884 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2885 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2886 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2887 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2888 | break; |
| 2889 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2890 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2891 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2892 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2893 | break; |
| 2894 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2895 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2896 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2897 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2898 | break; |
| 2899 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2900 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2901 | case LYS_ACTION: |
| 2902 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2903 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2904 | break; |
| 2905 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2906 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2907 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2908 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2909 | break; |
| 2910 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2911 | case LYS_INPUT: |
| 2912 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2913 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2914 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2915 | break; |
| 2916 | |
| 2917 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2918 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2919 | continue; |
| 2920 | } |
| 2921 | |
| 2922 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2923 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2924 | match = &tpdf[i]; |
| 2925 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2929 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2930 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2931 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2932 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2933 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2934 | if (!module) { |
| 2935 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | /* search in top level typedefs */ |
| 2940 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2941 | 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] | 2942 | match = &module->tpdf[i]; |
| 2943 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2948 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2949 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2950 | 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] | 2951 | match = &module->inc[i].submodule->tpdf[j]; |
| 2952 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | } |
| 2956 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2957 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2958 | |
| 2959 | check_leafref: |
| 2960 | if (ret) { |
| 2961 | *ret = match; |
| 2962 | } |
| 2963 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2964 | while (!match->type.info.lref.path) { |
| 2965 | match = match->type.der; |
| 2966 | assert(match); |
| 2967 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2968 | } |
| 2969 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2972 | /** |
| 2973 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2974 | * |
| 2975 | * @param[in] type Type definition to use. |
| 2976 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2977 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2978 | * |
| 2979 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2980 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2981 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 2982 | check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2983 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2984 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2985 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2986 | const char *dflt = NULL; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2987 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2988 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2989 | assert(value); |
| 2990 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2991 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2992 | /* the type was not resolved yet, nothing to do for now */ |
| 2993 | return EXIT_FAILURE; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 2994 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 2995 | /* leafref in typedef cannot be checked */ |
| 2996 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2997 | } |
| 2998 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2999 | dflt = *value; |
| 3000 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3001 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3002 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3003 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3004 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3005 | break; |
| 3006 | } |
| 3007 | } |
| 3008 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3009 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3010 | /* no default value, nothing to check, all is well */ |
| 3011 | return EXIT_SUCCESS; |
| 3012 | } |
| 3013 | |
| 3014 | /* 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)? */ |
| 3015 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3016 | case LY_TYPE_IDENT: |
| 3017 | case LY_TYPE_INST: |
| 3018 | case LY_TYPE_LEAFREF: |
| 3019 | case LY_TYPE_BOOL: |
| 3020 | case LY_TYPE_EMPTY: |
| 3021 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3022 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3023 | case LY_TYPE_BITS: |
| 3024 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3025 | if (type->info.bits.count) { |
| 3026 | break; |
| 3027 | } |
| 3028 | return EXIT_SUCCESS; |
| 3029 | case LY_TYPE_ENUM: |
| 3030 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3031 | if (type->info.enums.count) { |
| 3032 | break; |
| 3033 | } |
| 3034 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3035 | case LY_TYPE_DEC64: |
| 3036 | if (type->info.dec64.range) { |
| 3037 | break; |
| 3038 | } |
| 3039 | return EXIT_SUCCESS; |
| 3040 | case LY_TYPE_BINARY: |
| 3041 | if (type->info.binary.length) { |
| 3042 | break; |
| 3043 | } |
| 3044 | return EXIT_SUCCESS; |
| 3045 | case LY_TYPE_INT8: |
| 3046 | case LY_TYPE_INT16: |
| 3047 | case LY_TYPE_INT32: |
| 3048 | case LY_TYPE_INT64: |
| 3049 | case LY_TYPE_UINT8: |
| 3050 | case LY_TYPE_UINT16: |
| 3051 | case LY_TYPE_UINT32: |
| 3052 | case LY_TYPE_UINT64: |
| 3053 | if (type->info.num.range) { |
| 3054 | break; |
| 3055 | } |
| 3056 | return EXIT_SUCCESS; |
| 3057 | case LY_TYPE_STRING: |
| 3058 | if (type->info.str.length || type->info.str.patterns) { |
| 3059 | break; |
| 3060 | } |
| 3061 | return EXIT_SUCCESS; |
| 3062 | case LY_TYPE_UNION: |
| 3063 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3064 | break; |
| 3065 | default: |
| 3066 | LOGINT; |
| 3067 | return -1; |
| 3068 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3069 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3070 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3071 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3072 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3073 | } |
| 3074 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3075 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3076 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3077 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3078 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3079 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3080 | if (!node.schema) { |
| 3081 | LOGMEM; |
| 3082 | return -1; |
| 3083 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3084 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3085 | if (!node.schema->name) { |
| 3086 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3087 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3088 | return -1; |
| 3089 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3090 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3091 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3092 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3093 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3094 | if (!type->info.lref.target) { |
| 3095 | ret = EXIT_FAILURE; |
| 3096 | goto finish; |
| 3097 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3098 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3099 | if (!ret) { |
| 3100 | /* adopt possibly changed default value to its canonical form */ |
| 3101 | if (*value) { |
| 3102 | *value = dflt; |
| 3103 | } |
| 3104 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3105 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3106 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, NULL, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3107 | /* possible forward reference */ |
| 3108 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3109 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3110 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3111 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3112 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3113 | /* we have refined bits/enums */ |
| 3114 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3115 | "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] | 3116 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3117 | } |
| 3118 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3119 | } else { |
| 3120 | /* success - adopt canonical form from the node into the default value */ |
| 3121 | if (dflt != node.value_str) { |
| 3122 | /* this can happen only if we have non-inherited default value, |
| 3123 | * inherited default values are already in canonical form */ |
| 3124 | assert(dflt == *value); |
| 3125 | *value = node.value_str; |
| 3126 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3127 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | finish: |
| 3131 | if (node.value_type == LY_TYPE_BITS) { |
| 3132 | free(node.value.bit); |
| 3133 | } |
| 3134 | free((char *)node.schema->name); |
| 3135 | free(node.schema); |
| 3136 | |
| 3137 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3138 | } |
| 3139 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3140 | /** |
| 3141 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3142 | * |
| 3143 | * @param[in] key The key to check. |
| 3144 | * @param[in] flags What flags to check. |
| 3145 | * @param[in] list The list of all the keys. |
| 3146 | * @param[in] index Index of the key in the key list. |
| 3147 | * @param[in] name The name of the keys. |
| 3148 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3149 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3150 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3151 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3152 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3153 | 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] | 3154 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3155 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3156 | char *dup = NULL; |
| 3157 | int j; |
| 3158 | |
| 3159 | /* existence */ |
| 3160 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3161 | if (name[len] != '\0') { |
| 3162 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3163 | if (!dup) { |
| 3164 | LOGMEM; |
| 3165 | return -1; |
| 3166 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3167 | dup[len] = '\0'; |
| 3168 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3169 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3170 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3171 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3172 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | /* uniqueness */ |
| 3176 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3177 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3178 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3179 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3184 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3185 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3186 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3190 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3191 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3192 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3196 | 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] | 3197 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3198 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3199 | } |
| 3200 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3201 | /* key is not placed from augment */ |
| 3202 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3203 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3204 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3205 | return -1; |
| 3206 | } |
| 3207 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3208 | /* key is not when/if-feature -conditional */ |
| 3209 | j = 0; |
| 3210 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3211 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3212 | 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] | 3213 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3214 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3217 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3218 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3219 | |
| 3220 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3221 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3222 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3223 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3224 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3225 | * |
| 3226 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3227 | */ |
| 3228 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3229 | 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] | 3230 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3231 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3232 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3233 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3234 | rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3235 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3236 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3237 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3238 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3239 | 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] | 3240 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3241 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3242 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3243 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3244 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3245 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3246 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3247 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3248 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3249 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3250 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3251 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3252 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3253 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3254 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | } |
| 3256 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3257 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3258 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3259 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3260 | return -1; |
| 3261 | } |
| 3262 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3263 | /* check that all unique's targets are of the same config type */ |
| 3264 | if (*trg_type) { |
| 3265 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3266 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3267 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3268 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3269 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3270 | return -1; |
| 3271 | } |
| 3272 | } else { |
| 3273 | /* first unique */ |
| 3274 | if (leaf->flags & LYS_CONFIG_W) { |
| 3275 | *trg_type = 1; |
| 3276 | } else { |
| 3277 | *trg_type = 2; |
| 3278 | } |
| 3279 | } |
| 3280 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3281 | /* set leaf's unique flag */ |
| 3282 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3283 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3284 | return EXIT_SUCCESS; |
| 3285 | |
| 3286 | error: |
| 3287 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3288 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3291 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3292 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3293 | { |
| 3294 | /* there are items after the one deleted */ |
| 3295 | if (i+1 < unres->count) { |
| 3296 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3297 | 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] | 3298 | |
| 3299 | /* deleting the last item */ |
| 3300 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3301 | free(unres->node); |
| 3302 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3306 | --unres->count; |
| 3307 | } |
| 3308 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3309 | /** |
| 3310 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3311 | * |
| 3312 | * @param[in] mod Module to search in. |
| 3313 | * @param[in] name Name of the data node. |
| 3314 | * @param[in] nam_len Length of the name. |
| 3315 | * @param[in] start Data node to start the search from. |
| 3316 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3317 | * they are replaced (!!) with the resolvents. |
| 3318 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3319 | * @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] | 3320 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3321 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3322 | 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] | 3323 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3324 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3325 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3326 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3327 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3328 | if (!parents->count) { |
| 3329 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3330 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3331 | if (!parents->node) { |
| 3332 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3333 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3334 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3335 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3336 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3337 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3338 | 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] | 3339 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3340 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3341 | continue; |
| 3342 | } |
| 3343 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3344 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3346 | && node->schema->name[nam_len] == '\0') { |
| 3347 | /* matching target */ |
| 3348 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3349 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3350 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | flag = 1; |
| 3352 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3353 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3354 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3355 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3356 | if (!parents->node) { |
| 3357 | return EXIT_FAILURE; |
| 3358 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3359 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3360 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3364 | |
| 3365 | if (!flag) { |
| 3366 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3367 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3368 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3369 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3370 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3373 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3376 | /** |
| 3377 | * @brief Resolve (find) a data node. Does not log. |
| 3378 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3379 | * @param[in] mod_name Module name of the data node. |
| 3380 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3381 | * @param[in] name Name of the data node. |
| 3382 | * @param[in] nam_len Length of the name. |
| 3383 | * @param[in] start Data node to start the search from. |
| 3384 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3385 | * they are replaced (!!) with the resolvents. |
| 3386 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3387 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3388 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3389 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3390 | 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] | 3391 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3392 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3393 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3394 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3395 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3396 | assert(start); |
| 3397 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3398 | if (mod_name) { |
| 3399 | /* we have mod_name, find appropriate module */ |
| 3400 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3401 | if (!str) { |
| 3402 | LOGMEM; |
| 3403 | return -1; |
| 3404 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3405 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3406 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3407 | if (!mod) { |
| 3408 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3409 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3410 | } |
| 3411 | } else { |
| 3412 | /* no prefix, module is the same as of current node */ |
| 3413 | mod = start->schema->module; |
| 3414 | } |
| 3415 | |
| 3416 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3419 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3420 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3421 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3422 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3423 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3424 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3425 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3426 | * without the predicate. Nodes not |
| 3427 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3428 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3429 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3430 | * @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] | 3431 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3433 | 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] | 3434 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3435 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3436 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3437 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3438 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3439 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3440 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3441 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3442 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3443 | |
| 3444 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3445 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3446 | if (!source_match.node) { |
| 3447 | LOGMEM; |
| 3448 | return -1; |
| 3449 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3450 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3451 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3452 | if (!dest_match.node) { |
| 3453 | LOGMEM; |
| 3454 | return -1; |
| 3455 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3456 | |
| 3457 | do { |
| 3458 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3459 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3460 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3461 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3462 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3463 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3464 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3465 | pred += i; |
| 3466 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3467 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3468 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3469 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3470 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3471 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3472 | 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] | 3473 | &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] | 3474 | i = 0; |
| 3475 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3479 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3480 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3481 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3482 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3483 | 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] | 3484 | rc = -1; |
| 3485 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3486 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3487 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3488 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3489 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3490 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3491 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3492 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3493 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3497 | 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] | 3498 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3499 | i = 0; |
| 3500 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | if (pke_len == pke_parsed) { |
| 3504 | break; |
| 3505 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3506 | 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] | 3507 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3508 | 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] | 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 | } |
| 3512 | pke_parsed += i; |
| 3513 | } |
| 3514 | |
| 3515 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3516 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3517 | while (leaf_dst && leaf_dst->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3518 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3519 | } |
| 3520 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3521 | while (leaf_src && leaf_src->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3522 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3523 | } |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3524 | if (!leaf_src || !leaf_dst) { |
| 3525 | /* not yet resolved leafrefs */ |
| 3526 | return EXIT_FAILURE; |
| 3527 | } |
Radek Krejci | c7408e7 | 2017-03-17 10:43:51 +0100 | [diff] [blame] | 3528 | if ((leaf_src->value_type & LY_DATA_TYPE_MASK) != (leaf_dst->value_type & LY_DATA_TYPE_MASK)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3529 | goto remove_leafref; |
| 3530 | } |
| 3531 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3532 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3533 | goto remove_leafref; |
| 3534 | } |
| 3535 | |
| 3536 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3537 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3538 | continue; |
| 3539 | |
| 3540 | remove_leafref: |
| 3541 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3542 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3543 | } |
| 3544 | } while (has_predicate); |
| 3545 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3546 | free(source_match.node); |
| 3547 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3548 | if (parsed) { |
| 3549 | *parsed = parsed_loc; |
| 3550 | } |
| 3551 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3552 | |
| 3553 | error: |
| 3554 | |
| 3555 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3556 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3557 | } |
| 3558 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3559 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3560 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3561 | if (parsed) { |
| 3562 | *parsed = -parsed_loc+i; |
| 3563 | } |
| 3564 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3567 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3568 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3569 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3570 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3571 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3572 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3573 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3574 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3575 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3576 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3577 | 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] | 3578 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3579 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3580 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3581 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3582 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3583 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3584 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3585 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3586 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3587 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3588 | |
| 3589 | /* searching for nodeset */ |
| 3590 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3591 | 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] | 3592 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3593 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3594 | goto error; |
| 3595 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3596 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3597 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3598 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3599 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3600 | if (parent_times > 0) { |
| 3601 | data = node; |
| 3602 | for (i = 1; i < parent_times; ++i) { |
| 3603 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3604 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3605 | } else if (!parent_times) { |
| 3606 | data = node->child; |
| 3607 | } else { |
| 3608 | /* absolute path */ |
| 3609 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3612 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3613 | if (data->prev) { |
| 3614 | while (data->prev->next) { |
| 3615 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3616 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
| 3620 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3621 | 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] | 3622 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3623 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3624 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3625 | goto error; |
| 3626 | } |
| 3627 | |
| 3628 | if (has_predicate) { |
| 3629 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3630 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3631 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3632 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3633 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3634 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3635 | continue; |
| 3636 | } |
| 3637 | |
| 3638 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3639 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3640 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3641 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3642 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3643 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3644 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3645 | goto error; |
| 3646 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3647 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3648 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3649 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3650 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3651 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3652 | goto error; |
| 3653 | } |
| 3654 | } |
| 3655 | } while (path[0] != '\0'); |
| 3656 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3657 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3658 | |
| 3659 | error: |
| 3660 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3661 | free(ret->node); |
| 3662 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3663 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3664 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3665 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3668 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3669 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3670 | { |
| 3671 | int dep1, dep2; |
| 3672 | const struct lys_node *node; |
| 3673 | |
| 3674 | if (lys_parent(op_node)) { |
| 3675 | /* inner operation (notif/action) */ |
| 3676 | if (abs_path) { |
| 3677 | return 1; |
| 3678 | } else { |
| 3679 | /* compare depth of both nodes */ |
| 3680 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3681 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3682 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3683 | return 1; |
| 3684 | } |
| 3685 | } |
| 3686 | } else { |
| 3687 | /* top-level operation (notif/rpc) */ |
| 3688 | if (op_node != first_node) { |
| 3689 | return 1; |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | return 0; |
| 3694 | } |
| 3695 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3696 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3697 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3698 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3699 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3700 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3701 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3702 | * @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] | 3703 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3704 | * @return 0 on forward reference, otherwise the number |
| 3705 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3706 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3707 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3708 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3709 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3710 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3711 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3712 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3713 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3714 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3715 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3716 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3717 | |
| 3718 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3719 | 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] | 3720 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3721 | 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] | 3722 | return -parsed+i; |
| 3723 | } |
| 3724 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3725 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3726 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3727 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3728 | if (sour_pref) { |
| 3729 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3730 | } else { |
| 3731 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3732 | } |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3733 | 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] | 3734 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3735 | 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] | 3736 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3740 | dest_parent_times = 0; |
| 3741 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3742 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3743 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3744 | 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] | 3745 | return -parsed; |
| 3746 | } |
| 3747 | pke_parsed += i; |
| 3748 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3749 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3750 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3751 | * all schema nodes that cannot be instantiated in data tree */ |
| 3752 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3753 | 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] | 3754 | dst_node = lys_parent(dst_node)); |
| 3755 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3756 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3757 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3758 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3759 | } |
| 3760 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3761 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3762 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3763 | if (dest_pref) { |
| 3764 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3765 | } else { |
| 3766 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3767 | } |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3768 | 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] | 3769 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3770 | 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] | 3771 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3772 | } |
| 3773 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3774 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3775 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3776 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3777 | } |
| 3778 | first_iter = 0; |
| 3779 | } |
| 3780 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3781 | if (pke_len == pke_parsed) { |
| 3782 | break; |
| 3783 | } |
| 3784 | |
| 3785 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3786 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3787 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3788 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3789 | return -parsed; |
| 3790 | } |
| 3791 | pke_parsed += i; |
| 3792 | } |
| 3793 | |
| 3794 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3795 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3796 | 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] | 3797 | 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] | 3798 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3799 | return -parsed; |
| 3800 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3801 | } while (has_predicate); |
| 3802 | |
| 3803 | return parsed; |
| 3804 | } |
| 3805 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3806 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3807 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3808 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3809 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3810 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3811 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3812 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3813 | * @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] | 3814 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3815 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3816 | resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3817 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3818 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3819 | const struct lys_module *mod; |
| 3820 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3821 | const char *id, *prefix, *name; |
| 3822 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3823 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3824 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3825 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3826 | parent_times = 0; |
| 3827 | id = path; |
| 3828 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3829 | /* find operation schema we are in */ |
| 3830 | for (op_node = lys_parent(parent); |
| 3831 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3832 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3833 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3834 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3835 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3836 | if ((i = parse_path_arg(mod_start, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3837 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3838 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3839 | } |
| 3840 | id += i; |
| 3841 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3842 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3843 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3844 | /* resolve prefix of the module */ |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3845 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3846 | if (!mod) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3847 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3848 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3849 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3850 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3851 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3852 | if (!mod->implemented) { |
| 3853 | /* make the found module implemented */ |
| 3854 | if (lys_set_implemented(mod)) { |
| 3855 | return EXIT_FAILURE; |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | /* get start node */ |
| 3860 | if (!mod->data) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3861 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3862 | return EXIT_FAILURE; |
| 3863 | } |
| 3864 | node = mod->data; |
| 3865 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3866 | } else if (parent_times > 0) { |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3867 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3868 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3869 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3870 | * all schema nodes that cannot be instantiated in data tree */ |
| 3871 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3872 | 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] | 3873 | node = lys_parent(node)); |
| 3874 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3875 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3876 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3877 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3878 | } |
| 3879 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3880 | |
| 3881 | /* now we have to check that if we are going into a node from a different module, |
| 3882 | * the module is implemented (so its augments are applied) */ |
| 3883 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3884 | if (!mod) { |
| 3885 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3886 | return EXIT_FAILURE; |
| 3887 | } |
| 3888 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3889 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3890 | if (!mod->implemented) { |
| 3891 | /* make the found module implemented */ |
| 3892 | if (lys_set_implemented(mod)) { |
| 3893 | return EXIT_FAILURE; |
| 3894 | } |
| 3895 | } |
| 3896 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3897 | } else { |
| 3898 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3899 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3900 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3901 | } else { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3902 | /* we have to first check that the module we are going into is implemented */ |
| 3903 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3904 | if (!mod) { |
| 3905 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3906 | return EXIT_FAILURE; |
| 3907 | } |
| 3908 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3909 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3910 | if (!mod->implemented) { |
| 3911 | /* make the found module implemented */ |
| 3912 | if (lys_set_implemented(mod)) { |
| 3913 | return EXIT_FAILURE; |
| 3914 | } |
| 3915 | } |
| 3916 | } |
| 3917 | |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3918 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3919 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3920 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, name[0], name); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3921 | return -1; |
| 3922 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3923 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3924 | if (!node) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3925 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3926 | return EXIT_FAILURE; |
| 3927 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3928 | } |
| 3929 | |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3930 | rc = lys_get_data_sibling(mod, node, name, nam_len, LYS_LIST | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF |
| 3931 | | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3932 | if (rc) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3933 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3934 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3935 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3936 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3937 | if (first_iter) { |
| 3938 | /* set external dependency flag, we can decide based on the first found node */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3939 | if (op_node && parent_times && |
| 3940 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3941 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3942 | } |
| 3943 | first_iter = 0; |
| 3944 | } |
| 3945 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3946 | if (has_predicate) { |
| 3947 | /* we have predicate, so the current result must be list */ |
| 3948 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3949 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3950 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3951 | } |
| 3952 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3953 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3954 | if (i <= 0) { |
| 3955 | if (i == 0) { |
| 3956 | return EXIT_FAILURE; |
| 3957 | } else { /* i < 0 */ |
| 3958 | return -1; |
| 3959 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3960 | } |
| 3961 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3962 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3963 | } |
| 3964 | } while (id[0]); |
| 3965 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3966 | /* 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] | 3967 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3968 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3969 | 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] | 3970 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3973 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3974 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3975 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3976 | return -1; |
| 3977 | } |
| 3978 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3979 | if (ret) { |
| 3980 | *ret = node; |
| 3981 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3982 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3983 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3986 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3987 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3988 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3989 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3990 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3991 | * @param[in,out] node_match Nodes matching the restriction without |
| 3992 | * the predicate. Nodes not satisfying |
| 3993 | * the predicate are removed. |
| 3994 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3995 | * @return Number of characters successfully parsed, |
| 3996 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3997 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3998 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3999 | resolve_instid_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4000 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4001 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4002 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4003 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4004 | 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] | 4005 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4006 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4007 | assert(pred && node_match->count); |
| 4008 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4009 | idx = -1; |
| 4010 | parsed = 0; |
| 4011 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4012 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4013 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4014 | 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] | 4015 | return -parsed+i; |
| 4016 | } |
| 4017 | parsed += i; |
| 4018 | pred += i; |
| 4019 | |
| 4020 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4021 | /* pos */ |
| 4022 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4023 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4024 | } else if (name[0] != '.') { |
| 4025 | /* list keys */ |
| 4026 | if (pred_iter < 0) { |
| 4027 | pred_iter = 1; |
| 4028 | } else { |
| 4029 | ++pred_iter; |
| 4030 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4033 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4034 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4035 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4036 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4037 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4038 | goto remove_instid; |
| 4039 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4040 | |
| 4041 | target = node_match->node[j]; |
| 4042 | /* check the value */ |
| 4043 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4044 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4045 | goto remove_instid; |
| 4046 | } |
| 4047 | |
| 4048 | } else if (!value) { |
| 4049 | /* keyless list position */ |
| 4050 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4051 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4052 | goto remove_instid; |
| 4053 | } |
| 4054 | |
| 4055 | if (idx != cur_idx) { |
| 4056 | goto remove_instid; |
| 4057 | } |
| 4058 | |
| 4059 | } else { |
| 4060 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4061 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4062 | goto remove_instid; |
| 4063 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4064 | |
| 4065 | /* key module must match the list module */ |
| 4066 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4067 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4068 | goto remove_instid; |
| 4069 | } |
| 4070 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4071 | 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] | 4072 | if (!target) { |
| 4073 | goto remove_instid; |
| 4074 | } |
| 4075 | if ((struct lys_node_leaf *)target->schema != |
| 4076 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4077 | goto remove_instid; |
| 4078 | } |
| 4079 | |
| 4080 | /* check the value */ |
| 4081 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4082 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4083 | goto remove_instid; |
| 4084 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4085 | } |
| 4086 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4087 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4088 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4089 | continue; |
| 4090 | |
| 4091 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4092 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4093 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4094 | } |
| 4095 | } while (has_predicate); |
| 4096 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4097 | /* check that all list keys were specified */ |
| 4098 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4099 | j = 0; |
| 4100 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4101 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4102 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4103 | /* not enough predicates, just remove the list instance */ |
| 4104 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4105 | } else { |
| 4106 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | if (!node_match->count) { |
| 4111 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4112 | } |
| 4113 | } |
| 4114 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4115 | return parsed; |
| 4116 | } |
| 4117 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4118 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4119 | 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] | 4120 | { |
| 4121 | struct lys_node *parent, *elem; |
| 4122 | struct lyxp_set set; |
| 4123 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4124 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4125 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4126 | if (check_place) { |
| 4127 | parent = node; |
| 4128 | while (parent) { |
| 4129 | if (parent->nodetype == LYS_GROUPING) { |
| 4130 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4131 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4132 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4133 | if (parent->nodetype == LYS_AUGMENT) { |
| 4134 | if (!((struct lys_node_augment *)parent)->target) { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4135 | /* unresolved augment */ |
| 4136 | if (parent->module->implemented) { |
| 4137 | /* skip for now (will be checked later) */ |
| 4138 | return EXIT_FAILURE; |
| 4139 | } else { |
| 4140 | /* not implemented augment, skip resolving */ |
| 4141 | return EXIT_SUCCESS; |
| 4142 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4143 | } else { |
| 4144 | parent = ((struct lys_node_augment *)parent)->target; |
| 4145 | continue; |
| 4146 | } |
| 4147 | } |
| 4148 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4149 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4150 | } |
| 4151 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4152 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4153 | if (ret == -1) { |
| 4154 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4158 | |
| 4159 | for (i = 0; i < set.used; ++i) { |
| 4160 | /* skip roots'n'stuff */ |
| 4161 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4162 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4163 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4164 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4165 | return -1; |
| 4166 | } |
| 4167 | |
| 4168 | if (parent) { |
| 4169 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4170 | if (!elem) { |
| 4171 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4172 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4180 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4181 | } |
| 4182 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4183 | static int |
| 4184 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4185 | { |
| 4186 | int i; |
| 4187 | |
| 4188 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4189 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4190 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4191 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.", |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4192 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4193 | return -1; |
| 4194 | } |
| 4195 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4196 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4197 | } else if (type->base == LY_TYPE_UNION) { |
| 4198 | for (i = 0; i < type->info.uni.count; i++) { |
| 4199 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4200 | return -1; |
| 4201 | } |
| 4202 | } |
| 4203 | } |
| 4204 | return 0; |
| 4205 | } |
| 4206 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4207 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4208 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4209 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4210 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4211 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4212 | * @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] | 4213 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4214 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4215 | * |
| 4216 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4217 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4218 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4219 | 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] | 4220 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4221 | struct lys_node_leaf *leaf; |
| 4222 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4223 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4224 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4225 | if (clear) { |
| 4226 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4227 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4228 | } else { |
| 4229 | if (node->flags & LYS_CONFIG_SET) { |
| 4230 | /* skip nodes with an explicit config value */ |
| 4231 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4232 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4233 | 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] | 4234 | return -1; |
| 4235 | } |
| 4236 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4237 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4238 | |
| 4239 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4240 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4241 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4242 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4243 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4244 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4245 | return -1; |
| 4246 | } |
| 4247 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4248 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4249 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4250 | if (inherit_config_flag(node->child, flags, clear, unres)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4251 | return -1; |
| 4252 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4253 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4254 | leaf = (struct lys_node_leaf *)node; |
| 4255 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4256 | return -1; |
| 4257 | } |
| 4258 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4259 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4260 | |
| 4261 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4262 | } |
| 4263 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4264 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4265 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4266 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4267 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4268 | * @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] | 4269 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4270 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4271 | * @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] | 4272 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4273 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4274 | 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] | 4275 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4276 | int rc, clear_config; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4277 | unsigned int u; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4278 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4279 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4280 | struct lys_module *mod; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4281 | struct lys_ext_instance *ext; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4282 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4283 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4284 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4285 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4286 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4287 | 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] | 4288 | if (rc == -1) { |
| 4289 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4290 | } else if (rc > 0) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4291 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4292 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4293 | } else if (rc == 0 && aug->target) { |
| 4294 | /* augment was resolved as a side effect of setting module implemented when |
| 4295 | * resolving augment schema nodeid, so we are done here */ |
| 4296 | return 0; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4297 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4298 | if (!aug_target && mod->implemented) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4299 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4300 | return EXIT_FAILURE; |
| 4301 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4302 | /* check that we want to connect augment into its target */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4303 | if (!mod->implemented) { |
| 4304 | /* it must be augment only to the same module, |
| 4305 | * otherwise we do not apply augment in not-implemented |
| 4306 | * module. If the module is set to be implemented in future, |
| 4307 | * the augment is being resolved and checked again */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4308 | if (!aug_target) { |
| 4309 | /* target was not even resolved */ |
| 4310 | return EXIT_SUCCESS; |
| 4311 | } |
| 4312 | /* target was resolved, but it may refer another module */ |
| 4313 | for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4314 | if (lys_node_module(sub) != mod) { |
| 4315 | /* this is not an implemented module and the augment |
| 4316 | * target some other module, so avoid its connecting |
| 4317 | * to the target */ |
| 4318 | return EXIT_SUCCESS; |
| 4319 | } |
| 4320 | } |
| 4321 | } |
| 4322 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4323 | if (!aug->child) { |
Radek Krejci | 9bcb9f4 | 2017-04-06 12:57:01 +0200 | [diff] [blame] | 4324 | /* empty augment, warn about it ... */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4325 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 9bcb9f4 | 2017-04-06 12:57:01 +0200 | [diff] [blame] | 4326 | /* ... but use it, since it has no child, no reconnection work is needed */ |
| 4327 | aug->target = (struct lys_node *)aug_target; |
| 4328 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4329 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4332 | /* check for mandatory nodes - if the target node is in another module |
| 4333 | * the added nodes cannot be mandatory |
| 4334 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4335 | 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] | 4336 | && (rc = lyp_check_mandatory_augment(aug, aug_target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4337 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4338 | } |
| 4339 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4340 | /* check augment target type and then augment nodes type */ |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4341 | if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 4342 | LY_TREE_FOR(aug->child, sub) { |
| 4343 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4344 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4345 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4346 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
| 4347 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
| 4348 | return -1; |
| 4349 | } |
| 4350 | } |
| 4351 | } 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] | 4352 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4353 | 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] | 4354 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4355 | 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] | 4356 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4357 | return -1; |
| 4358 | } |
| 4359 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4360 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4361 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4362 | 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] | 4363 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4364 | 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] | 4365 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4366 | return -1; |
| 4367 | } |
| 4368 | } |
| 4369 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4370 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4371 | 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] | 4372 | return -1; |
| 4373 | } |
| 4374 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4375 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4376 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4377 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4378 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4379 | } |
| 4380 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4381 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4382 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4383 | * by setting aug->target we know the augment is fully resolved now */ |
| 4384 | aug->target = (struct lys_node *)aug_target; |
| 4385 | if (aug->target->child) { |
| 4386 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4387 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4388 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4389 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4390 | } else { |
| 4391 | aug->target->child = aug->child; |
| 4392 | } |
| 4393 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4394 | /* inherit config information from actual parent */ |
| 4395 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4396 | clear_config = (parent) ? 1 : 0; |
| 4397 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4398 | 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] | 4399 | return -1; |
| 4400 | } |
| 4401 | } |
| 4402 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4403 | /* inherit extensions if any */ |
| 4404 | for (u = 0; u < aug->target->ext_size; u++) { |
| 4405 | ext = aug->target->ext[u]; /* shortcut */ |
| 4406 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 4407 | if (unres_schema_add_node(mod, unres, &ext, UNRES_EXT_FINALIZE, NULL) == -1) { |
| 4408 | /* something really bad happend since the extension finalization is not actually |
| 4409 | * being resolved while adding into unres, so something more serious with the unres |
| 4410 | * list itself must happened */ |
| 4411 | return -1; |
| 4412 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 4413 | } |
| 4414 | } |
| 4415 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4416 | success: |
| 4417 | if (mod->implemented) { |
| 4418 | /* make target modules also implemented */ |
| 4419 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4420 | if (lys_set_implemented(sub->module)) { |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 4421 | LOGERR(ly_errno, "Setting the augmented module \"%s\" implemented failed.", sub->module->name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4422 | return -1; |
| 4423 | } |
| 4424 | } |
| 4425 | } |
| 4426 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4427 | return EXIT_SUCCESS; |
| 4428 | } |
| 4429 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4430 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4431 | 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] | 4432 | { |
| 4433 | enum LY_VLOG_ELEM vlog_type; |
| 4434 | void *vlog_node; |
| 4435 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4436 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4437 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4438 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4439 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4440 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4441 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4442 | |
| 4443 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4444 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4445 | vlog_node = info->parent; |
| 4446 | vlog_type = LY_VLOG_LYS; |
| 4447 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4448 | case LYEXT_PAR_MODULE: |
| 4449 | case LYEXT_PAR_IMPORT: |
| 4450 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4451 | vlog_node = NULL; |
| 4452 | vlog_type = LY_VLOG_LYS; |
| 4453 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4454 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4455 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4456 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4457 | break; |
| 4458 | } |
| 4459 | |
| 4460 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4461 | /* YIN */ |
| 4462 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4463 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4464 | 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] | 4465 | if (!mod) { |
| 4466 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4467 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | /* find the extension definition */ |
| 4471 | e = NULL; |
| 4472 | for (i = 0; i < mod->extensions_size; i++) { |
| 4473 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4474 | e = &mod->extensions[i]; |
| 4475 | break; |
| 4476 | } |
| 4477 | } |
| 4478 | /* try submodules */ |
| 4479 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4480 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4481 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4482 | e = &mod->inc[j].submodule->extensions[i]; |
| 4483 | break; |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | if (!e) { |
| 4488 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4489 | return EXIT_FAILURE; |
| 4490 | } |
| 4491 | |
| 4492 | /* 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] | 4493 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4494 | if (e->plugin && e->plugin->check_position) { |
| 4495 | /* common part - we have plugin with position checking function, use it first */ |
| 4496 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4497 | /* extension is not allowed here */ |
| 4498 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4499 | return -1; |
| 4500 | } |
| 4501 | } |
| 4502 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4503 | /* extension type-specific part - allocation */ |
| 4504 | if (e->plugin) { |
| 4505 | etype = e->plugin->type; |
| 4506 | } else { |
| 4507 | /* default type */ |
| 4508 | etype = LYEXT_FLAG; |
| 4509 | } |
| 4510 | switch (etype) { |
| 4511 | case LYEXT_FLAG: |
| 4512 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4513 | break; |
| 4514 | case LYEXT_COMPLEX: |
| 4515 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4516 | break; |
| 4517 | case LYEXT_ERR: |
| 4518 | /* we never should be here */ |
| 4519 | LOGINT; |
| 4520 | return -1; |
| 4521 | } |
| 4522 | |
| 4523 | /* common part for all extension types */ |
| 4524 | (*ext)->def = e; |
| 4525 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4526 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4527 | (*ext)->insubstmt = info->substmt; |
| 4528 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4529 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4530 | |
| 4531 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4532 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4533 | if (!(*ext)->arg_value) { |
| 4534 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4535 | return -1; |
| 4536 | } |
| 4537 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4538 | } |
| 4539 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4540 | (*ext)->nodetype = LYS_EXT; |
| 4541 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4542 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4543 | /* extension type-specific part - parsing content */ |
| 4544 | switch (etype) { |
| 4545 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4546 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4547 | if (!yin->ns) { |
| 4548 | /* garbage */ |
| 4549 | lyxml_free(mod->ctx, yin); |
| 4550 | continue; |
| 4551 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4552 | /* standard YANG statements are not expected here */ |
| 4553 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4554 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4555 | } else if (yin->ns == info->data.yin->ns && |
| 4556 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4557 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4558 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4559 | 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] | 4560 | return -1; |
| 4561 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4562 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4563 | yin->content = NULL; |
| 4564 | lyxml_free(mod->ctx, yin); |
| 4565 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4566 | /* extension instance */ |
| 4567 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4568 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4569 | return -1; |
| 4570 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4571 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4572 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4573 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4574 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4575 | break; |
| 4576 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4577 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4578 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4579 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4580 | return -1; |
| 4581 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4582 | break; |
| 4583 | default: |
| 4584 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4585 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4586 | |
| 4587 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4588 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4589 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4590 | /* YANG */ |
| 4591 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4592 | ext_prefix = (char *)(*ext)->def; |
| 4593 | tmp = strchr(ext_prefix, ':'); |
| 4594 | if (!tmp) { |
| 4595 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4596 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4597 | } |
| 4598 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4599 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4600 | /* get the module where the extension is supposed to be defined */ |
| 4601 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4602 | if (!mod) { |
| 4603 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4604 | return EXIT_FAILURE; |
| 4605 | } |
| 4606 | |
| 4607 | /* find the extension definition */ |
| 4608 | e = NULL; |
| 4609 | for (i = 0; i < mod->extensions_size; i++) { |
| 4610 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4611 | e = &mod->extensions[i]; |
| 4612 | break; |
| 4613 | } |
| 4614 | } |
| 4615 | /* try submodules */ |
| 4616 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4617 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4618 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4619 | e = &mod->inc[j].submodule->extensions[i]; |
| 4620 | break; |
| 4621 | } |
| 4622 | } |
| 4623 | } |
| 4624 | if (!e) { |
| 4625 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4626 | return EXIT_FAILURE; |
| 4627 | } |
| 4628 | |
| 4629 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4630 | |
| 4631 | if (e->plugin && e->plugin->check_position) { |
| 4632 | /* common part - we have plugin with position checking function, use it first */ |
| 4633 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4634 | /* extension is not allowed here */ |
| 4635 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4636 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4640 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4641 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4642 | (*ext)->def = e; |
| 4643 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4644 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4645 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4646 | if (e->argument && !(*ext)->arg_value) { |
| 4647 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4648 | goto error; |
| 4649 | } |
| 4650 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4651 | (*ext)->module = info->mod; |
| 4652 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4653 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4654 | /* extension type-specific part */ |
| 4655 | if (e->plugin) { |
| 4656 | etype = e->plugin->type; |
| 4657 | } else { |
| 4658 | /* default type */ |
| 4659 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4660 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4661 | switch (etype) { |
| 4662 | case LYEXT_FLAG: |
| 4663 | /* nothing change */ |
| 4664 | break; |
| 4665 | case LYEXT_COMPLEX: |
| 4666 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4667 | if (!tmp_ext) { |
| 4668 | LOGMEM; |
| 4669 | goto error; |
| 4670 | } |
| 4671 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4672 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4673 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4674 | if (info->data.yang) { |
| 4675 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4676 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4677 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4678 | goto error; |
| 4679 | } |
| 4680 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4681 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4682 | goto error; |
| 4683 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4684 | } |
| 4685 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4686 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4687 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4688 | break; |
| 4689 | case LYEXT_ERR: |
| 4690 | /* we never should be here */ |
| 4691 | LOGINT; |
| 4692 | goto error; |
| 4693 | } |
| 4694 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4695 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4696 | goto error; |
| 4697 | } |
| 4698 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4702 | error: |
| 4703 | free(ext_prefix); |
| 4704 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4705 | } |
| 4706 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4707 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4708 | * @brief Resolve (find) choice default case. Does not log. |
| 4709 | * |
| 4710 | * @param[in] choic Choice to use. |
| 4711 | * @param[in] dflt Name of the default case. |
| 4712 | * |
| 4713 | * @return Pointer to the default node or NULL. |
| 4714 | */ |
| 4715 | static struct lys_node * |
| 4716 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4717 | { |
| 4718 | struct lys_node *child, *ret; |
| 4719 | |
| 4720 | LY_TREE_FOR(choic->child, child) { |
| 4721 | if (child->nodetype == LYS_USES) { |
| 4722 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4723 | if (ret) { |
| 4724 | return ret; |
| 4725 | } |
| 4726 | } |
| 4727 | |
| 4728 | 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] | 4729 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4730 | return child; |
| 4731 | } |
| 4732 | } |
| 4733 | |
| 4734 | return NULL; |
| 4735 | } |
| 4736 | |
| 4737 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4738 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4739 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4740 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4741 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4742 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4743 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4744 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4745 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4746 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4747 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4748 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4749 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4750 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4751 | struct lys_node_leaflist *llist; |
| 4752 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4753 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4754 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4755 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4756 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4757 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4758 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4759 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4760 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4761 | |
| 4762 | /* HACK just check that the grouping is resolved |
| 4763 | * - the higher byte in flags is always empty in grouping (no flags there apply to the groupings) |
| 4764 | * so we use it to count unresolved uses inside the grouping */ |
| 4765 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 4766 | assert(!((uint8_t*)&uses->grp->flags)[1]); |
| 4767 | #else |
| 4768 | assert(!((uint8_t*)&uses->grp->flags)[0]); |
| 4769 | #endif |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4770 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4771 | if (!uses->grp->child) { |
| 4772 | /* grouping without children, warning was already displayed */ |
| 4773 | return EXIT_SUCCESS; |
| 4774 | } |
| 4775 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4776 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4777 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4778 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4779 | /* do not instantiate groupings from groupings */ |
| 4780 | continue; |
| 4781 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4782 | 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] | 4783 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4784 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4785 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4786 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4787 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4788 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4789 | LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 4790 | 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] | 4791 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4792 | } |
| 4793 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4794 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4795 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4796 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4797 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4798 | if (uses->refine_size) { |
| 4799 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4800 | if (!refine_nodes) { |
| 4801 | LOGMEM; |
| 4802 | goto fail; |
| 4803 | } |
| 4804 | } |
| 4805 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4806 | /* apply refines */ |
| 4807 | for (i = 0; i < uses->refine_size; i++) { |
| 4808 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4809 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4810 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4811 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4812 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4813 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4814 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4815 | } |
| 4816 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4817 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4818 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4819 | 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] | 4820 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4821 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4822 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4823 | |
| 4824 | /* description on any nodetype */ |
| 4825 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4826 | lydict_remove(ctx, node->dsc); |
| 4827 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | /* reference on any nodetype */ |
| 4831 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4832 | lydict_remove(ctx, node->ref); |
| 4833 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4834 | } |
| 4835 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4836 | /* config on any nodetype, |
| 4837 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4838 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4839 | node->flags &= ~LYS_CONFIG_MASK; |
| 4840 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4844 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4845 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4846 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4847 | leaf = (struct lys_node_leaf *)node; |
| 4848 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4849 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4850 | lydict_remove(ctx, leaf->dflt); |
| 4851 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4852 | |
| 4853 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4854 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4855 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4856 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4857 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4858 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4859 | /* leaf-list */ |
| 4860 | llist = (struct lys_node_leaflist *)node; |
| 4861 | |
| 4862 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4863 | for (j = 0; j < llist->dflt_size; j++) { |
| 4864 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4865 | } |
| 4866 | free(llist->dflt); |
| 4867 | |
| 4868 | /* copy the default set from refine */ |
| 4869 | llist->dflt_size = rfn->dflt_size; |
| 4870 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4871 | for (j = 0; j < llist->dflt_size; j++) { |
| 4872 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4876 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4877 | 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] | 4878 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4879 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4880 | } |
| 4881 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4886 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4887 | /* remove current value */ |
| 4888 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4889 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4890 | /* set new value */ |
| 4891 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4892 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4893 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4894 | /* check if node has default value */ |
| 4895 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 4896 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4897 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4898 | goto fail; |
| 4899 | } |
| 4900 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 4901 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4902 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4903 | goto fail; |
| 4904 | } |
| 4905 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4909 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4910 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4911 | ((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] | 4912 | } |
| 4913 | |
| 4914 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4915 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4916 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4917 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4918 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4919 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4920 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4921 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4922 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4923 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4924 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4925 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4926 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4927 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4932 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4933 | switch (node->nodetype) { |
| 4934 | case LYS_LEAF: |
| 4935 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4936 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4937 | break; |
| 4938 | case LYS_LEAFLIST: |
| 4939 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4940 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4941 | break; |
| 4942 | case LYS_LIST: |
| 4943 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4944 | old_must = &((struct lys_node_list *)node)->must; |
| 4945 | break; |
| 4946 | case LYS_CONTAINER: |
| 4947 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4948 | old_must = &((struct lys_node_container *)node)->must; |
| 4949 | break; |
| 4950 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4951 | case LYS_ANYDATA: |
| 4952 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4953 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4954 | break; |
| 4955 | default: |
| 4956 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4957 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | size = *old_size + rfn->must_size; |
| 4961 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4962 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4963 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4964 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4965 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4966 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4967 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4968 | lys_ext_dup(rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4969 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4970 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4971 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4972 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4973 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4974 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4975 | } |
| 4976 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4977 | *old_must = must; |
| 4978 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4979 | |
| 4980 | /* check XPath dependencies again */ |
| 4981 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4982 | goto fail; |
| 4983 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4984 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4985 | |
| 4986 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4987 | if (rfn->iffeature_size) { |
| 4988 | old_size = &node->iffeature_size; |
| 4989 | old_iff = &node->iffeature; |
| 4990 | |
| 4991 | size = *old_size + rfn->iffeature_size; |
| 4992 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4993 | if (!iff) { |
| 4994 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4995 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4996 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4997 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4998 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4999 | if (usize1) { |
| 5000 | /* there is something to duplicate */ |
| 5001 | /* duplicate compiled expression */ |
| 5002 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 5003 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5004 | 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] | 5005 | |
| 5006 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5007 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 5008 | 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] | 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | *old_iff = iff; |
| 5013 | *old_size = size; |
| 5014 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /* apply augments */ |
| 5018 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 5019 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5020 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5021 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5022 | } |
| 5023 | } |
| 5024 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5025 | /* check refines */ |
| 5026 | for (i = 0; i < uses->refine_size; i++) { |
| 5027 | node = refine_nodes[i]; |
| 5028 | rfn = &uses->refine[i]; |
| 5029 | |
| 5030 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5031 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5032 | 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] | 5033 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5034 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 5035 | (rfn->flags & LYS_CONFIG_W)) { |
| 5036 | /* setting config true under config false is prohibited */ |
| 5037 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5038 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5039 | "changing config from 'false' to 'true' is prohibited while " |
| 5040 | "the target's parent is still config 'false'."); |
| 5041 | goto fail; |
| 5042 | } |
| 5043 | |
| 5044 | /* inherit config change to the target children */ |
| 5045 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 5046 | if (rfn->flags & LYS_CONFIG_W) { |
| 5047 | if (iter->flags & LYS_CONFIG_SET) { |
| 5048 | /* config is set explicitely, go to next sibling */ |
| 5049 | next = NULL; |
| 5050 | goto nextsibling; |
| 5051 | } |
| 5052 | } else { /* LYS_CONFIG_R */ |
| 5053 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 5054 | /* error - we would have config data under status data */ |
| 5055 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5056 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5057 | "changing config from 'true' to 'false' is prohibited while the target " |
| 5058 | "has still a children with explicit config 'true'."); |
| 5059 | goto fail; |
| 5060 | } |
| 5061 | } |
| 5062 | /* change config */ |
| 5063 | iter->flags &= ~LYS_CONFIG_MASK; |
| 5064 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 5065 | |
| 5066 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5067 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5068 | next = NULL; |
| 5069 | } else { |
| 5070 | next = iter->child; |
| 5071 | } |
| 5072 | nextsibling: |
| 5073 | if (!next) { |
| 5074 | /* try siblings */ |
| 5075 | next = iter->next; |
| 5076 | } |
| 5077 | while (!next) { |
| 5078 | /* parent is already processed, go to its sibling */ |
| 5079 | iter = lys_parent(iter); |
| 5080 | |
| 5081 | /* no siblings, go back through parents */ |
| 5082 | if (iter == node) { |
| 5083 | /* we are done, no next element to process */ |
| 5084 | break; |
| 5085 | } |
| 5086 | next = iter->next; |
| 5087 | } |
| 5088 | } |
| 5089 | } |
| 5090 | |
| 5091 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5092 | if (rfn->dflt_size) { |
| 5093 | if (node->nodetype == LYS_CHOICE) { |
| 5094 | /* choice */ |
| 5095 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5096 | rfn->dflt[0]); |
| 5097 | if (!((struct lys_node_choice *)node)->dflt) { |
| 5098 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 5099 | goto fail; |
| 5100 | } |
| 5101 | if (lyp_check_mandatory_choice(node)) { |
| 5102 | goto fail; |
| 5103 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5108 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5109 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 5110 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5111 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5112 | goto fail; |
| 5113 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5114 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5115 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 5116 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5117 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5118 | goto fail; |
| 5119 | } |
| 5120 | } |
| 5121 | |
| 5122 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5123 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5124 | if (node->nodetype == LYS_LEAFLIST) { |
| 5125 | llist = (struct lys_node_leaflist *)node; |
| 5126 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 5127 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5128 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5129 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5130 | goto fail; |
| 5131 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5132 | } else if (node->nodetype == LYS_LEAF) { |
| 5133 | leaf = (struct lys_node_leaf *)node; |
| 5134 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame^] | 5135 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5136 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5137 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5138 | goto fail; |
| 5139 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5140 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5141 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5142 | /* 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] | 5143 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5144 | for (parent = node->parent; |
| 5145 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5146 | parent = parent->parent) { |
| 5147 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5148 | /* stop also on presence containers */ |
| 5149 | break; |
| 5150 | } |
| 5151 | } |
| 5152 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5153 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5154 | if (lyp_check_mandatory_choice(parent)) { |
| 5155 | goto fail; |
| 5156 | } |
| 5157 | } |
| 5158 | } |
| 5159 | } |
| 5160 | free(refine_nodes); |
| 5161 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5162 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5163 | |
| 5164 | fail: |
| 5165 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5166 | lys_node_free(iter, NULL, 0); |
| 5167 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5168 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5169 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5172 | void |
| 5173 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5174 | { |
| 5175 | int i; |
| 5176 | |
| 5177 | assert(der && base); |
| 5178 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5179 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5180 | /* create a set for backlinks if it does not exist */ |
| 5181 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5182 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5183 | /* store backlink */ |
| 5184 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5185 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5186 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5187 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5188 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5189 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5190 | } |
| 5191 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5192 | /** |
| 5193 | * @brief Resolve base identity recursively. Does not log. |
| 5194 | * |
| 5195 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5196 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5197 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5198 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5199 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5200 | * @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] | 5201 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5202 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5203 | 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] | 5204 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5205 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5206 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5207 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5208 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5209 | assert(ret); |
| 5210 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5211 | /* search module */ |
| 5212 | for (i = 0; i < module->ident_size; i++) { |
| 5213 | if (!strcmp(basename, module->ident[i].name)) { |
| 5214 | |
| 5215 | if (!ident) { |
| 5216 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5217 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5218 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5219 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5220 | } |
| 5221 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5222 | base = &module->ident[i]; |
| 5223 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5228 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5229 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5230 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5231 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5232 | if (!ident) { |
| 5233 | *ret = &module->inc[j].submodule->ident[i]; |
| 5234 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5235 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5236 | |
| 5237 | base = &module->inc[j].submodule->ident[i]; |
| 5238 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | } |
| 5242 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5243 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5244 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5245 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5246 | /* is it already completely resolved? */ |
| 5247 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5248 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5249 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5250 | |
| 5251 | /* simple check for circular reference, |
| 5252 | * the complete check is done as a side effect of using only completely |
| 5253 | * resolved identities (previous check of unres content) */ |
| 5254 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5255 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5256 | 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] | 5257 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5258 | } |
| 5259 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5260 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5261 | } |
| 5262 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5263 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5264 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5265 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5266 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5267 | } |
| 5268 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5269 | /* base not found (maybe a forward reference) */ |
| 5270 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5271 | } |
| 5272 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5273 | /** |
| 5274 | * @brief Resolve base identity. Logs directly. |
| 5275 | * |
| 5276 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5277 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5278 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5279 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5280 | * @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] | 5281 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5282 | * @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] | 5283 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5284 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5285 | 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] | 5286 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5287 | { |
| 5288 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5289 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5290 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5291 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5292 | struct lys_module *mod; |
| 5293 | |
| 5294 | assert((ident && !type) || (!ident && type)); |
| 5295 | |
| 5296 | if (!type) { |
| 5297 | /* have ident to resolve */ |
| 5298 | ret = ⌖ |
| 5299 | flags = ident->flags; |
| 5300 | mod = ident->module; |
| 5301 | } else { |
| 5302 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5303 | ++type->info.ident.count; |
| 5304 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5305 | if (!type->info.ident.ref) { |
| 5306 | LOGMEM; |
| 5307 | return -1; |
| 5308 | } |
| 5309 | |
| 5310 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5311 | flags = type->parent->flags; |
| 5312 | mod = type->parent->module; |
| 5313 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5314 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5315 | |
| 5316 | /* search for the base identity */ |
| 5317 | name = strchr(basename, ':'); |
| 5318 | if (name) { |
| 5319 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5320 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5321 | name++; |
| 5322 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5323 | 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] | 5324 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5325 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5326 | } |
| 5327 | } else { |
| 5328 | name = basename; |
| 5329 | } |
| 5330 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5331 | /* get module where to search */ |
| 5332 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5333 | if (!module) { |
| 5334 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5335 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5336 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5337 | } |
| 5338 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5339 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5340 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5341 | if (!rc) { |
| 5342 | assert(*ret); |
| 5343 | |
| 5344 | /* check status */ |
| 5345 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5346 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5347 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5348 | } else if (ident) { |
| 5349 | ident->base[ident->base_size++] = *ret; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5350 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5351 | /* maintain backlinks to the derived identities */ |
| 5352 | resolve_identity_backlink_update(ident, *ret); |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5353 | } |
| 5354 | } else if (rc == EXIT_FAILURE) { |
| 5355 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5356 | if (type) { |
| 5357 | --type->info.ident.count; |
| 5358 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5361 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5364 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5365 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5366 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5367 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5368 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5369 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5370 | * |
| 5371 | * @return Pointer to the identity resolvent, NULL on error. |
| 5372 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5373 | struct lys_ident * |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 5374 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5375 | { |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5376 | const char *mod_name, *name, *mod_name_iter; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5377 | int mod_name_len, rc, i; |
| 5378 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5379 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5380 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5381 | assert(type && ident_name && node); |
| 5382 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5383 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5384 | return NULL; |
| 5385 | } |
| 5386 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5387 | 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] | 5388 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5389 | 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] | 5390 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5391 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5392 | 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] | 5393 | return NULL; |
| 5394 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5395 | if (!mod_name) { |
| 5396 | /* no prefix, identity must be defined in the same module as node */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 5397 | mod_name = lys_main_module(mod)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5398 | mod_name_len = strlen(mod_name); |
| 5399 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5400 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5401 | /* go through all the bases in all the derived types */ |
| 5402 | while (type->der) { |
| 5403 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5404 | cur = type->info.ident.ref[i]; |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5405 | mod_name_iter = lys_main_module(cur->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5406 | if (!strcmp(cur->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5407 | !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] | 5408 | goto match; |
| 5409 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5410 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5411 | if (cur->der) { |
| 5412 | /* there are also some derived identities */ |
| 5413 | for (u = 0; u < cur->der->number; u++) { |
| 5414 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5415 | mod_name_iter = lys_main_module(der->module)->name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5416 | if (!strcmp(der->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5417 | !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] | 5418 | /* we have match */ |
| 5419 | cur = der; |
| 5420 | goto match; |
| 5421 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5422 | } |
| 5423 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5424 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5425 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5428 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5429 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5430 | |
| 5431 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5432 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5433 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5434 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5435 | 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] | 5436 | return NULL; |
| 5437 | } |
| 5438 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5439 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5440 | } |
| 5441 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5442 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5443 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5444 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5445 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5446 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5447 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5448 | * @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] | 5449 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5450 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5451 | 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] | 5452 | { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5453 | int rc, endian_idx; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5454 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5455 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5456 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5457 | endian_idx = 1; |
| 5458 | #else |
| 5459 | endian_idx = 0; |
| 5460 | #endif |
| 5461 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5462 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
| 5463 | * in some uses. When we see such a uses, the grouping's higher byte of the flags member (not used in |
| 5464 | * grouping) is used to store number of so far unresolved uses. The grouping cannot be used unless this |
| 5465 | * 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] | 5466 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5467 | 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] | 5468 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5469 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5470 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5471 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5472 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5473 | return -1; |
| 5474 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5475 | 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] | 5476 | return -1; |
| 5477 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5478 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5479 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5480 | * (and smaller flags - it uses only a limited set of flags) |
| 5481 | */ |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5482 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5483 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5484 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5485 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5486 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5487 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5490 | if (((uint8_t*)&uses->grp->flags)[endian_idx]) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5491 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5492 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5493 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5494 | } else { |
| 5495 | /* instantiate grouping only when it is completely resolved */ |
| 5496 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5497 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5498 | return EXIT_FAILURE; |
| 5499 | } |
| 5500 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5501 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5502 | if (!rc) { |
| 5503 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5504 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5505 | if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5506 | LOGINT; |
| 5507 | return -1; |
| 5508 | } |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5509 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5510 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5511 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5512 | |
| 5513 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5514 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5515 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5516 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5517 | return -1; |
| 5518 | } |
| 5519 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5520 | return EXIT_SUCCESS; |
| 5521 | } |
| 5522 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5523 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5524 | } |
| 5525 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5526 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5527 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5528 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5529 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5530 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5531 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5532 | * @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] | 5533 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5534 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5535 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5536 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5537 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5538 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5539 | |
| 5540 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5541 | if (!list->child) { |
| 5542 | /* no child, possible forward reference */ |
| 5543 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5544 | return EXIT_FAILURE; |
| 5545 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5546 | /* get the key name */ |
| 5547 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5548 | len = value - keys_str; |
| 5549 | while (isspace(value[0])) { |
| 5550 | value++; |
| 5551 | } |
| 5552 | } else { |
| 5553 | len = strlen(keys_str); |
| 5554 | } |
| 5555 | |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 5556 | rc = lys_get_data_sibling(lys_node_module((struct lys_node *)list), list->child, keys_str, len, LYS_LEAF, |
| 5557 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5558 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5559 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5560 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5561 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5562 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5563 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5564 | /* check_key logs */ |
| 5565 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5566 | } |
| 5567 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5568 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5569 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5570 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5571 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5572 | return -1; |
| 5573 | } |
| 5574 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5575 | /* prepare for next iteration */ |
| 5576 | while (value && isspace(value[0])) { |
| 5577 | value++; |
| 5578 | } |
| 5579 | keys_str = value; |
| 5580 | } |
| 5581 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5582 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5583 | } |
| 5584 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5585 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5586 | * @brief Resolve (check) all must conditions of \p node. |
| 5587 | * Logs directly. |
| 5588 | * |
| 5589 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5590 | * @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] | 5591 | * |
| 5592 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5593 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5594 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5595 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5596 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5597 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5598 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5599 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5600 | struct lys_restr *must; |
| 5601 | struct lyxp_set set; |
| 5602 | |
| 5603 | assert(node); |
| 5604 | memset(&set, 0, sizeof set); |
| 5605 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5606 | if (inout_parent) { |
| 5607 | for (schema = lys_parent(node->schema); |
| 5608 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5609 | schema = lys_parent(schema)); |
| 5610 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5611 | LOGINT; |
| 5612 | return -1; |
| 5613 | } |
| 5614 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5615 | must = ((struct lys_node_inout *)schema)->must; |
| 5616 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5617 | node_flags = schema->flags; |
| 5618 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5619 | /* context node is the RPC/action */ |
| 5620 | node = node->parent; |
| 5621 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5622 | LOGINT; |
| 5623 | return -1; |
| 5624 | } |
| 5625 | } else { |
| 5626 | switch (node->schema->nodetype) { |
| 5627 | case LYS_CONTAINER: |
| 5628 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5629 | must = ((struct lys_node_container *)node->schema)->must; |
| 5630 | break; |
| 5631 | case LYS_LEAF: |
| 5632 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5633 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5634 | break; |
| 5635 | case LYS_LEAFLIST: |
| 5636 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5637 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5638 | break; |
| 5639 | case LYS_LIST: |
| 5640 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5641 | must = ((struct lys_node_list *)node->schema)->must; |
| 5642 | break; |
| 5643 | case LYS_ANYXML: |
| 5644 | case LYS_ANYDATA: |
| 5645 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5646 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5647 | break; |
| 5648 | case LYS_NOTIF: |
| 5649 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5650 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5651 | break; |
| 5652 | default: |
| 5653 | must_size = 0; |
| 5654 | break; |
| 5655 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5656 | |
| 5657 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5661 | 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] | 5662 | return -1; |
| 5663 | } |
| 5664 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5665 | 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] | 5666 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5667 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5668 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5669 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5670 | } else { |
| 5671 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5672 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5673 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5674 | } |
| 5675 | if (must[i].eapptag) { |
| 5676 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5677 | } |
| 5678 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5679 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5680 | } |
| 5681 | } |
| 5682 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5683 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5684 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5685 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5686 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5687 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5688 | * |
| 5689 | * @param[in] schema Schema node with the when condition. |
| 5690 | * @param[out] ctx_snode When schema context node. |
| 5691 | * @param[out] ctx_snode_type Schema context node type. |
| 5692 | */ |
| 5693 | void |
| 5694 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5695 | { |
| 5696 | const struct lys_node *sparent; |
| 5697 | |
| 5698 | /* find a not schema-only node */ |
| 5699 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5700 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5701 | if (schema->nodetype == LYS_AUGMENT) { |
| 5702 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5703 | } else { |
| 5704 | sparent = schema->parent; |
| 5705 | } |
| 5706 | if (!sparent) { |
| 5707 | /* context node is the document root (fake root in our case) */ |
| 5708 | if (schema->flags & LYS_CONFIG_W) { |
| 5709 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5710 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5711 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5712 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5713 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5714 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5715 | break; |
| 5716 | } |
| 5717 | schema = sparent; |
| 5718 | } |
| 5719 | |
| 5720 | *ctx_snode = (struct lys_node *)schema; |
| 5721 | } |
| 5722 | |
| 5723 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5724 | * @brief Resolve (find) when condition context node. Does not log. |
| 5725 | * |
| 5726 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5727 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5728 | * @param[out] ctx_node Context node. |
| 5729 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5730 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5731 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5732 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5733 | static int |
| 5734 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5735 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5736 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5737 | struct lyd_node *parent; |
| 5738 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5739 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5740 | uint16_t i, data_depth, schema_depth; |
| 5741 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5742 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5743 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5744 | if (node_type == LYXP_NODE_ELEM) { |
| 5745 | /* standard element context node */ |
| 5746 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5747 | for (sparent = schema, schema_depth = 0; |
| 5748 | sparent; |
| 5749 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5750 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5751 | ++schema_depth; |
| 5752 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5753 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5754 | if (data_depth < schema_depth) { |
| 5755 | return -1; |
| 5756 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5757 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5758 | /* find the corresponding data node */ |
| 5759 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5760 | node = node->parent; |
| 5761 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5762 | if (node->schema != schema) { |
| 5763 | return -1; |
| 5764 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5765 | } else { |
| 5766 | /* root context node */ |
| 5767 | while (node->parent) { |
| 5768 | node = node->parent; |
| 5769 | } |
| 5770 | while (node->prev->next) { |
| 5771 | node = node->prev; |
| 5772 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5773 | } |
| 5774 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5775 | *ctx_node = node; |
| 5776 | *ctx_node_type = node_type; |
| 5777 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5778 | } |
| 5779 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5780 | /** |
| 5781 | * @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] | 5782 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5783 | * |
| 5784 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5785 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5786 | * it is moved to point to another sibling still in the original tree. |
| 5787 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5788 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5789 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5790 | * Ordering may change, but there will be no semantic change. |
| 5791 | * |
| 5792 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5793 | */ |
| 5794 | static int |
| 5795 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5796 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5797 | { |
| 5798 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5799 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5800 | |
| 5801 | switch (snode->nodetype) { |
| 5802 | case LYS_AUGMENT: |
| 5803 | case LYS_USES: |
| 5804 | case LYS_CHOICE: |
| 5805 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5806 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5807 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5808 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5809 | continue; |
| 5810 | } |
| 5811 | |
| 5812 | if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5813 | return -1; |
| 5814 | } |
| 5815 | } |
| 5816 | break; |
| 5817 | case LYS_CONTAINER: |
| 5818 | case LYS_LIST: |
| 5819 | case LYS_LEAF: |
| 5820 | case LYS_LEAFLIST: |
| 5821 | case LYS_ANYXML: |
| 5822 | case LYS_ANYDATA: |
| 5823 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5824 | if (elem->schema == snode) { |
| 5825 | |
| 5826 | if (elem == *ctx_node) { |
| 5827 | /* We are going to unlink our context node! This normally cannot happen, |
| 5828 | * but we use normal top-level data nodes for faking a document root node, |
| 5829 | * so if this is the context node, we just use the next top-level node. |
| 5830 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5831 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5832 | * lyxp_eval() can handle this special situation. |
| 5833 | */ |
| 5834 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5835 | LOGINT; |
| 5836 | return -1; |
| 5837 | } |
| 5838 | |
| 5839 | if (elem->prev == elem) { |
| 5840 | /* unlinking last top-level element, use an empty data tree */ |
| 5841 | *ctx_node = NULL; |
| 5842 | } else { |
| 5843 | /* in this case just use the previous/last top-level data node */ |
| 5844 | *ctx_node = elem->prev; |
| 5845 | } |
| 5846 | } else if (elem == *node) { |
| 5847 | /* We are going to unlink the currently processed node. This does not matter that |
| 5848 | * much, but we would lose access to the original data tree, so just move our |
| 5849 | * pointer somewhere still inside it. |
| 5850 | */ |
| 5851 | if ((*node)->prev != *node) { |
| 5852 | *node = (*node)->prev; |
| 5853 | } else { |
| 5854 | /* the processed node with sibings were all unlinked, oh well */ |
| 5855 | *node = NULL; |
| 5856 | } |
| 5857 | } |
| 5858 | |
| 5859 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5860 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5861 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5862 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5863 | LOGINT; |
| 5864 | return -1; |
| 5865 | } |
| 5866 | } else { |
| 5867 | *unlinked_nodes = elem; |
| 5868 | } |
| 5869 | |
| 5870 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5871 | /* there can be only one instance */ |
| 5872 | break; |
| 5873 | } |
| 5874 | } |
| 5875 | } |
| 5876 | break; |
| 5877 | default: |
| 5878 | LOGINT; |
| 5879 | return -1; |
| 5880 | } |
| 5881 | |
| 5882 | return EXIT_SUCCESS; |
| 5883 | } |
| 5884 | |
| 5885 | /** |
| 5886 | * @brief Relink the unlinked nodes back. |
| 5887 | * |
| 5888 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5889 | * we simply need a sibling from the original data tree. |
| 5890 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5891 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5892 | * or the sibling of \p unlinked_nodes. |
| 5893 | * |
| 5894 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5895 | */ |
| 5896 | static int |
| 5897 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5898 | { |
| 5899 | struct lyd_node *elem; |
| 5900 | |
| 5901 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5902 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5903 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5904 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5905 | return -1; |
| 5906 | } |
| 5907 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5908 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5909 | return -1; |
| 5910 | } |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | return EXIT_SUCCESS; |
| 5915 | } |
| 5916 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5917 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5918 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5919 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5920 | int ret = 0; |
| 5921 | uint8_t must_size; |
| 5922 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5923 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5924 | assert(node); |
| 5925 | |
| 5926 | schema = node->schema; |
| 5927 | |
| 5928 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5929 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5930 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5931 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5932 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5933 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5934 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5935 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5936 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5937 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5938 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5939 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5940 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5941 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5942 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5943 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5944 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5945 | break; |
| 5946 | case LYS_NOTIF: |
| 5947 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5948 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5949 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5950 | must_size = 0; |
| 5951 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5952 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5953 | |
| 5954 | if (must_size) { |
| 5955 | ++ret; |
| 5956 | } |
| 5957 | |
| 5958 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5959 | if (!node->prev->next) { |
| 5960 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5961 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5962 | ret += 0x2; |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5970 | 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] | 5971 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5972 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5973 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5974 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5975 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5976 | 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] | 5977 | return 1; |
| 5978 | } |
| 5979 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5980 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5981 | goto check_augment; |
| 5982 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5983 | while (parent) { |
| 5984 | /* stop conditions */ |
| 5985 | if (!mode) { |
| 5986 | /* stop on node that can be instantiated in data tree */ |
| 5987 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5988 | break; |
| 5989 | } |
| 5990 | } else { |
| 5991 | /* stop on the specified node */ |
| 5992 | if (parent == stop) { |
| 5993 | break; |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5998 | return 1; |
| 5999 | } |
| 6000 | check_augment: |
| 6001 | |
| 6002 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6003 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 6004 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6005 | } |
| 6006 | parent = lys_parent(parent); |
| 6007 | } |
| 6008 | |
| 6009 | return 0; |
| 6010 | } |
| 6011 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6012 | /** |
| 6013 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6014 | * Logs directly. |
| 6015 | * |
| 6016 | * @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] | 6017 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6018 | * @return |
| 6019 | * -1 - error, ly_errno is set |
| 6020 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6021 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6022 | * 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] | 6023 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6024 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6025 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6026 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6027 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6028 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6029 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6030 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6031 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6032 | |
| 6033 | assert(node); |
| 6034 | memset(&set, 0, sizeof set); |
| 6035 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6036 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6037 | /* make the node dummy for the evaluation */ |
| 6038 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6039 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6040 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6041 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6042 | if (rc) { |
| 6043 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6044 | 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] | 6045 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6046 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6047 | } |
| 6048 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6049 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6050 | 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] | 6051 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6052 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6053 | 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] | 6054 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6055 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6056 | } else { |
| 6057 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 6058 | goto cleanup; |
| 6059 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6060 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6061 | |
| 6062 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6063 | 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] | 6064 | } |
| 6065 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6066 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6067 | goto check_augment; |
| 6068 | |
| 6069 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6070 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6071 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6072 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6073 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6074 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6075 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6076 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6077 | } |
| 6078 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6079 | |
| 6080 | unlinked_nodes = NULL; |
| 6081 | /* we do not want our node pointer to change */ |
| 6082 | tmp_node = node; |
| 6083 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6084 | if (rc) { |
| 6085 | goto cleanup; |
| 6086 | } |
| 6087 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6088 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6089 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 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_uses *)sparent)->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), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6106 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6107 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6108 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6109 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6110 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6111 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6112 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->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), 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
| 6121 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6122 | 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] | 6123 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6124 | 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] | 6125 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6126 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6127 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6128 | } |
| 6129 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6130 | |
| 6131 | unlinked_nodes = NULL; |
| 6132 | tmp_node = node; |
| 6133 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6134 | if (rc) { |
| 6135 | goto cleanup; |
| 6136 | } |
| 6137 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6138 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6139 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6140 | |
| 6141 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6142 | * so the tree did not actually change and there is nothing for us to do |
| 6143 | */ |
| 6144 | if (unlinked_nodes && ctx_node) { |
| 6145 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6146 | rc = -1; |
| 6147 | goto cleanup; |
| 6148 | } |
| 6149 | } |
| 6150 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6151 | if (rc) { |
| 6152 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6153 | 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] | 6154 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6155 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6156 | } |
| 6157 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6158 | 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] | 6159 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6160 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6161 | 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] | 6162 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6163 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6164 | } else { |
| 6165 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6166 | goto cleanup; |
| 6167 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6168 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6169 | |
| 6170 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6171 | 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] | 6172 | } |
| 6173 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6174 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6175 | } |
| 6176 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6177 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6178 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6179 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6180 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6181 | 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] | 6182 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6183 | if (result) { |
| 6184 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6185 | *result = 1; |
| 6186 | } else { |
| 6187 | *result = 0; |
| 6188 | } |
| 6189 | } |
| 6190 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6191 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6192 | } |
| 6193 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6194 | static int |
| 6195 | check_leafref_features(struct lys_type *type) |
| 6196 | { |
| 6197 | struct lys_node *iter; |
| 6198 | struct ly_set *src_parents, *trg_parents, *features; |
| 6199 | unsigned int i, j, size, x; |
| 6200 | int ret = EXIT_SUCCESS; |
| 6201 | |
| 6202 | assert(type->parent); |
| 6203 | |
| 6204 | src_parents = ly_set_new(); |
| 6205 | trg_parents = ly_set_new(); |
| 6206 | features = ly_set_new(); |
| 6207 | |
| 6208 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6209 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6210 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6211 | continue; |
| 6212 | } |
| 6213 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6214 | } |
| 6215 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6216 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6217 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6218 | continue; |
| 6219 | } |
| 6220 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6221 | } |
| 6222 | |
| 6223 | /* compare the features used in if-feature statements in the rest of both |
| 6224 | * chains of parents. The set of features used for target must be a subset |
| 6225 | * of features used for the leafref. This is not a perfect, we should compare |
| 6226 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6227 | for (i = 0; i < src_parents->number; i++) { |
| 6228 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6229 | if (!iter->iffeature_size) { |
| 6230 | continue; |
| 6231 | } |
| 6232 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6233 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6234 | for (; size; size--) { |
| 6235 | if (!iter->iffeature[j].features[size - 1]) { |
| 6236 | /* not yet resolved feature, postpone this check */ |
| 6237 | ret = EXIT_FAILURE; |
| 6238 | goto cleanup; |
| 6239 | } |
| 6240 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6241 | } |
| 6242 | } |
| 6243 | } |
| 6244 | x = features->number; |
| 6245 | for (i = 0; i < trg_parents->number; i++) { |
| 6246 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6247 | if (!iter->iffeature_size) { |
| 6248 | continue; |
| 6249 | } |
| 6250 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6251 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6252 | for (; size; size--) { |
| 6253 | if (!iter->iffeature[j].features[size - 1]) { |
| 6254 | /* not yet resolved feature, postpone this check */ |
| 6255 | ret = EXIT_FAILURE; |
| 6256 | goto cleanup; |
| 6257 | } |
| 6258 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6259 | /* the feature is not present in features set of target's parents chain */ |
| 6260 | 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] | 6261 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6262 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6263 | iter->iffeature[j].features[size - 1]->name); |
| 6264 | ret = -1; |
| 6265 | goto cleanup; |
| 6266 | } |
| 6267 | } |
| 6268 | } |
| 6269 | } |
| 6270 | |
| 6271 | cleanup: |
| 6272 | ly_set_free(features); |
| 6273 | ly_set_free(src_parents); |
| 6274 | ly_set_free(trg_parents); |
| 6275 | |
| 6276 | return ret; |
| 6277 | } |
| 6278 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6279 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6280 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6281 | * |
| 6282 | * @param[in] mod Main module. |
| 6283 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6284 | * @param[in] type Type of the unresolved item. |
| 6285 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6286 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6287 | * @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] | 6288 | * |
| 6289 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6290 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6291 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6292 | 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] | 6293 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6294 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6295 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6296 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6297 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6298 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6299 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6300 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6301 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6302 | struct ly_set *refs, *procs; |
| 6303 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6304 | struct lys_ident *ident; |
| 6305 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6306 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6307 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6308 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6309 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6310 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6311 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6312 | struct lys_ext_instance *ext, **extlist; |
| 6313 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6314 | |
| 6315 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6316 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6317 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6318 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6319 | ident = item; |
| 6320 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6321 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6322 | break; |
| 6323 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6324 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6325 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6326 | stype = item; |
| 6327 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6328 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6329 | break; |
| 6330 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6331 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6332 | stype = item; |
| 6333 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6334 | if (!lys_node_module(node)->implemented) { |
| 6335 | /* not implemented module, don't bother with resolving the leafref |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 6336 | * 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] | 6337 | rc = 0; |
| 6338 | break; |
| 6339 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6340 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6341 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6342 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6343 | /* check if leafref and its target are under a common if-features */ |
| 6344 | rc = check_leafref_features(stype); |
| 6345 | if (rc) { |
| 6346 | break; |
| 6347 | } |
| 6348 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6349 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6350 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6351 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6352 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6353 | } |
| 6354 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6355 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6356 | case UNRES_TYPE_DER_EXT: |
| 6357 | parent_type++; |
| 6358 | /* no break */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6359 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6360 | parent_type++; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6361 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6362 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6363 | /* parent */ |
| 6364 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6365 | stype = item; |
| 6366 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6367 | /* HACK type->der is temporarily unparsed type statement */ |
| 6368 | yin = (struct lyxml_elem *)stype->der; |
| 6369 | stype->der = NULL; |
| 6370 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6371 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6372 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6373 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6374 | |
| 6375 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6376 | /* may try again later */ |
| 6377 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6378 | } else { |
| 6379 | /* 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] | 6380 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6381 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6382 | } |
| 6383 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6384 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6385 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6386 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6387 | /* we need to always be able to free this, it's safe only in this case */ |
| 6388 | lyxml_free(mod->ctx, yin); |
| 6389 | } else { |
| 6390 | /* may try again later, put all back how it was */ |
| 6391 | stype->der = (struct lys_tpdf *)yin; |
| 6392 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6393 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6394 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6395 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6396 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6397 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6398 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6399 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6400 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6401 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6402 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6403 | * 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] | 6404 | * 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] | 6405 | * of the type's base member. */ |
| 6406 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6407 | if (par_grp) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 6408 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 6409 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 6410 | #else |
| 6411 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 6412 | #endif |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6413 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6414 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6415 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6416 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6417 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6418 | iff_data = str_snode; |
| 6419 | 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] | 6420 | if (!rc) { |
| 6421 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6422 | if (iff_data->infeature) { |
| 6423 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6424 | feat = *((struct lys_feature **)item); |
| 6425 | if (!feat->depfeatures) { |
| 6426 | feat->depfeatures = ly_set_new(); |
| 6427 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6428 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6429 | } |
| 6430 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6431 | lydict_remove(mod->ctx, iff_data->fname); |
| 6432 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6433 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6434 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6435 | case UNRES_FEATURE: |
| 6436 | feat = (struct lys_feature *)item; |
| 6437 | |
| 6438 | if (feat->iffeature_size) { |
| 6439 | refs = ly_set_new(); |
| 6440 | procs = ly_set_new(); |
| 6441 | ly_set_add(procs, feat, 0); |
| 6442 | |
| 6443 | while (procs->number) { |
| 6444 | ref = procs->set.g[procs->number - 1]; |
| 6445 | ly_set_rm_index(procs, procs->number - 1); |
| 6446 | |
| 6447 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6448 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6449 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6450 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6451 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6452 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6453 | goto featurecheckdone; |
| 6454 | } |
| 6455 | |
| 6456 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6457 | k = refs->number; |
| 6458 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6459 | /* not yet seen feature, add it for processing */ |
| 6460 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6461 | } |
| 6462 | } |
| 6463 | } else { |
| 6464 | /* forward reference */ |
| 6465 | rc = EXIT_FAILURE; |
| 6466 | goto featurecheckdone; |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | } |
| 6471 | } |
| 6472 | rc = EXIT_SUCCESS; |
| 6473 | |
| 6474 | featurecheckdone: |
| 6475 | ly_set_free(refs); |
| 6476 | ly_set_free(procs); |
| 6477 | } |
| 6478 | |
| 6479 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6480 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6481 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6482 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6483 | case UNRES_TYPEDEF_DFLT: |
| 6484 | parent_type++; |
| 6485 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6486 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6487 | stype = item; |
| 6488 | |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6489 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6490 | break; |
| 6491 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6492 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6493 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6494 | choic = item; |
| 6495 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6496 | if (!choic->dflt) { |
| 6497 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6498 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6499 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6500 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6501 | } else { |
| 6502 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6503 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6504 | break; |
| 6505 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6506 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6507 | break; |
| 6508 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6509 | unique_info = (struct unres_list_uniq *)item; |
| 6510 | 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] | 6511 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6512 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6513 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6514 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6515 | case UNRES_XPATH: |
| 6516 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6517 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6518 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6519 | case UNRES_EXT: |
| 6520 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6521 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6522 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6523 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6524 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6525 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6526 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6527 | if (eplugin) { |
| 6528 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6529 | u = malloc(sizeof *u); |
| 6530 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6531 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6532 | /* something really bad happend since the extension finalization is not actually |
| 6533 | * being resolved while adding into unres, so something more serious with the unres |
| 6534 | * list itself must happened */ |
| 6535 | return -1; |
| 6536 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6537 | } |
| 6538 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6539 | } |
| 6540 | if (!rc || rc == -1) { |
| 6541 | /* cleanup on success or fatal error */ |
| 6542 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6543 | /* YIN */ |
| 6544 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6545 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6546 | /* YANG */ |
| 6547 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6548 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6549 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6550 | } |
| 6551 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6552 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6553 | u = (uint8_t *)str_snode; |
| 6554 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6555 | free(u); |
| 6556 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6557 | eplugin = ext->def->plugin; |
| 6558 | |
| 6559 | /* inherit */ |
| 6560 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6561 | root = (struct lys_node *)ext->parent; |
| 6562 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6563 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6564 | /* first, check if the node already contain instance of the same extension, |
| 6565 | * in such a case we won't inherit. In case the node was actually defined as |
| 6566 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6567 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6568 | goto inherit_dfs_sibling; |
| 6569 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6570 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6571 | goto inherit_dfs_sibling; |
| 6572 | } |
| 6573 | |
| 6574 | if (eplugin->check_inherit) { |
| 6575 | /* we have a callback to check the inheritance, use it */ |
| 6576 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6577 | case 0: |
| 6578 | /* yes - continue with the inheriting code */ |
| 6579 | break; |
| 6580 | case 1: |
| 6581 | /* no - continue with the node's sibling */ |
| 6582 | goto inherit_dfs_sibling; |
| 6583 | case 2: |
| 6584 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6585 | goto inherit_dfs_child; |
| 6586 | default: |
| 6587 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6588 | ext->def->module->name, ext->def->name, rc); |
| 6589 | } |
| 6590 | } |
| 6591 | |
| 6592 | /* inherit the extension */ |
| 6593 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
| 6594 | if (!extlist) { |
| 6595 | LOGMEM; |
| 6596 | return -1; |
| 6597 | } |
| 6598 | extlist[node->ext_size] = malloc(sizeof **extlist); |
| 6599 | if (!extlist[node->ext_size]) { |
| 6600 | LOGMEM; |
| 6601 | node->ext = extlist; |
| 6602 | return -1; |
| 6603 | } |
| 6604 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6605 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6606 | |
| 6607 | node->ext = extlist; |
| 6608 | node->ext_size++; |
| 6609 | |
| 6610 | inherit_dfs_child: |
| 6611 | /* modification of - select element for the next run - children first */ |
| 6612 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6613 | next = NULL; |
| 6614 | } else { |
| 6615 | next = node->child; |
| 6616 | } |
| 6617 | if (!next) { |
| 6618 | inherit_dfs_sibling: |
| 6619 | /* no children, try siblings */ |
| 6620 | next = node->next; |
| 6621 | } |
| 6622 | while (!next) { |
| 6623 | /* go to the parent */ |
| 6624 | node = lys_parent(node); |
| 6625 | |
| 6626 | /* we are done if we are back in the root (the starter's parent */ |
| 6627 | if (node == root) { |
| 6628 | break; |
| 6629 | } |
| 6630 | |
| 6631 | /* parent is already processed, go to its sibling */ |
| 6632 | next = node->next; |
| 6633 | } |
| 6634 | } |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | /* final check */ |
| 6639 | if (eplugin->check_result) { |
| 6640 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6641 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6642 | return -1; |
| 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | rc = 0; |
| 6647 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6648 | default: |
| 6649 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6650 | break; |
| 6651 | } |
| 6652 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6653 | if (has_str && !rc) { |
| 6654 | /* the string is no more needed in case of success. |
| 6655 | * 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] | 6656 | lydict_remove(mod->ctx, str_snode); |
| 6657 | } |
| 6658 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6659 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6660 | } |
| 6661 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6662 | /* logs directly */ |
| 6663 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6664 | 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] | 6665 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6666 | struct lyxml_elem *xml; |
| 6667 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6668 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6669 | const char *name = NULL; |
| 6670 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6671 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6672 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6673 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6674 | 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] | 6675 | break; |
| 6676 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6677 | 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] | 6678 | break; |
| 6679 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6680 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6681 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6682 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6683 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6684 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6685 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6686 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6687 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6688 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6689 | } else { |
| 6690 | LY_TREE_FOR(xml->attr, attr) { |
| 6691 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6692 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6693 | break; |
| 6694 | } |
| 6695 | } |
| 6696 | assert(attr); |
| 6697 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6698 | 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] | 6699 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6700 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6701 | iff_data = str_node; |
| 6702 | 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] | 6703 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6704 | case UNRES_FEATURE: |
| 6705 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6706 | ((struct lys_feature *)item)->name); |
| 6707 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6708 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6709 | 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] | 6710 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6711 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6712 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6713 | if (str_node) { |
| 6714 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6715 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6716 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6717 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6718 | break; |
| 6719 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6720 | 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] | 6721 | break; |
| 6722 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6723 | 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] | 6724 | break; |
| 6725 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6726 | 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] | 6727 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6728 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6729 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6730 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6731 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6732 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6733 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6734 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6735 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6736 | case UNRES_EXT: |
| 6737 | extinfo = (struct unres_ext *)str_node; |
| 6738 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6739 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6740 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6741 | default: |
| 6742 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6743 | break; |
| 6744 | } |
| 6745 | } |
| 6746 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6747 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6748 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6749 | * |
| 6750 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6751 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6752 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6753 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6754 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6755 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6756 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6757 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6758 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6759 | struct lyxml_elem *yin; |
| 6760 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6761 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6762 | |
| 6763 | assert(unres); |
| 6764 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6765 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6766 | if (*ly_vlog_hide_location()) { |
| 6767 | log_hidden = 1; |
| 6768 | } else { |
| 6769 | log_hidden = 0; |
| 6770 | ly_vlog_hide(1); |
| 6771 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6772 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6773 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6774 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6775 | unres_count = 0; |
| 6776 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6777 | |
| 6778 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6779 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6780 | * if-features are resolved here to make sure that we will have all if-features for |
| 6781 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6782 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6783 | continue; |
| 6784 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6785 | /* 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] | 6786 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6787 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6788 | ++unres_count; |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6789 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6790 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6791 | unres->type[i] = UNRES_RESOLVED; |
| 6792 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6793 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6794 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6795 | if (!log_hidden) { |
| 6796 | ly_vlog_hide(0); |
| 6797 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6798 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6799 | ly_err_repeat(); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6800 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6801 | } else { |
| 6802 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6803 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6804 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6805 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6806 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6807 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6808 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6809 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6810 | if (!log_hidden) { |
| 6811 | ly_vlog_hide(0); |
| 6812 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6813 | |
| 6814 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6815 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6816 | continue; |
| 6817 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6818 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6819 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6820 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6821 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6822 | yang =(struct yang_type *)yin; |
| 6823 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6824 | if (yang->base == LY_TYPE_UNION) { |
| 6825 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6826 | } |
| 6827 | lydict_remove(mod->ctx, yang->name); |
| 6828 | free(yang); |
| 6829 | } else { |
| 6830 | lyxml_free(mod->ctx, yin); |
| 6831 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6832 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6833 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6834 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6835 | } |
| 6836 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6837 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6838 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6839 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6840 | continue; |
| 6841 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6842 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6843 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6844 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6845 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6846 | /* free the allocated structure */ |
| 6847 | free(unres->item[i]); |
| 6848 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6849 | unres->type[i] = UNRES_RESOLVED; |
| 6850 | ++resolved; |
| 6851 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6852 | if (!log_hidden) { |
| 6853 | ly_vlog_hide(0); |
| 6854 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6855 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6856 | ly_err_repeat(); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6857 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6858 | } else { |
| 6859 | /* forward reference, erase ly_errno */ |
| 6860 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6861 | } |
| 6862 | } |
| 6863 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6864 | if (!log_hidden) { |
| 6865 | ly_vlog_hide(0); |
| 6866 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6867 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6868 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6869 | for (i = 0; i < unres->count; ++i) { |
| 6870 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6871 | continue; |
| 6872 | } |
| 6873 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6874 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6875 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6876 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6877 | ++resolved; |
| 6878 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6879 | /* else error - it was already printed, but resolved was not increased, |
| 6880 | so this unres item will not be resolved again in the following code, |
| 6881 | but it will cause returning -1 at the end, this way we are able to |
| 6882 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6883 | } |
| 6884 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6885 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6886 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6887 | * all the validation errors |
| 6888 | */ |
| 6889 | for (i = 0; i < unres->count; ++i) { |
| 6890 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6891 | continue; |
| 6892 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6893 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6894 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6895 | /* 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] | 6896 | unres->type[i] = UNRES_RESOLVED; |
| 6897 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6898 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6899 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6900 | if (resolved < unres->count) { |
| 6901 | return -1; |
| 6902 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6903 | } |
| 6904 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6905 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6906 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6907 | return EXIT_SUCCESS; |
| 6908 | } |
| 6909 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6910 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6911 | * @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] | 6912 | * |
| 6913 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6914 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6915 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6916 | * @param[in] type Type of the unresolved item. |
| 6917 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6918 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6919 | * @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] | 6920 | */ |
| 6921 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6922 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6923 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6924 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6925 | int rc; |
| 6926 | const char *dictstr; |
| 6927 | |
| 6928 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6929 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6930 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6931 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6932 | lydict_remove(mod->ctx, dictstr); |
| 6933 | } |
| 6934 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6935 | } |
| 6936 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6937 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6938 | * @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] | 6939 | * |
| 6940 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6941 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6942 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6943 | * @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] | 6944 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6945 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6946 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6947 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6948 | */ |
| 6949 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6950 | 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] | 6951 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6952 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6953 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6954 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6955 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6956 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6957 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6958 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6959 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 6960 | uint32_t u; |
| 6961 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6962 | /* check for duplicities in unres */ |
| 6963 | for (u = 0; u < unres->count; u++) { |
| 6964 | if (unres->type[u] == type && unres->item[u] == item && |
| 6965 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6966 | /* duplication, should not happen */ |
| 6967 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6968 | } |
| 6969 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6970 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6971 | |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6972 | if (type == UNRES_EXT_FINALIZE) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6973 | /* extension finalization is not even tried when adding the item into the inres list */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6974 | rc = EXIT_FAILURE; |
| 6975 | } else { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6976 | if (*ly_vlog_hide_location()) { |
| 6977 | log_hidden = 1; |
| 6978 | } else { |
| 6979 | log_hidden = 0; |
| 6980 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6981 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame] | 6982 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6983 | if (!log_hidden) { |
| 6984 | ly_vlog_hide(0); |
| 6985 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6986 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6987 | if (rc != EXIT_FAILURE) { |
| 6988 | if (rc == -1 && ly_errno == LY_EVALID) { |
| 6989 | ly_err_repeat(); |
| 6990 | } |
| 6991 | if (type == UNRES_LIST_UNIQ) { |
| 6992 | /* free the allocated structure */ |
| 6993 | free(item); |
| 6994 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6995 | /* free the allocated resources */ |
| 6996 | free(*((char **)item)); |
| 6997 | } |
| 6998 | return rc; |
| 6999 | } else { |
| 7000 | /* erase info about validation errors */ |
| 7001 | ly_err_clean(1); |
| 7002 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7003 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7004 | print_unres_schema_item_fail(item, type, snode); |
| 7005 | |
| 7006 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7007 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7008 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7009 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7010 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7011 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7012 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7013 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7014 | } |
| 7015 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7016 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7017 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 7018 | if (!unres->item) { |
| 7019 | LOGMEM; |
| 7020 | return -1; |
| 7021 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7022 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7023 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 7024 | if (!unres->type) { |
| 7025 | LOGMEM; |
| 7026 | return -1; |
| 7027 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7028 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7029 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 7030 | if (!unres->str_snode) { |
| 7031 | LOGMEM; |
| 7032 | return -1; |
| 7033 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7034 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7035 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 7036 | if (!unres->module) { |
| 7037 | LOGMEM; |
| 7038 | return -1; |
| 7039 | } |
| 7040 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7041 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7042 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7043 | } |
| 7044 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7045 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7046 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7047 | * |
| 7048 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7049 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7050 | * @param[in] item Old item to be resolved. |
| 7051 | * @param[in] type Type of the old unresolved item. |
| 7052 | * @param[in] new_item New item to use in the duplicate. |
| 7053 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7054 | * @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] | 7055 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7056 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7057 | 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] | 7058 | { |
| 7059 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7060 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7061 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7062 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7063 | 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] | 7064 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7065 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7066 | if (type == UNRES_LIST_UNIQ) { |
| 7067 | aux_uniq.list = item; |
| 7068 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7069 | item = &aux_uniq; |
| 7070 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7071 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7072 | |
| 7073 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7074 | if (type == UNRES_LIST_UNIQ) { |
| 7075 | free(new_item); |
| 7076 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7077 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7078 | } |
| 7079 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7080 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7081 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7082 | 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] | 7083 | LOGINT; |
| 7084 | return -1; |
| 7085 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7086 | } else if (type == UNRES_IFFEAT) { |
| 7087 | /* duplicate unres_iffeature_data */ |
| 7088 | iff_data = malloc(sizeof *iff_data); |
| 7089 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7090 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7091 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7092 | LOGINT; |
| 7093 | return -1; |
| 7094 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7095 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7096 | 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] | 7097 | LOGINT; |
| 7098 | return -1; |
| 7099 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7100 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7101 | |
| 7102 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7103 | } |
| 7104 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7105 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7106 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7107 | 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] | 7108 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7109 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7110 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7111 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7112 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7113 | i = start_on_backwards; |
| 7114 | } else { |
| 7115 | i = unres->count - 1; |
| 7116 | } |
| 7117 | for (; i > -1; i--) { |
| 7118 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7119 | continue; |
| 7120 | } |
| 7121 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7122 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7123 | break; |
| 7124 | } |
| 7125 | } else { |
| 7126 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7127 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7128 | 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] | 7129 | break; |
| 7130 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7131 | } |
| 7132 | } |
| 7133 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7134 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7135 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7136 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7137 | static void |
| 7138 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7139 | { |
| 7140 | struct lyxml_elem *yin; |
| 7141 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7142 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7143 | |
| 7144 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7145 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7146 | case UNRES_TYPE_DER: |
| 7147 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7148 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7149 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7150 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7151 | lydict_remove(ctx, yang->name); |
| 7152 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7153 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7154 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7155 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7156 | } else { |
| 7157 | lyxml_free(ctx, yin); |
| 7158 | } |
| 7159 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7160 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7161 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7162 | lydict_remove(ctx, iff_data->fname); |
| 7163 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7164 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7165 | case UNRES_IDENT: |
| 7166 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7167 | case UNRES_CHOICE_DFLT: |
| 7168 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7169 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7170 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7171 | case UNRES_LIST_UNIQ: |
| 7172 | free(unres->item[i]); |
| 7173 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7174 | case UNRES_EXT: |
| 7175 | free(unres->str_snode[i]); |
| 7176 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7177 | default: |
| 7178 | break; |
| 7179 | } |
| 7180 | unres->type[i] = UNRES_RESOLVED; |
| 7181 | } |
| 7182 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7183 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7184 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7185 | { |
| 7186 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7187 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7188 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7189 | if (!unres || !(*unres)) { |
| 7190 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7191 | } |
| 7192 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7193 | assert(module || (*unres)->count == 0); |
| 7194 | |
| 7195 | for (i = 0; i < (*unres)->count; ++i) { |
| 7196 | if ((*unres)->module[i] != module) { |
| 7197 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7198 | unresolved++; |
| 7199 | } |
| 7200 | continue; |
| 7201 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7202 | |
| 7203 | /* free heap memory for the specific item */ |
| 7204 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7205 | } |
| 7206 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7207 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7208 | if (!module || (!unresolved && !module->type)) { |
| 7209 | free((*unres)->item); |
| 7210 | free((*unres)->type); |
| 7211 | free((*unres)->str_snode); |
| 7212 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7213 | free((*unres)); |
| 7214 | (*unres) = NULL; |
| 7215 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7216 | } |
| 7217 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7218 | static int |
| 7219 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7220 | { |
| 7221 | struct ly_set *set; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7222 | struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7223 | char *buf; |
| 7224 | |
| 7225 | for (op_node = lys_parent(sleaf); |
| 7226 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7227 | op_node = lys_parent(op_node)); |
| 7228 | |
| 7229 | if (op_node && lys_parent(op_node)) { |
| 7230 | /* nested operation - any absolute path is external */ |
| 7231 | return 1; |
| 7232 | } |
| 7233 | |
| 7234 | /* get the first node from the instid */ |
| 7235 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7236 | if (!buf) { |
| 7237 | LOGMEM; |
| 7238 | return -1; |
| 7239 | } |
| 7240 | |
| 7241 | /* there is a predicate, remove it */ |
| 7242 | if (buf[strlen(buf) - 1] == ']') { |
| 7243 | assert(strchr(buf, '[')); |
| 7244 | *strchr(buf, '[') = '\0'; |
| 7245 | } |
| 7246 | |
| 7247 | /* find the first schema node */ |
Michal Vasko | 2611e19 | 2017-01-23 10:33:21 +0100 | [diff] [blame] | 7248 | set = lys_find_xpath(NULL, sleaf, buf, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7249 | if (!set || !set->number) { |
| 7250 | free(buf); |
Michal Vasko | 29fd974 | 2017-01-23 09:55:44 +0100 | [diff] [blame] | 7251 | ly_set_free(set); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7252 | return 1; |
| 7253 | } |
| 7254 | free(buf); |
| 7255 | |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7256 | first_node = set->set.s[0]; |
| 7257 | ly_set_free(set); |
| 7258 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7259 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7260 | |
| 7261 | if (op_node) { |
| 7262 | /* 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] | 7263 | if (op_node == first_node) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7264 | assert(set->number == 1); |
| 7265 | return 0; |
| 7266 | } else { |
| 7267 | return 1; |
| 7268 | } |
| 7269 | } |
| 7270 | |
| 7271 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7272 | * this itself), so we say it's not external */ |
| 7273 | return 0; |
| 7274 | } |
| 7275 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7276 | /** |
| 7277 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7278 | * |
| 7279 | * @param[in] data Data node where the path is used |
| 7280 | * @param[in] path Instance-identifier node value. |
| 7281 | * @param[in,out] ret Resolved instance or NULL. |
| 7282 | * |
| 7283 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7284 | */ |
| 7285 | static int |
| 7286 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7287 | { |
| 7288 | int i = 0, j; |
| 7289 | const struct lys_module *mod; |
| 7290 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7291 | const char *model, *name; |
| 7292 | char *str; |
| 7293 | int mod_len, name_len, has_predicate; |
| 7294 | struct unres_data node_match; |
| 7295 | |
| 7296 | memset(&node_match, 0, sizeof node_match); |
| 7297 | *ret = NULL; |
| 7298 | |
| 7299 | /* we need root to resolve absolute path */ |
| 7300 | for (; data->parent; data = data->parent); |
| 7301 | /* we're still parsing it and the pointer is not correct yet */ |
| 7302 | if (data->prev) { |
| 7303 | for (; data->prev->next; data = data->prev); |
| 7304 | } |
| 7305 | |
| 7306 | /* search for the instance node */ |
| 7307 | while (path[i]) { |
| 7308 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7309 | if (j <= 0) { |
| 7310 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7311 | goto error; |
| 7312 | } |
| 7313 | i += j; |
| 7314 | |
| 7315 | str = strndup(model, mod_len); |
| 7316 | if (!str) { |
| 7317 | LOGMEM; |
| 7318 | goto error; |
| 7319 | } |
| 7320 | mod = ly_ctx_get_module(ctx, str, NULL); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7321 | if (ctx->data_clb) { |
| 7322 | if (!mod) { |
| 7323 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7324 | } else if (!mod->implemented) { |
| 7325 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7326 | } |
| 7327 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7328 | free(str); |
| 7329 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7330 | if (!mod || !mod->implemented || mod->disabled) { |
| 7331 | break; |
| 7332 | } |
| 7333 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7334 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7335 | /* no instance exists */ |
| 7336 | break; |
| 7337 | } |
| 7338 | |
| 7339 | if (has_predicate) { |
| 7340 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 7341 | j = resolve_instid_predicate(&path[i], &node_match); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7342 | if (j < 1) { |
| 7343 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7344 | goto error; |
| 7345 | } |
| 7346 | i += j; |
| 7347 | |
| 7348 | if (!node_match.count) { |
| 7349 | /* no instance exists */ |
| 7350 | break; |
| 7351 | } |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7352 | } else if (node_match.count) { |
| 7353 | /* check that we are not addressing lists */ |
| 7354 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7355 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7356 | unres_data_del(&node_match, j--); |
| 7357 | } |
| 7358 | } |
| 7359 | if (!node_match.count) { |
| 7360 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys."); |
| 7361 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | if (!node_match.count) { |
| 7366 | /* no instance exists */ |
| 7367 | if (req_inst > -1) { |
| 7368 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7369 | return EXIT_FAILURE; |
| 7370 | } |
| 7371 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7372 | return EXIT_SUCCESS; |
| 7373 | } else if (node_match.count > 1) { |
| 7374 | /* instance identifier must resolve to a single node */ |
| 7375 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7376 | goto error; |
| 7377 | } else { |
| 7378 | /* we have required result, remember it and cleanup */ |
| 7379 | *ret = node_match.node[0]; |
| 7380 | free(node_match.node); |
| 7381 | return EXIT_SUCCESS; |
| 7382 | } |
| 7383 | |
| 7384 | error: |
| 7385 | /* cleanup */ |
| 7386 | free(node_match.node); |
| 7387 | return -1; |
| 7388 | } |
| 7389 | |
| 7390 | static int |
| 7391 | 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] | 7392 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7393 | struct unres_data matches; |
| 7394 | uint32_t i; |
| 7395 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7396 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7397 | memset(&matches, 0, sizeof matches); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7398 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7399 | |
| 7400 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7401 | if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7402 | return -1; |
| 7403 | } |
| 7404 | |
| 7405 | /* check that value matches */ |
| 7406 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7407 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7408 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7409 | 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] | 7410 | /* we have the match */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7411 | *ret = matches.node[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7412 | break; |
| 7413 | } |
| 7414 | } |
| 7415 | |
| 7416 | free(matches.node); |
| 7417 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7418 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7419 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7420 | if (req_inst > -1) { |
| 7421 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7422 | return EXIT_FAILURE; |
| 7423 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7424 | 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] | 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | return EXIT_SUCCESS; |
| 7429 | } |
| 7430 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7431 | /* 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] | 7432 | int |
| 7433 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7434 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7435 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7436 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7437 | struct lyd_node *ret; |
| 7438 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7439 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7440 | |
| 7441 | assert(type->base == LY_TYPE_UNION); |
| 7442 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7443 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7444 | /* either NULL or instid previously converted to JSON */ |
| 7445 | json_val = leaf->value.string; |
| 7446 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7447 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7448 | if (store) { |
| 7449 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7450 | free(leaf->value.bit); |
| 7451 | } |
| 7452 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7453 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7454 | |
| 7455 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7456 | hidden = *ly_vlog_hide_location(); |
| 7457 | ly_vlog_hide(1); |
| 7458 | |
| 7459 | t = NULL; |
| 7460 | found = 0; |
| 7461 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7462 | found = 0; |
| 7463 | |
| 7464 | switch (t->base) { |
| 7465 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7466 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7467 | req_inst = -1; |
| 7468 | } else { |
| 7469 | req_inst = t->info.lref.req; |
| 7470 | } |
| 7471 | |
| 7472 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7473 | if (store) { |
| 7474 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7475 | /* valid resolved */ |
| 7476 | leaf->value.leafref = ret; |
| 7477 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7478 | } else { |
| 7479 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7480 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7481 | return -1; |
| 7482 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7483 | } |
| 7484 | } |
| 7485 | |
| 7486 | success = 1; |
| 7487 | } |
| 7488 | break; |
| 7489 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7490 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7491 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7492 | req_inst = -1; |
| 7493 | } else { |
| 7494 | req_inst = t->info.inst.req; |
| 7495 | } |
| 7496 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7497 | 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] | 7498 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7499 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7500 | /* valid resolved */ |
| 7501 | leaf->value.instance = ret; |
| 7502 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7503 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7504 | if (json_val) { |
| 7505 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7506 | leaf->value_str = json_val; |
| 7507 | json_val = NULL; |
| 7508 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7509 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7510 | /* valid unresolved */ |
| 7511 | if (json_val) { |
| 7512 | /* put the JSON val back */ |
| 7513 | leaf->value.string = json_val; |
| 7514 | json_val = NULL; |
| 7515 | } else { |
| 7516 | leaf->value.instance = NULL; |
| 7517 | } |
| 7518 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7519 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7520 | } |
| 7521 | |
| 7522 | success = 1; |
| 7523 | } |
| 7524 | break; |
| 7525 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7526 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, store, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7527 | success = 1; |
| 7528 | } |
| 7529 | break; |
| 7530 | } |
| 7531 | |
| 7532 | if (success) { |
| 7533 | break; |
| 7534 | } |
| 7535 | |
| 7536 | /* erase information about errors - they are false or irrelevant |
| 7537 | * and will be replaced by a single error messages */ |
| 7538 | ly_err_clean(1); |
| 7539 | |
| 7540 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7541 | if (store) { |
| 7542 | if (t->base == LY_TYPE_BITS) { |
| 7543 | free(leaf->value.bit); |
| 7544 | } |
| 7545 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7546 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | /* turn logging back on */ |
| 7550 | if (!hidden) { |
| 7551 | ly_vlog_hide(0); |
| 7552 | } |
| 7553 | |
| 7554 | if (json_val) { |
| 7555 | if (!success) { |
| 7556 | /* put the value back for now */ |
| 7557 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7558 | leaf->value.string = json_val; |
| 7559 | } else { |
| 7560 | /* value was ultimately useless, but we could not have known */ |
| 7561 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7562 | } |
| 7563 | } |
| 7564 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7565 | if (success) { |
| 7566 | if (resolved_type) { |
| 7567 | *resolved_type = t; |
| 7568 | } |
| 7569 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7570 | /* not found and it is required */ |
| 7571 | 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] | 7572 | return EXIT_FAILURE; |
| 7573 | } |
| 7574 | |
| 7575 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7576 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7577 | } |
| 7578 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7579 | /** |
| 7580 | * @brief Resolve a single unres data item. Logs directly. |
| 7581 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7582 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7583 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7584 | * @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] | 7585 | * |
| 7586 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7587 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7588 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7589 | 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] | 7590 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7591 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7592 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7593 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7594 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7595 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7596 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7597 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7598 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7599 | switch (type) { |
| 7600 | case UNRES_LEAFREF: |
| 7601 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7602 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7603 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7604 | req_inst = -1; |
| 7605 | } else { |
| 7606 | req_inst = sleaf->type.info.lref.req; |
| 7607 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7608 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7609 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7610 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7611 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7612 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7613 | free(leaf->value.bit); |
| 7614 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7615 | leaf->value.leafref = ret; |
| 7616 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7617 | } else { |
| 7618 | /* valid unresolved */ |
| 7619 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7620 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7621 | return -1; |
| 7622 | } |
| 7623 | } |
| 7624 | } |
| 7625 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7626 | } else { |
| 7627 | return rc; |
| 7628 | } |
| 7629 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7630 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7631 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7632 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7633 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7634 | if (ext_dep == -1) { |
| 7635 | return -1; |
| 7636 | } |
| 7637 | |
| 7638 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7639 | req_inst = -1; |
| 7640 | } else { |
| 7641 | req_inst = sleaf->type.info.inst.req; |
| 7642 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7643 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7644 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7645 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7646 | /* valid resolved */ |
| 7647 | leaf->value.instance = ret; |
| 7648 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7649 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7650 | /* valid unresolved */ |
| 7651 | leaf->value.instance = NULL; |
| 7652 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7653 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7654 | } else { |
| 7655 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7656 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7657 | break; |
| 7658 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7659 | case UNRES_UNION: |
| 7660 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7661 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7662 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7663 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7664 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7665 | return rc; |
| 7666 | } |
| 7667 | break; |
| 7668 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7669 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7670 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7671 | return rc; |
| 7672 | } |
| 7673 | break; |
| 7674 | |
| 7675 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7676 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7677 | return rc; |
| 7678 | } |
| 7679 | break; |
| 7680 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7681 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7682 | LOGINT; |
| 7683 | return -1; |
| 7684 | } |
| 7685 | |
| 7686 | return EXIT_SUCCESS; |
| 7687 | } |
| 7688 | |
| 7689 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7690 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7691 | * |
| 7692 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7693 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7694 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7695 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7696 | */ |
| 7697 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7698 | 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] | 7699 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7700 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7701 | 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] | 7702 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7703 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7704 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7705 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 7706 | if (!unres->node) { |
| 7707 | LOGMEM; |
| 7708 | return -1; |
| 7709 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7710 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7711 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 7712 | if (!unres->type) { |
| 7713 | LOGMEM; |
| 7714 | return -1; |
| 7715 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7716 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7717 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7718 | if (type == UNRES_WHEN) { |
| 7719 | /* remove previous result */ |
| 7720 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7721 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7722 | |
| 7723 | return EXIT_SUCCESS; |
| 7724 | } |
| 7725 | |
| 7726 | /** |
| 7727 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7728 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7729 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7730 | * unresolved leafrefs/instids are accepted). |
| 7731 | * |
| 7732 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7733 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7734 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7735 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7736 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7737 | * |
| 7738 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7739 | */ |
| 7740 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7741 | 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] | 7742 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7743 | 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] | 7744 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7745 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7746 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7747 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7748 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7749 | |
| 7750 | if (!unres->count) { |
| 7751 | return EXIT_SUCCESS; |
| 7752 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7753 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7754 | 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] | 7755 | ignore_fail = 1; |
| 7756 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7757 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7758 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7759 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7760 | } |
| 7761 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7762 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7763 | ly_vlog_hide(1); |
| 7764 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7765 | /* when-stmt first */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7766 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7767 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7768 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7769 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 7770 | if (unres->type[i] != UNRES_WHEN && unres->type[i] != UNRES_LEAFREF) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7771 | continue; |
| 7772 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7773 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7774 | /* count when-stmt nodes in unres list */ |
| 7775 | when_stmt++; |
| 7776 | } |
| 7777 | |
| 7778 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7779 | for (parent = unres->node[i]->parent; |
| 7780 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7781 | parent = parent->parent) { |
| 7782 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7783 | /* the parent node was already unlinked, do not resolve this node, |
| 7784 | * it will be removed anyway, so just mark it as resolved |
| 7785 | */ |
| 7786 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7787 | unres->type[i] = UNRES_RESOLVED; |
| 7788 | resolved++; |
| 7789 | break; |
| 7790 | } |
| 7791 | } |
| 7792 | if (parent) { |
| 7793 | continue; |
| 7794 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7795 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7796 | 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] | 7797 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7798 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7799 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7800 | /* false when condition */ |
| 7801 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7802 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7803 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7804 | } /* follows else */ |
| 7805 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7806 | /* auto-delete */ |
| 7807 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7808 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7809 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7810 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7811 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7812 | * remove the container |
| 7813 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7814 | for (parent = unres->node[i]; |
| 7815 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7816 | parent = parent->parent) { |
| 7817 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7818 | /* presence container */ |
| 7819 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7820 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7821 | if (parent->next || parent->prev != parent) { |
| 7822 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7823 | break; |
| 7824 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7825 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7826 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7827 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7828 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7829 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7830 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7831 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7832 | lyd_unlink(unres->node[i]); |
| 7833 | unres->type[i] = UNRES_DELETE; |
| 7834 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7835 | |
| 7836 | /* update the rest of unres items */ |
| 7837 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7838 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7839 | continue; |
| 7840 | } |
| 7841 | |
| 7842 | /* test if the node is in subtree to be deleted */ |
| 7843 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7844 | if (parent == unres->node[i]) { |
| 7845 | /* yes, it is */ |
| 7846 | unres->type[j] = UNRES_RESOLVED; |
| 7847 | resolved++; |
| 7848 | break; |
| 7849 | } |
| 7850 | } |
| 7851 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7852 | } else { |
| 7853 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7854 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7855 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7856 | resolved++; |
| 7857 | progress = 1; |
| 7858 | } else if (rc == -1) { |
| 7859 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7860 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7861 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7862 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7863 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7864 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7865 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7866 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7867 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7868 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7869 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7870 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7871 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7872 | return -1; |
| 7873 | } |
| 7874 | |
| 7875 | for (i = 0; del_items && i < unres->count; i++) { |
| 7876 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7877 | if (unres->type[i] != UNRES_DELETE) { |
| 7878 | continue; |
| 7879 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7880 | if (!unres->node[i]) { |
| 7881 | unres->type[i] = UNRES_RESOLVED; |
| 7882 | del_items--; |
| 7883 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | /* really remove the complete subtree */ |
| 7887 | lyd_free(unres->node[i]); |
| 7888 | unres->type[i] = UNRES_RESOLVED; |
| 7889 | del_items--; |
| 7890 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7891 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7892 | |
| 7893 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7894 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7895 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7896 | continue; |
| 7897 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7898 | 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] | 7899 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7900 | 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] | 7901 | if (rc) { |
| 7902 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7903 | return -1; |
| 7904 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7905 | |
| 7906 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7907 | } |
| 7908 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7909 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7910 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7911 | return EXIT_SUCCESS; |
| 7912 | } |