Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
| 32 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 33 | int |
| 34 | 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] | 35 | { |
| 36 | const char *ptr; |
| 37 | int minus = 0; |
| 38 | int64_t ret = 0; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 39 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 40 | |
| 41 | ptr = *str_num; |
| 42 | |
| 43 | if (ptr[0] == '-') { |
| 44 | minus = 1; |
| 45 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 46 | } else if (ptr[0] == '+') { |
| 47 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 50 | if (!isdigit(ptr[0])) { |
| 51 | /* there must be at least one */ |
| 52 | return 1; |
| 53 | } |
| 54 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 55 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 56 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 57 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (ptr[0] == '.') { |
| 61 | if (ptr[1] == '.') { |
| 62 | /* it's the next interval */ |
| 63 | break; |
| 64 | } |
| 65 | ++str_dig; |
| 66 | } else { |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 67 | ret = ret * 10 + (ptr[0] - '0'); |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 68 | if (str_dig > -1) { |
| 69 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 70 | if (ptr[0] == '0') { |
| 71 | /* possibly trailing zero */ |
| 72 | trailing_zeros++; |
| 73 | } else { |
| 74 | trailing_zeros = 0; |
| 75 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 76 | } |
| 77 | ++str_exp; |
| 78 | } |
| 79 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 80 | if (str_dig == 0) { |
| 81 | /* no digits after '.' */ |
| 82 | return 1; |
| 83 | } else if (str_dig == -1) { |
| 84 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 85 | str_dig = 0; |
| 86 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 87 | /* remove trailing zeros */ |
| 88 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 89 | str_dig -= trailing_zeros; |
| 90 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 91 | ret = ret / dec_pow(trailing_zeros); |
| 92 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 93 | |
| 94 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 95 | if (str_dig < dig) { |
| 96 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 97 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 98 | } |
| 99 | ret *= dec_pow(dig - str_dig); |
| 100 | } |
| 101 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 102 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (minus) { |
| 106 | ret *= -1; |
| 107 | } |
| 108 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 109 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 110 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 111 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 115 | * @brief Parse an identifier. |
| 116 | * |
| 117 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 118 | * identifier = (ALPHA / "_") |
| 119 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 120 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 121 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 122 | * |
| 123 | * @return Number of characters successfully parsed. |
| 124 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 125 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 126 | parse_identifier(const char *id) |
| 127 | { |
| 128 | int parsed = 0; |
| 129 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 130 | assert(id); |
| 131 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 132 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 133 | return -parsed; |
| 134 | } |
| 135 | |
| 136 | ++parsed; |
| 137 | ++id; |
| 138 | |
| 139 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 140 | ++parsed; |
| 141 | ++id; |
| 142 | } |
| 143 | |
| 144 | return parsed; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @brief Parse a node-identifier. |
| 149 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 150 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 151 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 152 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 153 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 154 | * @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] | 155 | * @param[out] name Points to the node name. |
| 156 | * @param[out] nam_len Length of the node name. |
| 157 | * |
| 158 | * @return Number of characters successfully parsed, |
| 159 | * positive on success, negative on failure. |
| 160 | */ |
| 161 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 162 | 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] | 163 | { |
| 164 | int parsed = 0, ret; |
| 165 | |
| 166 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 167 | if (mod_name) { |
| 168 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 169 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 170 | if (mod_name_len) { |
| 171 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 172 | } |
| 173 | if (name) { |
| 174 | *name = NULL; |
| 175 | } |
| 176 | if (nam_len) { |
| 177 | *nam_len = 0; |
| 178 | } |
| 179 | |
| 180 | if ((ret = parse_identifier(id)) < 1) { |
| 181 | return ret; |
| 182 | } |
| 183 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 184 | if (mod_name) { |
| 185 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 186 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 187 | if (mod_name_len) { |
| 188 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | parsed += ret; |
| 192 | id += ret; |
| 193 | |
| 194 | /* there is prefix */ |
| 195 | if (id[0] == ':') { |
| 196 | ++parsed; |
| 197 | ++id; |
| 198 | |
| 199 | /* there isn't */ |
| 200 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 201 | if (name && mod_name) { |
| 202 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 203 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 204 | if (mod_name) { |
| 205 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 208 | if (nam_len && mod_name_len) { |
| 209 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 210 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 211 | if (mod_name_len) { |
| 212 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return parsed; |
| 216 | } |
| 217 | |
| 218 | /* identifier (node name) */ |
| 219 | if ((ret = parse_identifier(id)) < 1) { |
| 220 | return -parsed+ret; |
| 221 | } |
| 222 | |
| 223 | if (name) { |
| 224 | *name = id; |
| 225 | } |
| 226 | if (nam_len) { |
| 227 | *nam_len = ret; |
| 228 | } |
| 229 | |
| 230 | return parsed+ret; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @brief Parse a path-predicate (leafref). |
| 235 | * |
| 236 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 237 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 238 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 239 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 240 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 241 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 242 | * @param[out] name Points to the node name. |
| 243 | * @param[out] nam_len Length of the node name. |
| 244 | * @param[out] path_key_expr Points to the path-key-expr. |
| 245 | * @param[out] pke_len Length of the path-key-expr. |
| 246 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 247 | * |
| 248 | * @return Number of characters successfully parsed, |
| 249 | * positive on success, negative on failure. |
| 250 | */ |
| 251 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 252 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 253 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 254 | { |
| 255 | const char *ptr; |
| 256 | int parsed = 0, ret; |
| 257 | |
| 258 | assert(id); |
| 259 | if (prefix) { |
| 260 | *prefix = NULL; |
| 261 | } |
| 262 | if (pref_len) { |
| 263 | *pref_len = 0; |
| 264 | } |
| 265 | if (name) { |
| 266 | *name = NULL; |
| 267 | } |
| 268 | if (nam_len) { |
| 269 | *nam_len = 0; |
| 270 | } |
| 271 | if (path_key_expr) { |
| 272 | *path_key_expr = NULL; |
| 273 | } |
| 274 | if (pke_len) { |
| 275 | *pke_len = 0; |
| 276 | } |
| 277 | if (has_predicate) { |
| 278 | *has_predicate = 0; |
| 279 | } |
| 280 | |
| 281 | if (id[0] != '[') { |
| 282 | return -parsed; |
| 283 | } |
| 284 | |
| 285 | ++parsed; |
| 286 | ++id; |
| 287 | |
| 288 | while (isspace(id[0])) { |
| 289 | ++parsed; |
| 290 | ++id; |
| 291 | } |
| 292 | |
| 293 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 294 | return -parsed+ret; |
| 295 | } |
| 296 | |
| 297 | parsed += ret; |
| 298 | id += ret; |
| 299 | |
| 300 | while (isspace(id[0])) { |
| 301 | ++parsed; |
| 302 | ++id; |
| 303 | } |
| 304 | |
| 305 | if (id[0] != '=') { |
| 306 | return -parsed; |
| 307 | } |
| 308 | |
| 309 | ++parsed; |
| 310 | ++id; |
| 311 | |
| 312 | while (isspace(id[0])) { |
| 313 | ++parsed; |
| 314 | ++id; |
| 315 | } |
| 316 | |
| 317 | if ((ptr = strchr(id, ']')) == NULL) { |
| 318 | return -parsed; |
| 319 | } |
| 320 | |
| 321 | --ptr; |
| 322 | while (isspace(ptr[0])) { |
| 323 | --ptr; |
| 324 | } |
| 325 | ++ptr; |
| 326 | |
| 327 | ret = ptr-id; |
| 328 | if (path_key_expr) { |
| 329 | *path_key_expr = id; |
| 330 | } |
| 331 | if (pke_len) { |
| 332 | *pke_len = ret; |
| 333 | } |
| 334 | |
| 335 | parsed += ret; |
| 336 | id += ret; |
| 337 | |
| 338 | while (isspace(id[0])) { |
| 339 | ++parsed; |
| 340 | ++id; |
| 341 | } |
| 342 | |
| 343 | assert(id[0] == ']'); |
| 344 | |
| 345 | if (id[1] == '[') { |
| 346 | *has_predicate = 1; |
| 347 | } |
| 348 | |
| 349 | return parsed+1; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 354 | * the ".." and the first node-identifier, other calls parse a single |
| 355 | * node-identifier each. |
| 356 | * |
| 357 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 358 | * rel-path-keyexpr |
| 359 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 360 | * *(node-identifier *WSP "/" *WSP) |
| 361 | * node-identifier |
| 362 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 363 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 364 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 365 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 366 | * @param[out] name Points to the node name. |
| 367 | * @param[out] nam_len Length of the node name. |
| 368 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 369 | * must not be changed between consecutive calls. |
| 370 | * @return Number of characters successfully parsed, |
| 371 | * positive on success, negative on failure. |
| 372 | */ |
| 373 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 374 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 375 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 376 | { |
| 377 | int parsed = 0, ret, par_times = 0; |
| 378 | |
| 379 | assert(id); |
| 380 | assert(parent_times); |
| 381 | if (prefix) { |
| 382 | *prefix = NULL; |
| 383 | } |
| 384 | if (pref_len) { |
| 385 | *pref_len = 0; |
| 386 | } |
| 387 | if (name) { |
| 388 | *name = NULL; |
| 389 | } |
| 390 | if (nam_len) { |
| 391 | *nam_len = 0; |
| 392 | } |
| 393 | |
| 394 | if (!*parent_times) { |
| 395 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 396 | if (strncmp(id, "current()", 9)) { |
| 397 | return -parsed; |
| 398 | } |
| 399 | |
| 400 | parsed += 9; |
| 401 | id += 9; |
| 402 | |
| 403 | while (isspace(id[0])) { |
| 404 | ++parsed; |
| 405 | ++id; |
| 406 | } |
| 407 | |
| 408 | if (id[0] != '/') { |
| 409 | return -parsed; |
| 410 | } |
| 411 | |
| 412 | ++parsed; |
| 413 | ++id; |
| 414 | |
| 415 | while (isspace(id[0])) { |
| 416 | ++parsed; |
| 417 | ++id; |
| 418 | } |
| 419 | |
| 420 | /* rel-path-keyexpr */ |
| 421 | if (strncmp(id, "..", 2)) { |
| 422 | return -parsed; |
| 423 | } |
| 424 | ++par_times; |
| 425 | |
| 426 | parsed += 2; |
| 427 | id += 2; |
| 428 | |
| 429 | while (isspace(id[0])) { |
| 430 | ++parsed; |
| 431 | ++id; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 436 | * |
| 437 | * first parent reference with whitespaces already parsed |
| 438 | */ |
| 439 | if (id[0] != '/') { |
| 440 | return -parsed; |
| 441 | } |
| 442 | |
| 443 | ++parsed; |
| 444 | ++id; |
| 445 | |
| 446 | while (isspace(id[0])) { |
| 447 | ++parsed; |
| 448 | ++id; |
| 449 | } |
| 450 | |
| 451 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 452 | ++par_times; |
| 453 | |
| 454 | parsed += 2; |
| 455 | id += 2; |
| 456 | |
| 457 | while (isspace(id[0])) { |
| 458 | ++parsed; |
| 459 | ++id; |
| 460 | } |
| 461 | |
| 462 | if (id[0] != '/') { |
| 463 | return -parsed; |
| 464 | } |
| 465 | |
| 466 | ++parsed; |
| 467 | ++id; |
| 468 | |
| 469 | while (isspace(id[0])) { |
| 470 | ++parsed; |
| 471 | ++id; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (!*parent_times) { |
| 476 | *parent_times = par_times; |
| 477 | } |
| 478 | |
| 479 | /* all parent references must be parsed at this point */ |
| 480 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 481 | return -parsed+ret; |
| 482 | } |
| 483 | |
| 484 | parsed += ret; |
| 485 | id += ret; |
| 486 | |
| 487 | return parsed; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * @brief Parse path-arg (leafref). |
| 492 | * |
| 493 | * path-arg = absolute-path / relative-path |
| 494 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 495 | * relative-path = 1*(".." "/") descendant-path |
| 496 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 497 | * @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] | 498 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 499 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 500 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 501 | * @param[out] name Points to the node name. |
| 502 | * @param[out] nam_len Length of the node name. |
| 503 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 504 | * must not be changed between consecutive calls. -1 if the |
| 505 | * path is relative. |
| 506 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 507 | * |
| 508 | * @return Number of characters successfully parsed, |
| 509 | * positive on success, negative on failure. |
| 510 | */ |
| 511 | static int |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 512 | parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
| 513 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 514 | { |
| 515 | int parsed = 0, ret, par_times = 0; |
| 516 | |
| 517 | assert(id); |
| 518 | assert(parent_times); |
| 519 | if (prefix) { |
| 520 | *prefix = NULL; |
| 521 | } |
| 522 | if (pref_len) { |
| 523 | *pref_len = 0; |
| 524 | } |
| 525 | if (name) { |
| 526 | *name = NULL; |
| 527 | } |
| 528 | if (nam_len) { |
| 529 | *nam_len = 0; |
| 530 | } |
| 531 | if (has_predicate) { |
| 532 | *has_predicate = 0; |
| 533 | } |
| 534 | |
| 535 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 536 | ++par_times; |
| 537 | |
| 538 | parsed += 2; |
| 539 | id += 2; |
| 540 | |
| 541 | while (!strncmp(id, "/..", 3)) { |
| 542 | ++par_times; |
| 543 | |
| 544 | parsed += 3; |
| 545 | id += 3; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (!*parent_times) { |
| 550 | if (par_times) { |
| 551 | *parent_times = par_times; |
| 552 | } else { |
| 553 | *parent_times = -1; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (id[0] != '/') { |
| 558 | return -parsed; |
| 559 | } |
| 560 | |
| 561 | /* skip '/' */ |
| 562 | ++parsed; |
| 563 | ++id; |
| 564 | |
| 565 | /* node-identifier ([prefix:]identifier) */ |
| 566 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 567 | return -parsed-ret; |
| 568 | } |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 569 | if (!(*prefix)) { |
| 570 | /* actually we always need prefix even it is not specified */ |
| 571 | *prefix = lys_main_module(mod)->name; |
| 572 | *pref_len = strlen(*prefix); |
| 573 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 574 | |
| 575 | parsed += ret; |
| 576 | id += ret; |
| 577 | |
| 578 | /* there is no predicate */ |
| 579 | if ((id[0] == '/') || !id[0]) { |
| 580 | return parsed; |
| 581 | } else if (id[0] != '[') { |
| 582 | return -parsed; |
| 583 | } |
| 584 | |
| 585 | if (has_predicate) { |
| 586 | *has_predicate = 1; |
| 587 | } |
| 588 | |
| 589 | return parsed; |
| 590 | } |
| 591 | |
| 592 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 593 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 594 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 595 | * |
| 596 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 597 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 598 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 599 | * @param[out] model Points to the model name. |
| 600 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 601 | * @param[out] name Points to the node name. |
| 602 | * @param[out] nam_len Length of the node name. |
| 603 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 604 | * |
| 605 | * @return Number of characters successfully parsed, |
| 606 | * positive on success, negative on failure. |
| 607 | */ |
| 608 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 609 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 610 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 611 | { |
| 612 | int parsed = 0, ret; |
| 613 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 614 | if (has_predicate) { |
| 615 | *has_predicate = 0; |
| 616 | } |
| 617 | |
| 618 | if (id[0] != '/') { |
| 619 | return -parsed; |
| 620 | } |
| 621 | |
| 622 | ++parsed; |
| 623 | ++id; |
| 624 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 625 | if ((ret = parse_identifier(id)) < 1) { |
| 626 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 629 | *model = id; |
| 630 | *mod_len = ret; |
| 631 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 632 | parsed += ret; |
| 633 | id += ret; |
| 634 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 635 | if (id[0] != ':') { |
| 636 | return -parsed; |
| 637 | } |
| 638 | |
| 639 | ++parsed; |
| 640 | ++id; |
| 641 | |
| 642 | if ((ret = parse_identifier(id)) < 1) { |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | *name = id; |
| 647 | *nam_len = ret; |
| 648 | |
| 649 | parsed += ret; |
| 650 | id += ret; |
| 651 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 652 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 653 | *has_predicate = 1; |
| 654 | } |
| 655 | |
| 656 | return parsed; |
| 657 | } |
| 658 | |
| 659 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 660 | * @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] | 661 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 662 | * |
| 663 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 664 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 665 | * ((DQUOTE string DQUOTE) / |
| 666 | * (SQUOTE string SQUOTE)) |
| 667 | * pos = non-negative-integer-value |
| 668 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 669 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 670 | * @param[out] model Points to the model name. |
| 671 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 672 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 673 | * @param[out] nam_len Length of the node name. |
| 674 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 675 | * NULL if there is not any. |
| 676 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 677 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 678 | * |
| 679 | * @return Number of characters successfully parsed, |
| 680 | * positive on success, negative on failure. |
| 681 | */ |
| 682 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 683 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 684 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 685 | { |
| 686 | const char *ptr; |
| 687 | int parsed = 0, ret; |
| 688 | char quote; |
| 689 | |
| 690 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 691 | if (model) { |
| 692 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 693 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 694 | if (mod_len) { |
| 695 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 696 | } |
| 697 | if (name) { |
| 698 | *name = NULL; |
| 699 | } |
| 700 | if (nam_len) { |
| 701 | *nam_len = 0; |
| 702 | } |
| 703 | if (value) { |
| 704 | *value = NULL; |
| 705 | } |
| 706 | if (val_len) { |
| 707 | *val_len = 0; |
| 708 | } |
| 709 | if (has_predicate) { |
| 710 | *has_predicate = 0; |
| 711 | } |
| 712 | |
| 713 | if (id[0] != '[') { |
| 714 | return -parsed; |
| 715 | } |
| 716 | |
| 717 | ++parsed; |
| 718 | ++id; |
| 719 | |
| 720 | while (isspace(id[0])) { |
| 721 | ++parsed; |
| 722 | ++id; |
| 723 | } |
| 724 | |
| 725 | /* pos */ |
| 726 | if (isdigit(id[0])) { |
| 727 | if (name) { |
| 728 | *name = id; |
| 729 | } |
| 730 | |
| 731 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 732 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | while (isdigit(id[0])) { |
| 736 | ++parsed; |
| 737 | ++id; |
| 738 | } |
| 739 | |
| 740 | if (nam_len) { |
| 741 | *nam_len = id-(*name); |
| 742 | } |
| 743 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 744 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 745 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 746 | if (id[0] == '.') { |
| 747 | if (name) { |
| 748 | *name = id; |
| 749 | } |
| 750 | if (nam_len) { |
| 751 | *nam_len = 1; |
| 752 | } |
| 753 | |
| 754 | ++parsed; |
| 755 | ++id; |
| 756 | |
| 757 | } else { |
| 758 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
| 759 | return -parsed+ret; |
| 760 | } else if (model && !*model) { |
| 761 | return -parsed; |
| 762 | } |
| 763 | |
| 764 | parsed += ret; |
| 765 | id += ret; |
| 766 | } |
| 767 | |
| 768 | while (isspace(id[0])) { |
| 769 | ++parsed; |
| 770 | ++id; |
| 771 | } |
| 772 | |
| 773 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 774 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 775 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 776 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 777 | ++parsed; |
| 778 | ++id; |
| 779 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 780 | while (isspace(id[0])) { |
| 781 | ++parsed; |
| 782 | ++id; |
| 783 | } |
| 784 | |
| 785 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 786 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 787 | quote = id[0]; |
| 788 | |
| 789 | ++parsed; |
| 790 | ++id; |
| 791 | |
| 792 | if ((ptr = strchr(id, quote)) == NULL) { |
| 793 | return -parsed; |
| 794 | } |
| 795 | ret = ptr-id; |
| 796 | |
| 797 | if (value) { |
| 798 | *value = id; |
| 799 | } |
| 800 | if (val_len) { |
| 801 | *val_len = ret; |
| 802 | } |
| 803 | |
| 804 | parsed += ret+1; |
| 805 | id += ret+1; |
| 806 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 807 | return -parsed; |
| 808 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | while (isspace(id[0])) { |
| 812 | ++parsed; |
| 813 | ++id; |
| 814 | } |
| 815 | |
| 816 | if (id[0] != ']') { |
| 817 | return -parsed; |
| 818 | } |
| 819 | |
| 820 | ++parsed; |
| 821 | ++id; |
| 822 | |
| 823 | if ((id[0] == '[') && has_predicate) { |
| 824 | *has_predicate = 1; |
| 825 | } |
| 826 | |
| 827 | return parsed; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * @brief Parse schema-nodeid. |
| 832 | * |
| 833 | * schema-nodeid = absolute-schema-nodeid / |
| 834 | * descendant-schema-nodeid |
| 835 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 836 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 837 | * node-identifier |
| 838 | * absolute-schema-nodeid |
| 839 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 840 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 841 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 842 | * @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] | 843 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 844 | * @param[out] nam_len Length of the node name. |
| 845 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 846 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 847 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 848 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 849 | * |
| 850 | * @return Number of characters successfully parsed, |
| 851 | * positive on success, negative on failure. |
| 852 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 853 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 854 | 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] | 855 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 856 | { |
| 857 | int parsed = 0, ret; |
| 858 | |
| 859 | assert(id); |
| 860 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 861 | if (mod_name) { |
| 862 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 863 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 864 | if (mod_name_len) { |
| 865 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 866 | } |
| 867 | if (name) { |
| 868 | *name = NULL; |
| 869 | } |
| 870 | if (nam_len) { |
| 871 | *nam_len = 0; |
| 872 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 873 | if (has_predicate) { |
| 874 | *has_predicate = 0; |
| 875 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 876 | |
| 877 | if (id[0] != '/') { |
| 878 | if (*is_relative != -1) { |
| 879 | return -parsed; |
| 880 | } else { |
| 881 | *is_relative = 1; |
| 882 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 883 | if (!strncmp(id, "./", 2)) { |
| 884 | parsed += 2; |
| 885 | id += 2; |
| 886 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 887 | } else { |
| 888 | if (*is_relative == -1) { |
| 889 | *is_relative = 0; |
| 890 | } |
| 891 | ++parsed; |
| 892 | ++id; |
| 893 | } |
| 894 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 895 | 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] | 896 | return -parsed+ret; |
| 897 | } |
| 898 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 899 | parsed += ret; |
| 900 | id += ret; |
| 901 | |
| 902 | if ((id[0] == '[') && has_predicate) { |
| 903 | *has_predicate = 1; |
| 904 | } |
| 905 | |
| 906 | return parsed; |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * @brief Parse schema predicate (special format internally used). |
| 911 | * |
| 912 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 913 | * predicate-expr = "." / identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 914 | * key-with-value = identifier *WSP "=" *WSP |
| 915 | * ((DQUOTE string DQUOTE) / |
| 916 | * (SQUOTE string SQUOTE)) |
| 917 | * |
| 918 | * @param[in] id Identifier to use. |
| 919 | * @param[out] name Points to the list key name. |
| 920 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 921 | * @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] | 922 | * @param[out] val_len Length of \p value. |
| 923 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 924 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 925 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 926 | parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 927 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 928 | { |
| 929 | const char *ptr; |
| 930 | int parsed = 0, ret; |
| 931 | char quote; |
| 932 | |
| 933 | assert(id); |
| 934 | if (name) { |
| 935 | *name = NULL; |
| 936 | } |
| 937 | if (nam_len) { |
| 938 | *nam_len = 0; |
| 939 | } |
| 940 | if (value) { |
| 941 | *value = NULL; |
| 942 | } |
| 943 | if (val_len) { |
| 944 | *val_len = 0; |
| 945 | } |
| 946 | if (has_predicate) { |
| 947 | *has_predicate = 0; |
| 948 | } |
| 949 | |
| 950 | if (id[0] != '[') { |
| 951 | return -parsed; |
| 952 | } |
| 953 | |
| 954 | ++parsed; |
| 955 | ++id; |
| 956 | |
| 957 | while (isspace(id[0])) { |
| 958 | ++parsed; |
| 959 | ++id; |
| 960 | } |
| 961 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 962 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 963 | if (id[0] == '.') { |
| 964 | ret = 1; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 965 | } else if (isdigit(id[0])) { |
| 966 | if (id[0] == '0') { |
| 967 | return -parsed; |
| 968 | } |
| 969 | ret = 1; |
| 970 | while (isdigit(id[ret])) { |
| 971 | ++ret; |
| 972 | } |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 973 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 974 | return -parsed + ret; |
| 975 | } |
| 976 | if (name) { |
| 977 | *name = id; |
| 978 | } |
| 979 | if (nam_len) { |
| 980 | *nam_len = ret; |
| 981 | } |
| 982 | |
| 983 | parsed += ret; |
| 984 | id += ret; |
| 985 | |
| 986 | while (isspace(id[0])) { |
| 987 | ++parsed; |
| 988 | ++id; |
| 989 | } |
| 990 | |
| 991 | /* there is value as well */ |
| 992 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 993 | if (name && isdigit(**name)) { |
| 994 | return -parsed; |
| 995 | } |
| 996 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 997 | ++parsed; |
| 998 | ++id; |
| 999 | |
| 1000 | while (isspace(id[0])) { |
| 1001 | ++parsed; |
| 1002 | ++id; |
| 1003 | } |
| 1004 | |
| 1005 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1006 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1007 | quote = id[0]; |
| 1008 | |
| 1009 | ++parsed; |
| 1010 | ++id; |
| 1011 | |
| 1012 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1013 | return -parsed; |
| 1014 | } |
| 1015 | ret = ptr - id; |
| 1016 | |
| 1017 | if (value) { |
| 1018 | *value = id; |
| 1019 | } |
| 1020 | if (val_len) { |
| 1021 | *val_len = ret; |
| 1022 | } |
| 1023 | |
| 1024 | parsed += ret + 1; |
| 1025 | id += ret + 1; |
| 1026 | } else { |
| 1027 | return -parsed; |
| 1028 | } |
| 1029 | |
| 1030 | while (isspace(id[0])) { |
| 1031 | ++parsed; |
| 1032 | ++id; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | if (id[0] != ']') { |
| 1037 | return -parsed; |
| 1038 | } |
| 1039 | |
| 1040 | ++parsed; |
| 1041 | ++id; |
| 1042 | |
| 1043 | if ((id[0] == '[') && has_predicate) { |
| 1044 | *has_predicate = 1; |
| 1045 | } |
| 1046 | |
| 1047 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1051 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1052 | * |
| 1053 | * @param[in] feat_name Feature name to resolve. |
| 1054 | * @param[in] len Length of \p feat_name. |
| 1055 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1056 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1057 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1058 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1059 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1060 | */ |
| 1061 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1062 | 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] | 1063 | { |
| 1064 | char *str; |
| 1065 | const char *mod_name, *name; |
| 1066 | int mod_name_len, nam_len, i, j; |
| 1067 | const struct lys_module *module; |
| 1068 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1069 | assert(feature); |
| 1070 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1071 | /* check prefix */ |
| 1072 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
| 1073 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | |
| 1077 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1078 | if (!module) { |
| 1079 | /* identity refers unknown data model */ |
| 1080 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1084 | if (module != node->module && module == lys_node_module(node)) { |
| 1085 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1086 | for (j = 0; j < node->module->features_size; j++) { |
| 1087 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1088 | /* check status */ |
| 1089 | 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] | 1090 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1091 | return -1; |
| 1092 | } |
| 1093 | *feature = &node->module->features[j]; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1099 | /* search in the identified module ... */ |
| 1100 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1101 | 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] | 1102 | /* check status */ |
| 1103 | 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] | 1104 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1105 | return -1; |
| 1106 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1107 | *feature = &module->features[j]; |
| 1108 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | /* ... and all its submodules */ |
| 1112 | for (i = 0; i < module->inc_size; i++) { |
| 1113 | if (!module->inc[i].submodule) { |
| 1114 | /* not yet resolved */ |
| 1115 | continue; |
| 1116 | } |
| 1117 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1118 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1119 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1120 | /* check status */ |
| 1121 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1122 | module->inc[i].submodule->features[j].flags, |
| 1123 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1124 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1125 | return -1; |
| 1126 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1127 | *feature = &module->inc[i].submodule->features[j]; |
| 1128 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /* not found */ |
| 1134 | str = strndup(feat_name, len); |
| 1135 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1136 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1137 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1140 | /* |
| 1141 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1142 | * - 1 if enabled |
| 1143 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1144 | * - -1 if not usable by its if-feature expression |
| 1145 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1146 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1147 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1148 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1149 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1150 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1151 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1152 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1153 | return -1; |
| 1154 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1155 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1156 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1157 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1161 | 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] | 1162 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1163 | uint8_t op; |
| 1164 | int rc, a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1165 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1166 | op = iff_getop(expr->expr, *index_e); |
| 1167 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1168 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1169 | switch (op) { |
| 1170 | case LYS_IFF_F: |
| 1171 | /* resolve feature */ |
| 1172 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1173 | case LYS_IFF_NOT: |
| 1174 | rc = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1175 | if (rc == -1) { |
| 1176 | /* one of the referenced feature is hidden by its if-feature, |
| 1177 | * so this if-feature expression is always false */ |
| 1178 | return -1; |
| 1179 | } else { |
| 1180 | /* invert result */ |
| 1181 | return rc ? 0 : 1; |
| 1182 | } |
| 1183 | case LYS_IFF_AND: |
| 1184 | case LYS_IFF_OR: |
| 1185 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1186 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1187 | if (a == -1 || b == -1) { |
| 1188 | /* one of the referenced feature is hidden by its if-feature, |
| 1189 | * so this if-feature expression is always false */ |
| 1190 | return -1; |
| 1191 | } else if (op == LYS_IFF_AND) { |
| 1192 | return a && b; |
| 1193 | } else { /* LYS_IFF_OR */ |
| 1194 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | int |
| 1202 | resolve_iffeature(struct lys_iffeature *expr) |
| 1203 | { |
| 1204 | int rc = -1; |
| 1205 | int index_e = 0, index_f = 0; |
| 1206 | |
| 1207 | if (expr->expr) { |
| 1208 | rc = resolve_iffeature_recursive(expr, &index_e, &index_f); |
| 1209 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1210 | return (rc == 1) ? 1 : 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); |
| 1225 | if (!stack->stack) { |
| 1226 | LOGMEM; |
| 1227 | stack->size = 0; |
| 1228 | return EXIT_FAILURE; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | stack->stack[stack->index++] = value; |
| 1233 | return EXIT_SUCCESS; |
| 1234 | } |
| 1235 | |
| 1236 | static uint8_t |
| 1237 | iff_stack_pop(struct iff_stack *stack) |
| 1238 | { |
| 1239 | stack->index--; |
| 1240 | return stack->stack[stack->index]; |
| 1241 | } |
| 1242 | |
| 1243 | static void |
| 1244 | iff_stack_clean(struct iff_stack *stack) |
| 1245 | { |
| 1246 | stack->size = 0; |
| 1247 | free(stack->stack); |
| 1248 | } |
| 1249 | |
| 1250 | static void |
| 1251 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1252 | { |
| 1253 | uint8_t *item; |
| 1254 | uint8_t mask = 3; |
| 1255 | |
| 1256 | assert(pos >= 0); |
| 1257 | assert(op <= 3); /* max 2 bits */ |
| 1258 | |
| 1259 | item = &list[pos / 4]; |
| 1260 | mask = mask << 2 * (pos % 4); |
| 1261 | *item = (*item) & ~mask; |
| 1262 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1263 | } |
| 1264 | |
| 1265 | uint8_t |
| 1266 | iff_getop(uint8_t *list, int pos) |
| 1267 | { |
| 1268 | uint8_t *item; |
| 1269 | uint8_t mask = 3, result; |
| 1270 | |
| 1271 | assert(pos >= 0); |
| 1272 | |
| 1273 | item = &list[pos / 4]; |
| 1274 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1275 | return result >> 2 * (pos % 4); |
| 1276 | } |
| 1277 | |
| 1278 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1279 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1280 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1281 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1282 | struct unres_iffeat_data { |
| 1283 | struct lys_node *node; |
| 1284 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1285 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1286 | }; |
| 1287 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1288 | void |
| 1289 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1290 | { |
| 1291 | unsigned int e = 0, f = 0, r = 0; |
| 1292 | uint8_t op; |
| 1293 | |
| 1294 | assert(iffeat); |
| 1295 | |
| 1296 | if (!iffeat->expr) { |
| 1297 | goto result; |
| 1298 | } |
| 1299 | |
| 1300 | do { |
| 1301 | op = iff_getop(iffeat->expr, e++); |
| 1302 | switch (op) { |
| 1303 | case LYS_IFF_NOT: |
| 1304 | if (!r) { |
| 1305 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1306 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1307 | break; |
| 1308 | case LYS_IFF_AND: |
| 1309 | case LYS_IFF_OR: |
| 1310 | if (!r) { |
| 1311 | r += 2; |
| 1312 | } else { |
| 1313 | r += 1; |
| 1314 | } |
| 1315 | break; |
| 1316 | case LYS_IFF_F: |
| 1317 | f++; |
| 1318 | if (r) { |
| 1319 | r--; |
| 1320 | } |
| 1321 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1322 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1323 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1324 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1325 | result: |
| 1326 | if (expr_size) { |
| 1327 | *expr_size = e; |
| 1328 | } |
| 1329 | if (feat_size) { |
| 1330 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1335 | 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] | 1336 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1337 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1338 | const char *c = value; |
| 1339 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1340 | int i, j, last_not, checkversion = 0; |
| 1341 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1342 | uint8_t op; |
| 1343 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1344 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1345 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1346 | assert(c); |
| 1347 | |
| 1348 | if (isspace(c[0])) { |
| 1349 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1350 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1353 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1354 | for (i = j = last_not = 0; c[i]; i++) { |
| 1355 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1356 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1357 | j++; |
| 1358 | continue; |
| 1359 | } else if (c[i] == ')') { |
| 1360 | j--; |
| 1361 | continue; |
| 1362 | } else if (isspace(c[i])) { |
| 1363 | continue; |
| 1364 | } |
| 1365 | |
| 1366 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1367 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1368 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1369 | return EXIT_FAILURE; |
| 1370 | } else if (!isspace(c[i + r])) { |
| 1371 | /* feature name starting with the not/and/or */ |
| 1372 | last_not = 0; |
| 1373 | f_size++; |
| 1374 | } else if (c[i] == 'n') { /* not operation */ |
| 1375 | if (last_not) { |
| 1376 | /* double not */ |
| 1377 | expr_size = expr_size - 2; |
| 1378 | last_not = 0; |
| 1379 | } else { |
| 1380 | last_not = 1; |
| 1381 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1382 | } else { /* and, or */ |
| 1383 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1384 | /* not a not operation */ |
| 1385 | last_not = 0; |
| 1386 | } |
| 1387 | i += r; |
| 1388 | } else { |
| 1389 | f_size++; |
| 1390 | last_not = 0; |
| 1391 | } |
| 1392 | expr_size++; |
| 1393 | |
| 1394 | while (!isspace(c[i])) { |
| 1395 | if (!c[i] || c[i] == ')') { |
| 1396 | i--; |
| 1397 | break; |
| 1398 | } |
| 1399 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1400 | } |
| 1401 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1402 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1403 | /* not matching count of ( and ) */ |
| 1404 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1405 | return EXIT_FAILURE; |
| 1406 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1407 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1408 | if (checkversion || expr_size > 1) { |
| 1409 | /* check that we have 1.1 module */ |
| 1410 | if (node->module->version != 2) { |
| 1411 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1412 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1413 | return EXIT_FAILURE; |
| 1414 | } |
| 1415 | } |
| 1416 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1417 | /* allocate the memory */ |
| 1418 | 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] | 1419 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1420 | stack.size = expr_size; |
| 1421 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 1422 | if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) { |
| 1423 | LOGMEM; |
| 1424 | goto error; |
| 1425 | } |
| 1426 | f_size--; expr_size--; /* used as indexes from now */ |
| 1427 | |
| 1428 | for (i--; i >= 0; i--) { |
| 1429 | if (c[i] == ')') { |
| 1430 | /* push it on stack */ |
| 1431 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1432 | continue; |
| 1433 | } else if (c[i] == '(') { |
| 1434 | /* pop from the stack into result all operators until ) */ |
| 1435 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1436 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1437 | } |
| 1438 | continue; |
| 1439 | } else if (isspace(c[i])) { |
| 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | /* end operator or operand -> find beginning and get what is it */ |
| 1444 | j = i + 1; |
| 1445 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1446 | i--; |
| 1447 | } |
| 1448 | i++; /* get back by one step */ |
| 1449 | |
| 1450 | if (!strncmp(&c[i], "not ", 4)) { |
| 1451 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1452 | /* double not */ |
| 1453 | iff_stack_pop(&stack); |
| 1454 | } else { |
| 1455 | /* not has the highest priority, so do not pop from the stack |
| 1456 | * as in case of AND and OR */ |
| 1457 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1458 | } |
| 1459 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1460 | /* as for OR - pop from the stack all operators with the same or higher |
| 1461 | * priority and store them to the result, then push the AND to the stack */ |
| 1462 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1463 | op = iff_stack_pop(&stack); |
| 1464 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1465 | } |
| 1466 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1467 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1468 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1469 | op = iff_stack_pop(&stack); |
| 1470 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1471 | } |
| 1472 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1473 | } else { |
| 1474 | /* feature name, length is j - i */ |
| 1475 | |
| 1476 | /* add it to the result */ |
| 1477 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1478 | |
| 1479 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1480 | * forward referenced, we have to keep the feature name in auxiliary |
| 1481 | * structure passed into unres */ |
| 1482 | iff_data = malloc(sizeof *iff_data); |
| 1483 | iff_data->node = node; |
| 1484 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1485 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1486 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1487 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1488 | f_size--; |
| 1489 | |
| 1490 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1491 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1492 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1493 | } |
| 1494 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1495 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1496 | while (stack.index) { |
| 1497 | op = iff_stack_pop(&stack); |
| 1498 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1499 | } |
| 1500 | |
| 1501 | if (++expr_size || ++f_size) { |
| 1502 | /* not all expected operators and operands found */ |
| 1503 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1504 | rc = EXIT_FAILURE; |
| 1505 | } else { |
| 1506 | rc = EXIT_SUCCESS; |
| 1507 | } |
| 1508 | |
| 1509 | error: |
| 1510 | /* cleanup */ |
| 1511 | iff_stack_clean(&stack); |
| 1512 | |
| 1513 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1517 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1518 | * |
| 1519 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1520 | * module). |
| 1521 | * |
| 1522 | */ |
| 1523 | struct lyd_node * |
| 1524 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1525 | { |
| 1526 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1527 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1528 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1529 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1530 | |
| 1531 | assert(nodeid && start); |
| 1532 | |
| 1533 | if (nodeid[0] == '/') { |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
| 1537 | str = p = strdup(nodeid); |
| 1538 | if (!str) { |
| 1539 | LOGMEM; |
| 1540 | return NULL; |
| 1541 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1542 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1543 | while (p) { |
| 1544 | token = p; |
| 1545 | p = strchr(p, '/'); |
| 1546 | if (p) { |
| 1547 | *p = '\0'; |
| 1548 | p++; |
| 1549 | } |
| 1550 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1551 | if (p) { |
| 1552 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1553 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1554 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1555 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1556 | result = NULL; |
| 1557 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1558 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1559 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1560 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1561 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1562 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1563 | /* shorthand case */ |
| 1564 | if (!shorthand) { |
| 1565 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1566 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1567 | continue; |
| 1568 | } else { |
| 1569 | shorthand = 0; |
| 1570 | if (schema->nodetype == LYS_LEAF) { |
| 1571 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1572 | result = NULL; |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1576 | } |
| 1577 | } else { |
| 1578 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1579 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1580 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1581 | || !schema) { |
| 1582 | result = NULL; |
| 1583 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1584 | } |
| 1585 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1586 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1587 | if (iter->schema == schema) { |
| 1588 | /* move in data tree according to returned schema */ |
| 1589 | result = iter; |
| 1590 | break; |
| 1591 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1592 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1593 | if (!iter) { |
| 1594 | /* instance not found */ |
| 1595 | result = NULL; |
| 1596 | break; |
| 1597 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1598 | } |
| 1599 | free(str); |
| 1600 | |
| 1601 | return result; |
| 1602 | } |
| 1603 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1604 | /* |
| 1605 | * 0 - ok (done) |
| 1606 | * 1 - continue |
| 1607 | * 2 - break |
| 1608 | * -1 - error |
| 1609 | */ |
| 1610 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1611 | schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id, |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1612 | const struct lys_module *module, const char *mod_name, int mod_name_len, |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1613 | int implemented_mod, const struct lys_node **start) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1614 | { |
| 1615 | const struct lys_module *prefix_mod; |
| 1616 | |
| 1617 | /* module check */ |
| 1618 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1619 | if (prefix_mod && implemented_mod) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1620 | prefix_mod = lys_implemented_module(prefix_mod); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1621 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1622 | if (!prefix_mod) { |
| 1623 | return -1; |
| 1624 | } |
| 1625 | if (prefix_mod != lys_node_module(sibling)) { |
| 1626 | return 1; |
| 1627 | } |
| 1628 | |
| 1629 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1630 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1631 | if (*shorthand != -1) { |
| 1632 | *shorthand = *shorthand ? 0 : 1; |
| 1633 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /* the result node? */ |
| 1637 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1638 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1639 | return 1; |
| 1640 | } |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Radek Krejci | 3a13016 | 2016-11-07 16:21:20 +0100 | [diff] [blame] | 1644 | if (!(*shorthand)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1645 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1646 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1647 | return -1; |
| 1648 | } |
| 1649 | *start = sibling->child; |
| 1650 | } |
| 1651 | |
| 1652 | return 2; |
| 1653 | } |
| 1654 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1655 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 |
| 1656 | * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules |
| 1657 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1658 | int |
| 1659 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1660 | int implement, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1661 | { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1662 | const char *name, *mod_name, *mod_name_prev, *id; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1663 | const struct lys_node *sibling; |
| 1664 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1665 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1666 | /* 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] | 1667 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1668 | |
| 1669 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1670 | |
| 1671 | id = nodeid; |
| 1672 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1673 | 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] | 1674 | return ((id - nodeid) - r) + 1; |
| 1675 | } |
| 1676 | id += r; |
| 1677 | |
| 1678 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1679 | return -1; |
| 1680 | } |
| 1681 | |
| 1682 | /* descendant-schema-nodeid */ |
| 1683 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1684 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1685 | |
| 1686 | /* absolute-schema-nodeid */ |
| 1687 | } else { |
| 1688 | start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1689 | if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1690 | /* if the submodule augments the mainmodule (or in general a module augments |
| 1691 | * itself, we don't want to search for the implemented module but augments |
| 1692 | * the module anyway. But when augmenting another module, we need the implemented |
| 1693 | * revision of the module if any */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1694 | aux_mod = lys_implemented_module(start_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1695 | if (!aux_mod->implemented && implement) { |
| 1696 | /* make the found module implemented */ |
| 1697 | if (lys_set_implemented(aux_mod)) { |
| 1698 | return -1; |
| 1699 | } |
| 1700 | } |
| 1701 | start_mod = aux_mod; |
| 1702 | implement++; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1703 | } |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1704 | if (!start_mod) { |
| 1705 | return -1; |
| 1706 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1707 | start = start_mod->data; |
| 1708 | } |
| 1709 | |
| 1710 | while (1) { |
| 1711 | sibling = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1712 | mod_name_prev = mod_name; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1713 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1714 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1715 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1716 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1717 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1718 | implement, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1719 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1720 | *ret = sibling; |
| 1721 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1722 | } else if (r == 1) { |
| 1723 | continue; |
| 1724 | } else if (r == 2) { |
| 1725 | break; |
| 1726 | } else { |
| 1727 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1728 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | /* no match */ |
| 1733 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1734 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1735 | return EXIT_SUCCESS; |
| 1736 | } |
| 1737 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1738 | 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] | 1739 | return ((id - nodeid) - r) + 1; |
| 1740 | } |
| 1741 | id += r; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1742 | |
| 1743 | if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) || |
| 1744 | (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) { |
| 1745 | /* we are getting into another module (augment) */ |
| 1746 | if (implement) { |
| 1747 | /* we have to check that also target modules are implemented, if not, we have to change it */ |
| 1748 | aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1749 | if (!aux_mod) { |
| 1750 | return -1; |
| 1751 | } |
| 1752 | if (!aux_mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 1753 | aux_mod = lys_implemented_module(aux_mod); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1754 | if (!aux_mod->implemented) { |
| 1755 | /* make the found module implemented */ |
| 1756 | if (lys_set_implemented(aux_mod)) { |
| 1757 | return -1; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | } else { |
| 1762 | /* we are not implementing the module itself, so the augments outside the module are ignored */ |
| 1763 | *ret = NULL; |
| 1764 | return EXIT_SUCCESS; |
| 1765 | } |
| 1766 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | /* cannot get here */ |
| 1770 | LOGINT; |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1774 | /* unique, refine, |
| 1775 | * >0 - unexpected char on position (ret - 1), |
| 1776 | * 0 - ok (but ret can still be NULL), |
| 1777 | * -1 - error, |
| 1778 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | int |
| 1780 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1781 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1782 | { |
| 1783 | const char *name, *mod_name, *id; |
| 1784 | const struct lys_node *sibling; |
| 1785 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1786 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1787 | /* 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] | 1788 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1789 | |
| 1790 | assert(nodeid && start && ret); |
| 1791 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1792 | |
| 1793 | id = nodeid; |
| 1794 | module = start->module; |
| 1795 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1796 | 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] | 1797 | return ((id - nodeid) - r) + 1; |
| 1798 | } |
| 1799 | id += r; |
| 1800 | |
| 1801 | if (!is_relative) { |
| 1802 | return -1; |
| 1803 | } |
| 1804 | |
| 1805 | while (1) { |
| 1806 | sibling = NULL; |
| 1807 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1808 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1809 | /* name match */ |
| 1810 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1811 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1812 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1813 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1814 | /* wrong node type, too bad */ |
| 1815 | continue; |
| 1816 | } |
| 1817 | *ret = sibling; |
| 1818 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1819 | } else if (r == 1) { |
| 1820 | continue; |
| 1821 | } else if (r == 2) { |
| 1822 | break; |
| 1823 | } else { |
| 1824 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1825 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* no match */ |
| 1830 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1831 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1832 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1833 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1834 | *ret = NULL; |
| 1835 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1838 | 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] | 1839 | return ((id - nodeid) - r) + 1; |
| 1840 | } |
| 1841 | id += r; |
| 1842 | } |
| 1843 | |
| 1844 | /* cannot get here */ |
| 1845 | LOGINT; |
| 1846 | return -1; |
| 1847 | } |
| 1848 | |
| 1849 | /* choice default */ |
| 1850 | int |
| 1851 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1852 | { |
| 1853 | /* cannot actually be a path */ |
| 1854 | if (strchr(nodeid, '/')) { |
| 1855 | return -1; |
| 1856 | } |
| 1857 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1858 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1862 | static int |
| 1863 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1864 | { |
| 1865 | const struct lys_module *module; |
| 1866 | const char *mod_prefix, *name; |
| 1867 | int i, mod_prefix_len, nam_len; |
| 1868 | |
| 1869 | /* parse the identifier, it must be parsed on one call */ |
| 1870 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1871 | return -i + 1; |
| 1872 | } |
| 1873 | |
| 1874 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1875 | if (!module) { |
| 1876 | return -1; |
| 1877 | } |
| 1878 | if (module != start->module) { |
| 1879 | start = module->data; |
| 1880 | } |
| 1881 | |
| 1882 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1883 | |
| 1884 | return EXIT_SUCCESS; |
| 1885 | } |
| 1886 | |
| 1887 | int |
| 1888 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1889 | const struct lys_node **ret) |
| 1890 | { |
| 1891 | const char *name, *mod_name, *id; |
| 1892 | const struct lys_node *sibling, *start; |
| 1893 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1894 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1895 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1896 | |
| 1897 | assert(nodeid && module && ret); |
| 1898 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1899 | |
| 1900 | id = nodeid; |
| 1901 | start = module->data; |
| 1902 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1903 | 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] | 1904 | return ((id - nodeid) - r) + 1; |
| 1905 | } |
| 1906 | id += r; |
| 1907 | |
| 1908 | if (is_relative) { |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | 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] | 1913 | if (!abs_start_mod) { |
| 1914 | return -1; |
| 1915 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1916 | |
| 1917 | while (1) { |
| 1918 | sibling = NULL; |
| 1919 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1920 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1921 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1922 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1923 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start); |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1924 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1925 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1926 | /* wrong node type, too bad */ |
| 1927 | continue; |
| 1928 | } |
| 1929 | *ret = sibling; |
| 1930 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1931 | } else if (r == 1) { |
| 1932 | continue; |
| 1933 | } else if (r == 2) { |
| 1934 | break; |
| 1935 | } else { |
| 1936 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1937 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | /* no match */ |
| 1942 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1943 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1944 | return EXIT_SUCCESS; |
| 1945 | } |
| 1946 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1947 | 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] | 1948 | return ((id - nodeid) - r) + 1; |
| 1949 | } |
| 1950 | id += r; |
| 1951 | } |
| 1952 | |
| 1953 | /* cannot get here */ |
| 1954 | LOGINT; |
| 1955 | return -1; |
| 1956 | } |
| 1957 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1958 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1959 | resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed) |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1960 | { |
| 1961 | const char *name; |
| 1962 | int nam_len, has_predicate, i; |
| 1963 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1964 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1965 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1966 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1967 | return -1; |
| 1968 | } |
| 1969 | |
| 1970 | predicate += i; |
| 1971 | *parsed += i; |
| 1972 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1973 | if (!isdigit(name[0])) { |
| 1974 | for (i = 0; i < list->keys_size; ++i) { |
| 1975 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1976 | break; |
| 1977 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1978 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1979 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1980 | if (i == list->keys_size) { |
| 1981 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 1982 | return -1; |
| 1983 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* more predicates? */ |
| 1987 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1988 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1994 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1995 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1996 | 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] | 1997 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1998 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1999 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2000 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2001 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2002 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2003 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2004 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2005 | assert(nodeid && (ctx || start)); |
| 2006 | if (!ctx) { |
| 2007 | ctx = start->module->ctx; |
| 2008 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2009 | |
| 2010 | id = nodeid; |
| 2011 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2012 | 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] | 2013 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2014 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2015 | } |
| 2016 | id += r; |
| 2017 | |
| 2018 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2019 | assert(start); |
| 2020 | start = start->child; |
| 2021 | if (!start) { |
| 2022 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2023 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2024 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2025 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2026 | return NULL; |
| 2027 | } |
| 2028 | module = start->module; |
| 2029 | } else { |
| 2030 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2031 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2032 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2033 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2034 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2035 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2036 | LOGINT; |
| 2037 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2040 | if (ly_buf_used && module_name[0]) { |
| 2041 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2042 | } |
| 2043 | ly_buf_used++; |
| 2044 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2045 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2046 | module_name[mod_name_len] = '\0'; |
| 2047 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 2048 | |
| 2049 | if (buf_backup) { |
| 2050 | /* return previous internal buffer content */ |
| 2051 | strcpy(module_name, buf_backup); |
| 2052 | free(buf_backup); |
| 2053 | buf_backup = NULL; |
| 2054 | } |
| 2055 | ly_buf_used--; |
| 2056 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2057 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2058 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2059 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2060 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2061 | return NULL; |
| 2062 | } |
| 2063 | start = module->data; |
| 2064 | |
| 2065 | /* now it's as if there was no module name */ |
| 2066 | mod_name = NULL; |
| 2067 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2070 | prev_mod = module; |
| 2071 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2072 | while (1) { |
| 2073 | sibling = NULL; |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2074 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 2075 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2076 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2077 | 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] | 2078 | /* module check */ |
| 2079 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2080 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2081 | LOGINT; |
| 2082 | return NULL; |
| 2083 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2084 | |
| 2085 | if (ly_buf_used && module_name[0]) { |
| 2086 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2087 | } |
| 2088 | ly_buf_used++; |
| 2089 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2090 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2091 | module_name[mod_name_len] = '\0'; |
| 2092 | /* will also find an augment module */ |
| 2093 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2094 | |
| 2095 | if (buf_backup) { |
| 2096 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2097 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2098 | free(buf_backup); |
| 2099 | buf_backup = NULL; |
| 2100 | } |
| 2101 | ly_buf_used--; |
| 2102 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2103 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2104 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2105 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2106 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2107 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2108 | } |
| 2109 | } else { |
| 2110 | prefix_mod = prev_mod; |
| 2111 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2112 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2113 | continue; |
| 2114 | } |
| 2115 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2116 | /* do we have some predicates on it? */ |
| 2117 | if (has_predicate) { |
| 2118 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2119 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 2120 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 2121 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2122 | return NULL; |
| 2123 | } |
| 2124 | } else if (sibling->nodetype == LYS_LIST) { |
| 2125 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2129 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2130 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2131 | } |
| 2132 | id += r; |
| 2133 | } |
| 2134 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2135 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2136 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2137 | shorthand = ~shorthand; |
| 2138 | } |
| 2139 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2140 | /* the result node? */ |
| 2141 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2142 | if (shorthand) { |
| 2143 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2144 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2145 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 2146 | LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement."); |
Radek Krejci | 9a5fccc | 2016-05-18 15:44:58 +0200 | [diff] [blame] | 2147 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 2148 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2149 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2150 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2153 | if (!shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2154 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2155 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2156 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 2157 | return NULL; |
| 2158 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2159 | start = sibling->child; |
| 2160 | } |
| 2161 | |
| 2162 | /* update prev mod */ |
| 2163 | prev_mod = start->module; |
| 2164 | break; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | /* no match */ |
| 2169 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2170 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2171 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2172 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2173 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2176 | 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] | 2177 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2178 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2179 | } |
| 2180 | id += r; |
| 2181 | } |
| 2182 | |
| 2183 | /* cannot get here */ |
| 2184 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2185 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2186 | } |
| 2187 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2188 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2189 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
| 2190 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2191 | { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2192 | const char *name, *value, *key_val; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2193 | int nam_len, val_len, has_predicate = 1, r; |
| 2194 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2195 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2196 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2197 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2198 | assert(node->schema->nodetype == LYS_LIST); |
| 2199 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2200 | key = (struct lyd_node_leaf_list *)node->child; |
| 2201 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2202 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2203 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2204 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2207 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 2208 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2209 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2210 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | predicate += r; |
| 2214 | *parsed += r; |
| 2215 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2216 | if (isdigit(name[0])) { |
| 2217 | if (position == atoi(name)) { |
| 2218 | /* match */ |
| 2219 | break; |
| 2220 | } else { |
| 2221 | /* not a match */ |
| 2222 | return 1; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | if (!key) { |
| 2227 | /* it is not a position, so we need to key for it to be a match */ |
| 2228 | return 1; |
| 2229 | } |
| 2230 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2231 | 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] | 2232 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2233 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2236 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2237 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2238 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2239 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2240 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2241 | } else { |
| 2242 | key_val = key->value_str; |
| 2243 | } |
| 2244 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2245 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2246 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2247 | return 1; |
| 2248 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2249 | |
| 2250 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2254 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2255 | return -1; |
| 2256 | } |
| 2257 | |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2261 | /** |
| 2262 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2263 | * |
| 2264 | * @param[in] nodeid Node data path to find |
| 2265 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2266 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2267 | * @param[out] parsed Number of characters processed in \p id |
| 2268 | * @return The closes parent (or the node itself) from the path |
| 2269 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2270 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2271 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2272 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2273 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2274 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2275 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2276 | 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] | 2277 | 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] | 2278 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2279 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2280 | const struct lys_module *prefix_mod, *prev_mod; |
| 2281 | struct ly_ctx *ctx; |
| 2282 | |
| 2283 | assert(nodeid && start && parsed); |
| 2284 | |
| 2285 | ctx = start->schema->module->ctx; |
| 2286 | id = nodeid; |
| 2287 | |
| 2288 | 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] | 2289 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2290 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2291 | return NULL; |
| 2292 | } |
| 2293 | id += r; |
| 2294 | /* add it to parsed only after the data node was actually found */ |
| 2295 | last_parsed = r; |
| 2296 | |
| 2297 | if (is_relative) { |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2298 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2299 | start = start->child; |
| 2300 | } else { |
| 2301 | for (; start->parent; start = start->parent); |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2302 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2306 | list_instance_position = 0; |
| 2307 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2308 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2309 | /* 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] | 2310 | if (lys_parent(sibling->schema)) { |
| 2311 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2312 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2313 | 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] | 2314 | *parsed = -1; |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | } else { |
| 2318 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2319 | 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] | 2320 | *parsed = -1; |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2326 | /* name match */ |
| 2327 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2328 | |
| 2329 | /* module check */ |
| 2330 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2331 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2332 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2333 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2334 | return NULL; |
| 2335 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2336 | |
| 2337 | if (ly_buf_used && module_name[0]) { |
| 2338 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2339 | } |
| 2340 | ly_buf_used++; |
| 2341 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2342 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2343 | module_name[mod_name_len] = '\0'; |
| 2344 | /* will also find an augment module */ |
| 2345 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2346 | |
| 2347 | if (buf_backup) { |
| 2348 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2349 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2350 | free(buf_backup); |
| 2351 | buf_backup = NULL; |
| 2352 | } |
| 2353 | ly_buf_used--; |
| 2354 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2355 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2356 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2357 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2358 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2359 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2360 | return NULL; |
| 2361 | } |
| 2362 | } else { |
| 2363 | prefix_mod = prev_mod; |
| 2364 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2365 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2366 | continue; |
| 2367 | } |
| 2368 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2369 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2370 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2371 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2372 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2373 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2374 | if (has_predicate) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2375 | if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2376 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2377 | *parsed = -1; |
| 2378 | return NULL; |
| 2379 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2380 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2381 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2382 | *parsed = -1; |
| 2383 | return NULL; |
| 2384 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2385 | } else { |
| 2386 | r = 0; |
| 2387 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2388 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2392 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2393 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2394 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2395 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2396 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2397 | } else { |
| 2398 | data_val = llist->value_str; |
| 2399 | } |
| 2400 | |
| 2401 | if ((!llist_value && data_val && data_val[0]) |
| 2402 | || (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] | 2403 | continue; |
| 2404 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2405 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2406 | id += r; |
| 2407 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2408 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2409 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2410 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2411 | /* 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] | 2412 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2413 | /* none match */ |
| 2414 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2415 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2416 | |
| 2417 | ++list_instance_position; |
| 2418 | r = 0; |
| 2419 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2420 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2421 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2422 | return NULL; |
| 2423 | } else if (ret == 1) { |
| 2424 | /* this list instance does not match */ |
| 2425 | continue; |
| 2426 | } |
| 2427 | id += r; |
| 2428 | last_parsed += r; |
| 2429 | } |
| 2430 | |
| 2431 | *parsed += last_parsed; |
| 2432 | |
| 2433 | /* the result node? */ |
| 2434 | if (!id[0]) { |
| 2435 | return sibling; |
| 2436 | } |
| 2437 | |
| 2438 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2439 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2440 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2441 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2442 | return NULL; |
| 2443 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2444 | last_match = sibling; |
Michal Vasko | fc11b68 | 2016-11-18 09:52:41 +0100 | [diff] [blame] | 2445 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2446 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2447 | break; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | /* no match, return last match */ |
| 2452 | if (!sibling) { |
| 2453 | return last_match; |
| 2454 | } |
| 2455 | |
| 2456 | 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] | 2457 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2458 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2459 | return NULL; |
| 2460 | } |
| 2461 | id += r; |
| 2462 | last_parsed = r; |
| 2463 | } |
| 2464 | |
| 2465 | /* cannot get here */ |
| 2466 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2467 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2468 | return NULL; |
| 2469 | } |
| 2470 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2471 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2472 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2473 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2474 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2475 | * @param[in] str_restr Restriction as a string. |
| 2476 | * @param[in] type Type of the restriction. |
| 2477 | * @param[out] ret Final interval structure that starts with |
| 2478 | * the interval of the initial type, continues with intervals |
| 2479 | * of any superior types derived from the initial one, and |
| 2480 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2481 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2482 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2483 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2484 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2485 | 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] | 2486 | { |
| 2487 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2488 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2489 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2490 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2491 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2492 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2493 | 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] | 2494 | |
| 2495 | switch (type->base) { |
| 2496 | case LY_TYPE_BINARY: |
| 2497 | kind = 0; |
| 2498 | local_umin = 0; |
| 2499 | local_umax = 18446744073709551615UL; |
| 2500 | |
| 2501 | if (!str_restr && type->info.binary.length) { |
| 2502 | str_restr = type->info.binary.length->expr; |
| 2503 | } |
| 2504 | break; |
| 2505 | case LY_TYPE_DEC64: |
| 2506 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2507 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2508 | local_fmax = __INT64_C(9223372036854775807); |
| 2509 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2510 | |
| 2511 | if (!str_restr && type->info.dec64.range) { |
| 2512 | str_restr = type->info.dec64.range->expr; |
| 2513 | } |
| 2514 | break; |
| 2515 | case LY_TYPE_INT8: |
| 2516 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2517 | local_smin = __INT64_C(-128); |
| 2518 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2519 | |
| 2520 | if (!str_restr && type->info.num.range) { |
| 2521 | str_restr = type->info.num.range->expr; |
| 2522 | } |
| 2523 | break; |
| 2524 | case LY_TYPE_INT16: |
| 2525 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2526 | local_smin = __INT64_C(-32768); |
| 2527 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2528 | |
| 2529 | if (!str_restr && type->info.num.range) { |
| 2530 | str_restr = type->info.num.range->expr; |
| 2531 | } |
| 2532 | break; |
| 2533 | case LY_TYPE_INT32: |
| 2534 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2535 | local_smin = __INT64_C(-2147483648); |
| 2536 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2537 | |
| 2538 | if (!str_restr && type->info.num.range) { |
| 2539 | str_restr = type->info.num.range->expr; |
| 2540 | } |
| 2541 | break; |
| 2542 | case LY_TYPE_INT64: |
| 2543 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2544 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2545 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2546 | |
| 2547 | if (!str_restr && type->info.num.range) { |
| 2548 | str_restr = type->info.num.range->expr; |
| 2549 | } |
| 2550 | break; |
| 2551 | case LY_TYPE_UINT8: |
| 2552 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2553 | local_umin = __UINT64_C(0); |
| 2554 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2555 | |
| 2556 | if (!str_restr && type->info.num.range) { |
| 2557 | str_restr = type->info.num.range->expr; |
| 2558 | } |
| 2559 | break; |
| 2560 | case LY_TYPE_UINT16: |
| 2561 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2562 | local_umin = __UINT64_C(0); |
| 2563 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2564 | |
| 2565 | if (!str_restr && type->info.num.range) { |
| 2566 | str_restr = type->info.num.range->expr; |
| 2567 | } |
| 2568 | break; |
| 2569 | case LY_TYPE_UINT32: |
| 2570 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2571 | local_umin = __UINT64_C(0); |
| 2572 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2573 | |
| 2574 | if (!str_restr && type->info.num.range) { |
| 2575 | str_restr = type->info.num.range->expr; |
| 2576 | } |
| 2577 | break; |
| 2578 | case LY_TYPE_UINT64: |
| 2579 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2580 | local_umin = __UINT64_C(0); |
| 2581 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2582 | |
| 2583 | if (!str_restr && type->info.num.range) { |
| 2584 | str_restr = type->info.num.range->expr; |
| 2585 | } |
| 2586 | break; |
| 2587 | case LY_TYPE_STRING: |
| 2588 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2589 | local_umin = __UINT64_C(0); |
| 2590 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2591 | |
| 2592 | if (!str_restr && type->info.str.length) { |
| 2593 | str_restr = type->info.str.length->expr; |
| 2594 | } |
| 2595 | break; |
| 2596 | default: |
| 2597 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2598 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2602 | if (type->der) { |
| 2603 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2604 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2605 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2606 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2607 | assert(!intv || (intv->kind == kind)); |
| 2608 | } |
| 2609 | |
| 2610 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2611 | /* we do not have any restriction, return superior ones */ |
| 2612 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2613 | return EXIT_SUCCESS; |
| 2614 | } |
| 2615 | |
| 2616 | /* adjust local min and max */ |
| 2617 | if (intv) { |
| 2618 | tmp_intv = intv; |
| 2619 | |
| 2620 | if (kind == 0) { |
| 2621 | local_umin = tmp_intv->value.uval.min; |
| 2622 | } else if (kind == 1) { |
| 2623 | local_smin = tmp_intv->value.sval.min; |
| 2624 | } else if (kind == 2) { |
| 2625 | local_fmin = tmp_intv->value.fval.min; |
| 2626 | } |
| 2627 | |
| 2628 | while (tmp_intv->next) { |
| 2629 | tmp_intv = tmp_intv->next; |
| 2630 | } |
| 2631 | |
| 2632 | if (kind == 0) { |
| 2633 | local_umax = tmp_intv->value.uval.max; |
| 2634 | } else if (kind == 1) { |
| 2635 | local_smax = tmp_intv->value.sval.max; |
| 2636 | } else if (kind == 2) { |
| 2637 | local_fmax = tmp_intv->value.fval.max; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | /* finally parse our restriction */ |
| 2642 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2643 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2644 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2645 | if (!tmp_local_intv) { |
| 2646 | assert(!local_intv); |
| 2647 | local_intv = malloc(sizeof *local_intv); |
| 2648 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2649 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2650 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2651 | tmp_local_intv = tmp_local_intv->next; |
| 2652 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2653 | if (!tmp_local_intv) { |
| 2654 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2655 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2656 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2657 | |
| 2658 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2659 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2660 | tmp_local_intv->next = NULL; |
| 2661 | |
| 2662 | /* min */ |
| 2663 | ptr = seg_ptr; |
| 2664 | while (isspace(ptr[0])) { |
| 2665 | ++ptr; |
| 2666 | } |
| 2667 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2668 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2669 | tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2670 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2671 | tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2672 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2673 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2674 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2675 | goto error; |
| 2676 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2677 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2678 | } else if (!strncmp(ptr, "min", 3)) { |
| 2679 | if (kind == 0) { |
| 2680 | tmp_local_intv->value.uval.min = local_umin; |
| 2681 | } else if (kind == 1) { |
| 2682 | tmp_local_intv->value.sval.min = local_smin; |
| 2683 | } else if (kind == 2) { |
| 2684 | tmp_local_intv->value.fval.min = local_fmin; |
| 2685 | } |
| 2686 | |
| 2687 | ptr += 3; |
| 2688 | } else if (!strncmp(ptr, "max", 3)) { |
| 2689 | if (kind == 0) { |
| 2690 | tmp_local_intv->value.uval.min = local_umax; |
| 2691 | } else if (kind == 1) { |
| 2692 | tmp_local_intv->value.sval.min = local_smax; |
| 2693 | } else if (kind == 2) { |
| 2694 | tmp_local_intv->value.fval.min = local_fmax; |
| 2695 | } |
| 2696 | |
| 2697 | ptr += 3; |
| 2698 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2699 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2700 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | while (isspace(ptr[0])) { |
| 2704 | ptr++; |
| 2705 | } |
| 2706 | |
| 2707 | /* no interval or interval */ |
| 2708 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2709 | if (kind == 0) { |
| 2710 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2711 | } else if (kind == 1) { |
| 2712 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2713 | } else if (kind == 2) { |
| 2714 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2715 | } |
| 2716 | } else if (!strncmp(ptr, "..", 2)) { |
| 2717 | /* skip ".." */ |
| 2718 | ptr += 2; |
| 2719 | while (isspace(ptr[0])) { |
| 2720 | ++ptr; |
| 2721 | } |
| 2722 | |
| 2723 | /* max */ |
| 2724 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2725 | if (kind == 0) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2726 | tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2727 | } else if (kind == 1) { |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2728 | tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2729 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2730 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2731 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2732 | goto error; |
| 2733 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2734 | } |
| 2735 | } else if (!strncmp(ptr, "max", 3)) { |
| 2736 | if (kind == 0) { |
| 2737 | tmp_local_intv->value.uval.max = local_umax; |
| 2738 | } else if (kind == 1) { |
| 2739 | tmp_local_intv->value.sval.max = local_smax; |
| 2740 | } else if (kind == 2) { |
| 2741 | tmp_local_intv->value.fval.max = local_fmax; |
| 2742 | } |
| 2743 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2744 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2745 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2746 | } |
| 2747 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2748 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2749 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2752 | /* check min and max in correct order*/ |
| 2753 | if (kind == 0) { |
| 2754 | /* current segment */ |
| 2755 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2756 | goto error; |
| 2757 | } |
| 2758 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2759 | goto error; |
| 2760 | } |
| 2761 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2762 | 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] | 2763 | goto error; |
| 2764 | } |
| 2765 | } else if (kind == 1) { |
| 2766 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2767 | goto error; |
| 2768 | } |
| 2769 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2770 | goto error; |
| 2771 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2772 | 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] | 2773 | goto error; |
| 2774 | } |
| 2775 | } else if (kind == 2) { |
| 2776 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2777 | goto error; |
| 2778 | } |
| 2779 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2780 | goto error; |
| 2781 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2782 | 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] | 2783 | /* 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] | 2784 | goto error; |
| 2785 | } |
| 2786 | } |
| 2787 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2788 | /* next segment (next OR) */ |
| 2789 | seg_ptr = strchr(seg_ptr, '|'); |
| 2790 | if (!seg_ptr) { |
| 2791 | break; |
| 2792 | } |
| 2793 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2794 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | /* check local restrictions against superior ones */ |
| 2798 | if (intv) { |
| 2799 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2800 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2801 | |
| 2802 | while (tmp_local_intv && tmp_intv) { |
| 2803 | /* reuse local variables */ |
| 2804 | if (kind == 0) { |
| 2805 | local_umin = tmp_local_intv->value.uval.min; |
| 2806 | local_umax = tmp_local_intv->value.uval.max; |
| 2807 | |
| 2808 | /* it must be in this interval */ |
| 2809 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2810 | /* this interval is covered, next one */ |
| 2811 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2812 | tmp_local_intv = tmp_local_intv->next; |
| 2813 | continue; |
| 2814 | /* ascending order of restrictions -> fail */ |
| 2815 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2816 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | } else if (kind == 1) { |
| 2820 | local_smin = tmp_local_intv->value.sval.min; |
| 2821 | local_smax = tmp_local_intv->value.sval.max; |
| 2822 | |
| 2823 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2824 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2825 | tmp_local_intv = tmp_local_intv->next; |
| 2826 | continue; |
| 2827 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2828 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | } else if (kind == 2) { |
| 2832 | local_fmin = tmp_local_intv->value.fval.min; |
| 2833 | local_fmax = tmp_local_intv->value.fval.max; |
| 2834 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2835 | 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] | 2836 | && (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] | 2837 | 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] | 2838 | tmp_local_intv = tmp_local_intv->next; |
| 2839 | continue; |
| 2840 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2841 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | tmp_intv = tmp_intv->next; |
| 2847 | } |
| 2848 | |
| 2849 | /* some interval left uncovered -> fail */ |
| 2850 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2851 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2852 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2855 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2856 | if (intv) { |
| 2857 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2858 | tmp_intv->next = local_intv; |
| 2859 | } else { |
| 2860 | intv = local_intv; |
| 2861 | } |
| 2862 | *ret = intv; |
| 2863 | |
| 2864 | return EXIT_SUCCESS; |
| 2865 | |
| 2866 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2867 | while (intv) { |
| 2868 | tmp_intv = intv->next; |
| 2869 | free(intv); |
| 2870 | intv = tmp_intv; |
| 2871 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2872 | while (local_intv) { |
| 2873 | tmp_local_intv = local_intv->next; |
| 2874 | free(local_intv); |
| 2875 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2878 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2881 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2882 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 2883 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2884 | * |
| 2885 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2886 | * @param[in] mod_name Typedef name module name. |
| 2887 | * @param[in] module Main module. |
| 2888 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2889 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2890 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2891 | * @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] | 2892 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2893 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2894 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2895 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2896 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2897 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2898 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2899 | int tpdf_size; |
| 2900 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2901 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2902 | /* no prefix, try built-in types */ |
| 2903 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2904 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2905 | if (ret) { |
| 2906 | *ret = ly_types[i].def; |
| 2907 | } |
| 2908 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2909 | } |
| 2910 | } |
| 2911 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2912 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2913 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2914 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2918 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2919 | /* search in local typedefs */ |
| 2920 | while (parent) { |
| 2921 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2922 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2923 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2924 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2925 | break; |
| 2926 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2927 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2928 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2929 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2930 | break; |
| 2931 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2932 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2933 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2934 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2935 | break; |
| 2936 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2937 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2938 | case LYS_ACTION: |
| 2939 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 2940 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2941 | break; |
| 2942 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2943 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2944 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2945 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2946 | break; |
| 2947 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2948 | case LYS_INPUT: |
| 2949 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2950 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 2951 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2952 | break; |
| 2953 | |
| 2954 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2955 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2956 | continue; |
| 2957 | } |
| 2958 | |
| 2959 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2960 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2961 | match = &tpdf[i]; |
| 2962 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2966 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2967 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2968 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2969 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2970 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2971 | if (!module) { |
| 2972 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | /* search in top level typedefs */ |
| 2977 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2978 | 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] | 2979 | match = &module->tpdf[i]; |
| 2980 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2985 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2986 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 2987 | 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] | 2988 | match = &module->inc[i].submodule->tpdf[j]; |
| 2989 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2994 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2995 | |
| 2996 | check_leafref: |
| 2997 | if (ret) { |
| 2998 | *ret = match; |
| 2999 | } |
| 3000 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3001 | while (!match->type.info.lref.path) { |
| 3002 | match = match->type.der; |
| 3003 | assert(match); |
| 3004 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3005 | } |
| 3006 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3009 | /** |
| 3010 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3011 | * |
| 3012 | * @param[in] type Type definition to use. |
| 3013 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3014 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3015 | * |
| 3016 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3017 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3018 | static int |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3019 | check_default(struct lys_type *type, const char **value, struct lys_module *module) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3020 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3021 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3022 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3023 | const char *dflt = NULL; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3024 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3025 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3026 | assert(value); |
| 3027 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3028 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3029 | /* the type was not resolved yet, nothing to do for now */ |
| 3030 | return EXIT_FAILURE; |
| 3031 | } |
| 3032 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3033 | dflt = *value; |
| 3034 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3035 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3036 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3037 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3038 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3039 | break; |
| 3040 | } |
| 3041 | } |
| 3042 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3043 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3044 | /* no default value, nothing to check, all is well */ |
| 3045 | return EXIT_SUCCESS; |
| 3046 | } |
| 3047 | |
| 3048 | /* 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)? */ |
| 3049 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3050 | case LY_TYPE_IDENT: |
| 3051 | case LY_TYPE_INST: |
| 3052 | case LY_TYPE_LEAFREF: |
| 3053 | case LY_TYPE_BOOL: |
| 3054 | case LY_TYPE_EMPTY: |
| 3055 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3056 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3057 | case LY_TYPE_BITS: |
| 3058 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3059 | if (type->info.bits.count) { |
| 3060 | break; |
| 3061 | } |
| 3062 | return EXIT_SUCCESS; |
| 3063 | case LY_TYPE_ENUM: |
| 3064 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3065 | if (type->info.enums.count) { |
| 3066 | break; |
| 3067 | } |
| 3068 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3069 | case LY_TYPE_DEC64: |
| 3070 | if (type->info.dec64.range) { |
| 3071 | break; |
| 3072 | } |
| 3073 | return EXIT_SUCCESS; |
| 3074 | case LY_TYPE_BINARY: |
| 3075 | if (type->info.binary.length) { |
| 3076 | break; |
| 3077 | } |
| 3078 | return EXIT_SUCCESS; |
| 3079 | case LY_TYPE_INT8: |
| 3080 | case LY_TYPE_INT16: |
| 3081 | case LY_TYPE_INT32: |
| 3082 | case LY_TYPE_INT64: |
| 3083 | case LY_TYPE_UINT8: |
| 3084 | case LY_TYPE_UINT16: |
| 3085 | case LY_TYPE_UINT32: |
| 3086 | case LY_TYPE_UINT64: |
| 3087 | if (type->info.num.range) { |
| 3088 | break; |
| 3089 | } |
| 3090 | return EXIT_SUCCESS; |
| 3091 | case LY_TYPE_STRING: |
| 3092 | if (type->info.str.length || type->info.str.patterns) { |
| 3093 | break; |
| 3094 | } |
| 3095 | return EXIT_SUCCESS; |
| 3096 | case LY_TYPE_UNION: |
| 3097 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3098 | break; |
| 3099 | default: |
| 3100 | LOGINT; |
| 3101 | return -1; |
| 3102 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3103 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3104 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3105 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3106 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3107 | } |
| 3108 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3109 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3110 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3111 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3112 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3113 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3114 | if (!node.schema) { |
| 3115 | LOGMEM; |
| 3116 | return -1; |
| 3117 | } |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3118 | node.schema->name = strdup("fake-default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3119 | if (!node.schema->name) { |
| 3120 | LOGMEM; |
Michal Vasko | f49eda0 | 2016-07-21 12:17:01 +0200 | [diff] [blame] | 3121 | free(node.schema); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3122 | return -1; |
| 3123 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3124 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3125 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3126 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3127 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3128 | if (!type->info.lref.target) { |
| 3129 | ret = EXIT_FAILURE; |
| 3130 | goto finish; |
| 3131 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3132 | ret = check_default(&type->info.lref.target->type, &dflt, module); |
| 3133 | if (!ret) { |
| 3134 | /* adopt possibly changed default value to its canonical form */ |
| 3135 | if (*value) { |
| 3136 | *value = dflt; |
| 3137 | } |
| 3138 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3139 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3140 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3141 | /* possible forward reference */ |
| 3142 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3143 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3144 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3145 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3146 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3147 | /* we have refined bits/enums */ |
| 3148 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3149 | "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] | 3150 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3151 | } |
| 3152 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3153 | } else { |
| 3154 | /* success - adopt canonical form from the node into the default value */ |
| 3155 | if (dflt != node.value_str) { |
| 3156 | /* this can happen only if we have non-inherited default value, |
| 3157 | * inherited default values are already in canonical form */ |
| 3158 | assert(dflt == *value); |
| 3159 | *value = node.value_str; |
| 3160 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3161 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | finish: |
| 3165 | if (node.value_type == LY_TYPE_BITS) { |
| 3166 | free(node.value.bit); |
| 3167 | } |
| 3168 | free((char *)node.schema->name); |
| 3169 | free(node.schema); |
| 3170 | |
| 3171 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3174 | /** |
| 3175 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3176 | * |
| 3177 | * @param[in] key The key to check. |
| 3178 | * @param[in] flags What flags to check. |
| 3179 | * @param[in] list The list of all the keys. |
| 3180 | * @param[in] index Index of the key in the key list. |
| 3181 | * @param[in] name The name of the keys. |
| 3182 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3183 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3184 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3185 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3186 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3187 | 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] | 3188 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3189 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3190 | char *dup = NULL; |
| 3191 | int j; |
| 3192 | |
| 3193 | /* existence */ |
| 3194 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3195 | if (name[len] != '\0') { |
| 3196 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3197 | if (!dup) { |
| 3198 | LOGMEM; |
| 3199 | return -1; |
| 3200 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3201 | dup[len] = '\0'; |
| 3202 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3203 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3204 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3205 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3206 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /* uniqueness */ |
| 3210 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3211 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3212 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3213 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3218 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3219 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3220 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3224 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3225 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3226 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3230 | 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] | 3231 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3232 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3233 | } |
| 3234 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3235 | /* key is not placed from augment */ |
| 3236 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3237 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3238 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3239 | return -1; |
| 3240 | } |
| 3241 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3242 | /* key is not when/if-feature -conditional */ |
| 3243 | j = 0; |
| 3244 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3245 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3246 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.", |
| 3247 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3248 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3251 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3252 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3253 | |
| 3254 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3255 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3256 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3257 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3258 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3259 | * |
| 3260 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3261 | */ |
| 3262 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3263 | 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] | 3264 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3265 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3266 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3267 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3268 | rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3269 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3270 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3271 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3272 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3273 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3274 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 3275 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3276 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3277 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3278 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3279 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3280 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3281 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3282 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3283 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3284 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3285 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3286 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3287 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3288 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3291 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3292 | if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3293 | return -1; |
| 3294 | } |
| 3295 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3296 | /* check that all unique's targets are of the same config type */ |
| 3297 | if (*trg_type) { |
| 3298 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3299 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3300 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, |
| 3301 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3302 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3303 | return -1; |
| 3304 | } |
| 3305 | } else { |
| 3306 | /* first unique */ |
| 3307 | if (leaf->flags & LYS_CONFIG_W) { |
| 3308 | *trg_type = 1; |
| 3309 | } else { |
| 3310 | *trg_type = 2; |
| 3311 | } |
| 3312 | } |
| 3313 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3314 | /* set leaf's unique flag */ |
| 3315 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3316 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3317 | return EXIT_SUCCESS; |
| 3318 | |
| 3319 | error: |
| 3320 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3321 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3324 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3325 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3326 | { |
| 3327 | /* there are items after the one deleted */ |
| 3328 | if (i+1 < unres->count) { |
| 3329 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3330 | 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] | 3331 | |
| 3332 | /* deleting the last item */ |
| 3333 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3334 | free(unres->node); |
| 3335 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3339 | --unres->count; |
| 3340 | } |
| 3341 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3342 | /** |
| 3343 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3344 | * |
| 3345 | * @param[in] mod Module to search in. |
| 3346 | * @param[in] name Name of the data node. |
| 3347 | * @param[in] nam_len Length of the name. |
| 3348 | * @param[in] start Data node to start the search from. |
| 3349 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3350 | * they are replaced (!!) with the resolvents. |
| 3351 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3352 | * @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] | 3353 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3354 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3355 | 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] | 3356 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3357 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3358 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3359 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3360 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3361 | if (!parents->count) { |
| 3362 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3363 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3364 | if (!parents->node) { |
| 3365 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3366 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3367 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3368 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3369 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3370 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3371 | 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] | 3372 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3373 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3374 | continue; |
| 3375 | } |
| 3376 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3377 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3378 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 3379 | && node->schema->name[nam_len] == '\0') { |
| 3380 | /* matching target */ |
| 3381 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3382 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3383 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | flag = 1; |
| 3385 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3386 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3387 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3388 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 3389 | if (!parents->node) { |
| 3390 | return EXIT_FAILURE; |
| 3391 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3392 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3393 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3397 | |
| 3398 | if (!flag) { |
| 3399 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3400 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3401 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3402 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3403 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3404 | } |
| 3405 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3406 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3409 | /** |
| 3410 | * @brief Resolve (find) a data node. Does not log. |
| 3411 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3412 | * @param[in] mod_name Module name of the data node. |
| 3413 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3414 | * @param[in] name Name of the data node. |
| 3415 | * @param[in] nam_len Length of the name. |
| 3416 | * @param[in] start Data node to start the search from. |
| 3417 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3418 | * they are replaced (!!) with the resolvents. |
| 3419 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3420 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3421 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3422 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3423 | 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] | 3424 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3425 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3426 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3427 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3428 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3429 | assert(start); |
| 3430 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3431 | if (mod_name) { |
| 3432 | /* we have mod_name, find appropriate module */ |
| 3433 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3434 | if (!str) { |
| 3435 | LOGMEM; |
| 3436 | return -1; |
| 3437 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 3438 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 3439 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3440 | if (!mod) { |
| 3441 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3442 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3443 | } |
| 3444 | } else { |
| 3445 | /* no prefix, module is the same as of current node */ |
| 3446 | mod = start->schema->module; |
| 3447 | } |
| 3448 | |
| 3449 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3450 | } |
| 3451 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3452 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3453 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3454 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3455 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3456 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3457 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3458 | * @param[in,out] node_match Nodes satisfying the restriction |
| 3459 | * without the predicate. Nodes not |
| 3460 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3461 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3462 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3463 | * @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] | 3464 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3465 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3466 | 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] | 3467 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3468 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3469 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3470 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3471 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3472 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 3473 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3474 | uint32_t j; |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3475 | struct lyd_node_leaf_list *leaf_dst, *leaf_src; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3476 | |
| 3477 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3478 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3479 | if (!source_match.node) { |
| 3480 | LOGMEM; |
| 3481 | return -1; |
| 3482 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3483 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3484 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3485 | if (!dest_match.node) { |
| 3486 | LOGMEM; |
| 3487 | return -1; |
| 3488 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3489 | |
| 3490 | do { |
| 3491 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 3492 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3493 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3494 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3495 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3496 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3497 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3498 | pred += i; |
| 3499 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3500 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3501 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3502 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3503 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3504 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3505 | 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] | 3506 | &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] | 3507 | i = 0; |
| 3508 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | /* destination */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3512 | dest_match.node[0] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3513 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3514 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3515 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3516 | 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] | 3517 | rc = -1; |
| 3518 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3519 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3520 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3521 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3522 | dest_match.node[0] = dest_match.node[0]->parent; |
| 3523 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3524 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3525 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3526 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3530 | 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] | 3531 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3532 | i = 0; |
| 3533 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | if (pke_len == pke_parsed) { |
| 3537 | break; |
| 3538 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3539 | 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] | 3540 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3541 | 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] | 3542 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3543 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3544 | } |
| 3545 | pke_parsed += i; |
| 3546 | } |
| 3547 | |
| 3548 | /* check match between source and destination nodes */ |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3549 | leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0]; |
| 3550 | while (leaf_dst->value_type == LY_TYPE_LEAFREF) { |
| 3551 | leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref; |
| 3552 | } |
| 3553 | leaf_src = (struct lyd_node_leaf_list *)source_match.node[0]; |
| 3554 | while (leaf_src->value_type == LY_TYPE_LEAFREF) { |
| 3555 | leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref; |
| 3556 | } |
| 3557 | if (leaf_src->value_type != leaf_dst->value_type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3558 | goto remove_leafref; |
| 3559 | } |
| 3560 | |
Radek Krejci | 0c2fa3a | 2016-06-13 15:18:03 +0200 | [diff] [blame] | 3561 | if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3562 | goto remove_leafref; |
| 3563 | } |
| 3564 | |
| 3565 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3566 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3567 | continue; |
| 3568 | |
| 3569 | remove_leafref: |
| 3570 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3571 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3572 | } |
| 3573 | } while (has_predicate); |
| 3574 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3575 | free(source_match.node); |
| 3576 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3577 | if (parsed) { |
| 3578 | *parsed = parsed_loc; |
| 3579 | } |
| 3580 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3581 | |
| 3582 | error: |
| 3583 | |
| 3584 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3585 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3586 | } |
| 3587 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3588 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3589 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3590 | if (parsed) { |
| 3591 | *parsed = -parsed_loc+i; |
| 3592 | } |
| 3593 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3596 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3597 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3598 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3599 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3600 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3601 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3602 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3603 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3604 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3605 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3606 | 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] | 3607 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 3608 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3609 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3610 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3611 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3612 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3613 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3614 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3615 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3616 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3617 | |
| 3618 | /* searching for nodeset */ |
| 3619 | do { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 3620 | 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] | 3621 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3622 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3623 | goto error; |
| 3624 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3625 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3626 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3627 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3628 | if (!ret->count) { |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3629 | if (parent_times > 0) { |
| 3630 | data = node; |
| 3631 | for (i = 1; i < parent_times; ++i) { |
| 3632 | data = data->parent; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3633 | } |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3634 | } else if (!parent_times) { |
| 3635 | data = node->child; |
| 3636 | } else { |
| 3637 | /* absolute path */ |
| 3638 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
Michal Vasko | bfd98e6 | 2016-09-02 09:50:05 +0200 | [diff] [blame] | 3641 | /* we may still be parsing it and the pointer is not correct yet */ |
| 3642 | if (data->prev) { |
| 3643 | while (data->prev->next) { |
| 3644 | data = data->prev; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3645 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3650 | 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] | 3651 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3652 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3653 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | goto error; |
| 3655 | } |
| 3656 | |
| 3657 | if (has_predicate) { |
| 3658 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3659 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3660 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 3661 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3662 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3663 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3664 | continue; |
| 3665 | } |
| 3666 | |
| 3667 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3668 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3669 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3670 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 3671 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3672 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3673 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3674 | goto error; |
| 3675 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3676 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 3677 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3678 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3679 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3680 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3681 | goto error; |
| 3682 | } |
| 3683 | } |
| 3684 | } while (path[0] != '\0'); |
| 3685 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3686 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3687 | |
| 3688 | error: |
| 3689 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3690 | free(ret->node); |
| 3691 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3692 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3693 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3694 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3697 | static int |
| 3698 | resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
| 3699 | { |
| 3700 | int dep1, dep2; |
| 3701 | const struct lys_node *node; |
| 3702 | |
| 3703 | if (lys_parent(op_node)) { |
| 3704 | /* inner operation (notif/action) */ |
| 3705 | if (abs_path) { |
| 3706 | return 1; |
| 3707 | } else { |
| 3708 | /* compare depth of both nodes */ |
| 3709 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3710 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3711 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3712 | return 1; |
| 3713 | } |
| 3714 | } |
| 3715 | } else { |
| 3716 | /* top-level operation (notif/rpc) */ |
| 3717 | if (op_node != first_node) { |
| 3718 | return 1; |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3725 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3726 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3727 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3728 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3729 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3730 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3731 | * @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] | 3732 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3733 | * @return 0 on forward reference, otherwise the number |
| 3734 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3735 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3736 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3737 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3738 | resolve_path_predicate_schema(const char *path, const struct lys_node *context_node, |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3739 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3740 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3741 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3742 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3743 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3744 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3745 | |
| 3746 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3747 | 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] | 3748 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3749 | 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] | 3750 | return -parsed+i; |
| 3751 | } |
| 3752 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3753 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3754 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3755 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3756 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3757 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3758 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3759 | rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len, |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3760 | LYS_LEAF | LYS_LEAFLIST | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3761 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3762 | 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] | 3763 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3767 | dest_parent_times = 0; |
| 3768 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3769 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3770 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3771 | 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] | 3772 | return -parsed; |
| 3773 | } |
| 3774 | pke_parsed += i; |
| 3775 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3776 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3777 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3778 | * all schema nodes that cannot be instantiated in data tree */ |
| 3779 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3780 | 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] | 3781 | dst_node = lys_parent(dst_node)); |
| 3782 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3783 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3784 | 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] | 3785 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3786 | } |
| 3787 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3788 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3789 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3790 | if (!dest_pref) { |
| 3791 | dest_pref = dst_node->module->name; |
| 3792 | } |
| 3793 | rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len, |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 3794 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3795 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3796 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3797 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3800 | if (first_iter) { |
| 3801 | if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3802 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3803 | } |
| 3804 | first_iter = 0; |
| 3805 | } |
| 3806 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3807 | if (pke_len == pke_parsed) { |
| 3808 | break; |
| 3809 | } |
| 3810 | |
| 3811 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3812 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3813 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3814 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3815 | return -parsed; |
| 3816 | } |
| 3817 | pke_parsed += i; |
| 3818 | } |
| 3819 | |
| 3820 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3821 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3822 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3823 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "Destination node is not a %s, but a %s.", |
| 3824 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3825 | return -parsed; |
| 3826 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3827 | } while (has_predicate); |
| 3828 | |
| 3829 | return parsed; |
| 3830 | } |
| 3831 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3832 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3833 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3834 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3835 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3836 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3837 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3838 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3839 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3840 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3841 | * @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] | 3842 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3843 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3844 | resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf, |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3845 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3846 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3847 | const struct lys_node *node, *op_node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3848 | const struct lys_module *mod; |
| 3849 | struct lys_module *mod_start; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3850 | const char *id, *prefix, *name; |
| 3851 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3852 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3853 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3854 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3855 | parent_times = 0; |
| 3856 | id = path; |
| 3857 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3858 | /* find operation schema we are in, if applicable */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3859 | if (!parent_tpdf) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3860 | for (op_node = lys_parent(parent); |
| 3861 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3862 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3863 | } |
| 3864 | |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3865 | mod_start = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3866 | do { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3867 | if ((i = parse_path_arg(mod_start, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3868 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3869 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3870 | } |
| 3871 | id += i; |
| 3872 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3873 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3874 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3875 | /* resolve prefix of the module */ |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3876 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3877 | if (!mod) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3878 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3879 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3880 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3881 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3882 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3883 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3884 | if (!mod->implemented) { |
| 3885 | /* make the found module implemented */ |
| 3886 | if (lys_set_implemented(mod)) { |
| 3887 | return EXIT_FAILURE; |
| 3888 | } |
| 3889 | } |
| 3890 | } |
| 3891 | /* get start node */ |
| 3892 | if (!mod->data) { |
| 3893 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3894 | "leafref", path); |
| 3895 | return EXIT_FAILURE; |
| 3896 | } |
| 3897 | node = mod->data; |
| 3898 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3899 | } else if (parent_times > 0) { |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3900 | if (parent_tpdf) { |
| 3901 | /* the path is not allowed to contain relative path since we are in top level typedef */ |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3902 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3903 | return -1; |
| 3904 | } |
| 3905 | |
Michal Vasko | 9445808 | 2016-10-07 14:34:36 +0200 | [diff] [blame] | 3906 | /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */ |
| 3907 | for (i = 0, node = parent; i < parent_times - 1; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3908 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3909 | * all schema nodes that cannot be instantiated in data tree */ |
| 3910 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3911 | 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] | 3912 | node = lys_parent(node)); |
| 3913 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3914 | if (!node) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3915 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3916 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3917 | } |
| 3918 | } |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3919 | |
| 3920 | /* now we have to check that if we are going into a node from a different module, |
| 3921 | * the module is implemented (so its augments are applied) */ |
| 3922 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3923 | if (!mod) { |
| 3924 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3925 | return EXIT_FAILURE; |
| 3926 | } |
| 3927 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3928 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3929 | if (!mod->implemented) { |
| 3930 | /* make the found module implemented */ |
| 3931 | if (lys_set_implemented(mod)) { |
| 3932 | return EXIT_FAILURE; |
| 3933 | } |
| 3934 | } |
| 3935 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3936 | } else { |
| 3937 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3938 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3939 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3940 | } else { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3941 | /* we have to first check that the module we are going into is implemented */ |
| 3942 | mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start; |
| 3943 | if (!mod) { |
| 3944 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3945 | return EXIT_FAILURE; |
| 3946 | } |
| 3947 | if (!mod->implemented) { |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 3948 | mod = lys_implemented_module(mod); |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3949 | if (!mod->implemented) { |
| 3950 | /* make the found module implemented */ |
| 3951 | if (lys_set_implemented(mod)) { |
| 3952 | return EXIT_FAILURE; |
| 3953 | } |
| 3954 | } |
| 3955 | } |
| 3956 | |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3957 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3958 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3959 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, name[0], name); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3960 | return -1; |
| 3961 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3962 | node = node->child; |
Radek Krejci | 43ccc4c | 2016-10-18 20:40:06 +0200 | [diff] [blame] | 3963 | if (!node) { |
| 3964 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3965 | "leafref", path); |
| 3966 | return EXIT_FAILURE; |
| 3967 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3968 | } |
| 3969 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3970 | if (!prefix) { |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3971 | prefix = mod_start->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3972 | } |
| 3973 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3974 | rc = lys_get_sibling(node, prefix, pref_len, name, nam_len, LYS_ANY & ~(LYS_USES | LYS_GROUPING), &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3975 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3976 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3977 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3978 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3979 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3980 | if (first_iter) { |
| 3981 | /* set external dependency flag, we can decide based on the first found node */ |
| 3982 | if (!parent_tpdf && op_node && parent_times && |
| 3983 | resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3984 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3985 | } |
| 3986 | first_iter = 0; |
| 3987 | } |
| 3988 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3989 | if (has_predicate) { |
| 3990 | /* we have predicate, so the current result must be list */ |
| 3991 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3992 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3993 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3996 | i = resolve_path_predicate_schema(id, node, parent, op_node); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3997 | if (i <= 0) { |
| 3998 | if (i == 0) { |
| 3999 | return EXIT_FAILURE; |
| 4000 | } else { /* i < 0 */ |
| 4001 | return -1; |
| 4002 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4003 | } |
| 4004 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 4005 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4006 | } |
| 4007 | } while (id[0]); |
| 4008 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 4009 | /* 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] | 4010 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 4011 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Radek Krejci | d47daf6 | 2016-08-22 16:23:38 +0200 | [diff] [blame] | 4012 | LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
Radek Krejci | 2a5a960 | 2016-11-04 10:21:13 +0100 | [diff] [blame] | 4013 | "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4014 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4017 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4018 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4019 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4020 | return -1; |
| 4021 | } |
| 4022 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4023 | if (ret) { |
| 4024 | *ret = node; |
| 4025 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4026 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4027 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4030 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4031 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4032 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4033 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4034 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4035 | * @param[in,out] node_match Nodes matching the restriction without |
| 4036 | * the predicate. Nodes not satisfying |
| 4037 | * the predicate are removed. |
| 4038 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4039 | * @return Number of characters successfully parsed, |
| 4040 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4041 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4042 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4043 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4044 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4045 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4046 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4047 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4048 | 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] | 4049 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4050 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4051 | assert(pred && node_match->count); |
| 4052 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4053 | idx = -1; |
| 4054 | parsed = 0; |
| 4055 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4056 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4057 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4058 | 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] | 4059 | return -parsed+i; |
| 4060 | } |
| 4061 | parsed += i; |
| 4062 | pred += i; |
| 4063 | |
| 4064 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4065 | /* pos */ |
| 4066 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4067 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4068 | } else if (name[0] != '.') { |
| 4069 | /* list keys */ |
| 4070 | if (pred_iter < 0) { |
| 4071 | pred_iter = 1; |
| 4072 | } else { |
| 4073 | ++pred_iter; |
| 4074 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4075 | } |
| 4076 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 4077 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4078 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4079 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4080 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4081 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4082 | goto remove_instid; |
| 4083 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4084 | |
| 4085 | target = node_match->node[j]; |
| 4086 | /* check the value */ |
| 4087 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4088 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4089 | goto remove_instid; |
| 4090 | } |
| 4091 | |
| 4092 | } else if (!value) { |
| 4093 | /* keyless list position */ |
| 4094 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 4095 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 4096 | goto remove_instid; |
| 4097 | } |
| 4098 | |
| 4099 | if (idx != cur_idx) { |
| 4100 | goto remove_instid; |
| 4101 | } |
| 4102 | |
| 4103 | } else { |
| 4104 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4105 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4106 | goto remove_instid; |
| 4107 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4108 | |
| 4109 | /* key module must match the list module */ |
| 4110 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 4111 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 4112 | goto remove_instid; |
| 4113 | } |
| 4114 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4115 | 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] | 4116 | if (!target) { |
| 4117 | goto remove_instid; |
| 4118 | } |
| 4119 | if ((struct lys_node_leaf *)target->schema != |
| 4120 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 4121 | goto remove_instid; |
| 4122 | } |
| 4123 | |
| 4124 | /* check the value */ |
| 4125 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 4126 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 4127 | goto remove_instid; |
| 4128 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4129 | } |
| 4130 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4131 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4132 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4133 | continue; |
| 4134 | |
| 4135 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4136 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4137 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4138 | } |
| 4139 | } while (has_predicate); |
| 4140 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4141 | /* check that all list keys were specified */ |
| 4142 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4143 | j = 0; |
| 4144 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4145 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4146 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4147 | /* not enough predicates, just remove the list instance */ |
| 4148 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4149 | } else { |
| 4150 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | if (!node_match->count) { |
| 4155 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4156 | } |
| 4157 | } |
| 4158 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4159 | return parsed; |
| 4160 | } |
| 4161 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4162 | int |
| 4163 | lys_check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4164 | { |
| 4165 | struct lys_node *parent, *elem; |
| 4166 | struct lyxp_set set; |
| 4167 | uint32_t i; |
| 4168 | int rc; |
| 4169 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4170 | if (check_place) { |
| 4171 | parent = node; |
| 4172 | while (parent) { |
| 4173 | if (parent->nodetype == LYS_GROUPING) { |
| 4174 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4175 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4176 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4177 | if (parent->nodetype == LYS_AUGMENT) { |
| 4178 | if (!((struct lys_node_augment *)parent)->target) { |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4179 | /* unresolved augment */ |
| 4180 | if (parent->module->implemented) { |
| 4181 | /* skip for now (will be checked later) */ |
| 4182 | return EXIT_FAILURE; |
| 4183 | } else { |
| 4184 | /* not implemented augment, skip resolving */ |
| 4185 | return EXIT_SUCCESS; |
| 4186 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4187 | } else { |
| 4188 | parent = ((struct lys_node_augment *)parent)->target; |
| 4189 | continue; |
| 4190 | } |
| 4191 | } |
| 4192 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4193 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | rc = lyxp_node_atomize(node, &set); |
| 4197 | if (rc) { |
| 4198 | return rc; |
| 4199 | } |
| 4200 | |
| 4201 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4202 | |
| 4203 | for (i = 0; i < set.used; ++i) { |
| 4204 | /* skip roots'n'stuff */ |
| 4205 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4206 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4207 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4208 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4209 | return -1; |
| 4210 | } |
| 4211 | |
| 4212 | if (parent) { |
| 4213 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4214 | if (!elem) { |
| 4215 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4216 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4217 | break; |
| 4218 | } |
| 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | free(set.val.snodes); |
| 4224 | return EXIT_SUCCESS; |
| 4225 | } |
| 4226 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4227 | static int |
| 4228 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4229 | { |
| 4230 | int i; |
| 4231 | |
| 4232 | if (type->base == LY_TYPE_LEAFREF) { |
| 4233 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && (type->info.lref.target->flags & LYS_CONFIG_R)) { |
| 4234 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The %s is config but refers to a non-config %s.", |
| 4235 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4236 | return -1; |
| 4237 | } |
| 4238 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4239 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4240 | } else if (type->base == LY_TYPE_UNION) { |
| 4241 | for (i = 0; i < type->info.uni.count; i++) { |
| 4242 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4243 | return -1; |
| 4244 | } |
| 4245 | } |
| 4246 | } |
| 4247 | return 0; |
| 4248 | } |
| 4249 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4250 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4251 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 4252 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4253 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4254 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4255 | * @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] | 4256 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4257 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4258 | * |
| 4259 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4260 | */ |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4261 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4262 | inherit_config_flag(struct lys_node *node, int flags, int clear, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4263 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4264 | struct lys_node_leaf *leaf; |
| 4265 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4266 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4267 | LY_TREE_FOR(node, node) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4268 | if (lys_has_xpath(node) && unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4269 | return -1; |
| 4270 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4271 | if (clear) { |
| 4272 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4273 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4274 | } else { |
| 4275 | if (node->flags & LYS_CONFIG_SET) { |
| 4276 | /* skip nodes with an explicit config value */ |
| 4277 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4278 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4279 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 4280 | return -1; |
| 4281 | } |
| 4282 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4283 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4284 | |
| 4285 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4286 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4287 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4288 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4289 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4290 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4291 | return -1; |
| 4292 | } |
| 4293 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4294 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4295 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4296 | if (inherit_config_flag(node->child, flags, clear, unres)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4297 | return -1; |
| 4298 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4299 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4300 | leaf = (struct lys_node_leaf *)node; |
| 4301 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4302 | return -1; |
| 4303 | } |
| 4304 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4305 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4306 | |
| 4307 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4308 | } |
| 4309 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4310 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4311 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4312 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4313 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4314 | * @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] | 4315 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4316 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4317 | * @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] | 4318 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4319 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4320 | 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] | 4321 | { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4322 | int rc, clear_config; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4323 | struct lys_node *sub; |
Pavol Vican | 4731993 | 2016-08-29 09:14:47 +0200 | [diff] [blame] | 4324 | const struct lys_node *aug_target, *parent; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4325 | struct lys_module *mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4326 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4327 | assert(aug && !aug->target); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4328 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4329 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4330 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4331 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), mod->implemented, &aug_target); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4332 | if (rc == -1) { |
| 4333 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4334 | } else if (rc > 0) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4335 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
| 4336 | return -1; |
Radek Krejci | 5fb4c38 | 2016-11-28 09:21:42 +0100 | [diff] [blame] | 4337 | } else if (rc == 0 && aug->target) { |
| 4338 | /* augment was resolved as a side effect of setting module implemented when |
| 4339 | * resolving augment schema nodeid, so we are done here */ |
| 4340 | return 0; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4341 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4342 | if (!aug_target && mod->implemented) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4343 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4344 | return EXIT_FAILURE; |
| 4345 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4346 | /* check that we want to connect augment into its target */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4347 | if (!mod->implemented) { |
| 4348 | /* it must be augment only to the same module, |
| 4349 | * otherwise we do not apply augment in not-implemented |
| 4350 | * module. If the module is set to be implemented in future, |
| 4351 | * the augment is being resolved and checked again */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4352 | if (!aug_target) { |
| 4353 | /* target was not even resolved */ |
| 4354 | return EXIT_SUCCESS; |
| 4355 | } |
| 4356 | /* target was resolved, but it may refer another module */ |
| 4357 | for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4358 | if (lys_node_module(sub) != mod) { |
| 4359 | /* this is not an implemented module and the augment |
| 4360 | * target some other module, so avoid its connecting |
| 4361 | * to the target */ |
| 4362 | return EXIT_SUCCESS; |
| 4363 | } |
| 4364 | } |
| 4365 | } |
| 4366 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4367 | if (!aug->child) { |
| 4368 | /* nothing to do */ |
| 4369 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4370 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4371 | } |
| 4372 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4373 | /* check for mandatory nodes - if the target node is in another module |
| 4374 | * the added nodes cannot be mandatory |
| 4375 | */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4376 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target)) |
Radek Krejci | df24cbe | 2016-11-08 11:55:51 +0100 | [diff] [blame] | 4377 | && (rc = lyp_check_mandatory_augment(aug, aug_target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4378 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4379 | } |
| 4380 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4381 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4382 | if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4383 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4384 | 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] | 4385 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4386 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4387 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4388 | return -1; |
| 4389 | } |
| 4390 | } |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4391 | } else if (aug_target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4392 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4393 | 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] | 4394 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4395 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4396 | strnodetype(aug_target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4397 | return -1; |
| 4398 | } |
| 4399 | } |
| 4400 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4401 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4402 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug_target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4403 | return -1; |
| 4404 | } |
| 4405 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4406 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4407 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4408 | if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4409 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4410 | } |
| 4411 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4412 | |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4413 | /* finally reconnect augmenting data into the target - add them to the target child list, |
| 4414 | * by setting aug->target we know the augment is fully resolved now */ |
| 4415 | aug->target = (struct lys_node *)aug_target; |
| 4416 | if (aug->target->child) { |
| 4417 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 4418 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
| 4419 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 4420 | aug->child->prev = sub; /* finish connecting of both child lists */ |
| 4421 | } else { |
| 4422 | aug->target->child = aug->child; |
| 4423 | } |
| 4424 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4425 | /* inherit config information from actual parent */ |
| 4426 | for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4427 | clear_config = (parent) ? 1 : 0; |
| 4428 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4429 | if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, unres)) { |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4430 | return -1; |
| 4431 | } |
| 4432 | } |
| 4433 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4434 | success: |
| 4435 | if (mod->implemented) { |
| 4436 | /* make target modules also implemented */ |
| 4437 | for (sub = aug->target; sub; sub = lys_parent(sub)) { |
| 4438 | if (lys_set_implemented(sub->module)) { |
| 4439 | return -1; |
| 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4444 | return EXIT_SUCCESS; |
| 4445 | } |
| 4446 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4447 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4448 | * @brief Resolve (find) choice default case. Does not log. |
| 4449 | * |
| 4450 | * @param[in] choic Choice to use. |
| 4451 | * @param[in] dflt Name of the default case. |
| 4452 | * |
| 4453 | * @return Pointer to the default node or NULL. |
| 4454 | */ |
| 4455 | static struct lys_node * |
| 4456 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4457 | { |
| 4458 | struct lys_node *child, *ret; |
| 4459 | |
| 4460 | LY_TREE_FOR(choic->child, child) { |
| 4461 | if (child->nodetype == LYS_USES) { |
| 4462 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4463 | if (ret) { |
| 4464 | return ret; |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | 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] | 4469 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4470 | return child; |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | return NULL; |
| 4475 | } |
| 4476 | |
| 4477 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4478 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4479 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4480 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4481 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4482 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4483 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4484 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4485 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4486 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4487 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4488 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4489 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4490 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4491 | struct lys_node_leaflist *llist; |
| 4492 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4493 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4494 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4495 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4496 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4497 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4498 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4499 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4500 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 4501 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4502 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4503 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4504 | if (!uses->grp->child) { |
| 4505 | /* grouping without children, warning was already displayed */ |
| 4506 | return EXIT_SUCCESS; |
| 4507 | } |
| 4508 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4509 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4510 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4511 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, uses->nacm, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4512 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4513 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 4514 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4515 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4516 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4517 | /* test the name of siblings */ |
| 4518 | LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 4519 | 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] | 4520 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4521 | } |
| 4522 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4523 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4524 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4525 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4526 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4527 | if (uses->refine_size) { |
| 4528 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
| 4529 | if (!refine_nodes) { |
| 4530 | LOGMEM; |
| 4531 | goto fail; |
| 4532 | } |
| 4533 | } |
| 4534 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4535 | /* apply refines */ |
| 4536 | for (i = 0; i < uses->refine_size; i++) { |
| 4537 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4538 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 4539 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4540 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4541 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4542 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4543 | } |
| 4544 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4545 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4546 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 4547 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4548 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4549 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4550 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4551 | |
| 4552 | /* description on any nodetype */ |
| 4553 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4554 | lydict_remove(ctx, node->dsc); |
| 4555 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* reference on any nodetype */ |
| 4559 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4560 | lydict_remove(ctx, node->ref); |
| 4561 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4562 | } |
| 4563 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4564 | /* config on any nodetype, |
| 4565 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4566 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4567 | node->flags &= ~LYS_CONFIG_MASK; |
| 4568 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4572 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4573 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4574 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4575 | leaf = (struct lys_node_leaf *)node; |
| 4576 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4577 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4578 | lydict_remove(ctx, leaf->dflt); |
| 4579 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4580 | |
| 4581 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4582 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4583 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4584 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4585 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4586 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4587 | /* leaf-list */ |
| 4588 | llist = (struct lys_node_leaflist *)node; |
| 4589 | |
| 4590 | /* remove complete set of defaults in target */ |
| 4591 | for (i = 0; i < llist->dflt_size; i++) { |
| 4592 | lydict_remove(ctx, llist->dflt[i]); |
| 4593 | } |
| 4594 | free(llist->dflt); |
| 4595 | |
| 4596 | /* copy the default set from refine */ |
| 4597 | llist->dflt_size = rfn->dflt_size; |
| 4598 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 4599 | for (i = 0; i < llist->dflt_size; i++) { |
| 4600 | llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0); |
| 4601 | } |
| 4602 | |
| 4603 | /* check default value */ |
| 4604 | for (i = 0; i < llist->dflt_size; i++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4605 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 4606 | (struct lys_node *)(&llist->dflt[i])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4607 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4608 | } |
| 4609 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4614 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4615 | if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4616 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4617 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4618 | |
| 4619 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4620 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4621 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4622 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4623 | /* check if node has default value */ |
| 4624 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
| 4625 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 4626 | goto fail; |
| 4627 | } |
| 4628 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
| 4629 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 4630 | goto fail; |
| 4631 | } |
| 4632 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4636 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4637 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4638 | ((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] | 4639 | } |
| 4640 | |
| 4641 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4642 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4643 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4644 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4645 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4646 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4647 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4648 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4649 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4650 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4651 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4652 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4653 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4654 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4659 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4660 | switch (node->nodetype) { |
| 4661 | case LYS_LEAF: |
| 4662 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4663 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4664 | break; |
| 4665 | case LYS_LEAFLIST: |
| 4666 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4667 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4668 | break; |
| 4669 | case LYS_LIST: |
| 4670 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4671 | old_must = &((struct lys_node_list *)node)->must; |
| 4672 | break; |
| 4673 | case LYS_CONTAINER: |
| 4674 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4675 | old_must = &((struct lys_node_container *)node)->must; |
| 4676 | break; |
| 4677 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4678 | case LYS_ANYDATA: |
| 4679 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4680 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4681 | break; |
| 4682 | default: |
| 4683 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4684 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | size = *old_size + rfn->must_size; |
| 4688 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 4689 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4690 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4691 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4692 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4693 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
| 4694 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4695 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4696 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4697 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4698 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4699 | } |
| 4700 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4701 | *old_must = must; |
| 4702 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4703 | |
| 4704 | /* check XPath dependencies again */ |
| 4705 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4706 | goto fail; |
| 4707 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4708 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4709 | |
| 4710 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4711 | if (rfn->iffeature_size) { |
| 4712 | old_size = &node->iffeature_size; |
| 4713 | old_iff = &node->iffeature; |
| 4714 | |
| 4715 | size = *old_size + rfn->iffeature_size; |
| 4716 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
| 4717 | if (!iff) { |
| 4718 | LOGMEM; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4719 | goto fail; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4720 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4721 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4722 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4723 | if (usize1) { |
| 4724 | /* there is something to duplicate */ |
| 4725 | /* duplicate compiled expression */ |
| 4726 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4727 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4728 | 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] | 4729 | |
| 4730 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4731 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
| 4732 | 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] | 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | *old_iff = iff; |
| 4737 | *old_size = size; |
| 4738 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | /* apply augments */ |
| 4742 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4743 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4744 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4745 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4746 | } |
| 4747 | } |
| 4748 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4749 | /* check refines */ |
| 4750 | for (i = 0; i < uses->refine_size; i++) { |
| 4751 | node = refine_nodes[i]; |
| 4752 | rfn = &uses->refine[i]; |
| 4753 | |
| 4754 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4755 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4756 | 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] | 4757 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4758 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4759 | (rfn->flags & LYS_CONFIG_W)) { |
| 4760 | /* setting config true under config false is prohibited */ |
| 4761 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4762 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4763 | "changing config from 'false' to 'true' is prohibited while " |
| 4764 | "the target's parent is still config 'false'."); |
| 4765 | goto fail; |
| 4766 | } |
| 4767 | |
| 4768 | /* inherit config change to the target children */ |
| 4769 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4770 | if (rfn->flags & LYS_CONFIG_W) { |
| 4771 | if (iter->flags & LYS_CONFIG_SET) { |
| 4772 | /* config is set explicitely, go to next sibling */ |
| 4773 | next = NULL; |
| 4774 | goto nextsibling; |
| 4775 | } |
| 4776 | } else { /* LYS_CONFIG_R */ |
| 4777 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4778 | /* error - we would have config data under status data */ |
| 4779 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 4780 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4781 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4782 | "has still a children with explicit config 'true'."); |
| 4783 | goto fail; |
| 4784 | } |
| 4785 | } |
| 4786 | /* change config */ |
| 4787 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4788 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4789 | |
| 4790 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4791 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4792 | next = NULL; |
| 4793 | } else { |
| 4794 | next = iter->child; |
| 4795 | } |
| 4796 | nextsibling: |
| 4797 | if (!next) { |
| 4798 | /* try siblings */ |
| 4799 | next = iter->next; |
| 4800 | } |
| 4801 | while (!next) { |
| 4802 | /* parent is already processed, go to its sibling */ |
| 4803 | iter = lys_parent(iter); |
| 4804 | |
| 4805 | /* no siblings, go back through parents */ |
| 4806 | if (iter == node) { |
| 4807 | /* we are done, no next element to process */ |
| 4808 | break; |
| 4809 | } |
| 4810 | next = iter->next; |
| 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4816 | if (rfn->dflt_size) { |
| 4817 | if (node->nodetype == LYS_CHOICE) { |
| 4818 | /* choice */ |
| 4819 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4820 | rfn->dflt[0]); |
| 4821 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4822 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4823 | goto fail; |
| 4824 | } |
| 4825 | if (lyp_check_mandatory_choice(node)) { |
| 4826 | goto fail; |
| 4827 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | /* min/max-elements on list or leaf-list */ |
| 4832 | if (node->nodetype == LYS_LIST) { |
| 4833 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
| 4834 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4835 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4836 | goto fail; |
| 4837 | } |
| 4838 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4839 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
| 4840 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4841 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 4842 | goto fail; |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4847 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4848 | if (node->nodetype == LYS_LEAFLIST) { |
| 4849 | llist = (struct lys_node_leaflist *)node; |
| 4850 | if (llist->dflt_size && llist->min) { |
| 4851 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4852 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4853 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4854 | goto fail; |
| 4855 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4856 | } else if (node->nodetype == LYS_LEAF) { |
| 4857 | leaf = (struct lys_node_leaf *)node; |
| 4858 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
| 4859 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4860 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4861 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4862 | goto fail; |
| 4863 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4864 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4865 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4866 | /* 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] | 4867 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4868 | for (parent = node->parent; |
| 4869 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4870 | parent = parent->parent) { |
| 4871 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4872 | /* stop also on presence containers */ |
| 4873 | break; |
| 4874 | } |
| 4875 | } |
| 4876 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4877 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4878 | if (lyp_check_mandatory_choice(parent)) { |
| 4879 | goto fail; |
| 4880 | } |
| 4881 | } |
| 4882 | } |
| 4883 | } |
| 4884 | free(refine_nodes); |
| 4885 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4886 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4887 | |
| 4888 | fail: |
| 4889 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4890 | lys_node_free(iter, NULL, 0); |
| 4891 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4892 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4893 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4894 | } |
| 4895 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4896 | static int |
| 4897 | identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
| 4898 | { |
| 4899 | int i; |
| 4900 | |
| 4901 | assert(der && base); |
| 4902 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4903 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4904 | /* create a set for backlinks if it does not exist */ |
| 4905 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4906 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4907 | /* store backlink */ |
| 4908 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4909 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4910 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4911 | for (i = 0; i < base->base_size; i++) { |
| 4912 | if (identity_backlink_update(der, base->base[i])) { |
| 4913 | return EXIT_FAILURE; |
| 4914 | } |
| 4915 | } |
| 4916 | |
| 4917 | return EXIT_SUCCESS; |
| 4918 | } |
| 4919 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4920 | /** |
| 4921 | * @brief Resolve base identity recursively. Does not log. |
| 4922 | * |
| 4923 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4924 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4925 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4926 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4927 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4928 | * @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] | 4929 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4930 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4931 | 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] | 4932 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4933 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4934 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4935 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4936 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4937 | assert(ret); |
| 4938 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4939 | /* search module */ |
| 4940 | for (i = 0; i < module->ident_size; i++) { |
| 4941 | if (!strcmp(basename, module->ident[i].name)) { |
| 4942 | |
| 4943 | if (!ident) { |
| 4944 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4945 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4946 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4947 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4948 | } |
| 4949 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4950 | base = &module->ident[i]; |
| 4951 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4956 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 4957 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 4958 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4959 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4960 | if (!ident) { |
| 4961 | *ret = &module->inc[j].submodule->ident[i]; |
| 4962 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4963 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4964 | |
| 4965 | base = &module->inc[j].submodule->ident[i]; |
| 4966 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | } |
| 4970 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 4971 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4972 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4973 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4974 | /* is it already completely resolved? */ |
| 4975 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 4976 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4977 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 4978 | |
| 4979 | /* simple check for circular reference, |
| 4980 | * the complete check is done as a side effect of using only completely |
| 4981 | * resolved identities (previous check of unres content) */ |
| 4982 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 4983 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 4984 | 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] | 4985 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4986 | } |
| 4987 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 4988 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4989 | } |
| 4990 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4991 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 4992 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4993 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 4994 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4995 | } |
| 4996 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 4997 | /* base not found (maybe a forward reference) */ |
| 4998 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4999 | } |
| 5000 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5001 | /** |
| 5002 | * @brief Resolve base identity. Logs directly. |
| 5003 | * |
| 5004 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5005 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5006 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5007 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5008 | * @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] | 5009 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5010 | * @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] | 5011 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5012 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5013 | 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] | 5014 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5015 | { |
| 5016 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5017 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5018 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5019 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5020 | struct lys_module *mod; |
| 5021 | |
| 5022 | assert((ident && !type) || (!ident && type)); |
| 5023 | |
| 5024 | if (!type) { |
| 5025 | /* have ident to resolve */ |
| 5026 | ret = ⌖ |
| 5027 | flags = ident->flags; |
| 5028 | mod = ident->module; |
| 5029 | } else { |
| 5030 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5031 | ++type->info.ident.count; |
| 5032 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
| 5033 | if (!type->info.ident.ref) { |
| 5034 | LOGMEM; |
| 5035 | return -1; |
| 5036 | } |
| 5037 | |
| 5038 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5039 | flags = type->parent->flags; |
| 5040 | mod = type->parent->module; |
| 5041 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5042 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5043 | |
| 5044 | /* search for the base identity */ |
| 5045 | name = strchr(basename, ':'); |
| 5046 | if (name) { |
| 5047 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5048 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5049 | name++; |
| 5050 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5051 | 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] | 5052 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5053 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5054 | } |
| 5055 | } else { |
| 5056 | name = basename; |
| 5057 | } |
| 5058 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5059 | /* get module where to search */ |
| 5060 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5061 | if (!module) { |
| 5062 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5063 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5064 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5065 | } |
| 5066 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5067 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5068 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5069 | if (!rc) { |
| 5070 | assert(*ret); |
| 5071 | |
| 5072 | /* check status */ |
| 5073 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5074 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5075 | rc = -1; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5076 | } else { |
| 5077 | if (ident) { |
| 5078 | ident->base[ident->base_size++] = *ret; |
| 5079 | |
| 5080 | /* maintain backlinks to the derived identities */ |
| 5081 | rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS; |
| 5082 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5083 | } |
| 5084 | } else if (rc == EXIT_FAILURE) { |
| 5085 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5086 | if (type) { |
| 5087 | --type->info.ident.count; |
| 5088 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5089 | } |
| 5090 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5091 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5092 | } |
| 5093 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5094 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5095 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5096 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5097 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5098 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5099 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5100 | * |
| 5101 | * @return Pointer to the identity resolvent, NULL on error. |
| 5102 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5103 | struct lys_ident * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5104 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5105 | { |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5106 | const char *mod_name, *name, *mod_name_iter; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5107 | int mod_name_len, rc, i; |
| 5108 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5109 | struct lys_ident *der, *cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5110 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5111 | assert(type && ident_name && node); |
| 5112 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5113 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5114 | return NULL; |
| 5115 | } |
| 5116 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 5117 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL); |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5118 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5119 | 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] | 5120 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5121 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5122 | 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] | 5123 | return NULL; |
| 5124 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5125 | if (!mod_name) { |
| 5126 | /* no prefix, identity must be defined in the same module as node */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5127 | mod_name = lys_main_module(node->schema->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5128 | mod_name_len = strlen(mod_name); |
| 5129 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5130 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5131 | /* go through all the bases in all the derived types */ |
| 5132 | while (type->der) { |
| 5133 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5134 | cur = type->info.ident.ref[i]; |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5135 | mod_name_iter = lys_main_module(cur->module)->name; |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5136 | if (!strcmp(cur->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5137 | !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) { |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5138 | goto match; |
| 5139 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5140 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5141 | if (cur->der) { |
| 5142 | /* there are also some derived identities */ |
| 5143 | for (u = 0; u < cur->der->number; u++) { |
| 5144 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5145 | mod_name_iter = lys_main_module(der->module)->name; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5146 | if (!strcmp(der->name, name) && |
Radek Krejci | 639491e | 2016-12-05 13:30:42 +0100 | [diff] [blame] | 5147 | !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5148 | /* we have match */ |
| 5149 | cur = der; |
| 5150 | goto match; |
| 5151 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5152 | } |
| 5153 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5154 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5155 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5156 | } |
| 5157 | |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5158 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5159 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5160 | |
| 5161 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5162 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5163 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5164 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5165 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.", cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5166 | return NULL; |
| 5167 | } |
| 5168 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5169 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5172 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5173 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5174 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5175 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5176 | * @param[in] unres Specific unres item. |
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 | * @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] | 5179 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5180 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5181 | 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] | 5182 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5183 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5184 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5185 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5186 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 5187 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 5188 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 5189 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 5190 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5191 | 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] | 5192 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5193 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5194 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5195 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5196 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5197 | return -1; |
| 5198 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5199 | 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] | 5200 | return -1; |
| 5201 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5202 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5203 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 5204 | * (and smaller flags - it uses only a limited set of flags) |
| 5205 | */ |
| 5206 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5207 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5208 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5209 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5210 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5211 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5212 | } |
| 5213 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5214 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5215 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5216 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5217 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5218 | } else { |
| 5219 | /* instantiate grouping only when it is completely resolved */ |
| 5220 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5221 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5222 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5223 | return EXIT_FAILURE; |
| 5224 | } |
| 5225 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5226 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5227 | if (!rc) { |
| 5228 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5229 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5230 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5231 | LOGINT; |
| 5232 | return -1; |
| 5233 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5234 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5235 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5236 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5237 | |
| 5238 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5239 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5240 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5241 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5242 | return -1; |
| 5243 | } |
| 5244 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5245 | return EXIT_SUCCESS; |
| 5246 | } |
| 5247 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5248 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5249 | } |
| 5250 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5251 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5252 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5253 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5254 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5255 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5256 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5257 | * @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] | 5258 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5259 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5260 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5261 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5262 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5263 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5264 | |
| 5265 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5266 | if (!list->child) { |
| 5267 | /* no child, possible forward reference */ |
| 5268 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5269 | return EXIT_FAILURE; |
| 5270 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5271 | /* get the key name */ |
| 5272 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5273 | len = value - keys_str; |
| 5274 | while (isspace(value[0])) { |
| 5275 | value++; |
| 5276 | } |
| 5277 | } else { |
| 5278 | len = strlen(keys_str); |
| 5279 | } |
| 5280 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 5281 | rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5282 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5283 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5284 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5285 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5286 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5287 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5288 | /* check_key logs */ |
| 5289 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5290 | } |
| 5291 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5292 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5293 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5294 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5295 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5296 | return -1; |
| 5297 | } |
| 5298 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5299 | /* prepare for next iteration */ |
| 5300 | while (value && isspace(value[0])) { |
| 5301 | value++; |
| 5302 | } |
| 5303 | keys_str = value; |
| 5304 | } |
| 5305 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5306 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5307 | } |
| 5308 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5309 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5310 | * @brief Resolve (check) all must conditions of \p node. |
| 5311 | * Logs directly. |
| 5312 | * |
| 5313 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5314 | * @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] | 5315 | * |
| 5316 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5317 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5318 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5319 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5320 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5321 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5322 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5323 | struct lys_restr *must; |
| 5324 | struct lyxp_set set; |
| 5325 | |
| 5326 | assert(node); |
| 5327 | memset(&set, 0, sizeof set); |
| 5328 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5329 | if (inout_parent) { |
| 5330 | for (schema = lys_parent(node->schema); |
| 5331 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5332 | schema = lys_parent(schema)); |
| 5333 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5334 | LOGINT; |
| 5335 | return -1; |
| 5336 | } |
| 5337 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5338 | must = ((struct lys_node_inout *)schema)->must; |
| 5339 | |
| 5340 | /* context node is the RPC/action */ |
| 5341 | node = node->parent; |
| 5342 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5343 | LOGINT; |
| 5344 | return -1; |
| 5345 | } |
| 5346 | } else { |
| 5347 | switch (node->schema->nodetype) { |
| 5348 | case LYS_CONTAINER: |
| 5349 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5350 | must = ((struct lys_node_container *)node->schema)->must; |
| 5351 | break; |
| 5352 | case LYS_LEAF: |
| 5353 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5354 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5355 | break; |
| 5356 | case LYS_LEAFLIST: |
| 5357 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5358 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5359 | break; |
| 5360 | case LYS_LIST: |
| 5361 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5362 | must = ((struct lys_node_list *)node->schema)->must; |
| 5363 | break; |
| 5364 | case LYS_ANYXML: |
| 5365 | case LYS_ANYDATA: |
| 5366 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5367 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5368 | break; |
| 5369 | case LYS_NOTIF: |
| 5370 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5371 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5372 | break; |
| 5373 | default: |
| 5374 | must_size = 0; |
| 5375 | break; |
| 5376 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5380 | 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] | 5381 | return -1; |
| 5382 | } |
| 5383 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5384 | 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] | 5385 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5386 | if (!set.val.bool) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5387 | if (ignore_fail) { |
| 5388 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5389 | } else { |
| 5390 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5391 | if (must[i].emsg) { |
| 5392 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 5393 | } |
| 5394 | if (must[i].eapptag) { |
| 5395 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5396 | } |
| 5397 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5398 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5402 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5403 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5404 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5405 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5406 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5407 | * |
| 5408 | * @param[in] schema Schema node with the when condition. |
| 5409 | * @param[out] ctx_snode When schema context node. |
| 5410 | * @param[out] ctx_snode_type Schema context node type. |
| 5411 | */ |
| 5412 | void |
| 5413 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5414 | { |
| 5415 | const struct lys_node *sparent; |
| 5416 | |
| 5417 | /* find a not schema-only node */ |
| 5418 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5419 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5420 | if (schema->nodetype == LYS_AUGMENT) { |
| 5421 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5422 | } else { |
| 5423 | sparent = schema->parent; |
| 5424 | } |
| 5425 | if (!sparent) { |
| 5426 | /* context node is the document root (fake root in our case) */ |
| 5427 | if (schema->flags & LYS_CONFIG_W) { |
| 5428 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5429 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5430 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5431 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5432 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5433 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5434 | break; |
| 5435 | } |
| 5436 | schema = sparent; |
| 5437 | } |
| 5438 | |
| 5439 | *ctx_snode = (struct lys_node *)schema; |
| 5440 | } |
| 5441 | |
| 5442 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5443 | * @brief Resolve (find) when condition context node. Does not log. |
| 5444 | * |
| 5445 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5446 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5447 | * @param[out] ctx_node Context node. |
| 5448 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5449 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5450 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5451 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5452 | static int |
| 5453 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5454 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5455 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5456 | struct lyd_node *parent; |
| 5457 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5458 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5459 | uint16_t i, data_depth, schema_depth; |
| 5460 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5461 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5462 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5463 | if (node_type == LYXP_NODE_ELEM) { |
| 5464 | /* standard element context node */ |
| 5465 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5466 | for (sparent = schema, schema_depth = 0; |
| 5467 | sparent; |
| 5468 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5469 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5470 | ++schema_depth; |
| 5471 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5472 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5473 | if (data_depth < schema_depth) { |
| 5474 | return -1; |
| 5475 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5476 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5477 | /* find the corresponding data node */ |
| 5478 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5479 | node = node->parent; |
| 5480 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5481 | if (node->schema != schema) { |
| 5482 | return -1; |
| 5483 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5484 | } else { |
| 5485 | /* root context node */ |
| 5486 | while (node->parent) { |
| 5487 | node = node->parent; |
| 5488 | } |
| 5489 | while (node->prev->next) { |
| 5490 | node = node->prev; |
| 5491 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5492 | } |
| 5493 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5494 | *ctx_node = node; |
| 5495 | *ctx_node_type = node_type; |
| 5496 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5497 | } |
| 5498 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5499 | /** |
| 5500 | * @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] | 5501 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5502 | * |
| 5503 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5504 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5505 | * it is moved to point to another sibling still in the original tree. |
| 5506 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5507 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5508 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5509 | * Ordering may change, but there will be no semantic change. |
| 5510 | * |
| 5511 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5512 | */ |
| 5513 | static int |
| 5514 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5515 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5516 | { |
| 5517 | struct lyd_node *next, *elem; |
| 5518 | |
| 5519 | switch (snode->nodetype) { |
| 5520 | case LYS_AUGMENT: |
| 5521 | case LYS_USES: |
| 5522 | case LYS_CHOICE: |
| 5523 | case LYS_CASE: |
| 5524 | LY_TREE_FOR(snode->child, snode) { |
| 5525 | if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
| 5526 | return -1; |
| 5527 | } |
| 5528 | } |
| 5529 | break; |
| 5530 | case LYS_CONTAINER: |
| 5531 | case LYS_LIST: |
| 5532 | case LYS_LEAF: |
| 5533 | case LYS_LEAFLIST: |
| 5534 | case LYS_ANYXML: |
| 5535 | case LYS_ANYDATA: |
| 5536 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5537 | if (elem->schema == snode) { |
| 5538 | |
| 5539 | if (elem == *ctx_node) { |
| 5540 | /* We are going to unlink our context node! This normally cannot happen, |
| 5541 | * but we use normal top-level data nodes for faking a document root node, |
| 5542 | * so if this is the context node, we just use the next top-level node. |
| 5543 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5544 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5545 | * lyxp_eval() can handle this special situation. |
| 5546 | */ |
| 5547 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5548 | LOGINT; |
| 5549 | return -1; |
| 5550 | } |
| 5551 | |
| 5552 | if (elem->prev == elem) { |
| 5553 | /* unlinking last top-level element, use an empty data tree */ |
| 5554 | *ctx_node = NULL; |
| 5555 | } else { |
| 5556 | /* in this case just use the previous/last top-level data node */ |
| 5557 | *ctx_node = elem->prev; |
| 5558 | } |
| 5559 | } else if (elem == *node) { |
| 5560 | /* We are going to unlink the currently processed node. This does not matter that |
| 5561 | * much, but we would lose access to the original data tree, so just move our |
| 5562 | * pointer somewhere still inside it. |
| 5563 | */ |
| 5564 | if ((*node)->prev != *node) { |
| 5565 | *node = (*node)->prev; |
| 5566 | } else { |
| 5567 | /* the processed node with sibings were all unlinked, oh well */ |
| 5568 | *node = NULL; |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | /* temporarily unlink the node */ |
| 5573 | lyd_unlink(elem); |
| 5574 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5575 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5576 | LOGINT; |
| 5577 | return -1; |
| 5578 | } |
| 5579 | } else { |
| 5580 | *unlinked_nodes = elem; |
| 5581 | } |
| 5582 | |
| 5583 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5584 | /* there can be only one instance */ |
| 5585 | break; |
| 5586 | } |
| 5587 | } |
| 5588 | } |
| 5589 | break; |
| 5590 | default: |
| 5591 | LOGINT; |
| 5592 | return -1; |
| 5593 | } |
| 5594 | |
| 5595 | return EXIT_SUCCESS; |
| 5596 | } |
| 5597 | |
| 5598 | /** |
| 5599 | * @brief Relink the unlinked nodes back. |
| 5600 | * |
| 5601 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5602 | * we simply need a sibling from the original data tree. |
| 5603 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5604 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5605 | * or the sibling of \p unlinked_nodes. |
| 5606 | * |
| 5607 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5608 | */ |
| 5609 | static int |
| 5610 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5611 | { |
| 5612 | struct lyd_node *elem; |
| 5613 | |
| 5614 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | de1feeb | 2016-12-20 14:48:42 +0100 | [diff] [blame] | 5615 | lyd_unlink(elem); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5616 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5617 | if (lyd_insert(node, elem)) { |
| 5618 | return -1; |
| 5619 | } |
| 5620 | } else { |
| 5621 | if (lyd_insert_after(node, elem)) { |
| 5622 | return -1; |
| 5623 | } |
| 5624 | } |
| 5625 | } |
| 5626 | |
| 5627 | return EXIT_SUCCESS; |
| 5628 | } |
| 5629 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5630 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5631 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5632 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5633 | int ret = 0; |
| 5634 | uint8_t must_size; |
| 5635 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5636 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5637 | assert(node); |
| 5638 | |
| 5639 | schema = node->schema; |
| 5640 | |
| 5641 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5642 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5643 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5644 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5645 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5646 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5647 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5648 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5649 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5650 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5651 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5652 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5653 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5654 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5655 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5656 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5657 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5658 | break; |
| 5659 | case LYS_NOTIF: |
| 5660 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5661 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5662 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5663 | must_size = 0; |
| 5664 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5665 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5666 | |
| 5667 | if (must_size) { |
| 5668 | ++ret; |
| 5669 | } |
| 5670 | |
| 5671 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5672 | if (!node->prev->next) { |
| 5673 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5674 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5675 | ret += 0x2; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5683 | 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] | 5684 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5685 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5686 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5687 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5688 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5689 | 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] | 5690 | return 1; |
| 5691 | } |
| 5692 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5693 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5694 | goto check_augment; |
| 5695 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5696 | while (parent) { |
| 5697 | /* stop conditions */ |
| 5698 | if (!mode) { |
| 5699 | /* stop on node that can be instantiated in data tree */ |
| 5700 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5701 | break; |
| 5702 | } |
| 5703 | } else { |
| 5704 | /* stop on the specified node */ |
| 5705 | if (parent == stop) { |
| 5706 | break; |
| 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5711 | return 1; |
| 5712 | } |
| 5713 | check_augment: |
| 5714 | |
| 5715 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5716 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5717 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5718 | } |
| 5719 | parent = lys_parent(parent); |
| 5720 | } |
| 5721 | |
| 5722 | return 0; |
| 5723 | } |
| 5724 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5725 | /** |
| 5726 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5727 | * Logs directly. |
| 5728 | * |
| 5729 | * @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] | 5730 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5731 | * @return |
| 5732 | * -1 - error, ly_errno is set |
| 5733 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5734 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5735 | * 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] | 5736 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5737 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5738 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5739 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5740 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5741 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5742 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5743 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5744 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5745 | |
| 5746 | assert(node); |
| 5747 | memset(&set, 0, sizeof set); |
| 5748 | |
| 5749 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5750 | /* make the node dummy for the evaluation */ |
| 5751 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5752 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 5753 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5754 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5755 | if (rc) { |
| 5756 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5757 | 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] | 5758 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5759 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5762 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5763 | 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] | 5764 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5765 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5766 | if (ignore_fail) { |
| 5767 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 5768 | ((struct lys_node_container *)node->schema)->when->cond); |
| 5769 | } else { |
| 5770 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 5771 | goto cleanup; |
| 5772 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5773 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5774 | |
| 5775 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5776 | 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] | 5777 | } |
| 5778 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5779 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5780 | goto check_augment; |
| 5781 | |
| 5782 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5783 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5784 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5785 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5786 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5787 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5788 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5789 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5790 | } |
| 5791 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5792 | |
| 5793 | unlinked_nodes = NULL; |
| 5794 | /* we do not want our node pointer to change */ |
| 5795 | tmp_node = node; |
| 5796 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5797 | if (rc) { |
| 5798 | goto cleanup; |
| 5799 | } |
| 5800 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5801 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 5802 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5803 | |
| 5804 | if (unlinked_nodes && ctx_node) { |
| 5805 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5806 | rc = -1; |
| 5807 | goto cleanup; |
| 5808 | } |
| 5809 | } |
| 5810 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5811 | if (rc) { |
| 5812 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5813 | 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] | 5814 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5815 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5816 | } |
| 5817 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5818 | 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] | 5819 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5820 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5821 | if (ignore_fail) { |
| 5822 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 5823 | ((struct lys_node_uses *)sparent)->when->cond); |
| 5824 | } else { |
| 5825 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 5826 | goto cleanup; |
| 5827 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5828 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5829 | |
| 5830 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5831 | 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] | 5832 | } |
| 5833 | |
| 5834 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5835 | 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] | 5836 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5837 | 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] | 5838 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5839 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5840 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5841 | } |
| 5842 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5843 | |
| 5844 | unlinked_nodes = NULL; |
| 5845 | tmp_node = node; |
| 5846 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 5847 | if (rc) { |
| 5848 | goto cleanup; |
| 5849 | } |
| 5850 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5851 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 5852 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5853 | |
| 5854 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 5855 | * so the tree did not actually change and there is nothing for us to do |
| 5856 | */ |
| 5857 | if (unlinked_nodes && ctx_node) { |
| 5858 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 5859 | rc = -1; |
| 5860 | goto cleanup; |
| 5861 | } |
| 5862 | } |
| 5863 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5864 | if (rc) { |
| 5865 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5866 | 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] | 5867 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5868 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5871 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5872 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5873 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5874 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5875 | if (ignore_fail) { |
| 5876 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 5877 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 5878 | } else { |
| 5879 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 5880 | goto cleanup; |
| 5881 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5882 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5883 | |
| 5884 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5885 | 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] | 5886 | } |
| 5887 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5888 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5889 | } |
| 5890 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5891 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5892 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5893 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5894 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5895 | 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] | 5896 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5897 | if (result) { |
| 5898 | if (node->when_status & LYD_WHEN_TRUE) { |
| 5899 | *result = 1; |
| 5900 | } else { |
| 5901 | *result = 0; |
| 5902 | } |
| 5903 | } |
| 5904 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5905 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5906 | } |
| 5907 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 5908 | static int |
| 5909 | check_leafref_features(struct lys_type *type) |
| 5910 | { |
| 5911 | struct lys_node *iter; |
| 5912 | struct ly_set *src_parents, *trg_parents, *features; |
| 5913 | unsigned int i, j, size, x; |
| 5914 | int ret = EXIT_SUCCESS; |
| 5915 | |
| 5916 | assert(type->parent); |
| 5917 | |
| 5918 | src_parents = ly_set_new(); |
| 5919 | trg_parents = ly_set_new(); |
| 5920 | features = ly_set_new(); |
| 5921 | |
| 5922 | /* get parents chain of source (leafref) */ |
| 5923 | for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) { |
| 5924 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5925 | continue; |
| 5926 | } |
| 5927 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 5928 | } |
| 5929 | /* get parents chain of target */ |
| 5930 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) { |
| 5931 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 5932 | continue; |
| 5933 | } |
| 5934 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 5935 | } |
| 5936 | |
| 5937 | /* compare the features used in if-feature statements in the rest of both |
| 5938 | * chains of parents. The set of features used for target must be a subset |
| 5939 | * of features used for the leafref. This is not a perfect, we should compare |
| 5940 | * the truth tables but it could require too much resources, so we simplify that */ |
| 5941 | for (i = 0; i < src_parents->number; i++) { |
| 5942 | iter = src_parents->set.s[i]; /* shortcut */ |
| 5943 | if (!iter->iffeature_size) { |
| 5944 | continue; |
| 5945 | } |
| 5946 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5947 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5948 | for (; size; size--) { |
| 5949 | if (!iter->iffeature[j].features[size - 1]) { |
| 5950 | /* not yet resolved feature, postpone this check */ |
| 5951 | ret = EXIT_FAILURE; |
| 5952 | goto cleanup; |
| 5953 | } |
| 5954 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 5955 | } |
| 5956 | } |
| 5957 | } |
| 5958 | x = features->number; |
| 5959 | for (i = 0; i < trg_parents->number; i++) { |
| 5960 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 5961 | if (!iter->iffeature_size) { |
| 5962 | continue; |
| 5963 | } |
| 5964 | for (j = 0; j < iter->iffeature_size; j++) { |
| 5965 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 5966 | for (; size; size--) { |
| 5967 | if (!iter->iffeature[j].features[size - 1]) { |
| 5968 | /* not yet resolved feature, postpone this check */ |
| 5969 | ret = EXIT_FAILURE; |
| 5970 | goto cleanup; |
| 5971 | } |
| 5972 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 5973 | /* the feature is not present in features set of target's parents chain */ |
| 5974 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 5975 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, type->parent, |
| 5976 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 5977 | iter->iffeature[j].features[size - 1]->name); |
| 5978 | ret = -1; |
| 5979 | goto cleanup; |
| 5980 | } |
| 5981 | } |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | cleanup: |
| 5986 | ly_set_free(features); |
| 5987 | ly_set_free(src_parents); |
| 5988 | ly_set_free(trg_parents); |
| 5989 | |
| 5990 | return ret; |
| 5991 | } |
| 5992 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5993 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5994 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5995 | * |
| 5996 | * @param[in] mod Main module. |
| 5997 | * @param[in] item Item to resolve. Type determined by \p type. |
| 5998 | * @param[in] type Type of the unresolved item. |
| 5999 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6000 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6001 | * |
| 6002 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6003 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6004 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6005 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6006 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6007 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6008 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6009 | int rc = -1, has_str = 0, tpdf_flag = 0, i, k; |
| 6010 | unsigned int j; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6011 | struct lys_node *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6012 | const char *expr; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6013 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6014 | struct ly_set *refs, *procs; |
| 6015 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6016 | struct lys_ident *ident; |
| 6017 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6018 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6019 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6020 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6021 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6022 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6023 | |
| 6024 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6025 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6026 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6027 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6028 | ident = item; |
| 6029 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6030 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6031 | break; |
| 6032 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6033 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6034 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6035 | stype = item; |
| 6036 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6037 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6038 | break; |
| 6039 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6040 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6041 | stype = item; |
| 6042 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6043 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 6044 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 6045 | * know it via tpdf_flag */ |
| 6046 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6047 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 6048 | node = (struct lys_node *)stype->parent; |
| 6049 | } |
| 6050 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6051 | if (!lys_node_module(node)->implemented) { |
| 6052 | /* not implemented module, don't bother with resolving the leafref |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 6053 | * if the module is set to be implemented, the path will be resolved then */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 6054 | rc = 0; |
| 6055 | break; |
| 6056 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6057 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 6058 | (const struct lys_node **)&stype->info.lref.target); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6059 | if (!tpdf_flag && !rc) { |
| 6060 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6061 | /* check if leafref and its target are under a common if-features */ |
| 6062 | rc = check_leafref_features(stype); |
| 6063 | if (rc) { |
| 6064 | break; |
| 6065 | } |
| 6066 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6067 | /* store the backlink from leafref target */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6068 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6069 | rc = -1; |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6070 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6071 | } |
| 6072 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6073 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6074 | case UNRES_TYPE_DER_TPDF: |
| 6075 | tpdf_flag = 1; |
| 6076 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6077 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6078 | /* parent */ |
| 6079 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6080 | stype = item; |
| 6081 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6082 | /* HACK type->der is temporarily unparsed type statement */ |
| 6083 | yin = (struct lyxml_elem *)stype->der; |
| 6084 | stype->der = NULL; |
| 6085 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6086 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6087 | yang = (struct yang_type *)yin; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6088 | rc = yang_check_type(mod, node, yang, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6089 | |
| 6090 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6091 | /* may try again later */ |
| 6092 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6093 | } else { |
| 6094 | /* 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] | 6095 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6096 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6097 | } |
| 6098 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6099 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6100 | rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6101 | if (!rc) { |
| 6102 | /* we need to always be able to free this, it's safe only in this case */ |
| 6103 | lyxml_free(mod->ctx, yin); |
| 6104 | } else { |
| 6105 | /* may try again later, put all back how it was */ |
| 6106 | stype->der = (struct lys_tpdf *)yin; |
| 6107 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6108 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6109 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6110 | /* it does not make sense to have leaf-list of empty type */ |
| 6111 | if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
| 6112 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6113 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6114 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6115 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6116 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
| 6117 | * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of |
| 6118 | * 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] | 6119 | * 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] | 6120 | * of the type's base member. */ |
| 6121 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6122 | if (par_grp) { |
| 6123 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6124 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6125 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6126 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6127 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6128 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6129 | iff_data = str_snode; |
| 6130 | 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] | 6131 | if (!rc) { |
| 6132 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6133 | if (iff_data->infeature) { |
| 6134 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6135 | feat = *((struct lys_feature **)item); |
| 6136 | if (!feat->depfeatures) { |
| 6137 | feat->depfeatures = ly_set_new(); |
| 6138 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6139 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6140 | } |
| 6141 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6142 | lydict_remove(mod->ctx, iff_data->fname); |
| 6143 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6144 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6145 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6146 | case UNRES_FEATURE: |
| 6147 | feat = (struct lys_feature *)item; |
| 6148 | |
| 6149 | if (feat->iffeature_size) { |
| 6150 | refs = ly_set_new(); |
| 6151 | procs = ly_set_new(); |
| 6152 | ly_set_add(procs, feat, 0); |
| 6153 | |
| 6154 | while (procs->number) { |
| 6155 | ref = procs->set.g[procs->number - 1]; |
| 6156 | ly_set_rm_index(procs, procs->number - 1); |
| 6157 | |
| 6158 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6159 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6160 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6161 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6162 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6163 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6164 | goto featurecheckdone; |
| 6165 | } |
| 6166 | |
| 6167 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6168 | k = refs->number; |
| 6169 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6170 | /* not yet seen feature, add it for processing */ |
| 6171 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6172 | } |
| 6173 | } |
| 6174 | } else { |
| 6175 | /* forward reference */ |
| 6176 | rc = EXIT_FAILURE; |
| 6177 | goto featurecheckdone; |
| 6178 | } |
| 6179 | } |
| 6180 | |
| 6181 | } |
| 6182 | } |
| 6183 | rc = EXIT_SUCCESS; |
| 6184 | |
| 6185 | featurecheckdone: |
| 6186 | ly_set_free(refs); |
| 6187 | ly_set_free(procs); |
| 6188 | } |
| 6189 | |
| 6190 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6191 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6192 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6193 | break; |
| 6194 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6195 | stype = item; |
| 6196 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 6197 | rc = check_default(stype, (const char **)str_snode, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6198 | break; |
| 6199 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6200 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6201 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6202 | choic = item; |
| 6203 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6204 | if (!choic->dflt) { |
| 6205 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6206 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6207 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6208 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6209 | } else { |
| 6210 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6211 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6212 | break; |
| 6213 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6214 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6215 | break; |
| 6216 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6217 | unique_info = (struct unres_list_uniq *)item; |
| 6218 | 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] | 6219 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6220 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6221 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6222 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6223 | case UNRES_XPATH: |
| 6224 | node = (struct lys_node *)item; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 6225 | rc = lys_check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6226 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6227 | default: |
| 6228 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6229 | break; |
| 6230 | } |
| 6231 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6232 | if (has_str && !rc) { |
| 6233 | /* the string is no more needed in case of success. |
| 6234 | * 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] | 6235 | lydict_remove(mod->ctx, str_snode); |
| 6236 | } |
| 6237 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6238 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6239 | } |
| 6240 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6241 | /* logs directly */ |
| 6242 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6243 | 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] | 6244 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6245 | struct lyxml_elem *xml; |
| 6246 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6247 | struct unres_iffeat_data *iff_data; |
Radek Krejci | 76e15e1 | 2016-06-22 11:02:24 +0200 | [diff] [blame] | 6248 | const char *type_name = NULL; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6249 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6250 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6251 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6252 | 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] | 6253 | break; |
| 6254 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6255 | 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] | 6256 | break; |
| 6257 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6258 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6259 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6260 | break; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6261 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6262 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6263 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6264 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6265 | type_name = ((struct yang_type *)xml)->name; |
| 6266 | } else { |
| 6267 | LY_TREE_FOR(xml->attr, attr) { |
| 6268 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
| 6269 | type_name = attr->value; |
| 6270 | break; |
| 6271 | } |
| 6272 | } |
| 6273 | assert(attr); |
| 6274 | } |
| 6275 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", type_name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6276 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6277 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6278 | iff_data = str_node; |
| 6279 | 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] | 6280 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6281 | case UNRES_FEATURE: |
| 6282 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6283 | ((struct lys_feature *)item)->name); |
| 6284 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6285 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6286 | 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] | 6287 | break; |
| 6288 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6289 | if (str_node) { |
| 6290 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6291 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6292 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6293 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6294 | break; |
| 6295 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6296 | 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] | 6297 | break; |
| 6298 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6299 | 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] | 6300 | break; |
| 6301 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6302 | 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] | 6303 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6304 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6305 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6306 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6307 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6308 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6309 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6310 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6311 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6312 | default: |
| 6313 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6314 | break; |
| 6315 | } |
| 6316 | } |
| 6317 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6318 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6319 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6320 | * |
| 6321 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6322 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6323 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6324 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6325 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6326 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6327 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6328 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6329 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6330 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6331 | |
| 6332 | assert(unres); |
| 6333 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6334 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6335 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6336 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6337 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6338 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6339 | unres_count = 0; |
| 6340 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6341 | |
| 6342 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6343 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6344 | * if-features are resolved here to make sure that we will have all if-features for |
| 6345 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6346 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6347 | continue; |
| 6348 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6349 | /* 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] | 6350 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6351 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6352 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6353 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6354 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6355 | unres->type[i] = UNRES_RESOLVED; |
| 6356 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6357 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6358 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6359 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6360 | /* print the error */ |
| 6361 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6362 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6363 | } else { |
| 6364 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6365 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6366 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6367 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6368 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6369 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6370 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6371 | /* just print the errors */ |
| 6372 | ly_vlog_hide(0); |
| 6373 | |
| 6374 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6375 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6376 | continue; |
| 6377 | } |
| 6378 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6379 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6380 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6381 | } |
| 6382 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6383 | /* the rest */ |
| 6384 | for (i = 0; i < unres->count; ++i) { |
| 6385 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6386 | continue; |
| 6387 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6388 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6389 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6390 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6391 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6392 | /* free the allocated structure */ |
| 6393 | free(unres->item[i]); |
| 6394 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6395 | unres->type[i] = UNRES_RESOLVED; |
| 6396 | ++resolved; |
| 6397 | } else if (rc == -1) { |
| 6398 | ly_vlog_hide(0); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6399 | /* print the error */ |
| 6400 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 6401 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6402 | } |
| 6403 | } |
| 6404 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6405 | ly_vlog_hide(0); |
| 6406 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6407 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6408 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6409 | * all the validation errors |
| 6410 | */ |
| 6411 | for (i = 0; i < unres->count; ++i) { |
| 6412 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6413 | continue; |
| 6414 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6415 | if (unres->type[i] == UNRES_XPATH) { |
| 6416 | /* unresolvable XPaths are actually supposed to be warnings - they may be |
| 6417 | * unresolved due to the not implemented target module so it shouldn't avoid |
| 6418 | * parsing the module, but we still want to announce some issue here */ |
| 6419 | ly_vlog_hide(0xff); |
| 6420 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6421 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6422 | if (unres->type[i] == UNRES_XPATH && *ly_vlog_hide_location() == 0xff) { |
| 6423 | unres->type[i] = UNRES_RESOLVED; |
| 6424 | resolved++; |
| 6425 | ly_vlog_hide(0); |
| 6426 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6427 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6428 | if (resolved < unres->count) { |
| 6429 | return -1; |
| 6430 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6431 | } |
| 6432 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6433 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6434 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6435 | return EXIT_SUCCESS; |
| 6436 | } |
| 6437 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6438 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6439 | * @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] | 6440 | * |
| 6441 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6442 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6443 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6444 | * @param[in] type Type of the unresolved item. |
| 6445 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6446 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6447 | * @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] | 6448 | */ |
| 6449 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6450 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6451 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6452 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6453 | int rc; |
| 6454 | const char *dictstr; |
| 6455 | |
| 6456 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6457 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6458 | |
| 6459 | if (rc == -1) { |
| 6460 | lydict_remove(mod->ctx, dictstr); |
| 6461 | } |
| 6462 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6463 | } |
| 6464 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6465 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6466 | * @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] | 6467 | * |
| 6468 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6469 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6470 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6471 | * @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] | 6472 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6473 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6474 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6475 | */ |
| 6476 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6477 | 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] | 6478 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6479 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6480 | int rc, log_hidden; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6481 | uint32_t u; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6482 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6483 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6484 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6485 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6486 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6487 | /* check for duplicities in unres */ |
| 6488 | for (u = 0; u < unres->count; u++) { |
| 6489 | if (unres->type[u] == type && unres->item[u] == item && |
| 6490 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
| 6491 | /* duplication, will be resolved later */ |
| 6492 | return EXIT_FAILURE; |
| 6493 | } |
| 6494 | } |
| 6495 | |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6496 | if (*ly_vlog_hide_location()) { |
| 6497 | log_hidden = 1; |
| 6498 | } else { |
| 6499 | log_hidden = 0; |
| 6500 | ly_vlog_hide(1); |
| 6501 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6502 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6503 | if (!log_hidden) { |
| 6504 | ly_vlog_hide(0); |
| 6505 | } |
| 6506 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6507 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6508 | if (rc == -1 && ly_errno == LY_EVALID) { |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 6509 | ly_err_repeat(); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6510 | } |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6511 | if (type == UNRES_LIST_UNIQ) { |
| 6512 | /* free the allocated structure */ |
| 6513 | free(item); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6514 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6515 | /* free the allocated resources */ |
| 6516 | free(*((char **)item)); |
| 6517 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6518 | return rc; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 6519 | } else { |
| 6520 | /* erase info about validation errors */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6521 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6522 | } |
| 6523 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6524 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6525 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6526 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6527 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6528 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6529 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6530 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6531 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6532 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6533 | } |
| 6534 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6535 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6536 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 6537 | if (!unres->item) { |
| 6538 | LOGMEM; |
| 6539 | return -1; |
| 6540 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6541 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6542 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 6543 | if (!unres->type) { |
| 6544 | LOGMEM; |
| 6545 | return -1; |
| 6546 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6547 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6548 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 6549 | if (!unres->str_snode) { |
| 6550 | LOGMEM; |
| 6551 | return -1; |
| 6552 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6553 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6554 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 6555 | if (!unres->module) { |
| 6556 | LOGMEM; |
| 6557 | return -1; |
| 6558 | } |
| 6559 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6560 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6561 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6562 | } |
| 6563 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6564 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6565 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6566 | * |
| 6567 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6568 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6569 | * @param[in] item Old item to be resolved. |
| 6570 | * @param[in] type Type of the old unresolved item. |
| 6571 | * @param[in] new_item New item to use in the duplicate. |
| 6572 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6573 | * @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] | 6574 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6575 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6576 | 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] | 6577 | { |
| 6578 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6579 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6580 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6581 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6582 | 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] | 6583 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6584 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 6585 | if (type == UNRES_LIST_UNIQ) { |
| 6586 | aux_uniq.list = item; |
| 6587 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 6588 | item = &aux_uniq; |
| 6589 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6590 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6591 | |
| 6592 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6593 | if (type == UNRES_LIST_UNIQ) { |
| 6594 | free(new_item); |
| 6595 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 6596 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6597 | } |
| 6598 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6599 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 6600 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6601 | 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] | 6602 | LOGINT; |
| 6603 | return -1; |
| 6604 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6605 | } else if (type == UNRES_IFFEAT) { |
| 6606 | /* duplicate unres_iffeature_data */ |
| 6607 | iff_data = malloc(sizeof *iff_data); |
| 6608 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 6609 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 6610 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 6611 | LOGINT; |
| 6612 | return -1; |
| 6613 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6614 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6615 | 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] | 6616 | LOGINT; |
| 6617 | return -1; |
| 6618 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6619 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 6620 | |
| 6621 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6622 | } |
| 6623 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6624 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6625 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6626 | 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] | 6627 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6628 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6629 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6630 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6631 | if (start_on_backwards > 0) { |
| 6632 | i = start_on_backwards; |
| 6633 | } else { |
| 6634 | i = unres->count - 1; |
| 6635 | } |
| 6636 | for (; i > -1; i--) { |
| 6637 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6638 | continue; |
| 6639 | } |
| 6640 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6641 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6642 | break; |
| 6643 | } |
| 6644 | } else { |
| 6645 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 6646 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 6647 | 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] | 6648 | break; |
| 6649 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6650 | } |
| 6651 | } |
| 6652 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 6653 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6654 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 6655 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6656 | static void |
| 6657 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 6658 | { |
| 6659 | struct lyxml_elem *yin; |
| 6660 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6661 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6662 | |
| 6663 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6664 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6665 | case UNRES_TYPE_DER: |
| 6666 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 6667 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6668 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 6669 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6670 | lydict_remove(ctx, yang->name); |
| 6671 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 6672 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 6673 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 6674 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6675 | } else { |
| 6676 | lyxml_free(ctx, yin); |
| 6677 | } |
| 6678 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6679 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6680 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 6681 | lydict_remove(ctx, iff_data->fname); |
| 6682 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6683 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6684 | case UNRES_IDENT: |
| 6685 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6686 | case UNRES_CHOICE_DFLT: |
| 6687 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6688 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 6689 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6690 | case UNRES_LIST_UNIQ: |
| 6691 | free(unres->item[i]); |
| 6692 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6693 | default: |
| 6694 | break; |
| 6695 | } |
| 6696 | unres->type[i] = UNRES_RESOLVED; |
| 6697 | } |
| 6698 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6699 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6700 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6701 | { |
| 6702 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6703 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6704 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6705 | if (!unres || !(*unres)) { |
| 6706 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6709 | assert(module || (*unres)->count == 0); |
| 6710 | |
| 6711 | for (i = 0; i < (*unres)->count; ++i) { |
| 6712 | if ((*unres)->module[i] != module) { |
| 6713 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 6714 | unresolved++; |
| 6715 | } |
| 6716 | continue; |
| 6717 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6718 | |
| 6719 | /* free heap memory for the specific item */ |
| 6720 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6721 | } |
| 6722 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 6723 | /* free it all */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6724 | if (!module || (!unresolved && !module->type)) { |
| 6725 | free((*unres)->item); |
| 6726 | free((*unres)->type); |
| 6727 | free((*unres)->str_snode); |
| 6728 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6729 | free((*unres)); |
| 6730 | (*unres) = NULL; |
| 6731 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6732 | } |
| 6733 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6734 | /** |
| 6735 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 6736 | * |
| 6737 | * @param[in] data Data node where the path is used |
| 6738 | * @param[in] path Instance-identifier node value. |
| 6739 | * @param[in,out] ret Resolved instance or NULL. |
| 6740 | * |
| 6741 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 6742 | */ |
| 6743 | static int |
| 6744 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 6745 | { |
| 6746 | int i = 0, j; |
| 6747 | const struct lys_module *mod; |
| 6748 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 6749 | const char *model, *name; |
| 6750 | char *str; |
| 6751 | int mod_len, name_len, has_predicate; |
| 6752 | struct unres_data node_match; |
| 6753 | |
| 6754 | memset(&node_match, 0, sizeof node_match); |
| 6755 | *ret = NULL; |
| 6756 | |
| 6757 | /* we need root to resolve absolute path */ |
| 6758 | for (; data->parent; data = data->parent); |
| 6759 | /* we're still parsing it and the pointer is not correct yet */ |
| 6760 | if (data->prev) { |
| 6761 | for (; data->prev->next; data = data->prev); |
| 6762 | } |
| 6763 | |
| 6764 | /* search for the instance node */ |
| 6765 | while (path[i]) { |
| 6766 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 6767 | if (j <= 0) { |
| 6768 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 6769 | goto error; |
| 6770 | } |
| 6771 | i += j; |
| 6772 | |
| 6773 | str = strndup(model, mod_len); |
| 6774 | if (!str) { |
| 6775 | LOGMEM; |
| 6776 | goto error; |
| 6777 | } |
| 6778 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 6779 | free(str); |
| 6780 | |
| 6781 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 6782 | /* no instance exists */ |
| 6783 | break; |
| 6784 | } |
| 6785 | |
| 6786 | if (has_predicate) { |
| 6787 | /* we have predicate, so the current results must be list or leaf-list */ |
| 6788 | j = resolve_predicate(&path[i], &node_match); |
| 6789 | if (j < 1) { |
| 6790 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 6791 | goto error; |
| 6792 | } |
| 6793 | i += j; |
| 6794 | |
| 6795 | if (!node_match.count) { |
| 6796 | /* no instance exists */ |
| 6797 | break; |
| 6798 | } |
| 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | if (!node_match.count) { |
| 6803 | /* no instance exists */ |
| 6804 | if (req_inst > -1) { |
| 6805 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 6806 | return EXIT_FAILURE; |
| 6807 | } |
| 6808 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 6809 | return EXIT_SUCCESS; |
| 6810 | } else if (node_match.count > 1) { |
| 6811 | /* instance identifier must resolve to a single node */ |
| 6812 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 6813 | goto error; |
| 6814 | } else { |
| 6815 | /* we have required result, remember it and cleanup */ |
| 6816 | *ret = node_match.node[0]; |
| 6817 | free(node_match.node); |
| 6818 | return EXIT_SUCCESS; |
| 6819 | } |
| 6820 | |
| 6821 | error: |
| 6822 | /* cleanup */ |
| 6823 | free(node_match.node); |
| 6824 | return -1; |
| 6825 | } |
| 6826 | |
| 6827 | static int |
| 6828 | 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] | 6829 | { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6830 | struct unres_data matches; |
| 6831 | uint32_t i; |
| 6832 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6833 | /* init */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6834 | memset(&matches, 0, sizeof matches); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6835 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6836 | |
| 6837 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6838 | if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6839 | return -1; |
| 6840 | } |
| 6841 | |
| 6842 | /* check that value matches */ |
| 6843 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 6844 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 6845 | * so we can simply compare just the values */ |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6846 | 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] | 6847 | /* we have the match */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6848 | *ret = matches.node[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6849 | break; |
| 6850 | } |
| 6851 | } |
| 6852 | |
| 6853 | free(matches.node); |
| 6854 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6855 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6856 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6857 | if (req_inst > -1) { |
| 6858 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 6859 | return EXIT_FAILURE; |
| 6860 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6861 | 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] | 6862 | } |
| 6863 | } |
| 6864 | |
| 6865 | return EXIT_SUCCESS; |
| 6866 | } |
| 6867 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6868 | /* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */ |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6869 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6870 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int ignore_fail) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6871 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6872 | struct lys_type *t; |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 6873 | struct lyd_node *ret, *par, *op_node; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6874 | int found, hidden, success = 0; |
| 6875 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6876 | |
| 6877 | assert(type->base == LY_TYPE_UNION); |
| 6878 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6879 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 6880 | /* either NULL or instid previously converted to JSON */ |
| 6881 | json_val = leaf->value.string; |
| 6882 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame^] | 6883 | |
| 6884 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 6885 | free(leaf->value.bit); |
| 6886 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6887 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6888 | |
| 6889 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 6890 | hidden = *ly_vlog_hide_location(); |
| 6891 | ly_vlog_hide(1); |
| 6892 | |
| 6893 | t = NULL; |
| 6894 | found = 0; |
| 6895 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 6896 | found = 0; |
| 6897 | |
| 6898 | switch (t->base) { |
| 6899 | case LY_TYPE_LEAFREF: |
| 6900 | if (!resolve_leafref(leaf, t->info.lref.path, (ignore_fail ? -1 : t->info.lref.req), &ret)) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 6901 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6902 | /* valid resolved */ |
| 6903 | leaf->value.leafref = ret; |
| 6904 | leaf->value_type = LY_TYPE_LEAFREF; |
| 6905 | } else { |
| 6906 | /* valid unresolved */ |
| 6907 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, 1, 0)) { |
| 6908 | return -1; |
| 6909 | } |
| 6910 | } |
| 6911 | |
| 6912 | success = 1; |
| 6913 | } |
| 6914 | break; |
| 6915 | case LY_TYPE_INST: |
| 6916 | if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), |
| 6917 | (ignore_fail ? -1 : t->info.inst.req), &ret)) { |
| 6918 | if (ret) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 6919 | for (op_node = (struct lyd_node *)leaf; |
| 6920 | op_node && !(op_node->schema->nodetype & (LYS_RPC | LYS_NOTIF | LYS_ACTION)); |
| 6921 | op_node = op_node->parent); |
| 6922 | if (op_node) { |
| 6923 | /* this is an RPC/notif/action */ |
| 6924 | for (par = ret->parent; par && (par != op_node); par = par->parent); |
| 6925 | if (!par) { |
| 6926 | /* target instance is outside the operation - do not store the pointer */ |
| 6927 | ret = NULL; |
| 6928 | } |
| 6929 | } |
| 6930 | } |
| 6931 | if (ret) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6932 | /* valid resolved */ |
| 6933 | leaf->value.instance = ret; |
| 6934 | leaf->value_type = LY_TYPE_INST; |
| 6935 | |
| 6936 | if (json_val) { |
| 6937 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 6938 | leaf->value_str = json_val; |
| 6939 | json_val = NULL; |
| 6940 | } |
| 6941 | } else { |
| 6942 | /* valid unresolved */ |
| 6943 | if (json_val) { |
| 6944 | /* put the JSON val back */ |
| 6945 | leaf->value.string = json_val; |
| 6946 | json_val = NULL; |
| 6947 | } else { |
| 6948 | leaf->value.instance = NULL; |
| 6949 | } |
| 6950 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
| 6951 | } |
| 6952 | |
| 6953 | success = 1; |
| 6954 | } |
| 6955 | break; |
| 6956 | default: |
| 6957 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, 1, 0)) { |
| 6958 | success = 1; |
| 6959 | } |
| 6960 | break; |
| 6961 | } |
| 6962 | |
| 6963 | if (success) { |
| 6964 | break; |
| 6965 | } |
| 6966 | |
| 6967 | /* erase information about errors - they are false or irrelevant |
| 6968 | * and will be replaced by a single error messages */ |
| 6969 | ly_err_clean(1); |
| 6970 | |
| 6971 | /* erase possible present and invalid value data */ |
| 6972 | if (t->base == LY_TYPE_BITS) { |
| 6973 | free(leaf->value.bit); |
| 6974 | } |
| 6975 | memset(&leaf->value, 0, sizeof leaf->value); |
| 6976 | } |
| 6977 | |
| 6978 | /* turn logging back on */ |
| 6979 | if (!hidden) { |
| 6980 | ly_vlog_hide(0); |
| 6981 | } |
| 6982 | |
| 6983 | if (json_val) { |
| 6984 | if (!success) { |
| 6985 | /* put the value back for now */ |
| 6986 | assert(leaf->value_type == LY_TYPE_UNION); |
| 6987 | leaf->value.string = json_val; |
| 6988 | } else { |
| 6989 | /* value was ultimately useless, but we could not have known */ |
| 6990 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 6991 | } |
| 6992 | } |
| 6993 | |
| 6994 | if (!success && (!ignore_fail || !type->info.uni.has_ptr_type)) { |
| 6995 | /* not found and it is required */ |
| 6996 | 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] | 6997 | return EXIT_FAILURE; |
| 6998 | } |
| 6999 | |
| 7000 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7001 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7002 | } |
| 7003 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7004 | /** |
| 7005 | * @brief Resolve a single unres data item. Logs directly. |
| 7006 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7007 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7008 | * @param[in] type Type of the unresolved item. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7009 | * @param[in] ignore_fails Flag whether to ignore any false condition or unresolved nodes (e.g., for LYD_OPT_EDIT). |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7010 | * |
| 7011 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7012 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7013 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7014 | 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] | 7015 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7016 | int rc, req_inst; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7017 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7018 | struct lyd_node *ret, *op_node, *par; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7019 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7020 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7021 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7022 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7023 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7024 | switch (type) { |
| 7025 | case UNRES_LEAFREF: |
| 7026 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7027 | assert(leaf->validity & LYD_VAL_LEAFREF); |
| 7028 | req_inst = (ignore_fail ? -1 : sleaf->type.info.lref.req); |
| 7029 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7030 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7031 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7032 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame^] | 7033 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7034 | free(leaf->value.bit); |
| 7035 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7036 | leaf->value.leafref = ret; |
| 7037 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7038 | } else { |
| 7039 | /* valid unresolved */ |
| 7040 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
| 7041 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, 1, 0)) { |
| 7042 | return -1; |
| 7043 | } |
| 7044 | } |
| 7045 | } |
| 7046 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7047 | } else { |
| 7048 | return rc; |
| 7049 | } |
| 7050 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7051 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7052 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7053 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7054 | req_inst = (ignore_fail ? -1 : sleaf->type.info.inst.req); |
| 7055 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7056 | if (!rc) { |
| 7057 | if (ret) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7058 | for (op_node = (struct lyd_node *)leaf; |
| 7059 | op_node && !(op_node->schema->nodetype & (LYS_RPC | LYS_NOTIF | LYS_ACTION)); |
| 7060 | op_node = op_node->parent); |
| 7061 | if (op_node) { |
| 7062 | /* this is an RPC/notif/action */ |
| 7063 | for (par = ret->parent; par && (par != op_node); par = par->parent); |
| 7064 | if (!par) { |
| 7065 | /* target instance is outside the operation - do not store the pointer */ |
| 7066 | ret = NULL; |
| 7067 | } |
| 7068 | } |
| 7069 | } |
| 7070 | if (ret) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7071 | /* valid resolved */ |
| 7072 | leaf->value.instance = ret; |
| 7073 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7074 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7075 | /* valid unresolved */ |
| 7076 | leaf->value.instance = NULL; |
| 7077 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7078 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7079 | } else { |
| 7080 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7081 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7082 | break; |
| 7083 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7084 | case UNRES_UNION: |
| 7085 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7086 | return resolve_union(leaf, &sleaf->type, ignore_fail); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7087 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7088 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7089 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7090 | return rc; |
| 7091 | } |
| 7092 | break; |
| 7093 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7094 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7095 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7096 | return rc; |
| 7097 | } |
| 7098 | break; |
| 7099 | |
| 7100 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7101 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7102 | return rc; |
| 7103 | } |
| 7104 | break; |
| 7105 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7106 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7107 | LOGINT; |
| 7108 | return -1; |
| 7109 | } |
| 7110 | |
| 7111 | return EXIT_SUCCESS; |
| 7112 | } |
| 7113 | |
| 7114 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7115 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7116 | * |
| 7117 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7118 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7119 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7120 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7121 | */ |
| 7122 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7123 | 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] | 7124 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7125 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7126 | 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] | 7127 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7128 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7129 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7130 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 7131 | if (!unres->node) { |
| 7132 | LOGMEM; |
| 7133 | return -1; |
| 7134 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7135 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7136 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 7137 | if (!unres->type) { |
| 7138 | LOGMEM; |
| 7139 | return -1; |
| 7140 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7141 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7142 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7143 | if (type == UNRES_WHEN) { |
| 7144 | /* remove previous result */ |
| 7145 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7146 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7147 | |
| 7148 | return EXIT_SUCCESS; |
| 7149 | } |
| 7150 | |
| 7151 | /** |
| 7152 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7153 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7154 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7155 | * unresolved leafrefs/instids are accepted). |
| 7156 | * |
| 7157 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7158 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7159 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7160 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7161 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7162 | * |
| 7163 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7164 | */ |
| 7165 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7166 | 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] | 7167 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7168 | uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7169 | int rc, progress, ignore_fails; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7170 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7171 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7172 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7173 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7174 | |
| 7175 | if (!unres->count) { |
| 7176 | return EXIT_SUCCESS; |
| 7177 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7178 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7179 | if (options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7180 | ignore_fails = 1; |
| 7181 | } else { |
| 7182 | ignore_fails = 0; |
| 7183 | } |
| 7184 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7185 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7186 | ly_vlog_hide(1); |
| 7187 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7188 | /* when-stmt first */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7189 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7190 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7191 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7192 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7193 | if (unres->type[i] != UNRES_WHEN) { |
| 7194 | continue; |
| 7195 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7196 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7197 | /* count when-stmt nodes in unres list */ |
| 7198 | when_stmt++; |
| 7199 | } |
| 7200 | |
| 7201 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7202 | for (parent = unres->node[i]->parent; |
| 7203 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7204 | parent = parent->parent) { |
| 7205 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7206 | /* the parent node was already unlinked, do not resolve this node, |
| 7207 | * it will be removed anyway, so just mark it as resolved |
| 7208 | */ |
| 7209 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7210 | unres->type[i] = UNRES_RESOLVED; |
| 7211 | resolved++; |
| 7212 | break; |
| 7213 | } |
| 7214 | } |
| 7215 | if (parent) { |
| 7216 | continue; |
| 7217 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7218 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7219 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fails); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7220 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7221 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7222 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7223 | /* false when condition */ |
| 7224 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7225 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7226 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7227 | } /* follows else */ |
| 7228 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7229 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7230 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7231 | * remove the container |
| 7232 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7233 | for (parent = unres->node[i]; |
| 7234 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7235 | parent = parent->parent) { |
| 7236 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7237 | /* presence container */ |
| 7238 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7239 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7240 | if (parent->next || parent->prev != parent) { |
| 7241 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7242 | break; |
| 7243 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7244 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7245 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7246 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7247 | /* auto-delete */ |
| 7248 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7249 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7250 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7251 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7252 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7253 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7254 | lyd_unlink(unres->node[i]); |
| 7255 | unres->type[i] = UNRES_DELETE; |
| 7256 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7257 | |
| 7258 | /* update the rest of unres items */ |
| 7259 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7260 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7261 | continue; |
| 7262 | } |
| 7263 | |
| 7264 | /* test if the node is in subtree to be deleted */ |
| 7265 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7266 | if (parent == unres->node[i]) { |
| 7267 | /* yes, it is */ |
| 7268 | unres->type[j] = UNRES_RESOLVED; |
| 7269 | resolved++; |
| 7270 | break; |
| 7271 | } |
| 7272 | } |
| 7273 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7274 | } else { |
| 7275 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7276 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7277 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7278 | resolved++; |
| 7279 | progress = 1; |
| 7280 | } else if (rc == -1) { |
| 7281 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7282 | /* print only this last error */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7283 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fails); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7284 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7285 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7286 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7287 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7288 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7289 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7290 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 7291 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7292 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7293 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7294 | return -1; |
| 7295 | } |
| 7296 | |
| 7297 | for (i = 0; del_items && i < unres->count; i++) { |
| 7298 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7299 | if (unres->type[i] != UNRES_DELETE) { |
| 7300 | continue; |
| 7301 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7302 | if (!unres->node[i]) { |
| 7303 | unres->type[i] = UNRES_RESOLVED; |
| 7304 | del_items--; |
| 7305 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7306 | } |
| 7307 | |
| 7308 | /* really remove the complete subtree */ |
| 7309 | lyd_free(unres->node[i]); |
| 7310 | unres->type[i] = UNRES_RESOLVED; |
| 7311 | del_items--; |
| 7312 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7313 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7314 | |
| 7315 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7316 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7317 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7318 | continue; |
| 7319 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7320 | 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] | 7321 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7322 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fails); |
| 7323 | if (rc) { |
| 7324 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7325 | return -1; |
| 7326 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7327 | |
| 7328 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7329 | } |
| 7330 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7331 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7332 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7333 | return EXIT_SUCCESS; |
| 7334 | } |