Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 32 | #include "extensions.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 33 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 34 | int |
| 35 | parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num) |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 36 | { |
| 37 | const char *ptr; |
| 38 | int minus = 0; |
| 39 | int64_t ret = 0; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 40 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 41 | |
| 42 | ptr = *str_num; |
| 43 | |
| 44 | if (ptr[0] == '-') { |
| 45 | minus = 1; |
| 46 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 47 | } else if (ptr[0] == '+') { |
| 48 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 51 | if (!isdigit(ptr[0])) { |
| 52 | /* there must be at least one */ |
| 53 | return 1; |
| 54 | } |
| 55 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 57 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 58 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (ptr[0] == '.') { |
| 62 | if (ptr[1] == '.') { |
| 63 | /* it's the next interval */ |
| 64 | break; |
| 65 | } |
| 66 | ++str_dig; |
| 67 | } else { |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 68 | ret = ret * 10 + (ptr[0] - '0'); |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 69 | if (str_dig > -1) { |
| 70 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 71 | if (ptr[0] == '0') { |
| 72 | /* possibly trailing zero */ |
| 73 | trailing_zeros++; |
| 74 | } else { |
| 75 | trailing_zeros = 0; |
| 76 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 77 | } |
| 78 | ++str_exp; |
| 79 | } |
| 80 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 81 | if (str_dig == 0) { |
| 82 | /* no digits after '.' */ |
| 83 | return 1; |
| 84 | } else if (str_dig == -1) { |
| 85 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 86 | str_dig = 0; |
| 87 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 88 | /* remove trailing zeros */ |
| 89 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 90 | str_dig -= trailing_zeros; |
| 91 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 92 | ret = ret / dec_pow(trailing_zeros); |
| 93 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 94 | |
| 95 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 96 | if (str_dig < dig) { |
| 97 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 98 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 99 | } |
| 100 | ret *= dec_pow(dig - str_dig); |
| 101 | } |
| 102 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 103 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (minus) { |
| 107 | ret *= -1; |
| 108 | } |
| 109 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 110 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 111 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 112 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 116 | * @brief Parse an identifier. |
| 117 | * |
| 118 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 119 | * identifier = (ALPHA / "_") |
| 120 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 121 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 122 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 123 | * |
| 124 | * @return Number of characters successfully parsed. |
| 125 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 126 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 127 | parse_identifier(const char *id) |
| 128 | { |
| 129 | int parsed = 0; |
| 130 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 131 | assert(id); |
| 132 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 133 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 134 | return -parsed; |
| 135 | } |
| 136 | |
| 137 | ++parsed; |
| 138 | ++id; |
| 139 | |
| 140 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 141 | ++parsed; |
| 142 | ++id; |
| 143 | } |
| 144 | |
| 145 | return parsed; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief Parse a node-identifier. |
| 150 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 151 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 152 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 153 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 154 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 155 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 156 | * @param[out] name Points to the node name. |
| 157 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 158 | * @param[out] all_desc Whether the path starts with '/', only supported in extended paths. |
| 159 | * @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] | 160 | * |
| 161 | * @return Number of characters successfully parsed, |
| 162 | * positive on success, negative on failure. |
| 163 | */ |
| 164 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 165 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 166 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 167 | { |
| 168 | int parsed = 0, ret; |
| 169 | |
| 170 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 171 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 172 | assert((name && nam_len) || (!name && !nam_len)); |
| 173 | assert(!extended || all_desc); |
| 174 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 175 | if (mod_name) { |
| 176 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 177 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 178 | } |
| 179 | if (name) { |
| 180 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 181 | *nam_len = 0; |
| 182 | } |
| 183 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 184 | if (extended) { |
| 185 | /* try to parse only the extended expressions */ |
| 186 | if (id[parsed] == '/') { |
| 187 | *all_desc = 1; |
| 188 | } else { |
| 189 | *all_desc = 0; |
| 190 | } |
| 191 | |
| 192 | /* is there a prefix? */ |
| 193 | ret = parse_identifier(id + *all_desc); |
| 194 | if (ret > 0) { |
| 195 | if (id[*all_desc + ret] != ':') { |
| 196 | /* this is not a prefix, so not an extended id */ |
| 197 | goto standard_id; |
| 198 | } |
| 199 | |
| 200 | if (mod_name) { |
| 201 | *mod_name = id + *all_desc; |
| 202 | *mod_name_len = ret; |
| 203 | } |
| 204 | |
| 205 | /* "/" and ":" */ |
| 206 | ret += *all_desc + 1; |
| 207 | } else { |
| 208 | ret = *all_desc; |
| 209 | } |
| 210 | |
| 211 | /* parse either "*" or "." */ |
| 212 | if (!strcmp(id + ret, "*")) { |
| 213 | if (name) { |
| 214 | *name = id + ret; |
| 215 | *nam_len = 1; |
| 216 | } |
| 217 | ++ret; |
| 218 | |
| 219 | return ret; |
| 220 | } else if (!strcmp(id + ret, ".")) { |
| 221 | if (!*all_desc) { |
| 222 | /* /. is redundant expression, we do not accept it */ |
| 223 | return -ret; |
| 224 | } |
| 225 | |
| 226 | if (name) { |
| 227 | *name = id + ret; |
| 228 | *nam_len = 1; |
| 229 | } |
| 230 | ++ret; |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | /* else a standard id, parse it all again */ |
| 235 | } |
| 236 | |
| 237 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 238 | if ((ret = parse_identifier(id)) < 1) { |
| 239 | return ret; |
| 240 | } |
| 241 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 242 | if (mod_name) { |
| 243 | *mod_name = id; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 244 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | parsed += ret; |
| 248 | id += ret; |
| 249 | |
| 250 | /* there is prefix */ |
| 251 | if (id[0] == ':') { |
| 252 | ++parsed; |
| 253 | ++id; |
| 254 | |
| 255 | /* there isn't */ |
| 256 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 257 | if (name && mod_name) { |
| 258 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 259 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 260 | if (mod_name) { |
| 261 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 264 | if (nam_len && mod_name_len) { |
| 265 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 266 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 267 | if (mod_name_len) { |
| 268 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return parsed; |
| 272 | } |
| 273 | |
| 274 | /* identifier (node name) */ |
| 275 | if ((ret = parse_identifier(id)) < 1) { |
| 276 | return -parsed+ret; |
| 277 | } |
| 278 | |
| 279 | if (name) { |
| 280 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 281 | *nam_len = ret; |
| 282 | } |
| 283 | |
| 284 | return parsed+ret; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @brief Parse a path-predicate (leafref). |
| 289 | * |
| 290 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 291 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 292 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 293 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 294 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 295 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 296 | * @param[out] name Points to the node name. |
| 297 | * @param[out] nam_len Length of the node name. |
| 298 | * @param[out] path_key_expr Points to the path-key-expr. |
| 299 | * @param[out] pke_len Length of the path-key-expr. |
| 300 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 301 | * |
| 302 | * @return Number of characters successfully parsed, |
| 303 | * positive on success, negative on failure. |
| 304 | */ |
| 305 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 306 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 307 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 308 | { |
| 309 | const char *ptr; |
| 310 | int parsed = 0, ret; |
| 311 | |
| 312 | assert(id); |
| 313 | if (prefix) { |
| 314 | *prefix = NULL; |
| 315 | } |
| 316 | if (pref_len) { |
| 317 | *pref_len = 0; |
| 318 | } |
| 319 | if (name) { |
| 320 | *name = NULL; |
| 321 | } |
| 322 | if (nam_len) { |
| 323 | *nam_len = 0; |
| 324 | } |
| 325 | if (path_key_expr) { |
| 326 | *path_key_expr = NULL; |
| 327 | } |
| 328 | if (pke_len) { |
| 329 | *pke_len = 0; |
| 330 | } |
| 331 | if (has_predicate) { |
| 332 | *has_predicate = 0; |
| 333 | } |
| 334 | |
| 335 | if (id[0] != '[') { |
| 336 | return -parsed; |
| 337 | } |
| 338 | |
| 339 | ++parsed; |
| 340 | ++id; |
| 341 | |
| 342 | while (isspace(id[0])) { |
| 343 | ++parsed; |
| 344 | ++id; |
| 345 | } |
| 346 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 347 | 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] | 348 | return -parsed+ret; |
| 349 | } |
| 350 | |
| 351 | parsed += ret; |
| 352 | id += ret; |
| 353 | |
| 354 | while (isspace(id[0])) { |
| 355 | ++parsed; |
| 356 | ++id; |
| 357 | } |
| 358 | |
| 359 | if (id[0] != '=') { |
| 360 | return -parsed; |
| 361 | } |
| 362 | |
| 363 | ++parsed; |
| 364 | ++id; |
| 365 | |
| 366 | while (isspace(id[0])) { |
| 367 | ++parsed; |
| 368 | ++id; |
| 369 | } |
| 370 | |
| 371 | if ((ptr = strchr(id, ']')) == NULL) { |
| 372 | return -parsed; |
| 373 | } |
| 374 | |
| 375 | --ptr; |
| 376 | while (isspace(ptr[0])) { |
| 377 | --ptr; |
| 378 | } |
| 379 | ++ptr; |
| 380 | |
| 381 | ret = ptr-id; |
| 382 | if (path_key_expr) { |
| 383 | *path_key_expr = id; |
| 384 | } |
| 385 | if (pke_len) { |
| 386 | *pke_len = ret; |
| 387 | } |
| 388 | |
| 389 | parsed += ret; |
| 390 | id += ret; |
| 391 | |
| 392 | while (isspace(id[0])) { |
| 393 | ++parsed; |
| 394 | ++id; |
| 395 | } |
| 396 | |
| 397 | assert(id[0] == ']'); |
| 398 | |
| 399 | if (id[1] == '[') { |
| 400 | *has_predicate = 1; |
| 401 | } |
| 402 | |
| 403 | return parsed+1; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 408 | * the ".." and the first node-identifier, other calls parse a single |
| 409 | * node-identifier each. |
| 410 | * |
| 411 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 412 | * rel-path-keyexpr |
| 413 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 414 | * *(node-identifier *WSP "/" *WSP) |
| 415 | * node-identifier |
| 416 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 417 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 418 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 419 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 420 | * @param[out] name Points to the node name. |
| 421 | * @param[out] nam_len Length of the node name. |
| 422 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 423 | * must not be changed between consecutive calls. |
| 424 | * @return Number of characters successfully parsed, |
| 425 | * positive on success, negative on failure. |
| 426 | */ |
| 427 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 428 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 429 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 430 | { |
| 431 | int parsed = 0, ret, par_times = 0; |
| 432 | |
| 433 | assert(id); |
| 434 | assert(parent_times); |
| 435 | if (prefix) { |
| 436 | *prefix = NULL; |
| 437 | } |
| 438 | if (pref_len) { |
| 439 | *pref_len = 0; |
| 440 | } |
| 441 | if (name) { |
| 442 | *name = NULL; |
| 443 | } |
| 444 | if (nam_len) { |
| 445 | *nam_len = 0; |
| 446 | } |
| 447 | |
| 448 | if (!*parent_times) { |
| 449 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 450 | if (strncmp(id, "current()", 9)) { |
| 451 | return -parsed; |
| 452 | } |
| 453 | |
| 454 | parsed += 9; |
| 455 | id += 9; |
| 456 | |
| 457 | while (isspace(id[0])) { |
| 458 | ++parsed; |
| 459 | ++id; |
| 460 | } |
| 461 | |
| 462 | if (id[0] != '/') { |
| 463 | return -parsed; |
| 464 | } |
| 465 | |
| 466 | ++parsed; |
| 467 | ++id; |
| 468 | |
| 469 | while (isspace(id[0])) { |
| 470 | ++parsed; |
| 471 | ++id; |
| 472 | } |
| 473 | |
| 474 | /* rel-path-keyexpr */ |
| 475 | if (strncmp(id, "..", 2)) { |
| 476 | return -parsed; |
| 477 | } |
| 478 | ++par_times; |
| 479 | |
| 480 | parsed += 2; |
| 481 | id += 2; |
| 482 | |
| 483 | while (isspace(id[0])) { |
| 484 | ++parsed; |
| 485 | ++id; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 490 | * |
| 491 | * first parent reference with whitespaces already parsed |
| 492 | */ |
| 493 | if (id[0] != '/') { |
| 494 | return -parsed; |
| 495 | } |
| 496 | |
| 497 | ++parsed; |
| 498 | ++id; |
| 499 | |
| 500 | while (isspace(id[0])) { |
| 501 | ++parsed; |
| 502 | ++id; |
| 503 | } |
| 504 | |
| 505 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 506 | ++par_times; |
| 507 | |
| 508 | parsed += 2; |
| 509 | id += 2; |
| 510 | |
| 511 | while (isspace(id[0])) { |
| 512 | ++parsed; |
| 513 | ++id; |
| 514 | } |
| 515 | |
| 516 | if (id[0] != '/') { |
| 517 | return -parsed; |
| 518 | } |
| 519 | |
| 520 | ++parsed; |
| 521 | ++id; |
| 522 | |
| 523 | while (isspace(id[0])) { |
| 524 | ++parsed; |
| 525 | ++id; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (!*parent_times) { |
| 530 | *parent_times = par_times; |
| 531 | } |
| 532 | |
| 533 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 534 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 535 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | parsed += ret; |
| 539 | id += ret; |
| 540 | |
| 541 | return parsed; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * @brief Parse path-arg (leafref). |
| 546 | * |
| 547 | * path-arg = absolute-path / relative-path |
| 548 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 549 | * relative-path = 1*(".." "/") descendant-path |
| 550 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 551 | * @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] | 552 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 553 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 554 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 555 | * @param[out] name Points to the node name. |
| 556 | * @param[out] nam_len Length of the node name. |
| 557 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 558 | * must not be changed between consecutive calls. -1 if the |
| 559 | * path is relative. |
| 560 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 561 | * |
| 562 | * @return Number of characters successfully parsed, |
| 563 | * positive on success, negative on failure. |
| 564 | */ |
| 565 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 566 | 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] | 567 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 568 | { |
| 569 | int parsed = 0, ret, par_times = 0; |
| 570 | |
| 571 | assert(id); |
| 572 | assert(parent_times); |
| 573 | if (prefix) { |
| 574 | *prefix = NULL; |
| 575 | } |
| 576 | if (pref_len) { |
| 577 | *pref_len = 0; |
| 578 | } |
| 579 | if (name) { |
| 580 | *name = NULL; |
| 581 | } |
| 582 | if (nam_len) { |
| 583 | *nam_len = 0; |
| 584 | } |
| 585 | if (has_predicate) { |
| 586 | *has_predicate = 0; |
| 587 | } |
| 588 | |
| 589 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 590 | ++par_times; |
| 591 | |
| 592 | parsed += 2; |
| 593 | id += 2; |
| 594 | |
| 595 | while (!strncmp(id, "/..", 3)) { |
| 596 | ++par_times; |
| 597 | |
| 598 | parsed += 3; |
| 599 | id += 3; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!*parent_times) { |
| 604 | if (par_times) { |
| 605 | *parent_times = par_times; |
| 606 | } else { |
| 607 | *parent_times = -1; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (id[0] != '/') { |
| 612 | return -parsed; |
| 613 | } |
| 614 | |
| 615 | /* skip '/' */ |
| 616 | ++parsed; |
| 617 | ++id; |
| 618 | |
| 619 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 620 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 621 | return -parsed - ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 622 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 623 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 624 | /* actually we always need prefix even it is not specified */ |
| 625 | *prefix = lys_main_module(mod)->name; |
| 626 | *pref_len = strlen(*prefix); |
| 627 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 628 | |
| 629 | parsed += ret; |
| 630 | id += ret; |
| 631 | |
| 632 | /* there is no predicate */ |
| 633 | if ((id[0] == '/') || !id[0]) { |
| 634 | return parsed; |
| 635 | } else if (id[0] != '[') { |
| 636 | return -parsed; |
| 637 | } |
| 638 | |
| 639 | if (has_predicate) { |
| 640 | *has_predicate = 1; |
| 641 | } |
| 642 | |
| 643 | return parsed; |
| 644 | } |
| 645 | |
| 646 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 647 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 648 | * (which are mandatory for every node-identifier) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 649 | * |
| 650 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 651 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 652 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 653 | * @param[out] model Points to the model name. |
| 654 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 655 | * @param[out] name Points to the node name. |
| 656 | * @param[out] nam_len Length of the node name. |
| 657 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 658 | * |
| 659 | * @return Number of characters successfully parsed, |
| 660 | * positive on success, negative on failure. |
| 661 | */ |
| 662 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 663 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 664 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 665 | { |
| 666 | int parsed = 0, ret; |
| 667 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 668 | if (has_predicate) { |
| 669 | *has_predicate = 0; |
| 670 | } |
| 671 | |
| 672 | if (id[0] != '/') { |
| 673 | return -parsed; |
| 674 | } |
| 675 | |
| 676 | ++parsed; |
| 677 | ++id; |
| 678 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 679 | if ((ret = parse_identifier(id)) < 1) { |
| 680 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 683 | *model = id; |
| 684 | *mod_len = ret; |
| 685 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 686 | parsed += ret; |
| 687 | id += ret; |
| 688 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 689 | if (id[0] != ':') { |
| 690 | return -parsed; |
| 691 | } |
| 692 | |
| 693 | ++parsed; |
| 694 | ++id; |
| 695 | |
| 696 | if ((ret = parse_identifier(id)) < 1) { |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | *name = id; |
| 701 | *nam_len = ret; |
| 702 | |
| 703 | parsed += ret; |
| 704 | id += ret; |
| 705 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 706 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 707 | *has_predicate = 1; |
| 708 | } |
| 709 | |
| 710 | return parsed; |
| 711 | } |
| 712 | |
| 713 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 714 | * @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] | 715 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 716 | * |
| 717 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 718 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 719 | * ((DQUOTE string DQUOTE) / |
| 720 | * (SQUOTE string SQUOTE)) |
| 721 | * pos = non-negative-integer-value |
| 722 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 723 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 724 | * @param[out] model Points to the model name. |
| 725 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 726 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 727 | * @param[out] nam_len Length of the node name. |
| 728 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 729 | * NULL if there is not any. |
| 730 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 731 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 732 | * |
| 733 | * @return Number of characters successfully parsed, |
| 734 | * positive on success, negative on failure. |
| 735 | */ |
| 736 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 737 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 738 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 739 | { |
| 740 | const char *ptr; |
| 741 | int parsed = 0, ret; |
| 742 | char quote; |
| 743 | |
| 744 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 745 | if (model) { |
| 746 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 747 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 748 | if (mod_len) { |
| 749 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 750 | } |
| 751 | if (name) { |
| 752 | *name = NULL; |
| 753 | } |
| 754 | if (nam_len) { |
| 755 | *nam_len = 0; |
| 756 | } |
| 757 | if (value) { |
| 758 | *value = NULL; |
| 759 | } |
| 760 | if (val_len) { |
| 761 | *val_len = 0; |
| 762 | } |
| 763 | if (has_predicate) { |
| 764 | *has_predicate = 0; |
| 765 | } |
| 766 | |
| 767 | if (id[0] != '[') { |
| 768 | return -parsed; |
| 769 | } |
| 770 | |
| 771 | ++parsed; |
| 772 | ++id; |
| 773 | |
| 774 | while (isspace(id[0])) { |
| 775 | ++parsed; |
| 776 | ++id; |
| 777 | } |
| 778 | |
| 779 | /* pos */ |
| 780 | if (isdigit(id[0])) { |
| 781 | if (name) { |
| 782 | *name = id; |
| 783 | } |
| 784 | |
| 785 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 786 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | while (isdigit(id[0])) { |
| 790 | ++parsed; |
| 791 | ++id; |
| 792 | } |
| 793 | |
| 794 | if (nam_len) { |
| 795 | *nam_len = id-(*name); |
| 796 | } |
| 797 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 798 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 799 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 800 | if (id[0] == '.') { |
| 801 | if (name) { |
| 802 | *name = id; |
| 803 | } |
| 804 | if (nam_len) { |
| 805 | *nam_len = 1; |
| 806 | } |
| 807 | |
| 808 | ++parsed; |
| 809 | ++id; |
| 810 | |
| 811 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 812 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 813 | return -parsed+ret; |
| 814 | } else if (model && !*model) { |
| 815 | return -parsed; |
| 816 | } |
| 817 | |
| 818 | parsed += ret; |
| 819 | id += ret; |
| 820 | } |
| 821 | |
| 822 | while (isspace(id[0])) { |
| 823 | ++parsed; |
| 824 | ++id; |
| 825 | } |
| 826 | |
| 827 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 828 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 829 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 830 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 831 | ++parsed; |
| 832 | ++id; |
| 833 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 834 | while (isspace(id[0])) { |
| 835 | ++parsed; |
| 836 | ++id; |
| 837 | } |
| 838 | |
| 839 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 840 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 841 | quote = id[0]; |
| 842 | |
| 843 | ++parsed; |
| 844 | ++id; |
| 845 | |
| 846 | if ((ptr = strchr(id, quote)) == NULL) { |
| 847 | return -parsed; |
| 848 | } |
| 849 | ret = ptr-id; |
| 850 | |
| 851 | if (value) { |
| 852 | *value = id; |
| 853 | } |
| 854 | if (val_len) { |
| 855 | *val_len = ret; |
| 856 | } |
| 857 | |
| 858 | parsed += ret+1; |
| 859 | id += ret+1; |
| 860 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 861 | return -parsed; |
| 862 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | while (isspace(id[0])) { |
| 866 | ++parsed; |
| 867 | ++id; |
| 868 | } |
| 869 | |
| 870 | if (id[0] != ']') { |
| 871 | return -parsed; |
| 872 | } |
| 873 | |
| 874 | ++parsed; |
| 875 | ++id; |
| 876 | |
| 877 | if ((id[0] == '[') && has_predicate) { |
| 878 | *has_predicate = 1; |
| 879 | } |
| 880 | |
| 881 | return parsed; |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * @brief Parse schema-nodeid. |
| 886 | * |
| 887 | * schema-nodeid = absolute-schema-nodeid / |
| 888 | * descendant-schema-nodeid |
| 889 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 890 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 891 | * node-identifier |
| 892 | * absolute-schema-nodeid |
| 893 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 894 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 895 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 896 | * @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] | 897 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 898 | * @param[out] nam_len Length of the node name. |
| 899 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 900 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 901 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 902 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 903 | * @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] | 904 | * |
| 905 | * @return Number of characters successfully parsed, |
| 906 | * positive on success, negative on failure. |
| 907 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 908 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 909 | 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^] | 910 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 911 | { |
| 912 | int parsed = 0, ret; |
| 913 | |
| 914 | assert(id); |
| 915 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 916 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 917 | if (has_predicate) { |
| 918 | *has_predicate = 0; |
| 919 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 920 | |
| 921 | if (id[0] != '/') { |
| 922 | if (*is_relative != -1) { |
| 923 | return -parsed; |
| 924 | } else { |
| 925 | *is_relative = 1; |
| 926 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 927 | if (!strncmp(id, "./", 2)) { |
| 928 | parsed += 2; |
| 929 | id += 2; |
| 930 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 931 | } else { |
| 932 | if (*is_relative == -1) { |
| 933 | *is_relative = 0; |
| 934 | } |
| 935 | ++parsed; |
| 936 | ++id; |
| 937 | } |
| 938 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 939 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 940 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 943 | parsed += ret; |
| 944 | id += ret; |
| 945 | |
| 946 | if ((id[0] == '[') && has_predicate) { |
| 947 | *has_predicate = 1; |
| 948 | } |
| 949 | |
| 950 | return parsed; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * @brief Parse schema predicate (special format internally used). |
| 955 | * |
| 956 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 957 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 958 | * key-with-value = identifier *WSP "=" *WSP |
| 959 | * ((DQUOTE string DQUOTE) / |
| 960 | * (SQUOTE string SQUOTE)) |
| 961 | * |
| 962 | * @param[in] id Identifier to use. |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 963 | * @param[out] mod_name Points to the list key module name. |
| 964 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 965 | * @param[out] name Points to the list key name. |
| 966 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 967 | * @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] | 968 | * @param[out] val_len Length of \p value. |
| 969 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 970 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 971 | int |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 972 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 973 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 974 | { |
| 975 | const char *ptr; |
| 976 | int parsed = 0, ret; |
| 977 | char quote; |
| 978 | |
| 979 | assert(id); |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 980 | if (mod_name) { |
| 981 | *mod_name = NULL; |
| 982 | } |
| 983 | if (mod_name_len) { |
| 984 | *mod_name_len = 0; |
| 985 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 986 | if (name) { |
| 987 | *name = NULL; |
| 988 | } |
| 989 | if (nam_len) { |
| 990 | *nam_len = 0; |
| 991 | } |
| 992 | if (value) { |
| 993 | *value = NULL; |
| 994 | } |
| 995 | if (val_len) { |
| 996 | *val_len = 0; |
| 997 | } |
| 998 | if (has_predicate) { |
| 999 | *has_predicate = 0; |
| 1000 | } |
| 1001 | |
| 1002 | if (id[0] != '[') { |
| 1003 | return -parsed; |
| 1004 | } |
| 1005 | |
| 1006 | ++parsed; |
| 1007 | ++id; |
| 1008 | |
| 1009 | while (isspace(id[0])) { |
| 1010 | ++parsed; |
| 1011 | ++id; |
| 1012 | } |
| 1013 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1014 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1015 | if (id[0] == '.') { |
| 1016 | ret = 1; |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1017 | |
| 1018 | if (name) { |
| 1019 | *name = id; |
| 1020 | } |
| 1021 | if (nam_len) { |
| 1022 | *nam_len = ret; |
| 1023 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1024 | } else if (isdigit(id[0])) { |
| 1025 | if (id[0] == '0') { |
| 1026 | return -parsed; |
| 1027 | } |
| 1028 | ret = 1; |
| 1029 | while (isdigit(id[ret])) { |
| 1030 | ++ret; |
| 1031 | } |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1032 | |
| 1033 | if (name) { |
| 1034 | *name = id; |
| 1035 | } |
| 1036 | if (nam_len) { |
| 1037 | *nam_len = ret; |
| 1038 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1039 | } 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] | 1040 | return -parsed + ret; |
| 1041 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1042 | |
| 1043 | parsed += ret; |
| 1044 | id += ret; |
| 1045 | |
| 1046 | while (isspace(id[0])) { |
| 1047 | ++parsed; |
| 1048 | ++id; |
| 1049 | } |
| 1050 | |
| 1051 | /* there is value as well */ |
| 1052 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1053 | if (name && isdigit(**name)) { |
| 1054 | return -parsed; |
| 1055 | } |
| 1056 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1057 | ++parsed; |
| 1058 | ++id; |
| 1059 | |
| 1060 | while (isspace(id[0])) { |
| 1061 | ++parsed; |
| 1062 | ++id; |
| 1063 | } |
| 1064 | |
| 1065 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1066 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1067 | quote = id[0]; |
| 1068 | |
| 1069 | ++parsed; |
| 1070 | ++id; |
| 1071 | |
| 1072 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1073 | return -parsed; |
| 1074 | } |
| 1075 | ret = ptr - id; |
| 1076 | |
| 1077 | if (value) { |
| 1078 | *value = id; |
| 1079 | } |
| 1080 | if (val_len) { |
| 1081 | *val_len = ret; |
| 1082 | } |
| 1083 | |
| 1084 | parsed += ret + 1; |
| 1085 | id += ret + 1; |
| 1086 | } else { |
| 1087 | return -parsed; |
| 1088 | } |
| 1089 | |
| 1090 | while (isspace(id[0])) { |
| 1091 | ++parsed; |
| 1092 | ++id; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | if (id[0] != ']') { |
| 1097 | return -parsed; |
| 1098 | } |
| 1099 | |
| 1100 | ++parsed; |
| 1101 | ++id; |
| 1102 | |
| 1103 | if ((id[0] == '[') && has_predicate) { |
| 1104 | *has_predicate = 1; |
| 1105 | } |
| 1106 | |
| 1107 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1111 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1112 | * |
| 1113 | * @param[in] feat_name Feature name to resolve. |
| 1114 | * @param[in] len Length of \p feat_name. |
| 1115 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1116 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1117 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1118 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1119 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1120 | */ |
| 1121 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1122 | 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] | 1123 | { |
| 1124 | char *str; |
| 1125 | const char *mod_name, *name; |
| 1126 | int mod_name_len, nam_len, i, j; |
| 1127 | const struct lys_module *module; |
| 1128 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1129 | assert(feature); |
| 1130 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1131 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1132 | 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] | 1133 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
| 1134 | return -1; |
| 1135 | } |
| 1136 | |
| 1137 | module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len); |
| 1138 | if (!module) { |
| 1139 | /* identity refers unknown data model */ |
| 1140 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1144 | if (module != node->module && module == lys_node_module(node)) { |
| 1145 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1146 | for (j = 0; j < node->module->features_size; j++) { |
| 1147 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1148 | /* check status */ |
| 1149 | 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] | 1150 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1151 | return -1; |
| 1152 | } |
| 1153 | *feature = &node->module->features[j]; |
| 1154 | return 0; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1159 | /* search in the identified module ... */ |
| 1160 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1161 | 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] | 1162 | /* check status */ |
| 1163 | 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] | 1164 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1165 | return -1; |
| 1166 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1167 | *feature = &module->features[j]; |
| 1168 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1172 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1173 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1174 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1175 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1176 | /* check status */ |
| 1177 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1178 | module->inc[i].submodule->features[j].flags, |
| 1179 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1180 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1181 | return -1; |
| 1182 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1183 | *feature = &module->inc[i].submodule->features[j]; |
| 1184 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /* not found */ |
| 1190 | str = strndup(feat_name, len); |
| 1191 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
| 1192 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1193 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1196 | /* |
| 1197 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1198 | * - 1 if enabled |
| 1199 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1200 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1201 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1202 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1203 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1204 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1205 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1206 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1207 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1208 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1209 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1210 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1211 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1212 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1216 | 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] | 1217 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1218 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1219 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1220 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1221 | op = iff_getop(expr->expr, *index_e); |
| 1222 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1223 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1224 | switch (op) { |
| 1225 | case LYS_IFF_F: |
| 1226 | /* resolve feature */ |
| 1227 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1228 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1229 | /* invert result */ |
| 1230 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1231 | case LYS_IFF_AND: |
| 1232 | case LYS_IFF_OR: |
| 1233 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1234 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1235 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1236 | return a && b; |
| 1237 | } else { /* LYS_IFF_OR */ |
| 1238 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1242 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | int |
| 1246 | resolve_iffeature(struct lys_iffeature *expr) |
| 1247 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1248 | int index_e = 0, index_f = 0; |
| 1249 | |
| 1250 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1251 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1252 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1253 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | struct iff_stack { |
| 1257 | int size; |
| 1258 | int index; /* first empty item */ |
| 1259 | uint8_t *stack; |
| 1260 | }; |
| 1261 | |
| 1262 | static int |
| 1263 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1264 | { |
| 1265 | if (stack->index == stack->size) { |
| 1266 | stack->size += 4; |
| 1267 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1268 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | stack->stack[stack->index++] = value; |
| 1272 | return EXIT_SUCCESS; |
| 1273 | } |
| 1274 | |
| 1275 | static uint8_t |
| 1276 | iff_stack_pop(struct iff_stack *stack) |
| 1277 | { |
| 1278 | stack->index--; |
| 1279 | return stack->stack[stack->index]; |
| 1280 | } |
| 1281 | |
| 1282 | static void |
| 1283 | iff_stack_clean(struct iff_stack *stack) |
| 1284 | { |
| 1285 | stack->size = 0; |
| 1286 | free(stack->stack); |
| 1287 | } |
| 1288 | |
| 1289 | static void |
| 1290 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1291 | { |
| 1292 | uint8_t *item; |
| 1293 | uint8_t mask = 3; |
| 1294 | |
| 1295 | assert(pos >= 0); |
| 1296 | assert(op <= 3); /* max 2 bits */ |
| 1297 | |
| 1298 | item = &list[pos / 4]; |
| 1299 | mask = mask << 2 * (pos % 4); |
| 1300 | *item = (*item) & ~mask; |
| 1301 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1302 | } |
| 1303 | |
| 1304 | uint8_t |
| 1305 | iff_getop(uint8_t *list, int pos) |
| 1306 | { |
| 1307 | uint8_t *item; |
| 1308 | uint8_t mask = 3, result; |
| 1309 | |
| 1310 | assert(pos >= 0); |
| 1311 | |
| 1312 | item = &list[pos / 4]; |
| 1313 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1314 | return result >> 2 * (pos % 4); |
| 1315 | } |
| 1316 | |
| 1317 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1318 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1319 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1320 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1321 | struct unres_iffeat_data { |
| 1322 | struct lys_node *node; |
| 1323 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1324 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1325 | }; |
| 1326 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1327 | void |
| 1328 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1329 | { |
| 1330 | unsigned int e = 0, f = 0, r = 0; |
| 1331 | uint8_t op; |
| 1332 | |
| 1333 | assert(iffeat); |
| 1334 | |
| 1335 | if (!iffeat->expr) { |
| 1336 | goto result; |
| 1337 | } |
| 1338 | |
| 1339 | do { |
| 1340 | op = iff_getop(iffeat->expr, e++); |
| 1341 | switch (op) { |
| 1342 | case LYS_IFF_NOT: |
| 1343 | if (!r) { |
| 1344 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1345 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1346 | break; |
| 1347 | case LYS_IFF_AND: |
| 1348 | case LYS_IFF_OR: |
| 1349 | if (!r) { |
| 1350 | r += 2; |
| 1351 | } else { |
| 1352 | r += 1; |
| 1353 | } |
| 1354 | break; |
| 1355 | case LYS_IFF_F: |
| 1356 | f++; |
| 1357 | if (r) { |
| 1358 | r--; |
| 1359 | } |
| 1360 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1361 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1362 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1363 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1364 | result: |
| 1365 | if (expr_size) { |
| 1366 | *expr_size = e; |
| 1367 | } |
| 1368 | if (feat_size) { |
| 1369 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1374 | 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] | 1375 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1376 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1377 | const char *c = value; |
| 1378 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1379 | int i, j, last_not, checkversion = 0; |
| 1380 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1381 | uint8_t op; |
| 1382 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1383 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1384 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1385 | assert(c); |
| 1386 | |
| 1387 | if (isspace(c[0])) { |
| 1388 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
| 1389 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1392 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1393 | for (i = j = last_not = 0; c[i]; i++) { |
| 1394 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1395 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1396 | j++; |
| 1397 | continue; |
| 1398 | } else if (c[i] == ')') { |
| 1399 | j--; |
| 1400 | continue; |
| 1401 | } else if (isspace(c[i])) { |
| 1402 | continue; |
| 1403 | } |
| 1404 | |
| 1405 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1406 | if (c[i + r] == '\0') { |
Radek Krejci | a98da3f | 2016-07-27 14:05:22 +0200 | [diff] [blame] | 1407 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1408 | return EXIT_FAILURE; |
| 1409 | } else if (!isspace(c[i + r])) { |
| 1410 | /* feature name starting with the not/and/or */ |
| 1411 | last_not = 0; |
| 1412 | f_size++; |
| 1413 | } else if (c[i] == 'n') { /* not operation */ |
| 1414 | if (last_not) { |
| 1415 | /* double not */ |
| 1416 | expr_size = expr_size - 2; |
| 1417 | last_not = 0; |
| 1418 | } else { |
| 1419 | last_not = 1; |
| 1420 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1421 | } else { /* and, or */ |
| 1422 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1423 | /* not a not operation */ |
| 1424 | last_not = 0; |
| 1425 | } |
| 1426 | i += r; |
| 1427 | } else { |
| 1428 | f_size++; |
| 1429 | last_not = 0; |
| 1430 | } |
| 1431 | expr_size++; |
| 1432 | |
| 1433 | while (!isspace(c[i])) { |
| 1434 | if (!c[i] || c[i] == ')') { |
| 1435 | i--; |
| 1436 | break; |
| 1437 | } |
| 1438 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1439 | } |
| 1440 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1441 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1442 | /* not matching count of ( and ) */ |
| 1443 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1444 | return EXIT_FAILURE; |
| 1445 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1446 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1447 | if (checkversion || expr_size > 1) { |
| 1448 | /* check that we have 1.1 module */ |
| 1449 | if (node->module->version != 2) { |
| 1450 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1451 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
| 1452 | return EXIT_FAILURE; |
| 1453 | } |
| 1454 | } |
| 1455 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1456 | /* allocate the memory */ |
| 1457 | 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] | 1458 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1459 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1460 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error); |
| 1461 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1462 | f_size--; expr_size--; /* used as indexes from now */ |
| 1463 | |
| 1464 | for (i--; i >= 0; i--) { |
| 1465 | if (c[i] == ')') { |
| 1466 | /* push it on stack */ |
| 1467 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1468 | continue; |
| 1469 | } else if (c[i] == '(') { |
| 1470 | /* pop from the stack into result all operators until ) */ |
| 1471 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1472 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1473 | } |
| 1474 | continue; |
| 1475 | } else if (isspace(c[i])) { |
| 1476 | continue; |
| 1477 | } |
| 1478 | |
| 1479 | /* end operator or operand -> find beginning and get what is it */ |
| 1480 | j = i + 1; |
| 1481 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1482 | i--; |
| 1483 | } |
| 1484 | i++; /* get back by one step */ |
| 1485 | |
| 1486 | if (!strncmp(&c[i], "not ", 4)) { |
| 1487 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1488 | /* double not */ |
| 1489 | iff_stack_pop(&stack); |
| 1490 | } else { |
| 1491 | /* not has the highest priority, so do not pop from the stack |
| 1492 | * as in case of AND and OR */ |
| 1493 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1494 | } |
| 1495 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1496 | /* as for OR - pop from the stack all operators with the same or higher |
| 1497 | * priority and store them to the result, then push the AND to the stack */ |
| 1498 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1499 | op = iff_stack_pop(&stack); |
| 1500 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1501 | } |
| 1502 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1503 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1504 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1505 | op = iff_stack_pop(&stack); |
| 1506 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1507 | } |
| 1508 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1509 | } else { |
| 1510 | /* feature name, length is j - i */ |
| 1511 | |
| 1512 | /* add it to the result */ |
| 1513 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1514 | |
| 1515 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1516 | * forward referenced, we have to keep the feature name in auxiliary |
| 1517 | * structure passed into unres */ |
| 1518 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1519 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1520 | iff_data->node = node; |
| 1521 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1522 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1523 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1524 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1525 | f_size--; |
| 1526 | |
| 1527 | if (r == -1) { |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1528 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1529 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1530 | } |
| 1531 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1532 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1533 | while (stack.index) { |
| 1534 | op = iff_stack_pop(&stack); |
| 1535 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1536 | } |
| 1537 | |
| 1538 | if (++expr_size || ++f_size) { |
| 1539 | /* not all expected operators and operands found */ |
| 1540 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1541 | rc = EXIT_FAILURE; |
| 1542 | } else { |
| 1543 | rc = EXIT_SUCCESS; |
| 1544 | } |
| 1545 | |
| 1546 | error: |
| 1547 | /* cleanup */ |
| 1548 | iff_stack_clean(&stack); |
| 1549 | |
| 1550 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1554 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1555 | * |
| 1556 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1557 | * module). |
| 1558 | * |
| 1559 | */ |
| 1560 | struct lyd_node * |
| 1561 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1562 | { |
| 1563 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1564 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1565 | const struct lys_node *schema = NULL; |
| 1566 | |
| 1567 | assert(nodeid && start); |
| 1568 | |
| 1569 | if (nodeid[0] == '/') { |
| 1570 | return NULL; |
| 1571 | } |
| 1572 | |
| 1573 | str = p = strdup(nodeid); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1574 | LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1575 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1576 | while (p) { |
| 1577 | token = p; |
| 1578 | p = strchr(p, '/'); |
| 1579 | if (p) { |
| 1580 | *p = '\0'; |
| 1581 | p++; |
| 1582 | } |
| 1583 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1584 | if (p) { |
| 1585 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1586 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1587 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1588 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1589 | result = NULL; |
| 1590 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1591 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1592 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1593 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1594 | continue; |
| 1595 | } |
| 1596 | } else { |
| 1597 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1598 | 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] | 1599 | || !schema) { |
| 1600 | result = NULL; |
| 1601 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1602 | } |
| 1603 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1604 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1605 | if (iter->schema == schema) { |
| 1606 | /* move in data tree according to returned schema */ |
| 1607 | result = iter; |
| 1608 | break; |
| 1609 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1610 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1611 | if (!iter) { |
| 1612 | /* instance not found */ |
| 1613 | result = NULL; |
| 1614 | break; |
| 1615 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1616 | } |
| 1617 | free(str); |
| 1618 | |
| 1619 | return result; |
| 1620 | } |
| 1621 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1622 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1623 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1624 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1625 | { |
| 1626 | const struct lys_module *prefix_mod; |
| 1627 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1628 | /* name check */ |
| 1629 | if ((name[0] != '*') && (name[0] != '.') && (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len])) { |
| 1630 | return 1; |
| 1631 | } |
| 1632 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1633 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1634 | if (mod_name) { |
| 1635 | prefix_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len); |
| 1636 | if (!prefix_mod) { |
| 1637 | return -1; |
| 1638 | } |
| 1639 | } else { |
| 1640 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1641 | } |
| 1642 | if (prefix_mod != lys_node_module(sibling)) { |
| 1643 | return 1; |
| 1644 | } |
| 1645 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1646 | /* match */ |
| 1647 | switch (name[0]) { |
| 1648 | case '*': |
| 1649 | return 2; |
| 1650 | case '.': |
| 1651 | return 3; |
| 1652 | default: |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1657 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1658 | static int |
| 1659 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1660 | const struct lys_module *cur_module, int *nodeid_end) |
| 1661 | { |
| 1662 | int mod_len, nam_len, has_predicate, r, i; |
| 1663 | const char *model, *name; |
| 1664 | struct lys_node_list *list; |
| 1665 | |
| 1666 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1667 | return 1; |
| 1668 | } |
| 1669 | |
| 1670 | list = (struct lys_node_list *)node; |
| 1671 | do { |
| 1672 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1673 | if (r < 1) { |
| 1674 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]); |
| 1675 | return -1; |
| 1676 | } |
| 1677 | nodeid += r; |
| 1678 | |
| 1679 | if (node->nodetype == LYS_LEAFLIST) { |
| 1680 | /* just check syntax */ |
| 1681 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1682 | return 1; |
| 1683 | } |
| 1684 | break; |
| 1685 | } else { |
| 1686 | /* check the key */ |
| 1687 | for (i = 0; i < list->keys_size; ++i) { |
| 1688 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1689 | continue; |
| 1690 | } |
| 1691 | if (model) { |
| 1692 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1693 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1694 | continue; |
| 1695 | } |
| 1696 | } else { |
| 1697 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1698 | continue; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /* match */ |
| 1703 | break; |
| 1704 | } |
| 1705 | |
| 1706 | if (i == list->keys_size) { |
| 1707 | return 1; |
| 1708 | } |
| 1709 | } |
| 1710 | } while (has_predicate); |
| 1711 | |
| 1712 | if (!nodeid[0]) { |
| 1713 | *nodeid_end = 1; |
| 1714 | } |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | /* start - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1719 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1720 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1721 | resolve_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *cur_module, |
| 1722 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1723 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1724 | const char *name, *mod_name, *id; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1725 | const struct lys_node *sibling, *start_parent, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1726 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1727 | 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] | 1728 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1729 | const struct lys_module *start_mod, *aux_mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1730 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1731 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1732 | assert(nodeid && (start || cur_module) && ret); |
| 1733 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1734 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1735 | if (!cur_module) { |
| 1736 | cur_module = lys_node_module(start); |
| 1737 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1738 | id = nodeid; |
| 1739 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1740 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1741 | (extended ? &all_desc : NULL), extended); |
| 1742 | if (r < 1) { |
| 1743 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1744 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1745 | } |
| 1746 | id += r; |
| 1747 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1748 | if (is_relative && !start) { |
| 1749 | LOGINT; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1750 | return -1; |
| 1751 | } |
| 1752 | |
| 1753 | /* descendant-schema-nodeid */ |
| 1754 | if (is_relative) { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1755 | cur_module = start_mod = start->module; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1756 | start_parent = lys_parent(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1757 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1758 | /* absolute-schema-nodeid */ |
| 1759 | } else { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1760 | 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] | 1761 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1762 | str = strndup(mod_name, mod_name_len); |
| 1763 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1764 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1765 | return -1; |
| 1766 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1767 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | while (1) { |
| 1771 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1772 | last_aug = NULL; |
| 1773 | |
| 1774 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1775 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1776 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1777 | /* we are getting into another module (augment) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 1778 | 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] | 1779 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1780 | str = strndup(mod_name, mod_name_len); |
| 1781 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1782 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1783 | return -1; |
| 1784 | } |
| 1785 | } else { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1786 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1787 | * because this module may be not implemented and it augments something in another module and |
| 1788 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1789 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | /* if the module is implemented, all the augments will be connected */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1793 | if (!aux_mod->implemented && !extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1794 | get_next_augment: |
| 1795 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | 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] | 1800 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1801 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 1802 | |
| 1803 | /* resolve predicate */ |
| 1804 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 1805 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 1806 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1807 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1808 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1809 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1810 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1811 | } else if (!id[0]) { |
| 1812 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1813 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1814 | |
| 1815 | if (r == 0) { |
| 1816 | /* one matching result */ |
| 1817 | if (nodeid_end) { |
| 1818 | *ret = ly_set_new(); |
| 1819 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1820 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1821 | } else { |
| 1822 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1823 | return -1; |
| 1824 | } |
| 1825 | start_parent = sibling; |
| 1826 | } |
| 1827 | break; |
| 1828 | } else if (r == 1) { |
| 1829 | continue; |
| 1830 | } else if (r == 2) { |
| 1831 | /* "*" */ |
| 1832 | if (!*ret) { |
| 1833 | *ret = ly_set_new(); |
| 1834 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1835 | } |
| 1836 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1837 | if (all_desc) { |
| 1838 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1839 | if (elem != sibling) { |
| 1840 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1841 | } |
| 1842 | |
| 1843 | LY_TREE_DFS_END(sibling, next, elem); |
| 1844 | } |
| 1845 | } |
| 1846 | } else if (r == 3) { |
| 1847 | /* "." */ |
| 1848 | if (!*ret) { |
| 1849 | *ret = ly_set_new(); |
| 1850 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1); |
| 1851 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 1852 | } |
| 1853 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 1854 | if (all_desc) { |
| 1855 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 1856 | if (elem != sibling) { |
| 1857 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 1858 | } |
| 1859 | |
| 1860 | LY_TREE_DFS_END(sibling, next, elem); |
| 1861 | } |
| 1862 | } |
| 1863 | } else { |
| 1864 | LOGINT; |
| 1865 | return -1; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* skip predicate */ |
| 1870 | if (extended && has_predicate) { |
| 1871 | while (id[0] == '[') { |
| 1872 | id = strchr(id, ']'); |
| 1873 | if (!id) { |
| 1874 | LOGINT; |
| 1875 | return -1; |
| 1876 | } |
| 1877 | ++id; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 1882 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* no match */ |
| 1886 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1887 | if (last_aug) { |
| 1888 | /* it still could be in another augment */ |
| 1889 | goto get_next_augment; |
| 1890 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1891 | if (no_node_error) { |
| 1892 | str = strndup(nodeid, (name - nodeid) + nam_len); |
| 1893 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1894 | free(str); |
| 1895 | return -1; |
| 1896 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1897 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1898 | return EXIT_SUCCESS; |
| 1899 | } |
| 1900 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1901 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1902 | (extended ? &all_desc : NULL), extended); |
| 1903 | if (r < 1) { |
| 1904 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1905 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1906 | } |
| 1907 | id += r; |
| 1908 | } |
| 1909 | |
| 1910 | /* cannot get here */ |
| 1911 | LOGINT; |
| 1912 | return -1; |
| 1913 | } |
| 1914 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1915 | /* unique, refine, |
| 1916 | * >0 - unexpected char on position (ret - 1), |
| 1917 | * 0 - ok (but ret can still be NULL), |
| 1918 | * -1 - error, |
| 1919 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1920 | int |
| 1921 | 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] | 1922 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1923 | { |
| 1924 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1925 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1926 | int r, nam_len, mod_name_len, is_relative = -1; |
| 1927 | /* 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] | 1928 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1929 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1930 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 1931 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1932 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 1933 | if (!start) { |
| 1934 | /* leaf not found */ |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1938 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1939 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1940 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1941 | 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] | 1942 | return ((id - nodeid) - r) + 1; |
| 1943 | } |
| 1944 | id += r; |
| 1945 | |
| 1946 | if (!is_relative) { |
| 1947 | return -1; |
| 1948 | } |
| 1949 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1950 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1951 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1952 | start_parent = lys_parent(start_parent); |
| 1953 | } |
| 1954 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1955 | while (1) { |
| 1956 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1957 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 1958 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1959 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 1960 | if (r == 0) { |
| 1961 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1962 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1963 | /* wrong node type, too bad */ |
| 1964 | continue; |
| 1965 | } |
| 1966 | *ret = sibling; |
| 1967 | return EXIT_SUCCESS; |
| 1968 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1969 | start_parent = sibling; |
| 1970 | break; |
| 1971 | } else if (r == 1) { |
| 1972 | continue; |
| 1973 | } else { |
| 1974 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | /* no match */ |
| 1979 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1980 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1981 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1982 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1983 | *ret = NULL; |
| 1984 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 1987 | 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] | 1988 | return ((id - nodeid) - r) + 1; |
| 1989 | } |
| 1990 | id += r; |
| 1991 | } |
| 1992 | |
| 1993 | /* cannot get here */ |
| 1994 | LOGINT; |
| 1995 | return -1; |
| 1996 | } |
| 1997 | |
| 1998 | /* choice default */ |
| 1999 | int |
| 2000 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2001 | { |
| 2002 | /* cannot actually be a path */ |
| 2003 | if (strchr(nodeid, '/')) { |
| 2004 | return -1; |
| 2005 | } |
| 2006 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2007 | 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] | 2008 | } |
| 2009 | |
| 2010 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2011 | static int |
| 2012 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2013 | { |
| 2014 | const struct lys_module *module; |
| 2015 | const char *mod_prefix, *name; |
| 2016 | int i, mod_prefix_len, nam_len; |
| 2017 | |
| 2018 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2019 | 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] | 2020 | return -i + 1; |
| 2021 | } |
| 2022 | |
| 2023 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 2024 | if (!module) { |
| 2025 | return -1; |
| 2026 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2027 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2028 | start = module->data; |
| 2029 | } |
| 2030 | |
| 2031 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2032 | |
| 2033 | return EXIT_SUCCESS; |
| 2034 | } |
| 2035 | |
| 2036 | int |
| 2037 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2038 | const struct lys_node **ret) |
| 2039 | { |
| 2040 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2041 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2042 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2043 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2044 | |
| 2045 | assert(nodeid && module && ret); |
| 2046 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2047 | |
| 2048 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2049 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2050 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2051 | 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] | 2052 | return ((id - nodeid) - r) + 1; |
| 2053 | } |
| 2054 | id += r; |
| 2055 | |
| 2056 | if (is_relative) { |
| 2057 | return -1; |
| 2058 | } |
| 2059 | |
| 2060 | 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] | 2061 | if (!abs_start_mod) { |
| 2062 | return -1; |
| 2063 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2064 | |
| 2065 | while (1) { |
| 2066 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2067 | 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] | 2068 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2069 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2070 | if (r == 0) { |
| 2071 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2072 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2073 | /* wrong node type, too bad */ |
| 2074 | continue; |
| 2075 | } |
| 2076 | *ret = sibling; |
| 2077 | return EXIT_SUCCESS; |
| 2078 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2079 | start_parent = sibling; |
| 2080 | break; |
| 2081 | } else if (r == 1) { |
| 2082 | continue; |
| 2083 | } else { |
| 2084 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /* no match */ |
| 2089 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2090 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2091 | return EXIT_SUCCESS; |
| 2092 | } |
| 2093 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2094 | 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] | 2095 | return ((id - nodeid) - r) + 1; |
| 2096 | } |
| 2097 | id += r; |
| 2098 | } |
| 2099 | |
| 2100 | /* cannot get here */ |
| 2101 | LOGINT; |
| 2102 | return -1; |
| 2103 | } |
| 2104 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2105 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2106 | 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] | 2107 | { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2108 | const char *mod_name, *name; |
| 2109 | int mod_name_len, nam_len, has_predicate, i; |
| 2110 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2111 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2112 | 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] | 2113 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2114 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2115 | return -1; |
| 2116 | } |
| 2117 | |
| 2118 | predicate += i; |
| 2119 | *parsed += i; |
| 2120 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2121 | if (!isdigit(name[0])) { |
| 2122 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2123 | key = (struct lys_node *)list->keys[i]; |
| 2124 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2125 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2126 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2127 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2128 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2129 | if (i == list->keys_size) { |
| 2130 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2131 | return -1; |
| 2132 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* more predicates? */ |
| 2136 | if (has_predicate) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2137 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2143 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2144 | const struct lys_node * |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2145 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2146 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2147 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2148 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2149 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2150 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2151 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2152 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2153 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2154 | assert(nodeid && (ctx || start)); |
| 2155 | if (!ctx) { |
| 2156 | ctx = start->module->ctx; |
| 2157 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2158 | |
| 2159 | id = nodeid; |
| 2160 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2161 | 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] | 2162 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2163 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2164 | } |
| 2165 | id += r; |
| 2166 | |
| 2167 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2168 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2169 | start_parent = start; |
| 2170 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2171 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2172 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2173 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2174 | } else { |
| 2175 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2176 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2177 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 2178 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2179 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2180 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 2181 | LOGINT; |
| 2182 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2185 | if (ly_buf_used && module_name[0]) { |
| 2186 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2187 | } |
| 2188 | ly_buf_used++; |
| 2189 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2190 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2191 | module_name[mod_name_len] = '\0'; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2192 | module = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2193 | |
| 2194 | if (buf_backup) { |
| 2195 | /* return previous internal buffer content */ |
| 2196 | strcpy(module_name, buf_backup); |
| 2197 | free(buf_backup); |
| 2198 | buf_backup = NULL; |
| 2199 | } |
| 2200 | ly_buf_used--; |
| 2201 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2202 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2203 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2204 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2205 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2206 | return NULL; |
| 2207 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2208 | start_parent = NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2209 | |
| 2210 | /* now it's as if there was no module name */ |
| 2211 | mod_name = NULL; |
| 2212 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2213 | } |
| 2214 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2215 | prev_mod = module; |
| 2216 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2217 | while (1) { |
| 2218 | sibling = NULL; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2219 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2220 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2221 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2222 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2223 | /* module check */ |
| 2224 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2225 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2226 | LOGINT; |
| 2227 | return NULL; |
| 2228 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2229 | |
| 2230 | if (ly_buf_used && module_name[0]) { |
| 2231 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2232 | } |
| 2233 | ly_buf_used++; |
| 2234 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2235 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2236 | module_name[mod_name_len] = '\0'; |
| 2237 | /* will also find an augment module */ |
| 2238 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2239 | |
| 2240 | if (buf_backup) { |
| 2241 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2242 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2243 | free(buf_backup); |
| 2244 | buf_backup = NULL; |
| 2245 | } |
| 2246 | ly_buf_used--; |
| 2247 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2248 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2249 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2250 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2251 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2252 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2253 | } |
| 2254 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2255 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2256 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2257 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2258 | continue; |
| 2259 | } |
| 2260 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2261 | /* do we have some predicates on it? */ |
| 2262 | if (has_predicate) { |
| 2263 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2264 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2265 | 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] | 2266 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2267 | return NULL; |
| 2268 | } |
| 2269 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2270 | 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] | 2271 | return NULL; |
| 2272 | } |
| 2273 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2274 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2275 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2276 | } |
| 2277 | id += r; |
| 2278 | } |
| 2279 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2280 | /* the result node? */ |
| 2281 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2282 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2285 | /* move down the tree, if possible */ |
| 2286 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2287 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2288 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2289 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2290 | start_parent = sibling; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2291 | |
| 2292 | /* update prev mod */ |
| 2293 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | /* no match */ |
| 2299 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2300 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 2301 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2302 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2303 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2306 | 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] | 2307 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2308 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2309 | } |
| 2310 | id += r; |
| 2311 | } |
| 2312 | |
| 2313 | /* cannot get here */ |
| 2314 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2315 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2318 | static int |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2319 | 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^] | 2320 | int position, int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2321 | { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2322 | const char *mod_name, *name, *value, *key_val; |
| 2323 | int mod_name_len, nam_len, val_len, has_predicate = 1, r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2324 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2325 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2326 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2327 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2328 | assert(node->schema->nodetype == LYS_LIST); |
| 2329 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2330 | /* is the predicate a number? */ |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2331 | 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] | 2332 | || !strncmp(name, ".", nam_len)) { |
| 2333 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 2334 | return -1; |
| 2335 | } |
| 2336 | |
| 2337 | if (isdigit(name[0])) { |
| 2338 | if (position == atoi(name)) { |
| 2339 | /* match */ |
| 2340 | *parsed += r; |
| 2341 | return 0; |
| 2342 | } else { |
| 2343 | /* not a match */ |
| 2344 | return 1; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | if (!((struct lys_node_list *)node->schema)->keys_size) { |
| 2349 | /* no keys in schema - causes an error later */ |
| 2350 | return 0; |
| 2351 | } |
| 2352 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2353 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2354 | if (!key) { |
| 2355 | /* it is not a position, so we need a key for it to be a match */ |
| 2356 | return 1; |
| 2357 | } |
| 2358 | |
| 2359 | /* go through all the keys */ |
| 2360 | i = 0; |
| 2361 | goto check_parsed_values; |
| 2362 | |
| 2363 | for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2364 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2365 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2366 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2369 | 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] | 2370 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2371 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2372 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2373 | } |
| 2374 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2375 | check_parsed_values: |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2376 | predicate += r; |
| 2377 | *parsed += r; |
| 2378 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2379 | 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] | 2380 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2381 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2384 | if (mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2385 | /* specific module, check that the found key is from that module */ |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2386 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len) |
| 2387 | || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) { |
| 2388 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2389 | return -1; |
| 2390 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2391 | |
| 2392 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2393 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
| 2394 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2395 | return -1; |
| 2396 | } |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2397 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2398 | /* no module, so it must be the same as the list (parent) */ |
| 2399 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2400 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
| 2401 | return -1; |
| 2402 | } |
| 2403 | } |
| 2404 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2405 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2406 | if ((key->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2407 | && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name)) |
| 2408 | && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2409 | key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1; |
| 2410 | } else { |
| 2411 | key_val = key->value_str; |
| 2412 | } |
| 2413 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2414 | /* value does not match */ |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2415 | if (strncmp(key_val, value, val_len) || key_val[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2416 | return 1; |
| 2417 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2418 | |
| 2419 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2423 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2424 | return -1; |
| 2425 | } |
| 2426 | |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2430 | /** |
| 2431 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2432 | * |
| 2433 | * @param[in] nodeid Node data path to find |
| 2434 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2435 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2436 | * @param[out] parsed Number of characters processed in \p id |
| 2437 | * @return The closes parent (or the node itself) from the path |
| 2438 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2439 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2440 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2441 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2442 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2443 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2444 | const char *id, *mod_name, *name, *pred_name, *data_val; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2445 | 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] | 2446 | 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] | 2447 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2448 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2449 | const struct lys_module *prefix_mod, *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2450 | struct ly_ctx *ctx; |
| 2451 | |
| 2452 | assert(nodeid && start && parsed); |
| 2453 | |
| 2454 | ctx = start->schema->module->ctx; |
| 2455 | id = nodeid; |
| 2456 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2457 | 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] | 2458 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2459 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2460 | return NULL; |
| 2461 | } |
| 2462 | id += r; |
| 2463 | /* add it to parsed only after the data node was actually found */ |
| 2464 | last_parsed = r; |
| 2465 | |
| 2466 | if (is_relative) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2467 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2468 | start = start->child; |
| 2469 | } else { |
| 2470 | for (; start->parent; start = start->parent); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2471 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | while (1) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2475 | list_instance_position = 0; |
| 2476 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2477 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 2478 | /* 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] | 2479 | if (lys_parent(sibling->schema)) { |
| 2480 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2481 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2482 | 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] | 2483 | *parsed = -1; |
| 2484 | return NULL; |
| 2485 | } |
| 2486 | } else { |
| 2487 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 2488 | 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] | 2489 | *parsed = -1; |
| 2490 | return NULL; |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2495 | /* name match */ |
| 2496 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 2497 | |
| 2498 | /* module check */ |
| 2499 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2500 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2501 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2502 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2503 | return NULL; |
| 2504 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2505 | |
| 2506 | if (ly_buf_used && module_name[0]) { |
| 2507 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 2508 | } |
| 2509 | ly_buf_used++; |
| 2510 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 2511 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2512 | module_name[mod_name_len] = '\0'; |
| 2513 | /* will also find an augment module */ |
| 2514 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2515 | |
| 2516 | if (buf_backup) { |
| 2517 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 2518 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2519 | free(buf_backup); |
| 2520 | buf_backup = NULL; |
| 2521 | } |
| 2522 | ly_buf_used--; |
| 2523 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2524 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2525 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 2526 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 2527 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2528 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2529 | return NULL; |
| 2530 | } |
| 2531 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2532 | prefix_mod = prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2533 | } |
Michal Vasko | 1adc724 | 2016-11-16 11:05:28 +0100 | [diff] [blame] | 2534 | if (prefix_mod != lyd_node_module(sibling)) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2535 | continue; |
| 2536 | } |
| 2537 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2538 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2539 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2540 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2541 | |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2542 | last_has_pred = 0; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2543 | if (has_predicate) { |
Michal Vasko | f359b02 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2544 | if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value, |
| 2545 | &llval_len, &last_has_pred)) < 1) { |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2546 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2547 | *parsed = -1; |
| 2548 | return NULL; |
| 2549 | } |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2550 | if ((pred_name[0] != '.') || (pred_name_len != 1)) { |
| 2551 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1); |
| 2552 | *parsed = -1; |
| 2553 | return NULL; |
| 2554 | } |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2555 | } else { |
| 2556 | r = 0; |
| 2557 | if (llist_value) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2558 | llval_len = strlen(llist_value); |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2562 | /* make value canonical */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2563 | if ((llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2564 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2565 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2566 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2567 | } else { |
| 2568 | data_val = llist->value_str; |
| 2569 | } |
| 2570 | |
| 2571 | if ((!llist_value && data_val && data_val[0]) |
| 2572 | || (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] | 2573 | continue; |
| 2574 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2575 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2576 | id += r; |
| 2577 | last_parsed += r; |
Michal Vasko | 0a8d17f | 2016-10-19 15:51:36 +0200 | [diff] [blame] | 2578 | has_predicate = last_has_pred; |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2579 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2580 | } else if (sibling->schema->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2581 | /* 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] | 2582 | if (!has_predicate) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2583 | /* none match */ |
| 2584 | return last_match; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2585 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2586 | |
| 2587 | ++list_instance_position; |
| 2588 | r = 0; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2589 | 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] | 2590 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2591 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2592 | return NULL; |
| 2593 | } else if (ret == 1) { |
| 2594 | /* this list instance does not match */ |
| 2595 | continue; |
| 2596 | } |
| 2597 | id += r; |
| 2598 | last_parsed += r; |
| 2599 | } |
| 2600 | |
| 2601 | *parsed += last_parsed; |
| 2602 | |
| 2603 | /* the result node? */ |
| 2604 | if (!id[0]) { |
| 2605 | return sibling; |
| 2606 | } |
| 2607 | |
| 2608 | /* move down the tree, if possible */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2609 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 2610 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2611 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2612 | return NULL; |
| 2613 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 2614 | last_match = sibling; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2615 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2616 | start = sibling->child; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2617 | break; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | /* no match, return last match */ |
| 2622 | if (!sibling) { |
| 2623 | return last_match; |
| 2624 | } |
| 2625 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 2626 | 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] | 2627 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2628 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2629 | return NULL; |
| 2630 | } |
| 2631 | id += r; |
| 2632 | last_parsed = r; |
| 2633 | } |
| 2634 | |
| 2635 | /* cannot get here */ |
| 2636 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2637 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2638 | return NULL; |
| 2639 | } |
| 2640 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2641 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2642 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2643 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2644 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2645 | * @param[in] str_restr Restriction as a string. |
| 2646 | * @param[in] type Type of the restriction. |
| 2647 | * @param[out] ret Final interval structure that starts with |
| 2648 | * the interval of the initial type, continues with intervals |
| 2649 | * of any superior types derived from the initial one, and |
| 2650 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2651 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2652 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2653 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2654 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2655 | 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] | 2656 | { |
| 2657 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2658 | int kind; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2659 | int64_t local_smin, local_smax, local_fmin, local_fmax; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2660 | uint64_t local_umin, local_umax; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2661 | uint8_t local_fdig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2662 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2663 | 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] | 2664 | |
| 2665 | switch (type->base) { |
| 2666 | case LY_TYPE_BINARY: |
| 2667 | kind = 0; |
| 2668 | local_umin = 0; |
| 2669 | local_umax = 18446744073709551615UL; |
| 2670 | |
| 2671 | if (!str_restr && type->info.binary.length) { |
| 2672 | str_restr = type->info.binary.length->expr; |
| 2673 | } |
| 2674 | break; |
| 2675 | case LY_TYPE_DEC64: |
| 2676 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2677 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2678 | local_fmax = __INT64_C(9223372036854775807); |
| 2679 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2680 | |
| 2681 | if (!str_restr && type->info.dec64.range) { |
| 2682 | str_restr = type->info.dec64.range->expr; |
| 2683 | } |
| 2684 | break; |
| 2685 | case LY_TYPE_INT8: |
| 2686 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2687 | local_smin = __INT64_C(-128); |
| 2688 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2689 | |
| 2690 | if (!str_restr && type->info.num.range) { |
| 2691 | str_restr = type->info.num.range->expr; |
| 2692 | } |
| 2693 | break; |
| 2694 | case LY_TYPE_INT16: |
| 2695 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2696 | local_smin = __INT64_C(-32768); |
| 2697 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2698 | |
| 2699 | if (!str_restr && type->info.num.range) { |
| 2700 | str_restr = type->info.num.range->expr; |
| 2701 | } |
| 2702 | break; |
| 2703 | case LY_TYPE_INT32: |
| 2704 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2705 | local_smin = __INT64_C(-2147483648); |
| 2706 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2707 | |
| 2708 | if (!str_restr && type->info.num.range) { |
| 2709 | str_restr = type->info.num.range->expr; |
| 2710 | } |
| 2711 | break; |
| 2712 | case LY_TYPE_INT64: |
| 2713 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2714 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2715 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2716 | |
| 2717 | if (!str_restr && type->info.num.range) { |
| 2718 | str_restr = type->info.num.range->expr; |
| 2719 | } |
| 2720 | break; |
| 2721 | case LY_TYPE_UINT8: |
| 2722 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2723 | local_umin = __UINT64_C(0); |
| 2724 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2725 | |
| 2726 | if (!str_restr && type->info.num.range) { |
| 2727 | str_restr = type->info.num.range->expr; |
| 2728 | } |
| 2729 | break; |
| 2730 | case LY_TYPE_UINT16: |
| 2731 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2732 | local_umin = __UINT64_C(0); |
| 2733 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2734 | |
| 2735 | if (!str_restr && type->info.num.range) { |
| 2736 | str_restr = type->info.num.range->expr; |
| 2737 | } |
| 2738 | break; |
| 2739 | case LY_TYPE_UINT32: |
| 2740 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2741 | local_umin = __UINT64_C(0); |
| 2742 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2743 | |
| 2744 | if (!str_restr && type->info.num.range) { |
| 2745 | str_restr = type->info.num.range->expr; |
| 2746 | } |
| 2747 | break; |
| 2748 | case LY_TYPE_UINT64: |
| 2749 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2750 | local_umin = __UINT64_C(0); |
| 2751 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2752 | |
| 2753 | if (!str_restr && type->info.num.range) { |
| 2754 | str_restr = type->info.num.range->expr; |
| 2755 | } |
| 2756 | break; |
| 2757 | case LY_TYPE_STRING: |
| 2758 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2759 | local_umin = __UINT64_C(0); |
| 2760 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2761 | |
| 2762 | if (!str_restr && type->info.str.length) { |
| 2763 | str_restr = type->info.str.length->expr; |
| 2764 | } |
| 2765 | break; |
| 2766 | default: |
| 2767 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2768 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2772 | if (type->der) { |
| 2773 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2774 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2775 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2776 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2777 | assert(!intv || (intv->kind == kind)); |
| 2778 | } |
| 2779 | |
| 2780 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2781 | /* we do not have any restriction, return superior ones */ |
| 2782 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2783 | return EXIT_SUCCESS; |
| 2784 | } |
| 2785 | |
| 2786 | /* adjust local min and max */ |
| 2787 | if (intv) { |
| 2788 | tmp_intv = intv; |
| 2789 | |
| 2790 | if (kind == 0) { |
| 2791 | local_umin = tmp_intv->value.uval.min; |
| 2792 | } else if (kind == 1) { |
| 2793 | local_smin = tmp_intv->value.sval.min; |
| 2794 | } else if (kind == 2) { |
| 2795 | local_fmin = tmp_intv->value.fval.min; |
| 2796 | } |
| 2797 | |
| 2798 | while (tmp_intv->next) { |
| 2799 | tmp_intv = tmp_intv->next; |
| 2800 | } |
| 2801 | |
| 2802 | if (kind == 0) { |
| 2803 | local_umax = tmp_intv->value.uval.max; |
| 2804 | } else if (kind == 1) { |
| 2805 | local_smax = tmp_intv->value.sval.max; |
| 2806 | } else if (kind == 2) { |
| 2807 | local_fmax = tmp_intv->value.fval.max; |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | /* finally parse our restriction */ |
| 2812 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2813 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2814 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2815 | if (!tmp_local_intv) { |
| 2816 | assert(!local_intv); |
| 2817 | local_intv = malloc(sizeof *local_intv); |
| 2818 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2819 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2820 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2821 | tmp_local_intv = tmp_local_intv->next; |
| 2822 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2823 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2824 | |
| 2825 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2826 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2827 | tmp_local_intv->next = NULL; |
| 2828 | |
| 2829 | /* min */ |
| 2830 | ptr = seg_ptr; |
| 2831 | while (isspace(ptr[0])) { |
| 2832 | ++ptr; |
| 2833 | } |
| 2834 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2835 | if (kind == 0) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2836 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2837 | } else if (kind == 1) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2838 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2839 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2840 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
| 2841 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2842 | goto error; |
| 2843 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2844 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2845 | } else if (!strncmp(ptr, "min", 3)) { |
| 2846 | if (kind == 0) { |
| 2847 | tmp_local_intv->value.uval.min = local_umin; |
| 2848 | } else if (kind == 1) { |
| 2849 | tmp_local_intv->value.sval.min = local_smin; |
| 2850 | } else if (kind == 2) { |
| 2851 | tmp_local_intv->value.fval.min = local_fmin; |
| 2852 | } |
| 2853 | |
| 2854 | ptr += 3; |
| 2855 | } else if (!strncmp(ptr, "max", 3)) { |
| 2856 | if (kind == 0) { |
| 2857 | tmp_local_intv->value.uval.min = local_umax; |
| 2858 | } else if (kind == 1) { |
| 2859 | tmp_local_intv->value.sval.min = local_smax; |
| 2860 | } else if (kind == 2) { |
| 2861 | tmp_local_intv->value.fval.min = local_fmax; |
| 2862 | } |
| 2863 | |
| 2864 | ptr += 3; |
| 2865 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2866 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2867 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | while (isspace(ptr[0])) { |
| 2871 | ptr++; |
| 2872 | } |
| 2873 | |
| 2874 | /* no interval or interval */ |
| 2875 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2876 | if (kind == 0) { |
| 2877 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2878 | } else if (kind == 1) { |
| 2879 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2880 | } else if (kind == 2) { |
| 2881 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2882 | } |
| 2883 | } else if (!strncmp(ptr, "..", 2)) { |
| 2884 | /* skip ".." */ |
| 2885 | ptr += 2; |
| 2886 | while (isspace(ptr[0])) { |
| 2887 | ++ptr; |
| 2888 | } |
| 2889 | |
| 2890 | /* max */ |
| 2891 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2892 | if (kind == 0) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2893 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2894 | } else if (kind == 1) { |
Radek Krejci | f87f07d | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 2895 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2896 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 2897 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
| 2898 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
| 2899 | goto error; |
| 2900 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2901 | } |
| 2902 | } else if (!strncmp(ptr, "max", 3)) { |
| 2903 | if (kind == 0) { |
| 2904 | tmp_local_intv->value.uval.max = local_umax; |
| 2905 | } else if (kind == 1) { |
| 2906 | tmp_local_intv->value.sval.max = local_smax; |
| 2907 | } else if (kind == 2) { |
| 2908 | tmp_local_intv->value.fval.max = local_fmax; |
| 2909 | } |
| 2910 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2911 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2912 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2913 | } |
| 2914 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2915 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2916 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2917 | } |
| 2918 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2919 | /* check min and max in correct order*/ |
| 2920 | if (kind == 0) { |
| 2921 | /* current segment */ |
| 2922 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 2923 | goto error; |
| 2924 | } |
| 2925 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 2926 | goto error; |
| 2927 | } |
| 2928 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2929 | 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] | 2930 | goto error; |
| 2931 | } |
| 2932 | } else if (kind == 1) { |
| 2933 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 2934 | goto error; |
| 2935 | } |
| 2936 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 2937 | goto error; |
| 2938 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2939 | 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] | 2940 | goto error; |
| 2941 | } |
| 2942 | } else if (kind == 2) { |
| 2943 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 2944 | goto error; |
| 2945 | } |
| 2946 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 2947 | goto error; |
| 2948 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 2949 | 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] | 2950 | /* 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] | 2951 | goto error; |
| 2952 | } |
| 2953 | } |
| 2954 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2955 | /* next segment (next OR) */ |
| 2956 | seg_ptr = strchr(seg_ptr, '|'); |
| 2957 | if (!seg_ptr) { |
| 2958 | break; |
| 2959 | } |
| 2960 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 2961 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | /* check local restrictions against superior ones */ |
| 2965 | if (intv) { |
| 2966 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2967 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2968 | |
| 2969 | while (tmp_local_intv && tmp_intv) { |
| 2970 | /* reuse local variables */ |
| 2971 | if (kind == 0) { |
| 2972 | local_umin = tmp_local_intv->value.uval.min; |
| 2973 | local_umax = tmp_local_intv->value.uval.max; |
| 2974 | |
| 2975 | /* it must be in this interval */ |
| 2976 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2977 | /* this interval is covered, next one */ |
| 2978 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2979 | tmp_local_intv = tmp_local_intv->next; |
| 2980 | continue; |
| 2981 | /* ascending order of restrictions -> fail */ |
| 2982 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2983 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2984 | } |
| 2985 | } |
| 2986 | } else if (kind == 1) { |
| 2987 | local_smin = tmp_local_intv->value.sval.min; |
| 2988 | local_smax = tmp_local_intv->value.sval.max; |
| 2989 | |
| 2990 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2991 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2992 | tmp_local_intv = tmp_local_intv->next; |
| 2993 | continue; |
| 2994 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2995 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | } else if (kind == 2) { |
| 2999 | local_fmin = tmp_local_intv->value.fval.min; |
| 3000 | local_fmax = tmp_local_intv->value.fval.max; |
| 3001 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3002 | 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] | 3003 | && (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] | 3004 | 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] | 3005 | tmp_local_intv = tmp_local_intv->next; |
| 3006 | continue; |
| 3007 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3008 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | tmp_intv = tmp_intv->next; |
| 3014 | } |
| 3015 | |
| 3016 | /* some interval left uncovered -> fail */ |
| 3017 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3018 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3019 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3022 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3023 | if (intv) { |
| 3024 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3025 | tmp_intv->next = local_intv; |
| 3026 | } else { |
| 3027 | intv = local_intv; |
| 3028 | } |
| 3029 | *ret = intv; |
| 3030 | |
| 3031 | return EXIT_SUCCESS; |
| 3032 | |
| 3033 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3034 | while (intv) { |
| 3035 | tmp_intv = intv->next; |
| 3036 | free(intv); |
| 3037 | intv = tmp_intv; |
| 3038 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3039 | while (local_intv) { |
| 3040 | tmp_local_intv = local_intv->next; |
| 3041 | free(local_intv); |
| 3042 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3045 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3046 | } |
| 3047 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3048 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3049 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3050 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3051 | * |
| 3052 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3053 | * @param[in] mod_name Typedef name module name. |
| 3054 | * @param[in] module Main module. |
| 3055 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3056 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3057 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3058 | * @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] | 3059 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3060 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3061 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3062 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3063 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3064 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3065 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3066 | int tpdf_size; |
| 3067 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3068 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3069 | /* no prefix, try built-in types */ |
| 3070 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3071 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3072 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3073 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3074 | } |
| 3075 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3079 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3080 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3081 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3085 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3086 | /* search in local typedefs */ |
| 3087 | while (parent) { |
| 3088 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3089 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3090 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3091 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3092 | break; |
| 3093 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3094 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3095 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3096 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3097 | break; |
| 3098 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3099 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3100 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3101 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3102 | break; |
| 3103 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3104 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3105 | case LYS_ACTION: |
| 3106 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3107 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3108 | break; |
| 3109 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3110 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3111 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3112 | tpdf = ((struct lys_node_notif *)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_INPUT: |
| 3116 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3117 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3118 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3119 | break; |
| 3120 | |
| 3121 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3122 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3123 | continue; |
| 3124 | } |
| 3125 | |
| 3126 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3127 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3128 | match = &tpdf[i]; |
| 3129 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3133 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3134 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3135 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3136 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 3137 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3138 | if (!module) { |
| 3139 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | /* search in top level typedefs */ |
| 3144 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3145 | 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] | 3146 | match = &module->tpdf[i]; |
| 3147 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3152 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3153 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3154 | 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] | 3155 | match = &module->inc[i].submodule->tpdf[j]; |
| 3156 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | } |
| 3160 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3161 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3162 | |
| 3163 | check_leafref: |
| 3164 | if (ret) { |
| 3165 | *ret = match; |
| 3166 | } |
| 3167 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3168 | while (!match->type.info.lref.path) { |
| 3169 | match = match->type.der; |
| 3170 | assert(match); |
| 3171 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3172 | } |
| 3173 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3174 | } |
| 3175 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3176 | /** |
| 3177 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3178 | * |
| 3179 | * @param[in] type Type definition to use. |
| 3180 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3181 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3182 | * |
| 3183 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3184 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3185 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3186 | 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] | 3187 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3188 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3189 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3190 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3191 | char *s; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3192 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3193 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3194 | assert(value); |
| 3195 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3196 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3197 | /* the type was not resolved yet, nothing to do for now */ |
| 3198 | return EXIT_FAILURE; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3199 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3200 | /* do not check defaults in not implemented module's data */ |
| 3201 | return EXIT_SUCCESS; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3202 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3203 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3204 | * but in typedef the value has to be modified to include the prefix */ |
| 3205 | if (*value) { |
| 3206 | if (strchr(*value, ':')) { |
| 3207 | dflt = transform_schema2json(module, *value); |
| 3208 | } else { |
| 3209 | /* default prefix of the module where the typedef is defined */ |
| 3210 | asprintf(&s, "%s:%s", lys_main_module(module)->name, *value); |
| 3211 | dflt = lydict_insert_zc(module->ctx, s); |
| 3212 | } |
| 3213 | lydict_remove(module->ctx, *value); |
| 3214 | *value = dflt; |
| 3215 | } |
| 3216 | return EXIT_SUCCESS; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3217 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3218 | /* leafref in typedef cannot be checked */ |
| 3219 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3222 | dflt = *value; |
| 3223 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3224 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3225 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3226 | if (base_tpdf->dflt) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3227 | dflt = base_tpdf->dflt; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3228 | break; |
| 3229 | } |
| 3230 | } |
| 3231 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3232 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3233 | /* no default value, nothing to check, all is well */ |
| 3234 | return EXIT_SUCCESS; |
| 3235 | } |
| 3236 | |
| 3237 | /* 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)? */ |
| 3238 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3239 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3240 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
| 3241 | return EXIT_SUCCESS; |
| 3242 | } else { |
| 3243 | /* check the default value from typedef, but use also the typedef's module |
| 3244 | * due to possible searching in imported modules which is expected in |
| 3245 | * typedef's module instead of module where the typedef is used */ |
| 3246 | module = base_tpdf->module; |
| 3247 | } |
| 3248 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3249 | case LY_TYPE_INST: |
| 3250 | case LY_TYPE_LEAFREF: |
| 3251 | case LY_TYPE_BOOL: |
| 3252 | case LY_TYPE_EMPTY: |
| 3253 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
| 3254 | return EXIT_SUCCESS; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3255 | case LY_TYPE_BITS: |
| 3256 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3257 | if (type->info.bits.count) { |
| 3258 | break; |
| 3259 | } |
| 3260 | return EXIT_SUCCESS; |
| 3261 | case LY_TYPE_ENUM: |
| 3262 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3263 | if (type->info.enums.count) { |
| 3264 | break; |
| 3265 | } |
| 3266 | return EXIT_SUCCESS; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3267 | case LY_TYPE_DEC64: |
| 3268 | if (type->info.dec64.range) { |
| 3269 | break; |
| 3270 | } |
| 3271 | return EXIT_SUCCESS; |
| 3272 | case LY_TYPE_BINARY: |
| 3273 | if (type->info.binary.length) { |
| 3274 | break; |
| 3275 | } |
| 3276 | return EXIT_SUCCESS; |
| 3277 | case LY_TYPE_INT8: |
| 3278 | case LY_TYPE_INT16: |
| 3279 | case LY_TYPE_INT32: |
| 3280 | case LY_TYPE_INT64: |
| 3281 | case LY_TYPE_UINT8: |
| 3282 | case LY_TYPE_UINT16: |
| 3283 | case LY_TYPE_UINT32: |
| 3284 | case LY_TYPE_UINT64: |
| 3285 | if (type->info.num.range) { |
| 3286 | break; |
| 3287 | } |
| 3288 | return EXIT_SUCCESS; |
| 3289 | case LY_TYPE_STRING: |
| 3290 | if (type->info.str.length || type->info.str.patterns) { |
| 3291 | break; |
| 3292 | } |
| 3293 | return EXIT_SUCCESS; |
| 3294 | case LY_TYPE_UNION: |
| 3295 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3296 | break; |
| 3297 | default: |
| 3298 | LOGINT; |
| 3299 | return -1; |
| 3300 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3301 | } else if (type->base == LY_TYPE_EMPTY) { |
| 3302 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3303 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
| 3304 | return -1; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3305 | } |
| 3306 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3307 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3308 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3309 | node.value_str = dflt; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3310 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3311 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3312 | LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3313 | node.schema->name = strdup("fake-default"); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3314 | LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1); |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3315 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3316 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3317 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3318 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3319 | if (!type->info.lref.target) { |
| 3320 | ret = EXIT_FAILURE; |
| 3321 | goto finish; |
| 3322 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3323 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3324 | if (!ret) { |
| 3325 | /* adopt possibly changed default value to its canonical form */ |
| 3326 | if (*value) { |
| 3327 | *value = dflt; |
| 3328 | } |
| 3329 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3330 | } else { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 3331 | 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] | 3332 | /* possible forward reference */ |
| 3333 | ret = 1; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3334 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3335 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3336 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3337 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3338 | /* we have refined bits/enums */ |
| 3339 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3340 | "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] | 3341 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3342 | } |
| 3343 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3344 | } else { |
| 3345 | /* success - adopt canonical form from the node into the default value */ |
| 3346 | if (dflt != node.value_str) { |
| 3347 | /* this can happen only if we have non-inherited default value, |
| 3348 | * inherited default values are already in canonical form */ |
| 3349 | assert(dflt == *value); |
| 3350 | *value = node.value_str; |
| 3351 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3352 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | finish: |
| 3356 | if (node.value_type == LY_TYPE_BITS) { |
| 3357 | free(node.value.bit); |
| 3358 | } |
| 3359 | free((char *)node.schema->name); |
| 3360 | free(node.schema); |
| 3361 | |
| 3362 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3365 | /** |
| 3366 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3367 | * |
| 3368 | * @param[in] key The key to check. |
| 3369 | * @param[in] flags What flags to check. |
| 3370 | * @param[in] list The list of all the keys. |
| 3371 | * @param[in] index Index of the key in the key list. |
| 3372 | * @param[in] name The name of the keys. |
| 3373 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3374 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3375 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3376 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3377 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3378 | 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] | 3379 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3380 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3381 | char *dup = NULL; |
| 3382 | int j; |
| 3383 | |
| 3384 | /* existence */ |
| 3385 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3386 | if (name[len] != '\0') { |
| 3387 | dup = strdup(name); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3388 | LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3389 | dup[len] = '\0'; |
| 3390 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3391 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3392 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3393 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3394 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* uniqueness */ |
| 3398 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3399 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3400 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3401 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3406 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3407 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3408 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | /* type of the leaf is not built-in empty */ |
Radek Krejci | c3738db | 2016-09-15 15:51:21 +0200 | [diff] [blame] | 3412 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3413 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3414 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3418 | 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] | 3419 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3420 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3423 | /* key is not placed from augment */ |
| 3424 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3425 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3426 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3427 | return -1; |
| 3428 | } |
| 3429 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3430 | /* key is not when/if-feature -conditional */ |
| 3431 | j = 0; |
| 3432 | if (key->when || (key->iffeature_size && (j = 1))) { |
| 3433 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3434 | 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] | 3435 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3436 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3439 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3440 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3441 | |
| 3442 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3443 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3444 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3445 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3446 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3447 | * |
| 3448 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3449 | */ |
| 3450 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3451 | 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] | 3452 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3453 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3454 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3455 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3456 | 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] | 3457 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3458 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3459 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3460 | if (rc > 0) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3461 | 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] | 3462 | } else if (rc == -2) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3463 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3464 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3465 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3466 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3467 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3468 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3469 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3470 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3471 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3472 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3473 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3474 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3475 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3476 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3477 | } |
| 3478 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3479 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3480 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3481 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3482 | return -1; |
| 3483 | } |
| 3484 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3485 | /* check that all unique's targets are of the same config type */ |
| 3486 | if (*trg_type) { |
| 3487 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
| 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, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3490 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3491 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3492 | return -1; |
| 3493 | } |
| 3494 | } else { |
| 3495 | /* first unique */ |
| 3496 | if (leaf->flags & LYS_CONFIG_W) { |
| 3497 | *trg_type = 1; |
| 3498 | } else { |
| 3499 | *trg_type = 2; |
| 3500 | } |
| 3501 | } |
| 3502 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3503 | /* set leaf's unique flag */ |
| 3504 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3505 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3506 | return EXIT_SUCCESS; |
| 3507 | |
| 3508 | error: |
| 3509 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3510 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3511 | } |
| 3512 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3513 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3514 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3515 | { |
| 3516 | /* there are items after the one deleted */ |
| 3517 | if (i+1 < unres->count) { |
| 3518 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3519 | 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] | 3520 | |
| 3521 | /* deleting the last item */ |
| 3522 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3523 | free(unres->node); |
| 3524 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3528 | --unres->count; |
| 3529 | } |
| 3530 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3531 | /** |
| 3532 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3533 | * |
| 3534 | * @param[in] mod Module to search in. |
| 3535 | * @param[in] name Name of the data node. |
| 3536 | * @param[in] nam_len Length of the name. |
| 3537 | * @param[in] start Data node to start the search from. |
| 3538 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3539 | * they are replaced (!!) with the resolvents. |
| 3540 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3541 | * @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] | 3542 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3543 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3544 | 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] | 3545 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3546 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3547 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3548 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3549 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3550 | if (!parents->count) { |
| 3551 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3552 | parents->node = malloc(sizeof *parents->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3553 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3554 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3555 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3556 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3557 | 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] | 3558 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3559 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3560 | continue; |
| 3561 | } |
| 3562 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3563 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3564 | 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] | 3565 | && node->schema->name[nam_len] == '\0') { |
| 3566 | /* matching target */ |
| 3567 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3568 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3569 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3570 | flag = 1; |
| 3571 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3572 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3573 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3574 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3575 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3576 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3577 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3578 | } |
| 3579 | } |
| 3580 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3581 | |
| 3582 | if (!flag) { |
| 3583 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3584 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3585 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3586 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3587 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3588 | } |
| 3589 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3590 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3591 | } |
| 3592 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3593 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3594 | 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] | 3595 | { |
| 3596 | int dep1, dep2; |
| 3597 | const struct lys_node *node; |
| 3598 | |
| 3599 | if (lys_parent(op_node)) { |
| 3600 | /* inner operation (notif/action) */ |
| 3601 | if (abs_path) { |
| 3602 | return 1; |
| 3603 | } else { |
| 3604 | /* compare depth of both nodes */ |
| 3605 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3606 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3607 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3608 | return 1; |
| 3609 | } |
| 3610 | } |
| 3611 | } else { |
| 3612 | /* top-level operation (notif/rpc) */ |
| 3613 | if (op_node != first_node) { |
| 3614 | return 1; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3621 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3622 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3623 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3624 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3625 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3626 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3627 | * @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] | 3628 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3629 | * @return 0 on forward reference, otherwise the number |
| 3630 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3631 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3632 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3633 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3634 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3635 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3636 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3637 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3638 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3639 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3640 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3641 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3642 | |
| 3643 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3644 | 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] | 3645 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3646 | 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] | 3647 | return -parsed+i; |
| 3648 | } |
| 3649 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3650 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3651 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3652 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3653 | if (sour_pref) { |
| 3654 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len); |
| 3655 | } else { |
| 3656 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3657 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3658 | 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] | 3659 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3660 | 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] | 3661 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3665 | dest_parent_times = 0; |
| 3666 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3667 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3668 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3669 | 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] | 3670 | return -parsed; |
| 3671 | } |
| 3672 | pke_parsed += i; |
| 3673 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3674 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3675 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3676 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3677 | /* we are in an unresolved augment, cannot evaluate */ |
| 3678 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
| 3679 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3680 | return 0; |
| 3681 | } |
| 3682 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3683 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3684 | * all schema nodes that cannot be instantiated in data tree */ |
| 3685 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3686 | 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] | 3687 | dst_node = lys_parent(dst_node)); |
| 3688 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3689 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3690 | 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] | 3691 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3692 | } |
| 3693 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3694 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3695 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3696 | if (dest_pref) { |
| 3697 | trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len); |
| 3698 | } else { |
| 3699 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3700 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3701 | 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] | 3702 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame] | 3703 | 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] | 3704 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3707 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3708 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3709 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3710 | } |
| 3711 | first_iter = 0; |
| 3712 | } |
| 3713 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3714 | if (pke_len == pke_parsed) { |
| 3715 | break; |
| 3716 | } |
| 3717 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3718 | 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] | 3719 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3720 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3721 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3722 | return -parsed; |
| 3723 | } |
| 3724 | pke_parsed += i; |
| 3725 | } |
| 3726 | |
| 3727 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3728 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3729 | 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] | 3730 | 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] | 3731 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3732 | return -parsed; |
| 3733 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3734 | } while (has_predicate); |
| 3735 | |
| 3736 | return parsed; |
| 3737 | } |
| 3738 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3739 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3740 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3741 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3742 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3743 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3744 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3745 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3746 | * @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] | 3747 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3748 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3749 | 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] | 3750 | { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3751 | const struct lys_node *node, *op_node = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3752 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3753 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3754 | const char *id, *prefix, *name; |
| 3755 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3756 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3757 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3758 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3759 | parent_times = 0; |
| 3760 | id = path; |
| 3761 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3762 | /* find operation schema we are in */ |
| 3763 | for (op_node = lys_parent(parent); |
| 3764 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 3765 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3766 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3767 | cur_module = lys_node_module(parent); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3768 | do { |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3769 | 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] | 3770 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3771 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3772 | } |
| 3773 | id += i; |
| 3774 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3775 | /* get the current module */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3776 | tmp_mod = prefix ? lys_get_import_module(cur_module, NULL, 0, prefix, pref_len) : cur_module; |
| 3777 | if (!tmp_mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3778 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
| 3779 | return EXIT_FAILURE; |
| 3780 | } |
| 3781 | last_aug = NULL; |
| 3782 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3783 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3784 | if (parent_times == -1) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3785 | /* use module data */ |
| 3786 | node = NULL; |
Radek Krejci | 990af1f | 2016-11-09 13:53:36 +0100 | [diff] [blame] | 3787 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3788 | } else if (parent_times > 0) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3789 | /* we are looking for the right parent */ |
| 3790 | for (i = 0, node = parent; i < parent_times; i++) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3791 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 3792 | && !((struct lys_node_augment *)node->parent)->target) { |
| 3793 | /* we are in an unresolved augment, cannot evaluate */ |
| 3794 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 3795 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path); |
| 3796 | return EXIT_FAILURE; |
| 3797 | } |
| 3798 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 3799 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3800 | * all schema nodes that cannot be instantiated in data tree */ |
| 3801 | for (node = lys_parent(node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3802 | 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] | 3803 | node = lys_parent(node)); |
| 3804 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3805 | if (!node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3806 | if (i == parent_times - 1) { |
| 3807 | /* top-level */ |
| 3808 | break; |
| 3809 | } |
| 3810 | |
| 3811 | /* higher than top-level */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3812 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3813 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3814 | } |
| 3815 | } |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3816 | } else { |
| 3817 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3818 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3819 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3820 | } |
| 3821 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3822 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 3823 | * - useless to search for that in augments) */ |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3824 | if (!tmp_mod->implemented && node) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3825 | get_next_augment: |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3826 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3827 | } |
| 3828 | |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 3829 | 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] | 3830 | | 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] | 3831 | if (rc) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3832 | if (last_aug) { |
| 3833 | goto get_next_augment; |
| 3834 | } |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3835 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3836 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3837 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3838 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3839 | if (first_iter) { |
| 3840 | /* 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] | 3841 | if (op_node && parent_times && |
| 3842 | 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] | 3843 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3844 | } |
| 3845 | first_iter = 0; |
| 3846 | } |
| 3847 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3848 | if (has_predicate) { |
| 3849 | /* we have predicate, so the current result must be list */ |
| 3850 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3851 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3852 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3853 | } |
| 3854 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3855 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3856 | if (!i) { |
| 3857 | return EXIT_FAILURE; |
| 3858 | } else if (i < 0) { |
| 3859 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3860 | } |
| 3861 | id += i; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3862 | has_predicate = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3863 | } |
| 3864 | } while (id[0]); |
| 3865 | |
Michal Vasko | ca91768 | 2016-07-25 11:00:37 +0200 | [diff] [blame] | 3866 | /* 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] | 3867 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3868 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 3869 | 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] | 3870 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3873 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3874 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3875 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3876 | return -1; |
| 3877 | } |
| 3878 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3879 | if (ret) { |
| 3880 | *ret = node; |
| 3881 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3882 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3883 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3886 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3887 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3888 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3889 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3890 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3891 | * @param[in,out] node_match Nodes matching the restriction without |
| 3892 | * the predicate. Nodes not satisfying |
| 3893 | * the predicate are removed. |
| 3894 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3895 | * @return Number of characters successfully parsed, |
| 3896 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3897 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3898 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3899 | resolve_instid_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3900 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3901 | /* ... /node[target = value] ... */ |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3902 | struct lyd_node *target; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3903 | const char *model, *name, *value; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3904 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed, pred_iter, k; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3905 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3906 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3907 | assert(pred && node_match->count); |
| 3908 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3909 | idx = -1; |
| 3910 | parsed = 0; |
| 3911 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3912 | pred_iter = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3913 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3914 | if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3915 | return -parsed+i; |
| 3916 | } |
| 3917 | parsed += i; |
| 3918 | pred += i; |
| 3919 | |
| 3920 | if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3921 | /* pos */ |
| 3922 | assert(!value); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3923 | idx = atoi(name); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3924 | } else if (name[0] != '.') { |
| 3925 | /* list keys */ |
| 3926 | if (pred_iter < 0) { |
| 3927 | pred_iter = 1; |
| 3928 | } else { |
| 3929 | ++pred_iter; |
| 3930 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3931 | } |
| 3932 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 3933 | for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3934 | /* target */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3935 | if (name[0] == '.') { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3936 | /* leaf-list value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3937 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3938 | goto remove_instid; |
| 3939 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3940 | |
| 3941 | target = node_match->node[j]; |
| 3942 | /* check the value */ |
| 3943 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3944 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3945 | goto remove_instid; |
| 3946 | } |
| 3947 | |
| 3948 | } else if (!value) { |
| 3949 | /* keyless list position */ |
| 3950 | if ((node_match->node[j]->schema->nodetype != LYS_LIST) |
| 3951 | || ((struct lys_node_list *)node_match->node[j]->schema)->keys) { |
| 3952 | goto remove_instid; |
| 3953 | } |
| 3954 | |
| 3955 | if (idx != cur_idx) { |
| 3956 | goto remove_instid; |
| 3957 | } |
| 3958 | |
| 3959 | } else { |
| 3960 | /* list key value */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3961 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3962 | goto remove_instid; |
| 3963 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3964 | |
| 3965 | /* key module must match the list module */ |
| 3966 | if (strncmp(node_match->node[j]->schema->module->name, model, mod_len) |
| 3967 | || node_match->node[j]->schema->module->name[mod_len]) { |
| 3968 | goto remove_instid; |
| 3969 | } |
| 3970 | /* find the key leaf */ |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3971 | for (k = 1, target = node_match->node[j]->child; target && (k < pred_iter); k++, target = target->next); |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3972 | if (!target) { |
| 3973 | goto remove_instid; |
| 3974 | } |
| 3975 | if ((struct lys_node_leaf *)target->schema != |
| 3976 | ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) { |
| 3977 | goto remove_instid; |
| 3978 | } |
| 3979 | |
| 3980 | /* check the value */ |
| 3981 | if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len) |
| 3982 | || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) { |
| 3983 | goto remove_instid; |
| 3984 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3985 | } |
| 3986 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3987 | /* instid is ok, continue check with the next one */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3988 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3989 | continue; |
| 3990 | |
| 3991 | remove_instid: |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3992 | /* does not fulfill conditions, remove instid record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3993 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3994 | } |
| 3995 | } while (has_predicate); |
| 3996 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 3997 | /* check that all list keys were specified */ |
| 3998 | if ((pred_iter > 0) && node_match->count) { |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 3999 | j = 0; |
| 4000 | while (j < node_match->count) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4001 | assert(node_match->node[j]->schema->nodetype == LYS_LIST); |
| 4002 | if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) { |
| 4003 | /* not enough predicates, just remove the list instance */ |
| 4004 | unres_data_del(node_match, j); |
Michal Vasko | 045182c | 2016-09-09 12:44:07 +0200 | [diff] [blame] | 4005 | } else { |
| 4006 | ++j; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | if (!node_match->count) { |
| 4011 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys."); |
| 4012 | } |
| 4013 | } |
| 4014 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4015 | return parsed; |
| 4016 | } |
| 4017 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4018 | int |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4019 | lys_check_xpath(struct lys_node *node, int check_place, int warn_on_fwd_ref) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4020 | { |
| 4021 | struct lys_node *parent, *elem; |
| 4022 | struct lyxp_set set; |
| 4023 | uint32_t i; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4024 | int ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4025 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4026 | if (check_place) { |
| 4027 | parent = node; |
| 4028 | while (parent) { |
| 4029 | if (parent->nodetype == LYS_GROUPING) { |
| 4030 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4031 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4032 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4033 | if (parent->nodetype == LYS_AUGMENT) { |
| 4034 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4035 | /* unresolved augment, skip for now (will be checked later) */ |
| 4036 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4037 | } else { |
| 4038 | parent = ((struct lys_node_augment *)parent)->target; |
| 4039 | continue; |
| 4040 | } |
| 4041 | } |
| 4042 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4043 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4046 | ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref); |
| 4047 | if (ret == -1) { |
| 4048 | return -1; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent)); |
| 4052 | |
| 4053 | for (i = 0; i < set.used; ++i) { |
| 4054 | /* skip roots'n'stuff */ |
| 4055 | if (set.val.snodes[i].type == LYXP_NODE_ELEM) { |
| 4056 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
| 4057 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags, |
| 4058 | lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) { |
| 4059 | return -1; |
| 4060 | } |
| 4061 | |
| 4062 | if (parent) { |
| 4063 | for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem)); |
| 4064 | if (!elem) { |
| 4065 | /* not in node's RPC or notification subtree, set the flag */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4066 | node->flags |= LYS_XPATH_DEP; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4067 | break; |
| 4068 | } |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | free(set.val.snodes); |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 4074 | return ret; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4075 | } |
| 4076 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4077 | static int |
| 4078 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4079 | { |
| 4080 | int i; |
| 4081 | |
| 4082 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4083 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4084 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4085 | 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] | 4086 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4087 | return -1; |
| 4088 | } |
| 4089 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4090 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4091 | } else if (type->base == LY_TYPE_UNION) { |
| 4092 | for (i = 0; i < type->info.uni.count; i++) { |
| 4093 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4094 | return -1; |
| 4095 | } |
| 4096 | } |
| 4097 | } |
| 4098 | return 0; |
| 4099 | } |
| 4100 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4101 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4102 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4103 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4104 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4105 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4106 | * @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] | 4107 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4108 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4109 | * |
| 4110 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4111 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4112 | int |
| 4113 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4114 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4115 | struct lys_node_leaf *leaf; |
| 4116 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4117 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4118 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4119 | if (clear) { |
| 4120 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4121 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4122 | } else { |
| 4123 | if (node->flags & LYS_CONFIG_SET) { |
| 4124 | /* skip nodes with an explicit config value */ |
| 4125 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 4126 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4127 | 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] | 4128 | return -1; |
| 4129 | } |
| 4130 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4131 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4132 | |
| 4133 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4134 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4135 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4136 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4137 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4138 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
| 4139 | return -1; |
| 4140 | } |
| 4141 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4142 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4143 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4144 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4145 | return -1; |
| 4146 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4147 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4148 | leaf = (struct lys_node_leaf *)node; |
| 4149 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4150 | return -1; |
| 4151 | } |
| 4152 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4153 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4154 | |
| 4155 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4156 | } |
| 4157 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4158 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4159 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4160 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4161 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4162 | * @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] | 4163 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4164 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4165 | * @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] | 4166 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4167 | static int |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4168 | 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] | 4169 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4170 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4171 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4172 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 4173 | struct ly_set *set; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4174 | |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4175 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4176 | mod = lys_main_module(aug->module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4177 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4178 | /* set it as not applied for now */ |
| 4179 | aug->flags |= LYS_NOTAPPLIED; |
| 4180 | |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4181 | /* 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] | 4182 | if (!aug->target) { |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4183 | /* resolve target node */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 4184 | rc = resolve_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0); |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4185 | if (rc == -1) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 4186 | LOGVAL(LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4187 | return -1; |
| 4188 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 4189 | if (!set) { |
Michal Vasko | 9bf4aa0 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4190 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
| 4191 | return EXIT_FAILURE; |
| 4192 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 4193 | aug->target = set->set.s[0]; |
| 4194 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4195 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4196 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4197 | /* check for mandatory nodes - if the target node is in another module |
| 4198 | * the added nodes cannot be mandatory |
| 4199 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4200 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4201 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4202 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4203 | } |
| 4204 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4205 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4206 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4207 | LY_TREE_FOR(aug->child, sub) { |
| 4208 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4209 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
| 4210 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4211 | 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] | 4212 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4213 | return -1; |
| 4214 | } |
| 4215 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4216 | } 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] | 4217 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4218 | 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] | 4219 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4220 | 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] | 4221 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4222 | return -1; |
| 4223 | } |
| 4224 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4225 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4226 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4227 | 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] | 4228 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4229 | 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] | 4230 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4231 | return -1; |
| 4232 | } |
| 4233 | } |
| 4234 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4235 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4236 | 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] | 4237 | return -1; |
| 4238 | } |
| 4239 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4240 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4241 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4242 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4243 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4244 | } |
| 4245 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4246 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4247 | if (!aug->child) { |
| 4248 | /* empty augment, nothing to connect, but it is techincally applied */ |
| 4249 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
| 4250 | aug->flags &= ~LYS_NOTAPPLIED; |
| 4251 | } else if (mod->implemented && apply_aug(aug, unres)) { |
| 4252 | /* we tried to connect it, we failed */ |
| 4253 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4254 | } |
| 4255 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4256 | return EXIT_SUCCESS; |
| 4257 | } |
| 4258 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4259 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4260 | 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] | 4261 | { |
| 4262 | enum LY_VLOG_ELEM vlog_type; |
| 4263 | void *vlog_node; |
| 4264 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4265 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4266 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4267 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4268 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4269 | struct lys_ext_instance *tmp_ext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4270 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4271 | |
| 4272 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4273 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4274 | vlog_node = info->parent; |
| 4275 | vlog_type = LY_VLOG_LYS; |
| 4276 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4277 | case LYEXT_PAR_MODULE: |
| 4278 | case LYEXT_PAR_IMPORT: |
| 4279 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4280 | vlog_node = NULL; |
| 4281 | vlog_type = LY_VLOG_LYS; |
| 4282 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4283 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4284 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4285 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4286 | break; |
| 4287 | } |
| 4288 | |
| 4289 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4290 | /* YIN */ |
| 4291 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4292 | /* get the module where the extension is supposed to be defined */ |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4293 | 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] | 4294 | if (!mod) { |
| 4295 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4296 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | /* find the extension definition */ |
| 4300 | e = NULL; |
| 4301 | for (i = 0; i < mod->extensions_size; i++) { |
| 4302 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4303 | e = &mod->extensions[i]; |
| 4304 | break; |
| 4305 | } |
| 4306 | } |
| 4307 | /* try submodules */ |
| 4308 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4309 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4310 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4311 | e = &mod->inc[j].submodule->extensions[i]; |
| 4312 | break; |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | if (!e) { |
| 4317 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
| 4318 | return EXIT_FAILURE; |
| 4319 | } |
| 4320 | |
| 4321 | /* 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] | 4322 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4323 | if (e->plugin && e->plugin->check_position) { |
| 4324 | /* common part - we have plugin with position checking function, use it first */ |
| 4325 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4326 | /* extension is not allowed here */ |
| 4327 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
| 4328 | return -1; |
| 4329 | } |
| 4330 | } |
| 4331 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4332 | /* extension type-specific part - allocation */ |
| 4333 | if (e->plugin) { |
| 4334 | etype = e->plugin->type; |
| 4335 | } else { |
| 4336 | /* default type */ |
| 4337 | etype = LYEXT_FLAG; |
| 4338 | } |
| 4339 | switch (etype) { |
| 4340 | case LYEXT_FLAG: |
| 4341 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4342 | break; |
| 4343 | case LYEXT_COMPLEX: |
| 4344 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4345 | break; |
| 4346 | case LYEXT_ERR: |
| 4347 | /* we never should be here */ |
| 4348 | LOGINT; |
| 4349 | return -1; |
| 4350 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4351 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4352 | |
| 4353 | /* common part for all extension types */ |
| 4354 | (*ext)->def = e; |
| 4355 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4356 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4357 | (*ext)->insubstmt = info->substmt; |
| 4358 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4359 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4360 | |
| 4361 | if (!(e->flags & LYS_YINELEM) && e->argument) { |
| 4362 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4363 | if (!(*ext)->arg_value) { |
| 4364 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
| 4365 | return -1; |
| 4366 | } |
| 4367 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4368 | } |
| 4369 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4370 | (*ext)->nodetype = LYS_EXT; |
| 4371 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4372 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4373 | /* extension type-specific part - parsing content */ |
| 4374 | switch (etype) { |
| 4375 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4376 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4377 | if (!yin->ns) { |
| 4378 | /* garbage */ |
| 4379 | lyxml_free(mod->ctx, yin); |
| 4380 | continue; |
| 4381 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4382 | /* standard YANG statements are not expected here */ |
| 4383 | LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
| 4384 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4385 | } else if (yin->ns == info->data.yin->ns && |
| 4386 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4387 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4388 | if ((*ext)->arg_value) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4389 | 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] | 4390 | return -1; |
| 4391 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4392 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4393 | yin->content = NULL; |
| 4394 | lyxml_free(mod->ctx, yin); |
| 4395 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4396 | /* extension instance */ |
| 4397 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4398 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4399 | return -1; |
| 4400 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4401 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4402 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4403 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4404 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4405 | break; |
| 4406 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4407 | ((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] | 4408 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4409 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4410 | return -1; |
| 4411 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4412 | break; |
| 4413 | default: |
| 4414 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4415 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4416 | |
| 4417 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4418 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4419 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4420 | /* YANG */ |
| 4421 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4422 | ext_prefix = (char *)(*ext)->def; |
| 4423 | tmp = strchr(ext_prefix, ':'); |
| 4424 | if (!tmp) { |
| 4425 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4426 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4427 | } |
| 4428 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4429 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4430 | /* get the module where the extension is supposed to be defined */ |
| 4431 | mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0); |
| 4432 | if (!mod) { |
| 4433 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4434 | return EXIT_FAILURE; |
| 4435 | } |
| 4436 | |
| 4437 | /* find the extension definition */ |
| 4438 | e = NULL; |
| 4439 | for (i = 0; i < mod->extensions_size; i++) { |
| 4440 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4441 | e = &mod->extensions[i]; |
| 4442 | break; |
| 4443 | } |
| 4444 | } |
| 4445 | /* try submodules */ |
| 4446 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4447 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4448 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4449 | e = &mod->inc[j].submodule->extensions[i]; |
| 4450 | break; |
| 4451 | } |
| 4452 | } |
| 4453 | } |
| 4454 | if (!e) { |
| 4455 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
| 4456 | return EXIT_FAILURE; |
| 4457 | } |
| 4458 | |
| 4459 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4460 | |
| 4461 | if (e->plugin && e->plugin->check_position) { |
| 4462 | /* common part - we have plugin with position checking function, use it first */ |
| 4463 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4464 | /* extension is not allowed here */ |
| 4465 | LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4466 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4467 | } |
| 4468 | } |
| 4469 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4470 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4471 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4472 | (*ext)->def = e; |
| 4473 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4474 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4475 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4476 | if (e->argument && !(*ext)->arg_value) { |
| 4477 | LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
| 4478 | goto error; |
| 4479 | } |
| 4480 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4481 | (*ext)->module = info->mod; |
| 4482 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4483 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4484 | /* extension type-specific part */ |
| 4485 | if (e->plugin) { |
| 4486 | etype = e->plugin->type; |
| 4487 | } else { |
| 4488 | /* default type */ |
| 4489 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4490 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4491 | switch (etype) { |
| 4492 | case LYEXT_FLAG: |
| 4493 | /* nothing change */ |
| 4494 | break; |
| 4495 | case LYEXT_COMPLEX: |
| 4496 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4497 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4498 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 4499 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4500 | ((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] | 4501 | if (info->data.yang) { |
| 4502 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4503 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 4504 | (struct lys_ext_instance_complex*)(*ext))) { |
| 4505 | goto error; |
| 4506 | } |
| 4507 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 4508 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4509 | goto error; |
| 4510 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 4511 | } |
| 4512 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 4513 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 4514 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4515 | break; |
| 4516 | case LYEXT_ERR: |
| 4517 | /* we never should be here */ |
| 4518 | LOGINT; |
| 4519 | goto error; |
| 4520 | } |
| 4521 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4522 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 4523 | goto error; |
| 4524 | } |
| 4525 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4529 | error: |
| 4530 | free(ext_prefix); |
| 4531 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4532 | } |
| 4533 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4534 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4535 | * @brief Resolve (find) choice default case. Does not log. |
| 4536 | * |
| 4537 | * @param[in] choic Choice to use. |
| 4538 | * @param[in] dflt Name of the default case. |
| 4539 | * |
| 4540 | * @return Pointer to the default node or NULL. |
| 4541 | */ |
| 4542 | static struct lys_node * |
| 4543 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4544 | { |
| 4545 | struct lys_node *child, *ret; |
| 4546 | |
| 4547 | LY_TREE_FOR(choic->child, child) { |
| 4548 | if (child->nodetype == LYS_USES) { |
| 4549 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4550 | if (ret) { |
| 4551 | return ret; |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | 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] | 4556 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4557 | return child; |
| 4558 | } |
| 4559 | } |
| 4560 | |
| 4561 | return NULL; |
| 4562 | } |
| 4563 | |
| 4564 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4565 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 4566 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4567 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4568 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4569 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4570 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4571 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4572 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4573 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4574 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4575 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4576 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4577 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4578 | struct lys_node_leaflist *llist; |
| 4579 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 4580 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4581 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4582 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4583 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4584 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4585 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4586 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4587 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4588 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 4589 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 4590 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 4591 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4592 | if (!uses->grp->child) { |
| 4593 | /* grouping without children, warning was already displayed */ |
| 4594 | return EXIT_SUCCESS; |
| 4595 | } |
| 4596 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4597 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4598 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 4599 | if (node_aux->nodetype & LYS_GROUPING) { |
| 4600 | /* do not instantiate groupings from groupings */ |
| 4601 | continue; |
| 4602 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 4603 | 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] | 4604 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4605 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4606 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4607 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4608 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4609 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4610 | 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] | 4611 | 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] | 4612 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 4613 | } |
| 4614 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4615 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4616 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 4617 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 4618 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4619 | if (uses->refine_size) { |
| 4620 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4621 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4622 | } |
| 4623 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4624 | /* apply refines */ |
| 4625 | for (i = 0; i < uses->refine_size; i++) { |
| 4626 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 4627 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 4628 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 4629 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 4630 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4631 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4632 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4635 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4636 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4637 | 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] | 4638 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4639 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4640 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4641 | |
| 4642 | /* description on any nodetype */ |
| 4643 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4644 | lydict_remove(ctx, node->dsc); |
| 4645 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* reference on any nodetype */ |
| 4649 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4650 | lydict_remove(ctx, node->ref); |
| 4651 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4652 | } |
| 4653 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4654 | /* config on any nodetype, |
| 4655 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 4656 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4657 | node->flags &= ~LYS_CONFIG_MASK; |
| 4658 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4662 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4663 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4664 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4665 | leaf = (struct lys_node_leaf *)node; |
| 4666 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4667 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4668 | lydict_remove(ctx, leaf->dflt); |
| 4669 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 4670 | |
| 4671 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4672 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 4673 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4674 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4675 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4676 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 4677 | /* leaf-list */ |
| 4678 | llist = (struct lys_node_leaflist *)node; |
| 4679 | |
| 4680 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4681 | for (j = 0; j < llist->dflt_size; j++) { |
| 4682 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4683 | } |
| 4684 | free(llist->dflt); |
| 4685 | |
| 4686 | /* copy the default set from refine */ |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4687 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
| 4688 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4689 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4690 | for (j = 0; j < llist->dflt_size; j++) { |
| 4691 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 4695 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 4696 | 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] | 4697 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4698 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 4699 | } |
| 4700 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 4705 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4706 | /* remove current value */ |
| 4707 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4708 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 4709 | /* set new value */ |
| 4710 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 4711 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4712 | if (rfn->flags & LYS_MAND_TRUE) { |
| 4713 | /* check if node has default value */ |
| 4714 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4715 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4716 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4717 | goto fail; |
| 4718 | } |
| 4719 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4720 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 4721 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4722 | goto fail; |
| 4723 | } |
| 4724 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4728 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 4729 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 4730 | ((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] | 4731 | } |
| 4732 | |
| 4733 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4734 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4735 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4736 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4737 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4738 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4739 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4740 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4741 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4742 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4743 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4744 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 4745 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4746 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 4751 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4752 | switch (node->nodetype) { |
| 4753 | case LYS_LEAF: |
| 4754 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 4755 | old_must = &((struct lys_node_leaf *)node)->must; |
| 4756 | break; |
| 4757 | case LYS_LEAFLIST: |
| 4758 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 4759 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 4760 | break; |
| 4761 | case LYS_LIST: |
| 4762 | old_size = &((struct lys_node_list *)node)->must_size; |
| 4763 | old_must = &((struct lys_node_list *)node)->must; |
| 4764 | break; |
| 4765 | case LYS_CONTAINER: |
| 4766 | old_size = &((struct lys_node_container *)node)->must_size; |
| 4767 | old_must = &((struct lys_node_container *)node)->must; |
| 4768 | break; |
| 4769 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4770 | case LYS_ANYDATA: |
| 4771 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 4772 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4773 | break; |
| 4774 | default: |
| 4775 | LOGINT; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4776 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | size = *old_size + rfn->must_size; |
| 4780 | must = realloc(*old_must, size * sizeof *rfn->must); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4781 | LY_CHECK_ERR_GOTO(!must, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4782 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 4783 | must[j].ext_size = rfn->must[k].ext_size; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4784 | 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] | 4785 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4786 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 4787 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 4788 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 4789 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 4790 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4791 | } |
| 4792 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 4793 | *old_must = must; |
| 4794 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4795 | |
| 4796 | /* check XPath dependencies again */ |
| 4797 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4798 | goto fail; |
| 4799 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4800 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4801 | |
| 4802 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 4803 | if (rfn->iffeature_size) { |
| 4804 | old_size = &node->iffeature_size; |
| 4805 | old_iff = &node->iffeature; |
| 4806 | |
| 4807 | size = *old_size + rfn->iffeature_size; |
| 4808 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4809 | LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4810 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 4811 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 4812 | if (usize1) { |
| 4813 | /* there is something to duplicate */ |
| 4814 | /* duplicate compiled expression */ |
| 4815 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 4816 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4817 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4818 | 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] | 4819 | |
| 4820 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4821 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4822 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4823 | 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] | 4824 | } |
| 4825 | } |
| 4826 | |
| 4827 | *old_iff = iff; |
| 4828 | *old_size = size; |
| 4829 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /* apply augments */ |
| 4833 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4834 | rc = resolve_augment(&uses->augment[i], uses->child, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4835 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4836 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4837 | } |
| 4838 | } |
| 4839 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4840 | /* check refines */ |
| 4841 | for (i = 0; i < uses->refine_size; i++) { |
| 4842 | node = refine_nodes[i]; |
| 4843 | rfn = &uses->refine[i]; |
| 4844 | |
| 4845 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4846 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4847 | 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] | 4848 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4849 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 4850 | (rfn->flags & LYS_CONFIG_W)) { |
| 4851 | /* setting config true under config false is prohibited */ |
| 4852 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4853 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4854 | "changing config from 'false' to 'true' is prohibited while " |
| 4855 | "the target's parent is still config 'false'."); |
| 4856 | goto fail; |
| 4857 | } |
| 4858 | |
| 4859 | /* inherit config change to the target children */ |
| 4860 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 4861 | if (rfn->flags & LYS_CONFIG_W) { |
| 4862 | if (iter->flags & LYS_CONFIG_SET) { |
| 4863 | /* config is set explicitely, go to next sibling */ |
| 4864 | next = NULL; |
| 4865 | goto nextsibling; |
| 4866 | } |
| 4867 | } else { /* LYS_CONFIG_R */ |
| 4868 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 4869 | /* error - we would have config data under status data */ |
| 4870 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 4871 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4872 | "changing config from 'true' to 'false' is prohibited while the target " |
| 4873 | "has still a children with explicit config 'true'."); |
| 4874 | goto fail; |
| 4875 | } |
| 4876 | } |
| 4877 | /* change config */ |
| 4878 | iter->flags &= ~LYS_CONFIG_MASK; |
| 4879 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 4880 | |
| 4881 | /* select next iter - modified LY_TREE_DFS_END */ |
| 4882 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4883 | next = NULL; |
| 4884 | } else { |
| 4885 | next = iter->child; |
| 4886 | } |
| 4887 | nextsibling: |
| 4888 | if (!next) { |
| 4889 | /* try siblings */ |
| 4890 | next = iter->next; |
| 4891 | } |
| 4892 | while (!next) { |
| 4893 | /* parent is already processed, go to its sibling */ |
| 4894 | iter = lys_parent(iter); |
| 4895 | |
| 4896 | /* no siblings, go back through parents */ |
| 4897 | if (iter == node) { |
| 4898 | /* we are done, no next element to process */ |
| 4899 | break; |
| 4900 | } |
| 4901 | next = iter->next; |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4907 | if (rfn->dflt_size) { |
| 4908 | if (node->nodetype == LYS_CHOICE) { |
| 4909 | /* choice */ |
| 4910 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 4911 | rfn->dflt[0]); |
| 4912 | if (!((struct lys_node_choice *)node)->dflt) { |
| 4913 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
| 4914 | goto fail; |
| 4915 | } |
| 4916 | if (lyp_check_mandatory_choice(node)) { |
| 4917 | goto fail; |
| 4918 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4923 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4924 | 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] | 4925 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4926 | 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] | 4927 | goto fail; |
| 4928 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 4929 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4930 | 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] | 4931 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 4932 | 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] | 4933 | goto fail; |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4938 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4939 | if (node->nodetype == LYS_LEAFLIST) { |
| 4940 | llist = (struct lys_node_leaflist *)node; |
| 4941 | if (llist->dflt_size && llist->min) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4942 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 4943 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4944 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 4945 | goto fail; |
| 4946 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4947 | } else if (node->nodetype == LYS_LEAF) { |
| 4948 | leaf = (struct lys_node_leaf *)node; |
| 4949 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 4950 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 4951 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4952 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 4953 | goto fail; |
| 4954 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4955 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 4956 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4957 | /* 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] | 4958 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4959 | for (parent = node->parent; |
| 4960 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 4961 | parent = parent->parent) { |
| 4962 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 4963 | /* stop also on presence containers */ |
| 4964 | break; |
| 4965 | } |
| 4966 | } |
| 4967 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 4968 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 4969 | if (lyp_check_mandatory_choice(parent)) { |
| 4970 | goto fail; |
| 4971 | } |
| 4972 | } |
| 4973 | } |
| 4974 | } |
| 4975 | free(refine_nodes); |
| 4976 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4977 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4978 | |
| 4979 | fail: |
| 4980 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 4981 | lys_node_free(iter, NULL, 0); |
| 4982 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 4983 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4984 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4985 | } |
| 4986 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 4987 | void |
| 4988 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4989 | { |
| 4990 | int i; |
| 4991 | |
| 4992 | assert(der && base); |
| 4993 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4994 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4995 | /* create a set for backlinks if it does not exist */ |
| 4996 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 4997 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4998 | /* store backlink */ |
| 4999 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5000 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5001 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5002 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5003 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5004 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5005 | } |
| 5006 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5007 | /** |
| 5008 | * @brief Resolve base identity recursively. Does not log. |
| 5009 | * |
| 5010 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5011 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5012 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5013 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5014 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5015 | * @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] | 5016 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5017 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5018 | 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] | 5019 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5020 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5021 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5022 | struct lys_ident *base = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5023 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5024 | assert(ret); |
| 5025 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5026 | /* search module */ |
| 5027 | for (i = 0; i < module->ident_size; i++) { |
| 5028 | if (!strcmp(basename, module->ident[i].name)) { |
| 5029 | |
| 5030 | if (!ident) { |
| 5031 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5032 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5033 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5034 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5037 | base = &module->ident[i]; |
| 5038 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5043 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5044 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5045 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5046 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5047 | if (!ident) { |
| 5048 | *ret = &module->inc[j].submodule->ident[i]; |
| 5049 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5050 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5051 | |
| 5052 | base = &module->inc[j].submodule->ident[i]; |
| 5053 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | } |
| 5057 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5058 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5059 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5060 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5061 | /* is it already completely resolved? */ |
| 5062 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5063 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5064 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5065 | |
| 5066 | /* simple check for circular reference, |
| 5067 | * the complete check is done as a side effect of using only completely |
| 5068 | * resolved identities (previous check of unres content) */ |
| 5069 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
| 5070 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5071 | 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] | 5072 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5073 | } |
| 5074 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5075 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5076 | } |
| 5077 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5078 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5079 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5080 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5081 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5084 | /* base not found (maybe a forward reference) */ |
| 5085 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5086 | } |
| 5087 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5088 | /** |
| 5089 | * @brief Resolve base identity. Logs directly. |
| 5090 | * |
| 5091 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5092 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5093 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5094 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5095 | * @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] | 5096 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5097 | * @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] | 5098 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5099 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5100 | 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] | 5101 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5102 | { |
| 5103 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5104 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5105 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5106 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5107 | struct lys_module *mod; |
| 5108 | |
| 5109 | assert((ident && !type) || (!ident && type)); |
| 5110 | |
| 5111 | if (!type) { |
| 5112 | /* have ident to resolve */ |
| 5113 | ret = ⌖ |
| 5114 | flags = ident->flags; |
| 5115 | mod = ident->module; |
| 5116 | } else { |
| 5117 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5118 | ++type->info.ident.count; |
| 5119 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5120 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5121 | |
| 5122 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5123 | flags = type->parent->flags; |
| 5124 | mod = type->parent->module; |
| 5125 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5126 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5127 | |
| 5128 | /* search for the base identity */ |
| 5129 | name = strchr(basename, ':'); |
| 5130 | if (name) { |
| 5131 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5132 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5133 | name++; |
| 5134 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5135 | 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] | 5136 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5137 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5138 | } |
| 5139 | } else { |
| 5140 | name = basename; |
| 5141 | } |
| 5142 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5143 | /* get module where to search */ |
| 5144 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 5145 | if (!module) { |
| 5146 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 5147 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5148 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5149 | } |
| 5150 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5151 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5152 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5153 | if (!rc) { |
| 5154 | assert(*ret); |
| 5155 | |
| 5156 | /* check status */ |
| 5157 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5158 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5159 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5160 | } else if (ident) { |
| 5161 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5162 | if (lys_main_module(mod)->implemented) { |
| 5163 | /* in case of the implemented identity, maintain backlinks to it |
| 5164 | * from the base identities to make it available when resolving |
| 5165 | * data with the identity values (not implemented identity is not |
| 5166 | * allowed as an identityref value). */ |
| 5167 | resolve_identity_backlink_update(ident, *ret); |
| 5168 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5169 | } |
| 5170 | } else if (rc == EXIT_FAILURE) { |
| 5171 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5172 | if (type) { |
| 5173 | --type->info.ident.count; |
| 5174 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5175 | } |
| 5176 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5177 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5178 | } |
| 5179 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5180 | /* |
| 5181 | * 1 - true (der is derived from base) |
| 5182 | * 0 - false (der is not derived from base) |
| 5183 | */ |
| 5184 | static int |
| 5185 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5186 | { |
| 5187 | int i; |
| 5188 | |
| 5189 | if (der == base) { |
| 5190 | return 1; |
| 5191 | } else { |
| 5192 | for(i = 0; i < der->base_size; i++) { |
| 5193 | if (search_base_identity(der->base[i], base) == 1) { |
| 5194 | return 1; |
| 5195 | } |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5202 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5203 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5204 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5205 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5206 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5207 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5208 | * @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] | 5209 | * |
| 5210 | * @return Pointer to the identity resolvent, NULL on error. |
| 5211 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5212 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5213 | 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] | 5214 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5215 | const char *mod_name, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 5216 | int mod_name_len, nam_len, rc, i, j; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5217 | int make_implemented = 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5218 | unsigned int u; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5219 | struct lys_ident *der, *cur; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5220 | struct lys_module *imod = NULL, *m; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5221 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5222 | assert(type && ident_name && node && mod); |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5223 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5224 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5225 | return NULL; |
| 5226 | } |
| 5227 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 5228 | 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] | 5229 | if (rc < 1) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5230 | 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] | 5231 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5232 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5233 | 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] | 5234 | return NULL; |
| 5235 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5236 | |
| 5237 | m = lys_main_module(mod); /* shortcut */ |
| 5238 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5239 | /* identity is defined in the same module as node */ |
| 5240 | imod = m; |
| 5241 | } else if (dflt) { |
| 5242 | /* solving identityref in default definition in schema - |
| 5243 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5244 | for (i = 0; i < mod->imp_size; i++) { |
| 5245 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5246 | imod = mod->imp[i].module; |
| 5247 | break; |
| 5248 | } |
| 5249 | } |
| 5250 | } else { |
| 5251 | /* solving identityref in data - get the (implemented) module from the context */ |
| 5252 | u = 0; |
| 5253 | while ((imod = (struct lys_module*)ly_ctx_get_module_iter(mod->ctx, &u))) { |
| 5254 | if (imod->implemented && !strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5255 | break; |
| 5256 | } |
| 5257 | } |
| 5258 | } |
| 5259 | if (!imod) { |
| 5260 | goto fail; |
| 5261 | } |
| 5262 | |
| 5263 | if (dflt && (m != imod || lys_main_module(type->parent->module) != mod)) { |
| 5264 | /* we are solving default statement in schema AND the type is not referencing the same schema, |
| 5265 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5266 | * contains the identity */ |
| 5267 | if (!imod->implemented) { |
| 5268 | cur = NULL; |
| 5269 | /* get the identity in the module */ |
| 5270 | for (i = 0; i < imod->ident_size; i++) { |
| 5271 | if (!strcmp(name, imod->ident[i].name)) { |
| 5272 | cur = &imod->ident[i]; |
| 5273 | break; |
| 5274 | } |
| 5275 | } |
| 5276 | if (!cur) { |
| 5277 | /* go through includes */ |
| 5278 | for (j = 0; j < imod->inc_size; j++) { |
| 5279 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5280 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5281 | cur = &imod->inc[j].submodule->ident[i]; |
| 5282 | break; |
| 5283 | } |
| 5284 | } |
| 5285 | } |
| 5286 | if (!cur) { |
| 5287 | goto fail; |
| 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | /* check that identity is derived from one of the type's base */ |
| 5292 | while (type->der) { |
| 5293 | for (i = 0; i < type->info.ident.count; i++) { |
| 5294 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5295 | /* cur's base matches the type's base */ |
| 5296 | make_implemented = 1; |
| 5297 | goto match; |
| 5298 | } |
| 5299 | } |
| 5300 | type = &type->der->type; |
| 5301 | } |
| 5302 | /* matching base not found */ |
| 5303 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5304 | goto fail; |
| 5305 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5306 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5307 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5308 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5309 | while (type->der) { |
| 5310 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5311 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5312 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5313 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5314 | /* there are some derived identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5315 | for (u = 0; u < cur->der->number; u++) { |
| 5316 | der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5317 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5318 | /* we have match */ |
| 5319 | cur = der; |
| 5320 | goto match; |
| 5321 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5322 | } |
| 5323 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5324 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5325 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5328 | fail: |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5329 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5330 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5331 | |
| 5332 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5333 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5334 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5335 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5336 | 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] | 5337 | return NULL; |
| 5338 | } |
| 5339 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5340 | if (make_implemented) { |
| 5341 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5342 | imod->name, cur->name, mod->name); |
| 5343 | if (lys_set_implemented(imod)) { |
| 5344 | LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
| 5345 | imod->name, cur->name); |
| 5346 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
| 5347 | goto fail; |
| 5348 | } |
| 5349 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5350 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5351 | } |
| 5352 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5353 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5354 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5355 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5356 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5357 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5358 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5359 | * @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] | 5360 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5361 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5362 | 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] | 5363 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5364 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5365 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5366 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5367 | /* 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] | 5368 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5369 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5370 | * 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] | 5371 | 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] | 5372 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5373 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5374 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5375 | if (rc == -1) { |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5376 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5377 | return -1; |
| 5378 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5379 | 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] | 5380 | return -1; |
| 5381 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5382 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5383 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5384 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5385 | return -1; |
| 5386 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5387 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5388 | } |
Michal Vasko | 92981a6 | 2016-10-14 10:25:16 +0200 | [diff] [blame] | 5389 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5390 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5391 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5392 | } |
| 5393 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5394 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5395 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5396 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 5397 | LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
| 5398 | return -1; |
| 5399 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5400 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5401 | } else { |
| 5402 | /* instantiate grouping only when it is completely resolved */ |
| 5403 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5404 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5405 | return EXIT_FAILURE; |
| 5406 | } |
| 5407 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5408 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5409 | if (!rc) { |
| 5410 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5411 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5412 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 5413 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5414 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5415 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5416 | |
| 5417 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5418 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5419 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5420 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5421 | return -1; |
| 5422 | } |
| 5423 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5424 | return EXIT_SUCCESS; |
| 5425 | } |
| 5426 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5427 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5428 | } |
| 5429 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5430 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 5431 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5432 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5433 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5434 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5435 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5436 | * @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] | 5437 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5438 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5439 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5440 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5441 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 5442 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5443 | char *s = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5444 | |
| 5445 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 5446 | assert(keys_str); |
| 5447 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 5448 | if (!list->child) { |
| 5449 | /* no child, possible forward reference */ |
| 5450 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 5451 | return EXIT_FAILURE; |
| 5452 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5453 | /* get the key name */ |
| 5454 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 5455 | len = value - keys_str; |
| 5456 | while (isspace(value[0])) { |
| 5457 | value++; |
| 5458 | } |
| 5459 | } else { |
| 5460 | len = strlen(keys_str); |
| 5461 | } |
| 5462 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5463 | if (list->keys[i]) { |
| 5464 | /* skip already resolved keys */ |
| 5465 | goto next; |
| 5466 | } |
| 5467 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5468 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 5469 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5470 | if (rc) { |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5471 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 5472 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5473 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5474 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5475 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5476 | /* check_key logs */ |
| 5477 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5480 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 5481 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5482 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 5483 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5484 | return -1; |
| 5485 | } |
| 5486 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 5487 | /* default value - is ignored, keep it but print a warning */ |
| 5488 | if (list->keys[i]->dflt) { |
| 5489 | /* log is not hidden only in case this resolving fails and in such a case |
| 5490 | * we cannot get here |
| 5491 | */ |
| 5492 | assert(*ly_vlog_hide_location()); |
| 5493 | ly_vlog_hide(0); |
| 5494 | LOGWRN("Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt, |
| 5495 | list->keys[i]->name, s = lys_path((struct lys_node*)list)); |
| 5496 | ly_vlog_hide(1); |
| 5497 | free(s); |
| 5498 | } |
| 5499 | |
| 5500 | next: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5501 | /* prepare for next iteration */ |
| 5502 | while (value && isspace(value[0])) { |
| 5503 | value++; |
| 5504 | } |
| 5505 | keys_str = value; |
| 5506 | } |
| 5507 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5508 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5509 | } |
| 5510 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5511 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5512 | * @brief Resolve (check) all must conditions of \p node. |
| 5513 | * Logs directly. |
| 5514 | * |
| 5515 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5516 | * @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] | 5517 | * |
| 5518 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 5519 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5520 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5521 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5522 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5523 | int node_flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5524 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5525 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5526 | struct lys_restr *must; |
| 5527 | struct lyxp_set set; |
| 5528 | |
| 5529 | assert(node); |
| 5530 | memset(&set, 0, sizeof set); |
| 5531 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5532 | if (inout_parent) { |
| 5533 | for (schema = lys_parent(node->schema); |
| 5534 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 5535 | schema = lys_parent(schema)); |
| 5536 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5537 | LOGINT; |
| 5538 | return -1; |
| 5539 | } |
| 5540 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 5541 | must = ((struct lys_node_inout *)schema)->must; |
| 5542 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5543 | node_flags = schema->flags; |
| 5544 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5545 | /* context node is the RPC/action */ |
| 5546 | node = node->parent; |
| 5547 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 5548 | LOGINT; |
| 5549 | return -1; |
| 5550 | } |
| 5551 | } else { |
| 5552 | switch (node->schema->nodetype) { |
| 5553 | case LYS_CONTAINER: |
| 5554 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 5555 | must = ((struct lys_node_container *)node->schema)->must; |
| 5556 | break; |
| 5557 | case LYS_LEAF: |
| 5558 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 5559 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 5560 | break; |
| 5561 | case LYS_LEAFLIST: |
| 5562 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 5563 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 5564 | break; |
| 5565 | case LYS_LIST: |
| 5566 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 5567 | must = ((struct lys_node_list *)node->schema)->must; |
| 5568 | break; |
| 5569 | case LYS_ANYXML: |
| 5570 | case LYS_ANYDATA: |
| 5571 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 5572 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 5573 | break; |
| 5574 | case LYS_NOTIF: |
| 5575 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 5576 | must = ((struct lys_node_notif *)node->schema)->must; |
| 5577 | break; |
| 5578 | default: |
| 5579 | must_size = 0; |
| 5580 | break; |
| 5581 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5582 | |
| 5583 | node_flags = node->schema->flags; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5587 | 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] | 5588 | return -1; |
| 5589 | } |
| 5590 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5591 | 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] | 5592 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 5593 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5594 | if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5595 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 5596 | } else { |
| 5597 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 5598 | if (must[i].emsg) { |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 5599 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5600 | } |
| 5601 | if (must[i].eapptag) { |
| 5602 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 5603 | } |
| 5604 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 5605 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5606 | } |
| 5607 | } |
| 5608 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5609 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5610 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5611 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 5612 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5613 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 5614 | * |
| 5615 | * @param[in] schema Schema node with the when condition. |
| 5616 | * @param[out] ctx_snode When schema context node. |
| 5617 | * @param[out] ctx_snode_type Schema context node type. |
| 5618 | */ |
| 5619 | void |
| 5620 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 5621 | { |
| 5622 | const struct lys_node *sparent; |
| 5623 | |
| 5624 | /* find a not schema-only node */ |
| 5625 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 5626 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 5627 | if (schema->nodetype == LYS_AUGMENT) { |
| 5628 | sparent = ((struct lys_node_augment *)schema)->target; |
| 5629 | } else { |
| 5630 | sparent = schema->parent; |
| 5631 | } |
| 5632 | if (!sparent) { |
| 5633 | /* context node is the document root (fake root in our case) */ |
| 5634 | if (schema->flags & LYS_CONFIG_W) { |
| 5635 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 5636 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5637 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5638 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 5639 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 5640 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5641 | break; |
| 5642 | } |
| 5643 | schema = sparent; |
| 5644 | } |
| 5645 | |
| 5646 | *ctx_snode = (struct lys_node *)schema; |
| 5647 | } |
| 5648 | |
| 5649 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5650 | * @brief Resolve (find) when condition context node. Does not log. |
| 5651 | * |
| 5652 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5653 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5654 | * @param[out] ctx_node Context node. |
| 5655 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5656 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5657 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5658 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5659 | static int |
| 5660 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 5661 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5662 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5663 | struct lyd_node *parent; |
| 5664 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5665 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5666 | uint16_t i, data_depth, schema_depth; |
| 5667 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5668 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5669 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5670 | if (node_type == LYXP_NODE_ELEM) { |
| 5671 | /* standard element context node */ |
| 5672 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 5673 | for (sparent = schema, schema_depth = 0; |
| 5674 | sparent; |
| 5675 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 5676 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 5677 | ++schema_depth; |
| 5678 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5679 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5680 | if (data_depth < schema_depth) { |
| 5681 | return -1; |
| 5682 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5683 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 5684 | /* find the corresponding data node */ |
| 5685 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 5686 | node = node->parent; |
| 5687 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5688 | if (node->schema != schema) { |
| 5689 | return -1; |
| 5690 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 5691 | } else { |
| 5692 | /* root context node */ |
| 5693 | while (node->parent) { |
| 5694 | node = node->parent; |
| 5695 | } |
| 5696 | while (node->prev->next) { |
| 5697 | node = node->prev; |
| 5698 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5701 | *ctx_node = node; |
| 5702 | *ctx_node_type = node_type; |
| 5703 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5706 | /** |
| 5707 | * @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] | 5708 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5709 | * |
| 5710 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 5711 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 5712 | * it is moved to point to another sibling still in the original tree. |
| 5713 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 5714 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 5715 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 5716 | * Ordering may change, but there will be no semantic change. |
| 5717 | * |
| 5718 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5719 | */ |
| 5720 | static int |
| 5721 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 5722 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 5723 | { |
| 5724 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5725 | const struct lys_node *slast; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5726 | |
| 5727 | switch (snode->nodetype) { |
| 5728 | case LYS_AUGMENT: |
| 5729 | case LYS_USES: |
| 5730 | case LYS_CHOICE: |
| 5731 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5732 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 5733 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 5734 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 5735 | continue; |
| 5736 | } |
| 5737 | |
| 5738 | 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] | 5739 | return -1; |
| 5740 | } |
| 5741 | } |
| 5742 | break; |
| 5743 | case LYS_CONTAINER: |
| 5744 | case LYS_LIST: |
| 5745 | case LYS_LEAF: |
| 5746 | case LYS_LEAFLIST: |
| 5747 | case LYS_ANYXML: |
| 5748 | case LYS_ANYDATA: |
| 5749 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 5750 | if (elem->schema == snode) { |
| 5751 | |
| 5752 | if (elem == *ctx_node) { |
| 5753 | /* We are going to unlink our context node! This normally cannot happen, |
| 5754 | * but we use normal top-level data nodes for faking a document root node, |
| 5755 | * so if this is the context node, we just use the next top-level node. |
| 5756 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 5757 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 5758 | * lyxp_eval() can handle this special situation. |
| 5759 | */ |
| 5760 | if (ctx_node_type == LYXP_NODE_ELEM) { |
| 5761 | LOGINT; |
| 5762 | return -1; |
| 5763 | } |
| 5764 | |
| 5765 | if (elem->prev == elem) { |
| 5766 | /* unlinking last top-level element, use an empty data tree */ |
| 5767 | *ctx_node = NULL; |
| 5768 | } else { |
| 5769 | /* in this case just use the previous/last top-level data node */ |
| 5770 | *ctx_node = elem->prev; |
| 5771 | } |
| 5772 | } else if (elem == *node) { |
| 5773 | /* We are going to unlink the currently processed node. This does not matter that |
| 5774 | * much, but we would lose access to the original data tree, so just move our |
| 5775 | * pointer somewhere still inside it. |
| 5776 | */ |
| 5777 | if ((*node)->prev != *node) { |
| 5778 | *node = (*node)->prev; |
| 5779 | } else { |
| 5780 | /* the processed node with sibings were all unlinked, oh well */ |
| 5781 | *node = NULL; |
| 5782 | } |
| 5783 | } |
| 5784 | |
| 5785 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5786 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5787 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5788 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5789 | LOGINT; |
| 5790 | return -1; |
| 5791 | } |
| 5792 | } else { |
| 5793 | *unlinked_nodes = elem; |
| 5794 | } |
| 5795 | |
| 5796 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 5797 | /* there can be only one instance */ |
| 5798 | break; |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | break; |
| 5803 | default: |
| 5804 | LOGINT; |
| 5805 | return -1; |
| 5806 | } |
| 5807 | |
| 5808 | return EXIT_SUCCESS; |
| 5809 | } |
| 5810 | |
| 5811 | /** |
| 5812 | * @brief Relink the unlinked nodes back. |
| 5813 | * |
| 5814 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 5815 | * we simply need a sibling from the original data tree. |
| 5816 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 5817 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 5818 | * or the sibling of \p unlinked_nodes. |
| 5819 | * |
| 5820 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5821 | */ |
| 5822 | static int |
| 5823 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 5824 | { |
| 5825 | struct lyd_node *elem; |
| 5826 | |
| 5827 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5828 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5829 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5830 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5831 | return -1; |
| 5832 | } |
| 5833 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 5834 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5835 | return -1; |
| 5836 | } |
| 5837 | } |
| 5838 | } |
| 5839 | |
| 5840 | return EXIT_SUCCESS; |
| 5841 | } |
| 5842 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5843 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5844 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5845 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5846 | int ret = 0; |
| 5847 | uint8_t must_size; |
| 5848 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5849 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5850 | assert(node); |
| 5851 | |
| 5852 | schema = node->schema; |
| 5853 | |
| 5854 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5855 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5856 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5857 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 5858 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5859 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5860 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 5861 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5862 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5863 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 5864 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5865 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5866 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 5867 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5868 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5869 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5870 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 5871 | break; |
| 5872 | case LYS_NOTIF: |
| 5873 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 5874 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5875 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5876 | must_size = 0; |
| 5877 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5878 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 5879 | |
| 5880 | if (must_size) { |
| 5881 | ++ret; |
| 5882 | } |
| 5883 | |
| 5884 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 5885 | if (!node->prev->next) { |
| 5886 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 5887 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 5888 | ret += 0x2; |
| 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5896 | 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] | 5897 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5898 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5899 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5900 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5901 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5902 | 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] | 5903 | return 1; |
| 5904 | } |
| 5905 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5906 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5907 | goto check_augment; |
| 5908 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5909 | while (parent) { |
| 5910 | /* stop conditions */ |
| 5911 | if (!mode) { |
| 5912 | /* stop on node that can be instantiated in data tree */ |
| 5913 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5914 | break; |
| 5915 | } |
| 5916 | } else { |
| 5917 | /* stop on the specified node */ |
| 5918 | if (parent == stop) { |
| 5919 | break; |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5924 | return 1; |
| 5925 | } |
| 5926 | check_augment: |
| 5927 | |
| 5928 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5929 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 5930 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5931 | } |
| 5932 | parent = lys_parent(parent); |
| 5933 | } |
| 5934 | |
| 5935 | return 0; |
| 5936 | } |
| 5937 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5938 | /** |
| 5939 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 5940 | * Logs directly. |
| 5941 | * |
| 5942 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5943 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5944 | * @return |
| 5945 | * -1 - error, ly_errno is set |
| 5946 | * 0 - true "when" statement |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5947 | * 0, ly_vecode = LYVE_NOWHEN - false "when" statement |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5948 | * 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] | 5949 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 5950 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5951 | resolve_when(struct lyd_node *node, int *result, int ignore_fail) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5952 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5953 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5954 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5955 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 5956 | enum lyxp_node_type ctx_node_type; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5957 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5958 | |
| 5959 | assert(node); |
| 5960 | memset(&set, 0, sizeof set); |
| 5961 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 5962 | 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] | 5963 | /* make the node dummy for the evaluation */ |
| 5964 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5965 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 5966 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 5967 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5968 | if (rc) { |
| 5969 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5970 | 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] | 5971 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5972 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5973 | } |
| 5974 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5975 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5976 | 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] | 5977 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5978 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 5979 | if ((ignore_fail == 1) || ((node->schema->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5980 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 5981 | ((struct lys_node_container *)node->schema)->when->cond); |
| 5982 | } else { |
| 5983 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
| 5984 | goto cleanup; |
| 5985 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5986 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 5987 | |
| 5988 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 5989 | 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] | 5990 | } |
| 5991 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5992 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5993 | goto check_augment; |
| 5994 | |
| 5995 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5996 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 5997 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5998 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 5999 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6000 | if (rc) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6001 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6002 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6003 | } |
| 6004 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6005 | |
| 6006 | unlinked_nodes = NULL; |
| 6007 | /* we do not want our node pointer to change */ |
| 6008 | tmp_node = node; |
| 6009 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6010 | if (rc) { |
| 6011 | goto cleanup; |
| 6012 | } |
| 6013 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6014 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6015 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6016 | |
| 6017 | if (unlinked_nodes && ctx_node) { |
| 6018 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6019 | rc = -1; |
| 6020 | goto cleanup; |
| 6021 | } |
| 6022 | } |
| 6023 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6024 | if (rc) { |
| 6025 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6026 | 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] | 6027 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6028 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6029 | } |
| 6030 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6031 | 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] | 6032 | if (!set.val.bool) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6033 | if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6034 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6035 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6036 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6037 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6038 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
| 6039 | goto cleanup; |
| 6040 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6041 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6042 | |
| 6043 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6044 | 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] | 6045 | } |
| 6046 | |
| 6047 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6048 | 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] | 6049 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6050 | 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] | 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 | tmp_node = node; |
| 6059 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6060 | if (rc) { |
| 6061 | goto cleanup; |
| 6062 | } |
| 6063 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6064 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6065 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6066 | |
| 6067 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6068 | * so the tree did not actually change and there is nothing for us to do |
| 6069 | */ |
| 6070 | if (unlinked_nodes && ctx_node) { |
| 6071 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6072 | rc = -1; |
| 6073 | goto cleanup; |
| 6074 | } |
| 6075 | } |
| 6076 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6077 | if (rc) { |
| 6078 | if (rc == 1) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6079 | 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] | 6080 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6081 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6082 | } |
| 6083 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6084 | 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] | 6085 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6086 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6087 | if ((ignore_fail == 1) || ((sparent->parent->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6088 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6089 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6090 | } else { |
| 6091 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
| 6092 | goto cleanup; |
| 6093 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6094 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6095 | |
| 6096 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6097 | 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] | 6098 | } |
| 6099 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6100 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6101 | } |
| 6102 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6103 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6104 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6105 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6106 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6107 | 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] | 6108 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6109 | if (result) { |
| 6110 | if (node->when_status & LYD_WHEN_TRUE) { |
| 6111 | *result = 1; |
| 6112 | } else { |
| 6113 | *result = 0; |
| 6114 | } |
| 6115 | } |
| 6116 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6117 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6118 | } |
| 6119 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6120 | static int |
| 6121 | check_leafref_features(struct lys_type *type) |
| 6122 | { |
| 6123 | struct lys_node *iter; |
| 6124 | struct ly_set *src_parents, *trg_parents, *features; |
| 6125 | unsigned int i, j, size, x; |
| 6126 | int ret = EXIT_SUCCESS; |
| 6127 | |
| 6128 | assert(type->parent); |
| 6129 | |
| 6130 | src_parents = ly_set_new(); |
| 6131 | trg_parents = ly_set_new(); |
| 6132 | features = ly_set_new(); |
| 6133 | |
| 6134 | /* get parents chain of source (leafref) */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6135 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6136 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6137 | continue; |
| 6138 | } |
| 6139 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 6140 | } |
| 6141 | /* get parents chain of target */ |
Radek Krejci | ecda01a | 2017-04-05 15:44:27 +0200 | [diff] [blame] | 6142 | 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] | 6143 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 6144 | continue; |
| 6145 | } |
| 6146 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 6147 | } |
| 6148 | |
| 6149 | /* compare the features used in if-feature statements in the rest of both |
| 6150 | * chains of parents. The set of features used for target must be a subset |
| 6151 | * of features used for the leafref. This is not a perfect, we should compare |
| 6152 | * the truth tables but it could require too much resources, so we simplify that */ |
| 6153 | for (i = 0; i < src_parents->number; i++) { |
| 6154 | iter = src_parents->set.s[i]; /* shortcut */ |
| 6155 | if (!iter->iffeature_size) { |
| 6156 | continue; |
| 6157 | } |
| 6158 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6159 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6160 | for (; size; size--) { |
| 6161 | if (!iter->iffeature[j].features[size - 1]) { |
| 6162 | /* not yet resolved feature, postpone this check */ |
| 6163 | ret = EXIT_FAILURE; |
| 6164 | goto cleanup; |
| 6165 | } |
| 6166 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 6167 | } |
| 6168 | } |
| 6169 | } |
| 6170 | x = features->number; |
| 6171 | for (i = 0; i < trg_parents->number; i++) { |
| 6172 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 6173 | if (!iter->iffeature_size) { |
| 6174 | continue; |
| 6175 | } |
| 6176 | for (j = 0; j < iter->iffeature_size; j++) { |
| 6177 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 6178 | for (; size; size--) { |
| 6179 | if (!iter->iffeature[j].features[size - 1]) { |
| 6180 | /* not yet resolved feature, postpone this check */ |
| 6181 | ret = EXIT_FAILURE; |
| 6182 | goto cleanup; |
| 6183 | } |
| 6184 | if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 6185 | /* the feature is not present in features set of target's parents chain */ |
| 6186 | 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] | 6187 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6188 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 6189 | iter->iffeature[j].features[size - 1]->name); |
| 6190 | ret = -1; |
| 6191 | goto cleanup; |
| 6192 | } |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | |
| 6197 | cleanup: |
| 6198 | ly_set_free(features); |
| 6199 | ly_set_free(src_parents); |
| 6200 | ly_set_free(trg_parents); |
| 6201 | |
| 6202 | return ret; |
| 6203 | } |
| 6204 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6205 | static int |
| 6206 | check_type_union_leafref(struct lys_type *type) |
| 6207 | { |
| 6208 | uint8_t i; |
| 6209 | |
| 6210 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6211 | /* go through unions and look for leafref */ |
| 6212 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6213 | switch (type->info.uni.types[i].base) { |
| 6214 | case LY_TYPE_LEAFREF: |
| 6215 | return 1; |
| 6216 | case LY_TYPE_UNION: |
| 6217 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6218 | return 1; |
| 6219 | } |
| 6220 | break; |
| 6221 | default: |
| 6222 | break; |
| 6223 | } |
| 6224 | } |
| 6225 | |
| 6226 | return 0; |
| 6227 | } |
| 6228 | |
| 6229 | /* just inherit the flag value */ |
| 6230 | return type->der->has_union_leafref; |
| 6231 | } |
| 6232 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6233 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6234 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6235 | * |
| 6236 | * @param[in] mod Main module. |
| 6237 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6238 | * @param[in] type Type of the unresolved item. |
| 6239 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6240 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6241 | * @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] | 6242 | * |
| 6243 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6244 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6245 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6246 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6247 | struct unres_schema *unres, int final_fail) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6248 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6249 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6250 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6251 | unsigned int j; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6252 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6253 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6254 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6255 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6256 | struct ly_set *refs, *procs; |
| 6257 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6258 | struct lys_ident *ident; |
| 6259 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6260 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6261 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6262 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6263 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6264 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6265 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6266 | struct lys_ext_instance *ext, **extlist; |
| 6267 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6268 | |
| 6269 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6270 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6271 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6272 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6273 | ident = item; |
| 6274 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6275 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6276 | break; |
| 6277 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6278 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6279 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6280 | stype = item; |
| 6281 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6282 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6283 | break; |
| 6284 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6285 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6286 | stype = item; |
| 6287 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 6288 | rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target); |
| 6289 | if (!rc) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 6290 | assert(stype->info.lref.target); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6291 | /* check if leafref and its target are under a common if-features */ |
| 6292 | rc = check_leafref_features(stype); |
| 6293 | if (rc) { |
| 6294 | break; |
| 6295 | } |
| 6296 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6297 | if (lys_node_module(node)->implemented) { |
| 6298 | /* make all the modules on the path implemented */ |
| 6299 | for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) { |
| 6300 | if (!lys_node_module(next)->implemented) { |
| 6301 | if (lys_set_implemented(lys_node_module(next))) { |
| 6302 | rc = -1; |
| 6303 | break; |
| 6304 | } |
| 6305 | } |
| 6306 | } |
| 6307 | if (next) { |
| 6308 | break; |
| 6309 | } |
| 6310 | |
| 6311 | /* store the backlink from leafref target */ |
| 6312 | if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) { |
| 6313 | rc = -1; |
| 6314 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6315 | } |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 6316 | } |
| 6317 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6318 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6319 | case UNRES_TYPE_DER_EXT: |
| 6320 | parent_type++; |
| 6321 | /* no break */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6322 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6323 | parent_type++; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6324 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6325 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6326 | /* parent */ |
| 6327 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6328 | stype = item; |
| 6329 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6330 | /* HACK type->der is temporarily unparsed type statement */ |
| 6331 | yin = (struct lyxml_elem *)stype->der; |
| 6332 | stype->der = NULL; |
| 6333 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6334 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6335 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6336 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6337 | |
| 6338 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6339 | /* may try again later */ |
| 6340 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6341 | } else { |
| 6342 | /* 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] | 6343 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6344 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6345 | } |
| 6346 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6347 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6348 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6349 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6350 | /* we need to always be able to free this, it's safe only in this case */ |
| 6351 | lyxml_free(mod->ctx, yin); |
| 6352 | } else { |
| 6353 | /* may try again later, put all back how it was */ |
| 6354 | stype->der = (struct lys_tpdf *)yin; |
| 6355 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6356 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6357 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6358 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6359 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6360 | LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
| 6361 | } |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6362 | |
| 6363 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6364 | /* fill typedef union leafref flag */ |
| 6365 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6366 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6367 | /* copy the type in case it has union leafref flag */ |
| 6368 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
| 6369 | LOGERR(LY_EINT, "Failed to duplicate type."); |
| 6370 | return -1; |
| 6371 | } |
| 6372 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6373 | } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6374 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6375 | * 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] | 6376 | * grouping. The grouping cannot be used unless the unres counter is 0. |
| 6377 | * 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] | 6378 | * of the type's base member. */ |
| 6379 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6380 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6381 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
| 6382 | LOGERR(LY_EINT, "Too many unresolved items (type) inside a grouping."); |
| 6383 | return -1; |
| 6384 | } |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 6385 | stype->base = LY_TYPE_ERR; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6386 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6387 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6388 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6389 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6390 | iff_data = str_snode; |
| 6391 | 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] | 6392 | if (!rc) { |
| 6393 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6394 | if (iff_data->infeature) { |
| 6395 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6396 | feat = *((struct lys_feature **)item); |
| 6397 | if (!feat->depfeatures) { |
| 6398 | feat->depfeatures = ly_set_new(); |
| 6399 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6400 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6401 | } |
| 6402 | /* cleanup temporary data */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6403 | lydict_remove(mod->ctx, iff_data->fname); |
| 6404 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6405 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6406 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6407 | case UNRES_FEATURE: |
| 6408 | feat = (struct lys_feature *)item; |
| 6409 | |
| 6410 | if (feat->iffeature_size) { |
| 6411 | refs = ly_set_new(); |
| 6412 | procs = ly_set_new(); |
| 6413 | ly_set_add(procs, feat, 0); |
| 6414 | |
| 6415 | while (procs->number) { |
| 6416 | ref = procs->set.g[procs->number - 1]; |
| 6417 | ly_set_rm_index(procs, procs->number - 1); |
| 6418 | |
| 6419 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6420 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6421 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6422 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6423 | if (ref->iffeature[i].features[j - 1] == feat) { |
| 6424 | LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
| 6425 | goto featurecheckdone; |
| 6426 | } |
| 6427 | |
| 6428 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6429 | k = refs->number; |
| 6430 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6431 | /* not yet seen feature, add it for processing */ |
| 6432 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6433 | } |
| 6434 | } |
| 6435 | } else { |
| 6436 | /* forward reference */ |
| 6437 | rc = EXIT_FAILURE; |
| 6438 | goto featurecheckdone; |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | } |
| 6443 | } |
| 6444 | rc = EXIT_SUCCESS; |
| 6445 | |
| 6446 | featurecheckdone: |
| 6447 | ly_set_free(refs); |
| 6448 | ly_set_free(procs); |
| 6449 | } |
| 6450 | |
| 6451 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6452 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6453 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6454 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6455 | case UNRES_TYPEDEF_DFLT: |
| 6456 | parent_type++; |
| 6457 | /* no break */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6458 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6459 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6460 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6461 | break; |
| 6462 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6463 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6464 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6465 | choic = item; |
| 6466 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6467 | if (!choic->dflt) { |
| 6468 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6469 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6470 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6471 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6472 | } else { |
| 6473 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6474 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6475 | break; |
| 6476 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6477 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6478 | break; |
| 6479 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6480 | unique_info = (struct unres_list_uniq *)item; |
| 6481 | 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] | 6482 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6483 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6484 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6485 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6486 | case UNRES_XPATH: |
| 6487 | node = (struct lys_node *)item; |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6488 | rc = lys_check_xpath(node, 1, final_fail); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6489 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6490 | case UNRES_EXT: |
| 6491 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6492 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6493 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6494 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6495 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6496 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6497 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6498 | if (eplugin) { |
| 6499 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6500 | u = malloc(sizeof *u); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6501 | LY_CHECK_ERR_RETURN(!u, LOGMEM, -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6502 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6503 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6504 | /* something really bad happend since the extension finalization is not actually |
| 6505 | * being resolved while adding into unres, so something more serious with the unres |
| 6506 | * list itself must happened */ |
| 6507 | return -1; |
| 6508 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6509 | } |
| 6510 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6511 | } |
| 6512 | if (!rc || rc == -1) { |
| 6513 | /* cleanup on success or fatal error */ |
| 6514 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6515 | /* YIN */ |
| 6516 | lyxml_free(mod->ctx, ext_data->data.yin); |
| 6517 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 6518 | /* YANG */ |
| 6519 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6520 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6521 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6522 | } |
| 6523 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6524 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6525 | u = (uint8_t *)str_snode; |
| 6526 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 6527 | free(u); |
| 6528 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6529 | eplugin = ext->def->plugin; |
| 6530 | |
| 6531 | /* inherit */ |
| 6532 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 6533 | root = (struct lys_node *)ext->parent; |
| 6534 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6535 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 6536 | /* first, check if the node already contain instance of the same extension, |
| 6537 | * in such a case we won't inherit. In case the node was actually defined as |
| 6538 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 6539 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 6540 | goto inherit_dfs_sibling; |
| 6541 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 6542 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 6543 | goto inherit_dfs_sibling; |
| 6544 | } |
| 6545 | |
| 6546 | if (eplugin->check_inherit) { |
| 6547 | /* we have a callback to check the inheritance, use it */ |
| 6548 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 6549 | case 0: |
| 6550 | /* yes - continue with the inheriting code */ |
| 6551 | break; |
| 6552 | case 1: |
| 6553 | /* no - continue with the node's sibling */ |
| 6554 | goto inherit_dfs_sibling; |
| 6555 | case 2: |
| 6556 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 6557 | goto inherit_dfs_child; |
| 6558 | default: |
| 6559 | LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
| 6560 | ext->def->module->name, ext->def->name, rc); |
| 6561 | } |
| 6562 | } |
| 6563 | |
| 6564 | /* inherit the extension */ |
| 6565 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6566 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6567 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6568 | 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] | 6569 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 6570 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 6571 | |
| 6572 | node->ext = extlist; |
| 6573 | node->ext_size++; |
| 6574 | |
| 6575 | inherit_dfs_child: |
| 6576 | /* modification of - select element for the next run - children first */ |
| 6577 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 6578 | next = NULL; |
| 6579 | } else { |
| 6580 | next = node->child; |
| 6581 | } |
| 6582 | if (!next) { |
| 6583 | inherit_dfs_sibling: |
| 6584 | /* no children, try siblings */ |
| 6585 | next = node->next; |
| 6586 | } |
| 6587 | while (!next) { |
| 6588 | /* go to the parent */ |
| 6589 | node = lys_parent(node); |
| 6590 | |
| 6591 | /* we are done if we are back in the root (the starter's parent */ |
| 6592 | if (node == root) { |
| 6593 | break; |
| 6594 | } |
| 6595 | |
| 6596 | /* parent is already processed, go to its sibling */ |
| 6597 | next = node->next; |
| 6598 | } |
| 6599 | } |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | /* final check */ |
| 6604 | if (eplugin->check_result) { |
| 6605 | if ((*eplugin->check_result)(ext)) { |
Radek Krejci | 2c121b3 | 2017-02-24 10:03:16 +0100 | [diff] [blame] | 6606 | ly_errno = LY_EEXT; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6607 | return -1; |
| 6608 | } |
| 6609 | } |
| 6610 | |
| 6611 | rc = 0; |
| 6612 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6613 | default: |
| 6614 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6615 | break; |
| 6616 | } |
| 6617 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6618 | if (has_str && !rc) { |
| 6619 | /* the string is no more needed in case of success. |
| 6620 | * 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] | 6621 | lydict_remove(mod->ctx, str_snode); |
| 6622 | } |
| 6623 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6624 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6625 | } |
| 6626 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6627 | /* logs directly */ |
| 6628 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6629 | 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] | 6630 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6631 | struct lyxml_elem *xml; |
| 6632 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6633 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6634 | const char *name = NULL; |
| 6635 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6636 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6637 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6638 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6639 | 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] | 6640 | break; |
| 6641 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6642 | 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] | 6643 | break; |
| 6644 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6645 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 6646 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6647 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6648 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6649 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6650 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6651 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6652 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6653 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6654 | } else { |
| 6655 | LY_TREE_FOR(xml->attr, attr) { |
| 6656 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6657 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 6658 | break; |
| 6659 | } |
| 6660 | } |
| 6661 | assert(attr); |
| 6662 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6663 | 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] | 6664 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6665 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6666 | iff_data = str_node; |
| 6667 | 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] | 6668 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6669 | case UNRES_FEATURE: |
| 6670 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 6671 | ((struct lys_feature *)item)->name); |
| 6672 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6673 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6674 | 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] | 6675 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6676 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6677 | case UNRES_TYPE_DFLT: |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 6678 | if (str_node) { |
| 6679 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
| 6680 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 6681 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 6682 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6683 | break; |
| 6684 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6685 | 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] | 6686 | break; |
| 6687 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6688 | 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] | 6689 | break; |
| 6690 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6691 | 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] | 6692 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6693 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6694 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 6695 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6696 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6697 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 6698 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 6699 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6700 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6701 | case UNRES_EXT: |
| 6702 | extinfo = (struct unres_ext *)str_node; |
| 6703 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 6704 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 6705 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6706 | default: |
| 6707 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6708 | break; |
| 6709 | } |
| 6710 | } |
| 6711 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6712 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6713 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6714 | * |
| 6715 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6716 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6717 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6718 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6719 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6720 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6721 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6722 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6723 | uint32_t i, resolved = 0, unres_count, res_count; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6724 | struct lyxml_elem *yin; |
| 6725 | struct yang_type *yang; |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6726 | int rc, log_hidden; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6727 | |
| 6728 | assert(unres); |
| 6729 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6730 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6731 | if (*ly_vlog_hide_location()) { |
| 6732 | log_hidden = 1; |
| 6733 | } else { |
| 6734 | log_hidden = 0; |
| 6735 | ly_vlog_hide(1); |
| 6736 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6737 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6738 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6739 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6740 | unres_count = 0; |
| 6741 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6742 | |
| 6743 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6744 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6745 | * if-features are resolved here to make sure that we will have all if-features for |
| 6746 | * later check of feature circular dependency */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6747 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6748 | continue; |
| 6749 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6750 | /* 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] | 6751 | * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6752 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6753 | ++unres_count; |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6754 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6755 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6756 | unres->type[i] = UNRES_RESOLVED; |
| 6757 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6758 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 6759 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6760 | if (!log_hidden) { |
| 6761 | ly_vlog_hide(0); |
| 6762 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6763 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6764 | ly_err_repeat(); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6765 | return -1; |
Radek Krejci | c2a180f | 2016-06-22 13:28:16 +0200 | [diff] [blame] | 6766 | } else { |
| 6767 | /* forward reference, erase ly_errno */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 6768 | ly_err_clean(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6769 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6770 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6771 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 6772 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6773 | if (res_count < unres_count) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6774 | /* just print the errors */ |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6775 | if (!log_hidden) { |
| 6776 | ly_vlog_hide(0); |
| 6777 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6778 | |
| 6779 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6780 | if (unres->type[i] > UNRES_IDENT) { |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6781 | continue; |
| 6782 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6783 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6784 | if (unres->type[i] == UNRES_TYPE_DER_EXT) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 6785 | yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der; |
| 6786 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6787 | yang =(struct yang_type *)yin; |
| 6788 | ((struct lys_type *)unres->item[i])->base = yang->base; |
| 6789 | if (yang->base == LY_TYPE_UNION) { |
| 6790 | yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]); |
| 6791 | } |
| 6792 | lydict_remove(mod->ctx, yang->name); |
| 6793 | free(yang); |
| 6794 | } else { |
| 6795 | lyxml_free(mod->ctx, yin); |
| 6796 | } |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6797 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6798 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 6799 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6800 | } |
| 6801 | |
Radek Krejci | 07d0fb9 | 2017-01-13 14:11:05 +0100 | [diff] [blame] | 6802 | /* the rest except finalizing extensions */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6803 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6804 | if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6805 | continue; |
| 6806 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 6807 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6808 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6809 | if (rc == 0) { |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 6810 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 6811 | /* free the allocated structure */ |
| 6812 | free(unres->item[i]); |
| 6813 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6814 | unres->type[i] = UNRES_RESOLVED; |
| 6815 | ++resolved; |
| 6816 | } else if (rc == -1) { |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6817 | if (!log_hidden) { |
| 6818 | ly_vlog_hide(0); |
| 6819 | } |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6820 | /* print the error */ |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6821 | ly_err_repeat(); |
Michal Vasko | 22af5ca | 2016-05-20 11:44:02 +0200 | [diff] [blame] | 6822 | return -1; |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6823 | } else { |
| 6824 | /* forward reference, erase ly_errno */ |
| 6825 | ly_err_clean(1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6826 | } |
| 6827 | } |
| 6828 | |
Michal Vasko | 74a60c0 | 2017-03-08 10:19:48 +0100 | [diff] [blame] | 6829 | if (!log_hidden) { |
| 6830 | ly_vlog_hide(0); |
| 6831 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6832 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6833 | /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 6834 | for (i = 0; i < unres->count; ++i) { |
| 6835 | if (unres->type[i] != UNRES_EXT_FINALIZE) { |
| 6836 | continue; |
| 6837 | } |
| 6838 | |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6839 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0); |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6840 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6841 | if (rc == 0) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6842 | ++resolved; |
| 6843 | } |
Radek Krejci | 791f6c7 | 2017-02-22 15:23:39 +0100 | [diff] [blame] | 6844 | /* else error - it was already printed, but resolved was not increased, |
| 6845 | so this unres item will not be resolved again in the following code, |
| 6846 | but it will cause returning -1 at the end, this way we are able to |
| 6847 | print all the issues with unres */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6848 | } |
| 6849 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6850 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6851 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 6852 | * all the validation errors |
| 6853 | */ |
| 6854 | for (i = 0; i < unres->count; ++i) { |
| 6855 | if (unres->type[i] == UNRES_RESOLVED) { |
| 6856 | continue; |
| 6857 | } |
Radek Krejci | cd9a95a | 2017-03-25 12:02:35 -0500 | [diff] [blame] | 6858 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1); |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6859 | if (unres->type[i] == UNRES_XPATH) { |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6860 | /* 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] | 6861 | unres->type[i] = UNRES_RESOLVED; |
| 6862 | resolved++; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6863 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6864 | } |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6865 | if (resolved < unres->count) { |
| 6866 | return -1; |
| 6867 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6868 | } |
| 6869 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 6870 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6871 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6872 | return EXIT_SUCCESS; |
| 6873 | } |
| 6874 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6875 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6876 | * @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] | 6877 | * |
| 6878 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6879 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6880 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6881 | * @param[in] type Type of the unresolved item. |
| 6882 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6883 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6884 | * @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] | 6885 | */ |
| 6886 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6887 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 6888 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6889 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6890 | int rc; |
| 6891 | const char *dictstr; |
| 6892 | |
| 6893 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 6894 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 6895 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6896 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 6897 | lydict_remove(mod->ctx, dictstr); |
| 6898 | } |
| 6899 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6900 | } |
| 6901 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6902 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6903 | * @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] | 6904 | * |
| 6905 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6906 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6907 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6908 | * @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] | 6909 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6910 | * |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 6911 | * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item |
| 6912 | * is already in the unres list. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6913 | */ |
| 6914 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6915 | 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] | 6916 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6917 | { |
Michal Vasko | ef486d7 | 2016-09-27 12:10:44 +0200 | [diff] [blame] | 6918 | int rc, log_hidden; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6919 | struct lyxml_elem *yin; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6920 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 6921 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 6922 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6923 | |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6924 | #ifndef NDEBUG |
Radek Krejci | df056df | 2017-03-09 13:24:45 +0100 | [diff] [blame] | 6925 | uint32_t u; |
| 6926 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6927 | /* check for duplicities in unres */ |
| 6928 | for (u = 0; u < unres->count; u++) { |
| 6929 | if (unres->type[u] == type && unres->item[u] == item && |
| 6930 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6931 | /* duplication, should not happen */ |
| 6932 | assert(0); |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6933 | } |
| 6934 | } |
Michal Vasko | 9e862e8 | 2017-03-08 10:20:49 +0100 | [diff] [blame] | 6935 | #endif |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 6936 | |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6937 | if (type == UNRES_EXT_FINALIZE) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6938 | /* extension finalization is not even tried when adding the item into the inres list */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 6939 | rc = EXIT_FAILURE; |
| 6940 | } else { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6941 | if (*ly_vlog_hide_location()) { |
| 6942 | log_hidden = 1; |
| 6943 | } else { |
| 6944 | log_hidden = 0; |
| 6945 | ly_vlog_hide(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6946 | } |
Radek Krejci | cbba57c | 2017-01-24 13:43:20 +0100 | [diff] [blame] | 6947 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6948 | if (!log_hidden) { |
| 6949 | ly_vlog_hide(0); |
| 6950 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6951 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6952 | if (rc != EXIT_FAILURE) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6953 | if (rc == -1) { |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6954 | ly_err_repeat(); |
| 6955 | } |
| 6956 | if (type == UNRES_LIST_UNIQ) { |
| 6957 | /* free the allocated structure */ |
| 6958 | free(item); |
| 6959 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 6960 | /* free the allocated resources */ |
| 6961 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6962 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6963 | return rc; |
| 6964 | } else { |
| 6965 | /* erase info about validation errors */ |
| 6966 | ly_err_clean(1); |
| 6967 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6968 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6969 | print_unres_schema_item_fail(item, type, snode); |
| 6970 | |
| 6971 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 6972 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 6973 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 6974 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 6975 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 6976 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 6977 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6978 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6979 | } |
| 6980 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6981 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6982 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6983 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6984 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6985 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6986 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6987 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 6988 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6989 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6990 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6991 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 6992 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 6993 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6994 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 6995 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6996 | } |
| 6997 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6998 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6999 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7000 | * |
| 7001 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7002 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7003 | * @param[in] item Old item to be resolved. |
| 7004 | * @param[in] type Type of the old unresolved item. |
| 7005 | * @param[in] new_item New item to use in the duplicate. |
| 7006 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7007 | * @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] | 7008 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7009 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7010 | 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] | 7011 | { |
| 7012 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7013 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7014 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7015 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7016 | 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] | 7017 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7018 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7019 | if (type == UNRES_LIST_UNIQ) { |
| 7020 | aux_uniq.list = item; |
| 7021 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7022 | item = &aux_uniq; |
| 7023 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7024 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7025 | |
| 7026 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7027 | if (type == UNRES_LIST_UNIQ) { |
| 7028 | free(new_item); |
| 7029 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7030 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7031 | } |
| 7032 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7033 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7034 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7035 | 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] | 7036 | LOGINT; |
| 7037 | return -1; |
| 7038 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7039 | } else if (type == UNRES_IFFEAT) { |
| 7040 | /* duplicate unres_iffeature_data */ |
| 7041 | iff_data = malloc(sizeof *iff_data); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7042 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7043 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7044 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7045 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
| 7046 | LOGINT; |
| 7047 | return -1; |
| 7048 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7049 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7050 | 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] | 7051 | LOGINT; |
| 7052 | return -1; |
| 7053 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7054 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7055 | |
| 7056 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7057 | } |
| 7058 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7059 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7060 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7061 | 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] | 7062 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7063 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7064 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7065 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7066 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7067 | i = start_on_backwards; |
| 7068 | } else { |
| 7069 | i = unres->count - 1; |
| 7070 | } |
| 7071 | for (; i > -1; i--) { |
| 7072 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7073 | continue; |
| 7074 | } |
| 7075 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7076 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7077 | break; |
| 7078 | } |
| 7079 | } else { |
| 7080 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7081 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7082 | 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] | 7083 | break; |
| 7084 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7085 | } |
| 7086 | } |
| 7087 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7088 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7089 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7090 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7091 | static void |
| 7092 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7093 | { |
| 7094 | struct lyxml_elem *yin; |
| 7095 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7096 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7097 | |
| 7098 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7099 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7100 | case UNRES_TYPE_DER: |
| 7101 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7102 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7103 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7104 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7105 | lydict_remove(ctx, yang->name); |
| 7106 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7107 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7108 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7109 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7110 | } else { |
| 7111 | lyxml_free(ctx, yin); |
| 7112 | } |
| 7113 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7114 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7115 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7116 | lydict_remove(ctx, iff_data->fname); |
| 7117 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7118 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7119 | case UNRES_IDENT: |
| 7120 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7121 | case UNRES_CHOICE_DFLT: |
| 7122 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7123 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7124 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7125 | case UNRES_LIST_UNIQ: |
| 7126 | free(unres->item[i]); |
| 7127 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7128 | case UNRES_EXT: |
| 7129 | free(unres->str_snode[i]); |
| 7130 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7131 | default: |
| 7132 | break; |
| 7133 | } |
| 7134 | unres->type[i] = UNRES_RESOLVED; |
| 7135 | } |
| 7136 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7137 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7138 | 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] | 7139 | { |
| 7140 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7141 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7142 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7143 | if (!unres || !(*unres)) { |
| 7144 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7145 | } |
| 7146 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7147 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7148 | |
| 7149 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7150 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7151 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7152 | unresolved++; |
| 7153 | } |
| 7154 | continue; |
| 7155 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7156 | |
| 7157 | /* free heap memory for the specific item */ |
| 7158 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7159 | } |
| 7160 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7161 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7162 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7163 | free((*unres)->item); |
| 7164 | free((*unres)->type); |
| 7165 | free((*unres)->str_snode); |
| 7166 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7167 | free((*unres)); |
| 7168 | (*unres) = NULL; |
| 7169 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7170 | } |
| 7171 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7172 | static int |
| 7173 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7174 | { |
| 7175 | struct ly_set *set; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7176 | struct lys_node *op_node, *first_node; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7177 | char *buf; |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7178 | int ret = 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7179 | |
| 7180 | for (op_node = lys_parent(sleaf); |
| 7181 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7182 | op_node = lys_parent(op_node)); |
| 7183 | |
| 7184 | if (op_node && lys_parent(op_node)) { |
| 7185 | /* nested operation - any absolute path is external */ |
| 7186 | return 1; |
| 7187 | } |
| 7188 | |
| 7189 | /* get the first node from the instid */ |
| 7190 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7191 | if (!buf) { |
| 7192 | LOGMEM; |
| 7193 | return -1; |
| 7194 | } |
| 7195 | |
| 7196 | /* there is a predicate, remove it */ |
| 7197 | if (buf[strlen(buf) - 1] == ']') { |
| 7198 | assert(strchr(buf, '[')); |
| 7199 | *strchr(buf, '[') = '\0'; |
| 7200 | } |
| 7201 | |
| 7202 | /* find the first schema node */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 7203 | set = lys_find_path(NULL, sleaf, buf); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7204 | if (!set || !set->number) { |
| 7205 | free(buf); |
Michal Vasko | 29fd974 | 2017-01-23 09:55:44 +0100 | [diff] [blame] | 7206 | ly_set_free(set); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7207 | return 1; |
| 7208 | } |
| 7209 | free(buf); |
| 7210 | |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7211 | first_node = set->set.s[0]; |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7212 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7213 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7214 | |
| 7215 | if (op_node) { |
| 7216 | /* it is an operation, so we're good if it points somewhere inside it */ |
Michal Vasko | 3c77709 | 2017-01-17 14:10:09 +0100 | [diff] [blame] | 7217 | if (op_node == first_node) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7218 | assert(set->number == 1); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7219 | } else { |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7220 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7224 | /* cleanup */ |
| 7225 | ly_set_free(set); |
| 7226 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7227 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7228 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7229 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7230 | } |
| 7231 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7232 | /** |
| 7233 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7234 | * |
| 7235 | * @param[in] data Data node where the path is used |
| 7236 | * @param[in] path Instance-identifier node value. |
| 7237 | * @param[in,out] ret Resolved instance or NULL. |
| 7238 | * |
| 7239 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7240 | */ |
| 7241 | static int |
| 7242 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7243 | { |
| 7244 | int i = 0, j; |
| 7245 | const struct lys_module *mod; |
| 7246 | struct ly_ctx *ctx = data->schema->module->ctx; |
| 7247 | const char *model, *name; |
| 7248 | char *str; |
| 7249 | int mod_len, name_len, has_predicate; |
| 7250 | struct unres_data node_match; |
| 7251 | |
| 7252 | memset(&node_match, 0, sizeof node_match); |
| 7253 | *ret = NULL; |
| 7254 | |
| 7255 | /* we need root to resolve absolute path */ |
| 7256 | for (; data->parent; data = data->parent); |
| 7257 | /* we're still parsing it and the pointer is not correct yet */ |
| 7258 | if (data->prev) { |
| 7259 | for (; data->prev->next; data = data->prev); |
| 7260 | } |
| 7261 | |
| 7262 | /* search for the instance node */ |
| 7263 | while (path[i]) { |
| 7264 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7265 | if (j <= 0) { |
| 7266 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
| 7267 | goto error; |
| 7268 | } |
| 7269 | i += j; |
| 7270 | |
| 7271 | str = strndup(model, mod_len); |
| 7272 | if (!str) { |
| 7273 | LOGMEM; |
| 7274 | goto error; |
| 7275 | } |
| 7276 | mod = ly_ctx_get_module(ctx, str, NULL); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7277 | if (ctx->data_clb) { |
| 7278 | if (!mod) { |
| 7279 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7280 | } else if (!mod->implemented) { |
| 7281 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7282 | } |
| 7283 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7284 | free(str); |
| 7285 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7286 | if (!mod || !mod->implemented || mod->disabled) { |
| 7287 | break; |
| 7288 | } |
| 7289 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7290 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
| 7291 | /* no instance exists */ |
| 7292 | break; |
| 7293 | } |
| 7294 | |
| 7295 | if (has_predicate) { |
| 7296 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 7297 | j = resolve_instid_predicate(&path[i], &node_match); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7298 | if (j < 1) { |
| 7299 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
| 7300 | goto error; |
| 7301 | } |
| 7302 | i += j; |
| 7303 | |
| 7304 | if (!node_match.count) { |
| 7305 | /* no instance exists */ |
| 7306 | break; |
| 7307 | } |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7308 | } else if (node_match.count) { |
| 7309 | /* check that we are not addressing lists */ |
| 7310 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7311 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7312 | unres_data_del(&node_match, j--); |
| 7313 | } |
| 7314 | } |
| 7315 | if (!node_match.count) { |
| 7316 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys."); |
| 7317 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7318 | } |
| 7319 | } |
| 7320 | |
| 7321 | if (!node_match.count) { |
| 7322 | /* no instance exists */ |
| 7323 | if (req_inst > -1) { |
| 7324 | LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path); |
| 7325 | return EXIT_FAILURE; |
| 7326 | } |
| 7327 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7328 | return EXIT_SUCCESS; |
| 7329 | } else if (node_match.count > 1) { |
| 7330 | /* instance identifier must resolve to a single node */ |
| 7331 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
| 7332 | goto error; |
| 7333 | } else { |
| 7334 | /* we have required result, remember it and cleanup */ |
| 7335 | *ret = node_match.node[0]; |
| 7336 | free(node_match.node); |
| 7337 | return EXIT_SUCCESS; |
| 7338 | } |
| 7339 | |
| 7340 | error: |
| 7341 | /* cleanup */ |
| 7342 | free(node_match.node); |
| 7343 | return -1; |
| 7344 | } |
| 7345 | |
| 7346 | static int |
| 7347 | 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] | 7348 | { |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7349 | struct ly_set *set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7350 | uint32_t i; |
| 7351 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7352 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7353 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7354 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame^] | 7355 | set = lyd_find_path((struct lyd_node *)leaf, path); |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7356 | if (!set) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7357 | return -1; |
| 7358 | } |
| 7359 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7360 | for (i = 0; i < set->number; ++i) { |
| 7361 | if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7362 | continue; |
| 7363 | } |
| 7364 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7365 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7366 | * so we can simply compare just the values */ |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7367 | 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] | 7368 | /* we have the match */ |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7369 | *ret = set->set.d[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7370 | break; |
| 7371 | } |
| 7372 | } |
| 7373 | |
Michal Vasko | 86ae1f2 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7374 | ly_set_free(set); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7375 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7376 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7377 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7378 | if (req_inst > -1) { |
| 7379 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7380 | return EXIT_FAILURE; |
| 7381 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7382 | 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] | 7383 | } |
| 7384 | } |
| 7385 | |
| 7386 | return EXIT_SUCCESS; |
| 7387 | } |
| 7388 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7389 | /* 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] | 7390 | int |
| 7391 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7392 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7393 | { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7394 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7395 | struct lyd_node *ret; |
| 7396 | int found, hidden, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7397 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7398 | |
| 7399 | assert(type->base == LY_TYPE_UNION); |
| 7400 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7401 | if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) { |
| 7402 | /* either NULL or instid previously converted to JSON */ |
| 7403 | json_val = leaf->value.string; |
| 7404 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7405 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7406 | if (store) { |
| 7407 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7408 | free(leaf->value.bit); |
| 7409 | } |
| 7410 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7411 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7412 | |
| 7413 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
| 7414 | hidden = *ly_vlog_hide_location(); |
| 7415 | ly_vlog_hide(1); |
| 7416 | |
| 7417 | t = NULL; |
| 7418 | found = 0; |
| 7419 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7420 | found = 0; |
| 7421 | |
| 7422 | switch (t->base) { |
| 7423 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7424 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7425 | req_inst = -1; |
| 7426 | } else { |
| 7427 | req_inst = t->info.lref.req; |
| 7428 | } |
| 7429 | |
| 7430 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7431 | if (store) { |
| 7432 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7433 | /* valid resolved */ |
| 7434 | leaf->value.leafref = ret; |
| 7435 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7436 | } else { |
| 7437 | /* valid unresolved */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7438 | 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] | 7439 | return -1; |
| 7440 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7441 | } |
| 7442 | } |
| 7443 | |
| 7444 | success = 1; |
| 7445 | } |
| 7446 | break; |
| 7447 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7448 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7449 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7450 | req_inst = -1; |
| 7451 | } else { |
| 7452 | req_inst = t->info.inst.req; |
| 7453 | } |
| 7454 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7455 | 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] | 7456 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7457 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7458 | /* valid resolved */ |
| 7459 | leaf->value.instance = ret; |
| 7460 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7461 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7462 | if (json_val) { |
| 7463 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7464 | leaf->value_str = json_val; |
| 7465 | json_val = NULL; |
| 7466 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7467 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7468 | /* valid unresolved */ |
| 7469 | if (json_val) { |
| 7470 | /* put the JSON val back */ |
| 7471 | leaf->value.string = json_val; |
| 7472 | json_val = NULL; |
| 7473 | } else { |
| 7474 | leaf->value.instance = NULL; |
| 7475 | } |
| 7476 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7477 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7478 | } |
| 7479 | |
| 7480 | success = 1; |
| 7481 | } |
| 7482 | break; |
| 7483 | default: |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7484 | 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] | 7485 | success = 1; |
| 7486 | } |
| 7487 | break; |
| 7488 | } |
| 7489 | |
| 7490 | if (success) { |
| 7491 | break; |
| 7492 | } |
| 7493 | |
| 7494 | /* erase information about errors - they are false or irrelevant |
| 7495 | * and will be replaced by a single error messages */ |
| 7496 | ly_err_clean(1); |
| 7497 | |
| 7498 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7499 | if (store) { |
| 7500 | if (t->base == LY_TYPE_BITS) { |
| 7501 | free(leaf->value.bit); |
| 7502 | } |
| 7503 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7504 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | /* turn logging back on */ |
| 7508 | if (!hidden) { |
| 7509 | ly_vlog_hide(0); |
| 7510 | } |
| 7511 | |
| 7512 | if (json_val) { |
| 7513 | if (!success) { |
| 7514 | /* put the value back for now */ |
| 7515 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7516 | leaf->value.string = json_val; |
| 7517 | } else { |
| 7518 | /* value was ultimately useless, but we could not have known */ |
| 7519 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7520 | } |
| 7521 | } |
| 7522 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7523 | if (success) { |
| 7524 | if (resolved_type) { |
| 7525 | *resolved_type = t; |
| 7526 | } |
| 7527 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7528 | /* not found and it is required */ |
| 7529 | 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] | 7530 | return EXIT_FAILURE; |
| 7531 | } |
| 7532 | |
| 7533 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7534 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7535 | } |
| 7536 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7537 | /** |
| 7538 | * @brief Resolve a single unres data item. Logs directly. |
| 7539 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7540 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7541 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7542 | * @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] | 7543 | * |
| 7544 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7545 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7546 | int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7547 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7548 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7549 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7550 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7551 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7552 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7553 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 7554 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7555 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7556 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7557 | switch (type) { |
| 7558 | case UNRES_LEAFREF: |
| 7559 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7560 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7561 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7562 | req_inst = -1; |
| 7563 | } else { |
| 7564 | req_inst = sleaf->type.info.lref.req; |
| 7565 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7566 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 7567 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 7568 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7569 | /* valid resolved */ |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7570 | if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) { |
| 7571 | free(leaf->value.bit); |
| 7572 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7573 | leaf->value.leafref = ret; |
| 7574 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7575 | } else { |
| 7576 | /* valid unresolved */ |
| 7577 | if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) { |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 7578 | 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] | 7579 | return -1; |
| 7580 | } |
| 7581 | } |
| 7582 | } |
| 7583 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 7584 | } else { |
| 7585 | return rc; |
| 7586 | } |
| 7587 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7588 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7589 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7590 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7591 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 7592 | if (ext_dep == -1) { |
| 7593 | return -1; |
| 7594 | } |
| 7595 | |
| 7596 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7597 | req_inst = -1; |
| 7598 | } else { |
| 7599 | req_inst = sleaf->type.info.inst.req; |
| 7600 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7601 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 7602 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7603 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7604 | /* valid resolved */ |
| 7605 | leaf->value.instance = ret; |
| 7606 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7607 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7608 | /* valid unresolved */ |
| 7609 | leaf->value.instance = NULL; |
| 7610 | leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7611 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7612 | } else { |
| 7613 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7614 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7615 | break; |
| 7616 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7617 | case UNRES_UNION: |
| 7618 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7619 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7620 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7621 | case UNRES_WHEN: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7622 | if ((rc = resolve_when(node, NULL, ignore_fail))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7623 | return rc; |
| 7624 | } |
| 7625 | break; |
| 7626 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7627 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7628 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 7629 | return rc; |
| 7630 | } |
| 7631 | break; |
| 7632 | |
| 7633 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7634 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 7635 | return rc; |
| 7636 | } |
| 7637 | break; |
| 7638 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7639 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7640 | LOGINT; |
| 7641 | return -1; |
| 7642 | } |
| 7643 | |
| 7644 | return EXIT_SUCCESS; |
| 7645 | } |
| 7646 | |
| 7647 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7648 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7649 | * |
| 7650 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7651 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7652 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7653 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7654 | */ |
| 7655 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7656 | 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] | 7657 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7658 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 7659 | 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] | 7660 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7661 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7662 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7663 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7664 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7665 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7666 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 7667 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7668 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7669 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7670 | if (type == UNRES_WHEN) { |
| 7671 | /* remove previous result */ |
| 7672 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7673 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7674 | |
| 7675 | return EXIT_SUCCESS; |
| 7676 | } |
| 7677 | |
| 7678 | /** |
| 7679 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 7680 | * |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7681 | * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected, |
| 7682 | * unresolved leafrefs/instids are accepted). |
| 7683 | * |
| 7684 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 7685 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7686 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7687 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 7688 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7689 | * |
| 7690 | * @return EXIT_SUCCESS on success, -1 on error. |
| 7691 | */ |
| 7692 | int |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7693 | 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] | 7694 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7695 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7696 | int rc, progress, ignore_fail; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7697 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7698 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7699 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7700 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7701 | |
| 7702 | if (!unres->count) { |
| 7703 | return EXIT_SUCCESS; |
| 7704 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7705 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 7706 | 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] | 7707 | ignore_fail = 1; |
| 7708 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 7709 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7710 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7711 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7712 | } |
| 7713 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7714 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7715 | ly_vlog_hide(1); |
| 7716 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7717 | /* when-stmt first */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7718 | first = 1; |
| 7719 | stmt_count = 0; |
| 7720 | resolved = 0; |
| 7721 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7722 | do { |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7723 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7724 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 7725 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7726 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7727 | continue; |
| 7728 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7729 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7730 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7731 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7732 | } |
| 7733 | |
| 7734 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 7735 | for (parent = unres->node[i]->parent; |
| 7736 | parent && LYD_WHEN_DONE(parent->when_status); |
| 7737 | parent = parent->parent) { |
| 7738 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 7739 | /* the parent node was already unlinked, do not resolve this node, |
| 7740 | * it will be removed anyway, so just mark it as resolved |
| 7741 | */ |
| 7742 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 7743 | unres->type[i] = UNRES_RESOLVED; |
| 7744 | resolved++; |
| 7745 | break; |
| 7746 | } |
| 7747 | } |
| 7748 | if (parent) { |
| 7749 | continue; |
| 7750 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7751 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7752 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7753 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7754 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7755 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7756 | /* false when condition */ |
| 7757 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7758 | ly_err_repeat(); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7759 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7760 | } /* follows else */ |
| 7761 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 7762 | /* auto-delete */ |
| 7763 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 7764 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
| 7765 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7766 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 7767 | /* if it has parent non-presence containers that would be empty, we should actually |
| 7768 | * remove the container |
| 7769 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7770 | for (parent = unres->node[i]; |
| 7771 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 7772 | parent = parent->parent) { |
| 7773 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 7774 | /* presence container */ |
| 7775 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7776 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7777 | if (parent->next || parent->prev != parent) { |
| 7778 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 7779 | break; |
| 7780 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7781 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 7782 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7783 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7784 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7785 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7786 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7787 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7788 | lyd_unlink(unres->node[i]); |
| 7789 | unres->type[i] = UNRES_DELETE; |
| 7790 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7791 | |
| 7792 | /* update the rest of unres items */ |
| 7793 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 7794 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 7795 | continue; |
| 7796 | } |
| 7797 | |
| 7798 | /* test if the node is in subtree to be deleted */ |
| 7799 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 7800 | if (parent == unres->node[i]) { |
| 7801 | /* yes, it is */ |
| 7802 | unres->type[j] = UNRES_RESOLVED; |
| 7803 | resolved++; |
| 7804 | break; |
| 7805 | } |
| 7806 | } |
| 7807 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7808 | } else { |
| 7809 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 7810 | } |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 7811 | ly_err_clean(1); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7812 | resolved++; |
| 7813 | progress = 1; |
| 7814 | } else if (rc == -1) { |
| 7815 | ly_vlog_hide(0); |
Michal Vasko | 76e7340 | 2016-08-24 16:00:13 +0200 | [diff] [blame] | 7816 | /* print only this last error */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7817 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7818 | return -1; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7819 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7820 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7821 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7822 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7823 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7824 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7825 | if (stmt_count > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7826 | ly_vlog_hide(0); |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 7827 | ly_err_repeat(); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7828 | return -1; |
| 7829 | } |
| 7830 | |
| 7831 | for (i = 0; del_items && i < unres->count; i++) { |
| 7832 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 7833 | if (unres->type[i] != UNRES_DELETE) { |
| 7834 | continue; |
| 7835 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 7836 | if (!unres->node[i]) { |
| 7837 | unres->type[i] = UNRES_RESOLVED; |
| 7838 | del_items--; |
| 7839 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 7840 | } |
| 7841 | |
| 7842 | /* really remove the complete subtree */ |
| 7843 | lyd_free(unres->node[i]); |
| 7844 | unres->type[i] = UNRES_RESOLVED; |
| 7845 | del_items--; |
| 7846 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 7847 | |
| 7848 | /* now leafrefs */ |
| 7849 | first = 1; |
| 7850 | stmt_count = 0; |
| 7851 | resolved = 0; |
| 7852 | do { |
| 7853 | progress = 0; |
| 7854 | for (i = 0; i < unres->count; i++) { |
| 7855 | if (unres->type[i] != UNRES_LEAFREF) { |
| 7856 | continue; |
| 7857 | } |
| 7858 | if (first) { |
| 7859 | /* count leafref nodes in unres list */ |
| 7860 | stmt_count++; |
| 7861 | } |
| 7862 | |
| 7863 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7864 | if (!rc) { |
| 7865 | unres->type[i] = UNRES_RESOLVED; |
| 7866 | ly_err_clean(1); |
| 7867 | resolved++; |
| 7868 | progress = 1; |
| 7869 | } else if (rc == -1) { |
| 7870 | ly_vlog_hide(0); |
| 7871 | /* print only this last error */ |
| 7872 | resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
| 7873 | return -1; |
| 7874 | } /* else forward reference */ |
| 7875 | } |
| 7876 | first = 0; |
| 7877 | } while (progress && resolved < stmt_count); |
| 7878 | |
| 7879 | /* do we have some unresolved leafrefs? */ |
| 7880 | if (stmt_count > resolved) { |
| 7881 | ly_vlog_hide(0); |
| 7882 | ly_err_repeat(); |
| 7883 | return -1; |
| 7884 | } |
| 7885 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7886 | ly_vlog_hide(0); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7887 | |
| 7888 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7889 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7890 | if (unres->type[i] == UNRES_RESOLVED) { |
| 7891 | continue; |
| 7892 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 7893 | 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] | 7894 | |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7895 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7896 | if (rc) { |
| 7897 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7898 | return -1; |
| 7899 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7900 | |
| 7901 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7902 | } |
| 7903 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 7904 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 7905 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7906 | return EXIT_SUCCESS; |
| 7907 | } |