Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 32 | #include "extensions.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 33 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 34 | int |
| 35 | parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num) |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 36 | { |
| 37 | const char *ptr; |
| 38 | int minus = 0; |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 39 | int64_t ret = 0, prev_ret; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 40 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 41 | |
| 42 | ptr = *str_num; |
| 43 | |
| 44 | if (ptr[0] == '-') { |
| 45 | minus = 1; |
| 46 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 47 | } else if (ptr[0] == '+') { |
| 48 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 51 | if (!isdigit(ptr[0])) { |
| 52 | /* there must be at least one */ |
| 53 | return 1; |
| 54 | } |
| 55 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 57 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 58 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (ptr[0] == '.') { |
| 62 | if (ptr[1] == '.') { |
| 63 | /* it's the next interval */ |
| 64 | break; |
| 65 | } |
| 66 | ++str_dig; |
| 67 | } else { |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 68 | prev_ret = ret; |
| 69 | if (minus) { |
| 70 | ret = ret * 10 - (ptr[0] - '0'); |
| 71 | if (ret > prev_ret) { |
| 72 | return 1; |
| 73 | } |
| 74 | } else { |
| 75 | ret = ret * 10 + (ptr[0] - '0'); |
| 76 | if (ret < prev_ret) { |
| 77 | return 1; |
| 78 | } |
| 79 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 80 | if (str_dig > -1) { |
| 81 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 82 | if (ptr[0] == '0') { |
| 83 | /* possibly trailing zero */ |
| 84 | trailing_zeros++; |
| 85 | } else { |
| 86 | trailing_zeros = 0; |
| 87 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 88 | } |
| 89 | ++str_exp; |
| 90 | } |
| 91 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 92 | if (str_dig == 0) { |
| 93 | /* no digits after '.' */ |
| 94 | return 1; |
| 95 | } else if (str_dig == -1) { |
| 96 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 97 | str_dig = 0; |
| 98 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 99 | /* remove trailing zeros */ |
| 100 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 101 | str_dig -= trailing_zeros; |
| 102 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 103 | ret = ret / dec_pow(trailing_zeros); |
| 104 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 105 | |
| 106 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 107 | if (str_dig < dig) { |
| 108 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 109 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 110 | } |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 111 | prev_ret = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 112 | ret *= dec_pow(dig - str_dig); |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 113 | if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) { |
| 114 | return 1; |
| 115 | } |
| 116 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 117 | } |
| 118 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 119 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 122 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 123 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 124 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 125 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 129 | * @brief Parse an identifier. |
| 130 | * |
| 131 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 132 | * identifier = (ALPHA / "_") |
| 133 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 134 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 135 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 136 | * |
| 137 | * @return Number of characters successfully parsed. |
| 138 | */ |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 139 | unsigned int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 140 | parse_identifier(const char *id) |
| 141 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 142 | unsigned int parsed = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 143 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 144 | assert(id); |
| 145 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 146 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 147 | return -parsed; |
| 148 | } |
| 149 | |
| 150 | ++parsed; |
| 151 | ++id; |
| 152 | |
| 153 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 154 | ++parsed; |
| 155 | ++id; |
| 156 | } |
| 157 | |
| 158 | return parsed; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @brief Parse a node-identifier. |
| 163 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 164 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 165 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 166 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 167 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 168 | * @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] | 169 | * @param[out] name Points to the node name. |
| 170 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 171 | * @param[out] all_desc Whether the path starts with '/', only supported in extended paths. |
| 172 | * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:].). |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 173 | * |
| 174 | * @return Number of characters successfully parsed, |
| 175 | * positive on success, negative on failure. |
| 176 | */ |
| 177 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 178 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 179 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 180 | { |
| 181 | int parsed = 0, ret; |
| 182 | |
| 183 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 184 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 185 | assert((name && nam_len) || (!name && !nam_len)); |
| 186 | assert(!extended || all_desc); |
| 187 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 188 | if (mod_name) { |
| 189 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 190 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 191 | } |
| 192 | if (name) { |
| 193 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 194 | *nam_len = 0; |
| 195 | } |
| 196 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 197 | if (extended) { |
| 198 | /* try to parse only the extended expressions */ |
| 199 | if (id[parsed] == '/') { |
| 200 | *all_desc = 1; |
| 201 | } else { |
| 202 | *all_desc = 0; |
| 203 | } |
| 204 | |
| 205 | /* is there a prefix? */ |
| 206 | ret = parse_identifier(id + *all_desc); |
| 207 | if (ret > 0) { |
| 208 | if (id[*all_desc + ret] != ':') { |
| 209 | /* this is not a prefix, so not an extended id */ |
| 210 | goto standard_id; |
| 211 | } |
| 212 | |
| 213 | if (mod_name) { |
| 214 | *mod_name = id + *all_desc; |
| 215 | *mod_name_len = ret; |
| 216 | } |
| 217 | |
| 218 | /* "/" and ":" */ |
| 219 | ret += *all_desc + 1; |
| 220 | } else { |
| 221 | ret = *all_desc; |
| 222 | } |
| 223 | |
| 224 | /* parse either "*" or "." */ |
| 225 | if (!strcmp(id + ret, "*")) { |
| 226 | if (name) { |
| 227 | *name = id + ret; |
| 228 | *nam_len = 1; |
| 229 | } |
| 230 | ++ret; |
| 231 | |
| 232 | return ret; |
| 233 | } else if (!strcmp(id + ret, ".")) { |
| 234 | if (!*all_desc) { |
| 235 | /* /. is redundant expression, we do not accept it */ |
| 236 | return -ret; |
| 237 | } |
| 238 | |
| 239 | if (name) { |
| 240 | *name = id + ret; |
| 241 | *nam_len = 1; |
| 242 | } |
| 243 | ++ret; |
| 244 | |
| 245 | return ret; |
| 246 | } |
| 247 | /* else a standard id, parse it all again */ |
| 248 | } |
| 249 | |
| 250 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 251 | if ((ret = parse_identifier(id)) < 1) { |
| 252 | return ret; |
| 253 | } |
| 254 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 255 | if (mod_name) { |
| 256 | *mod_name = id; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 257 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | parsed += ret; |
| 261 | id += ret; |
| 262 | |
| 263 | /* there is prefix */ |
| 264 | if (id[0] == ':') { |
| 265 | ++parsed; |
| 266 | ++id; |
| 267 | |
| 268 | /* there isn't */ |
| 269 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 270 | if (name && mod_name) { |
| 271 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 272 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 273 | if (mod_name) { |
| 274 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 277 | if (nam_len && mod_name_len) { |
| 278 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 279 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 280 | if (mod_name_len) { |
| 281 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return parsed; |
| 285 | } |
| 286 | |
| 287 | /* identifier (node name) */ |
| 288 | if ((ret = parse_identifier(id)) < 1) { |
| 289 | return -parsed+ret; |
| 290 | } |
| 291 | |
| 292 | if (name) { |
| 293 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 294 | *nam_len = ret; |
| 295 | } |
| 296 | |
| 297 | return parsed+ret; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * @brief Parse a path-predicate (leafref). |
| 302 | * |
| 303 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 304 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 305 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 306 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 307 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 308 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 309 | * @param[out] name Points to the node name. |
| 310 | * @param[out] nam_len Length of the node name. |
| 311 | * @param[out] path_key_expr Points to the path-key-expr. |
| 312 | * @param[out] pke_len Length of the path-key-expr. |
| 313 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 314 | * |
| 315 | * @return Number of characters successfully parsed, |
| 316 | * positive on success, negative on failure. |
| 317 | */ |
| 318 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 319 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 320 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 321 | { |
| 322 | const char *ptr; |
| 323 | int parsed = 0, ret; |
| 324 | |
| 325 | assert(id); |
| 326 | if (prefix) { |
| 327 | *prefix = NULL; |
| 328 | } |
| 329 | if (pref_len) { |
| 330 | *pref_len = 0; |
| 331 | } |
| 332 | if (name) { |
| 333 | *name = NULL; |
| 334 | } |
| 335 | if (nam_len) { |
| 336 | *nam_len = 0; |
| 337 | } |
| 338 | if (path_key_expr) { |
| 339 | *path_key_expr = NULL; |
| 340 | } |
| 341 | if (pke_len) { |
| 342 | *pke_len = 0; |
| 343 | } |
| 344 | if (has_predicate) { |
| 345 | *has_predicate = 0; |
| 346 | } |
| 347 | |
| 348 | if (id[0] != '[') { |
| 349 | return -parsed; |
| 350 | } |
| 351 | |
| 352 | ++parsed; |
| 353 | ++id; |
| 354 | |
| 355 | while (isspace(id[0])) { |
| 356 | ++parsed; |
| 357 | ++id; |
| 358 | } |
| 359 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 360 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 361 | return -parsed+ret; |
| 362 | } |
| 363 | |
| 364 | parsed += ret; |
| 365 | id += ret; |
| 366 | |
| 367 | while (isspace(id[0])) { |
| 368 | ++parsed; |
| 369 | ++id; |
| 370 | } |
| 371 | |
| 372 | if (id[0] != '=') { |
| 373 | return -parsed; |
| 374 | } |
| 375 | |
| 376 | ++parsed; |
| 377 | ++id; |
| 378 | |
| 379 | while (isspace(id[0])) { |
| 380 | ++parsed; |
| 381 | ++id; |
| 382 | } |
| 383 | |
| 384 | if ((ptr = strchr(id, ']')) == NULL) { |
| 385 | return -parsed; |
| 386 | } |
| 387 | |
| 388 | --ptr; |
| 389 | while (isspace(ptr[0])) { |
| 390 | --ptr; |
| 391 | } |
| 392 | ++ptr; |
| 393 | |
| 394 | ret = ptr-id; |
| 395 | if (path_key_expr) { |
| 396 | *path_key_expr = id; |
| 397 | } |
| 398 | if (pke_len) { |
| 399 | *pke_len = ret; |
| 400 | } |
| 401 | |
| 402 | parsed += ret; |
| 403 | id += ret; |
| 404 | |
| 405 | while (isspace(id[0])) { |
| 406 | ++parsed; |
| 407 | ++id; |
| 408 | } |
| 409 | |
| 410 | assert(id[0] == ']'); |
| 411 | |
| 412 | if (id[1] == '[') { |
| 413 | *has_predicate = 1; |
| 414 | } |
| 415 | |
| 416 | return parsed+1; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 421 | * the ".." and the first node-identifier, other calls parse a single |
| 422 | * node-identifier each. |
| 423 | * |
| 424 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 425 | * rel-path-keyexpr |
| 426 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 427 | * *(node-identifier *WSP "/" *WSP) |
| 428 | * node-identifier |
| 429 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 430 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 431 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 432 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 433 | * @param[out] name Points to the node name. |
| 434 | * @param[out] nam_len Length of the node name. |
| 435 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 436 | * must not be changed between consecutive calls. |
| 437 | * @return Number of characters successfully parsed, |
| 438 | * positive on success, negative on failure. |
| 439 | */ |
| 440 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 441 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 442 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 443 | { |
| 444 | int parsed = 0, ret, par_times = 0; |
| 445 | |
| 446 | assert(id); |
| 447 | assert(parent_times); |
| 448 | if (prefix) { |
| 449 | *prefix = NULL; |
| 450 | } |
| 451 | if (pref_len) { |
| 452 | *pref_len = 0; |
| 453 | } |
| 454 | if (name) { |
| 455 | *name = NULL; |
| 456 | } |
| 457 | if (nam_len) { |
| 458 | *nam_len = 0; |
| 459 | } |
| 460 | |
| 461 | if (!*parent_times) { |
| 462 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 463 | if (strncmp(id, "current()", 9)) { |
| 464 | return -parsed; |
| 465 | } |
| 466 | |
| 467 | parsed += 9; |
| 468 | id += 9; |
| 469 | |
| 470 | while (isspace(id[0])) { |
| 471 | ++parsed; |
| 472 | ++id; |
| 473 | } |
| 474 | |
| 475 | if (id[0] != '/') { |
| 476 | return -parsed; |
| 477 | } |
| 478 | |
| 479 | ++parsed; |
| 480 | ++id; |
| 481 | |
| 482 | while (isspace(id[0])) { |
| 483 | ++parsed; |
| 484 | ++id; |
| 485 | } |
| 486 | |
| 487 | /* rel-path-keyexpr */ |
| 488 | if (strncmp(id, "..", 2)) { |
| 489 | return -parsed; |
| 490 | } |
| 491 | ++par_times; |
| 492 | |
| 493 | parsed += 2; |
| 494 | id += 2; |
| 495 | |
| 496 | while (isspace(id[0])) { |
| 497 | ++parsed; |
| 498 | ++id; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 503 | * |
| 504 | * first parent reference with whitespaces already parsed |
| 505 | */ |
| 506 | if (id[0] != '/') { |
| 507 | return -parsed; |
| 508 | } |
| 509 | |
| 510 | ++parsed; |
| 511 | ++id; |
| 512 | |
| 513 | while (isspace(id[0])) { |
| 514 | ++parsed; |
| 515 | ++id; |
| 516 | } |
| 517 | |
| 518 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 519 | ++par_times; |
| 520 | |
| 521 | parsed += 2; |
| 522 | id += 2; |
| 523 | |
| 524 | while (isspace(id[0])) { |
| 525 | ++parsed; |
| 526 | ++id; |
| 527 | } |
| 528 | |
| 529 | if (id[0] != '/') { |
| 530 | return -parsed; |
| 531 | } |
| 532 | |
| 533 | ++parsed; |
| 534 | ++id; |
| 535 | |
| 536 | while (isspace(id[0])) { |
| 537 | ++parsed; |
| 538 | ++id; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (!*parent_times) { |
| 543 | *parent_times = par_times; |
| 544 | } |
| 545 | |
| 546 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 547 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 548 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | parsed += ret; |
| 552 | id += ret; |
| 553 | |
| 554 | return parsed; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Parse path-arg (leafref). |
| 559 | * |
| 560 | * path-arg = absolute-path / relative-path |
| 561 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 562 | * relative-path = 1*(".." "/") descendant-path |
| 563 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 564 | * @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] | 565 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 566 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 567 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 568 | * @param[out] name Points to the node name. |
| 569 | * @param[out] nam_len Length of the node name. |
| 570 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 571 | * must not be changed between consecutive calls. -1 if the |
| 572 | * path is relative. |
| 573 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 574 | * |
| 575 | * @return Number of characters successfully parsed, |
| 576 | * positive on success, negative on failure. |
| 577 | */ |
| 578 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 579 | parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len, |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 580 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 581 | { |
| 582 | int parsed = 0, ret, par_times = 0; |
| 583 | |
| 584 | assert(id); |
| 585 | assert(parent_times); |
| 586 | if (prefix) { |
| 587 | *prefix = NULL; |
| 588 | } |
| 589 | if (pref_len) { |
| 590 | *pref_len = 0; |
| 591 | } |
| 592 | if (name) { |
| 593 | *name = NULL; |
| 594 | } |
| 595 | if (nam_len) { |
| 596 | *nam_len = 0; |
| 597 | } |
| 598 | if (has_predicate) { |
| 599 | *has_predicate = 0; |
| 600 | } |
| 601 | |
| 602 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 603 | ++par_times; |
| 604 | |
| 605 | parsed += 2; |
| 606 | id += 2; |
| 607 | |
| 608 | while (!strncmp(id, "/..", 3)) { |
| 609 | ++par_times; |
| 610 | |
| 611 | parsed += 3; |
| 612 | id += 3; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (!*parent_times) { |
| 617 | if (par_times) { |
| 618 | *parent_times = par_times; |
| 619 | } else { |
| 620 | *parent_times = -1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if (id[0] != '/') { |
| 625 | return -parsed; |
| 626 | } |
| 627 | |
| 628 | /* skip '/' */ |
| 629 | ++parsed; |
| 630 | ++id; |
| 631 | |
| 632 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 633 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 634 | return -parsed - ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 635 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 636 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 637 | /* actually we always need prefix even it is not specified */ |
| 638 | *prefix = lys_main_module(mod)->name; |
| 639 | *pref_len = strlen(*prefix); |
| 640 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 641 | |
| 642 | parsed += ret; |
| 643 | id += ret; |
| 644 | |
| 645 | /* there is no predicate */ |
| 646 | if ((id[0] == '/') || !id[0]) { |
| 647 | return parsed; |
| 648 | } else if (id[0] != '[') { |
| 649 | return -parsed; |
| 650 | } |
| 651 | |
| 652 | if (has_predicate) { |
| 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 instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 661 | * are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 662 | * |
| 663 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 664 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 665 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 666 | * @param[out] model Points to the model name. |
| 667 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 668 | * @param[out] name Points to the node name. |
| 669 | * @param[out] nam_len Length of the node name. |
| 670 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 671 | * |
| 672 | * @return Number of characters successfully parsed, |
| 673 | * positive on success, negative on failure. |
| 674 | */ |
| 675 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 676 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 677 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 678 | { |
| 679 | int parsed = 0, ret; |
| 680 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 681 | assert(id && model && mod_len && name && nam_len); |
| 682 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 683 | if (has_predicate) { |
| 684 | *has_predicate = 0; |
| 685 | } |
| 686 | |
| 687 | if (id[0] != '/') { |
| 688 | return -parsed; |
| 689 | } |
| 690 | |
| 691 | ++parsed; |
| 692 | ++id; |
| 693 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 694 | if ((ret = parse_identifier(id)) < 1) { |
| 695 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 698 | *name = id; |
| 699 | *nam_len = ret; |
| 700 | |
| 701 | parsed += ret; |
| 702 | id += ret; |
| 703 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 704 | if (id[0] == ':') { |
| 705 | /* we have prefix */ |
| 706 | *model = *name; |
| 707 | *mod_len = *nam_len; |
| 708 | |
| 709 | ++parsed; |
| 710 | ++id; |
| 711 | |
| 712 | if ((ret = parse_identifier(id)) < 1) { |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | *name = id; |
| 717 | *nam_len = ret; |
| 718 | |
| 719 | parsed += ret; |
| 720 | id += ret; |
| 721 | } |
| 722 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 723 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 724 | *has_predicate = 1; |
| 725 | } |
| 726 | |
| 727 | return parsed; |
| 728 | } |
| 729 | |
| 730 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 731 | * @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] | 732 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 733 | * |
| 734 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 735 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 736 | * ((DQUOTE string DQUOTE) / |
| 737 | * (SQUOTE string SQUOTE)) |
| 738 | * pos = non-negative-integer-value |
| 739 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 740 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 741 | * @param[out] model Points to the model name. |
| 742 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 743 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 744 | * @param[out] nam_len Length of the node name. |
| 745 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 746 | * NULL if there is not any. |
| 747 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 748 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 749 | * |
| 750 | * @return Number of characters successfully parsed, |
| 751 | * positive on success, negative on failure. |
| 752 | */ |
| 753 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 754 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 755 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 756 | { |
| 757 | const char *ptr; |
| 758 | int parsed = 0, ret; |
| 759 | char quote; |
| 760 | |
| 761 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 762 | if (model) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 763 | assert(mod_len); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 764 | *model = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 765 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 766 | } |
| 767 | if (name) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 768 | assert(nam_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 769 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 770 | *nam_len = 0; |
| 771 | } |
| 772 | if (value) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 773 | assert(val_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 774 | *value = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 775 | *val_len = 0; |
| 776 | } |
| 777 | if (has_predicate) { |
| 778 | *has_predicate = 0; |
| 779 | } |
| 780 | |
| 781 | if (id[0] != '[') { |
| 782 | return -parsed; |
| 783 | } |
| 784 | |
| 785 | ++parsed; |
| 786 | ++id; |
| 787 | |
| 788 | while (isspace(id[0])) { |
| 789 | ++parsed; |
| 790 | ++id; |
| 791 | } |
| 792 | |
| 793 | /* pos */ |
| 794 | if (isdigit(id[0])) { |
| 795 | if (name) { |
| 796 | *name = id; |
| 797 | } |
| 798 | |
| 799 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 800 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | while (isdigit(id[0])) { |
| 804 | ++parsed; |
| 805 | ++id; |
| 806 | } |
| 807 | |
| 808 | if (nam_len) { |
| 809 | *nam_len = id-(*name); |
| 810 | } |
| 811 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 812 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 813 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 814 | if (id[0] == '.') { |
| 815 | if (name) { |
| 816 | *name = id; |
| 817 | } |
| 818 | if (nam_len) { |
| 819 | *nam_len = 1; |
| 820 | } |
| 821 | |
| 822 | ++parsed; |
| 823 | ++id; |
| 824 | |
| 825 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 826 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 827 | return -parsed + ret; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | parsed += ret; |
| 831 | id += ret; |
| 832 | } |
| 833 | |
| 834 | while (isspace(id[0])) { |
| 835 | ++parsed; |
| 836 | ++id; |
| 837 | } |
| 838 | |
| 839 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 840 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 841 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 842 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 843 | ++parsed; |
| 844 | ++id; |
| 845 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 846 | while (isspace(id[0])) { |
| 847 | ++parsed; |
| 848 | ++id; |
| 849 | } |
| 850 | |
| 851 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 852 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 853 | quote = id[0]; |
| 854 | |
| 855 | ++parsed; |
| 856 | ++id; |
| 857 | |
| 858 | if ((ptr = strchr(id, quote)) == NULL) { |
| 859 | return -parsed; |
| 860 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 861 | ret = ptr - id; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 862 | |
| 863 | if (value) { |
| 864 | *value = id; |
| 865 | } |
| 866 | if (val_len) { |
| 867 | *val_len = ret; |
| 868 | } |
| 869 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 870 | parsed += ret + 1; |
| 871 | id += ret + 1; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 872 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 873 | return -parsed; |
| 874 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | while (isspace(id[0])) { |
| 878 | ++parsed; |
| 879 | ++id; |
| 880 | } |
| 881 | |
| 882 | if (id[0] != ']') { |
| 883 | return -parsed; |
| 884 | } |
| 885 | |
| 886 | ++parsed; |
| 887 | ++id; |
| 888 | |
| 889 | if ((id[0] == '[') && has_predicate) { |
| 890 | *has_predicate = 1; |
| 891 | } |
| 892 | |
| 893 | return parsed; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * @brief Parse schema-nodeid. |
| 898 | * |
| 899 | * schema-nodeid = absolute-schema-nodeid / |
| 900 | * descendant-schema-nodeid |
| 901 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 902 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 903 | * node-identifier |
| 904 | * absolute-schema-nodeid |
| 905 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 906 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 907 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 908 | * @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] | 909 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 910 | * @param[out] nam_len Length of the node name. |
| 911 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 912 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 913 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 914 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 915 | * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].). |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 916 | * |
| 917 | * @return Number of characters successfully parsed, |
| 918 | * positive on success, negative on failure. |
| 919 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 920 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 921 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 922 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 923 | { |
| 924 | int parsed = 0, ret; |
| 925 | |
| 926 | assert(id); |
| 927 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 928 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 929 | if (has_predicate) { |
| 930 | *has_predicate = 0; |
| 931 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 932 | |
| 933 | if (id[0] != '/') { |
| 934 | if (*is_relative != -1) { |
| 935 | return -parsed; |
| 936 | } else { |
| 937 | *is_relative = 1; |
| 938 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 939 | if (!strncmp(id, "./", 2)) { |
| 940 | parsed += 2; |
| 941 | id += 2; |
| 942 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 943 | } else { |
| 944 | if (*is_relative == -1) { |
| 945 | *is_relative = 0; |
| 946 | } |
| 947 | ++parsed; |
| 948 | ++id; |
| 949 | } |
| 950 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 951 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 952 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 955 | parsed += ret; |
| 956 | id += ret; |
| 957 | |
| 958 | if ((id[0] == '[') && has_predicate) { |
| 959 | *has_predicate = 1; |
| 960 | } |
| 961 | |
| 962 | return parsed; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @brief Parse schema predicate (special format internally used). |
| 967 | * |
| 968 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 969 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 970 | * key-with-value = identifier *WSP "=" *WSP |
| 971 | * ((DQUOTE string DQUOTE) / |
| 972 | * (SQUOTE string SQUOTE)) |
| 973 | * |
| 974 | * @param[in] id Identifier to use. |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 975 | * @param[out] mod_name Points to the list key module name. |
| 976 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 977 | * @param[out] name Points to the list key name. |
| 978 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 979 | * @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] | 980 | * @param[out] val_len Length of \p value. |
| 981 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 982 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 983 | int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 984 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 985 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 986 | { |
| 987 | const char *ptr; |
| 988 | int parsed = 0, ret; |
| 989 | char quote; |
| 990 | |
| 991 | assert(id); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 992 | if (mod_name) { |
| 993 | *mod_name = NULL; |
| 994 | } |
| 995 | if (mod_name_len) { |
| 996 | *mod_name_len = 0; |
| 997 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 998 | if (name) { |
| 999 | *name = NULL; |
| 1000 | } |
| 1001 | if (nam_len) { |
| 1002 | *nam_len = 0; |
| 1003 | } |
| 1004 | if (value) { |
| 1005 | *value = NULL; |
| 1006 | } |
| 1007 | if (val_len) { |
| 1008 | *val_len = 0; |
| 1009 | } |
| 1010 | if (has_predicate) { |
| 1011 | *has_predicate = 0; |
| 1012 | } |
| 1013 | |
| 1014 | if (id[0] != '[') { |
| 1015 | return -parsed; |
| 1016 | } |
| 1017 | |
| 1018 | ++parsed; |
| 1019 | ++id; |
| 1020 | |
| 1021 | while (isspace(id[0])) { |
| 1022 | ++parsed; |
| 1023 | ++id; |
| 1024 | } |
| 1025 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1026 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1027 | if (id[0] == '.') { |
| 1028 | ret = 1; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1029 | |
| 1030 | if (name) { |
| 1031 | *name = id; |
| 1032 | } |
| 1033 | if (nam_len) { |
| 1034 | *nam_len = ret; |
| 1035 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1036 | } else if (isdigit(id[0])) { |
| 1037 | if (id[0] == '0') { |
| 1038 | return -parsed; |
| 1039 | } |
| 1040 | ret = 1; |
| 1041 | while (isdigit(id[ret])) { |
| 1042 | ++ret; |
| 1043 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1044 | |
| 1045 | if (name) { |
| 1046 | *name = id; |
| 1047 | } |
| 1048 | if (nam_len) { |
| 1049 | *nam_len = ret; |
| 1050 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1051 | } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1052 | return -parsed + ret; |
| 1053 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1054 | |
| 1055 | parsed += ret; |
| 1056 | id += ret; |
| 1057 | |
| 1058 | while (isspace(id[0])) { |
| 1059 | ++parsed; |
| 1060 | ++id; |
| 1061 | } |
| 1062 | |
| 1063 | /* there is value as well */ |
| 1064 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1065 | if (name && isdigit(**name)) { |
| 1066 | return -parsed; |
| 1067 | } |
| 1068 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1069 | ++parsed; |
| 1070 | ++id; |
| 1071 | |
| 1072 | while (isspace(id[0])) { |
| 1073 | ++parsed; |
| 1074 | ++id; |
| 1075 | } |
| 1076 | |
| 1077 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1078 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1079 | quote = id[0]; |
| 1080 | |
| 1081 | ++parsed; |
| 1082 | ++id; |
| 1083 | |
| 1084 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1085 | return -parsed; |
| 1086 | } |
| 1087 | ret = ptr - id; |
| 1088 | |
| 1089 | if (value) { |
| 1090 | *value = id; |
| 1091 | } |
| 1092 | if (val_len) { |
| 1093 | *val_len = ret; |
| 1094 | } |
| 1095 | |
| 1096 | parsed += ret + 1; |
| 1097 | id += ret + 1; |
| 1098 | } else { |
| 1099 | return -parsed; |
| 1100 | } |
| 1101 | |
| 1102 | while (isspace(id[0])) { |
| 1103 | ++parsed; |
| 1104 | ++id; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if (id[0] != ']') { |
| 1109 | return -parsed; |
| 1110 | } |
| 1111 | |
| 1112 | ++parsed; |
| 1113 | ++id; |
| 1114 | |
| 1115 | if ((id[0] == '[') && has_predicate) { |
| 1116 | *has_predicate = 1; |
| 1117 | } |
| 1118 | |
| 1119 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1123 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1124 | * |
| 1125 | * @param[in] feat_name Feature name to resolve. |
| 1126 | * @param[in] len Length of \p feat_name. |
| 1127 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1128 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1129 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1130 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1131 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1132 | */ |
| 1133 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1134 | 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] | 1135 | { |
| 1136 | char *str; |
| 1137 | const char *mod_name, *name; |
| 1138 | int mod_name_len, nam_len, i, j; |
| 1139 | const struct lys_module *module; |
| 1140 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1141 | assert(feature); |
| 1142 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1143 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1144 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1145 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1146 | return -1; |
| 1147 | } |
| 1148 | |
| 1149 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1150 | if (!module) { |
| 1151 | /* identity refers unknown data model */ |
| 1152 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1153 | return -1; |
| 1154 | } |
| 1155 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1156 | if (module != node->module && module == lys_node_module(node)) { |
| 1157 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1158 | for (j = 0; j < node->module->features_size; j++) { |
| 1159 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1160 | /* check status */ |
| 1161 | 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] | 1162 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1163 | return -1; |
| 1164 | } |
| 1165 | *feature = &node->module->features[j]; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1171 | /* search in the identified module ... */ |
| 1172 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1173 | 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] | 1174 | /* check status */ |
| 1175 | 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] | 1176 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1177 | return -1; |
| 1178 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1179 | *feature = &module->features[j]; |
| 1180 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1184 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1185 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1186 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1187 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1188 | /* check status */ |
| 1189 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1190 | module->inc[i].submodule->features[j].flags, |
| 1191 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1192 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1193 | return -1; |
| 1194 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1195 | *feature = &module->inc[i].submodule->features[j]; |
| 1196 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | /* not found */ |
| 1202 | str = strndup(feat_name, len); |
| 1203 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1204 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1205 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1208 | /* |
| 1209 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1210 | * - 1 if enabled |
| 1211 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1212 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1213 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1214 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1215 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1216 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1217 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1218 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1219 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1220 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1221 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1222 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1223 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1224 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1228 | 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] | 1229 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1230 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1231 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1232 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1233 | op = iff_getop(expr->expr, *index_e); |
| 1234 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1235 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1236 | switch (op) { |
| 1237 | case LYS_IFF_F: |
| 1238 | /* resolve feature */ |
| 1239 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1240 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1241 | /* invert result */ |
| 1242 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1243 | case LYS_IFF_AND: |
| 1244 | case LYS_IFF_OR: |
| 1245 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1246 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1247 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1248 | return a && b; |
| 1249 | } else { /* LYS_IFF_OR */ |
| 1250 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1254 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | int |
| 1258 | resolve_iffeature(struct lys_iffeature *expr) |
| 1259 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1260 | int index_e = 0, index_f = 0; |
| 1261 | |
| 1262 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1263 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1264 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1265 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | struct iff_stack { |
| 1269 | int size; |
| 1270 | int index; /* first empty item */ |
| 1271 | uint8_t *stack; |
| 1272 | }; |
| 1273 | |
| 1274 | static int |
| 1275 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1276 | { |
| 1277 | if (stack->index == stack->size) { |
| 1278 | stack->size += 4; |
| 1279 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1280 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | stack->stack[stack->index++] = value; |
| 1284 | return EXIT_SUCCESS; |
| 1285 | } |
| 1286 | |
| 1287 | static uint8_t |
| 1288 | iff_stack_pop(struct iff_stack *stack) |
| 1289 | { |
| 1290 | stack->index--; |
| 1291 | return stack->stack[stack->index]; |
| 1292 | } |
| 1293 | |
| 1294 | static void |
| 1295 | iff_stack_clean(struct iff_stack *stack) |
| 1296 | { |
| 1297 | stack->size = 0; |
| 1298 | free(stack->stack); |
| 1299 | } |
| 1300 | |
| 1301 | static void |
| 1302 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1303 | { |
| 1304 | uint8_t *item; |
| 1305 | uint8_t mask = 3; |
| 1306 | |
| 1307 | assert(pos >= 0); |
| 1308 | assert(op <= 3); /* max 2 bits */ |
| 1309 | |
| 1310 | item = &list[pos / 4]; |
| 1311 | mask = mask << 2 * (pos % 4); |
| 1312 | *item = (*item) & ~mask; |
| 1313 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1314 | } |
| 1315 | |
| 1316 | uint8_t |
| 1317 | iff_getop(uint8_t *list, int pos) |
| 1318 | { |
| 1319 | uint8_t *item; |
| 1320 | uint8_t mask = 3, result; |
| 1321 | |
| 1322 | assert(pos >= 0); |
| 1323 | |
| 1324 | item = &list[pos / 4]; |
| 1325 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1326 | return result >> 2 * (pos % 4); |
| 1327 | } |
| 1328 | |
| 1329 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1330 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1331 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1332 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1333 | struct unres_iffeat_data { |
| 1334 | struct lys_node *node; |
| 1335 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1336 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1337 | }; |
| 1338 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1339 | void |
| 1340 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1341 | { |
| 1342 | unsigned int e = 0, f = 0, r = 0; |
| 1343 | uint8_t op; |
| 1344 | |
| 1345 | assert(iffeat); |
| 1346 | |
| 1347 | if (!iffeat->expr) { |
| 1348 | goto result; |
| 1349 | } |
| 1350 | |
| 1351 | do { |
| 1352 | op = iff_getop(iffeat->expr, e++); |
| 1353 | switch (op) { |
| 1354 | case LYS_IFF_NOT: |
| 1355 | if (!r) { |
| 1356 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1357 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1358 | break; |
| 1359 | case LYS_IFF_AND: |
| 1360 | case LYS_IFF_OR: |
| 1361 | if (!r) { |
| 1362 | r += 2; |
| 1363 | } else { |
| 1364 | r += 1; |
| 1365 | } |
| 1366 | break; |
| 1367 | case LYS_IFF_F: |
| 1368 | f++; |
| 1369 | if (r) { |
| 1370 | r--; |
| 1371 | } |
| 1372 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1373 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1374 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1375 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1376 | result: |
| 1377 | if (expr_size) { |
| 1378 | *expr_size = e; |
| 1379 | } |
| 1380 | if (feat_size) { |
| 1381 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1386 | 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] | 1387 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1388 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1389 | const char *c = value; |
| 1390 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1391 | int i, j, last_not, checkversion = 0; |
| 1392 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1393 | uint8_t op; |
| 1394 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1395 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1396 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1397 | assert(c); |
| 1398 | |
| 1399 | if (isspace(c[0])) { |
| 1400 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1401 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1404 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1405 | for (i = j = last_not = 0; c[i]; i++) { |
| 1406 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1407 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1408 | j++; |
| 1409 | continue; |
| 1410 | } else if (c[i] == ')') { |
| 1411 | j--; |
| 1412 | continue; |
| 1413 | } else if (isspace(c[i])) { |
| 1414 | continue; |
| 1415 | } |
| 1416 | |
| 1417 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1418 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1419 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1420 | return EXIT_FAILURE; |
| 1421 | } else if (!isspace(c[i + r])) { |
| 1422 | /* feature name starting with the not/and/or */ |
| 1423 | last_not = 0; |
| 1424 | f_size++; |
| 1425 | } else if (c[i] == 'n') { /* not operation */ |
| 1426 | if (last_not) { |
| 1427 | /* double not */ |
| 1428 | expr_size = expr_size - 2; |
| 1429 | last_not = 0; |
| 1430 | } else { |
| 1431 | last_not = 1; |
| 1432 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1433 | } else { /* and, or */ |
| 1434 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1435 | /* not a not operation */ |
| 1436 | last_not = 0; |
| 1437 | } |
| 1438 | i += r; |
| 1439 | } else { |
| 1440 | f_size++; |
| 1441 | last_not = 0; |
| 1442 | } |
| 1443 | expr_size++; |
| 1444 | |
| 1445 | while (!isspace(c[i])) { |
| 1446 | if (!c[i] || c[i] == ')') { |
| 1447 | i--; |
| 1448 | break; |
| 1449 | } |
| 1450 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1451 | } |
| 1452 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1453 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1454 | /* not matching count of ( and ) */ |
| 1455 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1456 | return EXIT_FAILURE; |
| 1457 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1458 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1459 | if (checkversion || expr_size > 1) { |
| 1460 | /* check that we have 1.1 module */ |
| 1461 | if (node->module->version != 2) { |
| 1462 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1463 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1464 | return EXIT_FAILURE; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1468 | /* allocate the memory */ |
| 1469 | 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] | 1470 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1471 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1472 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error); |
| 1473 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1474 | f_size--; expr_size--; /* used as indexes from now */ |
| 1475 | |
| 1476 | for (i--; i >= 0; i--) { |
| 1477 | if (c[i] == ')') { |
| 1478 | /* push it on stack */ |
| 1479 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1480 | continue; |
| 1481 | } else if (c[i] == '(') { |
| 1482 | /* pop from the stack into result all operators until ) */ |
| 1483 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1484 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1485 | } |
| 1486 | continue; |
| 1487 | } else if (isspace(c[i])) { |
| 1488 | continue; |
| 1489 | } |
| 1490 | |
| 1491 | /* end operator or operand -> find beginning and get what is it */ |
| 1492 | j = i + 1; |
| 1493 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1494 | i--; |
| 1495 | } |
| 1496 | i++; /* get back by one step */ |
| 1497 | |
| 1498 | if (!strncmp(&c[i], "not ", 4)) { |
| 1499 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1500 | /* double not */ |
| 1501 | iff_stack_pop(&stack); |
| 1502 | } else { |
| 1503 | /* not has the highest priority, so do not pop from the stack |
| 1504 | * as in case of AND and OR */ |
| 1505 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1506 | } |
| 1507 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1508 | /* as for OR - pop from the stack all operators with the same or higher |
| 1509 | * priority and store them to the result, then push the AND to the stack */ |
| 1510 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1511 | op = iff_stack_pop(&stack); |
| 1512 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1513 | } |
| 1514 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1515 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1516 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1517 | op = iff_stack_pop(&stack); |
| 1518 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1519 | } |
| 1520 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1521 | } else { |
| 1522 | /* feature name, length is j - i */ |
| 1523 | |
| 1524 | /* add it to the result */ |
| 1525 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1526 | |
| 1527 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1528 | * forward referenced, we have to keep the feature name in auxiliary |
| 1529 | * structure passed into unres */ |
| 1530 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1531 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1532 | iff_data->node = node; |
| 1533 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1534 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1535 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1536 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1537 | f_size--; |
| 1538 | |
| 1539 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1540 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1541 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1542 | } |
| 1543 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1544 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1545 | while (stack.index) { |
| 1546 | op = iff_stack_pop(&stack); |
| 1547 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1548 | } |
| 1549 | |
| 1550 | if (++expr_size || ++f_size) { |
| 1551 | /* not all expected operators and operands found */ |
| 1552 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1553 | rc = EXIT_FAILURE; |
| 1554 | } else { |
| 1555 | rc = EXIT_SUCCESS; |
| 1556 | } |
| 1557 | |
| 1558 | error: |
| 1559 | /* cleanup */ |
| 1560 | iff_stack_clean(&stack); |
| 1561 | |
| 1562 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1566 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1567 | * |
| 1568 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1569 | * module). |
| 1570 | * |
| 1571 | */ |
| 1572 | struct lyd_node * |
| 1573 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1574 | { |
| 1575 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1576 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1577 | const struct lys_node *schema = NULL; |
| 1578 | |
| 1579 | assert(nodeid && start); |
| 1580 | |
| 1581 | if (nodeid[0] == '/') { |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | |
| 1585 | str = p = strdup(nodeid); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1586 | LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1587 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1588 | while (p) { |
| 1589 | token = p; |
| 1590 | p = strchr(p, '/'); |
| 1591 | if (p) { |
| 1592 | *p = '\0'; |
| 1593 | p++; |
| 1594 | } |
| 1595 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1596 | if (p) { |
| 1597 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1598 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1599 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1600 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1601 | result = NULL; |
| 1602 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1603 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1604 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1605 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1606 | continue; |
| 1607 | } |
| 1608 | } else { |
| 1609 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1610 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1611 | || !schema) { |
| 1612 | result = NULL; |
| 1613 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1614 | } |
| 1615 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1616 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1617 | if (iter->schema == schema) { |
| 1618 | /* move in data tree according to returned schema */ |
| 1619 | result = iter; |
| 1620 | break; |
| 1621 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1622 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1623 | if (!iter) { |
| 1624 | /* instance not found */ |
| 1625 | result = NULL; |
| 1626 | break; |
| 1627 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1628 | } |
| 1629 | free(str); |
| 1630 | |
| 1631 | return result; |
| 1632 | } |
| 1633 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1634 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1635 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1636 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1637 | { |
| 1638 | const struct lys_module *prefix_mod; |
| 1639 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1640 | /* name check */ |
| 1641 | if ((name[0] != '*') && (name[0] != '.') && (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len])) { |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1645 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1646 | if (mod_name) { |
| 1647 | prefix_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
| 1648 | if (!prefix_mod) { |
| 1649 | return -1; |
| 1650 | } |
| 1651 | } else { |
| 1652 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1653 | } |
| 1654 | if (prefix_mod != lys_node_module(sibling)) { |
| 1655 | return 1; |
| 1656 | } |
| 1657 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1658 | /* match */ |
| 1659 | switch (name[0]) { |
| 1660 | case '*': |
| 1661 | return 2; |
| 1662 | case '.': |
| 1663 | return 3; |
| 1664 | default: |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1665 | return 0; |
| 1666 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1669 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1670 | static int |
| 1671 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1672 | const struct lys_module *cur_module, int *nodeid_end) |
| 1673 | { |
| 1674 | int mod_len, nam_len, has_predicate, r, i; |
| 1675 | const char *model, *name; |
| 1676 | struct lys_node_list *list; |
| 1677 | |
| 1678 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1679 | return 1; |
| 1680 | } |
| 1681 | |
| 1682 | list = (struct lys_node_list *)node; |
| 1683 | do { |
| 1684 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1685 | if (r < 1) { |
| 1686 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]); |
| 1687 | return -1; |
| 1688 | } |
| 1689 | nodeid += r; |
| 1690 | |
| 1691 | if (node->nodetype == LYS_LEAFLIST) { |
| 1692 | /* just check syntax */ |
| 1693 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1694 | return 1; |
| 1695 | } |
| 1696 | break; |
| 1697 | } else { |
| 1698 | /* check the key */ |
| 1699 | for (i = 0; i < list->keys_size; ++i) { |
| 1700 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1701 | continue; |
| 1702 | } |
| 1703 | if (model) { |
| 1704 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1705 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1706 | continue; |
| 1707 | } |
| 1708 | } else { |
| 1709 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1710 | continue; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /* match */ |
| 1715 | break; |
| 1716 | } |
| 1717 | |
| 1718 | if (i == list->keys_size) { |
| 1719 | return 1; |
| 1720 | } |
| 1721 | } |
| 1722 | } while (has_predicate); |
| 1723 | |
| 1724 | if (!nodeid[0]) { |
| 1725 | *nodeid_end = 1; |
| 1726 | } |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | /* start - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1731 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1732 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1733 | resolve_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *cur_module, |
| 1734 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1735 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1736 | const char *name, *mod_name, *id; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1737 | const struct lys_node *sibling, *start_parent, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1738 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1739 | int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1740 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 1741 | const struct lys_module *start_mod, *aux_mod = NULL; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1742 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1743 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1744 | assert(nodeid && (start || cur_module) && ret); |
| 1745 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1746 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1747 | if (!cur_module) { |
| 1748 | cur_module = lys_node_module(start); |
| 1749 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1750 | id = nodeid; |
| 1751 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1752 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1753 | (extended ? &all_desc : NULL), extended); |
| 1754 | if (r < 1) { |
| 1755 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1756 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1757 | } |
| 1758 | id += r; |
| 1759 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1760 | if (is_relative && !start) { |
| 1761 | LOGINT; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1762 | return -1; |
| 1763 | } |
| 1764 | |
| 1765 | /* descendant-schema-nodeid */ |
| 1766 | if (is_relative) { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1767 | cur_module = start_mod = start->module; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1768 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1769 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1770 | /* absolute-schema-nodeid */ |
| 1771 | } else { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1772 | start_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1773 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1774 | str = strndup(mod_name, mod_name_len); |
| 1775 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1776 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1777 | return -1; |
| 1778 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1779 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | while (1) { |
| 1783 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1784 | last_aug = NULL; |
| 1785 | |
| 1786 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1787 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1788 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1789 | /* we are getting into another module (augment) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1790 | aux_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1791 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1792 | str = strndup(mod_name, mod_name_len); |
| 1793 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1794 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1795 | return -1; |
| 1796 | } |
| 1797 | } else { |
Michal Vasko | 201c339 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1798 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1799 | * because this module may be not implemented and it augments something in another module and |
| 1800 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1801 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* if the module is implemented, all the augments will be connected */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1805 | if (!aux_mod->implemented && !extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1806 | get_next_augment: |
| 1807 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod, |
Michal Vasko | 5b99790 | 2017-04-03 14:16:22 +0200 | [diff] [blame] | 1812 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1813 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 1814 | |
| 1815 | /* resolve predicate */ |
| 1816 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 1817 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 1818 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1819 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1820 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1821 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1822 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1823 | } else if (!id[0]) { |
| 1824 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1825 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1826 | |
| 1827 | if (r == 0) { |
| 1828 | /* one matching result */ |
| 1829 | if (nodeid_end) { |
| 1830 | *ret = ly_set_new(); |
| 1831 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1832 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1833 | } else { |
| 1834 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1835 | return -1; |
| 1836 | } |
| 1837 | start_parent = sibling; |
| 1838 | } |
| 1839 | break; |
| 1840 | } else if (r == 1) { |
| 1841 | continue; |
| 1842 | } else if (r == 2) { |
| 1843 | /* "*" */ |
| 1844 | if (!*ret) { |
| 1845 | *ret = ly_set_new(); |
| 1846 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1847 | } |
| 1848 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1849 | if (all_desc) { |
| 1850 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1851 | if (elem != sibling) { |
| 1852 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1853 | } |
| 1854 | |
| 1855 | LY_TREE_DFS_END(sibling, next, elem); |
| 1856 | } |
| 1857 | } |
| 1858 | } else if (r == 3) { |
| 1859 | /* "." */ |
| 1860 | if (!*ret) { |
| 1861 | *ret = ly_set_new(); |
| 1862 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1863 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 1864 | } |
| 1865 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1866 | if (all_desc) { |
| 1867 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1868 | if (elem != sibling) { |
| 1869 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1870 | } |
| 1871 | |
| 1872 | LY_TREE_DFS_END(sibling, next, elem); |
| 1873 | } |
| 1874 | } |
| 1875 | } else { |
| 1876 | LOGINT; |
| 1877 | return -1; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | /* skip predicate */ |
| 1882 | if (extended && has_predicate) { |
| 1883 | while (id[0] == '[') { |
| 1884 | id = strchr(id, ']'); |
| 1885 | if (!id) { |
| 1886 | LOGINT; |
| 1887 | return -1; |
| 1888 | } |
| 1889 | ++id; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 1894 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* no match */ |
| 1898 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1899 | if (last_aug) { |
| 1900 | /* it still could be in another augment */ |
| 1901 | goto get_next_augment; |
| 1902 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1903 | if (no_node_error) { |
| 1904 | str = strndup(nodeid, (name - nodeid) + nam_len); |
| 1905 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1906 | free(str); |
| 1907 | return -1; |
| 1908 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1909 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1910 | return EXIT_SUCCESS; |
| 1911 | } |
| 1912 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1913 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1914 | (extended ? &all_desc : NULL), extended); |
| 1915 | if (r < 1) { |
| 1916 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1917 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1918 | } |
| 1919 | id += r; |
| 1920 | } |
| 1921 | |
| 1922 | /* cannot get here */ |
| 1923 | LOGINT; |
| 1924 | return -1; |
| 1925 | } |
| 1926 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1927 | /* unique, refine, |
| 1928 | * >0 - unexpected char on position (ret - 1), |
| 1929 | * 0 - ok (but ret can still be NULL), |
| 1930 | * -1 - error, |
| 1931 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1932 | int |
| 1933 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1934 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1935 | { |
| 1936 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1937 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1938 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1939 | /* 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] | 1940 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1941 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1942 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1943 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1944 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1945 | if (!start) { |
| 1946 | /* leaf not found */ |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1950 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1951 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1952 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1953 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1954 | return ((id - nodeid) - r) + 1; |
| 1955 | } |
| 1956 | id += r; |
| 1957 | |
| 1958 | if (!is_relative) { |
| 1959 | return -1; |
| 1960 | } |
| 1961 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1962 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1963 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1964 | start_parent = lys_parent(start_parent); |
| 1965 | } |
| 1966 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1967 | while (1) { |
| 1968 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1969 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1970 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1971 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 1972 | if (r == 0) { |
| 1973 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1974 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1975 | /* wrong node type, too bad */ |
| 1976 | continue; |
| 1977 | } |
| 1978 | *ret = sibling; |
| 1979 | return EXIT_SUCCESS; |
| 1980 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1981 | start_parent = sibling; |
| 1982 | break; |
| 1983 | } else if (r == 1) { |
| 1984 | continue; |
| 1985 | } else { |
| 1986 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | /* no match */ |
| 1991 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1992 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1993 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1994 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1995 | *ret = NULL; |
| 1996 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1999 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2000 | return ((id - nodeid) - r) + 1; |
| 2001 | } |
| 2002 | id += r; |
| 2003 | } |
| 2004 | |
| 2005 | /* cannot get here */ |
| 2006 | LOGINT; |
| 2007 | return -1; |
| 2008 | } |
| 2009 | |
| 2010 | /* choice default */ |
| 2011 | int |
| 2012 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2013 | { |
| 2014 | /* cannot actually be a path */ |
| 2015 | if (strchr(nodeid, '/')) { |
| 2016 | return -1; |
| 2017 | } |
| 2018 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2019 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2023 | static int |
| 2024 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2025 | { |
| 2026 | const struct lys_module *module; |
| 2027 | const char *mod_prefix, *name; |
| 2028 | int i, mod_prefix_len, nam_len; |
| 2029 | |
| 2030 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2031 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len, NULL, 0)) < 1) || nodeid[i]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2032 | return -i + 1; |
| 2033 | } |
| 2034 | |
| 2035 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 2036 | if (!module) { |
| 2037 | return -1; |
| 2038 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2039 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2040 | start = module->data; |
| 2041 | } |
| 2042 | |
| 2043 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2044 | |
| 2045 | return EXIT_SUCCESS; |
| 2046 | } |
| 2047 | |
| 2048 | int |
| 2049 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2050 | const struct lys_node **ret) |
| 2051 | { |
| 2052 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2053 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2054 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2055 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2056 | |
| 2057 | assert(nodeid && module && ret); |
| 2058 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2059 | |
| 2060 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2061 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2062 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2063 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2064 | return ((id - nodeid) - r) + 1; |
| 2065 | } |
| 2066 | id += r; |
| 2067 | |
| 2068 | if (is_relative) { |
| 2069 | return -1; |
| 2070 | } |
| 2071 | |
| 2072 | 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] | 2073 | if (!abs_start_mod) { |
| 2074 | return -1; |
| 2075 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2076 | |
| 2077 | while (1) { |
| 2078 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2079 | while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2080 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2081 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2082 | if (r == 0) { |
| 2083 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2084 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2085 | /* wrong node type, too bad */ |
| 2086 | continue; |
| 2087 | } |
| 2088 | *ret = sibling; |
| 2089 | return EXIT_SUCCESS; |
| 2090 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2091 | start_parent = sibling; |
| 2092 | break; |
| 2093 | } else if (r == 1) { |
| 2094 | continue; |
| 2095 | } else { |
| 2096 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | /* no match */ |
| 2101 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2102 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2103 | return EXIT_SUCCESS; |
| 2104 | } |
| 2105 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2106 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2107 | return ((id - nodeid) - r) + 1; |
| 2108 | } |
| 2109 | id += r; |
| 2110 | } |
| 2111 | |
| 2112 | /* cannot get here */ |
| 2113 | LOGINT; |
| 2114 | return -1; |
| 2115 | } |
| 2116 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2117 | static int |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2118 | 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] | 2119 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2120 | const char *mod_name, *name; |
| 2121 | int mod_name_len, nam_len, has_predicate, i; |
| 2122 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2123 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2124 | if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2125 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2126 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2127 | return -1; |
| 2128 | } |
| 2129 | |
| 2130 | predicate += i; |
| 2131 | *parsed += i; |
| 2132 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2133 | if (!isdigit(name[0])) { |
| 2134 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2135 | key = (struct lys_node *)list->keys[i]; |
| 2136 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2137 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2138 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2139 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2140 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2141 | if (i == list->keys_size) { |
| 2142 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2143 | return -1; |
| 2144 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* more predicates? */ |
| 2148 | if (has_predicate) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2149 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2155 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2156 | const struct lys_node * |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2157 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2158 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2159 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2160 | const char *name, *mod_name, *id; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2161 | const struct lys_node *sibling, *start_parent, *parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2162 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2163 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2164 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2165 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2166 | assert(nodeid && (ctx || start)); |
| 2167 | if (!ctx) { |
| 2168 | ctx = start->module->ctx; |
| 2169 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2170 | |
| 2171 | id = nodeid; |
| 2172 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2173 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2174 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2175 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2176 | } |
| 2177 | id += r; |
| 2178 | |
| 2179 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2180 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2181 | start_parent = start; |
| 2182 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2183 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2184 | } |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2185 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2186 | } else { |
| 2187 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2188 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2189 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2190 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2191 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2192 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2193 | LOGINT; |
| 2194 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2195 | } |
| 2196 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2197 | if (ly_buf_used && module_name[0]) { |
| 2198 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2199 | } |
| 2200 | ly_buf_used++; |
| 2201 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2202 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2203 | module_name[mod_name_len] = '\0'; |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 2204 | module = ly_ctx_get_module(ctx, module_name, NULL, 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2205 | |
| 2206 | if (buf_backup) { |
| 2207 | /* return previous internal buffer content */ |
| 2208 | strcpy(module_name, buf_backup); |
| 2209 | free(buf_backup); |
| 2210 | buf_backup = NULL; |
| 2211 | } |
| 2212 | ly_buf_used--; |
| 2213 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2214 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2215 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2216 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2217 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2218 | return NULL; |
| 2219 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2220 | start_parent = NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2221 | |
| 2222 | /* now it's as if there was no module name */ |
| 2223 | mod_name = NULL; |
| 2224 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2227 | prev_mod = module; |
| 2228 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2229 | while (1) { |
| 2230 | sibling = NULL; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2231 | while ((sibling = lys_getnext(sibling, start_parent, module, 0))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2232 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2233 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2234 | /* output check */ |
| 2235 | for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2236 | if (parent) { |
| 2237 | if (output && (parent->nodetype == LYS_INPUT)) { |
| 2238 | continue; |
| 2239 | } else if (!output && (parent->nodetype == LYS_OUTPUT)) { |
| 2240 | continue; |
| 2241 | } |
| 2242 | } |
| 2243 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2244 | /* module check */ |
| 2245 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2246 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2247 | LOGINT; |
| 2248 | return NULL; |
| 2249 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2250 | |
| 2251 | if (ly_buf_used && module_name[0]) { |
| 2252 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2253 | } |
| 2254 | ly_buf_used++; |
| 2255 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2256 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2257 | module_name[mod_name_len] = '\0'; |
| 2258 | /* will also find an augment module */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 2259 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL, 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2260 | |
| 2261 | if (buf_backup) { |
| 2262 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2263 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2264 | free(buf_backup); |
| 2265 | buf_backup = NULL; |
| 2266 | } |
| 2267 | ly_buf_used--; |
| 2268 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2269 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2270 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2271 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2272 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2273 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2274 | } |
| 2275 | } else { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2276 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2277 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2278 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2279 | continue; |
| 2280 | } |
| 2281 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2282 | /* do we have some predicates on it? */ |
| 2283 | if (has_predicate) { |
| 2284 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2285 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2286 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2287 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2288 | return NULL; |
| 2289 | } |
| 2290 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2291 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2292 | return NULL; |
| 2293 | } |
| 2294 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2295 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2296 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2297 | } |
| 2298 | id += r; |
| 2299 | } |
| 2300 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2301 | /* the result node? */ |
| 2302 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2303 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2306 | /* move down the tree, if possible */ |
| 2307 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2308 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2309 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2310 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2311 | start_parent = sibling; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2312 | |
| 2313 | /* update prev mod */ |
| 2314 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2315 | break; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /* no match */ |
| 2320 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2321 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2322 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2323 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2324 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2327 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2328 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2329 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2330 | } |
| 2331 | id += r; |
| 2332 | } |
| 2333 | |
| 2334 | /* cannot get here */ |
| 2335 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2336 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2339 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2340 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2341 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2342 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2343 | const char *mod_name, *name, *value, *key_val; |
| 2344 | int mod_name_len, nam_len, val_len, has_predicate = 1, r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2345 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2346 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2347 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2348 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2349 | assert(node->schema->nodetype == LYS_LIST); |
| 2350 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2351 | /* is the predicate a number? */ |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2352 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2353 | || !strncmp(name, ".", nam_len)) { |
| 2354 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2355 | return -1; |
| 2356 | } |
| 2357 | |
| 2358 | if (isdigit(name[0])) { |
| 2359 | if (position == atoi(name)) { |
| 2360 | /* match */ |
| 2361 | *parsed += r; |
| 2362 | return 0; |
| 2363 | } else { |
| 2364 | /* not a match */ |
| 2365 | return 1; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2370 | /* no keys in schema - causes an error later */ |
| 2371 | return 0; |
| 2372 | } |
| 2373 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2374 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2375 | if (!key) { |
| 2376 | /* it is not a position, so we need a key for it to be a match */ |
| 2377 | return 1; |
| 2378 | } |
| 2379 | |
| 2380 | /* go through all the keys */ |
| 2381 | i = 0; |
| 2382 | goto check_parsed_values; |
| 2383 | |
| 2384 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2385 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2386 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2387 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2388 | } |
| 2389 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2390 | if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2391 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2392 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2393 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2394 | } |
| 2395 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2396 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2397 | predicate += r; |
| 2398 | *parsed += r; |
| 2399 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2400 | 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] | 2401 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2402 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2405 | if (mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2406 | /* specific module, check that the found key is from that module */ |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2407 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len) |
| 2408 | || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) { |
| 2409 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2410 | return -1; |
| 2411 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2412 | |
| 2413 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2414 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
| 2415 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2416 | return -1; |
| 2417 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2418 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2419 | /* no module, so it must be the same as the list (parent) */ |
| 2420 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2421 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2422 | return -1; |
| 2423 | } |
| 2424 | } |
| 2425 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2426 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2427 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2428 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2429 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2430 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2431 | } else { |
| 2432 | key_val = key->value_str; |
| 2433 | } |
| 2434 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2435 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2436 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2437 | return 1; |
| 2438 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2439 | |
| 2440 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2444 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2445 | return -1; |
| 2446 | } |
| 2447 | |
| 2448 | return 0; |
| 2449 | } |
| 2450 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2451 | /** |
| 2452 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2453 | * |
| 2454 | * @param[in] nodeid Node data path to find |
| 2455 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2456 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2457 | * @param[out] parsed Number of characters processed in \p id |
| 2458 | * @return The closes parent (or the node itself) from the path |
| 2459 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2460 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2461 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2462 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2463 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2464 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2465 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2466 | 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] | 2467 | 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] | 2468 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2469 | struct lyd_node_leaf_list *llist; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2470 | const struct lys_module *prefix_mod, *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2471 | struct ly_ctx *ctx; |
| 2472 | |
| 2473 | assert(nodeid && start && parsed); |
| 2474 | |
| 2475 | ctx = start->schema->module->ctx; |
| 2476 | id = nodeid; |
| 2477 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2478 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2479 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2480 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2481 | return NULL; |
| 2482 | } |
| 2483 | id += r; |
| 2484 | /* add it to parsed only after the data node was actually found */ |
| 2485 | last_parsed = r; |
| 2486 | |
| 2487 | if (is_relative) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2488 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2489 | start = start->child; |
| 2490 | } else { |
| 2491 | for (; start->parent; start = start->parent); |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2492 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2496 | list_instance_position = 0; |
| 2497 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2498 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2499 | /* 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] | 2500 | if (lys_parent(sibling->schema)) { |
| 2501 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2502 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2503 | 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] | 2504 | *parsed = -1; |
| 2505 | return NULL; |
| 2506 | } |
| 2507 | } else { |
| 2508 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2509 | 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] | 2510 | *parsed = -1; |
| 2511 | return NULL; |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2516 | /* name match */ |
| 2517 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2518 | |
| 2519 | /* module check */ |
| 2520 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2521 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2522 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2523 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2524 | return NULL; |
| 2525 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2526 | |
| 2527 | if (ly_buf_used && module_name[0]) { |
| 2528 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2529 | } |
| 2530 | ly_buf_used++; |
| 2531 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2532 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2533 | module_name[mod_name_len] = '\0'; |
| 2534 | /* will also find an augment module */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 2535 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL, 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2536 | |
| 2537 | if (buf_backup) { |
| 2538 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2539 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2540 | free(buf_backup); |
| 2541 | buf_backup = NULL; |
| 2542 | } |
| 2543 | ly_buf_used--; |
| 2544 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2545 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2546 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2547 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2548 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2549 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2550 | return NULL; |
| 2551 | } |
| 2552 | } else { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2553 | prefix_mod = prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2554 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2555 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2556 | continue; |
| 2557 | } |
| 2558 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2559 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2560 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2561 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2562 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2563 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2564 | if (has_predicate) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2565 | if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value, |
| 2566 | &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2567 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2568 | *parsed = -1; |
| 2569 | return NULL; |
| 2570 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2571 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2572 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2573 | *parsed = -1; |
| 2574 | return NULL; |
| 2575 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2576 | } else { |
| 2577 | r = 0; |
| 2578 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2579 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
Michal Vasko | 21b90ce | 2017-09-19 09:38:27 +0200 | [diff] [blame] | 2583 | /* make value canonical (remove module name prefix) unless it was specified with it */ |
Michal Vasko | d6d5129 | 2017-09-22 14:30:48 +0200 | [diff] [blame] | 2584 | if (llist_value && !strchr(llist_value, ':') && (llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2585 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2586 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2587 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2588 | } else { |
| 2589 | data_val = llist->value_str; |
| 2590 | } |
| 2591 | |
| 2592 | if ((!llist_value && data_val && data_val[0]) |
| 2593 | || (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] | 2594 | continue; |
| 2595 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2596 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2597 | id += r; |
| 2598 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2599 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2600 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2601 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2602 | /* 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] | 2603 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2604 | /* none match */ |
| 2605 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2606 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2607 | |
| 2608 | ++list_instance_position; |
| 2609 | r = 0; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2610 | 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] | 2611 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2612 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2613 | return NULL; |
| 2614 | } else if (ret == 1) { |
| 2615 | /* this list instance does not match */ |
| 2616 | continue; |
| 2617 | } |
| 2618 | id += r; |
| 2619 | last_parsed += r; |
| 2620 | } |
| 2621 | |
| 2622 | *parsed += last_parsed; |
| 2623 | |
| 2624 | /* the result node? */ |
| 2625 | if (!id[0]) { |
| 2626 | return sibling; |
| 2627 | } |
| 2628 | |
| 2629 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2630 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2631 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2632 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2633 | return NULL; |
| 2634 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2635 | last_match = sibling; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2636 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2637 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2638 | break; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | /* no match, return last match */ |
| 2643 | if (!sibling) { |
| 2644 | return last_match; |
| 2645 | } |
| 2646 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2647 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2648 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2649 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2650 | return NULL; |
| 2651 | } |
| 2652 | id += r; |
| 2653 | last_parsed = r; |
| 2654 | } |
| 2655 | |
| 2656 | /* cannot get here */ |
| 2657 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2658 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2659 | return NULL; |
| 2660 | } |
| 2661 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2662 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2663 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2664 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2665 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2666 | * @param[in] str_restr Restriction as a string. |
| 2667 | * @param[in] type Type of the restriction. |
| 2668 | * @param[out] ret Final interval structure that starts with |
| 2669 | * the interval of the initial type, continues with intervals |
| 2670 | * of any superior types derived from the initial one, and |
| 2671 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2672 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2673 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2674 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2675 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2676 | 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] | 2677 | { |
| 2678 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2679 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2680 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2681 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2682 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2683 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2684 | 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] | 2685 | |
| 2686 | switch (type->base) { |
| 2687 | case LY_TYPE_BINARY: |
| 2688 | kind = 0; |
| 2689 | local_umin = 0; |
| 2690 | local_umax = 18446744073709551615UL; |
| 2691 | |
| 2692 | if (!str_restr && type->info.binary.length) { |
| 2693 | str_restr = type->info.binary.length->expr; |
| 2694 | } |
| 2695 | break; |
| 2696 | case LY_TYPE_DEC64: |
| 2697 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2698 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2699 | local_fmax = __INT64_C(9223372036854775807); |
| 2700 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2701 | |
| 2702 | if (!str_restr && type->info.dec64.range) { |
| 2703 | str_restr = type->info.dec64.range->expr; |
| 2704 | } |
| 2705 | break; |
| 2706 | case LY_TYPE_INT8: |
| 2707 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2708 | local_smin = __INT64_C(-128); |
| 2709 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2710 | |
| 2711 | if (!str_restr && type->info.num.range) { |
| 2712 | str_restr = type->info.num.range->expr; |
| 2713 | } |
| 2714 | break; |
| 2715 | case LY_TYPE_INT16: |
| 2716 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2717 | local_smin = __INT64_C(-32768); |
| 2718 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2719 | |
| 2720 | if (!str_restr && type->info.num.range) { |
| 2721 | str_restr = type->info.num.range->expr; |
| 2722 | } |
| 2723 | break; |
| 2724 | case LY_TYPE_INT32: |
| 2725 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2726 | local_smin = __INT64_C(-2147483648); |
| 2727 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2728 | |
| 2729 | if (!str_restr && type->info.num.range) { |
| 2730 | str_restr = type->info.num.range->expr; |
| 2731 | } |
| 2732 | break; |
| 2733 | case LY_TYPE_INT64: |
| 2734 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2735 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2736 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2737 | |
| 2738 | if (!str_restr && type->info.num.range) { |
| 2739 | str_restr = type->info.num.range->expr; |
| 2740 | } |
| 2741 | break; |
| 2742 | case LY_TYPE_UINT8: |
| 2743 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2744 | local_umin = __UINT64_C(0); |
| 2745 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2746 | |
| 2747 | if (!str_restr && type->info.num.range) { |
| 2748 | str_restr = type->info.num.range->expr; |
| 2749 | } |
| 2750 | break; |
| 2751 | case LY_TYPE_UINT16: |
| 2752 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2753 | local_umin = __UINT64_C(0); |
| 2754 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2755 | |
| 2756 | if (!str_restr && type->info.num.range) { |
| 2757 | str_restr = type->info.num.range->expr; |
| 2758 | } |
| 2759 | break; |
| 2760 | case LY_TYPE_UINT32: |
| 2761 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2762 | local_umin = __UINT64_C(0); |
| 2763 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2764 | |
| 2765 | if (!str_restr && type->info.num.range) { |
| 2766 | str_restr = type->info.num.range->expr; |
| 2767 | } |
| 2768 | break; |
| 2769 | case LY_TYPE_UINT64: |
| 2770 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2771 | local_umin = __UINT64_C(0); |
| 2772 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2773 | |
| 2774 | if (!str_restr && type->info.num.range) { |
| 2775 | str_restr = type->info.num.range->expr; |
| 2776 | } |
| 2777 | break; |
| 2778 | case LY_TYPE_STRING: |
| 2779 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2780 | local_umin = __UINT64_C(0); |
| 2781 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2782 | |
| 2783 | if (!str_restr && type->info.str.length) { |
| 2784 | str_restr = type->info.str.length->expr; |
| 2785 | } |
| 2786 | break; |
| 2787 | default: |
| 2788 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2789 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2793 | if (type->der) { |
| 2794 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2795 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2796 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2797 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2798 | assert(!intv || (intv->kind == kind)); |
| 2799 | } |
| 2800 | |
| 2801 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2802 | /* we do not have any restriction, return superior ones */ |
| 2803 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2804 | return EXIT_SUCCESS; |
| 2805 | } |
| 2806 | |
| 2807 | /* adjust local min and max */ |
| 2808 | if (intv) { |
| 2809 | tmp_intv = intv; |
| 2810 | |
| 2811 | if (kind == 0) { |
| 2812 | local_umin = tmp_intv->value.uval.min; |
| 2813 | } else if (kind == 1) { |
| 2814 | local_smin = tmp_intv->value.sval.min; |
| 2815 | } else if (kind == 2) { |
| 2816 | local_fmin = tmp_intv->value.fval.min; |
| 2817 | } |
| 2818 | |
| 2819 | while (tmp_intv->next) { |
| 2820 | tmp_intv = tmp_intv->next; |
| 2821 | } |
| 2822 | |
| 2823 | if (kind == 0) { |
| 2824 | local_umax = tmp_intv->value.uval.max; |
| 2825 | } else if (kind == 1) { |
| 2826 | local_smax = tmp_intv->value.sval.max; |
| 2827 | } else if (kind == 2) { |
| 2828 | local_fmax = tmp_intv->value.fval.max; |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | /* finally parse our restriction */ |
| 2833 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2834 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2835 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2836 | if (!tmp_local_intv) { |
| 2837 | assert(!local_intv); |
| 2838 | local_intv = malloc(sizeof *local_intv); |
| 2839 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2840 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2841 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2842 | tmp_local_intv = tmp_local_intv->next; |
| 2843 | } |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2844 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2845 | |
| 2846 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2847 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2848 | tmp_local_intv->next = NULL; |
| 2849 | |
| 2850 | /* min */ |
| 2851 | ptr = seg_ptr; |
| 2852 | while (isspace(ptr[0])) { |
| 2853 | ++ptr; |
| 2854 | } |
| 2855 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2856 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2857 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2858 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2859 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2860 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2861 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2862 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2863 | goto error; |
| 2864 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2865 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2866 | } else if (!strncmp(ptr, "min", 3)) { |
| 2867 | if (kind == 0) { |
| 2868 | tmp_local_intv->value.uval.min = local_umin; |
| 2869 | } else if (kind == 1) { |
| 2870 | tmp_local_intv->value.sval.min = local_smin; |
| 2871 | } else if (kind == 2) { |
| 2872 | tmp_local_intv->value.fval.min = local_fmin; |
| 2873 | } |
| 2874 | |
| 2875 | ptr += 3; |
| 2876 | } else if (!strncmp(ptr, "max", 3)) { |
| 2877 | if (kind == 0) { |
| 2878 | tmp_local_intv->value.uval.min = local_umax; |
| 2879 | } else if (kind == 1) { |
| 2880 | tmp_local_intv->value.sval.min = local_smax; |
| 2881 | } else if (kind == 2) { |
| 2882 | tmp_local_intv->value.fval.min = local_fmax; |
| 2883 | } |
| 2884 | |
| 2885 | ptr += 3; |
| 2886 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2887 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2888 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | while (isspace(ptr[0])) { |
| 2892 | ptr++; |
| 2893 | } |
| 2894 | |
| 2895 | /* no interval or interval */ |
| 2896 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2897 | if (kind == 0) { |
| 2898 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2899 | } else if (kind == 1) { |
| 2900 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2901 | } else if (kind == 2) { |
| 2902 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2903 | } |
| 2904 | } else if (!strncmp(ptr, "..", 2)) { |
| 2905 | /* skip ".." */ |
| 2906 | ptr += 2; |
| 2907 | while (isspace(ptr[0])) { |
| 2908 | ++ptr; |
| 2909 | } |
| 2910 | |
| 2911 | /* max */ |
| 2912 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2913 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2914 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2915 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2916 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2917 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2918 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2919 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2920 | goto error; |
| 2921 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2922 | } |
| 2923 | } else if (!strncmp(ptr, "max", 3)) { |
| 2924 | if (kind == 0) { |
| 2925 | tmp_local_intv->value.uval.max = local_umax; |
| 2926 | } else if (kind == 1) { |
| 2927 | tmp_local_intv->value.sval.max = local_smax; |
| 2928 | } else if (kind == 2) { |
| 2929 | tmp_local_intv->value.fval.max = local_fmax; |
| 2930 | } |
| 2931 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2932 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2933 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2934 | } |
| 2935 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2936 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2937 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2940 | /* check min and max in correct order*/ |
| 2941 | if (kind == 0) { |
| 2942 | /* current segment */ |
| 2943 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2944 | goto error; |
| 2945 | } |
| 2946 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2947 | goto error; |
| 2948 | } |
| 2949 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2950 | 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] | 2951 | goto error; |
| 2952 | } |
| 2953 | } else if (kind == 1) { |
| 2954 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2955 | goto error; |
| 2956 | } |
| 2957 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2958 | goto error; |
| 2959 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2960 | 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] | 2961 | goto error; |
| 2962 | } |
| 2963 | } else if (kind == 2) { |
| 2964 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2965 | goto error; |
| 2966 | } |
| 2967 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2968 | goto error; |
| 2969 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2970 | 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] | 2971 | /* 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] | 2972 | goto error; |
| 2973 | } |
| 2974 | } |
| 2975 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2976 | /* next segment (next OR) */ |
| 2977 | seg_ptr = strchr(seg_ptr, '|'); |
| 2978 | if (!seg_ptr) { |
| 2979 | break; |
| 2980 | } |
| 2981 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2982 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | /* check local restrictions against superior ones */ |
| 2986 | if (intv) { |
| 2987 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2988 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2989 | |
| 2990 | while (tmp_local_intv && tmp_intv) { |
| 2991 | /* reuse local variables */ |
| 2992 | if (kind == 0) { |
| 2993 | local_umin = tmp_local_intv->value.uval.min; |
| 2994 | local_umax = tmp_local_intv->value.uval.max; |
| 2995 | |
| 2996 | /* it must be in this interval */ |
| 2997 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2998 | /* this interval is covered, next one */ |
| 2999 | if (local_umax <= tmp_intv->value.uval.max) { |
| 3000 | tmp_local_intv = tmp_local_intv->next; |
| 3001 | continue; |
| 3002 | /* ascending order of restrictions -> fail */ |
| 3003 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3004 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | } else if (kind == 1) { |
| 3008 | local_smin = tmp_local_intv->value.sval.min; |
| 3009 | local_smax = tmp_local_intv->value.sval.max; |
| 3010 | |
| 3011 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 3012 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3013 | tmp_local_intv = tmp_local_intv->next; |
| 3014 | continue; |
| 3015 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3016 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3017 | } |
| 3018 | } |
| 3019 | } else if (kind == 2) { |
| 3020 | local_fmin = tmp_local_intv->value.fval.min; |
| 3021 | local_fmax = tmp_local_intv->value.fval.max; |
| 3022 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3023 | 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] | 3024 | && (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] | 3025 | 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] | 3026 | tmp_local_intv = tmp_local_intv->next; |
| 3027 | continue; |
| 3028 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3029 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | tmp_intv = tmp_intv->next; |
| 3035 | } |
| 3036 | |
| 3037 | /* some interval left uncovered -> fail */ |
| 3038 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3039 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3040 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3043 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3044 | if (intv) { |
| 3045 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3046 | tmp_intv->next = local_intv; |
| 3047 | } else { |
| 3048 | intv = local_intv; |
| 3049 | } |
| 3050 | *ret = intv; |
| 3051 | |
| 3052 | return EXIT_SUCCESS; |
| 3053 | |
| 3054 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3055 | while (intv) { |
| 3056 | tmp_intv = intv->next; |
| 3057 | free(intv); |
| 3058 | intv = tmp_intv; |
| 3059 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3060 | while (local_intv) { |
| 3061 | tmp_local_intv = local_intv->next; |
| 3062 | free(local_intv); |
| 3063 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3064 | } |
| 3065 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3066 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3067 | } |
| 3068 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3069 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3070 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3071 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3072 | * |
| 3073 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3074 | * @param[in] mod_name Typedef name module name. |
| 3075 | * @param[in] module Main module. |
| 3076 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3077 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3078 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3079 | * @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] | 3080 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3081 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3082 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3083 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3084 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3085 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3086 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3087 | int tpdf_size; |
| 3088 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3089 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3090 | /* no prefix, try built-in types */ |
| 3091 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3092 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3093 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3094 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3095 | } |
| 3096 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3100 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3101 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3102 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3103 | } |
| 3104 | } |
| 3105 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3106 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3107 | /* search in local typedefs */ |
| 3108 | while (parent) { |
| 3109 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3110 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3111 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3112 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3113 | break; |
| 3114 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3115 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3116 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3117 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3118 | break; |
| 3119 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3120 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3121 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3122 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3123 | break; |
| 3124 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3125 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3126 | case LYS_ACTION: |
| 3127 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3128 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3129 | break; |
| 3130 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3131 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3132 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3133 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3134 | break; |
| 3135 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3136 | case LYS_INPUT: |
| 3137 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3138 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3139 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3140 | break; |
| 3141 | |
| 3142 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3143 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3144 | continue; |
| 3145 | } |
| 3146 | |
| 3147 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3148 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3149 | match = &tpdf[i]; |
| 3150 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3154 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3155 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3156 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3157 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 3158 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3159 | if (!module) { |
| 3160 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | /* search in top level typedefs */ |
| 3165 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3166 | 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] | 3167 | match = &module->tpdf[i]; |
| 3168 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3173 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3174 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3175 | 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] | 3176 | match = &module->inc[i].submodule->tpdf[j]; |
| 3177 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3182 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3183 | |
| 3184 | check_leafref: |
| 3185 | if (ret) { |
| 3186 | *ret = match; |
| 3187 | } |
| 3188 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3189 | while (!match->type.info.lref.path) { |
| 3190 | match = match->type.der; |
| 3191 | assert(match); |
| 3192 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3193 | } |
| 3194 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3195 | } |
| 3196 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3197 | /** |
| 3198 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3199 | * |
| 3200 | * @param[in] type Type definition to use. |
| 3201 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3202 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3203 | * |
| 3204 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3205 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3206 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3207 | check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3208 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3209 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3210 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3211 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3212 | char *s; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3213 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3214 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3215 | assert(value); |
| 3216 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3217 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3218 | /* the type was not resolved yet, nothing to do for now */ |
| 3219 | return EXIT_FAILURE; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3220 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3221 | /* do not check defaults in not implemented module's data */ |
| 3222 | return EXIT_SUCCESS; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3223 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3224 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3225 | * but in typedef the value has to be modified to include the prefix */ |
| 3226 | if (*value) { |
| 3227 | if (strchr(*value, ':')) { |
| 3228 | dflt = transform_schema2json(module, *value); |
| 3229 | } else { |
| 3230 | /* default prefix of the module where the typedef is defined */ |
| 3231 | asprintf(&s, "%s:%s", lys_main_module(module)->name, *value); |
| 3232 | dflt = lydict_insert_zc(module->ctx, s); |
| 3233 | } |
| 3234 | lydict_remove(module->ctx, *value); |
| 3235 | *value = dflt; |
| 3236 | } |
| 3237 | return EXIT_SUCCESS; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3238 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3239 | /* leafref in typedef cannot be checked */ |
| 3240 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3241 | } |
| 3242 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3243 | dflt = *value; |
| 3244 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3245 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3246 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3247 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3248 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3249 | break; |
| 3250 | } |
| 3251 | } |
| 3252 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3253 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3254 | /* no default value, nothing to check, all is well */ |
| 3255 | return EXIT_SUCCESS; |
| 3256 | } |
| 3257 | |
| 3258 | /* 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)? */ |
| 3259 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3260 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3261 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
| 3262 | return EXIT_SUCCESS; |
| 3263 | } else { |
| 3264 | /* check the default value from typedef, but use also the typedef's module |
| 3265 | * due to possible searching in imported modules which is expected in |
| 3266 | * typedef's module instead of module where the typedef is used */ |
| 3267 | module = base_tpdf->module; |
| 3268 | } |
| 3269 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3270 | case LY_TYPE_INST: |
| 3271 | case LY_TYPE_LEAFREF: |
| 3272 | case LY_TYPE_BOOL: |
| 3273 | case LY_TYPE_EMPTY: |
| 3274 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3275 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3276 | case LY_TYPE_BITS: |
| 3277 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3278 | if (type->info.bits.count) { |
| 3279 | break; |
| 3280 | } |
| 3281 | return EXIT_SUCCESS; |
| 3282 | case LY_TYPE_ENUM: |
| 3283 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3284 | if (type->info.enums.count) { |
| 3285 | break; |
| 3286 | } |
| 3287 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3288 | case LY_TYPE_DEC64: |
| 3289 | if (type->info.dec64.range) { |
| 3290 | break; |
| 3291 | } |
| 3292 | return EXIT_SUCCESS; |
| 3293 | case LY_TYPE_BINARY: |
| 3294 | if (type->info.binary.length) { |
| 3295 | break; |
| 3296 | } |
| 3297 | return EXIT_SUCCESS; |
| 3298 | case LY_TYPE_INT8: |
| 3299 | case LY_TYPE_INT16: |
| 3300 | case LY_TYPE_INT32: |
| 3301 | case LY_TYPE_INT64: |
| 3302 | case LY_TYPE_UINT8: |
| 3303 | case LY_TYPE_UINT16: |
| 3304 | case LY_TYPE_UINT32: |
| 3305 | case LY_TYPE_UINT64: |
| 3306 | if (type->info.num.range) { |
| 3307 | break; |
| 3308 | } |
| 3309 | return EXIT_SUCCESS; |
| 3310 | case LY_TYPE_STRING: |
| 3311 | if (type->info.str.length || type->info.str.patterns) { |
| 3312 | break; |
| 3313 | } |
| 3314 | return EXIT_SUCCESS; |
| 3315 | case LY_TYPE_UNION: |
| 3316 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3317 | break; |
| 3318 | default: |
| 3319 | LOGINT; |
| 3320 | return -1; |
| 3321 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3322 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3323 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3324 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3325 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3328 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3329 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3330 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3331 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3332 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3333 | LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3334 | node.schema->name = strdup("fake-default"); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3335 | LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1); |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3336 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3337 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3338 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3339 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3340 | if (!type->info.lref.target) { |
| 3341 | ret = EXIT_FAILURE; |
| 3342 | goto finish; |
| 3343 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3344 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3345 | if (!ret) { |
| 3346 | /* adopt possibly changed default value to its canonical form */ |
| 3347 | if (*value) { |
| 3348 | *value = dflt; |
| 3349 | } |
| 3350 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3351 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3352 | if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, NULL, 1, 1)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3353 | /* possible forward reference */ |
| 3354 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3355 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3356 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3357 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3358 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3359 | /* we have refined bits/enums */ |
| 3360 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3361 | "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] | 3362 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3363 | } |
| 3364 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3365 | } else { |
| 3366 | /* success - adopt canonical form from the node into the default value */ |
| 3367 | if (dflt != node.value_str) { |
| 3368 | /* this can happen only if we have non-inherited default value, |
| 3369 | * inherited default values are already in canonical form */ |
| 3370 | assert(dflt == *value); |
| 3371 | *value = node.value_str; |
| 3372 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3373 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | finish: |
| 3377 | if (node.value_type == LY_TYPE_BITS) { |
| 3378 | free(node.value.bit); |
| 3379 | } |
| 3380 | free((char *)node.schema->name); |
| 3381 | free(node.schema); |
| 3382 | |
| 3383 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3386 | /** |
| 3387 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3388 | * |
| 3389 | * @param[in] key The key to check. |
| 3390 | * @param[in] flags What flags to check. |
| 3391 | * @param[in] list The list of all the keys. |
| 3392 | * @param[in] index Index of the key in the key list. |
| 3393 | * @param[in] name The name of the keys. |
| 3394 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3395 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3396 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3397 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3398 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3399 | 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] | 3400 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3401 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3402 | char *dup = NULL; |
| 3403 | int j; |
| 3404 | |
| 3405 | /* existence */ |
| 3406 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3407 | if (name[len] != '\0') { |
| 3408 | dup = strdup(name); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3409 | LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3410 | dup[len] = '\0'; |
| 3411 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3412 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3413 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3414 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3415 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /* uniqueness */ |
| 3419 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3420 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3421 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3422 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3427 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3428 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3429 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3433 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3434 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3435 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3439 | 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] | 3440 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3441 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3442 | } |
| 3443 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3444 | /* key is not placed from augment */ |
| 3445 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3446 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3447 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3448 | return -1; |
| 3449 | } |
| 3450 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3451 | /* key is not when/if-feature -conditional */ |
| 3452 | j = 0; |
| 3453 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3454 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3455 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.", |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3456 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3457 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3458 | } |
| 3459 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3460 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3461 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3462 | |
| 3463 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3464 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3465 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3466 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3467 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3468 | * |
| 3469 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3470 | */ |
| 3471 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3472 | 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] | 3473 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3474 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3475 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3476 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3477 | rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3478 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3479 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3480 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3481 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3482 | LOGVAL(LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3483 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3484 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3485 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3486 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3487 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3488 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3489 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3490 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3491 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3492 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3493 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3494 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3495 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3496 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3497 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3498 | } |
| 3499 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3500 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3501 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3502 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3503 | return -1; |
| 3504 | } |
| 3505 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3506 | /* check that all unique's targets are of the same config type */ |
| 3507 | if (*trg_type) { |
| 3508 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 3509 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3510 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3511 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3512 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3513 | return -1; |
| 3514 | } |
| 3515 | } else { |
| 3516 | /* first unique */ |
| 3517 | if (leaf->flags & LYS_CONFIG_W) { |
| 3518 | *trg_type = 1; |
| 3519 | } else { |
| 3520 | *trg_type = 2; |
| 3521 | } |
| 3522 | } |
| 3523 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3524 | /* set leaf's unique flag */ |
| 3525 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3526 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3527 | return EXIT_SUCCESS; |
| 3528 | |
| 3529 | error: |
| 3530 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3531 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3532 | } |
| 3533 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3534 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3535 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3536 | { |
| 3537 | /* there are items after the one deleted */ |
| 3538 | if (i+1 < unres->count) { |
| 3539 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3540 | 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] | 3541 | |
| 3542 | /* deleting the last item */ |
| 3543 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3544 | free(unres->node); |
| 3545 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3549 | --unres->count; |
| 3550 | } |
| 3551 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3552 | /** |
| 3553 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3554 | * |
| 3555 | * @param[in] mod Module to search in. |
| 3556 | * @param[in] name Name of the data node. |
| 3557 | * @param[in] nam_len Length of the name. |
| 3558 | * @param[in] start Data node to start the search from. |
| 3559 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3560 | * they are replaced (!!) with the resolvents. |
| 3561 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3562 | * @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] | 3563 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3564 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3565 | 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] | 3566 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3567 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3568 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3569 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3570 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3571 | if (!parents->count) { |
| 3572 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3573 | parents->node = malloc(sizeof *parents->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3574 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3575 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3576 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3577 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3578 | 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] | 3579 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3580 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3581 | continue; |
| 3582 | } |
| 3583 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3584 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3585 | if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3586 | && node->schema->name[nam_len] == '\0') { |
| 3587 | /* matching target */ |
| 3588 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3589 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3590 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3591 | flag = 1; |
| 3592 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3593 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3594 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3595 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3596 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3597 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3598 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3602 | |
| 3603 | if (!flag) { |
| 3604 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3605 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3606 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3607 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3608 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3611 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3612 | } |
| 3613 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3614 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3615 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3616 | { |
| 3617 | int dep1, dep2; |
| 3618 | const struct lys_node *node; |
| 3619 | |
| 3620 | if (lys_parent(op_node)) { |
| 3621 | /* inner operation (notif/action) */ |
| 3622 | if (abs_path) { |
| 3623 | return 1; |
| 3624 | } else { |
| 3625 | /* compare depth of both nodes */ |
| 3626 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3627 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3628 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3629 | return 1; |
| 3630 | } |
| 3631 | } |
| 3632 | } else { |
| 3633 | /* top-level operation (notif/rpc) */ |
| 3634 | if (op_node != first_node) { |
| 3635 | return 1; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | return 0; |
| 3640 | } |
| 3641 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3642 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3643 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3644 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3645 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3646 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3647 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3648 | * @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] | 3649 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3650 | * @return 0 on forward reference, otherwise the number |
| 3651 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3652 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3653 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3654 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3655 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3656 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3657 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3658 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3659 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3660 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3661 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3662 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3663 | |
| 3664 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3665 | 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] | 3666 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3667 | 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] | 3668 | return -parsed+i; |
| 3669 | } |
| 3670 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3671 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3672 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3673 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3674 | if (sour_pref) { |
| 3675 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3676 | } else { |
| 3677 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3678 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3679 | rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3680 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3681 | 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] | 3682 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3686 | dest_parent_times = 0; |
| 3687 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3688 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3689 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3690 | 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] | 3691 | return -parsed; |
| 3692 | } |
| 3693 | pke_parsed += i; |
| 3694 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3695 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3696 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3697 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3698 | /* we are in an unresolved augment, cannot evaluate */ |
| 3699 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
| 3700 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3704 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3705 | * all schema nodes that cannot be instantiated in data tree */ |
| 3706 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3707 | 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] | 3708 | dst_node = lys_parent(dst_node)); |
| 3709 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3710 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3711 | 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] | 3712 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3713 | } |
| 3714 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3715 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3716 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3717 | if (dest_pref) { |
| 3718 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3719 | } else { |
| 3720 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3721 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3722 | rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3723 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3724 | 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] | 3725 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3726 | } |
| 3727 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3728 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3729 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3730 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3731 | } |
| 3732 | first_iter = 0; |
| 3733 | } |
| 3734 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3735 | if (pke_len == pke_parsed) { |
| 3736 | break; |
| 3737 | } |
| 3738 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3739 | if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3740 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3741 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3742 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3743 | return -parsed; |
| 3744 | } |
| 3745 | pke_parsed += i; |
| 3746 | } |
| 3747 | |
| 3748 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3749 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3750 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path - parsed); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3751 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.", |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3752 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3753 | return -parsed; |
| 3754 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3755 | } while (has_predicate); |
| 3756 | |
| 3757 | return parsed; |
| 3758 | } |
| 3759 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3760 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3761 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3762 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3763 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3764 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3765 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3766 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3767 | * @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] | 3768 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3769 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3770 | resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3771 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3772 | const struct lys_node *node, *op_node = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3773 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3774 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3775 | const char *id, *prefix, *name; |
| 3776 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3777 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3778 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3779 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3780 | parent_times = 0; |
| 3781 | id = path; |
| 3782 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3783 | /* find operation schema we are in */ |
| 3784 | for (op_node = lys_parent(parent); |
| 3785 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3786 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3787 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3788 | cur_module = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3789 | do { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3790 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3791 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3792 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3793 | } |
| 3794 | id += i; |
| 3795 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3796 | /* get the current module */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3797 | tmp_mod = prefix ? lys_get_import_module(cur_module, NULL, 0, prefix, pref_len) : cur_module; |
| 3798 | if (!tmp_mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3799 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3800 | return EXIT_FAILURE; |
| 3801 | } |
| 3802 | last_aug = NULL; |
| 3803 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3804 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3805 | if (parent_times == -1) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3806 | /* use module data */ |
| 3807 | node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3808 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3809 | } else if (parent_times > 0) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3810 | /* we are looking for the right parent */ |
| 3811 | for (i = 0, node = parent; i < parent_times; i++) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3812 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 3813 | && !((struct lys_node_augment *)node->parent)->target) { |
| 3814 | /* we are in an unresolved augment, cannot evaluate */ |
| 3815 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 3816 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path); |
| 3817 | return EXIT_FAILURE; |
| 3818 | } |
| 3819 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3820 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3821 | * all schema nodes that cannot be instantiated in data tree */ |
| 3822 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3823 | 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] | 3824 | node = lys_parent(node)); |
| 3825 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3826 | if (!node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3827 | if (i == parent_times - 1) { |
| 3828 | /* top-level */ |
| 3829 | break; |
| 3830 | } |
| 3831 | |
| 3832 | /* higher than top-level */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3833 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3834 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3835 | } |
| 3836 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3837 | } else { |
| 3838 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3839 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3840 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3841 | } |
| 3842 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3843 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 3844 | * - useless to search for that in augments) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3845 | if (!tmp_mod->implemented && node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3846 | get_next_augment: |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3847 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3850 | rc = lys_getnext_data(tmp_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3851 | | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3852 | if (rc) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3853 | if (last_aug) { |
| 3854 | goto get_next_augment; |
| 3855 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3856 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3857 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3858 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3859 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3860 | if (first_iter) { |
| 3861 | /* set external dependency flag, we can decide based on the first found node */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3862 | if (op_node && parent_times && |
| 3863 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3864 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3865 | } |
| 3866 | first_iter = 0; |
| 3867 | } |
| 3868 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3869 | if (has_predicate) { |
| 3870 | /* we have predicate, so the current result must be list */ |
| 3871 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3872 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3873 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3876 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3877 | if (!i) { |
| 3878 | return EXIT_FAILURE; |
| 3879 | } else if (i < 0) { |
| 3880 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3881 | } |
| 3882 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3883 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3884 | } |
| 3885 | } while (id[0]); |
| 3886 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3887 | /* 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] | 3888 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3889 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3890 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3891 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3894 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3895 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3896 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3897 | return -1; |
| 3898 | } |
| 3899 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3900 | if (ret) { |
| 3901 | *ret = node; |
| 3902 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3903 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3904 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3907 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3908 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3909 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3910 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 3911 | * @param[in] prev_mod Previous module to use in case there is no prefix. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3912 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3913 | * @param[in,out] node Node matching the restriction without |
| 3914 | * the predicate. If it does not satisfy the predicate, |
| 3915 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3916 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3917 | * @return Number of characters successfully parsed, |
| 3918 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3919 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3920 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3921 | resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct lyd_node **node, int cur_idx) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3922 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3923 | /* ... /node[key=value] ... */ |
| 3924 | struct lyd_node_leaf_list *key; |
| 3925 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 3926 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3927 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3928 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3929 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3930 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3931 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3932 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3933 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3934 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 3935 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3936 | } |
| 3937 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3938 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 3939 | if (!(*node)) { |
| 3940 | /* just parse it all */ |
| 3941 | continue; |
| 3942 | } |
| 3943 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3944 | /* target */ |
| 3945 | if (name[0] == '.') { |
| 3946 | /* leaf-list value */ |
| 3947 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
| 3948 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".", |
| 3949 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 3950 | parsed = -1; |
| 3951 | goto cleanup; |
| 3952 | } |
| 3953 | |
| 3954 | /* check the value */ |
| 3955 | if (strncmp(((struct lyd_node_leaf_list *)*node)->value_str, value, val_len) |
| 3956 | || ((struct lyd_node_leaf_list *)*node)->value_str[val_len]) { |
| 3957 | *node = NULL; |
| 3958 | goto cleanup; |
| 3959 | } |
| 3960 | |
| 3961 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3962 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3963 | |
| 3964 | /* keyless list position */ |
| 3965 | if ((*node)->schema->nodetype != LYS_LIST) { |
| 3966 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
| 3967 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 3968 | parsed = -1; |
| 3969 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3970 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3971 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3972 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
| 3973 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".", |
| 3974 | (*node)->schema->name); |
| 3975 | parsed = -1; |
| 3976 | goto cleanup; |
| 3977 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3978 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3979 | /* check the index */ |
| 3980 | if (atoi(name) != cur_idx) { |
| 3981 | *node = NULL; |
| 3982 | goto cleanup; |
| 3983 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3984 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3985 | } else { |
| 3986 | /* list key value */ |
| 3987 | if ((*node)->schema->nodetype != LYS_LIST) { |
| 3988 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
| 3989 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 3990 | parsed = -1; |
| 3991 | goto cleanup; |
| 3992 | } |
| 3993 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3994 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 3995 | /* prepare key array */ |
| 3996 | if (!list_keys) { |
| 3997 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
| 3998 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM, -1); |
| 3999 | for (i = 0; i < slist->keys_size; ++i) { |
| 4000 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4001 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4002 | } |
| 4003 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4004 | /* find the schema key leaf */ |
| 4005 | for (i = 0; i < slist->keys_size; ++i) { |
| 4006 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4007 | break; |
| 4008 | } |
| 4009 | } |
| 4010 | if (i == slist->keys_size) { |
| 4011 | /* this list has no such key */ |
| 4012 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\"," |
| 4013 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4014 | parsed = -1; |
| 4015 | goto cleanup; |
| 4016 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4017 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4018 | /* check module */ |
| 4019 | if (model) { |
| 4020 | if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) { |
| 4021 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".", |
| 4022 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4023 | parsed = -1; |
| 4024 | goto cleanup; |
| 4025 | } |
| 4026 | } else { |
| 4027 | if (list_keys[i]->module != prev_mod) { |
| 4028 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".", |
| 4029 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4030 | parsed = -1; |
| 4031 | goto cleanup; |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | /* find the actual data key */ |
| 4036 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4037 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4038 | break; |
| 4039 | } |
| 4040 | } |
| 4041 | if (!key) { |
| 4042 | /* list instance is missing a key? definitely should not happen */ |
| 4043 | LOGINT; |
| 4044 | parsed = -1; |
| 4045 | goto cleanup; |
| 4046 | } |
| 4047 | |
| 4048 | /* check the value */ |
| 4049 | if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) { |
| 4050 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4051 | /* we still want to parse the whole predicate */ |
| 4052 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | /* everything is fine, mark this key as resolved */ |
| 4056 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4057 | } |
| 4058 | } while (has_predicate); |
| 4059 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4060 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4061 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4062 | for (i = 0; i < slist->keys_size; ++i) { |
| 4063 | if (list_keys[i]) { |
| 4064 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name); |
| 4065 | parsed = -1; |
| 4066 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4067 | } |
| 4068 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4069 | } |
| 4070 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4071 | cleanup: |
| 4072 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4073 | return parsed; |
| 4074 | } |
| 4075 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4076 | int |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 4077 | lys_check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4078 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4079 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4080 | struct lyxp_set set; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4081 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4082 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4083 | if (check_place) { |
| 4084 | parent = node; |
| 4085 | while (parent) { |
| 4086 | if (parent->nodetype == LYS_GROUPING) { |
| 4087 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4088 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4089 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4090 | if (parent->nodetype == LYS_AUGMENT) { |
| 4091 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4092 | /* unresolved augment, skip for now (will be checked later) */ |
| 4093 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4094 | } else { |
| 4095 | parent = ((struct lys_node_augment *)parent)->target; |
| 4096 | continue; |
| 4097 | } |
| 4098 | } |
| 4099 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4100 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4101 | } |
| 4102 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 4103 | ret = lyxp_node_atomize(node, &set, 1); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4104 | if (ret == -1) { |
| 4105 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4106 | } |
| 4107 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4108 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4109 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4110 | } |
| 4111 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4112 | static int |
| 4113 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4114 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4115 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4116 | |
| 4117 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4118 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4119 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4120 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.", |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4121 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4122 | return -1; |
| 4123 | } |
| 4124 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4125 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4126 | } else if (type->base == LY_TYPE_UNION) { |
| 4127 | for (i = 0; i < type->info.uni.count; i++) { |
| 4128 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4129 | return -1; |
| 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4136 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4137 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4138 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4139 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4140 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4141 | * @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] | 4142 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4143 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4144 | * |
| 4145 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4146 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4147 | int |
| 4148 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4149 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4150 | struct lys_node_leaf *leaf; |
| 4151 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4152 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4153 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4154 | if (clear) { |
| 4155 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4156 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4157 | } else { |
| 4158 | if (node->flags & LYS_CONFIG_SET) { |
| 4159 | /* skip nodes with an explicit config value */ |
| 4160 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4161 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4162 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children."); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4163 | return -1; |
| 4164 | } |
| 4165 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4166 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4167 | |
| 4168 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4169 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4170 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4171 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4172 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4173 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4174 | return -1; |
| 4175 | } |
| 4176 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4177 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4178 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4179 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4180 | return -1; |
| 4181 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4182 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4183 | leaf = (struct lys_node_leaf *)node; |
| 4184 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4185 | return -1; |
| 4186 | } |
| 4187 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4188 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4189 | |
| 4190 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4193 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4194 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4195 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4196 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4197 | * @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] | 4198 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4199 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4200 | * @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] | 4201 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4202 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4203 | 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] | 4204 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4205 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4206 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4207 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4208 | struct ly_set *set; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4209 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4210 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4211 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4212 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4213 | /* set it as not applied for now */ |
| 4214 | aug->flags |= LYS_NOTAPPLIED; |
| 4215 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4216 | /* it can already be resolved in case we returned EXIT_FAILURE from if block below */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4217 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4218 | /* resolve target node */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4219 | rc = resolve_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4220 | if (rc == -1) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4221 | LOGVAL(LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4222 | return -1; |
| 4223 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4224 | if (!set) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4225 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4226 | return EXIT_FAILURE; |
| 4227 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4228 | aug->target = set->set.s[0]; |
| 4229 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4230 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4231 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4232 | /* check for mandatory nodes - if the target node is in another module |
| 4233 | * the added nodes cannot be mandatory |
| 4234 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4235 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4236 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4237 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4238 | } |
| 4239 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4240 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4241 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4242 | LY_TREE_FOR(aug->child, sub) { |
| 4243 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4244 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4245 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4246 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4247 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4248 | return -1; |
| 4249 | } |
| 4250 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4251 | } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4252 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4253 | 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] | 4254 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4255 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4256 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4257 | return -1; |
| 4258 | } |
| 4259 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4260 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4261 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4262 | 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] | 4263 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4264 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4265 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4266 | return -1; |
| 4267 | } |
| 4268 | } |
| 4269 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4270 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4271 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4272 | return -1; |
| 4273 | } |
| 4274 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4275 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4276 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4277 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4278 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4279 | } |
| 4280 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4281 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4282 | if (!aug->child) { |
| 4283 | /* empty augment, nothing to connect, but it is techincally applied */ |
| 4284 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 4285 | aug->flags &= ~LYS_NOTAPPLIED; |
| 4286 | } else if (mod->implemented && apply_aug(aug, unres)) { |
| 4287 | /* we tried to connect it, we failed */ |
| 4288 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4291 | return EXIT_SUCCESS; |
| 4292 | } |
| 4293 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4294 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4295 | resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4296 | { |
| 4297 | enum LY_VLOG_ELEM vlog_type; |
| 4298 | void *vlog_node; |
| 4299 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4300 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4301 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4302 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4303 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4304 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4305 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4306 | |
| 4307 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4308 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4309 | vlog_node = info->parent; |
| 4310 | vlog_type = LY_VLOG_LYS; |
| 4311 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4312 | case LYEXT_PAR_MODULE: |
| 4313 | case LYEXT_PAR_IMPORT: |
| 4314 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4315 | vlog_node = NULL; |
| 4316 | vlog_type = LY_VLOG_LYS; |
| 4317 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4318 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4319 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4320 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4325 | /* YIN */ |
| 4326 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4327 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4328 | mod = lys_get_import_module_ns(info->mod, info->data.yin->ns->value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4329 | if (!mod) { |
| 4330 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4331 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | /* find the extension definition */ |
| 4335 | e = NULL; |
| 4336 | for (i = 0; i < mod->extensions_size; i++) { |
| 4337 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4338 | e = &mod->extensions[i]; |
| 4339 | break; |
| 4340 | } |
| 4341 | } |
| 4342 | /* try submodules */ |
| 4343 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4344 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4345 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4346 | e = &mod->inc[j].submodule->extensions[i]; |
| 4347 | break; |
| 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | if (!e) { |
| 4352 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4353 | return EXIT_FAILURE; |
| 4354 | } |
| 4355 | |
| 4356 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4357 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4358 | if (e->plugin && e->plugin->check_position) { |
| 4359 | /* common part - we have plugin with position checking function, use it first */ |
| 4360 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4361 | /* extension is not allowed here */ |
| 4362 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4363 | return -1; |
| 4364 | } |
| 4365 | } |
| 4366 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4367 | /* extension type-specific part - allocation */ |
| 4368 | if (e->plugin) { |
| 4369 | etype = e->plugin->type; |
| 4370 | } else { |
| 4371 | /* default type */ |
| 4372 | etype = LYEXT_FLAG; |
| 4373 | } |
| 4374 | switch (etype) { |
| 4375 | case LYEXT_FLAG: |
| 4376 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4377 | break; |
| 4378 | case LYEXT_COMPLEX: |
| 4379 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4380 | break; |
| 4381 | case LYEXT_ERR: |
| 4382 | /* we never should be here */ |
| 4383 | LOGINT; |
| 4384 | return -1; |
| 4385 | } |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4386 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4387 | |
| 4388 | /* common part for all extension types */ |
| 4389 | (*ext)->def = e; |
| 4390 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4391 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4392 | (*ext)->insubstmt = info->substmt; |
| 4393 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4394 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4395 | |
| 4396 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4397 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4398 | if (!(*ext)->arg_value) { |
| 4399 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4400 | return -1; |
| 4401 | } |
| 4402 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4403 | } |
| 4404 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4405 | (*ext)->nodetype = LYS_EXT; |
| 4406 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4407 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4408 | /* extension type-specific part - parsing content */ |
| 4409 | switch (etype) { |
| 4410 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4411 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4412 | if (!yin->ns) { |
| 4413 | /* garbage */ |
| 4414 | lyxml_free(mod->ctx, yin); |
| 4415 | continue; |
| 4416 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4417 | /* standard YANG statements are not expected here */ |
| 4418 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4419 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4420 | } else if (yin->ns == info->data.yin->ns && |
| 4421 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4422 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4423 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4424 | LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4425 | return -1; |
| 4426 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4427 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4428 | yin->content = NULL; |
| 4429 | lyxml_free(mod->ctx, yin); |
| 4430 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4431 | /* extension instance */ |
| 4432 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4433 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4434 | return -1; |
| 4435 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4436 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4437 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4438 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4439 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4440 | break; |
| 4441 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4442 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4443 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4444 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4445 | return -1; |
| 4446 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4447 | break; |
| 4448 | default: |
| 4449 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4450 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4451 | |
| 4452 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4453 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4454 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4455 | /* YANG */ |
| 4456 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4457 | ext_prefix = (char *)(*ext)->def; |
| 4458 | tmp = strchr(ext_prefix, ':'); |
| 4459 | if (!tmp) { |
| 4460 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4461 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4462 | } |
| 4463 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4464 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4465 | /* get the module where the extension is supposed to be defined */ |
| 4466 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4467 | if (!mod) { |
| 4468 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4469 | return EXIT_FAILURE; |
| 4470 | } |
| 4471 | |
| 4472 | /* find the extension definition */ |
| 4473 | e = NULL; |
| 4474 | for (i = 0; i < mod->extensions_size; i++) { |
| 4475 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4476 | e = &mod->extensions[i]; |
| 4477 | break; |
| 4478 | } |
| 4479 | } |
| 4480 | /* try submodules */ |
| 4481 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4482 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4483 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4484 | e = &mod->inc[j].submodule->extensions[i]; |
| 4485 | break; |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | if (!e) { |
| 4490 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4491 | return EXIT_FAILURE; |
| 4492 | } |
| 4493 | |
| 4494 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4495 | |
| 4496 | if (e->plugin && e->plugin->check_position) { |
| 4497 | /* common part - we have plugin with position checking function, use it first */ |
| 4498 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4499 | /* extension is not allowed here */ |
| 4500 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4501 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4505 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4506 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4507 | (*ext)->def = e; |
| 4508 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4509 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4510 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4511 | if (e->argument && !(*ext)->arg_value) { |
| 4512 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4513 | goto error; |
| 4514 | } |
| 4515 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4516 | (*ext)->module = info->mod; |
| 4517 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4518 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4519 | /* extension type-specific part */ |
| 4520 | if (e->plugin) { |
| 4521 | etype = e->plugin->type; |
| 4522 | } else { |
| 4523 | /* default type */ |
| 4524 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4525 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4526 | switch (etype) { |
| 4527 | case LYEXT_FLAG: |
| 4528 | /* nothing change */ |
| 4529 | break; |
| 4530 | case LYEXT_COMPLEX: |
| 4531 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4532 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4533 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4534 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4535 | ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4536 | if (info->data.yang) { |
| 4537 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4538 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4539 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4540 | goto error; |
| 4541 | } |
| 4542 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4543 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4544 | goto error; |
| 4545 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4546 | } |
| 4547 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4548 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4549 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4550 | break; |
| 4551 | case LYEXT_ERR: |
| 4552 | /* we never should be here */ |
| 4553 | LOGINT; |
| 4554 | goto error; |
| 4555 | } |
| 4556 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4557 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4558 | goto error; |
| 4559 | } |
| 4560 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4564 | error: |
| 4565 | free(ext_prefix); |
| 4566 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4567 | } |
| 4568 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4569 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4570 | * @brief Resolve (find) choice default case. Does not log. |
| 4571 | * |
| 4572 | * @param[in] choic Choice to use. |
| 4573 | * @param[in] dflt Name of the default case. |
| 4574 | * |
| 4575 | * @return Pointer to the default node or NULL. |
| 4576 | */ |
| 4577 | static struct lys_node * |
| 4578 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4579 | { |
| 4580 | struct lys_node *child, *ret; |
| 4581 | |
| 4582 | LY_TREE_FOR(choic->child, child) { |
| 4583 | if (child->nodetype == LYS_USES) { |
| 4584 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4585 | if (ret) { |
| 4586 | return ret; |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | 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] | 4591 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4592 | return child; |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | return NULL; |
| 4597 | } |
| 4598 | |
| 4599 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4600 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4601 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4602 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4603 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4604 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4605 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4606 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4607 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4608 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4609 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4610 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4611 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4612 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4613 | struct lys_node_leaflist *llist; |
| 4614 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4615 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4616 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4617 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4618 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4619 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4620 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4621 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4622 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4623 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 4624 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 4625 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4626 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4627 | if (!uses->grp->child) { |
| 4628 | /* grouping without children, warning was already displayed */ |
| 4629 | return EXIT_SUCCESS; |
| 4630 | } |
| 4631 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4632 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4633 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4634 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4635 | /* do not instantiate groupings from groupings */ |
| 4636 | continue; |
| 4637 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4638 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4639 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4640 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4641 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4642 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4643 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4644 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4645 | LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) { |
Pavol Vican | 2510ddc | 2016-07-18 16:23:44 +0200 | [diff] [blame] | 4646 | 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] | 4647 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4648 | } |
| 4649 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4650 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4651 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4652 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4653 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4654 | if (uses->refine_size) { |
| 4655 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4656 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4657 | } |
| 4658 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4659 | /* apply refines */ |
| 4660 | for (i = 0; i < uses->refine_size; i++) { |
| 4661 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4662 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4663 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4664 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4665 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4666 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4667 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4670 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4671 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4672 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4673 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4674 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4675 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4676 | |
| 4677 | /* description on any nodetype */ |
| 4678 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4679 | lydict_remove(ctx, node->dsc); |
| 4680 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | /* reference on any nodetype */ |
| 4684 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4685 | lydict_remove(ctx, node->ref); |
| 4686 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4687 | } |
| 4688 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4689 | /* config on any nodetype, |
| 4690 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4691 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4692 | node->flags &= ~LYS_CONFIG_MASK; |
| 4693 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4697 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4698 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4699 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4700 | leaf = (struct lys_node_leaf *)node; |
| 4701 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4702 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4703 | lydict_remove(ctx, leaf->dflt); |
| 4704 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4705 | |
| 4706 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4707 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4708 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4709 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4710 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4711 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4712 | /* leaf-list */ |
| 4713 | llist = (struct lys_node_leaflist *)node; |
| 4714 | |
| 4715 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4716 | for (j = 0; j < llist->dflt_size; j++) { |
| 4717 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4718 | } |
| 4719 | free(llist->dflt); |
| 4720 | |
| 4721 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4722 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
| 4723 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4724 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4725 | for (j = 0; j < llist->dflt_size; j++) { |
| 4726 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4730 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4731 | if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT, |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4732 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4733 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4734 | } |
| 4735 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4740 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4741 | /* remove current value */ |
| 4742 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4743 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4744 | /* set new value */ |
| 4745 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4746 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4747 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4748 | /* check if node has default value */ |
| 4749 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4750 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4751 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4752 | goto fail; |
| 4753 | } |
| 4754 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4755 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4756 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4757 | goto fail; |
| 4758 | } |
| 4759 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4763 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4764 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4765 | ((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] | 4766 | } |
| 4767 | |
| 4768 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4769 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4770 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4771 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4772 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4773 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4774 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4775 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4776 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4777 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4778 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4779 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4780 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4781 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4786 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4787 | switch (node->nodetype) { |
| 4788 | case LYS_LEAF: |
| 4789 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4790 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4791 | break; |
| 4792 | case LYS_LEAFLIST: |
| 4793 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4794 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4795 | break; |
| 4796 | case LYS_LIST: |
| 4797 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4798 | old_must = &((struct lys_node_list *)node)->must; |
| 4799 | break; |
| 4800 | case LYS_CONTAINER: |
| 4801 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4802 | old_must = &((struct lys_node_container *)node)->must; |
| 4803 | break; |
| 4804 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4805 | case LYS_ANYDATA: |
| 4806 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4807 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4808 | break; |
| 4809 | default: |
| 4810 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4811 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | size = *old_size + rfn->must_size; |
| 4815 | must = realloc(*old_must, size * sizeof *rfn->must); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4816 | LY_CHECK_ERR_GOTO(!must, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4817 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4818 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4819 | lys_ext_dup(rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4820 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4821 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4822 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4823 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4824 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4825 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Radek Krejci | cfcd8a5 | 2017-09-04 13:19:57 +0200 | [diff] [blame] | 4826 | must[j].flags = rfn->must[k].flags; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4827 | } |
| 4828 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4829 | *old_must = must; |
| 4830 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4831 | |
| 4832 | /* check XPath dependencies again */ |
| 4833 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4834 | goto fail; |
| 4835 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4836 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4837 | |
| 4838 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4839 | if (rfn->iffeature_size) { |
| 4840 | old_size = &node->iffeature_size; |
| 4841 | old_iff = &node->iffeature; |
| 4842 | |
| 4843 | size = *old_size + rfn->iffeature_size; |
| 4844 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4845 | LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail); |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 4846 | *old_iff = iff; |
| 4847 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4848 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4849 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4850 | if (usize1) { |
| 4851 | /* there is something to duplicate */ |
| 4852 | /* duplicate compiled expression */ |
| 4853 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4854 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4855 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4856 | 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] | 4857 | |
| 4858 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4859 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4860 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4861 | 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] | 4862 | |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 4863 | /* duplicate extensions */ |
| 4864 | iff[j].ext_size = rfn->iffeature[k].ext_size; |
| 4865 | lys_ext_dup(rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size, |
| 4866 | &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres); |
| 4867 | } |
| 4868 | (*old_size)++; |
| 4869 | } |
| 4870 | assert(*old_size == size); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4871 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | /* apply augments */ |
| 4875 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4876 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4877 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4878 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4879 | } |
| 4880 | } |
| 4881 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4882 | /* check refines */ |
| 4883 | for (i = 0; i < uses->refine_size; i++) { |
| 4884 | node = refine_nodes[i]; |
| 4885 | rfn = &uses->refine[i]; |
| 4886 | |
| 4887 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4888 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4889 | 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] | 4890 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4891 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4892 | (rfn->flags & LYS_CONFIG_W)) { |
| 4893 | /* setting config true under config false is prohibited */ |
| 4894 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4895 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4896 | "changing config from 'false' to 'true' is prohibited while " |
| 4897 | "the target's parent is still config 'false'."); |
| 4898 | goto fail; |
| 4899 | } |
| 4900 | |
| 4901 | /* inherit config change to the target children */ |
| 4902 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4903 | if (rfn->flags & LYS_CONFIG_W) { |
| 4904 | if (iter->flags & LYS_CONFIG_SET) { |
| 4905 | /* config is set explicitely, go to next sibling */ |
| 4906 | next = NULL; |
| 4907 | goto nextsibling; |
| 4908 | } |
| 4909 | } else { /* LYS_CONFIG_R */ |
| 4910 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4911 | /* error - we would have config data under status data */ |
| 4912 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4913 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4914 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4915 | "has still a children with explicit config 'true'."); |
| 4916 | goto fail; |
| 4917 | } |
| 4918 | } |
| 4919 | /* change config */ |
| 4920 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4921 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4922 | |
| 4923 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4924 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4925 | next = NULL; |
| 4926 | } else { |
| 4927 | next = iter->child; |
| 4928 | } |
| 4929 | nextsibling: |
| 4930 | if (!next) { |
| 4931 | /* try siblings */ |
| 4932 | next = iter->next; |
| 4933 | } |
| 4934 | while (!next) { |
| 4935 | /* parent is already processed, go to its sibling */ |
| 4936 | iter = lys_parent(iter); |
| 4937 | |
| 4938 | /* no siblings, go back through parents */ |
| 4939 | if (iter == node) { |
| 4940 | /* we are done, no next element to process */ |
| 4941 | break; |
| 4942 | } |
| 4943 | next = iter->next; |
| 4944 | } |
| 4945 | } |
| 4946 | } |
| 4947 | |
| 4948 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4949 | if (rfn->dflt_size) { |
| 4950 | if (node->nodetype == LYS_CHOICE) { |
| 4951 | /* choice */ |
| 4952 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4953 | rfn->dflt[0]); |
| 4954 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4955 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4956 | goto fail; |
| 4957 | } |
| 4958 | if (lyp_check_mandatory_choice(node)) { |
| 4959 | goto fail; |
| 4960 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4961 | } |
| 4962 | } |
| 4963 | |
| 4964 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4965 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4966 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4967 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4968 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4969 | goto fail; |
| 4970 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4971 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4972 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4973 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4974 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4975 | goto fail; |
| 4976 | } |
| 4977 | } |
| 4978 | |
| 4979 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4980 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4981 | if (node->nodetype == LYS_LEAFLIST) { |
| 4982 | llist = (struct lys_node_leaflist *)node; |
| 4983 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4984 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4985 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4986 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4987 | goto fail; |
| 4988 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4989 | } else if (node->nodetype == LYS_LEAF) { |
| 4990 | leaf = (struct lys_node_leaf *)node; |
| 4991 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4992 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4993 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4994 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4995 | goto fail; |
| 4996 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4997 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4998 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4999 | /* 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] | 5000 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5001 | for (parent = node->parent; |
| 5002 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5003 | parent = parent->parent) { |
| 5004 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5005 | /* stop also on presence containers */ |
| 5006 | break; |
| 5007 | } |
| 5008 | } |
| 5009 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5010 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5011 | if (lyp_check_mandatory_choice(parent)) { |
| 5012 | goto fail; |
| 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | free(refine_nodes); |
| 5018 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5019 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5020 | |
| 5021 | fail: |
| 5022 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5023 | lys_node_free(iter, NULL, 0); |
| 5024 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5025 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5026 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5027 | } |
| 5028 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5029 | void |
| 5030 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5031 | { |
| 5032 | int i; |
| 5033 | |
| 5034 | assert(der && base); |
| 5035 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5036 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5037 | /* create a set for backlinks if it does not exist */ |
| 5038 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5039 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5040 | /* store backlink */ |
| 5041 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5042 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5043 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5044 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5045 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5046 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5047 | } |
| 5048 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5049 | /** |
| 5050 | * @brief Resolve base identity recursively. Does not log. |
| 5051 | * |
| 5052 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5053 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5054 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5055 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5056 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5057 | * @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] | 5058 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5059 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5060 | 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] | 5061 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5062 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5063 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5064 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5065 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5066 | assert(ret); |
| 5067 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5068 | /* search module */ |
| 5069 | for (i = 0; i < module->ident_size; i++) { |
| 5070 | if (!strcmp(basename, module->ident[i].name)) { |
| 5071 | |
| 5072 | if (!ident) { |
| 5073 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5074 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5075 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5076 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5077 | } |
| 5078 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5079 | base = &module->ident[i]; |
| 5080 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | |
| 5084 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5085 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5086 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5087 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5088 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5089 | if (!ident) { |
| 5090 | *ret = &module->inc[j].submodule->ident[i]; |
| 5091 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5092 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5093 | |
| 5094 | base = &module->inc[j].submodule->ident[i]; |
| 5095 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5100 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5101 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5102 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5103 | /* is it already completely resolved? */ |
| 5104 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5105 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5106 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5107 | |
| 5108 | /* simple check for circular reference, |
| 5109 | * the complete check is done as a side effect of using only completely |
| 5110 | * resolved identities (previous check of unres content) */ |
| 5111 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5112 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5113 | 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] | 5114 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5115 | } |
| 5116 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5117 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5118 | } |
| 5119 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5120 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5121 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5122 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5123 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5124 | } |
| 5125 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5126 | /* base not found (maybe a forward reference) */ |
| 5127 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5128 | } |
| 5129 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5130 | /** |
| 5131 | * @brief Resolve base identity. Logs directly. |
| 5132 | * |
| 5133 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5134 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5135 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5136 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5137 | * @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] | 5138 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5139 | * @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] | 5140 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5141 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5142 | 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] | 5143 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5144 | { |
| 5145 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5146 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5147 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5148 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5149 | struct lys_module *mod; |
| 5150 | |
| 5151 | assert((ident && !type) || (!ident && type)); |
| 5152 | |
| 5153 | if (!type) { |
| 5154 | /* have ident to resolve */ |
| 5155 | ret = ⌖ |
| 5156 | flags = ident->flags; |
| 5157 | mod = ident->module; |
| 5158 | } else { |
| 5159 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5160 | ++type->info.ident.count; |
| 5161 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5162 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5163 | |
| 5164 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5165 | flags = type->parent->flags; |
| 5166 | mod = type->parent->module; |
| 5167 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5168 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5169 | |
| 5170 | /* search for the base identity */ |
| 5171 | name = strchr(basename, ':'); |
| 5172 | if (name) { |
| 5173 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5174 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5175 | name++; |
| 5176 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5177 | 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] | 5178 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5179 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5180 | } |
| 5181 | } else { |
| 5182 | name = basename; |
| 5183 | } |
| 5184 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5185 | /* get module where to search */ |
| 5186 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5187 | if (!module) { |
| 5188 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5189 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5190 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5191 | } |
| 5192 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5193 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5194 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5195 | if (!rc) { |
| 5196 | assert(*ret); |
| 5197 | |
| 5198 | /* check status */ |
| 5199 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5200 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5201 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5202 | } else if (ident) { |
| 5203 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5204 | if (lys_main_module(mod)->implemented) { |
| 5205 | /* in case of the implemented identity, maintain backlinks to it |
| 5206 | * from the base identities to make it available when resolving |
| 5207 | * data with the identity values (not implemented identity is not |
| 5208 | * allowed as an identityref value). */ |
| 5209 | resolve_identity_backlink_update(ident, *ret); |
| 5210 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5211 | } |
| 5212 | } else if (rc == EXIT_FAILURE) { |
| 5213 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5214 | if (type) { |
| 5215 | --type->info.ident.count; |
| 5216 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5217 | } |
| 5218 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5219 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5220 | } |
| 5221 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5222 | /* |
| 5223 | * 1 - true (der is derived from base) |
| 5224 | * 0 - false (der is not derived from base) |
| 5225 | */ |
| 5226 | static int |
| 5227 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5228 | { |
| 5229 | int i; |
| 5230 | |
| 5231 | if (der == base) { |
| 5232 | return 1; |
| 5233 | } else { |
| 5234 | for(i = 0; i < der->base_size; i++) { |
| 5235 | if (search_base_identity(der->base[i], base) == 1) { |
| 5236 | return 1; |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | return 0; |
| 5242 | } |
| 5243 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5244 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5245 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5246 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5247 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5248 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5249 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5250 | * @param[in] dflt flag if we are resolving default value in the schema |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5251 | * |
| 5252 | * @return Pointer to the identity resolvent, NULL on error. |
| 5253 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5254 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5255 | resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5256 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5257 | const char *mod_name, *name; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5258 | int mod_name_len, nam_len, rc; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame^] | 5259 | int need_implemented = 0; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5260 | unsigned int u, i, j; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5261 | struct lys_ident *der, *cur; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5262 | struct lys_module *imod = NULL, *m; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5263 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5264 | assert(type && ident_name && node && mod); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5265 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5266 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5267 | return NULL; |
| 5268 | } |
| 5269 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5270 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0); |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5271 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5272 | 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] | 5273 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5274 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5275 | 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] | 5276 | return NULL; |
| 5277 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5278 | |
| 5279 | m = lys_main_module(mod); /* shortcut */ |
| 5280 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5281 | /* identity is defined in the same module as node */ |
| 5282 | imod = m; |
| 5283 | } else if (dflt) { |
| 5284 | /* solving identityref in default definition in schema - |
| 5285 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5286 | for (i = 0; i < mod->imp_size; i++) { |
| 5287 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5288 | imod = mod->imp[i].module; |
| 5289 | break; |
| 5290 | } |
| 5291 | } |
| 5292 | } else { |
| 5293 | /* solving identityref in data - get the (implemented) module from the context */ |
| 5294 | u = 0; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame^] | 5295 | while ((imod = (struct lys_module *)ly_ctx_get_module_iter(mod->ctx, &u))) { |
| 5296 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5297 | break; |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | if (!imod) { |
| 5302 | goto fail; |
| 5303 | } |
| 5304 | |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame^] | 5305 | if (m != imod || lys_main_module(type->parent->module) != mod) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5306 | /* we are solving default statement in schema AND the type is not referencing the same schema, |
| 5307 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5308 | * contains the identity */ |
| 5309 | if (!imod->implemented) { |
| 5310 | cur = NULL; |
| 5311 | /* get the identity in the module */ |
| 5312 | for (i = 0; i < imod->ident_size; i++) { |
| 5313 | if (!strcmp(name, imod->ident[i].name)) { |
| 5314 | cur = &imod->ident[i]; |
| 5315 | break; |
| 5316 | } |
| 5317 | } |
| 5318 | if (!cur) { |
| 5319 | /* go through includes */ |
| 5320 | for (j = 0; j < imod->inc_size; j++) { |
| 5321 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5322 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5323 | cur = &imod->inc[j].submodule->ident[i]; |
| 5324 | break; |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | if (!cur) { |
| 5329 | goto fail; |
| 5330 | } |
| 5331 | } |
| 5332 | |
| 5333 | /* check that identity is derived from one of the type's base */ |
| 5334 | while (type->der) { |
| 5335 | for (i = 0; i < type->info.ident.count; i++) { |
| 5336 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5337 | /* cur's base matches the type's base */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame^] | 5338 | need_implemented = 1; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5339 | goto match; |
| 5340 | } |
| 5341 | } |
| 5342 | type = &type->der->type; |
| 5343 | } |
| 5344 | /* matching base not found */ |
| 5345 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5346 | goto fail; |
| 5347 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5348 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5349 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5350 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5351 | while (type->der) { |
| 5352 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5353 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5354 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5355 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5356 | /* there are some derived identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5357 | for (u = 0; u < cur->der->number; u++) { |
| 5358 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5359 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5360 | /* we have match */ |
| 5361 | cur = der; |
| 5362 | goto match; |
| 5363 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5364 | } |
| 5365 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5366 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5367 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5368 | } |
| 5369 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5370 | fail: |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5371 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5372 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5373 | |
| 5374 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5375 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5376 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5377 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5378 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5379 | return NULL; |
| 5380 | } |
| 5381 | } |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame^] | 5382 | if (need_implemented) { |
| 5383 | if (dflt) { |
| 5384 | /* try to make the module implemented */ |
| 5385 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5386 | imod->name, cur->name, mod->name); |
| 5387 | if (lys_set_implemented(imod)) { |
| 5388 | LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
| 5389 | imod->name, cur->name); |
| 5390 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5391 | goto fail; |
| 5392 | } |
| 5393 | } else { |
| 5394 | /* just say that it was found, but in a non-implemented module */ |
| 5395 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".", |
| 5396 | lys_main_module(cur->module)->name); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5397 | goto fail; |
| 5398 | } |
| 5399 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5400 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5401 | } |
| 5402 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5403 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5404 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5405 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5406 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5407 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5408 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5409 | * @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] | 5410 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5411 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5412 | 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] | 5413 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5414 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5415 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5416 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5417 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5418 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5419 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5420 | * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 5421 | 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] | 5422 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5423 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5424 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5425 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5426 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5427 | return -1; |
| 5428 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5429 | 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] | 5430 | return -1; |
| 5431 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5432 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5433 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5434 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5435 | return -1; |
| 5436 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5437 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5438 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5439 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5440 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5441 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5442 | } |
| 5443 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5444 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5445 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5446 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5447 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5448 | return -1; |
| 5449 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5450 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5451 | } else { |
| 5452 | /* instantiate grouping only when it is completely resolved */ |
| 5453 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5454 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5455 | return EXIT_FAILURE; |
| 5456 | } |
| 5457 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5458 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5459 | if (!rc) { |
| 5460 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5461 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5462 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 5463 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5464 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5465 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5466 | |
| 5467 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5468 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5469 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5470 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5471 | return -1; |
| 5472 | } |
| 5473 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5474 | return EXIT_SUCCESS; |
| 5475 | } |
| 5476 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5477 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5480 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5481 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5482 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5483 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5484 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5485 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5486 | * @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] | 5487 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5488 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5489 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5490 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5491 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5492 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5493 | char *s = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5494 | |
| 5495 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 5496 | assert(keys_str); |
| 5497 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5498 | if (!list->child) { |
| 5499 | /* no child, possible forward reference */ |
| 5500 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5501 | return EXIT_FAILURE; |
| 5502 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5503 | /* get the key name */ |
| 5504 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5505 | len = value - keys_str; |
| 5506 | while (isspace(value[0])) { |
| 5507 | value++; |
| 5508 | } |
| 5509 | } else { |
| 5510 | len = strlen(keys_str); |
| 5511 | } |
| 5512 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5513 | if (list->keys[i]) { |
| 5514 | /* skip already resolved keys */ |
| 5515 | goto next; |
| 5516 | } |
| 5517 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5518 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 5519 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5520 | if (rc) { |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5521 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5522 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5523 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5524 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5525 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5526 | /* check_key logs */ |
| 5527 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5528 | } |
| 5529 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5530 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5531 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5532 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5533 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5534 | return -1; |
| 5535 | } |
| 5536 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5537 | /* default value - is ignored, keep it but print a warning */ |
| 5538 | if (list->keys[i]->dflt) { |
| 5539 | /* log is not hidden only in case this resolving fails and in such a case |
| 5540 | * we cannot get here |
| 5541 | */ |
Michal Vasko | 4814fb0 | 2017-08-17 14:49:38 +0200 | [diff] [blame] | 5542 | assert(ly_err_main.vlog_hide); |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5543 | ly_vlog_hide(0); |
| 5544 | LOGWRN("Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt, |
| 5545 | list->keys[i]->name, s = lys_path((struct lys_node*)list)); |
| 5546 | ly_vlog_hide(1); |
| 5547 | free(s); |
| 5548 | } |
| 5549 | |
| 5550 | next: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5551 | /* prepare for next iteration */ |
| 5552 | while (value && isspace(value[0])) { |
| 5553 | value++; |
| 5554 | } |
| 5555 | keys_str = value; |
| 5556 | } |
| 5557 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5558 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5559 | } |
| 5560 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5561 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5562 | * @brief Resolve (check) all must conditions of \p node. |
| 5563 | * Logs directly. |
| 5564 | * |
| 5565 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5566 | * @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] | 5567 | * |
| 5568 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5569 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5570 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5571 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5572 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5573 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5574 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5575 | struct lys_restr *must; |
| 5576 | struct lyxp_set set; |
| 5577 | |
| 5578 | assert(node); |
| 5579 | memset(&set, 0, sizeof set); |
| 5580 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5581 | if (inout_parent) { |
| 5582 | for (schema = lys_parent(node->schema); |
| 5583 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5584 | schema = lys_parent(schema)); |
| 5585 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5586 | LOGINT; |
| 5587 | return -1; |
| 5588 | } |
| 5589 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5590 | must = ((struct lys_node_inout *)schema)->must; |
| 5591 | |
| 5592 | /* context node is the RPC/action */ |
| 5593 | node = node->parent; |
| 5594 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5595 | LOGINT; |
| 5596 | return -1; |
| 5597 | } |
| 5598 | } else { |
| 5599 | switch (node->schema->nodetype) { |
| 5600 | case LYS_CONTAINER: |
| 5601 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5602 | must = ((struct lys_node_container *)node->schema)->must; |
| 5603 | break; |
| 5604 | case LYS_LEAF: |
| 5605 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5606 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5607 | break; |
| 5608 | case LYS_LEAFLIST: |
| 5609 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5610 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5611 | break; |
| 5612 | case LYS_LIST: |
| 5613 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5614 | must = ((struct lys_node_list *)node->schema)->must; |
| 5615 | break; |
| 5616 | case LYS_ANYXML: |
| 5617 | case LYS_ANYDATA: |
| 5618 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5619 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5620 | break; |
| 5621 | case LYS_NOTIF: |
| 5622 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5623 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5624 | break; |
| 5625 | default: |
| 5626 | must_size = 0; |
| 5627 | break; |
| 5628 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5632 | 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] | 5633 | return -1; |
| 5634 | } |
| 5635 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5636 | 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] | 5637 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5638 | if (!set.val.bool) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 5639 | if ((ignore_fail == 1) || ((must[i].flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5640 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5641 | } else { |
| 5642 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5643 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5644 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5645 | } |
| 5646 | if (must[i].eapptag) { |
| 5647 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5648 | } |
| 5649 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5650 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5651 | } |
| 5652 | } |
| 5653 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5654 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5655 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5656 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5657 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5658 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5659 | * |
| 5660 | * @param[in] schema Schema node with the when condition. |
| 5661 | * @param[out] ctx_snode When schema context node. |
| 5662 | * @param[out] ctx_snode_type Schema context node type. |
| 5663 | */ |
| 5664 | void |
| 5665 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5666 | { |
| 5667 | const struct lys_node *sparent; |
| 5668 | |
| 5669 | /* find a not schema-only node */ |
| 5670 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5671 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5672 | if (schema->nodetype == LYS_AUGMENT) { |
| 5673 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5674 | } else { |
| 5675 | sparent = schema->parent; |
| 5676 | } |
| 5677 | if (!sparent) { |
| 5678 | /* context node is the document root (fake root in our case) */ |
| 5679 | if (schema->flags & LYS_CONFIG_W) { |
| 5680 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5681 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5682 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5683 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5684 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5685 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5686 | break; |
| 5687 | } |
| 5688 | schema = sparent; |
| 5689 | } |
| 5690 | |
| 5691 | *ctx_snode = (struct lys_node *)schema; |
| 5692 | } |
| 5693 | |
| 5694 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5695 | * @brief Resolve (find) when condition context node. Does not log. |
| 5696 | * |
| 5697 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5698 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5699 | * @param[out] ctx_node Context node. |
| 5700 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5701 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5702 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5703 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5704 | static int |
| 5705 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5706 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5707 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5708 | struct lyd_node *parent; |
| 5709 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5710 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5711 | uint16_t i, data_depth, schema_depth; |
| 5712 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5713 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5714 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5715 | if (node_type == LYXP_NODE_ELEM) { |
| 5716 | /* standard element context node */ |
| 5717 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5718 | for (sparent = schema, schema_depth = 0; |
| 5719 | sparent; |
| 5720 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5721 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5722 | ++schema_depth; |
| 5723 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5724 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5725 | if (data_depth < schema_depth) { |
| 5726 | return -1; |
| 5727 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5728 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5729 | /* find the corresponding data node */ |
| 5730 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5731 | node = node->parent; |
| 5732 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5733 | if (node->schema != schema) { |
| 5734 | return -1; |
| 5735 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5736 | } else { |
| 5737 | /* root context node */ |
| 5738 | while (node->parent) { |
| 5739 | node = node->parent; |
| 5740 | } |
| 5741 | while (node->prev->next) { |
| 5742 | node = node->prev; |
| 5743 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5744 | } |
| 5745 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5746 | *ctx_node = node; |
| 5747 | *ctx_node_type = node_type; |
| 5748 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5749 | } |
| 5750 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5751 | /** |
| 5752 | * @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] | 5753 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5754 | * |
| 5755 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5756 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5757 | * it is moved to point to another sibling still in the original tree. |
| 5758 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5759 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5760 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5761 | * Ordering may change, but there will be no semantic change. |
| 5762 | * |
| 5763 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5764 | */ |
| 5765 | static int |
| 5766 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5767 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5768 | { |
| 5769 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5770 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5771 | |
| 5772 | switch (snode->nodetype) { |
| 5773 | case LYS_AUGMENT: |
| 5774 | case LYS_USES: |
| 5775 | case LYS_CHOICE: |
| 5776 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5777 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5778 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5779 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5780 | continue; |
| 5781 | } |
| 5782 | |
| 5783 | if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5784 | return -1; |
| 5785 | } |
| 5786 | } |
| 5787 | break; |
| 5788 | case LYS_CONTAINER: |
| 5789 | case LYS_LIST: |
| 5790 | case LYS_LEAF: |
| 5791 | case LYS_LEAFLIST: |
| 5792 | case LYS_ANYXML: |
| 5793 | case LYS_ANYDATA: |
| 5794 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5795 | if (elem->schema == snode) { |
| 5796 | |
| 5797 | if (elem == *ctx_node) { |
| 5798 | /* We are going to unlink our context node! This normally cannot happen, |
| 5799 | * but we use normal top-level data nodes for faking a document root node, |
| 5800 | * so if this is the context node, we just use the next top-level node. |
| 5801 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5802 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5803 | * lyxp_eval() can handle this special situation. |
| 5804 | */ |
| 5805 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5806 | LOGINT; |
| 5807 | return -1; |
| 5808 | } |
| 5809 | |
| 5810 | if (elem->prev == elem) { |
| 5811 | /* unlinking last top-level element, use an empty data tree */ |
| 5812 | *ctx_node = NULL; |
| 5813 | } else { |
| 5814 | /* in this case just use the previous/last top-level data node */ |
| 5815 | *ctx_node = elem->prev; |
| 5816 | } |
| 5817 | } else if (elem == *node) { |
| 5818 | /* We are going to unlink the currently processed node. This does not matter that |
| 5819 | * much, but we would lose access to the original data tree, so just move our |
| 5820 | * pointer somewhere still inside it. |
| 5821 | */ |
| 5822 | if ((*node)->prev != *node) { |
| 5823 | *node = (*node)->prev; |
| 5824 | } else { |
| 5825 | /* the processed node with sibings were all unlinked, oh well */ |
| 5826 | *node = NULL; |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5831 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5832 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5833 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5834 | LOGINT; |
| 5835 | return -1; |
| 5836 | } |
| 5837 | } else { |
| 5838 | *unlinked_nodes = elem; |
| 5839 | } |
| 5840 | |
| 5841 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5842 | /* there can be only one instance */ |
| 5843 | break; |
| 5844 | } |
| 5845 | } |
| 5846 | } |
| 5847 | break; |
| 5848 | default: |
| 5849 | LOGINT; |
| 5850 | return -1; |
| 5851 | } |
| 5852 | |
| 5853 | return EXIT_SUCCESS; |
| 5854 | } |
| 5855 | |
| 5856 | /** |
| 5857 | * @brief Relink the unlinked nodes back. |
| 5858 | * |
| 5859 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5860 | * we simply need a sibling from the original data tree. |
| 5861 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5862 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5863 | * or the sibling of \p unlinked_nodes. |
| 5864 | * |
| 5865 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5866 | */ |
| 5867 | static int |
| 5868 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5869 | { |
| 5870 | struct lyd_node *elem; |
| 5871 | |
| 5872 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5873 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5874 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5875 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5876 | return -1; |
| 5877 | } |
| 5878 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5879 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5880 | return -1; |
| 5881 | } |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | return EXIT_SUCCESS; |
| 5886 | } |
| 5887 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5888 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5889 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5890 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5891 | int ret = 0; |
| 5892 | uint8_t must_size; |
| 5893 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5894 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5895 | assert(node); |
| 5896 | |
| 5897 | schema = node->schema; |
| 5898 | |
| 5899 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5900 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5901 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5902 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5903 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5904 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5905 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5906 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5907 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5908 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5909 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5910 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5911 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5912 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5913 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5914 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5915 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5916 | break; |
| 5917 | case LYS_NOTIF: |
| 5918 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5919 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5920 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5921 | must_size = 0; |
| 5922 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5923 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5924 | |
| 5925 | if (must_size) { |
| 5926 | ++ret; |
| 5927 | } |
| 5928 | |
| 5929 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5930 | if (!node->prev->next) { |
| 5931 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5932 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5933 | ret += 0x2; |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5941 | 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] | 5942 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5943 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5944 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5945 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5946 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5947 | 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] | 5948 | return 1; |
| 5949 | } |
| 5950 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5951 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5952 | goto check_augment; |
| 5953 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5954 | while (parent) { |
| 5955 | /* stop conditions */ |
| 5956 | if (!mode) { |
| 5957 | /* stop on node that can be instantiated in data tree */ |
| 5958 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5959 | break; |
| 5960 | } |
| 5961 | } else { |
| 5962 | /* stop on the specified node */ |
| 5963 | if (parent == stop) { |
| 5964 | break; |
| 5965 | } |
| 5966 | } |
| 5967 | |
| 5968 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5969 | return 1; |
| 5970 | } |
| 5971 | check_augment: |
| 5972 | |
| 5973 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5974 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5975 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5976 | } |
| 5977 | parent = lys_parent(parent); |
| 5978 | } |
| 5979 | |
| 5980 | return 0; |
| 5981 | } |
| 5982 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5983 | /** |
| 5984 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5985 | * Logs directly. |
| 5986 | * |
| 5987 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 5988 | * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied |
| 5989 | * only when requiring external dependencies. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5990 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5991 | * @return |
| 5992 | * -1 - error, ly_errno is set |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 5993 | * 0 - all "when" statements true |
| 5994 | * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5995 | * 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] | 5996 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5997 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 5998 | resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5999 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6000 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6001 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6002 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6003 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6004 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6005 | |
| 6006 | assert(node); |
| 6007 | memset(&set, 0, sizeof set); |
| 6008 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6009 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6010 | /* make the node dummy for the evaluation */ |
| 6011 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6012 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6013 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6014 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6015 | if (rc) { |
| 6016 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6017 | 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] | 6018 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6019 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6020 | } |
| 6021 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6022 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6023 | 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] | 6024 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6025 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6026 | if ((ignore_fail == 1) |
| 6027 | || ((((struct lys_node_container *)node->schema)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6028 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6029 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6030 | } else { |
| 6031 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6032 | if (failed_when) { |
| 6033 | *failed_when = ((struct lys_node_container *)node->schema)->when; |
| 6034 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6035 | goto cleanup; |
| 6036 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6037 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6038 | |
| 6039 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6040 | 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] | 6041 | } |
| 6042 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6043 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6044 | goto check_augment; |
| 6045 | |
| 6046 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6047 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6048 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6049 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6050 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6051 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6052 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6053 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6054 | } |
| 6055 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6056 | |
| 6057 | unlinked_nodes = NULL; |
| 6058 | /* we do not want our node pointer to change */ |
| 6059 | tmp_node = node; |
| 6060 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6061 | if (rc) { |
| 6062 | goto cleanup; |
| 6063 | } |
| 6064 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6065 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6066 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6067 | |
| 6068 | if (unlinked_nodes && ctx_node) { |
| 6069 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6070 | rc = -1; |
| 6071 | goto cleanup; |
| 6072 | } |
| 6073 | } |
| 6074 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6075 | if (rc) { |
| 6076 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6077 | 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] | 6078 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6079 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6080 | } |
| 6081 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6082 | 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] | 6083 | if (!set.val.bool) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6084 | if ((ignore_fail == 1) |
| 6085 | || ((((struct lys_node_uses *)sparent)->when->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6086 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6087 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6088 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6089 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6090 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6091 | if (failed_when) { |
| 6092 | *failed_when = ((struct lys_node_uses *)sparent)->when; |
| 6093 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6094 | goto cleanup; |
| 6095 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6096 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6097 | |
| 6098 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6099 | 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] | 6100 | } |
| 6101 | |
| 6102 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6103 | 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] | 6104 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6105 | 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] | 6106 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6107 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6108 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6109 | } |
| 6110 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6111 | |
| 6112 | unlinked_nodes = NULL; |
| 6113 | tmp_node = node; |
| 6114 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6115 | if (rc) { |
| 6116 | goto cleanup; |
| 6117 | } |
| 6118 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6119 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6120 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6121 | |
| 6122 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6123 | * so the tree did not actually change and there is nothing for us to do |
| 6124 | */ |
| 6125 | if (unlinked_nodes && ctx_node) { |
| 6126 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6127 | rc = -1; |
| 6128 | goto cleanup; |
| 6129 | } |
| 6130 | } |
| 6131 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6132 | if (rc) { |
| 6133 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6134 | 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] | 6135 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6136 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6137 | } |
| 6138 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6139 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6140 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6141 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6142 | if ((ignore_fail == 1) |
| 6143 | || ((((struct lys_node_augment *)sparent->parent)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6144 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6145 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6146 | } else { |
| 6147 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6148 | if (failed_when) { |
| 6149 | *failed_when = ((struct lys_node_augment *)sparent->parent)->when; |
| 6150 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6151 | goto cleanup; |
| 6152 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6153 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6154 | |
| 6155 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6156 | 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] | 6157 | } |
| 6158 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6159 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6160 | } |
| 6161 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6162 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6163 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6164 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6165 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6166 | 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] | 6167 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6168 | } |
| 6169 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6170 | static int |
| 6171 | check_leafref_features(struct lys_type *type) |
| 6172 | { |
| 6173 | struct lys_node *iter; |
| 6174 | struct ly_set *src_parents, *trg_parents, *features; |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 6175 | struct lys_node_augment *aug; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6176 | unsigned int i, j, size, x; |
| 6177 | int ret = EXIT_SUCCESS; |
| 6178 | |
| 6179 | assert(type->parent); |
| 6180 | |
| 6181 | src_parents = ly_set_new(); |
| 6182 | trg_parents = ly_set_new(); |
| 6183 | features = ly_set_new(); |
| 6184 | |
| 6185 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6186 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6187 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6188 | continue; |
| 6189 | } |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 6190 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT) && lys_node_module(iter->parent)->implemented) { |
| 6191 | aug = (struct lys_node_augment *)iter->parent; |
| 6192 | if ((aug->flags & LYS_NOTAPPLIED) || !aug->target) { |
| 6193 | /* unresolved augment, wait until it's resolved */ |
| 6194 | ret = EXIT_FAILURE; |
| 6195 | goto cleanup; |
| 6196 | } |
| 6197 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6198 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6199 | } |
| 6200 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6201 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6202 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6203 | continue; |
| 6204 | } |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 6205 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT) && lys_node_module(iter->parent)->implemented) { |
| 6206 | aug = (struct lys_node_augment *)iter->parent; |
| 6207 | if ((aug->flags & LYS_NOTAPPLIED) || !aug->target) { |
| 6208 | /* unresolved augment, wait until it's resolved */ |
| 6209 | ret = EXIT_FAILURE; |
| 6210 | goto cleanup; |
| 6211 | } |
| 6212 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6213 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6214 | } |
| 6215 | |
| 6216 | /* compare the features used in if-feature statements in the rest of both |
| 6217 | * chains of parents. The set of features used for target must be a subset |
| 6218 | * of features used for the leafref. This is not a perfect, we should compare |
| 6219 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6220 | for (i = 0; i < src_parents->number; i++) { |
| 6221 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6222 | if (!iter->iffeature_size) { |
| 6223 | continue; |
| 6224 | } |
| 6225 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6226 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6227 | for (; size; size--) { |
| 6228 | if (!iter->iffeature[j].features[size - 1]) { |
| 6229 | /* not yet resolved feature, postpone this check */ |
| 6230 | ret = EXIT_FAILURE; |
| 6231 | goto cleanup; |
| 6232 | } |
| 6233 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | x = features->number; |
| 6238 | for (i = 0; i < trg_parents->number; i++) { |
| 6239 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6240 | if (!iter->iffeature_size) { |
| 6241 | continue; |
| 6242 | } |
| 6243 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6244 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6245 | for (; size; size--) { |
| 6246 | if (!iter->iffeature[j].features[size - 1]) { |
| 6247 | /* not yet resolved feature, postpone this check */ |
| 6248 | ret = EXIT_FAILURE; |
| 6249 | goto cleanup; |
| 6250 | } |
| 6251 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6252 | /* the feature is not present in features set of target's parents chain */ |
| 6253 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 6254 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6255 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6256 | iter->iffeature[j].features[size - 1]->name); |
| 6257 | ret = -1; |
| 6258 | goto cleanup; |
| 6259 | } |
| 6260 | } |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | cleanup: |
| 6265 | ly_set_free(features); |
| 6266 | ly_set_free(src_parents); |
| 6267 | ly_set_free(trg_parents); |
| 6268 | |
| 6269 | return ret; |
| 6270 | } |
| 6271 | |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6272 | static int |
| 6273 | check_type_union_leafref(struct lys_type *type) |
| 6274 | { |
| 6275 | uint8_t i; |
| 6276 | |
| 6277 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6278 | /* go through unions and look for leafref */ |
| 6279 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6280 | switch (type->info.uni.types[i].base) { |
| 6281 | case LY_TYPE_LEAFREF: |
| 6282 | return 1; |
| 6283 | case LY_TYPE_UNION: |
| 6284 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6285 | return 1; |
| 6286 | } |
| 6287 | break; |
| 6288 | default: |
| 6289 | break; |
| 6290 | } |
| 6291 | } |
| 6292 | |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
| 6296 | /* just inherit the flag value */ |
| 6297 | return type->der->has_union_leafref; |
| 6298 | } |
| 6299 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6300 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6301 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6302 | * |
| 6303 | * @param[in] mod Main module. |
| 6304 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6305 | * @param[in] type Type of the unresolved item. |
| 6306 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6307 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6308 | * @param[in] final_fail Whether we are just printing errors of the failed unres items. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6309 | * |
| 6310 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6311 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6312 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6313 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6314 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6315 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6316 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 6317 | int rc = -1, has_str = 0, parent_type = 0, i, k, hidden; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6318 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6319 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6320 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6321 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6322 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6323 | struct ly_set *refs, *procs; |
| 6324 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6325 | struct lys_ident *ident; |
| 6326 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6327 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6328 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6329 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6330 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6331 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6332 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6333 | struct lys_ext_instance *ext, **extlist; |
| 6334 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6335 | |
| 6336 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6337 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6338 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6339 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6340 | ident = item; |
| 6341 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6342 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6343 | break; |
| 6344 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6345 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6346 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6347 | stype = item; |
| 6348 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6349 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6350 | break; |
| 6351 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6352 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6353 | stype = item; |
| 6354 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6355 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6356 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6357 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6358 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6359 | if (lys_node_module(node)->implemented) { |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 6360 | /* make all the modules on the path implemented, print verbose messages */ |
| 6361 | hidden = ly_vlog_hidden; |
| 6362 | if (hidden) { |
| 6363 | ly_vlog_hide(0); |
| 6364 | } |
| 6365 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6366 | for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) { |
| 6367 | if (!lys_node_module(next)->implemented) { |
| 6368 | if (lys_set_implemented(lys_node_module(next))) { |
| 6369 | rc = -1; |
| 6370 | break; |
| 6371 | } |
| 6372 | } |
| 6373 | } |
| 6374 | if (next) { |
| 6375 | break; |
| 6376 | } |
| 6377 | |
| 6378 | /* store the backlink from leafref target */ |
| 6379 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6380 | rc = -1; |
| 6381 | } |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 6382 | |
| 6383 | if (hidden) { |
| 6384 | ly_vlog_hide(1); |
| 6385 | } |
| 6386 | |
| 6387 | if (rc) { |
| 6388 | break; |
| 6389 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6390 | } |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 6391 | |
| 6392 | /* check if leafref and its target are under common if-features */ |
| 6393 | rc = check_leafref_features(stype); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6394 | } |
| 6395 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6396 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6397 | case UNRES_TYPE_DER_EXT: |
| 6398 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6399 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6400 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6401 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6402 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6403 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6404 | /* parent */ |
| 6405 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6406 | stype = item; |
| 6407 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6408 | /* HACK type->der is temporarily unparsed type statement */ |
| 6409 | yin = (struct lyxml_elem *)stype->der; |
| 6410 | stype->der = NULL; |
| 6411 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6412 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6413 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6414 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6415 | |
| 6416 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6417 | /* may try again later */ |
| 6418 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6419 | } else { |
| 6420 | /* 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] | 6421 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6422 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6423 | } |
| 6424 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6425 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6426 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6427 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6428 | /* we need to always be able to free this, it's safe only in this case */ |
| 6429 | lyxml_free(mod->ctx, yin); |
| 6430 | } else { |
| 6431 | /* may try again later, put all back how it was */ |
| 6432 | stype->der = (struct lys_tpdf *)yin; |
| 6433 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6434 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6435 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6436 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6437 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6438 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6439 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6440 | |
| 6441 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6442 | /* fill typedef union leafref flag */ |
| 6443 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6444 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6445 | /* copy the type in case it has union leafref flag */ |
| 6446 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
| 6447 | LOGERR(LY_EINT, "Failed to duplicate type."); |
| 6448 | return -1; |
| 6449 | } |
| 6450 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6451 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6452 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6453 | * by uses statement until the type is resolved. We do that the same way as uses statements inside |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6454 | * grouping. The grouping cannot be used unless the unres counter is 0. |
| 6455 | * To remember that the grouping already increased the counter, the LY_TYPE_ERR is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6456 | * of the type's base member. */ |
| 6457 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6458 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6459 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 6460 | LOGERR(LY_EINT, "Too many unresolved items (type) inside a grouping."); |
| 6461 | return -1; |
| 6462 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6463 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6464 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6465 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6466 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6467 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6468 | iff_data = str_snode; |
| 6469 | 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] | 6470 | if (!rc) { |
| 6471 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6472 | if (iff_data->infeature) { |
| 6473 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6474 | feat = *((struct lys_feature **)item); |
| 6475 | if (!feat->depfeatures) { |
| 6476 | feat->depfeatures = ly_set_new(); |
| 6477 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6478 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6479 | } |
| 6480 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6481 | lydict_remove(mod->ctx, iff_data->fname); |
| 6482 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6483 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6484 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6485 | case UNRES_FEATURE: |
| 6486 | feat = (struct lys_feature *)item; |
| 6487 | |
| 6488 | if (feat->iffeature_size) { |
| 6489 | refs = ly_set_new(); |
| 6490 | procs = ly_set_new(); |
| 6491 | ly_set_add(procs, feat, 0); |
| 6492 | |
| 6493 | while (procs->number) { |
| 6494 | ref = procs->set.g[procs->number - 1]; |
| 6495 | ly_set_rm_index(procs, procs->number - 1); |
| 6496 | |
| 6497 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6498 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6499 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6500 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6501 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6502 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6503 | goto featurecheckdone; |
| 6504 | } |
| 6505 | |
| 6506 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6507 | k = refs->number; |
| 6508 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6509 | /* not yet seen feature, add it for processing */ |
| 6510 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6511 | } |
| 6512 | } |
| 6513 | } else { |
| 6514 | /* forward reference */ |
| 6515 | rc = EXIT_FAILURE; |
| 6516 | goto featurecheckdone; |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | } |
| 6521 | } |
| 6522 | rc = EXIT_SUCCESS; |
| 6523 | |
| 6524 | featurecheckdone: |
| 6525 | ly_set_free(refs); |
| 6526 | ly_set_free(procs); |
| 6527 | } |
| 6528 | |
| 6529 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6530 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6531 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6532 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6533 | case UNRES_TYPEDEF_DFLT: |
| 6534 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6535 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6536 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6537 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6538 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6539 | break; |
| 6540 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6541 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6542 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6543 | choic = item; |
| 6544 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6545 | if (!choic->dflt) { |
| 6546 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6547 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6548 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6549 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6550 | } else { |
| 6551 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6552 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6553 | break; |
| 6554 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6555 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6556 | break; |
| 6557 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6558 | unique_info = (struct unres_list_uniq *)item; |
| 6559 | 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] | 6560 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6561 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6562 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6563 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6564 | case UNRES_XPATH: |
| 6565 | node = (struct lys_node *)item; |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6566 | rc = lys_check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6567 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6568 | case UNRES_EXT: |
| 6569 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6570 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6571 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6572 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6573 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6574 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6575 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6576 | if (eplugin) { |
| 6577 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6578 | u = malloc(sizeof *u); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6579 | LY_CHECK_ERR_RETURN(!u, LOGMEM, -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6580 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6581 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6582 | /* something really bad happend since the extension finalization is not actually |
| 6583 | * being resolved while adding into unres, so something more serious with the unres |
| 6584 | * list itself must happened */ |
| 6585 | return -1; |
| 6586 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6587 | } |
| 6588 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6589 | } |
| 6590 | if (!rc || rc == -1) { |
| 6591 | /* cleanup on success or fatal error */ |
| 6592 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6593 | /* YIN */ |
| 6594 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6595 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6596 | /* YANG */ |
| 6597 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6598 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6599 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6600 | } |
| 6601 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6602 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6603 | u = (uint8_t *)str_snode; |
| 6604 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6605 | free(u); |
| 6606 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6607 | eplugin = ext->def->plugin; |
| 6608 | |
| 6609 | /* inherit */ |
| 6610 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6611 | root = (struct lys_node *)ext->parent; |
| 6612 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6613 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6614 | /* first, check if the node already contain instance of the same extension, |
| 6615 | * in such a case we won't inherit. In case the node was actually defined as |
| 6616 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6617 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6618 | goto inherit_dfs_sibling; |
| 6619 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6620 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6621 | goto inherit_dfs_sibling; |
| 6622 | } |
| 6623 | |
| 6624 | if (eplugin->check_inherit) { |
| 6625 | /* we have a callback to check the inheritance, use it */ |
| 6626 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6627 | case 0: |
| 6628 | /* yes - continue with the inheriting code */ |
| 6629 | break; |
| 6630 | case 1: |
| 6631 | /* no - continue with the node's sibling */ |
| 6632 | goto inherit_dfs_sibling; |
| 6633 | case 2: |
| 6634 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6635 | goto inherit_dfs_child; |
| 6636 | default: |
| 6637 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6638 | ext->def->module->name, ext->def->name, rc); |
| 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | /* inherit the extension */ |
| 6643 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6644 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6645 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6646 | LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM; node->ext = extlist, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6647 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6648 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6649 | |
| 6650 | node->ext = extlist; |
| 6651 | node->ext_size++; |
| 6652 | |
| 6653 | inherit_dfs_child: |
| 6654 | /* modification of - select element for the next run - children first */ |
| 6655 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6656 | next = NULL; |
| 6657 | } else { |
| 6658 | next = node->child; |
| 6659 | } |
| 6660 | if (!next) { |
| 6661 | inherit_dfs_sibling: |
| 6662 | /* no children, try siblings */ |
| 6663 | next = node->next; |
| 6664 | } |
| 6665 | while (!next) { |
| 6666 | /* go to the parent */ |
| 6667 | node = lys_parent(node); |
| 6668 | |
| 6669 | /* we are done if we are back in the root (the starter's parent */ |
| 6670 | if (node == root) { |
| 6671 | break; |
| 6672 | } |
| 6673 | |
| 6674 | /* parent is already processed, go to its sibling */ |
| 6675 | next = node->next; |
| 6676 | } |
| 6677 | } |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | /* final check */ |
| 6682 | if (eplugin->check_result) { |
| 6683 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6684 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6685 | return -1; |
| 6686 | } |
| 6687 | } |
| 6688 | |
| 6689 | rc = 0; |
| 6690 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6691 | default: |
| 6692 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6693 | break; |
| 6694 | } |
| 6695 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6696 | if (has_str && !rc) { |
| 6697 | /* the string is no more needed in case of success. |
| 6698 | * 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] | 6699 | lydict_remove(mod->ctx, str_snode); |
| 6700 | } |
| 6701 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6702 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6703 | } |
| 6704 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6705 | /* logs directly */ |
| 6706 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6707 | 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] | 6708 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6709 | struct lyxml_elem *xml; |
| 6710 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6711 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6712 | const char *name = NULL; |
| 6713 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6714 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6715 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6716 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6717 | 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] | 6718 | break; |
| 6719 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6720 | 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] | 6721 | break; |
| 6722 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6723 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6724 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6725 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6726 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6727 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6728 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6729 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6730 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6731 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6732 | } else { |
| 6733 | LY_TREE_FOR(xml->attr, attr) { |
| 6734 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6735 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6736 | break; |
| 6737 | } |
| 6738 | } |
| 6739 | assert(attr); |
| 6740 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6741 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6742 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6743 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6744 | iff_data = str_node; |
| 6745 | 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] | 6746 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6747 | case UNRES_FEATURE: |
| 6748 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6749 | ((struct lys_feature *)item)->name); |
| 6750 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6751 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6752 | 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] | 6753 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6754 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6755 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6756 | if (str_node) { |
| 6757 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6758 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6759 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6760 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6761 | break; |
| 6762 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6763 | 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] | 6764 | break; |
| 6765 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6766 | 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] | 6767 | break; |
| 6768 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6769 | 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] | 6770 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6771 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6772 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6773 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6774 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6775 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6776 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6777 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6778 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6779 | case UNRES_EXT: |
| 6780 | extinfo = (struct unres_ext *)str_node; |
| 6781 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6782 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6783 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6784 | default: |
| 6785 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6786 | break; |
| 6787 | } |
| 6788 | } |
| 6789 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6790 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6791 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6792 | * |
| 6793 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6794 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6795 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6796 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6797 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6798 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6799 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6800 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6801 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6802 | struct lyxml_elem *yin; |
| 6803 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6804 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6805 | |
| 6806 | assert(unres); |
| 6807 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6808 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 4814fb0 | 2017-08-17 14:49:38 +0200 | [diff] [blame] | 6809 | if (ly_vlog_hidden) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6810 | log_hidden = 1; |
| 6811 | } else { |
| 6812 | log_hidden = 0; |
| 6813 | ly_vlog_hide(1); |
| 6814 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6815 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6816 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6817 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6818 | unres_count = 0; |
| 6819 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6820 | |
| 6821 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6822 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6823 | * if-features are resolved here to make sure that we will have all if-features for |
| 6824 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6825 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6826 | continue; |
| 6827 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6828 | /* 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] | 6829 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6830 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6831 | ++unres_count; |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6832 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6833 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6834 | unres->type[i] = UNRES_RESOLVED; |
| 6835 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6836 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6837 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6838 | if (!log_hidden) { |
| 6839 | ly_vlog_hide(0); |
| 6840 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6841 | /* print the error */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 6842 | ly_err_repeat(ly_parser_data.ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6843 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6844 | } else { |
| 6845 | /* forward reference, erase ly_errno */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 6846 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6847 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6848 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6849 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6850 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6851 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6852 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6853 | if (!log_hidden) { |
| 6854 | ly_vlog_hide(0); |
| 6855 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6856 | |
| 6857 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6858 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6859 | continue; |
| 6860 | } |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6861 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6862 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6863 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6864 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6865 | yang =(struct yang_type *)yin; |
| 6866 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6867 | if (yang->base == LY_TYPE_UNION) { |
| 6868 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6869 | } |
| 6870 | lydict_remove(mod->ctx, yang->name); |
| 6871 | free(yang); |
| 6872 | } else { |
| 6873 | lyxml_free(mod->ctx, yin); |
| 6874 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6875 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6876 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6877 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6878 | } |
| 6879 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6880 | /* the rest except finalizing extensions and xpath */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6881 | for (i = 0; i < unres->count; ++i) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6882 | if ((unres->type[i] == UNRES_RESOLVED) || (unres->type[i] == UNRES_EXT_FINALIZE) || (unres->type[i] == UNRES_XPATH)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6883 | continue; |
| 6884 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6885 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6886 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6887 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6888 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6889 | /* free the allocated structure */ |
| 6890 | free(unres->item[i]); |
| 6891 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6892 | unres->type[i] = UNRES_RESOLVED; |
| 6893 | ++resolved; |
| 6894 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6895 | if (!log_hidden) { |
| 6896 | ly_vlog_hide(0); |
| 6897 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6898 | /* print the error */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 6899 | ly_err_repeat(ly_parser_data.ctx); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6900 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6901 | } else { |
| 6902 | /* forward reference, erase ly_errno */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 6903 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6907 | if (!log_hidden) { |
| 6908 | ly_vlog_hide(0); |
| 6909 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6910 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6911 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6912 | for (i = 0; i < unres->count; ++i) { |
| 6913 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6914 | continue; |
| 6915 | } |
| 6916 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6917 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6918 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6919 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6920 | ++resolved; |
| 6921 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6922 | /* else error - it was already printed, but resolved was not increased, |
| 6923 | so this unres item will not be resolved again in the following code, |
| 6924 | but it will cause returning -1 at the end, this way we are able to |
| 6925 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6926 | } |
| 6927 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6928 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6929 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6930 | * all the validation errors, xpath is resolved only here to properly print all the messages |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6931 | */ |
| 6932 | for (i = 0; i < unres->count; ++i) { |
| 6933 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6934 | continue; |
| 6935 | } |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6936 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6937 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6938 | /* XPath referencing an unknown node is actually supposed to be just a warning */ |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6939 | unres->type[i] = UNRES_RESOLVED; |
| 6940 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6941 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6942 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6943 | if (resolved < unres->count) { |
| 6944 | return -1; |
| 6945 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6946 | } |
| 6947 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6948 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6949 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6950 | return EXIT_SUCCESS; |
| 6951 | } |
| 6952 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6953 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6954 | * @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] | 6955 | * |
| 6956 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6957 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6958 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6959 | * @param[in] type Type of the unresolved item. |
| 6960 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6961 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6962 | * @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] | 6963 | */ |
| 6964 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6965 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6966 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6967 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6968 | int rc; |
| 6969 | const char *dictstr; |
| 6970 | |
| 6971 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6972 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6973 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6974 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6975 | lydict_remove(mod->ctx, dictstr); |
| 6976 | } |
| 6977 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6978 | } |
| 6979 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6980 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6981 | * @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] | 6982 | * |
| 6983 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6984 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6985 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6986 | * @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] | 6987 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6988 | * |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6989 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error, -2 if the unres item |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6990 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6991 | */ |
| 6992 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6993 | 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] | 6994 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6995 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6996 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6997 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6998 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6999 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 7000 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7001 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 7002 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 7003 | uint32_t u; |
| 7004 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7005 | /* check for duplicities in unres */ |
| 7006 | for (u = 0; u < unres->count; u++) { |
| 7007 | if (unres->type[u] == type && unres->item[u] == item && |
| 7008 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 7009 | /* duplication, should not happen */ |
| 7010 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7011 | } |
| 7012 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 7013 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7014 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7015 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH)) { |
| 7016 | /* extension finalization is not even tried when adding the item into the inres list, |
| 7017 | * xpath is not tried because it would hide some potential warnings */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7018 | rc = EXIT_FAILURE; |
| 7019 | } else { |
Michal Vasko | 4814fb0 | 2017-08-17 14:49:38 +0200 | [diff] [blame] | 7020 | if (ly_vlog_hidden) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7021 | log_hidden = 1; |
| 7022 | } else { |
| 7023 | log_hidden = 0; |
| 7024 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7025 | } |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7026 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7027 | if (!log_hidden) { |
| 7028 | ly_vlog_hide(0); |
| 7029 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7030 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7031 | if (rc != EXIT_FAILURE) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7032 | if (rc == -1) { |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7033 | ly_err_repeat(ly_parser_data.ctx); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7034 | } |
| 7035 | if (type == UNRES_LIST_UNIQ) { |
| 7036 | /* free the allocated structure */ |
| 7037 | free(item); |
| 7038 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7039 | /* free the allocated resources */ |
| 7040 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7041 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7042 | return rc; |
| 7043 | } else { |
| 7044 | /* erase info about validation errors */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7045 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7046 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7047 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7048 | print_unres_schema_item_fail(item, type, snode); |
| 7049 | |
| 7050 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7051 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7052 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7053 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7054 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7055 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7056 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7057 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7058 | } |
| 7059 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7060 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7061 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7062 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7063 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7064 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7065 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7066 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7067 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7068 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7069 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7070 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7071 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7072 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7073 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7074 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7075 | } |
| 7076 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7077 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7078 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7079 | * |
| 7080 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7081 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7082 | * @param[in] item Old item to be resolved. |
| 7083 | * @param[in] type Type of the old unresolved item. |
| 7084 | * @param[in] new_item New item to use in the duplicate. |
| 7085 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7086 | * @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] | 7087 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7088 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7089 | 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] | 7090 | { |
| 7091 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7092 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7093 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7094 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7095 | 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] | 7096 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7097 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7098 | if (type == UNRES_LIST_UNIQ) { |
| 7099 | aux_uniq.list = item; |
| 7100 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7101 | item = &aux_uniq; |
| 7102 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7103 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7104 | |
| 7105 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7106 | if (type == UNRES_LIST_UNIQ) { |
| 7107 | free(new_item); |
| 7108 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7109 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7110 | } |
| 7111 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7112 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7113 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7114 | 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] | 7115 | LOGINT; |
| 7116 | return -1; |
| 7117 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7118 | } else if (type == UNRES_IFFEAT) { |
| 7119 | /* duplicate unres_iffeature_data */ |
| 7120 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7121 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7122 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7123 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7124 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7125 | LOGINT; |
| 7126 | return -1; |
| 7127 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7128 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7129 | 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] | 7130 | LOGINT; |
| 7131 | return -1; |
| 7132 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7133 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7134 | |
| 7135 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7136 | } |
| 7137 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7138 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7139 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7140 | 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] | 7141 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7142 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7143 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7144 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7145 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7146 | i = start_on_backwards; |
| 7147 | } else { |
| 7148 | i = unres->count - 1; |
| 7149 | } |
| 7150 | for (; i > -1; i--) { |
| 7151 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7152 | continue; |
| 7153 | } |
| 7154 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7155 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7156 | break; |
| 7157 | } |
| 7158 | } else { |
| 7159 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7160 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7161 | 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] | 7162 | break; |
| 7163 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7164 | } |
| 7165 | } |
| 7166 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7167 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7168 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7169 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7170 | static void |
| 7171 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7172 | { |
| 7173 | struct lyxml_elem *yin; |
| 7174 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7175 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7176 | |
| 7177 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7178 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7179 | case UNRES_TYPE_DER: |
| 7180 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7181 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7182 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7183 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7184 | lydict_remove(ctx, yang->name); |
| 7185 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7186 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7187 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7188 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7189 | } else { |
| 7190 | lyxml_free(ctx, yin); |
| 7191 | } |
| 7192 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7193 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7194 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7195 | lydict_remove(ctx, iff_data->fname); |
| 7196 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7197 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7198 | case UNRES_IDENT: |
| 7199 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7200 | case UNRES_CHOICE_DFLT: |
| 7201 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7202 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7203 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7204 | case UNRES_LIST_UNIQ: |
| 7205 | free(unres->item[i]); |
| 7206 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7207 | case UNRES_EXT: |
| 7208 | free(unres->str_snode[i]); |
| 7209 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7210 | default: |
| 7211 | break; |
| 7212 | } |
| 7213 | unres->type[i] = UNRES_RESOLVED; |
| 7214 | } |
| 7215 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7216 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7217 | unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7218 | { |
| 7219 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7220 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7221 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7222 | if (!unres || !(*unres)) { |
| 7223 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7224 | } |
| 7225 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7226 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7227 | |
| 7228 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7229 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7230 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7231 | unresolved++; |
| 7232 | } |
| 7233 | continue; |
| 7234 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7235 | |
| 7236 | /* free heap memory for the specific item */ |
| 7237 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7240 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7241 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7242 | free((*unres)->item); |
| 7243 | free((*unres)->type); |
| 7244 | free((*unres)->str_snode); |
| 7245 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7246 | free((*unres)); |
| 7247 | (*unres) = NULL; |
| 7248 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7249 | } |
| 7250 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7251 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7252 | * outside the operation subtree, otherwise it is a node from a foreign model) */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7253 | static int |
| 7254 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7255 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7256 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7257 | char *buf; |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7258 | int ret = 0, hidden; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7259 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7260 | if (!json_instid || !json_instid[0]) { |
| 7261 | /* no/empty value */ |
| 7262 | return 0; |
| 7263 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7264 | |
| 7265 | for (op_node = lys_parent(sleaf); |
| 7266 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7267 | op_node = lys_parent(op_node)); |
| 7268 | |
| 7269 | if (op_node && lys_parent(op_node)) { |
| 7270 | /* nested operation - any absolute path is external */ |
| 7271 | return 1; |
| 7272 | } |
| 7273 | |
| 7274 | /* get the first node from the instid */ |
| 7275 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7276 | if (!buf) { |
| 7277 | LOGMEM; |
| 7278 | return -1; |
| 7279 | } |
| 7280 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7281 | /* find the first schema node, do not log */ |
Michal Vasko | 4814fb0 | 2017-08-17 14:49:38 +0200 | [diff] [blame] | 7282 | hidden = ly_vlog_hidden; |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7283 | if (!hidden) { |
| 7284 | ly_vlog_hide(1); |
| 7285 | } |
| 7286 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
| 7287 | if (!hidden) { |
| 7288 | ly_vlog_hide(0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7289 | } |
| 7290 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7291 | if (!first_node) { |
| 7292 | /* unknown path, say it is not external */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7293 | free(buf); |
Radek Krejci | 98caa58 | 2017-09-07 14:32:56 +0200 | [diff] [blame] | 7294 | ly_errno = LY_SUCCESS; |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7295 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7296 | } |
| 7297 | free(buf); |
| 7298 | |
| 7299 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7300 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7301 | if (op_node && (op_node != first_node)) { |
| 7302 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7303 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7304 | } |
| 7305 | |
| 7306 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7307 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7308 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7309 | } |
| 7310 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7311 | /** |
| 7312 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7313 | * |
| 7314 | * @param[in] data Data node where the path is used |
| 7315 | * @param[in] path Instance-identifier node value. |
| 7316 | * @param[in,out] ret Resolved instance or NULL. |
| 7317 | * |
| 7318 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7319 | */ |
| 7320 | static int |
| 7321 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7322 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7323 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7324 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7325 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7326 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7327 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7328 | char *str; |
| 7329 | int mod_len, name_len, has_predicate; |
| 7330 | struct unres_data node_match; |
| 7331 | |
| 7332 | memset(&node_match, 0, sizeof node_match); |
| 7333 | *ret = NULL; |
| 7334 | |
| 7335 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7336 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7337 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7338 | if (root->prev) { |
| 7339 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | /* search for the instance node */ |
| 7343 | while (path[i]) { |
| 7344 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7345 | if (j <= 0) { |
| 7346 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7347 | goto error; |
| 7348 | } |
| 7349 | i += j; |
| 7350 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7351 | if (model) { |
| 7352 | str = strndup(model, mod_len); |
| 7353 | if (!str) { |
| 7354 | LOGMEM; |
| 7355 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7356 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7357 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7358 | if (ctx->data_clb) { |
| 7359 | if (!mod) { |
| 7360 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7361 | } else if (!mod->implemented) { |
| 7362 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7363 | } |
| 7364 | } |
| 7365 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7366 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7367 | if (!mod || !mod->implemented || mod->disabled) { |
| 7368 | break; |
| 7369 | } |
| 7370 | } else if (!prev_mod) { |
| 7371 | /* first iteration and we are missing module name */ |
| 7372 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_NONE, NULL, name_len, name); |
| 7373 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Instane-identifier is missing prefix in the first node."); |
| 7374 | goto error; |
| 7375 | } else { |
| 7376 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7377 | } |
| 7378 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7379 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7380 | /* no instance exists */ |
| 7381 | break; |
| 7382 | } |
| 7383 | |
| 7384 | if (has_predicate) { |
| 7385 | /* we have predicate, so the current results must be list or leaf-list */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7386 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7387 | /* index of the current node (for lists with position predicates) */ |
| 7388 | cur_idx = 1; |
| 7389 | while (j < (signed)node_match.count) { |
| 7390 | node = node_match.node[j]; |
| 7391 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7392 | if (parsed < 1) { |
| 7393 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
| 7394 | goto error; |
| 7395 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7396 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7397 | if (!node) { |
| 7398 | /* current node does not satisfy the predicate */ |
| 7399 | unres_data_del(&node_match, j); |
| 7400 | } else { |
| 7401 | ++j; |
| 7402 | } |
| 7403 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7404 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7405 | |
| 7406 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7407 | } else if (node_match.count) { |
| 7408 | /* check that we are not addressing lists */ |
| 7409 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7410 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7411 | unres_data_del(&node_match, j--); |
| 7412 | } |
| 7413 | } |
| 7414 | if (!node_match.count) { |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7415 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys."); |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7416 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7417 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7418 | |
| 7419 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7420 | } |
| 7421 | |
| 7422 | if (!node_match.count) { |
| 7423 | /* no instance exists */ |
| 7424 | if (req_inst > -1) { |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7425 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7426 | return EXIT_FAILURE; |
| 7427 | } |
| 7428 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7429 | return EXIT_SUCCESS; |
| 7430 | } else if (node_match.count > 1) { |
| 7431 | /* instance identifier must resolve to a single node */ |
| 7432 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7433 | goto error; |
| 7434 | } else { |
| 7435 | /* we have required result, remember it and cleanup */ |
| 7436 | *ret = node_match.node[0]; |
| 7437 | free(node_match.node); |
| 7438 | return EXIT_SUCCESS; |
| 7439 | } |
| 7440 | |
| 7441 | error: |
| 7442 | /* cleanup */ |
| 7443 | free(node_match.node); |
| 7444 | return -1; |
| 7445 | } |
| 7446 | |
| 7447 | static int |
| 7448 | 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] | 7449 | { |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7450 | struct ly_set *set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7451 | uint32_t i; |
| 7452 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7453 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7454 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7455 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 7456 | set = lyd_find_path((struct lyd_node *)leaf, path); |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7457 | if (!set) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7458 | return -1; |
| 7459 | } |
| 7460 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7461 | for (i = 0; i < set->number; ++i) { |
| 7462 | if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7463 | continue; |
| 7464 | } |
| 7465 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7466 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7467 | * so we can simply compare just the values */ |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7468 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7469 | /* we have the match */ |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7470 | *ret = set->set.d[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7471 | break; |
| 7472 | } |
| 7473 | } |
| 7474 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7475 | ly_set_free(set); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7476 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7477 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7478 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7479 | if (req_inst > -1) { |
| 7480 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7481 | return EXIT_FAILURE; |
| 7482 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7483 | 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] | 7484 | } |
| 7485 | } |
| 7486 | |
| 7487 | return EXIT_SUCCESS; |
| 7488 | } |
| 7489 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7490 | /* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7491 | int |
| 7492 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7493 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7494 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7495 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7496 | struct lyd_node *ret; |
| 7497 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7498 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7499 | |
| 7500 | assert(type->base == LY_TYPE_UNION); |
| 7501 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7502 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7503 | /* either NULL or instid previously converted to JSON */ |
| 7504 | json_val = leaf->value.string; |
| 7505 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7506 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7507 | if (store) { |
| 7508 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7509 | free(leaf->value.bit); |
| 7510 | } |
| 7511 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7512 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7513 | |
| 7514 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
Michal Vasko | 4814fb0 | 2017-08-17 14:49:38 +0200 | [diff] [blame] | 7515 | hidden = ly_vlog_hidden; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7516 | ly_vlog_hide(1); |
| 7517 | |
| 7518 | t = NULL; |
| 7519 | found = 0; |
| 7520 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7521 | found = 0; |
| 7522 | |
| 7523 | switch (t->base) { |
| 7524 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7525 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7526 | req_inst = -1; |
| 7527 | } else { |
| 7528 | req_inst = t->info.lref.req; |
| 7529 | } |
| 7530 | |
| 7531 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7532 | if (store) { |
| 7533 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7534 | /* valid resolved */ |
| 7535 | leaf->value.leafref = ret; |
| 7536 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7537 | } else { |
| 7538 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7539 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7540 | return -1; |
| 7541 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7542 | } |
| 7543 | } |
| 7544 | |
| 7545 | success = 1; |
| 7546 | } |
| 7547 | break; |
| 7548 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7549 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7550 | if (ext_dep == -1) { |
| 7551 | return -1; |
| 7552 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7553 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7554 | req_inst = -1; |
| 7555 | } else { |
| 7556 | req_inst = t->info.inst.req; |
| 7557 | } |
| 7558 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7559 | if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7560 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7561 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7562 | /* valid resolved */ |
| 7563 | leaf->value.instance = ret; |
| 7564 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7565 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7566 | if (json_val) { |
| 7567 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7568 | leaf->value_str = json_val; |
| 7569 | json_val = NULL; |
| 7570 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7571 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7572 | /* valid unresolved */ |
| 7573 | if (json_val) { |
| 7574 | /* put the JSON val back */ |
| 7575 | leaf->value.string = json_val; |
| 7576 | json_val = NULL; |
| 7577 | } else { |
| 7578 | leaf->value.instance = NULL; |
| 7579 | } |
| 7580 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7581 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7582 | } |
| 7583 | |
| 7584 | success = 1; |
| 7585 | } |
| 7586 | break; |
| 7587 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7588 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, store, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7589 | success = 1; |
| 7590 | } |
| 7591 | break; |
| 7592 | } |
| 7593 | |
| 7594 | if (success) { |
| 7595 | break; |
| 7596 | } |
| 7597 | |
| 7598 | /* erase information about errors - they are false or irrelevant |
| 7599 | * and will be replaced by a single error messages */ |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7600 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7601 | |
| 7602 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7603 | if (store) { |
| 7604 | if (t->base == LY_TYPE_BITS) { |
| 7605 | free(leaf->value.bit); |
| 7606 | } |
| 7607 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7608 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | /* turn logging back on */ |
| 7612 | if (!hidden) { |
| 7613 | ly_vlog_hide(0); |
| 7614 | } |
| 7615 | |
| 7616 | if (json_val) { |
| 7617 | if (!success) { |
| 7618 | /* put the value back for now */ |
| 7619 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7620 | leaf->value.string = json_val; |
| 7621 | } else { |
| 7622 | /* value was ultimately useless, but we could not have known */ |
| 7623 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7624 | } |
| 7625 | } |
| 7626 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7627 | if (success) { |
| 7628 | if (resolved_type) { |
| 7629 | *resolved_type = t; |
| 7630 | } |
| 7631 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7632 | /* not found and it is required */ |
| 7633 | 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] | 7634 | return EXIT_FAILURE; |
| 7635 | } |
| 7636 | |
| 7637 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7638 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7639 | } |
| 7640 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7641 | /** |
| 7642 | * @brief Resolve a single unres data item. Logs directly. |
| 7643 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7644 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7645 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7646 | * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7647 | * |
| 7648 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7649 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7650 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7651 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7652 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7653 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7654 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7655 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7656 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7657 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7658 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7659 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7660 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7661 | switch (type) { |
| 7662 | case UNRES_LEAFREF: |
| 7663 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7664 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7665 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7666 | req_inst = -1; |
| 7667 | } else { |
| 7668 | req_inst = sleaf->type.info.lref.req; |
| 7669 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7670 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7671 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7672 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7673 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7674 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7675 | free(leaf->value.bit); |
| 7676 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7677 | leaf->value.leafref = ret; |
| 7678 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7679 | } else { |
| 7680 | /* valid unresolved */ |
| 7681 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7682 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7683 | return -1; |
| 7684 | } |
| 7685 | } |
| 7686 | } |
| 7687 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7688 | } else { |
| 7689 | return rc; |
| 7690 | } |
| 7691 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7692 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7693 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7694 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7695 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7696 | if (ext_dep == -1) { |
| 7697 | return -1; |
| 7698 | } |
| 7699 | |
| 7700 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7701 | req_inst = -1; |
| 7702 | } else { |
| 7703 | req_inst = sleaf->type.info.inst.req; |
| 7704 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7705 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7706 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7707 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7708 | /* valid resolved */ |
| 7709 | leaf->value.instance = ret; |
| 7710 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7711 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7712 | /* valid unresolved */ |
| 7713 | leaf->value.instance = NULL; |
| 7714 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7715 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7716 | } else { |
| 7717 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7718 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7719 | break; |
| 7720 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7721 | case UNRES_UNION: |
| 7722 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7723 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7724 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7725 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7726 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7727 | return rc; |
| 7728 | } |
| 7729 | break; |
| 7730 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7731 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7732 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7733 | return rc; |
| 7734 | } |
| 7735 | break; |
| 7736 | |
| 7737 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7738 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7739 | return rc; |
| 7740 | } |
| 7741 | break; |
| 7742 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7743 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7744 | LOGINT; |
| 7745 | return -1; |
| 7746 | } |
| 7747 | |
| 7748 | return EXIT_SUCCESS; |
| 7749 | } |
| 7750 | |
| 7751 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7752 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7753 | * |
| 7754 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7755 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7756 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7757 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7758 | */ |
| 7759 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7760 | 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] | 7761 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7762 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7763 | 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] | 7764 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7765 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7766 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7767 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7768 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7769 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7770 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7771 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7772 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7773 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7774 | if (type == UNRES_WHEN) { |
| 7775 | /* remove previous result */ |
| 7776 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7777 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7778 | |
| 7779 | return EXIT_SUCCESS; |
| 7780 | } |
| 7781 | |
| 7782 | /** |
| 7783 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7784 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7785 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7786 | * unresolved leafrefs/instids are accepted). |
| 7787 | * |
| 7788 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7789 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7790 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7791 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7792 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7793 | * |
| 7794 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7795 | */ |
| 7796 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7797 | 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] | 7798 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7799 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7800 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7801 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7802 | struct lys_when *when; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7803 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7804 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7805 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7806 | |
| 7807 | if (!unres->count) { |
| 7808 | return EXIT_SUCCESS; |
| 7809 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7810 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7811 | if (options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7812 | ignore_fail = 1; |
| 7813 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7814 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7815 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7816 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7817 | } |
| 7818 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7819 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7820 | ly_vlog_hide(1); |
| 7821 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7822 | /* when-stmt first */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7823 | first = 1; |
| 7824 | stmt_count = 0; |
| 7825 | resolved = 0; |
| 7826 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7827 | do { |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7828 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7829 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7830 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7831 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7832 | continue; |
| 7833 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7834 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7835 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7836 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7837 | } |
| 7838 | |
| 7839 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7840 | for (parent = unres->node[i]->parent; |
| 7841 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7842 | parent = parent->parent) { |
| 7843 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7844 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 7845 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7846 | */ |
| 7847 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7848 | unres->type[i] = UNRES_RESOLVED; |
| 7849 | resolved++; |
| 7850 | break; |
| 7851 | } |
| 7852 | } |
| 7853 | if (parent) { |
| 7854 | continue; |
| 7855 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7856 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7857 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7858 | if (!rc) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7859 | /* finish with error/delete the node only if when was false, an external dependency was not required, |
| 7860 | * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */ |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 7861 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7862 | && (!(when->flags & LYS_XPATH_DEP) || !(options & LYD_OPT_NOEXTDEPS))) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7863 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7864 | /* false when condition */ |
| 7865 | ly_vlog_hide(0); |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7866 | ly_err_repeat(ly_parser_data.ctx); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7867 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7868 | } /* follows else */ |
| 7869 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7870 | /* auto-delete */ |
| 7871 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7872 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7873 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7874 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7875 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7876 | * remove the container |
| 7877 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7878 | for (parent = unres->node[i]; |
| 7879 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7880 | parent = parent->parent) { |
| 7881 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7882 | /* presence container */ |
| 7883 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7884 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7885 | if (parent->next || parent->prev != parent) { |
| 7886 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7887 | break; |
| 7888 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7889 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7890 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7891 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7892 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7893 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7894 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7895 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7896 | lyd_unlink(unres->node[i]); |
| 7897 | unres->type[i] = UNRES_DELETE; |
| 7898 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7899 | |
| 7900 | /* update the rest of unres items */ |
| 7901 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7902 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7903 | continue; |
| 7904 | } |
| 7905 | |
| 7906 | /* test if the node is in subtree to be deleted */ |
| 7907 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7908 | if (parent == unres->node[i]) { |
| 7909 | /* yes, it is */ |
| 7910 | unres->type[j] = UNRES_RESOLVED; |
| 7911 | resolved++; |
| 7912 | break; |
| 7913 | } |
| 7914 | } |
| 7915 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7916 | } else { |
| 7917 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7918 | } |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7919 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7920 | resolved++; |
| 7921 | progress = 1; |
| 7922 | } else if (rc == -1) { |
| 7923 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7924 | /* print only this last error */ |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7925 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7926 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7927 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7928 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7929 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7930 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7931 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7932 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7933 | if (stmt_count > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7934 | ly_vlog_hide(0); |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7935 | ly_err_repeat(ly_parser_data.ctx); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7936 | return -1; |
| 7937 | } |
| 7938 | |
| 7939 | for (i = 0; del_items && i < unres->count; i++) { |
| 7940 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7941 | if (unres->type[i] != UNRES_DELETE) { |
| 7942 | continue; |
| 7943 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7944 | if (!unres->node[i]) { |
| 7945 | unres->type[i] = UNRES_RESOLVED; |
| 7946 | del_items--; |
| 7947 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | /* really remove the complete subtree */ |
| 7951 | lyd_free(unres->node[i]); |
| 7952 | unres->type[i] = UNRES_RESOLVED; |
| 7953 | del_items--; |
| 7954 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7955 | |
| 7956 | /* now leafrefs */ |
| 7957 | first = 1; |
| 7958 | stmt_count = 0; |
| 7959 | resolved = 0; |
| 7960 | do { |
| 7961 | progress = 0; |
| 7962 | for (i = 0; i < unres->count; i++) { |
| 7963 | if (unres->type[i] != UNRES_LEAFREF) { |
| 7964 | continue; |
| 7965 | } |
| 7966 | if (first) { |
| 7967 | /* count leafref nodes in unres list */ |
| 7968 | stmt_count++; |
| 7969 | } |
| 7970 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7971 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7972 | if (!rc) { |
| 7973 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7974 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7975 | resolved++; |
| 7976 | progress = 1; |
| 7977 | } else if (rc == -1) { |
| 7978 | ly_vlog_hide(0); |
| 7979 | /* print only this last error */ |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7980 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7981 | return -1; |
| 7982 | } /* else forward reference */ |
| 7983 | } |
| 7984 | first = 0; |
| 7985 | } while (progress && resolved < stmt_count); |
| 7986 | |
| 7987 | /* do we have some unresolved leafrefs? */ |
| 7988 | if (stmt_count > resolved) { |
| 7989 | ly_vlog_hide(0); |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 7990 | ly_err_repeat(ly_parser_data.ctx); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7991 | return -1; |
| 7992 | } |
| 7993 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7994 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7995 | |
| 7996 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7997 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7998 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7999 | continue; |
| 8000 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8001 | 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] | 8002 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8003 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8004 | if (rc) { |
| 8005 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8006 | return -1; |
| 8007 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8008 | |
| 8009 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8010 | } |
| 8011 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8012 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8013 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8014 | return EXIT_SUCCESS; |
| 8015 | } |