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 | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 914 | * predicate-expr = "." / [prefix:]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. |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 920 | * @param[out] mod_name Points to the list key module name. |
| 921 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 922 | * @param[out] name Points to the list key name. |
| 923 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 924 | * @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] | 925 | * @param[out] val_len Length of \p value. |
| 926 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 927 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 928 | int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 929 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 930 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 931 | { |
| 932 | const char *ptr; |
| 933 | int parsed = 0, ret; |
| 934 | char quote; |
| 935 | |
| 936 | assert(id); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 937 | if (mod_name) { |
| 938 | *mod_name = NULL; |
| 939 | } |
| 940 | if (mod_name_len) { |
| 941 | *mod_name_len = 0; |
| 942 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 943 | if (name) { |
| 944 | *name = NULL; |
| 945 | } |
| 946 | if (nam_len) { |
| 947 | *nam_len = 0; |
| 948 | } |
| 949 | if (value) { |
| 950 | *value = NULL; |
| 951 | } |
| 952 | if (val_len) { |
| 953 | *val_len = 0; |
| 954 | } |
| 955 | if (has_predicate) { |
| 956 | *has_predicate = 0; |
| 957 | } |
| 958 | |
| 959 | if (id[0] != '[') { |
| 960 | return -parsed; |
| 961 | } |
| 962 | |
| 963 | ++parsed; |
| 964 | ++id; |
| 965 | |
| 966 | while (isspace(id[0])) { |
| 967 | ++parsed; |
| 968 | ++id; |
| 969 | } |
| 970 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 971 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 972 | if (id[0] == '.') { |
| 973 | ret = 1; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 974 | |
| 975 | if (name) { |
| 976 | *name = id; |
| 977 | } |
| 978 | if (nam_len) { |
| 979 | *nam_len = ret; |
| 980 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 981 | } else if (isdigit(id[0])) { |
| 982 | if (id[0] == '0') { |
| 983 | return -parsed; |
| 984 | } |
| 985 | ret = 1; |
| 986 | while (isdigit(id[ret])) { |
| 987 | ++ret; |
| 988 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 989 | |
| 990 | if (name) { |
| 991 | *name = id; |
| 992 | } |
| 993 | if (nam_len) { |
| 994 | *nam_len = ret; |
| 995 | } |
| 996 | } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 997 | return -parsed + ret; |
| 998 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 999 | |
| 1000 | parsed += ret; |
| 1001 | id += ret; |
| 1002 | |
| 1003 | while (isspace(id[0])) { |
| 1004 | ++parsed; |
| 1005 | ++id; |
| 1006 | } |
| 1007 | |
| 1008 | /* there is value as well */ |
| 1009 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1010 | if (name && isdigit(**name)) { |
| 1011 | return -parsed; |
| 1012 | } |
| 1013 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1014 | ++parsed; |
| 1015 | ++id; |
| 1016 | |
| 1017 | while (isspace(id[0])) { |
| 1018 | ++parsed; |
| 1019 | ++id; |
| 1020 | } |
| 1021 | |
| 1022 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1023 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1024 | quote = id[0]; |
| 1025 | |
| 1026 | ++parsed; |
| 1027 | ++id; |
| 1028 | |
| 1029 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1030 | return -parsed; |
| 1031 | } |
| 1032 | ret = ptr - id; |
| 1033 | |
| 1034 | if (value) { |
| 1035 | *value = id; |
| 1036 | } |
| 1037 | if (val_len) { |
| 1038 | *val_len = ret; |
| 1039 | } |
| 1040 | |
| 1041 | parsed += ret + 1; |
| 1042 | id += ret + 1; |
| 1043 | } else { |
| 1044 | return -parsed; |
| 1045 | } |
| 1046 | |
| 1047 | while (isspace(id[0])) { |
| 1048 | ++parsed; |
| 1049 | ++id; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (id[0] != ']') { |
| 1054 | return -parsed; |
| 1055 | } |
| 1056 | |
| 1057 | ++parsed; |
| 1058 | ++id; |
| 1059 | |
| 1060 | if ((id[0] == '[') && has_predicate) { |
| 1061 | *has_predicate = 1; |
| 1062 | } |
| 1063 | |
| 1064 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1068 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1069 | * |
| 1070 | * @param[in] feat_name Feature name to resolve. |
| 1071 | * @param[in] len Length of \p feat_name. |
| 1072 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1073 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1074 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1075 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1076 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1077 | */ |
| 1078 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1079 | 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] | 1080 | { |
| 1081 | char *str; |
| 1082 | const char *mod_name, *name; |
| 1083 | int mod_name_len, nam_len, i, j; |
| 1084 | const struct lys_module *module; |
| 1085 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1086 | assert(feature); |
| 1087 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1088 | /* check prefix */ |
| 1089 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1090 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1091 | return -1; |
| 1092 | } |
| 1093 | |
| 1094 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1095 | if (!module) { |
| 1096 | /* identity refers unknown data model */ |
| 1097 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1101 | if (module != node->module && module == lys_node_module(node)) { |
| 1102 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1103 | for (j = 0; j < node->module->features_size; j++) { |
| 1104 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1105 | /* check status */ |
| 1106 | 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] | 1107 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1108 | return -1; |
| 1109 | } |
| 1110 | *feature = &node->module->features[j]; |
| 1111 | return 0; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1116 | /* search in the identified module ... */ |
| 1117 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1118 | 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] | 1119 | /* check status */ |
| 1120 | 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] | 1121 | module->features[j].module, module->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->features[j]; |
| 1125 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1129 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1130 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1131 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1132 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1133 | /* check status */ |
| 1134 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1135 | module->inc[i].submodule->features[j].flags, |
| 1136 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1137 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1138 | return -1; |
| 1139 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1140 | *feature = &module->inc[i].submodule->features[j]; |
| 1141 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* not found */ |
| 1147 | str = strndup(feat_name, len); |
| 1148 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1149 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1150 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1153 | /* |
| 1154 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1155 | * - 1 if enabled |
| 1156 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1157 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1158 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1159 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1160 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1161 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1162 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1163 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1164 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1165 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1166 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1167 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1168 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1169 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1173 | 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] | 1174 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1175 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1176 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1177 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1178 | op = iff_getop(expr->expr, *index_e); |
| 1179 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1180 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1181 | switch (op) { |
| 1182 | case LYS_IFF_F: |
| 1183 | /* resolve feature */ |
| 1184 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1185 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1186 | /* invert result */ |
| 1187 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1188 | case LYS_IFF_AND: |
| 1189 | case LYS_IFF_OR: |
| 1190 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1191 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1192 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1193 | return a && b; |
| 1194 | } else { /* LYS_IFF_OR */ |
| 1195 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1199 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | int |
| 1203 | resolve_iffeature(struct lys_iffeature *expr) |
| 1204 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1205 | int index_e = 0, index_f = 0; |
| 1206 | |
| 1207 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1208 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1209 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1210 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | struct iff_stack { |
| 1214 | int size; |
| 1215 | int index; /* first empty item */ |
| 1216 | uint8_t *stack; |
| 1217 | }; |
| 1218 | |
| 1219 | static int |
| 1220 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1221 | { |
| 1222 | if (stack->index == stack->size) { |
| 1223 | stack->size += 4; |
| 1224 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1225 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | stack->stack[stack->index++] = value; |
| 1229 | return EXIT_SUCCESS; |
| 1230 | } |
| 1231 | |
| 1232 | static uint8_t |
| 1233 | iff_stack_pop(struct iff_stack *stack) |
| 1234 | { |
| 1235 | stack->index--; |
| 1236 | return stack->stack[stack->index]; |
| 1237 | } |
| 1238 | |
| 1239 | static void |
| 1240 | iff_stack_clean(struct iff_stack *stack) |
| 1241 | { |
| 1242 | stack->size = 0; |
| 1243 | free(stack->stack); |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1248 | { |
| 1249 | uint8_t *item; |
| 1250 | uint8_t mask = 3; |
| 1251 | |
| 1252 | assert(pos >= 0); |
| 1253 | assert(op <= 3); /* max 2 bits */ |
| 1254 | |
| 1255 | item = &list[pos / 4]; |
| 1256 | mask = mask << 2 * (pos % 4); |
| 1257 | *item = (*item) & ~mask; |
| 1258 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1259 | } |
| 1260 | |
| 1261 | uint8_t |
| 1262 | iff_getop(uint8_t *list, int pos) |
| 1263 | { |
| 1264 | uint8_t *item; |
| 1265 | uint8_t mask = 3, result; |
| 1266 | |
| 1267 | assert(pos >= 0); |
| 1268 | |
| 1269 | item = &list[pos / 4]; |
| 1270 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1271 | return result >> 2 * (pos % 4); |
| 1272 | } |
| 1273 | |
| 1274 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1275 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1276 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1277 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1278 | struct unres_iffeat_data { |
| 1279 | struct lys_node *node; |
| 1280 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1281 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1282 | }; |
| 1283 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1284 | void |
| 1285 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1286 | { |
| 1287 | unsigned int e = 0, f = 0, r = 0; |
| 1288 | uint8_t op; |
| 1289 | |
| 1290 | assert(iffeat); |
| 1291 | |
| 1292 | if (!iffeat->expr) { |
| 1293 | goto result; |
| 1294 | } |
| 1295 | |
| 1296 | do { |
| 1297 | op = iff_getop(iffeat->expr, e++); |
| 1298 | switch (op) { |
| 1299 | case LYS_IFF_NOT: |
| 1300 | if (!r) { |
| 1301 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1302 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1303 | break; |
| 1304 | case LYS_IFF_AND: |
| 1305 | case LYS_IFF_OR: |
| 1306 | if (!r) { |
| 1307 | r += 2; |
| 1308 | } else { |
| 1309 | r += 1; |
| 1310 | } |
| 1311 | break; |
| 1312 | case LYS_IFF_F: |
| 1313 | f++; |
| 1314 | if (r) { |
| 1315 | r--; |
| 1316 | } |
| 1317 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1318 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1319 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1320 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1321 | result: |
| 1322 | if (expr_size) { |
| 1323 | *expr_size = e; |
| 1324 | } |
| 1325 | if (feat_size) { |
| 1326 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1331 | 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] | 1332 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1333 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1334 | const char *c = value; |
| 1335 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1336 | int i, j, last_not, checkversion = 0; |
| 1337 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1338 | uint8_t op; |
| 1339 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1340 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1341 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1342 | assert(c); |
| 1343 | |
| 1344 | if (isspace(c[0])) { |
| 1345 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1346 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1349 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1350 | for (i = j = last_not = 0; c[i]; i++) { |
| 1351 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1352 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1353 | j++; |
| 1354 | continue; |
| 1355 | } else if (c[i] == ')') { |
| 1356 | j--; |
| 1357 | continue; |
| 1358 | } else if (isspace(c[i])) { |
| 1359 | continue; |
| 1360 | } |
| 1361 | |
| 1362 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1363 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1364 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1365 | return EXIT_FAILURE; |
| 1366 | } else if (!isspace(c[i + r])) { |
| 1367 | /* feature name starting with the not/and/or */ |
| 1368 | last_not = 0; |
| 1369 | f_size++; |
| 1370 | } else if (c[i] == 'n') { /* not operation */ |
| 1371 | if (last_not) { |
| 1372 | /* double not */ |
| 1373 | expr_size = expr_size - 2; |
| 1374 | last_not = 0; |
| 1375 | } else { |
| 1376 | last_not = 1; |
| 1377 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1378 | } else { /* and, or */ |
| 1379 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1380 | /* not a not operation */ |
| 1381 | last_not = 0; |
| 1382 | } |
| 1383 | i += r; |
| 1384 | } else { |
| 1385 | f_size++; |
| 1386 | last_not = 0; |
| 1387 | } |
| 1388 | expr_size++; |
| 1389 | |
| 1390 | while (!isspace(c[i])) { |
| 1391 | if (!c[i] || c[i] == ')') { |
| 1392 | i--; |
| 1393 | break; |
| 1394 | } |
| 1395 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1396 | } |
| 1397 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1398 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1399 | /* not matching count of ( and ) */ |
| 1400 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1401 | return EXIT_FAILURE; |
| 1402 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1403 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1404 | if (checkversion || expr_size > 1) { |
| 1405 | /* check that we have 1.1 module */ |
| 1406 | if (node->module->version != 2) { |
| 1407 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1408 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1409 | return EXIT_FAILURE; |
| 1410 | } |
| 1411 | } |
| 1412 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1413 | /* allocate the memory */ |
| 1414 | 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] | 1415 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1416 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1417 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error); |
| 1418 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1419 | f_size--; expr_size--; /* used as indexes from now */ |
| 1420 | |
| 1421 | for (i--; i >= 0; i--) { |
| 1422 | if (c[i] == ')') { |
| 1423 | /* push it on stack */ |
| 1424 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1425 | continue; |
| 1426 | } else if (c[i] == '(') { |
| 1427 | /* pop from the stack into result all operators until ) */ |
| 1428 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1429 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1430 | } |
| 1431 | continue; |
| 1432 | } else if (isspace(c[i])) { |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
| 1436 | /* end operator or operand -> find beginning and get what is it */ |
| 1437 | j = i + 1; |
| 1438 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1439 | i--; |
| 1440 | } |
| 1441 | i++; /* get back by one step */ |
| 1442 | |
| 1443 | if (!strncmp(&c[i], "not ", 4)) { |
| 1444 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1445 | /* double not */ |
| 1446 | iff_stack_pop(&stack); |
| 1447 | } else { |
| 1448 | /* not has the highest priority, so do not pop from the stack |
| 1449 | * as in case of AND and OR */ |
| 1450 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1451 | } |
| 1452 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1453 | /* as for OR - pop from the stack all operators with the same or higher |
| 1454 | * priority and store them to the result, then push the AND to the stack */ |
| 1455 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1456 | op = iff_stack_pop(&stack); |
| 1457 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1458 | } |
| 1459 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1460 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1461 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1462 | op = iff_stack_pop(&stack); |
| 1463 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1464 | } |
| 1465 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1466 | } else { |
| 1467 | /* feature name, length is j - i */ |
| 1468 | |
| 1469 | /* add it to the result */ |
| 1470 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1471 | |
| 1472 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1473 | * forward referenced, we have to keep the feature name in auxiliary |
| 1474 | * structure passed into unres */ |
| 1475 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1476 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1477 | iff_data->node = node; |
| 1478 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1479 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1480 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1481 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1482 | f_size--; |
| 1483 | |
| 1484 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1485 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1486 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1487 | } |
| 1488 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1489 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1490 | while (stack.index) { |
| 1491 | op = iff_stack_pop(&stack); |
| 1492 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1493 | } |
| 1494 | |
| 1495 | if (++expr_size || ++f_size) { |
| 1496 | /* not all expected operators and operands found */ |
| 1497 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1498 | rc = EXIT_FAILURE; |
| 1499 | } else { |
| 1500 | rc = EXIT_SUCCESS; |
| 1501 | } |
| 1502 | |
| 1503 | error: |
| 1504 | /* cleanup */ |
| 1505 | iff_stack_clean(&stack); |
| 1506 | |
| 1507 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1511 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1512 | * |
| 1513 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1514 | * module). |
| 1515 | * |
| 1516 | */ |
| 1517 | struct lyd_node * |
| 1518 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1519 | { |
| 1520 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1521 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1522 | const struct lys_node *schema = NULL; |
| 1523 | |
| 1524 | assert(nodeid && start); |
| 1525 | |
| 1526 | if (nodeid[0] == '/') { |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | |
| 1530 | str = p = strdup(nodeid); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1531 | LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1532 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1533 | while (p) { |
| 1534 | token = p; |
| 1535 | p = strchr(p, '/'); |
| 1536 | if (p) { |
| 1537 | *p = '\0'; |
| 1538 | p++; |
| 1539 | } |
| 1540 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1541 | if (p) { |
| 1542 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1543 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1544 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1545 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1546 | result = NULL; |
| 1547 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1548 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1549 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1550 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1551 | continue; |
| 1552 | } |
| 1553 | } else { |
| 1554 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1555 | 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] | 1556 | || !schema) { |
| 1557 | result = NULL; |
| 1558 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1559 | } |
| 1560 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1561 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1562 | if (iter->schema == schema) { |
| 1563 | /* move in data tree according to returned schema */ |
| 1564 | result = iter; |
| 1565 | break; |
| 1566 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1567 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1568 | if (!iter) { |
| 1569 | /* instance not found */ |
| 1570 | result = NULL; |
| 1571 | break; |
| 1572 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1573 | } |
| 1574 | free(str); |
| 1575 | |
| 1576 | return result; |
| 1577 | } |
| 1578 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1579 | /* |
| 1580 | * 0 - ok (done) |
| 1581 | * 1 - continue |
| 1582 | * 2 - break |
| 1583 | * -1 - error |
| 1584 | */ |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1585 | int |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1586 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const char *id, const struct lys_module *module, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1587 | const char *mod_name, int mod_name_len, const struct lys_node **start_parent) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1588 | { |
| 1589 | const struct lys_module *prefix_mod; |
| 1590 | |
| 1591 | /* module check */ |
| 1592 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1593 | if (!prefix_mod) { |
| 1594 | return -1; |
| 1595 | } |
| 1596 | if (prefix_mod != lys_node_module(sibling)) { |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1600 | /* the result node? */ |
| 1601 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1605 | /* move down the tree, if possible */ |
| 1606 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1607 | return -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1608 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1609 | *start_parent = sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1610 | |
| 1611 | return 2; |
| 1612 | } |
| 1613 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1614 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1615 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1616 | int |
| 1617 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1618 | const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1619 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1620 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1621 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1622 | struct lys_node_augment *last_aug; |
| 1623 | int r, nam_len, mod_name_len = 0, is_relative = -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1624 | /* 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] | 1625 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1626 | |
| 1627 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1628 | |
| 1629 | id = nodeid; |
| 1630 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1631 | 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] | 1632 | return ((id - nodeid) - r) + 1; |
| 1633 | } |
| 1634 | id += r; |
| 1635 | |
| 1636 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1637 | return -1; |
| 1638 | } |
| 1639 | |
| 1640 | /* descendant-schema-nodeid */ |
| 1641 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1642 | module = start_mod = start->module; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1643 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1644 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1645 | /* absolute-schema-nodeid */ |
| 1646 | } else { |
| 1647 | 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] | 1648 | if (!start_mod) { |
| 1649 | return -1; |
| 1650 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1651 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | while (1) { |
| 1655 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1656 | last_aug = NULL; |
| 1657 | |
| 1658 | if (start_parent) { |
Michal Vasko | e4c8344 | 2017-07-11 08:49:01 +0200 | [diff] [blame] | 1659 | if (mod_name && (strncmp(mod_name, module->name, mod_name_len) |
| 1660 | || (mod_name_len != (signed)strlen(module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1661 | /* we are getting into another module (augment) */ |
| 1662 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1663 | if (!aux_mod) { |
| 1664 | return -1; |
| 1665 | } |
| 1666 | } else { |
Michal Vasko | 201c339 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1667 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1668 | * because this module may be not implemented and it augments something in another module and |
| 1669 | * there is another augment augmenting that previous one */ |
Michal Vasko | e4c8344 | 2017-07-11 08:49:01 +0200 | [diff] [blame] | 1670 | aux_mod = module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | /* if the module is implemented, all the augments will be connected */ |
| 1674 | if (!aux_mod->implemented) { |
| 1675 | get_next_augment: |
| 1676 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod, |
Michal Vasko | 5b99790 | 2017-04-03 14:16:22 +0200 | [diff] [blame] | 1681 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1682 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1683 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1684 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1685 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1686 | *ret = sibling; |
| 1687 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1688 | } else if (r == 1) { |
| 1689 | continue; |
| 1690 | } else if (r == 2) { |
| 1691 | break; |
| 1692 | } else { |
| 1693 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1694 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /* no match */ |
| 1699 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1700 | if (last_aug) { |
| 1701 | /* it still could be in another augment */ |
| 1702 | goto get_next_augment; |
| 1703 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1704 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1705 | return EXIT_SUCCESS; |
| 1706 | } |
| 1707 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1708 | 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] | 1709 | return ((id - nodeid) - r) + 1; |
| 1710 | } |
| 1711 | id += r; |
| 1712 | } |
| 1713 | |
| 1714 | /* cannot get here */ |
| 1715 | LOGINT; |
| 1716 | return -1; |
| 1717 | } |
| 1718 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1719 | /* unique, refine, |
| 1720 | * >0 - unexpected char on position (ret - 1), |
| 1721 | * 0 - ok (but ret can still be NULL), |
| 1722 | * -1 - error, |
| 1723 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1724 | int |
| 1725 | 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] | 1726 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1727 | { |
| 1728 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1729 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1730 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1731 | /* 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] | 1732 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1733 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1734 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1735 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1736 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1737 | if (!start) { |
| 1738 | /* leaf not found */ |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1742 | id = nodeid; |
| 1743 | module = start->module; |
| 1744 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1745 | 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] | 1746 | return ((id - nodeid) - r) + 1; |
| 1747 | } |
| 1748 | id += r; |
| 1749 | |
| 1750 | if (!is_relative) { |
| 1751 | return -1; |
| 1752 | } |
| 1753 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1754 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1755 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1756 | start_parent = lys_parent(start_parent); |
| 1757 | } |
| 1758 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1759 | while (1) { |
| 1760 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1761 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1762 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1763 | /* name match */ |
| 1764 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1765 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1766 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1767 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1768 | /* wrong node type, too bad */ |
| 1769 | continue; |
| 1770 | } |
| 1771 | *ret = sibling; |
| 1772 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1773 | } else if (r == 1) { |
| 1774 | continue; |
| 1775 | } else if (r == 2) { |
| 1776 | break; |
| 1777 | } else { |
| 1778 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* no match */ |
| 1784 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1785 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1786 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1787 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1788 | *ret = NULL; |
| 1789 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1790 | } |
| 1791 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1792 | 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] | 1793 | return ((id - nodeid) - r) + 1; |
| 1794 | } |
| 1795 | id += r; |
| 1796 | } |
| 1797 | |
| 1798 | /* cannot get here */ |
| 1799 | LOGINT; |
| 1800 | return -1; |
| 1801 | } |
| 1802 | |
| 1803 | /* choice default */ |
| 1804 | int |
| 1805 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1806 | { |
| 1807 | /* cannot actually be a path */ |
| 1808 | if (strchr(nodeid, '/')) { |
| 1809 | return -1; |
| 1810 | } |
| 1811 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1812 | 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] | 1813 | } |
| 1814 | |
| 1815 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1816 | static int |
| 1817 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1818 | { |
| 1819 | const struct lys_module *module; |
| 1820 | const char *mod_prefix, *name; |
| 1821 | int i, mod_prefix_len, nam_len; |
| 1822 | |
| 1823 | /* parse the identifier, it must be parsed on one call */ |
| 1824 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1825 | return -i + 1; |
| 1826 | } |
| 1827 | |
| 1828 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1829 | if (!module) { |
| 1830 | return -1; |
| 1831 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 1832 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1833 | start = module->data; |
| 1834 | } |
| 1835 | |
| 1836 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1837 | |
| 1838 | return EXIT_SUCCESS; |
| 1839 | } |
| 1840 | |
| 1841 | int |
| 1842 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1843 | const struct lys_node **ret) |
| 1844 | { |
| 1845 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1846 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1847 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1848 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1849 | |
| 1850 | assert(nodeid && module && ret); |
| 1851 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1852 | |
| 1853 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1854 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1855 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1856 | 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] | 1857 | return ((id - nodeid) - r) + 1; |
| 1858 | } |
| 1859 | id += r; |
| 1860 | |
| 1861 | if (is_relative) { |
| 1862 | return -1; |
| 1863 | } |
| 1864 | |
| 1865 | 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] | 1866 | if (!abs_start_mod) { |
| 1867 | return -1; |
| 1868 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1869 | |
| 1870 | while (1) { |
| 1871 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1872 | 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] | 1873 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1874 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1875 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1876 | r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len, &start_parent); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1877 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1878 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1879 | /* wrong node type, too bad */ |
| 1880 | continue; |
| 1881 | } |
| 1882 | *ret = sibling; |
| 1883 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1884 | } else if (r == 1) { |
| 1885 | continue; |
| 1886 | } else if (r == 2) { |
| 1887 | break; |
| 1888 | } else { |
| 1889 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1890 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | /* no match */ |
| 1895 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1896 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1897 | return EXIT_SUCCESS; |
| 1898 | } |
| 1899 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1900 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1901 | return ((id - nodeid) - r) + 1; |
| 1902 | } |
| 1903 | id += r; |
| 1904 | } |
| 1905 | |
| 1906 | /* cannot get here */ |
| 1907 | LOGINT; |
| 1908 | return -1; |
| 1909 | } |
| 1910 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1911 | static int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1912 | resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, |
| 1913 | const struct lys_module *cur_module, int *parsed) |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1914 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1915 | const char *mod_name, *name; |
| 1916 | int mod_name_len, nam_len, has_predicate, i; |
| 1917 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1918 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1919 | if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1920 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1921 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1922 | return -1; |
| 1923 | } |
| 1924 | |
| 1925 | predicate += i; |
| 1926 | *parsed += i; |
| 1927 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1928 | if (!isdigit(name[0])) { |
| 1929 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1930 | key = (struct lys_node *)list->keys[i]; |
| 1931 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
| 1932 | if (mod_name) { |
| 1933 | if (!strncmp(lys_node_module(key)->name, mod_name, mod_name_len) && !lys_node_module(key)->name[mod_name_len]) { |
| 1934 | break; |
| 1935 | } |
| 1936 | } else { |
| 1937 | if (!strcmp(lys_node_module(key)->name, cur_module->name)) { |
| 1938 | break; |
| 1939 | } |
| 1940 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 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 | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1952 | return resolve_json_schema_list_predicate(predicate, list, cur_module, 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 | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1967 | const struct lys_module *prefix_mod, *cur_module; |
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 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1988 | cur_module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 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'; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2007 | cur_module = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 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 | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2017 | if (!cur_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 | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2030 | while (1) { |
| 2031 | sibling = NULL; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2032 | while ((sibling = lys_getnext(sibling, start_parent, cur_module, |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2033 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2034 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2035 | 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] | 2036 | /* module check */ |
| 2037 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2038 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2039 | LOGINT; |
| 2040 | return NULL; |
| 2041 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2042 | |
| 2043 | if (ly_buf_used && module_name[0]) { |
| 2044 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2045 | } |
| 2046 | ly_buf_used++; |
| 2047 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2048 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2049 | module_name[mod_name_len] = '\0'; |
| 2050 | /* will also find an augment module */ |
| 2051 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2052 | |
| 2053 | if (buf_backup) { |
| 2054 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2055 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2056 | free(buf_backup); |
| 2057 | buf_backup = NULL; |
| 2058 | } |
| 2059 | ly_buf_used--; |
| 2060 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2061 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2062 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2063 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2064 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2065 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2066 | } |
| 2067 | } else { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2068 | prefix_mod = cur_module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2069 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2070 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2071 | continue; |
| 2072 | } |
| 2073 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2074 | /* do we have some predicates on it? */ |
| 2075 | if (has_predicate) { |
| 2076 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2077 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2078 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2079 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2080 | return NULL; |
| 2081 | } |
| 2082 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2083 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, cur_module, &r)) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2084 | return NULL; |
| 2085 | } |
| 2086 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2087 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2088 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2089 | } |
| 2090 | id += r; |
| 2091 | } |
| 2092 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2093 | /* the result node? */ |
| 2094 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2095 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2096 | } |
| 2097 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2098 | /* move down the tree, if possible */ |
| 2099 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2100 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2101 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2102 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2103 | start_parent = sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2104 | break; |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | /* no match */ |
| 2109 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2110 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2111 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2112 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2113 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2114 | } |
| 2115 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2116 | 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] | 2117 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
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 | id += r; |
| 2121 | } |
| 2122 | |
| 2123 | /* cannot get here */ |
| 2124 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2125 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2128 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2129 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2130 | int position, const struct lys_module *cur_module, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2131 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2132 | const char *mod_name, *name, *value, *key_val; |
| 2133 | int mod_name_len, nam_len, val_len, has_predicate = 1, r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2134 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2135 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2136 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2137 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2138 | assert(node->schema->nodetype == LYS_LIST); |
| 2139 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2140 | /* is the predicate a number? */ |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2141 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2142 | || !strncmp(name, ".", nam_len)) { |
| 2143 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2144 | return -1; |
| 2145 | } |
| 2146 | |
| 2147 | if (isdigit(name[0])) { |
| 2148 | if (position == atoi(name)) { |
| 2149 | /* match */ |
| 2150 | *parsed += r; |
| 2151 | return 0; |
| 2152 | } else { |
| 2153 | /* not a match */ |
| 2154 | return 1; |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2159 | /* no keys in schema - causes an error later */ |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2163 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2164 | if (!key) { |
| 2165 | /* it is not a position, so we need a key for it to be a match */ |
| 2166 | return 1; |
| 2167 | } |
| 2168 | |
| 2169 | /* go through all the keys */ |
| 2170 | i = 0; |
| 2171 | goto check_parsed_values; |
| 2172 | |
| 2173 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2174 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2175 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2176 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2179 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2180 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2181 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2182 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2185 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2186 | predicate += r; |
| 2187 | *parsed += r; |
| 2188 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2189 | 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] | 2190 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2191 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2194 | if (mod_name) { |
| 2195 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len) |
| 2196 | || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) { |
| 2197 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2198 | return -1; |
| 2199 | } |
| 2200 | } else { |
| 2201 | if (strcmp(lyd_node_module((struct lyd_node *)key)->name, cur_module->name)) { |
| 2202 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2203 | return -1; |
| 2204 | } |
| 2205 | } |
| 2206 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2207 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2208 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2209 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2210 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2211 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2212 | } else { |
| 2213 | key_val = key->value_str; |
| 2214 | } |
| 2215 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2216 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2217 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2218 | return 1; |
| 2219 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2220 | |
| 2221 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2225 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2226 | return -1; |
| 2227 | } |
| 2228 | |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2232 | /** |
| 2233 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2234 | * |
| 2235 | * @param[in] nodeid Node data path to find |
| 2236 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2237 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2238 | * @param[out] parsed Number of characters processed in \p id |
| 2239 | * @return The closes parent (or the node itself) from the path |
| 2240 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2241 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2242 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2243 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2244 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2245 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2246 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2247 | 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] | 2248 | 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] | 2249 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2250 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2251 | const struct lys_module *prefix_mod, *cur_module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2252 | struct ly_ctx *ctx; |
| 2253 | |
| 2254 | assert(nodeid && start && parsed); |
| 2255 | |
| 2256 | ctx = start->schema->module->ctx; |
| 2257 | id = nodeid; |
| 2258 | |
| 2259 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2260 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2261 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } |
| 2264 | id += r; |
| 2265 | /* add it to parsed only after the data node was actually found */ |
| 2266 | last_parsed = r; |
| 2267 | |
| 2268 | if (is_relative) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2269 | cur_module = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2270 | start = start->child; |
| 2271 | } else { |
| 2272 | for (; start->parent; start = start->parent); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2273 | cur_module = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2277 | list_instance_position = 0; |
| 2278 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2279 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2280 | /* 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] | 2281 | if (lys_parent(sibling->schema)) { |
| 2282 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2283 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2284 | 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] | 2285 | *parsed = -1; |
| 2286 | return NULL; |
| 2287 | } |
| 2288 | } else { |
| 2289 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2290 | 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] | 2291 | *parsed = -1; |
| 2292 | return NULL; |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2297 | /* name match */ |
| 2298 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2299 | |
| 2300 | /* module check */ |
| 2301 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2302 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2303 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2304 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2305 | return NULL; |
| 2306 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2307 | |
| 2308 | if (ly_buf_used && module_name[0]) { |
| 2309 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2310 | } |
| 2311 | ly_buf_used++; |
| 2312 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2313 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2314 | module_name[mod_name_len] = '\0'; |
| 2315 | /* will also find an augment module */ |
| 2316 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2317 | |
| 2318 | if (buf_backup) { |
| 2319 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2320 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2321 | free(buf_backup); |
| 2322 | buf_backup = NULL; |
| 2323 | } |
| 2324 | ly_buf_used--; |
| 2325 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2326 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2327 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2328 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2329 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2330 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2331 | return NULL; |
| 2332 | } |
| 2333 | } else { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2334 | prefix_mod = cur_module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2335 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2336 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2337 | continue; |
| 2338 | } |
| 2339 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2340 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2341 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2342 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2343 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2344 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2345 | if (has_predicate) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2346 | if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value, |
| 2347 | &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2348 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2349 | *parsed = -1; |
| 2350 | return NULL; |
| 2351 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2352 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2353 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2354 | *parsed = -1; |
| 2355 | return NULL; |
| 2356 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2357 | } else { |
| 2358 | r = 0; |
| 2359 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2360 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2364 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2365 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2366 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2367 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2368 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2369 | } else { |
| 2370 | data_val = llist->value_str; |
| 2371 | } |
| 2372 | |
| 2373 | if ((!llist_value && data_val && data_val[0]) |
| 2374 | || (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] | 2375 | continue; |
| 2376 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2377 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2378 | id += r; |
| 2379 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2380 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2381 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2382 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2383 | /* 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] | 2384 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2385 | /* none match */ |
| 2386 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2387 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2388 | |
| 2389 | ++list_instance_position; |
| 2390 | r = 0; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2391 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, cur_module, &r); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2392 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2393 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2394 | return NULL; |
| 2395 | } else if (ret == 1) { |
| 2396 | /* this list instance does not match */ |
| 2397 | continue; |
| 2398 | } |
| 2399 | id += r; |
| 2400 | last_parsed += r; |
| 2401 | } |
| 2402 | |
| 2403 | *parsed += last_parsed; |
| 2404 | |
| 2405 | /* the result node? */ |
| 2406 | if (!id[0]) { |
| 2407 | return sibling; |
| 2408 | } |
| 2409 | |
| 2410 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2411 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2412 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2413 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2414 | return NULL; |
| 2415 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2416 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2417 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2418 | break; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | /* no match, return last match */ |
| 2423 | if (!sibling) { |
| 2424 | return last_match; |
| 2425 | } |
| 2426 | |
| 2427 | 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] | 2428 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2429 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2430 | return NULL; |
| 2431 | } |
| 2432 | id += r; |
| 2433 | last_parsed = r; |
| 2434 | } |
| 2435 | |
| 2436 | /* cannot get here */ |
| 2437 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2438 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2439 | return NULL; |
| 2440 | } |
| 2441 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2442 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2443 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2444 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2445 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2446 | * @param[in] str_restr Restriction as a string. |
| 2447 | * @param[in] type Type of the restriction. |
| 2448 | * @param[out] ret Final interval structure that starts with |
| 2449 | * the interval of the initial type, continues with intervals |
| 2450 | * of any superior types derived from the initial one, and |
| 2451 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2452 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2453 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2454 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2455 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2456 | 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] | 2457 | { |
| 2458 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2459 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2460 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2461 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2462 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2463 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2464 | 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] | 2465 | |
| 2466 | switch (type->base) { |
| 2467 | case LY_TYPE_BINARY: |
| 2468 | kind = 0; |
| 2469 | local_umin = 0; |
| 2470 | local_umax = 18446744073709551615UL; |
| 2471 | |
| 2472 | if (!str_restr && type->info.binary.length) { |
| 2473 | str_restr = type->info.binary.length->expr; |
| 2474 | } |
| 2475 | break; |
| 2476 | case LY_TYPE_DEC64: |
| 2477 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2478 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2479 | local_fmax = __INT64_C(9223372036854775807); |
| 2480 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2481 | |
| 2482 | if (!str_restr && type->info.dec64.range) { |
| 2483 | str_restr = type->info.dec64.range->expr; |
| 2484 | } |
| 2485 | break; |
| 2486 | case LY_TYPE_INT8: |
| 2487 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2488 | local_smin = __INT64_C(-128); |
| 2489 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2490 | |
| 2491 | if (!str_restr && type->info.num.range) { |
| 2492 | str_restr = type->info.num.range->expr; |
| 2493 | } |
| 2494 | break; |
| 2495 | case LY_TYPE_INT16: |
| 2496 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2497 | local_smin = __INT64_C(-32768); |
| 2498 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2499 | |
| 2500 | if (!str_restr && type->info.num.range) { |
| 2501 | str_restr = type->info.num.range->expr; |
| 2502 | } |
| 2503 | break; |
| 2504 | case LY_TYPE_INT32: |
| 2505 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2506 | local_smin = __INT64_C(-2147483648); |
| 2507 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2508 | |
| 2509 | if (!str_restr && type->info.num.range) { |
| 2510 | str_restr = type->info.num.range->expr; |
| 2511 | } |
| 2512 | break; |
| 2513 | case LY_TYPE_INT64: |
| 2514 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2515 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2516 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2517 | |
| 2518 | if (!str_restr && type->info.num.range) { |
| 2519 | str_restr = type->info.num.range->expr; |
| 2520 | } |
| 2521 | break; |
| 2522 | case LY_TYPE_UINT8: |
| 2523 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2524 | local_umin = __UINT64_C(0); |
| 2525 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2526 | |
| 2527 | if (!str_restr && type->info.num.range) { |
| 2528 | str_restr = type->info.num.range->expr; |
| 2529 | } |
| 2530 | break; |
| 2531 | case LY_TYPE_UINT16: |
| 2532 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2533 | local_umin = __UINT64_C(0); |
| 2534 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2535 | |
| 2536 | if (!str_restr && type->info.num.range) { |
| 2537 | str_restr = type->info.num.range->expr; |
| 2538 | } |
| 2539 | break; |
| 2540 | case LY_TYPE_UINT32: |
| 2541 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2542 | local_umin = __UINT64_C(0); |
| 2543 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2544 | |
| 2545 | if (!str_restr && type->info.num.range) { |
| 2546 | str_restr = type->info.num.range->expr; |
| 2547 | } |
| 2548 | break; |
| 2549 | case LY_TYPE_UINT64: |
| 2550 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2551 | local_umin = __UINT64_C(0); |
| 2552 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2553 | |
| 2554 | if (!str_restr && type->info.num.range) { |
| 2555 | str_restr = type->info.num.range->expr; |
| 2556 | } |
| 2557 | break; |
| 2558 | case LY_TYPE_STRING: |
| 2559 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2560 | local_umin = __UINT64_C(0); |
| 2561 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2562 | |
| 2563 | if (!str_restr && type->info.str.length) { |
| 2564 | str_restr = type->info.str.length->expr; |
| 2565 | } |
| 2566 | break; |
| 2567 | default: |
| 2568 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2569 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2573 | if (type->der) { |
| 2574 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2575 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2576 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2577 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2578 | assert(!intv || (intv->kind == kind)); |
| 2579 | } |
| 2580 | |
| 2581 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2582 | /* we do not have any restriction, return superior ones */ |
| 2583 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2584 | return EXIT_SUCCESS; |
| 2585 | } |
| 2586 | |
| 2587 | /* adjust local min and max */ |
| 2588 | if (intv) { |
| 2589 | tmp_intv = intv; |
| 2590 | |
| 2591 | if (kind == 0) { |
| 2592 | local_umin = tmp_intv->value.uval.min; |
| 2593 | } else if (kind == 1) { |
| 2594 | local_smin = tmp_intv->value.sval.min; |
| 2595 | } else if (kind == 2) { |
| 2596 | local_fmin = tmp_intv->value.fval.min; |
| 2597 | } |
| 2598 | |
| 2599 | while (tmp_intv->next) { |
| 2600 | tmp_intv = tmp_intv->next; |
| 2601 | } |
| 2602 | |
| 2603 | if (kind == 0) { |
| 2604 | local_umax = tmp_intv->value.uval.max; |
| 2605 | } else if (kind == 1) { |
| 2606 | local_smax = tmp_intv->value.sval.max; |
| 2607 | } else if (kind == 2) { |
| 2608 | local_fmax = tmp_intv->value.fval.max; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | /* finally parse our restriction */ |
| 2613 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2614 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2615 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2616 | if (!tmp_local_intv) { |
| 2617 | assert(!local_intv); |
| 2618 | local_intv = malloc(sizeof *local_intv); |
| 2619 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2620 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2621 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2622 | tmp_local_intv = tmp_local_intv->next; |
| 2623 | } |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2624 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2625 | |
| 2626 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2627 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2628 | tmp_local_intv->next = NULL; |
| 2629 | |
| 2630 | /* min */ |
| 2631 | ptr = seg_ptr; |
| 2632 | while (isspace(ptr[0])) { |
| 2633 | ++ptr; |
| 2634 | } |
| 2635 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2636 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame^] | 2637 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2638 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame^] | 2639 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2640 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2641 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2642 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2643 | goto error; |
| 2644 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2645 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2646 | } else if (!strncmp(ptr, "min", 3)) { |
| 2647 | if (kind == 0) { |
| 2648 | tmp_local_intv->value.uval.min = local_umin; |
| 2649 | } else if (kind == 1) { |
| 2650 | tmp_local_intv->value.sval.min = local_smin; |
| 2651 | } else if (kind == 2) { |
| 2652 | tmp_local_intv->value.fval.min = local_fmin; |
| 2653 | } |
| 2654 | |
| 2655 | ptr += 3; |
| 2656 | } else if (!strncmp(ptr, "max", 3)) { |
| 2657 | if (kind == 0) { |
| 2658 | tmp_local_intv->value.uval.min = local_umax; |
| 2659 | } else if (kind == 1) { |
| 2660 | tmp_local_intv->value.sval.min = local_smax; |
| 2661 | } else if (kind == 2) { |
| 2662 | tmp_local_intv->value.fval.min = local_fmax; |
| 2663 | } |
| 2664 | |
| 2665 | ptr += 3; |
| 2666 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2667 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2668 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | while (isspace(ptr[0])) { |
| 2672 | ptr++; |
| 2673 | } |
| 2674 | |
| 2675 | /* no interval or interval */ |
| 2676 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2677 | if (kind == 0) { |
| 2678 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2679 | } else if (kind == 1) { |
| 2680 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2681 | } else if (kind == 2) { |
| 2682 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2683 | } |
| 2684 | } else if (!strncmp(ptr, "..", 2)) { |
| 2685 | /* skip ".." */ |
| 2686 | ptr += 2; |
| 2687 | while (isspace(ptr[0])) { |
| 2688 | ++ptr; |
| 2689 | } |
| 2690 | |
| 2691 | /* max */ |
| 2692 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2693 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame^] | 2694 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2695 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame^] | 2696 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2697 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2698 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2699 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2700 | goto error; |
| 2701 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2702 | } |
| 2703 | } else if (!strncmp(ptr, "max", 3)) { |
| 2704 | if (kind == 0) { |
| 2705 | tmp_local_intv->value.uval.max = local_umax; |
| 2706 | } else if (kind == 1) { |
| 2707 | tmp_local_intv->value.sval.max = local_smax; |
| 2708 | } else if (kind == 2) { |
| 2709 | tmp_local_intv->value.fval.max = local_fmax; |
| 2710 | } |
| 2711 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2712 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2713 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2714 | } |
| 2715 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2716 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2717 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2718 | } |
| 2719 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2720 | /* check min and max in correct order*/ |
| 2721 | if (kind == 0) { |
| 2722 | /* current segment */ |
| 2723 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2724 | goto error; |
| 2725 | } |
| 2726 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2727 | goto error; |
| 2728 | } |
| 2729 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2730 | 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] | 2731 | goto error; |
| 2732 | } |
| 2733 | } else if (kind == 1) { |
| 2734 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2735 | goto error; |
| 2736 | } |
| 2737 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2738 | goto error; |
| 2739 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2740 | 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] | 2741 | goto error; |
| 2742 | } |
| 2743 | } else if (kind == 2) { |
| 2744 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2745 | goto error; |
| 2746 | } |
| 2747 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2748 | goto error; |
| 2749 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2750 | 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] | 2751 | /* 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] | 2752 | goto error; |
| 2753 | } |
| 2754 | } |
| 2755 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2756 | /* next segment (next OR) */ |
| 2757 | seg_ptr = strchr(seg_ptr, '|'); |
| 2758 | if (!seg_ptr) { |
| 2759 | break; |
| 2760 | } |
| 2761 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2762 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | /* check local restrictions against superior ones */ |
| 2766 | if (intv) { |
| 2767 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2768 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2769 | |
| 2770 | while (tmp_local_intv && tmp_intv) { |
| 2771 | /* reuse local variables */ |
| 2772 | if (kind == 0) { |
| 2773 | local_umin = tmp_local_intv->value.uval.min; |
| 2774 | local_umax = tmp_local_intv->value.uval.max; |
| 2775 | |
| 2776 | /* it must be in this interval */ |
| 2777 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2778 | /* this interval is covered, next one */ |
| 2779 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2780 | tmp_local_intv = tmp_local_intv->next; |
| 2781 | continue; |
| 2782 | /* ascending order of restrictions -> fail */ |
| 2783 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2784 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | } else if (kind == 1) { |
| 2788 | local_smin = tmp_local_intv->value.sval.min; |
| 2789 | local_smax = tmp_local_intv->value.sval.max; |
| 2790 | |
| 2791 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2792 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2793 | tmp_local_intv = tmp_local_intv->next; |
| 2794 | continue; |
| 2795 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2796 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | } else if (kind == 2) { |
| 2800 | local_fmin = tmp_local_intv->value.fval.min; |
| 2801 | local_fmax = tmp_local_intv->value.fval.max; |
| 2802 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2803 | 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] | 2804 | && (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] | 2805 | 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] | 2806 | tmp_local_intv = tmp_local_intv->next; |
| 2807 | continue; |
| 2808 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2809 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | tmp_intv = tmp_intv->next; |
| 2815 | } |
| 2816 | |
| 2817 | /* some interval left uncovered -> fail */ |
| 2818 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2819 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2820 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2823 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2824 | if (intv) { |
| 2825 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2826 | tmp_intv->next = local_intv; |
| 2827 | } else { |
| 2828 | intv = local_intv; |
| 2829 | } |
| 2830 | *ret = intv; |
| 2831 | |
| 2832 | return EXIT_SUCCESS; |
| 2833 | |
| 2834 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2835 | while (intv) { |
| 2836 | tmp_intv = intv->next; |
| 2837 | free(intv); |
| 2838 | intv = tmp_intv; |
| 2839 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2840 | while (local_intv) { |
| 2841 | tmp_local_intv = local_intv->next; |
| 2842 | free(local_intv); |
| 2843 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2844 | } |
| 2845 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2846 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2849 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2850 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2851 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2852 | * |
| 2853 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2854 | * @param[in] mod_name Typedef name module name. |
| 2855 | * @param[in] module Main module. |
| 2856 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2857 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2858 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2859 | * @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] | 2860 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2861 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2862 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2863 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2864 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2865 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2866 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2867 | int tpdf_size; |
| 2868 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2869 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2870 | /* no prefix, try built-in types */ |
| 2871 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2872 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2873 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 2874 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2875 | } |
| 2876 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2880 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2881 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2882 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2886 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2887 | /* search in local typedefs */ |
| 2888 | while (parent) { |
| 2889 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2890 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2891 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2892 | tpdf = ((struct lys_node_container *)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_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2896 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2897 | tpdf = ((struct lys_node_list *)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_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2901 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2902 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2903 | break; |
| 2904 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2905 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2906 | case LYS_ACTION: |
| 2907 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2908 | tpdf = ((struct lys_node_rpc_action *)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_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2912 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2913 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2914 | break; |
| 2915 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2916 | case LYS_INPUT: |
| 2917 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2918 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2919 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2920 | break; |
| 2921 | |
| 2922 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2923 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2924 | continue; |
| 2925 | } |
| 2926 | |
| 2927 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2928 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2929 | match = &tpdf[i]; |
| 2930 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2934 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2935 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2936 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2937 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2938 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2939 | if (!module) { |
| 2940 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | /* search in top level typedefs */ |
| 2945 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2946 | 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] | 2947 | match = &module->tpdf[i]; |
| 2948 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2953 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2954 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2955 | 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] | 2956 | match = &module->inc[i].submodule->tpdf[j]; |
| 2957 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | } |
| 2961 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2962 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2963 | |
| 2964 | check_leafref: |
| 2965 | if (ret) { |
| 2966 | *ret = match; |
| 2967 | } |
| 2968 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 2969 | while (!match->type.info.lref.path) { |
| 2970 | match = match->type.der; |
| 2971 | assert(match); |
| 2972 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2973 | } |
| 2974 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2977 | /** |
| 2978 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2979 | * |
| 2980 | * @param[in] type Type definition to use. |
| 2981 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2982 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2983 | * |
| 2984 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2985 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2986 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 2987 | 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] | 2988 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 2989 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2990 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2991 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 2992 | char *s; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2993 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2994 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2995 | assert(value); |
| 2996 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2997 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 2998 | /* the type was not resolved yet, nothing to do for now */ |
| 2999 | return EXIT_FAILURE; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3000 | } else if (!tpdf && !lys_main_module(module)->implemented) { |
| 3001 | /* do not check defaults in not implemented module's data */ |
| 3002 | return EXIT_SUCCESS; |
| 3003 | } else if (tpdf && !lys_main_module(module)->implemented && type->base == LY_TYPE_IDENT) { |
| 3004 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3005 | * but in typedef the value has to be modified to include the prefix */ |
| 3006 | if (*value) { |
| 3007 | if (strchr(*value, ':')) { |
| 3008 | dflt = transform_schema2json(module, *value); |
| 3009 | } else { |
| 3010 | /* default prefix of the module where the typedef is defined */ |
| 3011 | asprintf(&s, "%s:%s", lys_main_module(module)->name, *value); |
| 3012 | dflt = lydict_insert_zc(module->ctx, s); |
| 3013 | } |
| 3014 | lydict_remove(module->ctx, *value); |
| 3015 | *value = dflt; |
| 3016 | } |
| 3017 | return EXIT_SUCCESS; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3018 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3019 | /* leafref in typedef cannot be checked */ |
| 3020 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3021 | } |
| 3022 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3023 | dflt = *value; |
| 3024 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3025 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3026 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3027 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3028 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3029 | break; |
| 3030 | } |
| 3031 | } |
| 3032 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3033 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3034 | /* no default value, nothing to check, all is well */ |
| 3035 | return EXIT_SUCCESS; |
| 3036 | } |
| 3037 | |
| 3038 | /* 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)? */ |
| 3039 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3040 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3041 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
| 3042 | return EXIT_SUCCESS; |
| 3043 | } else { |
| 3044 | /* check the default value from typedef, but use also the typedef's module |
| 3045 | * due to possible searching in imported modules which is expected in |
| 3046 | * typedef's module instead of module where the typedef is used */ |
| 3047 | module = base_tpdf->module; |
| 3048 | } |
| 3049 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3050 | case LY_TYPE_INST: |
| 3051 | case LY_TYPE_LEAFREF: |
| 3052 | case LY_TYPE_BOOL: |
| 3053 | case LY_TYPE_EMPTY: |
| 3054 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3055 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3056 | case LY_TYPE_BITS: |
| 3057 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3058 | if (type->info.bits.count) { |
| 3059 | break; |
| 3060 | } |
| 3061 | return EXIT_SUCCESS; |
| 3062 | case LY_TYPE_ENUM: |
| 3063 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3064 | if (type->info.enums.count) { |
| 3065 | break; |
| 3066 | } |
| 3067 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3068 | case LY_TYPE_DEC64: |
| 3069 | if (type->info.dec64.range) { |
| 3070 | break; |
| 3071 | } |
| 3072 | return EXIT_SUCCESS; |
| 3073 | case LY_TYPE_BINARY: |
| 3074 | if (type->info.binary.length) { |
| 3075 | break; |
| 3076 | } |
| 3077 | return EXIT_SUCCESS; |
| 3078 | case LY_TYPE_INT8: |
| 3079 | case LY_TYPE_INT16: |
| 3080 | case LY_TYPE_INT32: |
| 3081 | case LY_TYPE_INT64: |
| 3082 | case LY_TYPE_UINT8: |
| 3083 | case LY_TYPE_UINT16: |
| 3084 | case LY_TYPE_UINT32: |
| 3085 | case LY_TYPE_UINT64: |
| 3086 | if (type->info.num.range) { |
| 3087 | break; |
| 3088 | } |
| 3089 | return EXIT_SUCCESS; |
| 3090 | case LY_TYPE_STRING: |
| 3091 | if (type->info.str.length || type->info.str.patterns) { |
| 3092 | break; |
| 3093 | } |
| 3094 | return EXIT_SUCCESS; |
| 3095 | case LY_TYPE_UNION: |
| 3096 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3097 | break; |
| 3098 | default: |
| 3099 | LOGINT; |
| 3100 | return -1; |
| 3101 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3102 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3103 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3104 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3105 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3106 | } |
| 3107 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3108 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3109 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3110 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3111 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3112 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3113 | LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3114 | node.schema->name = strdup("fake-default"); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3115 | LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1); |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3116 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3117 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3118 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3119 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3120 | if (!type->info.lref.target) { |
| 3121 | ret = EXIT_FAILURE; |
| 3122 | goto finish; |
| 3123 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3124 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3125 | if (!ret) { |
| 3126 | /* adopt possibly changed default value to its canonical form */ |
| 3127 | if (*value) { |
| 3128 | *value = dflt; |
| 3129 | } |
| 3130 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3131 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3132 | 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] | 3133 | /* possible forward reference */ |
| 3134 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3135 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3136 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3137 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3138 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3139 | /* we have refined bits/enums */ |
| 3140 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3141 | "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] | 3142 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3143 | } |
| 3144 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3145 | } else { |
| 3146 | /* success - adopt canonical form from the node into the default value */ |
| 3147 | if (dflt != node.value_str) { |
| 3148 | /* this can happen only if we have non-inherited default value, |
| 3149 | * inherited default values are already in canonical form */ |
| 3150 | assert(dflt == *value); |
| 3151 | *value = node.value_str; |
| 3152 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3153 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | finish: |
| 3157 | if (node.value_type == LY_TYPE_BITS) { |
| 3158 | free(node.value.bit); |
| 3159 | } |
| 3160 | free((char *)node.schema->name); |
| 3161 | free(node.schema); |
| 3162 | |
| 3163 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3166 | /** |
| 3167 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3168 | * |
| 3169 | * @param[in] key The key to check. |
| 3170 | * @param[in] flags What flags to check. |
| 3171 | * @param[in] list The list of all the keys. |
| 3172 | * @param[in] index Index of the key in the key list. |
| 3173 | * @param[in] name The name of the keys. |
| 3174 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3175 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3176 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3177 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3178 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3179 | 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] | 3180 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3181 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3182 | char *dup = NULL; |
| 3183 | int j; |
| 3184 | |
| 3185 | /* existence */ |
| 3186 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3187 | if (name[len] != '\0') { |
| 3188 | dup = strdup(name); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3189 | LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3190 | dup[len] = '\0'; |
| 3191 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3192 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3193 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3194 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3195 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | /* uniqueness */ |
| 3199 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3200 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3201 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3202 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3207 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3208 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3209 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3213 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3214 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3215 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3219 | 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] | 3220 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3221 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3224 | /* key is not placed from augment */ |
| 3225 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3226 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3227 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3228 | return -1; |
| 3229 | } |
| 3230 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3231 | /* key is not when/if-feature -conditional */ |
| 3232 | j = 0; |
| 3233 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3234 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3235 | 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] | 3236 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3237 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3238 | } |
| 3239 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3240 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3241 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3242 | |
| 3243 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3244 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3245 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3246 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3247 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3248 | * |
| 3249 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3250 | */ |
| 3251 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3252 | 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] | 3253 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3254 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3255 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3256 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3257 | 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] | 3258 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3259 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3260 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3261 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3262 | 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] | 3263 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3264 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3265 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3266 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3267 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3268 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3269 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3270 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3271 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3272 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3273 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3274 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3275 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3276 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3277 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3278 | } |
| 3279 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3280 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3281 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3282 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3283 | return -1; |
| 3284 | } |
| 3285 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3286 | /* check that all unique's targets are of the same config type */ |
| 3287 | if (*trg_type) { |
| 3288 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3289 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3290 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3291 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3292 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3293 | return -1; |
| 3294 | } |
| 3295 | } else { |
| 3296 | /* first unique */ |
| 3297 | if (leaf->flags & LYS_CONFIG_W) { |
| 3298 | *trg_type = 1; |
| 3299 | } else { |
| 3300 | *trg_type = 2; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3304 | /* set leaf's unique flag */ |
| 3305 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3306 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3307 | return EXIT_SUCCESS; |
| 3308 | |
| 3309 | error: |
| 3310 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3311 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3312 | } |
| 3313 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3314 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3315 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3316 | { |
| 3317 | /* there are items after the one deleted */ |
| 3318 | if (i+1 < unres->count) { |
| 3319 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3320 | 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] | 3321 | |
| 3322 | /* deleting the last item */ |
| 3323 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3324 | free(unres->node); |
| 3325 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3329 | --unres->count; |
| 3330 | } |
| 3331 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3332 | /** |
| 3333 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3334 | * |
| 3335 | * @param[in] mod Module to search in. |
| 3336 | * @param[in] name Name of the data node. |
| 3337 | * @param[in] nam_len Length of the name. |
| 3338 | * @param[in] start Data node to start the search from. |
| 3339 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3340 | * they are replaced (!!) with the resolvents. |
| 3341 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3342 | * @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] | 3343 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3344 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3345 | 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] | 3346 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3347 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3348 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3349 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3350 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3351 | if (!parents->count) { |
| 3352 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3353 | parents->node = malloc(sizeof *parents->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3354 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3355 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3356 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3357 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3358 | 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] | 3359 | /* skip */ |
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 | continue; |
| 3362 | } |
| 3363 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3364 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3365 | if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | && node->schema->name[nam_len] == '\0') { |
| 3367 | /* matching target */ |
| 3368 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3369 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3370 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3371 | flag = 1; |
| 3372 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3373 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3374 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3375 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3376 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3377 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3378 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3382 | |
| 3383 | if (!flag) { |
| 3384 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3385 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3386 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3387 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3388 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3391 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3394 | /** |
| 3395 | * @brief Resolve (find) a data node. Does not log. |
| 3396 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3397 | * @param[in] mod_name Module name of the data node. |
| 3398 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3399 | * @param[in] name Name of the data node. |
| 3400 | * @param[in] nam_len Length of the name. |
| 3401 | * @param[in] start Data node to start the search from. |
| 3402 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3403 | * they are replaced (!!) with the resolvents. |
| 3404 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3405 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3406 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3407 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3408 | 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] | 3409 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3410 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3411 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3412 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3413 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3414 | assert(start); |
| 3415 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3416 | if (mod_name) { |
| 3417 | /* we have mod_name, find appropriate module */ |
| 3418 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3419 | if (!str) { |
| 3420 | LOGMEM; |
| 3421 | return -1; |
| 3422 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3423 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3424 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3425 | if (!mod) { |
| 3426 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3427 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3428 | } |
| 3429 | } else { |
| 3430 | /* no prefix, module is the same as of current node */ |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3431 | mod = lyd_node_module(start); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3437 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3438 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3439 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3440 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3441 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3442 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3443 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3444 | * without the predicate. Nodes not |
| 3445 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3446 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3447 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3448 | * @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] | 3449 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3450 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3451 | 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] | 3452 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3454 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3455 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3456 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3457 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3458 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3459 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3460 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3461 | |
| 3462 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3463 | source_match.node = malloc(sizeof *source_match.node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3464 | LY_CHECK_ERR_RETURN(!source_match.node, LOGMEM, -1); |
| 3465 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3466 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3467 | dest_match.node = malloc(sizeof *dest_match.node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3468 | LY_CHECK_ERR_RETURN(!dest_match.node, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3469 | |
| 3470 | do { |
| 3471 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3472 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3473 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3474 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3475 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3476 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3477 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3478 | pred += i; |
| 3479 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3480 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3481 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3482 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3483 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3484 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3485 | 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] | 3486 | &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] | 3487 | i = 0; |
| 3488 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3492 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3493 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3494 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3495 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3496 | 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] | 3497 | rc = -1; |
| 3498 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3499 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3500 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3501 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3502 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3503 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3504 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3505 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3506 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3510 | 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] | 3511 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3512 | i = 0; |
| 3513 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | if (pke_len == pke_parsed) { |
| 3517 | break; |
| 3518 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3519 | 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] | 3520 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3521 | 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] | 3522 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3523 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3524 | } |
| 3525 | pke_parsed += i; |
| 3526 | } |
| 3527 | |
| 3528 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3529 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3530 | while (leaf_dst && leaf_dst->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3531 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3532 | } |
| 3533 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3534 | while (leaf_src && leaf_src->value_type == LY_TYPE_LEAFREF) { |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3535 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3536 | } |
Radek Krejci | 981a8ee | 2017-03-17 16:48:26 +0100 | [diff] [blame] | 3537 | if (!leaf_src || !leaf_dst) { |
| 3538 | /* not yet resolved leafrefs */ |
| 3539 | return EXIT_FAILURE; |
| 3540 | } |
Radek Krejci | c7408e7 | 2017-03-17 10:43:51 +0100 | [diff] [blame] | 3541 | 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] | 3542 | goto remove_leafref; |
| 3543 | } |
| 3544 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3545 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3546 | goto remove_leafref; |
| 3547 | } |
| 3548 | |
| 3549 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3550 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3551 | continue; |
| 3552 | |
| 3553 | remove_leafref: |
| 3554 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3555 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3556 | } |
| 3557 | } while (has_predicate); |
| 3558 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3559 | free(source_match.node); |
| 3560 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3561 | if (parsed) { |
| 3562 | *parsed = parsed_loc; |
| 3563 | } |
| 3564 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3565 | |
| 3566 | error: |
| 3567 | |
| 3568 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3569 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3570 | } |
| 3571 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3572 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3573 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3574 | if (parsed) { |
| 3575 | *parsed = -parsed_loc+i; |
| 3576 | } |
| 3577 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | } |
| 3579 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3580 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3581 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3582 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3583 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3584 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3585 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3586 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3587 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3588 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3589 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3590 | 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] | 3591 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3592 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3593 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3594 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3595 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3596 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3597 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3598 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3599 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3600 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3601 | |
| 3602 | /* searching for nodeset */ |
| 3603 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3604 | 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] | 3605 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3606 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3607 | goto error; |
| 3608 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3609 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3610 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3611 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3612 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3613 | if (parent_times > 0) { |
| 3614 | data = node; |
| 3615 | for (i = 1; i < parent_times; ++i) { |
| 3616 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3617 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3618 | } else if (!parent_times) { |
| 3619 | data = node->child; |
| 3620 | } else { |
| 3621 | /* absolute path */ |
| 3622 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3623 | } |
| 3624 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3625 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3626 | if (data->prev) { |
| 3627 | while (data->prev->next) { |
| 3628 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3629 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3634 | 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] | 3635 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3636 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3637 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3638 | goto error; |
| 3639 | } |
| 3640 | |
| 3641 | if (has_predicate) { |
| 3642 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3643 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3644 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3645 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3646 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3647 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3648 | continue; |
| 3649 | } |
| 3650 | |
| 3651 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3652 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3653 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3654 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3655 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3656 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3657 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3658 | goto error; |
| 3659 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3660 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3661 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3662 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3663 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3664 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3665 | goto error; |
| 3666 | } |
| 3667 | } |
| 3668 | } while (path[0] != '\0'); |
| 3669 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3670 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3671 | |
| 3672 | error: |
| 3673 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3674 | free(ret->node); |
| 3675 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3676 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3677 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3678 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3681 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3682 | 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] | 3683 | { |
| 3684 | int dep1, dep2; |
| 3685 | const struct lys_node *node; |
| 3686 | |
| 3687 | if (lys_parent(op_node)) { |
| 3688 | /* inner operation (notif/action) */ |
| 3689 | if (abs_path) { |
| 3690 | return 1; |
| 3691 | } else { |
| 3692 | /* compare depth of both nodes */ |
| 3693 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3694 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3695 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3696 | return 1; |
| 3697 | } |
| 3698 | } |
| 3699 | } else { |
| 3700 | /* top-level operation (notif/rpc) */ |
| 3701 | if (op_node != first_node) { |
| 3702 | return 1; |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | return 0; |
| 3707 | } |
| 3708 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3709 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3710 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3711 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3712 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3713 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3714 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3715 | * @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] | 3716 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3717 | * @return 0 on forward reference, otherwise the number |
| 3718 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3719 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3720 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3721 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3722 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3723 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3724 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3725 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3726 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3727 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3728 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3729 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3730 | |
| 3731 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3732 | 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] | 3733 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3734 | 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] | 3735 | return -parsed+i; |
| 3736 | } |
| 3737 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3738 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3739 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3740 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3741 | if (sour_pref) { |
| 3742 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3743 | } else { |
| 3744 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3745 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3746 | rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3747 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3748 | 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] | 3749 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3753 | dest_parent_times = 0; |
| 3754 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3755 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3756 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3757 | 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] | 3758 | return -parsed; |
| 3759 | } |
| 3760 | pke_parsed += i; |
| 3761 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3762 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3763 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3764 | * all schema nodes that cannot be instantiated in data tree */ |
| 3765 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3766 | 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] | 3767 | dst_node = lys_parent(dst_node)); |
| 3768 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3769 | if (!dst_node) { |
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 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3775 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3776 | if (dest_pref) { |
| 3777 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3778 | } else { |
| 3779 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3780 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3781 | rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3782 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3783 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3784 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3785 | } |
| 3786 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3787 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3788 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3789 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3790 | } |
| 3791 | first_iter = 0; |
| 3792 | } |
| 3793 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3794 | if (pke_len == pke_parsed) { |
| 3795 | break; |
| 3796 | } |
| 3797 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3798 | if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3799 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3800 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3801 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3802 | return -parsed; |
| 3803 | } |
| 3804 | pke_parsed += i; |
| 3805 | } |
| 3806 | |
| 3807 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3808 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3809 | 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] | 3810 | 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] | 3811 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3812 | return -parsed; |
| 3813 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3814 | } while (has_predicate); |
| 3815 | |
| 3816 | return parsed; |
| 3817 | } |
| 3818 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3819 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3820 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3821 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3822 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3823 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3824 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3825 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3826 | * @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] | 3827 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3828 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3829 | 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] | 3830 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3831 | const struct lys_node *node, *op_node = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3832 | struct lys_node_augment *last_aug; |
| 3833 | const struct lys_module *cur_mod; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3834 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3835 | const char *id, *prefix, *name; |
| 3836 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3837 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3838 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3839 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3840 | parent_times = 0; |
| 3841 | id = path; |
| 3842 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3843 | /* find operation schema we are in */ |
| 3844 | for (op_node = lys_parent(parent); |
| 3845 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3846 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3847 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3848 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3849 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3850 | 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] | 3851 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3852 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3853 | } |
| 3854 | id += i; |
| 3855 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3856 | /* get the current module */ |
| 3857 | cur_mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3858 | if (!cur_mod) { |
| 3859 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3860 | return EXIT_FAILURE; |
| 3861 | } |
| 3862 | last_aug = NULL; |
| 3863 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3864 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3865 | if (parent_times == -1) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3866 | /* use module data */ |
| 3867 | node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3868 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3869 | } else if (parent_times > 0) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3870 | /* we are looking for the right parent */ |
| 3871 | for (i = 0, node = parent; i < parent_times; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3872 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3873 | * all schema nodes that cannot be instantiated in data tree */ |
| 3874 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3875 | 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] | 3876 | node = lys_parent(node)); |
| 3877 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3878 | if (!node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3879 | if (i == parent_times - 1) { |
| 3880 | /* top-level */ |
| 3881 | break; |
| 3882 | } |
| 3883 | |
| 3884 | /* higher than top-level */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3885 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3886 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3887 | } |
| 3888 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3889 | } else { |
| 3890 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3891 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3892 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3895 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 3896 | * - useless to search for that in augments) */ |
| 3897 | if (!cur_mod->implemented && node) { |
| 3898 | get_next_augment: |
| 3899 | last_aug = lys_getnext_target_aug(last_aug, cur_mod, node); |
| 3900 | } |
| 3901 | |
| 3902 | rc = lys_getnext_data(cur_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST |
| 3903 | | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3904 | if (rc) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3905 | if (last_aug) { |
| 3906 | goto get_next_augment; |
| 3907 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3908 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3909 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3910 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3911 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3912 | if (first_iter) { |
| 3913 | /* 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] | 3914 | if (op_node && parent_times && |
| 3915 | 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] | 3916 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3917 | } |
| 3918 | first_iter = 0; |
| 3919 | } |
| 3920 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3921 | if (has_predicate) { |
| 3922 | /* we have predicate, so the current result must be list */ |
| 3923 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3924 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3925 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3926 | } |
| 3927 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3928 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3929 | if (!i) { |
| 3930 | return EXIT_FAILURE; |
| 3931 | } else if (i < 0) { |
| 3932 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3933 | } |
| 3934 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3935 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3936 | } |
| 3937 | } while (id[0]); |
| 3938 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3939 | /* 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] | 3940 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3941 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3942 | 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] | 3943 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3946 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3947 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3948 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3949 | return -1; |
| 3950 | } |
| 3951 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3952 | if (ret) { |
| 3953 | *ret = node; |
| 3954 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3955 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3956 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3957 | } |
| 3958 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3959 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3960 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3961 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3962 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3963 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3964 | * @param[in,out] node_match Nodes matching the restriction without |
| 3965 | * the predicate. Nodes not satisfying |
| 3966 | * the predicate are removed. |
| 3967 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3968 | * @return Number of characters successfully parsed, |
| 3969 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3970 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3971 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3972 | resolve_instid_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3973 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3974 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3975 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3976 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3977 | 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] | 3978 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3979 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3980 | assert(pred && node_match->count); |
| 3981 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3982 | idx = -1; |
| 3983 | parsed = 0; |
| 3984 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3985 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3986 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3987 | 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] | 3988 | return -parsed+i; |
| 3989 | } |
| 3990 | parsed += i; |
| 3991 | pred += i; |
| 3992 | |
| 3993 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3994 | /* pos */ |
| 3995 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3996 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3997 | } else if (name[0] != '.') { |
| 3998 | /* list keys */ |
| 3999 | if (pred_iter < 0) { |
| 4000 | pred_iter = 1; |
| 4001 | } else { |
| 4002 | ++pred_iter; |
| 4003 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4006 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4007 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4008 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4009 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4010 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4011 | goto remove_instid; |
| 4012 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4013 | |
| 4014 | target = node_match->node[j]; |
| 4015 | /* check the value */ |
| 4016 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4017 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4018 | goto remove_instid; |
| 4019 | } |
| 4020 | |
| 4021 | } else if (!value) { |
| 4022 | /* keyless list position */ |
| 4023 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4024 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4025 | goto remove_instid; |
| 4026 | } |
| 4027 | |
| 4028 | if (idx != cur_idx) { |
| 4029 | goto remove_instid; |
| 4030 | } |
| 4031 | |
| 4032 | } else { |
| 4033 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4034 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4035 | goto remove_instid; |
| 4036 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4037 | |
| 4038 | /* key module must match the list module */ |
| 4039 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4040 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4041 | goto remove_instid; |
| 4042 | } |
| 4043 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4044 | 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] | 4045 | if (!target) { |
| 4046 | goto remove_instid; |
| 4047 | } |
| 4048 | if ((struct lys_node_leaf *)target->schema != |
| 4049 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4050 | goto remove_instid; |
| 4051 | } |
| 4052 | |
| 4053 | /* check the value */ |
| 4054 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4055 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4056 | goto remove_instid; |
| 4057 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4058 | } |
| 4059 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4060 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4061 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4062 | continue; |
| 4063 | |
| 4064 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4065 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4066 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4067 | } |
| 4068 | } while (has_predicate); |
| 4069 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4070 | /* check that all list keys were specified */ |
| 4071 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4072 | j = 0; |
| 4073 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4074 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4075 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4076 | /* not enough predicates, just remove the list instance */ |
| 4077 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4078 | } else { |
| 4079 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | if (!node_match->count) { |
| 4084 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4085 | } |
| 4086 | } |
| 4087 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4088 | return parsed; |
| 4089 | } |
| 4090 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4091 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4092 | 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] | 4093 | { |
| 4094 | struct lys_node *parent, *elem; |
| 4095 | struct lyxp_set set; |
| 4096 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4097 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4098 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4099 | if (check_place) { |
| 4100 | parent = node; |
| 4101 | while (parent) { |
| 4102 | if (parent->nodetype == LYS_GROUPING) { |
| 4103 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4104 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4105 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4106 | if (parent->nodetype == LYS_AUGMENT) { |
| 4107 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4108 | /* unresolved augment, skip for now (will be checked later) */ |
| 4109 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4110 | } else { |
| 4111 | parent = ((struct lys_node_augment *)parent)->target; |
| 4112 | continue; |
| 4113 | } |
| 4114 | } |
| 4115 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4116 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4119 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4120 | if (ret == -1) { |
| 4121 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4125 | |
| 4126 | for (i = 0; i < set.used; ++i) { |
| 4127 | /* skip roots'n'stuff */ |
| 4128 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4129 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4130 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4131 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4132 | return -1; |
| 4133 | } |
| 4134 | |
| 4135 | if (parent) { |
| 4136 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4137 | if (!elem) { |
| 4138 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4139 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4140 | break; |
| 4141 | } |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4147 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4150 | static int |
| 4151 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4152 | { |
| 4153 | int i; |
| 4154 | |
| 4155 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4156 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4157 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4158 | 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] | 4159 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4160 | return -1; |
| 4161 | } |
| 4162 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4163 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4164 | } else if (type->base == LY_TYPE_UNION) { |
| 4165 | for (i = 0; i < type->info.uni.count; i++) { |
| 4166 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4167 | return -1; |
| 4168 | } |
| 4169 | } |
| 4170 | } |
| 4171 | return 0; |
| 4172 | } |
| 4173 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4174 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4175 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4176 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4177 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4178 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4179 | * @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] | 4180 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4181 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4182 | * |
| 4183 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4184 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4185 | int |
| 4186 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4187 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4188 | struct lys_node_leaf *leaf; |
| 4189 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4190 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4191 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4192 | if (clear) { |
| 4193 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4194 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4195 | } else { |
| 4196 | if (node->flags & LYS_CONFIG_SET) { |
| 4197 | /* skip nodes with an explicit config value */ |
| 4198 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4199 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4200 | 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] | 4201 | return -1; |
| 4202 | } |
| 4203 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4204 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4205 | |
| 4206 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4207 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4208 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4209 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4210 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4211 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4212 | return -1; |
| 4213 | } |
| 4214 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4215 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4216 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4217 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4218 | return -1; |
| 4219 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4220 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4221 | leaf = (struct lys_node_leaf *)node; |
| 4222 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4223 | return -1; |
| 4224 | } |
| 4225 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4226 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4227 | |
| 4228 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4229 | } |
| 4230 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4231 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4232 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4233 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4234 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4235 | * @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] | 4236 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4237 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4238 | * @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] | 4239 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4240 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4241 | 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] | 4242 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4243 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4244 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4245 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4246 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4247 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4248 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4249 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4250 | /* set it as not applied for now */ |
| 4251 | aug->flags |= LYS_NOTAPPLIED; |
| 4252 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4253 | /* it can already be resolved in case we returned EXIT_FAILURE from if block below */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4254 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4255 | /* resolve target node */ |
| 4256 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), |
| 4257 | (const struct lys_node **)&aug->target); |
| 4258 | if (rc == -1) { |
| 4259 | return -1; |
| 4260 | } else if (rc > 0) { |
| 4261 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4262 | return -1; |
| 4263 | } |
| 4264 | if (!aug->target) { |
| 4265 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4266 | return EXIT_FAILURE; |
| 4267 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4268 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4269 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4270 | /* check for mandatory nodes - if the target node is in another module |
| 4271 | * the added nodes cannot be mandatory |
| 4272 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4273 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4274 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4275 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4276 | } |
| 4277 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4278 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4279 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4280 | LY_TREE_FOR(aug->child, sub) { |
| 4281 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4282 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4283 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4284 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4285 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4286 | return -1; |
| 4287 | } |
| 4288 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4289 | } 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] | 4290 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4291 | 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] | 4292 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4293 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4294 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4295 | return -1; |
| 4296 | } |
| 4297 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4298 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4299 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4300 | 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] | 4301 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4302 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4303 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4304 | return -1; |
| 4305 | } |
| 4306 | } |
| 4307 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4308 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4309 | 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] | 4310 | return -1; |
| 4311 | } |
| 4312 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4313 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4314 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4315 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4316 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4317 | } |
| 4318 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4319 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4320 | if (!aug->child) { |
| 4321 | /* empty augment, nothing to connect, but it is techincally applied */ |
| 4322 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 4323 | aug->flags &= ~LYS_NOTAPPLIED; |
| 4324 | } else if (mod->implemented && apply_aug(aug, unres)) { |
| 4325 | /* we tried to connect it, we failed */ |
| 4326 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4327 | } |
| 4328 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4329 | return EXIT_SUCCESS; |
| 4330 | } |
| 4331 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4332 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4333 | 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] | 4334 | { |
| 4335 | enum LY_VLOG_ELEM vlog_type; |
| 4336 | void *vlog_node; |
| 4337 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4338 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4339 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4340 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4341 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4342 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4343 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4344 | |
| 4345 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4346 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4347 | vlog_node = info->parent; |
| 4348 | vlog_type = LY_VLOG_LYS; |
| 4349 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4350 | case LYEXT_PAR_MODULE: |
| 4351 | case LYEXT_PAR_IMPORT: |
| 4352 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4353 | vlog_node = NULL; |
| 4354 | vlog_type = LY_VLOG_LYS; |
| 4355 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4356 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4357 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4358 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4359 | break; |
| 4360 | } |
| 4361 | |
| 4362 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4363 | /* YIN */ |
| 4364 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4365 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4366 | 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] | 4367 | if (!mod) { |
| 4368 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4369 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /* find the extension definition */ |
| 4373 | e = NULL; |
| 4374 | for (i = 0; i < mod->extensions_size; i++) { |
| 4375 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4376 | e = &mod->extensions[i]; |
| 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | /* try submodules */ |
| 4381 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4382 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4383 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4384 | e = &mod->inc[j].submodule->extensions[i]; |
| 4385 | break; |
| 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | if (!e) { |
| 4390 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4391 | return EXIT_FAILURE; |
| 4392 | } |
| 4393 | |
| 4394 | /* 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] | 4395 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4396 | if (e->plugin && e->plugin->check_position) { |
| 4397 | /* common part - we have plugin with position checking function, use it first */ |
| 4398 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4399 | /* extension is not allowed here */ |
| 4400 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4401 | return -1; |
| 4402 | } |
| 4403 | } |
| 4404 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4405 | /* extension type-specific part - allocation */ |
| 4406 | if (e->plugin) { |
| 4407 | etype = e->plugin->type; |
| 4408 | } else { |
| 4409 | /* default type */ |
| 4410 | etype = LYEXT_FLAG; |
| 4411 | } |
| 4412 | switch (etype) { |
| 4413 | case LYEXT_FLAG: |
| 4414 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4415 | break; |
| 4416 | case LYEXT_COMPLEX: |
| 4417 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4418 | break; |
| 4419 | case LYEXT_ERR: |
| 4420 | /* we never should be here */ |
| 4421 | LOGINT; |
| 4422 | return -1; |
| 4423 | } |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4424 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4425 | |
| 4426 | /* common part for all extension types */ |
| 4427 | (*ext)->def = e; |
| 4428 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4429 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4430 | (*ext)->insubstmt = info->substmt; |
| 4431 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4432 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4433 | |
| 4434 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4435 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4436 | if (!(*ext)->arg_value) { |
| 4437 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4438 | return -1; |
| 4439 | } |
| 4440 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4441 | } |
| 4442 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4443 | (*ext)->nodetype = LYS_EXT; |
| 4444 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4445 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4446 | /* extension type-specific part - parsing content */ |
| 4447 | switch (etype) { |
| 4448 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4449 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4450 | if (!yin->ns) { |
| 4451 | /* garbage */ |
| 4452 | lyxml_free(mod->ctx, yin); |
| 4453 | continue; |
| 4454 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4455 | /* standard YANG statements are not expected here */ |
| 4456 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4457 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4458 | } else if (yin->ns == info->data.yin->ns && |
| 4459 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4460 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4461 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4462 | 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] | 4463 | return -1; |
| 4464 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4465 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4466 | yin->content = NULL; |
| 4467 | lyxml_free(mod->ctx, yin); |
| 4468 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4469 | /* extension instance */ |
| 4470 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4471 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4472 | return -1; |
| 4473 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4474 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4475 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4476 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4477 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4478 | break; |
| 4479 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4480 | ((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] | 4481 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4482 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4483 | return -1; |
| 4484 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4485 | break; |
| 4486 | default: |
| 4487 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4488 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4489 | |
| 4490 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4491 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4492 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4493 | /* YANG */ |
| 4494 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4495 | ext_prefix = (char *)(*ext)->def; |
| 4496 | tmp = strchr(ext_prefix, ':'); |
| 4497 | if (!tmp) { |
| 4498 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4499 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4500 | } |
| 4501 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4502 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4503 | /* get the module where the extension is supposed to be defined */ |
| 4504 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4505 | if (!mod) { |
| 4506 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4507 | return EXIT_FAILURE; |
| 4508 | } |
| 4509 | |
| 4510 | /* find the extension definition */ |
| 4511 | e = NULL; |
| 4512 | for (i = 0; i < mod->extensions_size; i++) { |
| 4513 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4514 | e = &mod->extensions[i]; |
| 4515 | break; |
| 4516 | } |
| 4517 | } |
| 4518 | /* try submodules */ |
| 4519 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4520 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4521 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4522 | e = &mod->inc[j].submodule->extensions[i]; |
| 4523 | break; |
| 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | if (!e) { |
| 4528 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4529 | return EXIT_FAILURE; |
| 4530 | } |
| 4531 | |
| 4532 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4533 | |
| 4534 | if (e->plugin && e->plugin->check_position) { |
| 4535 | /* common part - we have plugin with position checking function, use it first */ |
| 4536 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4537 | /* extension is not allowed here */ |
| 4538 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4539 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4543 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4544 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4545 | (*ext)->def = e; |
| 4546 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4547 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4548 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4549 | if (e->argument && !(*ext)->arg_value) { |
| 4550 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4551 | goto error; |
| 4552 | } |
| 4553 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4554 | (*ext)->module = info->mod; |
| 4555 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4556 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4557 | /* extension type-specific part */ |
| 4558 | if (e->plugin) { |
| 4559 | etype = e->plugin->type; |
| 4560 | } else { |
| 4561 | /* default type */ |
| 4562 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4563 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4564 | switch (etype) { |
| 4565 | case LYEXT_FLAG: |
| 4566 | /* nothing change */ |
| 4567 | break; |
| 4568 | case LYEXT_COMPLEX: |
| 4569 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4570 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4571 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4572 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4573 | ((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] | 4574 | if (info->data.yang) { |
| 4575 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4576 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4577 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4578 | goto error; |
| 4579 | } |
| 4580 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4581 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4582 | goto error; |
| 4583 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4584 | } |
| 4585 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4586 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4587 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4588 | break; |
| 4589 | case LYEXT_ERR: |
| 4590 | /* we never should be here */ |
| 4591 | LOGINT; |
| 4592 | goto error; |
| 4593 | } |
| 4594 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4595 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4596 | goto error; |
| 4597 | } |
| 4598 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4602 | error: |
| 4603 | free(ext_prefix); |
| 4604 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4605 | } |
| 4606 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4607 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4608 | * @brief Resolve (find) choice default case. Does not log. |
| 4609 | * |
| 4610 | * @param[in] choic Choice to use. |
| 4611 | * @param[in] dflt Name of the default case. |
| 4612 | * |
| 4613 | * @return Pointer to the default node or NULL. |
| 4614 | */ |
| 4615 | static struct lys_node * |
| 4616 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4617 | { |
| 4618 | struct lys_node *child, *ret; |
| 4619 | |
| 4620 | LY_TREE_FOR(choic->child, child) { |
| 4621 | if (child->nodetype == LYS_USES) { |
| 4622 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4623 | if (ret) { |
| 4624 | return ret; |
| 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | 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] | 4629 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4630 | return child; |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | return NULL; |
| 4635 | } |
| 4636 | |
| 4637 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4638 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4639 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4640 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4641 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4642 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4643 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4644 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4645 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4646 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4647 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4648 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4649 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4650 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4651 | struct lys_node_leaflist *llist; |
| 4652 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4653 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4654 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4655 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4656 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4657 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4658 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4659 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4660 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4661 | |
| 4662 | /* HACK just check that the grouping is resolved |
| 4663 | * - the higher byte in flags is always empty in grouping (no flags there apply to the groupings) |
| 4664 | * so we use it to count unresolved uses inside the grouping */ |
| 4665 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 4666 | assert(!((uint8_t*)&uses->grp->flags)[1]); |
| 4667 | #else |
| 4668 | assert(!((uint8_t*)&uses->grp->flags)[0]); |
| 4669 | #endif |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4670 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4671 | if (!uses->grp->child) { |
| 4672 | /* grouping without children, warning was already displayed */ |
| 4673 | return EXIT_SUCCESS; |
| 4674 | } |
| 4675 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4676 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4677 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4678 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4679 | /* do not instantiate groupings from groupings */ |
| 4680 | continue; |
| 4681 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4682 | 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] | 4683 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4684 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4685 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4686 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4688 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4689 | 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] | 4690 | 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] | 4691 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4692 | } |
| 4693 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4694 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4695 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4696 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4697 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4698 | if (uses->refine_size) { |
| 4699 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4700 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4701 | } |
| 4702 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4703 | /* apply refines */ |
| 4704 | for (i = 0; i < uses->refine_size; i++) { |
| 4705 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4706 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4707 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4708 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4709 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4710 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4711 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4712 | } |
| 4713 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4714 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4715 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4716 | 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] | 4717 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4718 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4719 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4720 | |
| 4721 | /* description on any nodetype */ |
| 4722 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4723 | lydict_remove(ctx, node->dsc); |
| 4724 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | /* reference on any nodetype */ |
| 4728 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4729 | lydict_remove(ctx, node->ref); |
| 4730 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4731 | } |
| 4732 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4733 | /* config on any nodetype, |
| 4734 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4735 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4736 | node->flags &= ~LYS_CONFIG_MASK; |
| 4737 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4741 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4742 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4743 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4744 | leaf = (struct lys_node_leaf *)node; |
| 4745 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4746 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4747 | lydict_remove(ctx, leaf->dflt); |
| 4748 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4749 | |
| 4750 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4751 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4752 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4753 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4754 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4755 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4756 | /* leaf-list */ |
| 4757 | llist = (struct lys_node_leaflist *)node; |
| 4758 | |
| 4759 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4760 | for (j = 0; j < llist->dflt_size; j++) { |
| 4761 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4762 | } |
| 4763 | free(llist->dflt); |
| 4764 | |
| 4765 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4766 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
| 4767 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4768 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4769 | for (j = 0; j < llist->dflt_size; j++) { |
| 4770 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4774 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4775 | 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] | 4776 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4777 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4778 | } |
| 4779 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4780 | } |
| 4781 | } |
| 4782 | |
| 4783 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4784 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4785 | /* remove current value */ |
| 4786 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4787 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4788 | /* set new value */ |
| 4789 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4790 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4791 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4792 | /* check if node has default value */ |
| 4793 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4794 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4795 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4796 | goto fail; |
| 4797 | } |
| 4798 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4799 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4800 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4801 | goto fail; |
| 4802 | } |
| 4803 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4807 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4808 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4809 | ((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] | 4810 | } |
| 4811 | |
| 4812 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4813 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4814 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4815 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4816 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4817 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4818 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4819 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4820 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4821 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4822 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4823 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4824 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4825 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4830 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4831 | switch (node->nodetype) { |
| 4832 | case LYS_LEAF: |
| 4833 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4834 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4835 | break; |
| 4836 | case LYS_LEAFLIST: |
| 4837 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4838 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4839 | break; |
| 4840 | case LYS_LIST: |
| 4841 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4842 | old_must = &((struct lys_node_list *)node)->must; |
| 4843 | break; |
| 4844 | case LYS_CONTAINER: |
| 4845 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4846 | old_must = &((struct lys_node_container *)node)->must; |
| 4847 | break; |
| 4848 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4849 | case LYS_ANYDATA: |
| 4850 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4851 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4852 | break; |
| 4853 | default: |
| 4854 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4855 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | size = *old_size + rfn->must_size; |
| 4859 | must = realloc(*old_must, size * sizeof *rfn->must); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4860 | LY_CHECK_ERR_GOTO(!must, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4861 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4862 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4863 | 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] | 4864 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4865 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4866 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4867 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4868 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4869 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4870 | } |
| 4871 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4872 | *old_must = must; |
| 4873 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4874 | |
| 4875 | /* check XPath dependencies again */ |
| 4876 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4877 | goto fail; |
| 4878 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4879 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4880 | |
| 4881 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4882 | if (rfn->iffeature_size) { |
| 4883 | old_size = &node->iffeature_size; |
| 4884 | old_iff = &node->iffeature; |
| 4885 | |
| 4886 | size = *old_size + rfn->iffeature_size; |
| 4887 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4888 | LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4889 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4890 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4891 | if (usize1) { |
| 4892 | /* there is something to duplicate */ |
| 4893 | /* duplicate compiled expression */ |
| 4894 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4895 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4896 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4897 | 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] | 4898 | |
| 4899 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4900 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4901 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4902 | 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] | 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | *old_iff = iff; |
| 4907 | *old_size = size; |
| 4908 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /* apply augments */ |
| 4912 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4913 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4914 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4915 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4916 | } |
| 4917 | } |
| 4918 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4919 | /* check refines */ |
| 4920 | for (i = 0; i < uses->refine_size; i++) { |
| 4921 | node = refine_nodes[i]; |
| 4922 | rfn = &uses->refine[i]; |
| 4923 | |
| 4924 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4925 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4926 | 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] | 4927 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4928 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4929 | (rfn->flags & LYS_CONFIG_W)) { |
| 4930 | /* setting config true under config false is prohibited */ |
| 4931 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4932 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4933 | "changing config from 'false' to 'true' is prohibited while " |
| 4934 | "the target's parent is still config 'false'."); |
| 4935 | goto fail; |
| 4936 | } |
| 4937 | |
| 4938 | /* inherit config change to the target children */ |
| 4939 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4940 | if (rfn->flags & LYS_CONFIG_W) { |
| 4941 | if (iter->flags & LYS_CONFIG_SET) { |
| 4942 | /* config is set explicitely, go to next sibling */ |
| 4943 | next = NULL; |
| 4944 | goto nextsibling; |
| 4945 | } |
| 4946 | } else { /* LYS_CONFIG_R */ |
| 4947 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4948 | /* error - we would have config data under status data */ |
| 4949 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4950 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4951 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4952 | "has still a children with explicit config 'true'."); |
| 4953 | goto fail; |
| 4954 | } |
| 4955 | } |
| 4956 | /* change config */ |
| 4957 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4958 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4959 | |
| 4960 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4961 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4962 | next = NULL; |
| 4963 | } else { |
| 4964 | next = iter->child; |
| 4965 | } |
| 4966 | nextsibling: |
| 4967 | if (!next) { |
| 4968 | /* try siblings */ |
| 4969 | next = iter->next; |
| 4970 | } |
| 4971 | while (!next) { |
| 4972 | /* parent is already processed, go to its sibling */ |
| 4973 | iter = lys_parent(iter); |
| 4974 | |
| 4975 | /* no siblings, go back through parents */ |
| 4976 | if (iter == node) { |
| 4977 | /* we are done, no next element to process */ |
| 4978 | break; |
| 4979 | } |
| 4980 | next = iter->next; |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4986 | if (rfn->dflt_size) { |
| 4987 | if (node->nodetype == LYS_CHOICE) { |
| 4988 | /* choice */ |
| 4989 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4990 | rfn->dflt[0]); |
| 4991 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4992 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4993 | goto fail; |
| 4994 | } |
| 4995 | if (lyp_check_mandatory_choice(node)) { |
| 4996 | goto fail; |
| 4997 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5002 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5003 | 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] | 5004 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5005 | 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] | 5006 | goto fail; |
| 5007 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5008 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5009 | 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] | 5010 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5011 | 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] | 5012 | goto fail; |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5017 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5018 | if (node->nodetype == LYS_LEAFLIST) { |
| 5019 | llist = (struct lys_node_leaflist *)node; |
| 5020 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5021 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5022 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5023 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5024 | goto fail; |
| 5025 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5026 | } else if (node->nodetype == LYS_LEAF) { |
| 5027 | leaf = (struct lys_node_leaf *)node; |
| 5028 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5029 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5030 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5031 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5032 | goto fail; |
| 5033 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5034 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5035 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5036 | /* 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] | 5037 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5038 | for (parent = node->parent; |
| 5039 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5040 | parent = parent->parent) { |
| 5041 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5042 | /* stop also on presence containers */ |
| 5043 | break; |
| 5044 | } |
| 5045 | } |
| 5046 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5047 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5048 | if (lyp_check_mandatory_choice(parent)) { |
| 5049 | goto fail; |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | } |
| 5054 | free(refine_nodes); |
| 5055 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5056 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5057 | |
| 5058 | fail: |
| 5059 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5060 | lys_node_free(iter, NULL, 0); |
| 5061 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5062 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5063 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5064 | } |
| 5065 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5066 | void |
| 5067 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5068 | { |
| 5069 | int i; |
| 5070 | |
| 5071 | assert(der && base); |
| 5072 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5073 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5074 | /* create a set for backlinks if it does not exist */ |
| 5075 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5076 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5077 | /* store backlink */ |
| 5078 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5079 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5080 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5081 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5082 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5083 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5086 | /** |
| 5087 | * @brief Resolve base identity recursively. Does not log. |
| 5088 | * |
| 5089 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5090 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5091 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5092 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5093 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5094 | * @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] | 5095 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5096 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5097 | 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] | 5098 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5099 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5100 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5101 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5102 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5103 | assert(ret); |
| 5104 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5105 | /* search module */ |
| 5106 | for (i = 0; i < module->ident_size; i++) { |
| 5107 | if (!strcmp(basename, module->ident[i].name)) { |
| 5108 | |
| 5109 | if (!ident) { |
| 5110 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5111 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5112 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5113 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5114 | } |
| 5115 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5116 | base = &module->ident[i]; |
| 5117 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5122 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5123 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5124 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5125 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5126 | if (!ident) { |
| 5127 | *ret = &module->inc[j].submodule->ident[i]; |
| 5128 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5129 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5130 | |
| 5131 | base = &module->inc[j].submodule->ident[i]; |
| 5132 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5137 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5138 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5139 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5140 | /* is it already completely resolved? */ |
| 5141 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5142 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5143 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5144 | |
| 5145 | /* simple check for circular reference, |
| 5146 | * the complete check is done as a side effect of using only completely |
| 5147 | * resolved identities (previous check of unres content) */ |
| 5148 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5149 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5150 | 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] | 5151 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5152 | } |
| 5153 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5154 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5155 | } |
| 5156 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5157 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5158 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5159 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5160 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5161 | } |
| 5162 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5163 | /* base not found (maybe a forward reference) */ |
| 5164 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5167 | /** |
| 5168 | * @brief Resolve base identity. Logs directly. |
| 5169 | * |
| 5170 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5171 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5172 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5173 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5174 | * @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] | 5175 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5176 | * @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] | 5177 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5178 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5179 | 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] | 5180 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5181 | { |
| 5182 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5183 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5184 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5185 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5186 | struct lys_module *mod; |
| 5187 | |
| 5188 | assert((ident && !type) || (!ident && type)); |
| 5189 | |
| 5190 | if (!type) { |
| 5191 | /* have ident to resolve */ |
| 5192 | ret = ⌖ |
| 5193 | flags = ident->flags; |
| 5194 | mod = ident->module; |
| 5195 | } else { |
| 5196 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5197 | ++type->info.ident.count; |
| 5198 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5199 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5200 | |
| 5201 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5202 | flags = type->parent->flags; |
| 5203 | mod = type->parent->module; |
| 5204 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5205 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5206 | |
| 5207 | /* search for the base identity */ |
| 5208 | name = strchr(basename, ':'); |
| 5209 | if (name) { |
| 5210 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5211 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5212 | name++; |
| 5213 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5214 | 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] | 5215 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5216 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5217 | } |
| 5218 | } else { |
| 5219 | name = basename; |
| 5220 | } |
| 5221 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5222 | /* get module where to search */ |
| 5223 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5224 | if (!module) { |
| 5225 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5226 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5227 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5230 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5231 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5232 | if (!rc) { |
| 5233 | assert(*ret); |
| 5234 | |
| 5235 | /* check status */ |
| 5236 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5237 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5238 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5239 | } else if (ident) { |
| 5240 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5241 | if (lys_main_module(mod)->implemented) { |
| 5242 | /* in case of the implemented identity, maintain backlinks to it |
| 5243 | * from the base identities to make it available when resolving |
| 5244 | * data with the identity values (not implemented identity is not |
| 5245 | * allowed as an identityref value). */ |
| 5246 | resolve_identity_backlink_update(ident, *ret); |
| 5247 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5248 | } |
| 5249 | } else if (rc == EXIT_FAILURE) { |
| 5250 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5251 | if (type) { |
| 5252 | --type->info.ident.count; |
| 5253 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5254 | } |
| 5255 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5256 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5257 | } |
| 5258 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5259 | /* |
| 5260 | * 1 - true (der is derived from base) |
| 5261 | * 0 - false (der is not derived from base) |
| 5262 | */ |
| 5263 | static int |
| 5264 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5265 | { |
| 5266 | int i; |
| 5267 | |
| 5268 | if (der == base) { |
| 5269 | return 1; |
| 5270 | } else { |
| 5271 | for(i = 0; i < der->base_size; i++) { |
| 5272 | if (search_base_identity(der->base[i], base) == 1) { |
| 5273 | return 1; |
| 5274 | } |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5281 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5282 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5283 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5284 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5285 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5286 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5287 | * @param[in] dflt flag if we are resolving default value in the schema |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5288 | * |
| 5289 | * @return Pointer to the identity resolvent, NULL on error. |
| 5290 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5291 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5292 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5293 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5294 | const char *mod_name, *name; |
| 5295 | int mod_name_len, rc, i, j; |
| 5296 | int make_implemented = 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5297 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5298 | struct lys_ident *der, *cur; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5299 | struct lys_module *imod = NULL, *m; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5300 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5301 | assert(type && ident_name && node && mod); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5302 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5303 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5304 | return NULL; |
| 5305 | } |
| 5306 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5307 | 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] | 5308 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5309 | 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] | 5310 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5311 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5312 | 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] | 5313 | return NULL; |
| 5314 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5315 | |
| 5316 | m = lys_main_module(mod); /* shortcut */ |
| 5317 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5318 | /* identity is defined in the same module as node */ |
| 5319 | imod = m; |
| 5320 | } else if (dflt) { |
| 5321 | /* solving identityref in default definition in schema - |
| 5322 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5323 | for (i = 0; i < mod->imp_size; i++) { |
| 5324 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5325 | imod = mod->imp[i].module; |
| 5326 | break; |
| 5327 | } |
| 5328 | } |
| 5329 | } else { |
| 5330 | /* solving identityref in data - get the (implemented) module from the context */ |
| 5331 | u = 0; |
| 5332 | while ((imod = (struct lys_module*)ly_ctx_get_module_iter(mod->ctx, &u))) { |
| 5333 | if (imod->implemented && !strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5334 | break; |
| 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | if (!imod) { |
| 5339 | goto fail; |
| 5340 | } |
| 5341 | |
| 5342 | if (dflt && (m != imod || lys_main_module(type->parent->module) != mod)) { |
| 5343 | /* we are solving default statement in schema AND the type is not referencing the same schema, |
| 5344 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5345 | * contains the identity */ |
| 5346 | if (!imod->implemented) { |
| 5347 | cur = NULL; |
| 5348 | /* get the identity in the module */ |
| 5349 | for (i = 0; i < imod->ident_size; i++) { |
| 5350 | if (!strcmp(name, imod->ident[i].name)) { |
| 5351 | cur = &imod->ident[i]; |
| 5352 | break; |
| 5353 | } |
| 5354 | } |
| 5355 | if (!cur) { |
| 5356 | /* go through includes */ |
| 5357 | for (j = 0; j < imod->inc_size; j++) { |
| 5358 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5359 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5360 | cur = &imod->inc[j].submodule->ident[i]; |
| 5361 | break; |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | if (!cur) { |
| 5366 | goto fail; |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | /* check that identity is derived from one of the type's base */ |
| 5371 | while (type->der) { |
| 5372 | for (i = 0; i < type->info.ident.count; i++) { |
| 5373 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5374 | /* cur's base matches the type's base */ |
| 5375 | make_implemented = 1; |
| 5376 | goto match; |
| 5377 | } |
| 5378 | } |
| 5379 | type = &type->der->type; |
| 5380 | } |
| 5381 | /* matching base not found */ |
| 5382 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5383 | goto fail; |
| 5384 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5385 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5386 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5387 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5388 | while (type->der) { |
| 5389 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5390 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5391 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5392 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5393 | /* there are some derived identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5394 | for (u = 0; u < cur->der->number; u++) { |
| 5395 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5396 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5397 | /* we have match */ |
| 5398 | cur = der; |
| 5399 | goto match; |
| 5400 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5401 | } |
| 5402 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5403 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5404 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5405 | } |
| 5406 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5407 | fail: |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5408 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5409 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5410 | |
| 5411 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5412 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5413 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5414 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5415 | 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] | 5416 | return NULL; |
| 5417 | } |
| 5418 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5419 | if (make_implemented) { |
| 5420 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5421 | imod->name, cur->name, mod->name); |
| 5422 | if (lys_set_implemented(imod)) { |
| 5423 | LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
| 5424 | imod->name, cur->name); |
| 5425 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5426 | goto fail; |
| 5427 | } |
| 5428 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5429 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5430 | } |
| 5431 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5432 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5433 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5434 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5435 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5436 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5437 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5438 | * @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] | 5439 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5440 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5441 | 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] | 5442 | { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5443 | int rc, endian_idx; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5444 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5445 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5446 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 5447 | endian_idx = 1; |
| 5448 | #else |
| 5449 | endian_idx = 0; |
| 5450 | #endif |
| 5451 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5452 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
| 5453 | * in some uses. When we see such a uses, the grouping's higher byte of the flags member (not used in |
| 5454 | * grouping) is used to store number of so far unresolved uses. The grouping cannot be used unless this |
| 5455 | * 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] | 5456 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5457 | 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] | 5458 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5459 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5460 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5461 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5462 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5463 | return -1; |
| 5464 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5465 | 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] | 5466 | return -1; |
| 5467 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5468 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5469 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5470 | * (and smaller flags - it uses only a limited set of flags) |
| 5471 | */ |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5472 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5473 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5474 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5475 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5476 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5477 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5480 | if (((uint8_t*)&uses->grp->flags)[endian_idx]) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5481 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
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; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5484 | } else { |
| 5485 | /* instantiate grouping only when it is completely resolved */ |
| 5486 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5487 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5488 | return EXIT_FAILURE; |
| 5489 | } |
| 5490 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5491 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5492 | if (!rc) { |
| 5493 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5494 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5495 | if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5496 | LOGINT; |
| 5497 | return -1; |
| 5498 | } |
Michal Vasko | 5191755 | 2017-03-08 10:21:34 +0100 | [diff] [blame] | 5499 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[endian_idx]--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5500 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5501 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5502 | |
| 5503 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5504 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5505 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5506 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5507 | return -1; |
| 5508 | } |
| 5509 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5510 | return EXIT_SUCCESS; |
| 5511 | } |
| 5512 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5513 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5514 | } |
| 5515 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5516 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5517 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5518 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5519 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5520 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5521 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5522 | * @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] | 5523 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5524 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5525 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5526 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5527 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5528 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5529 | |
| 5530 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5531 | if (!list->child) { |
| 5532 | /* no child, possible forward reference */ |
| 5533 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5534 | return EXIT_FAILURE; |
| 5535 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5536 | /* get the key name */ |
| 5537 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5538 | len = value - keys_str; |
| 5539 | while (isspace(value[0])) { |
| 5540 | value++; |
| 5541 | } |
| 5542 | } else { |
| 5543 | len = strlen(keys_str); |
| 5544 | } |
| 5545 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5546 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 5547 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5548 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5549 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5550 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5551 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5552 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5553 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5554 | /* check_key logs */ |
| 5555 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5556 | } |
| 5557 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5558 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5559 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5560 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5561 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5562 | return -1; |
| 5563 | } |
| 5564 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5565 | /* prepare for next iteration */ |
| 5566 | while (value && isspace(value[0])) { |
| 5567 | value++; |
| 5568 | } |
| 5569 | keys_str = value; |
| 5570 | } |
| 5571 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5572 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5573 | } |
| 5574 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5575 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5576 | * @brief Resolve (check) all must conditions of \p node. |
| 5577 | * Logs directly. |
| 5578 | * |
| 5579 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5580 | * @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] | 5581 | * |
| 5582 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5583 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5584 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5585 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5586 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5587 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5588 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5589 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5590 | struct lys_restr *must; |
| 5591 | struct lyxp_set set; |
| 5592 | |
| 5593 | assert(node); |
| 5594 | memset(&set, 0, sizeof set); |
| 5595 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5596 | if (inout_parent) { |
| 5597 | for (schema = lys_parent(node->schema); |
| 5598 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5599 | schema = lys_parent(schema)); |
| 5600 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5601 | LOGINT; |
| 5602 | return -1; |
| 5603 | } |
| 5604 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5605 | must = ((struct lys_node_inout *)schema)->must; |
| 5606 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5607 | node_flags = schema->flags; |
| 5608 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5609 | /* context node is the RPC/action */ |
| 5610 | node = node->parent; |
| 5611 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5612 | LOGINT; |
| 5613 | return -1; |
| 5614 | } |
| 5615 | } else { |
| 5616 | switch (node->schema->nodetype) { |
| 5617 | case LYS_CONTAINER: |
| 5618 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5619 | must = ((struct lys_node_container *)node->schema)->must; |
| 5620 | break; |
| 5621 | case LYS_LEAF: |
| 5622 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5623 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5624 | break; |
| 5625 | case LYS_LEAFLIST: |
| 5626 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5627 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5628 | break; |
| 5629 | case LYS_LIST: |
| 5630 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5631 | must = ((struct lys_node_list *)node->schema)->must; |
| 5632 | break; |
| 5633 | case LYS_ANYXML: |
| 5634 | case LYS_ANYDATA: |
| 5635 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5636 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5637 | break; |
| 5638 | case LYS_NOTIF: |
| 5639 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5640 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5641 | break; |
| 5642 | default: |
| 5643 | must_size = 0; |
| 5644 | break; |
| 5645 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5646 | |
| 5647 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5651 | 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] | 5652 | return -1; |
| 5653 | } |
| 5654 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5655 | 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] | 5656 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5657 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5658 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5659 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5660 | } else { |
| 5661 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5662 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5663 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5664 | } |
| 5665 | if (must[i].eapptag) { |
| 5666 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5667 | } |
| 5668 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5669 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5670 | } |
| 5671 | } |
| 5672 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5673 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5674 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5675 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5676 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5677 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5678 | * |
| 5679 | * @param[in] schema Schema node with the when condition. |
| 5680 | * @param[out] ctx_snode When schema context node. |
| 5681 | * @param[out] ctx_snode_type Schema context node type. |
| 5682 | */ |
| 5683 | void |
| 5684 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5685 | { |
| 5686 | const struct lys_node *sparent; |
| 5687 | |
| 5688 | /* find a not schema-only node */ |
| 5689 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5690 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5691 | if (schema->nodetype == LYS_AUGMENT) { |
| 5692 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5693 | } else { |
| 5694 | sparent = schema->parent; |
| 5695 | } |
| 5696 | if (!sparent) { |
| 5697 | /* context node is the document root (fake root in our case) */ |
| 5698 | if (schema->flags & LYS_CONFIG_W) { |
| 5699 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5700 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5701 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5702 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5703 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5704 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5705 | break; |
| 5706 | } |
| 5707 | schema = sparent; |
| 5708 | } |
| 5709 | |
| 5710 | *ctx_snode = (struct lys_node *)schema; |
| 5711 | } |
| 5712 | |
| 5713 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5714 | * @brief Resolve (find) when condition context node. Does not log. |
| 5715 | * |
| 5716 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5717 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5718 | * @param[out] ctx_node Context node. |
| 5719 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5720 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5721 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5722 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5723 | static int |
| 5724 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5725 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5726 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5727 | struct lyd_node *parent; |
| 5728 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5729 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5730 | uint16_t i, data_depth, schema_depth; |
| 5731 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5732 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5733 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5734 | if (node_type == LYXP_NODE_ELEM) { |
| 5735 | /* standard element context node */ |
| 5736 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5737 | for (sparent = schema, schema_depth = 0; |
| 5738 | sparent; |
| 5739 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5740 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5741 | ++schema_depth; |
| 5742 | } |
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 (data_depth < schema_depth) { |
| 5745 | return -1; |
| 5746 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5747 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5748 | /* find the corresponding data node */ |
| 5749 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5750 | node = node->parent; |
| 5751 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5752 | if (node->schema != schema) { |
| 5753 | return -1; |
| 5754 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5755 | } else { |
| 5756 | /* root context node */ |
| 5757 | while (node->parent) { |
| 5758 | node = node->parent; |
| 5759 | } |
| 5760 | while (node->prev->next) { |
| 5761 | node = node->prev; |
| 5762 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5763 | } |
| 5764 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5765 | *ctx_node = node; |
| 5766 | *ctx_node_type = node_type; |
| 5767 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5768 | } |
| 5769 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5770 | /** |
| 5771 | * @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] | 5772 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5773 | * |
| 5774 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5775 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5776 | * it is moved to point to another sibling still in the original tree. |
| 5777 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5778 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5779 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5780 | * Ordering may change, but there will be no semantic change. |
| 5781 | * |
| 5782 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5783 | */ |
| 5784 | static int |
| 5785 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5786 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5787 | { |
| 5788 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5789 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5790 | |
| 5791 | switch (snode->nodetype) { |
| 5792 | case LYS_AUGMENT: |
| 5793 | case LYS_USES: |
| 5794 | case LYS_CHOICE: |
| 5795 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5796 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5797 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5798 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5799 | continue; |
| 5800 | } |
| 5801 | |
| 5802 | 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] | 5803 | return -1; |
| 5804 | } |
| 5805 | } |
| 5806 | break; |
| 5807 | case LYS_CONTAINER: |
| 5808 | case LYS_LIST: |
| 5809 | case LYS_LEAF: |
| 5810 | case LYS_LEAFLIST: |
| 5811 | case LYS_ANYXML: |
| 5812 | case LYS_ANYDATA: |
| 5813 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5814 | if (elem->schema == snode) { |
| 5815 | |
| 5816 | if (elem == *ctx_node) { |
| 5817 | /* We are going to unlink our context node! This normally cannot happen, |
| 5818 | * but we use normal top-level data nodes for faking a document root node, |
| 5819 | * so if this is the context node, we just use the next top-level node. |
| 5820 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5821 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5822 | * lyxp_eval() can handle this special situation. |
| 5823 | */ |
| 5824 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5825 | LOGINT; |
| 5826 | return -1; |
| 5827 | } |
| 5828 | |
| 5829 | if (elem->prev == elem) { |
| 5830 | /* unlinking last top-level element, use an empty data tree */ |
| 5831 | *ctx_node = NULL; |
| 5832 | } else { |
| 5833 | /* in this case just use the previous/last top-level data node */ |
| 5834 | *ctx_node = elem->prev; |
| 5835 | } |
| 5836 | } else if (elem == *node) { |
| 5837 | /* We are going to unlink the currently processed node. This does not matter that |
| 5838 | * much, but we would lose access to the original data tree, so just move our |
| 5839 | * pointer somewhere still inside it. |
| 5840 | */ |
| 5841 | if ((*node)->prev != *node) { |
| 5842 | *node = (*node)->prev; |
| 5843 | } else { |
| 5844 | /* the processed node with sibings were all unlinked, oh well */ |
| 5845 | *node = NULL; |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5850 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5851 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5852 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5853 | LOGINT; |
| 5854 | return -1; |
| 5855 | } |
| 5856 | } else { |
| 5857 | *unlinked_nodes = elem; |
| 5858 | } |
| 5859 | |
| 5860 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5861 | /* there can be only one instance */ |
| 5862 | break; |
| 5863 | } |
| 5864 | } |
| 5865 | } |
| 5866 | break; |
| 5867 | default: |
| 5868 | LOGINT; |
| 5869 | return -1; |
| 5870 | } |
| 5871 | |
| 5872 | return EXIT_SUCCESS; |
| 5873 | } |
| 5874 | |
| 5875 | /** |
| 5876 | * @brief Relink the unlinked nodes back. |
| 5877 | * |
| 5878 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5879 | * we simply need a sibling from the original data tree. |
| 5880 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5881 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5882 | * or the sibling of \p unlinked_nodes. |
| 5883 | * |
| 5884 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5885 | */ |
| 5886 | static int |
| 5887 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5888 | { |
| 5889 | struct lyd_node *elem; |
| 5890 | |
| 5891 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5892 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5893 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5894 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5895 | return -1; |
| 5896 | } |
| 5897 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5898 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5899 | return -1; |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | return EXIT_SUCCESS; |
| 5905 | } |
| 5906 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5907 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5908 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5909 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5910 | int ret = 0; |
| 5911 | uint8_t must_size; |
| 5912 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5913 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5914 | assert(node); |
| 5915 | |
| 5916 | schema = node->schema; |
| 5917 | |
| 5918 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5919 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5920 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5921 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5922 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5923 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5924 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5925 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5926 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5927 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5928 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5929 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5930 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5931 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5932 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5933 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5934 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5935 | break; |
| 5936 | case LYS_NOTIF: |
| 5937 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5938 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5939 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5940 | must_size = 0; |
| 5941 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5942 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5943 | |
| 5944 | if (must_size) { |
| 5945 | ++ret; |
| 5946 | } |
| 5947 | |
| 5948 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5949 | if (!node->prev->next) { |
| 5950 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5951 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5952 | ret += 0x2; |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5960 | 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] | 5961 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5962 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5963 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5964 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5965 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5966 | 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] | 5967 | return 1; |
| 5968 | } |
| 5969 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5970 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5971 | goto check_augment; |
| 5972 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5973 | while (parent) { |
| 5974 | /* stop conditions */ |
| 5975 | if (!mode) { |
| 5976 | /* stop on node that can be instantiated in data tree */ |
| 5977 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5978 | break; |
| 5979 | } |
| 5980 | } else { |
| 5981 | /* stop on the specified node */ |
| 5982 | if (parent == stop) { |
| 5983 | break; |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5988 | return 1; |
| 5989 | } |
| 5990 | check_augment: |
| 5991 | |
| 5992 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5993 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5994 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5995 | } |
| 5996 | parent = lys_parent(parent); |
| 5997 | } |
| 5998 | |
| 5999 | return 0; |
| 6000 | } |
| 6001 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6002 | /** |
| 6003 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6004 | * Logs directly. |
| 6005 | * |
| 6006 | * @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] | 6007 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6008 | * @return |
| 6009 | * -1 - error, ly_errno is set |
| 6010 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6011 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6012 | * 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] | 6013 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6014 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6015 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6016 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6017 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6018 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6019 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6020 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6021 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6022 | |
| 6023 | assert(node); |
| 6024 | memset(&set, 0, sizeof set); |
| 6025 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6026 | 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] | 6027 | /* make the node dummy for the evaluation */ |
| 6028 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6029 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6030 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6031 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6032 | if (rc) { |
| 6033 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6034 | 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] | 6035 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6036 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6037 | } |
| 6038 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6039 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6040 | 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] | 6041 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6042 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6043 | 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] | 6044 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6045 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6046 | } else { |
| 6047 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 6048 | goto cleanup; |
| 6049 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6050 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6051 | |
| 6052 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6053 | 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] | 6054 | } |
| 6055 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6056 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6057 | goto check_augment; |
| 6058 | |
| 6059 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6060 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6061 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6062 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6063 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6064 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6065 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6066 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6067 | } |
| 6068 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6069 | |
| 6070 | unlinked_nodes = NULL; |
| 6071 | /* we do not want our node pointer to change */ |
| 6072 | tmp_node = node; |
| 6073 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6074 | if (rc) { |
| 6075 | goto cleanup; |
| 6076 | } |
| 6077 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6078 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6079 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6080 | |
| 6081 | if (unlinked_nodes && ctx_node) { |
| 6082 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6083 | rc = -1; |
| 6084 | goto cleanup; |
| 6085 | } |
| 6086 | } |
| 6087 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6088 | if (rc) { |
| 6089 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6090 | 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] | 6091 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6092 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6093 | } |
| 6094 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6095 | 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] | 6096 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6097 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6098 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6099 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6100 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6101 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6102 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 6103 | goto cleanup; |
| 6104 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6105 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6106 | |
| 6107 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6108 | 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] | 6109 | } |
| 6110 | |
| 6111 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6112 | 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] | 6113 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6114 | 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] | 6115 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6116 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6117 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6118 | } |
| 6119 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6120 | |
| 6121 | unlinked_nodes = NULL; |
| 6122 | tmp_node = node; |
| 6123 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6124 | if (rc) { |
| 6125 | goto cleanup; |
| 6126 | } |
| 6127 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6128 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6129 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6130 | |
| 6131 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6132 | * so the tree did not actually change and there is nothing for us to do |
| 6133 | */ |
| 6134 | if (unlinked_nodes && ctx_node) { |
| 6135 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6136 | rc = -1; |
| 6137 | goto cleanup; |
| 6138 | } |
| 6139 | } |
| 6140 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6141 | if (rc) { |
| 6142 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6143 | 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] | 6144 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6145 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6146 | } |
| 6147 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6148 | 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] | 6149 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6150 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6151 | 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] | 6152 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6153 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6154 | } else { |
| 6155 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6156 | goto cleanup; |
| 6157 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6158 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6159 | |
| 6160 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6161 | 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] | 6162 | } |
| 6163 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6164 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6165 | } |
| 6166 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6167 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6168 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6169 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 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 ? ctx_node : node, NULL, 0); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6172 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6173 | if (result) { |
| 6174 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6175 | *result = 1; |
| 6176 | } else { |
| 6177 | *result = 0; |
| 6178 | } |
| 6179 | } |
| 6180 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6181 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6182 | } |
| 6183 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6184 | static int |
| 6185 | check_leafref_features(struct lys_type *type) |
| 6186 | { |
| 6187 | struct lys_node *iter; |
| 6188 | struct ly_set *src_parents, *trg_parents, *features; |
| 6189 | unsigned int i, j, size, x; |
| 6190 | int ret = EXIT_SUCCESS; |
| 6191 | |
| 6192 | assert(type->parent); |
| 6193 | |
| 6194 | src_parents = ly_set_new(); |
| 6195 | trg_parents = ly_set_new(); |
| 6196 | features = ly_set_new(); |
| 6197 | |
| 6198 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6199 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6200 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6201 | continue; |
| 6202 | } |
| 6203 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6204 | } |
| 6205 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6206 | 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] | 6207 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6208 | continue; |
| 6209 | } |
| 6210 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6211 | } |
| 6212 | |
| 6213 | /* compare the features used in if-feature statements in the rest of both |
| 6214 | * chains of parents. The set of features used for target must be a subset |
| 6215 | * of features used for the leafref. This is not a perfect, we should compare |
| 6216 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6217 | for (i = 0; i < src_parents->number; i++) { |
| 6218 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6219 | if (!iter->iffeature_size) { |
| 6220 | continue; |
| 6221 | } |
| 6222 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6223 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6224 | for (; size; size--) { |
| 6225 | if (!iter->iffeature[j].features[size - 1]) { |
| 6226 | /* not yet resolved feature, postpone this check */ |
| 6227 | ret = EXIT_FAILURE; |
| 6228 | goto cleanup; |
| 6229 | } |
| 6230 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6231 | } |
| 6232 | } |
| 6233 | } |
| 6234 | x = features->number; |
| 6235 | for (i = 0; i < trg_parents->number; i++) { |
| 6236 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6237 | if (!iter->iffeature_size) { |
| 6238 | continue; |
| 6239 | } |
| 6240 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6241 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6242 | for (; size; size--) { |
| 6243 | if (!iter->iffeature[j].features[size - 1]) { |
| 6244 | /* not yet resolved feature, postpone this check */ |
| 6245 | ret = EXIT_FAILURE; |
| 6246 | goto cleanup; |
| 6247 | } |
| 6248 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6249 | /* the feature is not present in features set of target's parents chain */ |
| 6250 | 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] | 6251 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6252 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6253 | iter->iffeature[j].features[size - 1]->name); |
| 6254 | ret = -1; |
| 6255 | goto cleanup; |
| 6256 | } |
| 6257 | } |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | cleanup: |
| 6262 | ly_set_free(features); |
| 6263 | ly_set_free(src_parents); |
| 6264 | ly_set_free(trg_parents); |
| 6265 | |
| 6266 | return ret; |
| 6267 | } |
| 6268 | |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6269 | static int |
| 6270 | check_type_union_leafref(struct lys_type *type) |
| 6271 | { |
| 6272 | uint8_t i; |
| 6273 | |
| 6274 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6275 | /* go through unions and look for leafref */ |
| 6276 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6277 | switch (type->info.uni.types[i].base) { |
| 6278 | case LY_TYPE_LEAFREF: |
| 6279 | return 1; |
| 6280 | case LY_TYPE_UNION: |
| 6281 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6282 | return 1; |
| 6283 | } |
| 6284 | break; |
| 6285 | default: |
| 6286 | break; |
| 6287 | } |
| 6288 | } |
| 6289 | |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
| 6293 | /* just inherit the flag value */ |
| 6294 | return type->der->has_union_leafref; |
| 6295 | } |
| 6296 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6297 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6298 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6299 | * |
| 6300 | * @param[in] mod Main module. |
| 6301 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6302 | * @param[in] type Type of the unresolved item. |
| 6303 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6304 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6305 | * @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] | 6306 | * |
| 6307 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6308 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6309 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6310 | 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] | 6311 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6312 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6313 | /* 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] | 6314 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6315 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6316 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6317 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6318 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6319 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6320 | struct ly_set *refs, *procs; |
| 6321 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6322 | struct lys_ident *ident; |
| 6323 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6324 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6325 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6326 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6327 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6328 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6329 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6330 | struct lys_ext_instance *ext, **extlist; |
| 6331 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6332 | |
| 6333 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6334 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6335 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6336 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6337 | ident = item; |
| 6338 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6339 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6340 | break; |
| 6341 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6342 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6343 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6344 | stype = item; |
| 6345 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6346 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6347 | break; |
| 6348 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6349 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6350 | stype = item; |
| 6351 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6352 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6353 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6354 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6355 | /* check if leafref and its target are under a common if-features */ |
| 6356 | rc = check_leafref_features(stype); |
| 6357 | if (rc) { |
| 6358 | break; |
| 6359 | } |
| 6360 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6361 | if (lys_node_module(node)->implemented) { |
| 6362 | /* make all the modules on the path implemented */ |
| 6363 | for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) { |
| 6364 | if (!lys_node_module(next)->implemented) { |
| 6365 | if (lys_set_implemented(lys_node_module(next))) { |
| 6366 | rc = -1; |
| 6367 | break; |
| 6368 | } |
| 6369 | } |
| 6370 | } |
| 6371 | if (next) { |
| 6372 | break; |
| 6373 | } |
| 6374 | |
| 6375 | /* store the backlink from leafref target */ |
| 6376 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6377 | rc = -1; |
| 6378 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6379 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6380 | } |
| 6381 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6382 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6383 | case UNRES_TYPE_DER_EXT: |
| 6384 | parent_type++; |
| 6385 | /* no break */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6386 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6387 | parent_type++; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6388 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6389 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6390 | /* parent */ |
| 6391 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6392 | stype = item; |
| 6393 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6394 | /* HACK type->der is temporarily unparsed type statement */ |
| 6395 | yin = (struct lyxml_elem *)stype->der; |
| 6396 | stype->der = NULL; |
| 6397 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6398 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6399 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6400 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6401 | |
| 6402 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6403 | /* may try again later */ |
| 6404 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6405 | } else { |
| 6406 | /* 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] | 6407 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6408 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6409 | } |
| 6410 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6411 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6412 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6413 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6414 | /* we need to always be able to free this, it's safe only in this case */ |
| 6415 | lyxml_free(mod->ctx, yin); |
| 6416 | } else { |
| 6417 | /* may try again later, put all back how it was */ |
| 6418 | stype->der = (struct lys_tpdf *)yin; |
| 6419 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6420 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6421 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6422 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6423 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6424 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6425 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6426 | |
| 6427 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6428 | /* fill typedef union leafref flag */ |
| 6429 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6430 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6431 | /* copy the type in case it has union leafref flag */ |
| 6432 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
| 6433 | LOGERR(LY_EINT, "Failed to duplicate type."); |
| 6434 | return -1; |
| 6435 | } |
| 6436 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6437 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6438 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6439 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6440 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6441 | * 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] | 6442 | * 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] | 6443 | * of the type's base member. */ |
| 6444 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6445 | if (par_grp) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 6446 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 6447 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++; |
| 6448 | #else |
| 6449 | ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++; |
| 6450 | #endif |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6451 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6452 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6453 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6455 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6456 | iff_data = str_snode; |
| 6457 | 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] | 6458 | if (!rc) { |
| 6459 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6460 | if (iff_data->infeature) { |
| 6461 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6462 | feat = *((struct lys_feature **)item); |
| 6463 | if (!feat->depfeatures) { |
| 6464 | feat->depfeatures = ly_set_new(); |
| 6465 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6466 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6467 | } |
| 6468 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6469 | lydict_remove(mod->ctx, iff_data->fname); |
| 6470 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6471 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6472 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6473 | case UNRES_FEATURE: |
| 6474 | feat = (struct lys_feature *)item; |
| 6475 | |
| 6476 | if (feat->iffeature_size) { |
| 6477 | refs = ly_set_new(); |
| 6478 | procs = ly_set_new(); |
| 6479 | ly_set_add(procs, feat, 0); |
| 6480 | |
| 6481 | while (procs->number) { |
| 6482 | ref = procs->set.g[procs->number - 1]; |
| 6483 | ly_set_rm_index(procs, procs->number - 1); |
| 6484 | |
| 6485 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6486 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6487 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6488 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6489 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6490 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6491 | goto featurecheckdone; |
| 6492 | } |
| 6493 | |
| 6494 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6495 | k = refs->number; |
| 6496 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6497 | /* not yet seen feature, add it for processing */ |
| 6498 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6499 | } |
| 6500 | } |
| 6501 | } else { |
| 6502 | /* forward reference */ |
| 6503 | rc = EXIT_FAILURE; |
| 6504 | goto featurecheckdone; |
| 6505 | } |
| 6506 | } |
| 6507 | |
| 6508 | } |
| 6509 | } |
| 6510 | rc = EXIT_SUCCESS; |
| 6511 | |
| 6512 | featurecheckdone: |
| 6513 | ly_set_free(refs); |
| 6514 | ly_set_free(procs); |
| 6515 | } |
| 6516 | |
| 6517 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6518 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6519 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6520 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6521 | case UNRES_TYPEDEF_DFLT: |
| 6522 | parent_type++; |
| 6523 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6524 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6525 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6526 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6527 | break; |
| 6528 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6529 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6530 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6531 | choic = item; |
| 6532 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6533 | if (!choic->dflt) { |
| 6534 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6535 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6536 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6537 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6538 | } else { |
| 6539 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6540 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6541 | break; |
| 6542 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6543 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6544 | break; |
| 6545 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6546 | unique_info = (struct unres_list_uniq *)item; |
| 6547 | 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] | 6548 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6549 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6550 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6551 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6552 | case UNRES_XPATH: |
| 6553 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6554 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6555 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6556 | case UNRES_EXT: |
| 6557 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6558 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6559 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6560 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6561 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6562 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6563 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6564 | if (eplugin) { |
| 6565 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6566 | u = malloc(sizeof *u); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6567 | LY_CHECK_ERR_RETURN(!u, LOGMEM, -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6568 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6569 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6570 | /* something really bad happend since the extension finalization is not actually |
| 6571 | * being resolved while adding into unres, so something more serious with the unres |
| 6572 | * list itself must happened */ |
| 6573 | return -1; |
| 6574 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6575 | } |
| 6576 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6577 | } |
| 6578 | if (!rc || rc == -1) { |
| 6579 | /* cleanup on success or fatal error */ |
| 6580 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6581 | /* YIN */ |
| 6582 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6583 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6584 | /* YANG */ |
| 6585 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6586 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6587 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6588 | } |
| 6589 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6590 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6591 | u = (uint8_t *)str_snode; |
| 6592 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6593 | free(u); |
| 6594 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6595 | eplugin = ext->def->plugin; |
| 6596 | |
| 6597 | /* inherit */ |
| 6598 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6599 | root = (struct lys_node *)ext->parent; |
| 6600 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6601 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6602 | /* first, check if the node already contain instance of the same extension, |
| 6603 | * in such a case we won't inherit. In case the node was actually defined as |
| 6604 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6605 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6606 | goto inherit_dfs_sibling; |
| 6607 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6608 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6609 | goto inherit_dfs_sibling; |
| 6610 | } |
| 6611 | |
| 6612 | if (eplugin->check_inherit) { |
| 6613 | /* we have a callback to check the inheritance, use it */ |
| 6614 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6615 | case 0: |
| 6616 | /* yes - continue with the inheriting code */ |
| 6617 | break; |
| 6618 | case 1: |
| 6619 | /* no - continue with the node's sibling */ |
| 6620 | goto inherit_dfs_sibling; |
| 6621 | case 2: |
| 6622 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6623 | goto inherit_dfs_child; |
| 6624 | default: |
| 6625 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6626 | ext->def->module->name, ext->def->name, rc); |
| 6627 | } |
| 6628 | } |
| 6629 | |
| 6630 | /* inherit the extension */ |
| 6631 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6632 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6633 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6634 | LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM; node->ext = extlist, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6635 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6636 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6637 | |
| 6638 | node->ext = extlist; |
| 6639 | node->ext_size++; |
| 6640 | |
| 6641 | inherit_dfs_child: |
| 6642 | /* modification of - select element for the next run - children first */ |
| 6643 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6644 | next = NULL; |
| 6645 | } else { |
| 6646 | next = node->child; |
| 6647 | } |
| 6648 | if (!next) { |
| 6649 | inherit_dfs_sibling: |
| 6650 | /* no children, try siblings */ |
| 6651 | next = node->next; |
| 6652 | } |
| 6653 | while (!next) { |
| 6654 | /* go to the parent */ |
| 6655 | node = lys_parent(node); |
| 6656 | |
| 6657 | /* we are done if we are back in the root (the starter's parent */ |
| 6658 | if (node == root) { |
| 6659 | break; |
| 6660 | } |
| 6661 | |
| 6662 | /* parent is already processed, go to its sibling */ |
| 6663 | next = node->next; |
| 6664 | } |
| 6665 | } |
| 6666 | } |
| 6667 | } |
| 6668 | |
| 6669 | /* final check */ |
| 6670 | if (eplugin->check_result) { |
| 6671 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6672 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6673 | return -1; |
| 6674 | } |
| 6675 | } |
| 6676 | |
| 6677 | rc = 0; |
| 6678 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6679 | default: |
| 6680 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6681 | break; |
| 6682 | } |
| 6683 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6684 | if (has_str && !rc) { |
| 6685 | /* the string is no more needed in case of success. |
| 6686 | * 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] | 6687 | lydict_remove(mod->ctx, str_snode); |
| 6688 | } |
| 6689 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6690 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6691 | } |
| 6692 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6693 | /* logs directly */ |
| 6694 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6695 | 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] | 6696 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6697 | struct lyxml_elem *xml; |
| 6698 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6699 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6700 | const char *name = NULL; |
| 6701 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6702 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6703 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6704 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6705 | 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] | 6706 | break; |
| 6707 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6708 | 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] | 6709 | break; |
| 6710 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6711 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6712 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6713 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6714 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6715 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6716 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6717 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6718 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6719 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6720 | } else { |
| 6721 | LY_TREE_FOR(xml->attr, attr) { |
| 6722 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6723 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6724 | break; |
| 6725 | } |
| 6726 | } |
| 6727 | assert(attr); |
| 6728 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6729 | 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] | 6730 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6731 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6732 | iff_data = str_node; |
| 6733 | 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] | 6734 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6735 | case UNRES_FEATURE: |
| 6736 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6737 | ((struct lys_feature *)item)->name); |
| 6738 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6739 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6740 | 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] | 6741 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6742 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6743 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6744 | if (str_node) { |
| 6745 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6746 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6747 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6748 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6749 | break; |
| 6750 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6751 | 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] | 6752 | break; |
| 6753 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6754 | 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] | 6755 | break; |
| 6756 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6757 | 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] | 6758 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6759 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6760 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6761 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6762 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6763 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6764 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6765 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6766 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6767 | case UNRES_EXT: |
| 6768 | extinfo = (struct unres_ext *)str_node; |
| 6769 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6770 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6771 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6772 | default: |
| 6773 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6774 | break; |
| 6775 | } |
| 6776 | } |
| 6777 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6778 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6779 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6780 | * |
| 6781 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6782 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6783 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6784 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6785 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6786 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6787 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6788 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6789 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6790 | struct lyxml_elem *yin; |
| 6791 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6792 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6793 | |
| 6794 | assert(unres); |
| 6795 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6796 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6797 | if (*ly_vlog_hide_location()) { |
| 6798 | log_hidden = 1; |
| 6799 | } else { |
| 6800 | log_hidden = 0; |
| 6801 | ly_vlog_hide(1); |
| 6802 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6803 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6804 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6805 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6806 | unres_count = 0; |
| 6807 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6808 | |
| 6809 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6810 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6811 | * if-features are resolved here to make sure that we will have all if-features for |
| 6812 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6813 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6814 | continue; |
| 6815 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6816 | /* 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] | 6817 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6818 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6819 | ++unres_count; |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6820 | 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] | 6821 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6822 | unres->type[i] = UNRES_RESOLVED; |
| 6823 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6824 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6825 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6826 | if (!log_hidden) { |
| 6827 | ly_vlog_hide(0); |
| 6828 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6829 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6830 | ly_err_repeat(); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6831 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6832 | } else { |
| 6833 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6834 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6835 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6836 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6837 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6838 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6839 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6840 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6841 | if (!log_hidden) { |
| 6842 | ly_vlog_hide(0); |
| 6843 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6844 | |
| 6845 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6846 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6847 | continue; |
| 6848 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6849 | 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] | 6850 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6851 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6852 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6853 | yang =(struct yang_type *)yin; |
| 6854 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6855 | if (yang->base == LY_TYPE_UNION) { |
| 6856 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6857 | } |
| 6858 | lydict_remove(mod->ctx, yang->name); |
| 6859 | free(yang); |
| 6860 | } else { |
| 6861 | lyxml_free(mod->ctx, yin); |
| 6862 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6863 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6864 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6865 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6866 | } |
| 6867 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6868 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6869 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6870 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6871 | continue; |
| 6872 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 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 | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6875 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6876 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6877 | /* free the allocated structure */ |
| 6878 | free(unres->item[i]); |
| 6879 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6880 | unres->type[i] = UNRES_RESOLVED; |
| 6881 | ++resolved; |
| 6882 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6883 | if (!log_hidden) { |
| 6884 | ly_vlog_hide(0); |
| 6885 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6886 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6887 | ly_err_repeat(); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6888 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6889 | } else { |
| 6890 | /* forward reference, erase ly_errno */ |
| 6891 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6892 | } |
| 6893 | } |
| 6894 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6895 | if (!log_hidden) { |
| 6896 | ly_vlog_hide(0); |
| 6897 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6898 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6899 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6900 | for (i = 0; i < unres->count; ++i) { |
| 6901 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6902 | continue; |
| 6903 | } |
| 6904 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6905 | 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] | 6906 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6907 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6908 | ++resolved; |
| 6909 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6910 | /* else error - it was already printed, but resolved was not increased, |
| 6911 | so this unres item will not be resolved again in the following code, |
| 6912 | but it will cause returning -1 at the end, this way we are able to |
| 6913 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6914 | } |
| 6915 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6916 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6917 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6918 | * all the validation errors |
| 6919 | */ |
| 6920 | for (i = 0; i < unres->count; ++i) { |
| 6921 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6922 | continue; |
| 6923 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6924 | 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] | 6925 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6926 | /* 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] | 6927 | unres->type[i] = UNRES_RESOLVED; |
| 6928 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6929 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6930 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6931 | if (resolved < unres->count) { |
| 6932 | return -1; |
| 6933 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6934 | } |
| 6935 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6936 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6937 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6938 | return EXIT_SUCCESS; |
| 6939 | } |
| 6940 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6941 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6942 | * @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] | 6943 | * |
| 6944 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6945 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6946 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6947 | * @param[in] type Type of the unresolved item. |
| 6948 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6949 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6950 | * @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] | 6951 | */ |
| 6952 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6953 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6954 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6955 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6956 | int rc; |
| 6957 | const char *dictstr; |
| 6958 | |
| 6959 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6960 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6961 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6962 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6963 | lydict_remove(mod->ctx, dictstr); |
| 6964 | } |
| 6965 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6966 | } |
| 6967 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6968 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6969 | * @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] | 6970 | * |
| 6971 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6972 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6973 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6974 | * @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] | 6975 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6976 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6977 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6978 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6979 | */ |
| 6980 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6981 | 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] | 6982 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6983 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6984 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6985 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6986 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6987 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6988 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6989 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6990 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 6991 | uint32_t u; |
| 6992 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6993 | /* check for duplicities in unres */ |
| 6994 | for (u = 0; u < unres->count; u++) { |
| 6995 | if (unres->type[u] == type && unres->item[u] == item && |
| 6996 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6997 | /* duplication, should not happen */ |
| 6998 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6999 | } |
| 7000 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 7001 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7002 | |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7003 | if (type == UNRES_EXT_FINALIZE) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7004 | /* 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] | 7005 | rc = EXIT_FAILURE; |
| 7006 | } else { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7007 | if (*ly_vlog_hide_location()) { |
| 7008 | log_hidden = 1; |
| 7009 | } else { |
| 7010 | log_hidden = 0; |
| 7011 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7012 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame] | 7013 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7014 | if (!log_hidden) { |
| 7015 | ly_vlog_hide(0); |
| 7016 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7017 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7018 | if (rc != EXIT_FAILURE) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7019 | if (rc == -1) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7020 | ly_err_repeat(); |
| 7021 | } |
| 7022 | if (type == UNRES_LIST_UNIQ) { |
| 7023 | /* free the allocated structure */ |
| 7024 | free(item); |
| 7025 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7026 | /* free the allocated resources */ |
| 7027 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7028 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7029 | return rc; |
| 7030 | } else { |
| 7031 | /* erase info about validation errors */ |
| 7032 | ly_err_clean(1); |
| 7033 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7034 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7035 | print_unres_schema_item_fail(item, type, snode); |
| 7036 | |
| 7037 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7038 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7039 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7040 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7041 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7042 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7043 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7044 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7045 | } |
| 7046 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7047 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7048 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7049 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7050 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7051 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7052 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7053 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7054 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7055 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7056 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7057 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7058 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7059 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7060 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7061 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7062 | } |
| 7063 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7064 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7065 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7066 | * |
| 7067 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7068 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7069 | * @param[in] item Old item to be resolved. |
| 7070 | * @param[in] type Type of the old unresolved item. |
| 7071 | * @param[in] new_item New item to use in the duplicate. |
| 7072 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7073 | * @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] | 7074 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7075 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7076 | 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] | 7077 | { |
| 7078 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7079 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7080 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7081 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7082 | 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] | 7083 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7084 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7085 | if (type == UNRES_LIST_UNIQ) { |
| 7086 | aux_uniq.list = item; |
| 7087 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7088 | item = &aux_uniq; |
| 7089 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7090 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7091 | |
| 7092 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7093 | if (type == UNRES_LIST_UNIQ) { |
| 7094 | free(new_item); |
| 7095 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7096 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7097 | } |
| 7098 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7099 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7100 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7101 | 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] | 7102 | LOGINT; |
| 7103 | return -1; |
| 7104 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7105 | } else if (type == UNRES_IFFEAT) { |
| 7106 | /* duplicate unres_iffeature_data */ |
| 7107 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7108 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7109 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7110 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7111 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7112 | LOGINT; |
| 7113 | return -1; |
| 7114 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7115 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7116 | 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] | 7117 | LOGINT; |
| 7118 | return -1; |
| 7119 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7120 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7121 | |
| 7122 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7123 | } |
| 7124 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7125 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7126 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7127 | 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] | 7128 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7129 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7130 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7131 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7132 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7133 | i = start_on_backwards; |
| 7134 | } else { |
| 7135 | i = unres->count - 1; |
| 7136 | } |
| 7137 | for (; i > -1; i--) { |
| 7138 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7139 | continue; |
| 7140 | } |
| 7141 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7142 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7143 | break; |
| 7144 | } |
| 7145 | } else { |
| 7146 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7147 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7148 | 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] | 7149 | break; |
| 7150 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7151 | } |
| 7152 | } |
| 7153 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7154 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7155 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7156 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7157 | static void |
| 7158 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7159 | { |
| 7160 | struct lyxml_elem *yin; |
| 7161 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7162 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7163 | |
| 7164 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7165 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7166 | case UNRES_TYPE_DER: |
| 7167 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7168 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7169 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7170 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7171 | lydict_remove(ctx, yang->name); |
| 7172 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7173 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7174 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7175 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7176 | } else { |
| 7177 | lyxml_free(ctx, yin); |
| 7178 | } |
| 7179 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7180 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7181 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7182 | lydict_remove(ctx, iff_data->fname); |
| 7183 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7184 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7185 | case UNRES_IDENT: |
| 7186 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7187 | case UNRES_CHOICE_DFLT: |
| 7188 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7189 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7190 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7191 | case UNRES_LIST_UNIQ: |
| 7192 | free(unres->item[i]); |
| 7193 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7194 | case UNRES_EXT: |
| 7195 | free(unres->str_snode[i]); |
| 7196 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7197 | default: |
| 7198 | break; |
| 7199 | } |
| 7200 | unres->type[i] = UNRES_RESOLVED; |
| 7201 | } |
| 7202 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7203 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7204 | unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7205 | { |
| 7206 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7207 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7208 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7209 | if (!unres || !(*unres)) { |
| 7210 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7211 | } |
| 7212 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7213 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7214 | |
| 7215 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7216 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7217 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7218 | unresolved++; |
| 7219 | } |
| 7220 | continue; |
| 7221 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7222 | |
| 7223 | /* free heap memory for the specific item */ |
| 7224 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7225 | } |
| 7226 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7227 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7228 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7229 | free((*unres)->item); |
| 7230 | free((*unres)->type); |
| 7231 | free((*unres)->str_snode); |
| 7232 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7233 | free((*unres)); |
| 7234 | (*unres) = NULL; |
| 7235 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7236 | } |
| 7237 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7238 | static int |
| 7239 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7240 | { |
| 7241 | struct ly_set *set; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7242 | struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7243 | char *buf; |
| 7244 | |
| 7245 | for (op_node = lys_parent(sleaf); |
| 7246 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7247 | op_node = lys_parent(op_node)); |
| 7248 | |
| 7249 | if (op_node && lys_parent(op_node)) { |
| 7250 | /* nested operation - any absolute path is external */ |
| 7251 | return 1; |
| 7252 | } |
| 7253 | |
| 7254 | /* get the first node from the instid */ |
| 7255 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7256 | if (!buf) { |
| 7257 | LOGMEM; |
| 7258 | return -1; |
| 7259 | } |
| 7260 | |
| 7261 | /* there is a predicate, remove it */ |
| 7262 | if (buf[strlen(buf) - 1] == ']') { |
| 7263 | assert(strchr(buf, '[')); |
| 7264 | *strchr(buf, '[') = '\0'; |
| 7265 | } |
| 7266 | |
| 7267 | /* find the first schema node */ |
Michal Vasko | 2611e19 | 2017-01-23 10:33:21 +0100 | [diff] [blame] | 7268 | set = lys_find_xpath(NULL, sleaf, buf, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7269 | if (!set || !set->number) { |
| 7270 | free(buf); |
Michal Vasko | 29fd974 | 2017-01-23 09:55:44 +0100 | [diff] [blame] | 7271 | ly_set_free(set); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7272 | return 1; |
| 7273 | } |
| 7274 | free(buf); |
| 7275 | |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7276 | first_node = set->set.s[0]; |
| 7277 | ly_set_free(set); |
| 7278 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7279 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7280 | |
| 7281 | if (op_node) { |
| 7282 | /* 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] | 7283 | if (op_node == first_node) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7284 | assert(set->number == 1); |
| 7285 | return 0; |
| 7286 | } else { |
| 7287 | return 1; |
| 7288 | } |
| 7289 | } |
| 7290 | |
| 7291 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7292 | * this itself), so we say it's not external */ |
| 7293 | return 0; |
| 7294 | } |
| 7295 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7296 | /** |
| 7297 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7298 | * |
| 7299 | * @param[in] data Data node where the path is used |
| 7300 | * @param[in] path Instance-identifier node value. |
| 7301 | * @param[in,out] ret Resolved instance or NULL. |
| 7302 | * |
| 7303 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7304 | */ |
| 7305 | static int |
| 7306 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7307 | { |
| 7308 | int i = 0, j; |
| 7309 | const struct lys_module *mod; |
| 7310 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7311 | const char *model, *name; |
| 7312 | char *str; |
| 7313 | int mod_len, name_len, has_predicate; |
| 7314 | struct unres_data node_match; |
| 7315 | |
| 7316 | memset(&node_match, 0, sizeof node_match); |
| 7317 | *ret = NULL; |
| 7318 | |
| 7319 | /* we need root to resolve absolute path */ |
| 7320 | for (; data->parent; data = data->parent); |
| 7321 | /* we're still parsing it and the pointer is not correct yet */ |
| 7322 | if (data->prev) { |
| 7323 | for (; data->prev->next; data = data->prev); |
| 7324 | } |
| 7325 | |
| 7326 | /* search for the instance node */ |
| 7327 | while (path[i]) { |
| 7328 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7329 | if (j <= 0) { |
| 7330 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7331 | goto error; |
| 7332 | } |
| 7333 | i += j; |
| 7334 | |
| 7335 | str = strndup(model, mod_len); |
| 7336 | if (!str) { |
| 7337 | LOGMEM; |
| 7338 | goto error; |
| 7339 | } |
| 7340 | mod = ly_ctx_get_module(ctx, str, NULL); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7341 | if (ctx->data_clb) { |
| 7342 | if (!mod) { |
| 7343 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7344 | } else if (!mod->implemented) { |
| 7345 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7346 | } |
| 7347 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7348 | free(str); |
| 7349 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7350 | if (!mod || !mod->implemented || mod->disabled) { |
| 7351 | break; |
| 7352 | } |
| 7353 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7354 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7355 | /* no instance exists */ |
| 7356 | break; |
| 7357 | } |
| 7358 | |
| 7359 | if (has_predicate) { |
| 7360 | /* 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] | 7361 | j = resolve_instid_predicate(&path[i], &node_match); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7362 | if (j < 1) { |
| 7363 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7364 | goto error; |
| 7365 | } |
| 7366 | i += j; |
| 7367 | |
| 7368 | if (!node_match.count) { |
| 7369 | /* no instance exists */ |
| 7370 | break; |
| 7371 | } |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7372 | } else if (node_match.count) { |
| 7373 | /* check that we are not addressing lists */ |
| 7374 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7375 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7376 | unres_data_del(&node_match, j--); |
| 7377 | } |
| 7378 | } |
| 7379 | if (!node_match.count) { |
| 7380 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys."); |
| 7381 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7382 | } |
| 7383 | } |
| 7384 | |
| 7385 | if (!node_match.count) { |
| 7386 | /* no instance exists */ |
| 7387 | if (req_inst > -1) { |
| 7388 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7389 | return EXIT_FAILURE; |
| 7390 | } |
| 7391 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7392 | return EXIT_SUCCESS; |
| 7393 | } else if (node_match.count > 1) { |
| 7394 | /* instance identifier must resolve to a single node */ |
| 7395 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7396 | goto error; |
| 7397 | } else { |
| 7398 | /* we have required result, remember it and cleanup */ |
| 7399 | *ret = node_match.node[0]; |
| 7400 | free(node_match.node); |
| 7401 | return EXIT_SUCCESS; |
| 7402 | } |
| 7403 | |
| 7404 | error: |
| 7405 | /* cleanup */ |
| 7406 | free(node_match.node); |
| 7407 | return -1; |
| 7408 | } |
| 7409 | |
| 7410 | static int |
| 7411 | 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] | 7412 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7413 | struct unres_data matches; |
| 7414 | uint32_t i; |
| 7415 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7416 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7417 | memset(&matches, 0, sizeof matches); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7418 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7419 | |
| 7420 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7421 | if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7422 | return -1; |
| 7423 | } |
| 7424 | |
| 7425 | /* check that value matches */ |
| 7426 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7427 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7428 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7429 | 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] | 7430 | /* we have the match */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7431 | *ret = matches.node[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7432 | break; |
| 7433 | } |
| 7434 | } |
| 7435 | |
| 7436 | free(matches.node); |
| 7437 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7438 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7439 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7440 | if (req_inst > -1) { |
| 7441 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7442 | return EXIT_FAILURE; |
| 7443 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7444 | 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] | 7445 | } |
| 7446 | } |
| 7447 | |
| 7448 | return EXIT_SUCCESS; |
| 7449 | } |
| 7450 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7451 | /* 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] | 7452 | int |
| 7453 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7454 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7455 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7456 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7457 | struct lyd_node *ret; |
| 7458 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7459 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7460 | |
| 7461 | assert(type->base == LY_TYPE_UNION); |
| 7462 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7463 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7464 | /* either NULL or instid previously converted to JSON */ |
| 7465 | json_val = leaf->value.string; |
| 7466 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7467 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7468 | if (store) { |
| 7469 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7470 | free(leaf->value.bit); |
| 7471 | } |
| 7472 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7473 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7474 | |
| 7475 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7476 | hidden = *ly_vlog_hide_location(); |
| 7477 | ly_vlog_hide(1); |
| 7478 | |
| 7479 | t = NULL; |
| 7480 | found = 0; |
| 7481 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7482 | found = 0; |
| 7483 | |
| 7484 | switch (t->base) { |
| 7485 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7486 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7487 | req_inst = -1; |
| 7488 | } else { |
| 7489 | req_inst = t->info.lref.req; |
| 7490 | } |
| 7491 | |
| 7492 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7493 | if (store) { |
| 7494 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7495 | /* valid resolved */ |
| 7496 | leaf->value.leafref = ret; |
| 7497 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7498 | } else { |
| 7499 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7500 | 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] | 7501 | return -1; |
| 7502 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | success = 1; |
| 7507 | } |
| 7508 | break; |
| 7509 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7510 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7511 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7512 | req_inst = -1; |
| 7513 | } else { |
| 7514 | req_inst = t->info.inst.req; |
| 7515 | } |
| 7516 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7517 | 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] | 7518 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7519 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7520 | /* valid resolved */ |
| 7521 | leaf->value.instance = ret; |
| 7522 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7523 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7524 | if (json_val) { |
| 7525 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7526 | leaf->value_str = json_val; |
| 7527 | json_val = NULL; |
| 7528 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7529 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7530 | /* valid unresolved */ |
| 7531 | if (json_val) { |
| 7532 | /* put the JSON val back */ |
| 7533 | leaf->value.string = json_val; |
| 7534 | json_val = NULL; |
| 7535 | } else { |
| 7536 | leaf->value.instance = NULL; |
| 7537 | } |
| 7538 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7539 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7540 | } |
| 7541 | |
| 7542 | success = 1; |
| 7543 | } |
| 7544 | break; |
| 7545 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7546 | 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] | 7547 | success = 1; |
| 7548 | } |
| 7549 | break; |
| 7550 | } |
| 7551 | |
| 7552 | if (success) { |
| 7553 | break; |
| 7554 | } |
| 7555 | |
| 7556 | /* erase information about errors - they are false or irrelevant |
| 7557 | * and will be replaced by a single error messages */ |
| 7558 | ly_err_clean(1); |
| 7559 | |
| 7560 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7561 | if (store) { |
| 7562 | if (t->base == LY_TYPE_BITS) { |
| 7563 | free(leaf->value.bit); |
| 7564 | } |
| 7565 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7566 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7567 | } |
| 7568 | |
| 7569 | /* turn logging back on */ |
| 7570 | if (!hidden) { |
| 7571 | ly_vlog_hide(0); |
| 7572 | } |
| 7573 | |
| 7574 | if (json_val) { |
| 7575 | if (!success) { |
| 7576 | /* put the value back for now */ |
| 7577 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7578 | leaf->value.string = json_val; |
| 7579 | } else { |
| 7580 | /* value was ultimately useless, but we could not have known */ |
| 7581 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7582 | } |
| 7583 | } |
| 7584 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7585 | if (success) { |
| 7586 | if (resolved_type) { |
| 7587 | *resolved_type = t; |
| 7588 | } |
| 7589 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7590 | /* not found and it is required */ |
| 7591 | 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] | 7592 | return EXIT_FAILURE; |
| 7593 | } |
| 7594 | |
| 7595 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7596 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7597 | } |
| 7598 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7599 | /** |
| 7600 | * @brief Resolve a single unres data item. Logs directly. |
| 7601 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7602 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7603 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7604 | * @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] | 7605 | * |
| 7606 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7607 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7608 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7609 | 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] | 7610 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7611 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7612 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7613 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7614 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7615 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7616 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7617 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7618 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7619 | switch (type) { |
| 7620 | case UNRES_LEAFREF: |
| 7621 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7622 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7623 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7624 | req_inst = -1; |
| 7625 | } else { |
| 7626 | req_inst = sleaf->type.info.lref.req; |
| 7627 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7628 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7629 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7630 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7631 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7632 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7633 | free(leaf->value.bit); |
| 7634 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7635 | leaf->value.leafref = ret; |
| 7636 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7637 | } else { |
| 7638 | /* valid unresolved */ |
| 7639 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7640 | 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] | 7641 | return -1; |
| 7642 | } |
| 7643 | } |
| 7644 | } |
| 7645 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7646 | } else { |
| 7647 | return rc; |
| 7648 | } |
| 7649 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7650 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7651 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7652 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7653 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7654 | if (ext_dep == -1) { |
| 7655 | return -1; |
| 7656 | } |
| 7657 | |
| 7658 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7659 | req_inst = -1; |
| 7660 | } else { |
| 7661 | req_inst = sleaf->type.info.inst.req; |
| 7662 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7663 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7664 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7665 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7666 | /* valid resolved */ |
| 7667 | leaf->value.instance = ret; |
| 7668 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7669 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7670 | /* valid unresolved */ |
| 7671 | leaf->value.instance = NULL; |
| 7672 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7673 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7674 | } else { |
| 7675 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7676 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7677 | break; |
| 7678 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7679 | case UNRES_UNION: |
| 7680 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7681 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7682 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7683 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7684 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7685 | return rc; |
| 7686 | } |
| 7687 | break; |
| 7688 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7689 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7690 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7691 | return rc; |
| 7692 | } |
| 7693 | break; |
| 7694 | |
| 7695 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7696 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7697 | return rc; |
| 7698 | } |
| 7699 | break; |
| 7700 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7701 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7702 | LOGINT; |
| 7703 | return -1; |
| 7704 | } |
| 7705 | |
| 7706 | return EXIT_SUCCESS; |
| 7707 | } |
| 7708 | |
| 7709 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7710 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7711 | * |
| 7712 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7713 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7714 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7715 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7716 | */ |
| 7717 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7718 | 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] | 7719 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7720 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7721 | 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] | 7722 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7723 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7724 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7725 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7726 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7727 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7728 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7729 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7730 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7731 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7732 | if (type == UNRES_WHEN) { |
| 7733 | /* remove previous result */ |
| 7734 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7735 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7736 | |
| 7737 | return EXIT_SUCCESS; |
| 7738 | } |
| 7739 | |
| 7740 | /** |
| 7741 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7742 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7743 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7744 | * unresolved leafrefs/instids are accepted). |
| 7745 | * |
| 7746 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7747 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7748 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7749 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7750 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7751 | * |
| 7752 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7753 | */ |
| 7754 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7755 | 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] | 7756 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7757 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7758 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7759 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7760 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7761 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7762 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7763 | |
| 7764 | if (!unres->count) { |
| 7765 | return EXIT_SUCCESS; |
| 7766 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7767 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7768 | 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] | 7769 | ignore_fail = 1; |
| 7770 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7771 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7772 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7773 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7774 | } |
| 7775 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7776 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7777 | ly_vlog_hide(1); |
| 7778 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7779 | /* when-stmt first */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7780 | first = 1; |
| 7781 | stmt_count = 0; |
| 7782 | resolved = 0; |
| 7783 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7784 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7785 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7786 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7787 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7788 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7789 | continue; |
| 7790 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7791 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7792 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7793 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7794 | } |
| 7795 | |
| 7796 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7797 | for (parent = unres->node[i]->parent; |
| 7798 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7799 | parent = parent->parent) { |
| 7800 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7801 | /* the parent node was already unlinked, do not resolve this node, |
| 7802 | * it will be removed anyway, so just mark it as resolved |
| 7803 | */ |
| 7804 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7805 | unres->type[i] = UNRES_RESOLVED; |
| 7806 | resolved++; |
| 7807 | break; |
| 7808 | } |
| 7809 | } |
| 7810 | if (parent) { |
| 7811 | continue; |
| 7812 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7813 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7814 | 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] | 7815 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7816 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7817 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7818 | /* false when condition */ |
| 7819 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7820 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7821 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7822 | } /* follows else */ |
| 7823 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7824 | /* auto-delete */ |
| 7825 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7826 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7827 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7828 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7829 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7830 | * remove the container |
| 7831 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7832 | for (parent = unres->node[i]; |
| 7833 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7834 | parent = parent->parent) { |
| 7835 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7836 | /* presence container */ |
| 7837 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7838 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7839 | if (parent->next || parent->prev != parent) { |
| 7840 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7841 | break; |
| 7842 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7843 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7844 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7845 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7846 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7847 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7848 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7849 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7850 | lyd_unlink(unres->node[i]); |
| 7851 | unres->type[i] = UNRES_DELETE; |
| 7852 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7853 | |
| 7854 | /* update the rest of unres items */ |
| 7855 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7856 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7857 | continue; |
| 7858 | } |
| 7859 | |
| 7860 | /* test if the node is in subtree to be deleted */ |
| 7861 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7862 | if (parent == unres->node[i]) { |
| 7863 | /* yes, it is */ |
| 7864 | unres->type[j] = UNRES_RESOLVED; |
| 7865 | resolved++; |
| 7866 | break; |
| 7867 | } |
| 7868 | } |
| 7869 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7870 | } else { |
| 7871 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7872 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7873 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7874 | resolved++; |
| 7875 | progress = 1; |
| 7876 | } else if (rc == -1) { |
| 7877 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7878 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7879 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7880 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7881 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7882 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7883 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7884 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7885 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7886 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7887 | if (stmt_count > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7888 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7889 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7890 | return -1; |
| 7891 | } |
| 7892 | |
| 7893 | for (i = 0; del_items && i < unres->count; i++) { |
| 7894 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7895 | if (unres->type[i] != UNRES_DELETE) { |
| 7896 | continue; |
| 7897 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7898 | if (!unres->node[i]) { |
| 7899 | unres->type[i] = UNRES_RESOLVED; |
| 7900 | del_items--; |
| 7901 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | /* really remove the complete subtree */ |
| 7905 | lyd_free(unres->node[i]); |
| 7906 | unres->type[i] = UNRES_RESOLVED; |
| 7907 | del_items--; |
| 7908 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7909 | |
| 7910 | /* now leafrefs */ |
| 7911 | first = 1; |
| 7912 | stmt_count = 0; |
| 7913 | resolved = 0; |
| 7914 | do { |
| 7915 | progress = 0; |
| 7916 | for (i = 0; i < unres->count; i++) { |
| 7917 | if (unres->type[i] != UNRES_LEAFREF) { |
| 7918 | continue; |
| 7919 | } |
| 7920 | if (first) { |
| 7921 | /* count leafref nodes in unres list */ |
| 7922 | stmt_count++; |
| 7923 | } |
| 7924 | |
| 7925 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7926 | if (!rc) { |
| 7927 | unres->type[i] = UNRES_RESOLVED; |
| 7928 | ly_err_clean(1); |
| 7929 | resolved++; |
| 7930 | progress = 1; |
| 7931 | } else if (rc == -1) { |
| 7932 | ly_vlog_hide(0); |
| 7933 | /* print only this last error */ |
| 7934 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7935 | return -1; |
| 7936 | } /* else forward reference */ |
| 7937 | } |
| 7938 | first = 0; |
| 7939 | } while (progress && resolved < stmt_count); |
| 7940 | |
| 7941 | /* do we have some unresolved leafrefs? */ |
| 7942 | if (stmt_count > resolved) { |
| 7943 | ly_vlog_hide(0); |
| 7944 | ly_err_repeat(); |
| 7945 | return -1; |
| 7946 | } |
| 7947 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7948 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7949 | |
| 7950 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7951 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7952 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7953 | continue; |
| 7954 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7955 | 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] | 7956 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7957 | 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] | 7958 | if (rc) { |
| 7959 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7960 | return -1; |
| 7961 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7962 | |
| 7963 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7964 | } |
| 7965 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7966 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7967 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7968 | return EXIT_SUCCESS; |
| 7969 | } |