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 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 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" |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 30 | #include "hash_table.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 | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 33 | #include "validation.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 34 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 35 | /* internal parsed predicate structure */ |
| 36 | struct parsed_pred { |
| 37 | const struct lys_node *schema; |
| 38 | int len; |
| 39 | struct { |
| 40 | const char *mod_name; |
| 41 | int mod_name_len; |
| 42 | const char *name; |
| 43 | int nam_len; |
| 44 | const char *value; |
| 45 | int val_len; |
| 46 | } *pred; |
| 47 | }; |
| 48 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 49 | int |
| 50 | 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] | 51 | { |
| 52 | const char *ptr; |
| 53 | int minus = 0; |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 54 | int64_t ret = 0, prev_ret; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 55 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 56 | |
| 57 | ptr = *str_num; |
| 58 | |
| 59 | if (ptr[0] == '-') { |
| 60 | minus = 1; |
| 61 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 62 | } else if (ptr[0] == '+') { |
| 63 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 66 | if (!isdigit(ptr[0])) { |
| 67 | /* there must be at least one */ |
| 68 | return 1; |
| 69 | } |
| 70 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 71 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 72 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 73 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (ptr[0] == '.') { |
| 77 | if (ptr[1] == '.') { |
| 78 | /* it's the next interval */ |
| 79 | break; |
| 80 | } |
| 81 | ++str_dig; |
| 82 | } else { |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 83 | prev_ret = ret; |
| 84 | if (minus) { |
| 85 | ret = ret * 10 - (ptr[0] - '0'); |
| 86 | if (ret > prev_ret) { |
| 87 | return 1; |
| 88 | } |
| 89 | } else { |
| 90 | ret = ret * 10 + (ptr[0] - '0'); |
| 91 | if (ret < prev_ret) { |
| 92 | return 1; |
| 93 | } |
| 94 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 95 | if (str_dig > -1) { |
| 96 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 97 | if (ptr[0] == '0') { |
| 98 | /* possibly trailing zero */ |
| 99 | trailing_zeros++; |
| 100 | } else { |
| 101 | trailing_zeros = 0; |
| 102 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 103 | } |
| 104 | ++str_exp; |
| 105 | } |
| 106 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 107 | if (str_dig == 0) { |
| 108 | /* no digits after '.' */ |
| 109 | return 1; |
| 110 | } else if (str_dig == -1) { |
| 111 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 112 | str_dig = 0; |
| 113 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 114 | /* remove trailing zeros */ |
| 115 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 116 | str_dig -= trailing_zeros; |
| 117 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 118 | ret = ret / dec_pow(trailing_zeros); |
| 119 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 120 | |
| 121 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 122 | if (str_dig < dig) { |
| 123 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 124 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 125 | } |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 126 | prev_ret = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 127 | ret *= dec_pow(dig - str_dig); |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 128 | if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) { |
| 129 | return 1; |
| 130 | } |
| 131 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 132 | } |
| 133 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 134 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 137 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 138 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 139 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 140 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 144 | * @brief Parse an identifier. |
| 145 | * |
| 146 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 147 | * identifier = (ALPHA / "_") |
| 148 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 149 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 150 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 151 | * |
| 152 | * @return Number of characters successfully parsed. |
| 153 | */ |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 154 | unsigned int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 155 | parse_identifier(const char *id) |
| 156 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 157 | unsigned int parsed = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 158 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 159 | assert(id); |
| 160 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 161 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 162 | return -parsed; |
| 163 | } |
| 164 | |
| 165 | ++parsed; |
| 166 | ++id; |
| 167 | |
| 168 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 169 | ++parsed; |
| 170 | ++id; |
| 171 | } |
| 172 | |
| 173 | return parsed; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @brief Parse a node-identifier. |
| 178 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 179 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 180 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 181 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 182 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 183 | * @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] | 184 | * @param[out] name Points to the node name. |
| 185 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 186 | * @param[out] all_desc Whether the path starts with '/', only supported in extended paths. |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 187 | * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier). |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 188 | * |
| 189 | * @return Number of characters successfully parsed, |
| 190 | * positive on success, negative on failure. |
| 191 | */ |
| 192 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 193 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 194 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 195 | { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 196 | int parsed = 0, ret, all_desc_local = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 197 | |
| 198 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 199 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 200 | assert((name && nam_len) || (!name && !nam_len)); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 201 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 202 | if (mod_name) { |
| 203 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 204 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 205 | } |
| 206 | if (name) { |
| 207 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 208 | *nam_len = 0; |
| 209 | } |
| 210 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 211 | if (extended) { |
| 212 | /* try to parse only the extended expressions */ |
| 213 | if (id[parsed] == '/') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 214 | if (all_desc) { |
| 215 | *all_desc = 1; |
| 216 | } |
| 217 | all_desc_local = 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 218 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 219 | if (all_desc) { |
| 220 | *all_desc = 0; |
| 221 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* is there a prefix? */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 225 | ret = parse_identifier(id + all_desc_local); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 226 | if (ret > 0) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 227 | if (id[all_desc_local + ret] != ':') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 228 | /* this is not a prefix, so not an extended id */ |
| 229 | goto standard_id; |
| 230 | } |
| 231 | |
| 232 | if (mod_name) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 233 | *mod_name = id + all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 234 | *mod_name_len = ret; |
| 235 | } |
| 236 | |
| 237 | /* "/" and ":" */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 238 | ret += all_desc_local + 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 239 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 240 | ret = all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* parse either "*" or "." */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 244 | if (*(id + ret) == '*') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 245 | if (name) { |
| 246 | *name = id + ret; |
| 247 | *nam_len = 1; |
| 248 | } |
| 249 | ++ret; |
| 250 | |
| 251 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 252 | } else if (*(id + ret) == '.') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 253 | if (!all_desc_local) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 254 | /* /. is redundant expression, we do not accept it */ |
| 255 | return -ret; |
| 256 | } |
| 257 | |
| 258 | if (name) { |
| 259 | *name = id + ret; |
| 260 | *nam_len = 1; |
| 261 | } |
| 262 | ++ret; |
| 263 | |
| 264 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 265 | } else if (*(id + ret) == '#') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 266 | if (all_desc_local || !ret) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 267 | /* no prefix */ |
| 268 | return 0; |
| 269 | } |
| 270 | parsed = ret + 1; |
| 271 | if ((ret = parse_identifier(id + parsed)) < 1) { |
| 272 | return -parsed + ret; |
| 273 | } |
| 274 | *name = id + parsed - 1; |
| 275 | *nam_len = ret + 1; |
| 276 | return parsed + ret; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 277 | } |
| 278 | /* else a standard id, parse it all again */ |
| 279 | } |
| 280 | |
| 281 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 282 | if ((ret = parse_identifier(id)) < 1) { |
| 283 | return ret; |
| 284 | } |
| 285 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 286 | if (mod_name) { |
| 287 | *mod_name = id; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 288 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | parsed += ret; |
| 292 | id += ret; |
| 293 | |
| 294 | /* there is prefix */ |
| 295 | if (id[0] == ':') { |
| 296 | ++parsed; |
| 297 | ++id; |
| 298 | |
| 299 | /* there isn't */ |
| 300 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 301 | if (name && mod_name) { |
| 302 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 303 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 304 | if (mod_name) { |
| 305 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 308 | if (nam_len && mod_name_len) { |
| 309 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 310 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 311 | if (mod_name_len) { |
| 312 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return parsed; |
| 316 | } |
| 317 | |
| 318 | /* identifier (node name) */ |
| 319 | if ((ret = parse_identifier(id)) < 1) { |
| 320 | return -parsed+ret; |
| 321 | } |
| 322 | |
| 323 | if (name) { |
| 324 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 325 | *nam_len = ret; |
| 326 | } |
| 327 | |
| 328 | return parsed+ret; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * @brief Parse a path-predicate (leafref). |
| 333 | * |
| 334 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 335 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 336 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 337 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 338 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 339 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 340 | * @param[out] name Points to the node name. |
| 341 | * @param[out] nam_len Length of the node name. |
| 342 | * @param[out] path_key_expr Points to the path-key-expr. |
| 343 | * @param[out] pke_len Length of the path-key-expr. |
| 344 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 345 | * |
| 346 | * @return Number of characters successfully parsed, |
| 347 | * positive on success, negative on failure. |
| 348 | */ |
| 349 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 350 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 351 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 352 | { |
| 353 | const char *ptr; |
| 354 | int parsed = 0, ret; |
| 355 | |
| 356 | assert(id); |
| 357 | if (prefix) { |
| 358 | *prefix = NULL; |
| 359 | } |
| 360 | if (pref_len) { |
| 361 | *pref_len = 0; |
| 362 | } |
| 363 | if (name) { |
| 364 | *name = NULL; |
| 365 | } |
| 366 | if (nam_len) { |
| 367 | *nam_len = 0; |
| 368 | } |
| 369 | if (path_key_expr) { |
| 370 | *path_key_expr = NULL; |
| 371 | } |
| 372 | if (pke_len) { |
| 373 | *pke_len = 0; |
| 374 | } |
| 375 | if (has_predicate) { |
| 376 | *has_predicate = 0; |
| 377 | } |
| 378 | |
| 379 | if (id[0] != '[') { |
| 380 | return -parsed; |
| 381 | } |
| 382 | |
| 383 | ++parsed; |
| 384 | ++id; |
| 385 | |
| 386 | while (isspace(id[0])) { |
| 387 | ++parsed; |
| 388 | ++id; |
| 389 | } |
| 390 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 391 | 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] | 392 | return -parsed+ret; |
| 393 | } |
| 394 | |
| 395 | parsed += ret; |
| 396 | id += ret; |
| 397 | |
| 398 | while (isspace(id[0])) { |
| 399 | ++parsed; |
| 400 | ++id; |
| 401 | } |
| 402 | |
| 403 | if (id[0] != '=') { |
| 404 | return -parsed; |
| 405 | } |
| 406 | |
| 407 | ++parsed; |
| 408 | ++id; |
| 409 | |
| 410 | while (isspace(id[0])) { |
| 411 | ++parsed; |
| 412 | ++id; |
| 413 | } |
| 414 | |
| 415 | if ((ptr = strchr(id, ']')) == NULL) { |
| 416 | return -parsed; |
| 417 | } |
| 418 | |
| 419 | --ptr; |
| 420 | while (isspace(ptr[0])) { |
| 421 | --ptr; |
| 422 | } |
| 423 | ++ptr; |
| 424 | |
| 425 | ret = ptr-id; |
| 426 | if (path_key_expr) { |
| 427 | *path_key_expr = id; |
| 428 | } |
| 429 | if (pke_len) { |
| 430 | *pke_len = ret; |
| 431 | } |
| 432 | |
| 433 | parsed += ret; |
| 434 | id += ret; |
| 435 | |
| 436 | while (isspace(id[0])) { |
| 437 | ++parsed; |
| 438 | ++id; |
| 439 | } |
| 440 | |
| 441 | assert(id[0] == ']'); |
| 442 | |
| 443 | if (id[1] == '[') { |
| 444 | *has_predicate = 1; |
| 445 | } |
| 446 | |
| 447 | return parsed+1; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 452 | * the ".." and the first node-identifier, other calls parse a single |
| 453 | * node-identifier each. |
| 454 | * |
| 455 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 456 | * rel-path-keyexpr |
| 457 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 458 | * *(node-identifier *WSP "/" *WSP) |
| 459 | * node-identifier |
| 460 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 461 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 462 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 463 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 464 | * @param[out] name Points to the node name. |
| 465 | * @param[out] nam_len Length of the node name. |
| 466 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 467 | * must not be changed between consecutive calls. |
| 468 | * @return Number of characters successfully parsed, |
| 469 | * positive on success, negative on failure. |
| 470 | */ |
| 471 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 472 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 473 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 474 | { |
| 475 | int parsed = 0, ret, par_times = 0; |
| 476 | |
| 477 | assert(id); |
| 478 | assert(parent_times); |
| 479 | if (prefix) { |
| 480 | *prefix = NULL; |
| 481 | } |
| 482 | if (pref_len) { |
| 483 | *pref_len = 0; |
| 484 | } |
| 485 | if (name) { |
| 486 | *name = NULL; |
| 487 | } |
| 488 | if (nam_len) { |
| 489 | *nam_len = 0; |
| 490 | } |
| 491 | |
| 492 | if (!*parent_times) { |
| 493 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 494 | if (strncmp(id, "current()", 9)) { |
| 495 | return -parsed; |
| 496 | } |
| 497 | |
| 498 | parsed += 9; |
| 499 | id += 9; |
| 500 | |
| 501 | while (isspace(id[0])) { |
| 502 | ++parsed; |
| 503 | ++id; |
| 504 | } |
| 505 | |
| 506 | if (id[0] != '/') { |
| 507 | return -parsed; |
| 508 | } |
| 509 | |
| 510 | ++parsed; |
| 511 | ++id; |
| 512 | |
| 513 | while (isspace(id[0])) { |
| 514 | ++parsed; |
| 515 | ++id; |
| 516 | } |
| 517 | |
| 518 | /* rel-path-keyexpr */ |
| 519 | if (strncmp(id, "..", 2)) { |
| 520 | return -parsed; |
| 521 | } |
| 522 | ++par_times; |
| 523 | |
| 524 | parsed += 2; |
| 525 | id += 2; |
| 526 | |
| 527 | while (isspace(id[0])) { |
| 528 | ++parsed; |
| 529 | ++id; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 534 | * |
| 535 | * first parent reference with whitespaces already parsed |
| 536 | */ |
| 537 | if (id[0] != '/') { |
| 538 | return -parsed; |
| 539 | } |
| 540 | |
| 541 | ++parsed; |
| 542 | ++id; |
| 543 | |
| 544 | while (isspace(id[0])) { |
| 545 | ++parsed; |
| 546 | ++id; |
| 547 | } |
| 548 | |
| 549 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 550 | ++par_times; |
| 551 | |
| 552 | parsed += 2; |
| 553 | id += 2; |
| 554 | |
| 555 | while (isspace(id[0])) { |
| 556 | ++parsed; |
| 557 | ++id; |
| 558 | } |
| 559 | |
| 560 | if (id[0] != '/') { |
| 561 | return -parsed; |
| 562 | } |
| 563 | |
| 564 | ++parsed; |
| 565 | ++id; |
| 566 | |
| 567 | while (isspace(id[0])) { |
| 568 | ++parsed; |
| 569 | ++id; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (!*parent_times) { |
| 574 | *parent_times = par_times; |
| 575 | } |
| 576 | |
| 577 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 578 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 579 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | parsed += ret; |
| 583 | id += ret; |
| 584 | |
| 585 | return parsed; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * @brief Parse path-arg (leafref). |
| 590 | * |
| 591 | * path-arg = absolute-path / relative-path |
| 592 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 593 | * relative-path = 1*(".." "/") descendant-path |
| 594 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 595 | * @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] | 596 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 597 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 598 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 599 | * @param[out] name Points to the node name. |
| 600 | * @param[out] nam_len Length of the node name. |
| 601 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 602 | * must not be changed between consecutive calls. -1 if the |
| 603 | * path is relative. |
| 604 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 605 | * |
| 606 | * @return Number of characters successfully parsed, |
| 607 | * positive on success, negative on failure. |
| 608 | */ |
| 609 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 610 | 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] | 611 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 612 | { |
| 613 | int parsed = 0, ret, par_times = 0; |
| 614 | |
| 615 | assert(id); |
| 616 | assert(parent_times); |
| 617 | if (prefix) { |
| 618 | *prefix = NULL; |
| 619 | } |
| 620 | if (pref_len) { |
| 621 | *pref_len = 0; |
| 622 | } |
| 623 | if (name) { |
| 624 | *name = NULL; |
| 625 | } |
| 626 | if (nam_len) { |
| 627 | *nam_len = 0; |
| 628 | } |
| 629 | if (has_predicate) { |
| 630 | *has_predicate = 0; |
| 631 | } |
| 632 | |
| 633 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 634 | ++par_times; |
| 635 | |
| 636 | parsed += 2; |
| 637 | id += 2; |
| 638 | |
| 639 | while (!strncmp(id, "/..", 3)) { |
| 640 | ++par_times; |
| 641 | |
| 642 | parsed += 3; |
| 643 | id += 3; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (!*parent_times) { |
| 648 | if (par_times) { |
| 649 | *parent_times = par_times; |
| 650 | } else { |
| 651 | *parent_times = -1; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (id[0] != '/') { |
| 656 | return -parsed; |
| 657 | } |
| 658 | |
| 659 | /* skip '/' */ |
| 660 | ++parsed; |
| 661 | ++id; |
| 662 | |
| 663 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 664 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 665 | return -parsed - ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 666 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 667 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 668 | /* actually we always need prefix even it is not specified */ |
| 669 | *prefix = lys_main_module(mod)->name; |
| 670 | *pref_len = strlen(*prefix); |
| 671 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 672 | |
| 673 | parsed += ret; |
| 674 | id += ret; |
| 675 | |
| 676 | /* there is no predicate */ |
| 677 | if ((id[0] == '/') || !id[0]) { |
| 678 | return parsed; |
| 679 | } else if (id[0] != '[') { |
| 680 | return -parsed; |
| 681 | } |
| 682 | |
| 683 | if (has_predicate) { |
| 684 | *has_predicate = 1; |
| 685 | } |
| 686 | |
| 687 | return parsed; |
| 688 | } |
| 689 | |
| 690 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 691 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 692 | * are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 693 | * |
| 694 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 695 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 696 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 697 | * @param[out] model Points to the model name. |
| 698 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 699 | * @param[out] name Points to the node name. |
| 700 | * @param[out] nam_len Length of the node name. |
| 701 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 702 | * |
| 703 | * @return Number of characters successfully parsed, |
| 704 | * positive on success, negative on failure. |
| 705 | */ |
| 706 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 707 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 708 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 709 | { |
| 710 | int parsed = 0, ret; |
| 711 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 712 | assert(id && model && mod_len && name && nam_len); |
| 713 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 714 | if (has_predicate) { |
| 715 | *has_predicate = 0; |
| 716 | } |
| 717 | |
| 718 | if (id[0] != '/') { |
| 719 | return -parsed; |
| 720 | } |
| 721 | |
| 722 | ++parsed; |
| 723 | ++id; |
| 724 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 725 | if ((ret = parse_identifier(id)) < 1) { |
| 726 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 729 | *name = id; |
| 730 | *nam_len = ret; |
| 731 | |
| 732 | parsed += ret; |
| 733 | id += ret; |
| 734 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 735 | if (id[0] == ':') { |
| 736 | /* we have prefix */ |
| 737 | *model = *name; |
| 738 | *mod_len = *nam_len; |
| 739 | |
| 740 | ++parsed; |
| 741 | ++id; |
| 742 | |
| 743 | if ((ret = parse_identifier(id)) < 1) { |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | *name = id; |
| 748 | *nam_len = ret; |
| 749 | |
| 750 | parsed += ret; |
| 751 | id += ret; |
| 752 | } |
| 753 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 754 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 755 | *has_predicate = 1; |
| 756 | } |
| 757 | |
| 758 | return parsed; |
| 759 | } |
| 760 | |
| 761 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 762 | * @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] | 763 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 764 | * |
| 765 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 766 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 767 | * ((DQUOTE string DQUOTE) / |
| 768 | * (SQUOTE string SQUOTE)) |
| 769 | * pos = non-negative-integer-value |
| 770 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 771 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 772 | * @param[out] model Points to the model name. |
| 773 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 774 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 775 | * @param[out] nam_len Length of the node name. |
| 776 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 777 | * NULL if there is not any. |
| 778 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 779 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 780 | * |
| 781 | * @return Number of characters successfully parsed, |
| 782 | * positive on success, negative on failure. |
| 783 | */ |
| 784 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 785 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 786 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 787 | { |
| 788 | const char *ptr; |
| 789 | int parsed = 0, ret; |
| 790 | char quote; |
| 791 | |
| 792 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 793 | if (model) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 794 | assert(mod_len); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 795 | *model = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 796 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 797 | } |
| 798 | if (name) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 799 | assert(nam_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 800 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 801 | *nam_len = 0; |
| 802 | } |
| 803 | if (value) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 804 | assert(val_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 805 | *value = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 806 | *val_len = 0; |
| 807 | } |
| 808 | if (has_predicate) { |
| 809 | *has_predicate = 0; |
| 810 | } |
| 811 | |
| 812 | if (id[0] != '[') { |
| 813 | return -parsed; |
| 814 | } |
| 815 | |
| 816 | ++parsed; |
| 817 | ++id; |
| 818 | |
| 819 | while (isspace(id[0])) { |
| 820 | ++parsed; |
| 821 | ++id; |
| 822 | } |
| 823 | |
| 824 | /* pos */ |
| 825 | if (isdigit(id[0])) { |
| 826 | if (name) { |
| 827 | *name = id; |
| 828 | } |
| 829 | |
| 830 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 831 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | while (isdigit(id[0])) { |
| 835 | ++parsed; |
| 836 | ++id; |
| 837 | } |
| 838 | |
| 839 | if (nam_len) { |
| 840 | *nam_len = id-(*name); |
| 841 | } |
| 842 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 843 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 844 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 845 | if (id[0] == '.') { |
| 846 | if (name) { |
| 847 | *name = id; |
| 848 | } |
| 849 | if (nam_len) { |
| 850 | *nam_len = 1; |
| 851 | } |
| 852 | |
| 853 | ++parsed; |
| 854 | ++id; |
| 855 | |
| 856 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 857 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 858 | return -parsed + ret; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | parsed += ret; |
| 862 | id += ret; |
| 863 | } |
| 864 | |
| 865 | while (isspace(id[0])) { |
| 866 | ++parsed; |
| 867 | ++id; |
| 868 | } |
| 869 | |
| 870 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 871 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 872 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 873 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 874 | ++parsed; |
| 875 | ++id; |
| 876 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 877 | while (isspace(id[0])) { |
| 878 | ++parsed; |
| 879 | ++id; |
| 880 | } |
| 881 | |
| 882 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 883 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 884 | quote = id[0]; |
| 885 | |
| 886 | ++parsed; |
| 887 | ++id; |
| 888 | |
| 889 | if ((ptr = strchr(id, quote)) == NULL) { |
| 890 | return -parsed; |
| 891 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 892 | ret = ptr - id; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 893 | |
| 894 | if (value) { |
| 895 | *value = id; |
| 896 | } |
| 897 | if (val_len) { |
| 898 | *val_len = ret; |
| 899 | } |
| 900 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 901 | parsed += ret + 1; |
| 902 | id += ret + 1; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 903 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 904 | return -parsed; |
| 905 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | while (isspace(id[0])) { |
| 909 | ++parsed; |
| 910 | ++id; |
| 911 | } |
| 912 | |
| 913 | if (id[0] != ']') { |
| 914 | return -parsed; |
| 915 | } |
| 916 | |
| 917 | ++parsed; |
| 918 | ++id; |
| 919 | |
| 920 | if ((id[0] == '[') && has_predicate) { |
| 921 | *has_predicate = 1; |
| 922 | } |
| 923 | |
| 924 | return parsed; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * @brief Parse schema-nodeid. |
| 929 | * |
| 930 | * schema-nodeid = absolute-schema-nodeid / |
| 931 | * descendant-schema-nodeid |
| 932 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 933 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 934 | * node-identifier |
| 935 | * absolute-schema-nodeid |
| 936 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 937 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 938 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 939 | * @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] | 940 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 941 | * @param[out] nam_len Length of the node name. |
| 942 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 943 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 944 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 945 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 946 | * @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] | 947 | * |
| 948 | * @return Number of characters successfully parsed, |
| 949 | * positive on success, negative on failure. |
| 950 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 951 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 952 | 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] | 953 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 954 | { |
| 955 | int parsed = 0, ret; |
| 956 | |
| 957 | assert(id); |
| 958 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 959 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 960 | if (has_predicate) { |
| 961 | *has_predicate = 0; |
| 962 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 963 | |
| 964 | if (id[0] != '/') { |
| 965 | if (*is_relative != -1) { |
| 966 | return -parsed; |
| 967 | } else { |
| 968 | *is_relative = 1; |
| 969 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 970 | if (!strncmp(id, "./", 2)) { |
| 971 | parsed += 2; |
| 972 | id += 2; |
| 973 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 974 | } else { |
| 975 | if (*is_relative == -1) { |
| 976 | *is_relative = 0; |
| 977 | } |
| 978 | ++parsed; |
| 979 | ++id; |
| 980 | } |
| 981 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 982 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 983 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 984 | } |
| 985 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 986 | parsed += ret; |
| 987 | id += ret; |
| 988 | |
| 989 | if ((id[0] == '[') && has_predicate) { |
| 990 | *has_predicate = 1; |
| 991 | } |
| 992 | |
| 993 | return parsed; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * @brief Parse schema predicate (special format internally used). |
| 998 | * |
| 999 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1000 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1001 | * key-with-value = identifier *WSP "=" *WSP |
| 1002 | * ((DQUOTE string DQUOTE) / |
| 1003 | * (SQUOTE string SQUOTE)) |
| 1004 | * |
| 1005 | * @param[in] id Identifier to use. |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1006 | * @param[out] mod_name Points to the list key module name. |
| 1007 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1008 | * @param[out] name Points to the list key name. |
| 1009 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1010 | * @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] | 1011 | * @param[out] val_len Length of \p value. |
| 1012 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 1013 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1014 | int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1015 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 1016 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1017 | { |
| 1018 | const char *ptr; |
| 1019 | int parsed = 0, ret; |
| 1020 | char quote; |
| 1021 | |
| 1022 | assert(id); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1023 | if (mod_name) { |
| 1024 | *mod_name = NULL; |
| 1025 | } |
| 1026 | if (mod_name_len) { |
| 1027 | *mod_name_len = 0; |
| 1028 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1029 | if (name) { |
| 1030 | *name = NULL; |
| 1031 | } |
| 1032 | if (nam_len) { |
| 1033 | *nam_len = 0; |
| 1034 | } |
| 1035 | if (value) { |
| 1036 | *value = NULL; |
| 1037 | } |
| 1038 | if (val_len) { |
| 1039 | *val_len = 0; |
| 1040 | } |
| 1041 | if (has_predicate) { |
| 1042 | *has_predicate = 0; |
| 1043 | } |
| 1044 | |
| 1045 | if (id[0] != '[') { |
| 1046 | return -parsed; |
| 1047 | } |
| 1048 | |
| 1049 | ++parsed; |
| 1050 | ++id; |
| 1051 | |
| 1052 | while (isspace(id[0])) { |
| 1053 | ++parsed; |
| 1054 | ++id; |
| 1055 | } |
| 1056 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1057 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1058 | if (id[0] == '.') { |
| 1059 | ret = 1; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1060 | |
| 1061 | if (name) { |
| 1062 | *name = id; |
| 1063 | } |
| 1064 | if (nam_len) { |
| 1065 | *nam_len = ret; |
| 1066 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1067 | } else if (isdigit(id[0])) { |
| 1068 | if (id[0] == '0') { |
| 1069 | return -parsed; |
| 1070 | } |
| 1071 | ret = 1; |
| 1072 | while (isdigit(id[ret])) { |
| 1073 | ++ret; |
| 1074 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1075 | |
| 1076 | if (name) { |
| 1077 | *name = id; |
| 1078 | } |
| 1079 | if (nam_len) { |
| 1080 | *nam_len = ret; |
| 1081 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1082 | } 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] | 1083 | return -parsed + ret; |
| 1084 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1085 | |
| 1086 | parsed += ret; |
| 1087 | id += ret; |
| 1088 | |
| 1089 | while (isspace(id[0])) { |
| 1090 | ++parsed; |
| 1091 | ++id; |
| 1092 | } |
| 1093 | |
| 1094 | /* there is value as well */ |
| 1095 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1096 | if (name && isdigit(**name)) { |
| 1097 | return -parsed; |
| 1098 | } |
| 1099 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1100 | ++parsed; |
| 1101 | ++id; |
| 1102 | |
| 1103 | while (isspace(id[0])) { |
| 1104 | ++parsed; |
| 1105 | ++id; |
| 1106 | } |
| 1107 | |
| 1108 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1109 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1110 | quote = id[0]; |
| 1111 | |
| 1112 | ++parsed; |
| 1113 | ++id; |
| 1114 | |
| 1115 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1116 | return -parsed; |
| 1117 | } |
| 1118 | ret = ptr - id; |
| 1119 | |
| 1120 | if (value) { |
| 1121 | *value = id; |
| 1122 | } |
| 1123 | if (val_len) { |
| 1124 | *val_len = ret; |
| 1125 | } |
| 1126 | |
| 1127 | parsed += ret + 1; |
| 1128 | id += ret + 1; |
| 1129 | } else { |
| 1130 | return -parsed; |
| 1131 | } |
| 1132 | |
| 1133 | while (isspace(id[0])) { |
| 1134 | ++parsed; |
| 1135 | ++id; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (id[0] != ']') { |
| 1140 | return -parsed; |
| 1141 | } |
| 1142 | |
| 1143 | ++parsed; |
| 1144 | ++id; |
| 1145 | |
| 1146 | if ((id[0] == '[') && has_predicate) { |
| 1147 | *has_predicate = 1; |
| 1148 | } |
| 1149 | |
| 1150 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1153 | #ifdef LY_ENABLED_CACHE |
| 1154 | |
| 1155 | static int |
| 1156 | resolve_hash_table_find_equal(void *val1_p, void *val2_p, int mod, void *UNUSED(cb_data)) |
| 1157 | { |
| 1158 | struct lyd_node *val2, *elem2; |
| 1159 | struct parsed_pred pp; |
| 1160 | const char *str; |
| 1161 | int i; |
| 1162 | |
| 1163 | assert(!mod); |
Michal Vasko | 87e29a8 | 2018-05-21 13:50:43 +0200 | [diff] [blame] | 1164 | (void)mod; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1165 | |
| 1166 | pp = *((struct parsed_pred *)val1_p); |
| 1167 | val2 = *((struct lyd_node **)val2_p); |
| 1168 | |
| 1169 | if (val2->schema != pp.schema) { |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | switch (val2->schema->nodetype) { |
| 1174 | case LYS_CONTAINER: |
| 1175 | case LYS_LEAF: |
| 1176 | case LYS_ANYXML: |
| 1177 | case LYS_ANYDATA: |
| 1178 | return 1; |
| 1179 | case LYS_LEAFLIST: |
| 1180 | str = ((struct lyd_node_leaf_list *)val2)->value_str; |
| 1181 | if (!strncmp(str, pp.pred[0].value, pp.pred[0].val_len) && !str[pp.pred[0].val_len]) { |
| 1182 | return 1; |
| 1183 | } |
| 1184 | return 0; |
| 1185 | case LYS_LIST: |
| 1186 | assert(((struct lys_node_list *)val2->schema)->keys_size); |
| 1187 | assert(((struct lys_node_list *)val2->schema)->keys_size == pp.len); |
| 1188 | |
| 1189 | /* lists with keys, their equivalence is based on their keys */ |
| 1190 | elem2 = val2->child; |
| 1191 | /* the exact data order is guaranteed */ |
| 1192 | for (i = 0; elem2 && (i < pp.len); ++i) { |
| 1193 | /* module check */ |
| 1194 | if (pp.pred[i].mod_name) { |
| 1195 | if (strncmp(lyd_node_module(elem2)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len) |
| 1196 | || lyd_node_module(elem2)->name[pp.pred[i].mod_name_len]) { |
| 1197 | break; |
| 1198 | } |
| 1199 | } else { |
| 1200 | if (lyd_node_module(elem2) != lys_node_module(pp.schema)) { |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /* name check */ |
| 1206 | if (strncmp(elem2->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || elem2->schema->name[pp.pred[i].nam_len]) { |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | /* value check */ |
| 1211 | str = ((struct lyd_node_leaf_list *)elem2)->value_str; |
| 1212 | if (strncmp(str, pp.pred[i].value, pp.pred[i].val_len) || str[pp.pred[i].val_len]) { |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | /* next key */ |
| 1217 | elem2 = elem2->next; |
| 1218 | } |
| 1219 | if (i == pp.len) { |
| 1220 | return 1; |
| 1221 | } |
| 1222 | return 0; |
| 1223 | default: |
| 1224 | break; |
| 1225 | } |
| 1226 | |
| 1227 | LOGINT(val2->schema->module->ctx); |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static struct lyd_node * |
| 1232 | resolve_json_data_node_hash(struct lyd_node *parent, struct parsed_pred pp) |
| 1233 | { |
| 1234 | values_equal_cb prev_cb; |
| 1235 | struct lyd_node **ret = NULL; |
| 1236 | uint32_t hash; |
| 1237 | int i; |
| 1238 | |
| 1239 | assert(parent && parent->hash); |
| 1240 | |
| 1241 | /* set our value equivalence callback that does not require data nodes */ |
| 1242 | prev_cb = lyht_set_cb(parent->ht, resolve_hash_table_find_equal); |
| 1243 | |
| 1244 | /* get the hash of the searched node */ |
| 1245 | hash = dict_hash_multi(0, lys_node_module(pp.schema)->name, strlen(lys_node_module(pp.schema)->name)); |
| 1246 | hash = dict_hash_multi(hash, pp.schema->name, strlen(pp.schema->name)); |
| 1247 | if (pp.schema->nodetype == LYS_LEAFLIST) { |
| 1248 | assert((pp.len == 1) && (pp.pred[0].name[0] == '.') && (pp.pred[0].nam_len == 1)); |
| 1249 | /* leaf-list value in predicate */ |
| 1250 | hash = dict_hash_multi(hash, pp.pred[0].value, pp.pred[0].val_len); |
| 1251 | } else if (pp.schema->nodetype == LYS_LIST) { |
| 1252 | /* list keys in predicates */ |
| 1253 | for (i = 0; i < pp.len; ++i) { |
| 1254 | hash = dict_hash_multi(hash, pp.pred[i].value, pp.pred[i].val_len); |
| 1255 | } |
| 1256 | } |
| 1257 | hash = dict_hash_multi(hash, NULL, 0); |
| 1258 | |
| 1259 | /* try to find the node */ |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 1260 | i = lyht_find(parent->ht, &pp, hash, (void **)&ret); |
| 1261 | assert(i || *ret); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1262 | |
| 1263 | /* restore the original callback */ |
| 1264 | lyht_set_cb(parent->ht, prev_cb); |
| 1265 | |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 1266 | return (i ? NULL : *ret); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | #endif |
| 1270 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1271 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1272 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1273 | * |
| 1274 | * @param[in] feat_name Feature name to resolve. |
| 1275 | * @param[in] len Length of \p feat_name. |
| 1276 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1277 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1278 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1279 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1280 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1281 | */ |
| 1282 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1283 | 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] | 1284 | { |
| 1285 | char *str; |
| 1286 | const char *mod_name, *name; |
| 1287 | int mod_name_len, nam_len, i, j; |
| 1288 | const struct lys_module *module; |
| 1289 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1290 | assert(feature); |
| 1291 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1292 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1293 | if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1294 | LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1295 | return -1; |
| 1296 | } |
| 1297 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1298 | module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1299 | if (!module) { |
| 1300 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1301 | LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1302 | return -1; |
| 1303 | } |
| 1304 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1305 | if (module != node->module && module == lys_node_module(node)) { |
| 1306 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1307 | for (j = 0; j < node->module->features_size; j++) { |
| 1308 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1309 | /* check status */ |
| 1310 | 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] | 1311 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1312 | return -1; |
| 1313 | } |
| 1314 | *feature = &node->module->features[j]; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1320 | /* search in the identified module ... */ |
| 1321 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1322 | 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] | 1323 | /* check status */ |
| 1324 | 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] | 1325 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1326 | return -1; |
| 1327 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1328 | *feature = &module->features[j]; |
| 1329 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1333 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1334 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1335 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1336 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1337 | /* check status */ |
| 1338 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1339 | module->inc[i].submodule->features[j].flags, |
| 1340 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1341 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1342 | return -1; |
| 1343 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1344 | *feature = &module->inc[i].submodule->features[j]; |
| 1345 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* not found */ |
| 1351 | str = strndup(feat_name, len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1352 | LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1353 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1354 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1357 | /* |
| 1358 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1359 | * - 1 if enabled |
| 1360 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1361 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1362 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1363 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1364 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1365 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1366 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1367 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1368 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1369 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1370 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1371 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1372 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1373 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1377 | 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] | 1378 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1379 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1380 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1381 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1382 | op = iff_getop(expr->expr, *index_e); |
| 1383 | (*index_e)++; |
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 | switch (op) { |
| 1386 | case LYS_IFF_F: |
| 1387 | /* resolve feature */ |
| 1388 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1389 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1390 | /* invert result */ |
| 1391 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1392 | case LYS_IFF_AND: |
| 1393 | case LYS_IFF_OR: |
| 1394 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1395 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1396 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1397 | return a && b; |
| 1398 | } else { /* LYS_IFF_OR */ |
| 1399 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1403 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | int |
| 1407 | resolve_iffeature(struct lys_iffeature *expr) |
| 1408 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1409 | int index_e = 0, index_f = 0; |
| 1410 | |
| 1411 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1412 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1413 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1414 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | struct iff_stack { |
| 1418 | int size; |
| 1419 | int index; /* first empty item */ |
| 1420 | uint8_t *stack; |
| 1421 | }; |
| 1422 | |
| 1423 | static int |
| 1424 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1425 | { |
| 1426 | if (stack->index == stack->size) { |
| 1427 | stack->size += 4; |
| 1428 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1429 | LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | stack->stack[stack->index++] = value; |
| 1433 | return EXIT_SUCCESS; |
| 1434 | } |
| 1435 | |
| 1436 | static uint8_t |
| 1437 | iff_stack_pop(struct iff_stack *stack) |
| 1438 | { |
| 1439 | stack->index--; |
| 1440 | return stack->stack[stack->index]; |
| 1441 | } |
| 1442 | |
| 1443 | static void |
| 1444 | iff_stack_clean(struct iff_stack *stack) |
| 1445 | { |
| 1446 | stack->size = 0; |
| 1447 | free(stack->stack); |
| 1448 | } |
| 1449 | |
| 1450 | static void |
| 1451 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1452 | { |
| 1453 | uint8_t *item; |
| 1454 | uint8_t mask = 3; |
| 1455 | |
| 1456 | assert(pos >= 0); |
| 1457 | assert(op <= 3); /* max 2 bits */ |
| 1458 | |
| 1459 | item = &list[pos / 4]; |
| 1460 | mask = mask << 2 * (pos % 4); |
| 1461 | *item = (*item) & ~mask; |
| 1462 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1463 | } |
| 1464 | |
| 1465 | uint8_t |
| 1466 | iff_getop(uint8_t *list, int pos) |
| 1467 | { |
| 1468 | uint8_t *item; |
| 1469 | uint8_t mask = 3, result; |
| 1470 | |
| 1471 | assert(pos >= 0); |
| 1472 | |
| 1473 | item = &list[pos / 4]; |
| 1474 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1475 | return result >> 2 * (pos % 4); |
| 1476 | } |
| 1477 | |
| 1478 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1479 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1480 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1481 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1482 | struct unres_iffeat_data { |
| 1483 | struct lys_node *node; |
| 1484 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1485 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1486 | }; |
| 1487 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1488 | void |
| 1489 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1490 | { |
| 1491 | unsigned int e = 0, f = 0, r = 0; |
| 1492 | uint8_t op; |
| 1493 | |
| 1494 | assert(iffeat); |
| 1495 | |
| 1496 | if (!iffeat->expr) { |
| 1497 | goto result; |
| 1498 | } |
| 1499 | |
| 1500 | do { |
| 1501 | op = iff_getop(iffeat->expr, e++); |
| 1502 | switch (op) { |
| 1503 | case LYS_IFF_NOT: |
| 1504 | if (!r) { |
| 1505 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1506 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1507 | break; |
| 1508 | case LYS_IFF_AND: |
| 1509 | case LYS_IFF_OR: |
| 1510 | if (!r) { |
| 1511 | r += 2; |
| 1512 | } else { |
| 1513 | r += 1; |
| 1514 | } |
| 1515 | break; |
| 1516 | case LYS_IFF_F: |
| 1517 | f++; |
| 1518 | if (r) { |
| 1519 | r--; |
| 1520 | } |
| 1521 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1522 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1523 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1524 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1525 | result: |
| 1526 | if (expr_size) { |
| 1527 | *expr_size = e; |
| 1528 | } |
| 1529 | if (feat_size) { |
| 1530 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1535 | 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] | 1536 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1537 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1538 | const char *c = value; |
| 1539 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1540 | int i, j, last_not, checkversion = 0; |
| 1541 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1542 | uint8_t op; |
| 1543 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1544 | struct unres_iffeat_data *iff_data; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1545 | struct ly_ctx *ctx = node->module->ctx; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1546 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1547 | assert(c); |
| 1548 | |
| 1549 | if (isspace(c[0])) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1550 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1551 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1552 | } |
| 1553 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1554 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1555 | for (i = j = last_not = 0; c[i]; i++) { |
| 1556 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1557 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1558 | j++; |
| 1559 | continue; |
| 1560 | } else if (c[i] == ')') { |
| 1561 | j--; |
| 1562 | continue; |
| 1563 | } else if (isspace(c[i])) { |
| 1564 | continue; |
| 1565 | } |
| 1566 | |
| 1567 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1568 | if (c[i + r] == '\0') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1569 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1570 | return EXIT_FAILURE; |
| 1571 | } else if (!isspace(c[i + r])) { |
| 1572 | /* feature name starting with the not/and/or */ |
| 1573 | last_not = 0; |
| 1574 | f_size++; |
| 1575 | } else if (c[i] == 'n') { /* not operation */ |
| 1576 | if (last_not) { |
| 1577 | /* double not */ |
| 1578 | expr_size = expr_size - 2; |
| 1579 | last_not = 0; |
| 1580 | } else { |
| 1581 | last_not = 1; |
| 1582 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1583 | } else { /* and, or */ |
| 1584 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1585 | /* not a not operation */ |
| 1586 | last_not = 0; |
| 1587 | } |
| 1588 | i += r; |
| 1589 | } else { |
| 1590 | f_size++; |
| 1591 | last_not = 0; |
| 1592 | } |
| 1593 | expr_size++; |
| 1594 | |
| 1595 | while (!isspace(c[i])) { |
| 1596 | if (!c[i] || c[i] == ')') { |
| 1597 | i--; |
| 1598 | break; |
| 1599 | } |
| 1600 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1601 | } |
| 1602 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1603 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1604 | /* not matching count of ( and ) */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1605 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1606 | return EXIT_FAILURE; |
| 1607 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1608 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1609 | if (checkversion || expr_size > 1) { |
| 1610 | /* check that we have 1.1 module */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 1611 | if (node->module->version != LYS_VERSION_1_1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1612 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1613 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module."); |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1614 | return EXIT_FAILURE; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1618 | /* allocate the memory */ |
| 1619 | 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] | 1620 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1621 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1622 | LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error); |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1623 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1624 | f_size--; expr_size--; /* used as indexes from now */ |
| 1625 | |
| 1626 | for (i--; i >= 0; i--) { |
| 1627 | if (c[i] == ')') { |
| 1628 | /* push it on stack */ |
| 1629 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1630 | continue; |
| 1631 | } else if (c[i] == '(') { |
| 1632 | /* pop from the stack into result all operators until ) */ |
| 1633 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1634 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1635 | } |
| 1636 | continue; |
| 1637 | } else if (isspace(c[i])) { |
| 1638 | continue; |
| 1639 | } |
| 1640 | |
| 1641 | /* end operator or operand -> find beginning and get what is it */ |
| 1642 | j = i + 1; |
| 1643 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1644 | i--; |
| 1645 | } |
| 1646 | i++; /* get back by one step */ |
| 1647 | |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1648 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1649 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1650 | /* double not */ |
| 1651 | iff_stack_pop(&stack); |
| 1652 | } else { |
| 1653 | /* not has the highest priority, so do not pop from the stack |
| 1654 | * as in case of AND and OR */ |
| 1655 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1656 | } |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1657 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1658 | /* as for OR - pop from the stack all operators with the same or higher |
| 1659 | * priority and store them to the result, then push the AND to the stack */ |
| 1660 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1661 | op = iff_stack_pop(&stack); |
| 1662 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1663 | } |
| 1664 | iff_stack_push(&stack, LYS_IFF_AND); |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1665 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1666 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1667 | op = iff_stack_pop(&stack); |
| 1668 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1669 | } |
| 1670 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1671 | } else { |
| 1672 | /* feature name, length is j - i */ |
| 1673 | |
| 1674 | /* add it to the result */ |
| 1675 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1676 | |
| 1677 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1678 | * forward referenced, we have to keep the feature name in auxiliary |
| 1679 | * structure passed into unres */ |
| 1680 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1681 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1682 | iff_data->node = node; |
| 1683 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1684 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1685 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1686 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1687 | f_size--; |
| 1688 | |
| 1689 | if (r == -1) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 1690 | lydict_remove(node->module->ctx, iff_data->fname); |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1691 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1692 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1693 | } |
| 1694 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1695 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1696 | while (stack.index) { |
| 1697 | op = iff_stack_pop(&stack); |
| 1698 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1699 | } |
| 1700 | |
| 1701 | if (++expr_size || ++f_size) { |
| 1702 | /* not all expected operators and operands found */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1703 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1704 | rc = EXIT_FAILURE; |
| 1705 | } else { |
| 1706 | rc = EXIT_SUCCESS; |
| 1707 | } |
| 1708 | |
| 1709 | error: |
| 1710 | /* cleanup */ |
| 1711 | iff_stack_clean(&stack); |
| 1712 | |
| 1713 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1717 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1718 | * |
| 1719 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1720 | * module). |
| 1721 | * |
| 1722 | */ |
| 1723 | struct lyd_node * |
| 1724 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1725 | { |
| 1726 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1727 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1728 | const struct lys_node *schema = NULL; |
| 1729 | |
| 1730 | assert(nodeid && start); |
| 1731 | |
| 1732 | if (nodeid[0] == '/') { |
| 1733 | return NULL; |
| 1734 | } |
| 1735 | |
| 1736 | str = p = strdup(nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1737 | LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1738 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1739 | while (p) { |
| 1740 | token = p; |
| 1741 | p = strchr(p, '/'); |
| 1742 | if (p) { |
| 1743 | *p = '\0'; |
| 1744 | p++; |
| 1745 | } |
| 1746 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1747 | if (p) { |
| 1748 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1749 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1750 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1751 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1752 | result = NULL; |
| 1753 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1754 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1755 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1756 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1757 | continue; |
| 1758 | } |
| 1759 | } else { |
| 1760 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1761 | 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] | 1762 | || !schema) { |
| 1763 | result = NULL; |
| 1764 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1765 | } |
| 1766 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1767 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1768 | if (iter->schema == schema) { |
| 1769 | /* move in data tree according to returned schema */ |
| 1770 | result = iter; |
| 1771 | break; |
| 1772 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1773 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1774 | if (!iter) { |
| 1775 | /* instance not found */ |
| 1776 | result = NULL; |
| 1777 | break; |
| 1778 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1779 | } |
| 1780 | free(str); |
| 1781 | |
| 1782 | return result; |
| 1783 | } |
| 1784 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1785 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1786 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1787 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1788 | { |
| 1789 | const struct lys_module *prefix_mod; |
| 1790 | |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1791 | /* handle special names */ |
| 1792 | if (name[0] == '*') { |
| 1793 | return 2; |
| 1794 | } else if (name[0] == '.') { |
| 1795 | return 3; |
| 1796 | } |
| 1797 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1798 | /* name check */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1799 | if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1800 | return 1; |
| 1801 | } |
| 1802 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1803 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1804 | if (mod_name) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1805 | prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1806 | if (!prefix_mod) { |
| 1807 | return -1; |
| 1808 | } |
| 1809 | } else { |
| 1810 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1811 | } |
| 1812 | if (prefix_mod != lys_node_module(sibling)) { |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1816 | /* match */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1817 | return 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1820 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1821 | static int |
| 1822 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1823 | const struct lys_module *cur_module, int *nodeid_end) |
| 1824 | { |
| 1825 | int mod_len, nam_len, has_predicate, r, i; |
| 1826 | const char *model, *name; |
| 1827 | struct lys_node_list *list; |
| 1828 | |
| 1829 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1830 | return 1; |
| 1831 | } |
| 1832 | |
| 1833 | list = (struct lys_node_list *)node; |
| 1834 | do { |
| 1835 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1836 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1837 | LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1838 | return -1; |
| 1839 | } |
| 1840 | nodeid += r; |
| 1841 | |
| 1842 | if (node->nodetype == LYS_LEAFLIST) { |
| 1843 | /* just check syntax */ |
| 1844 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1845 | return 1; |
| 1846 | } |
| 1847 | break; |
| 1848 | } else { |
| 1849 | /* check the key */ |
| 1850 | for (i = 0; i < list->keys_size; ++i) { |
| 1851 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1852 | continue; |
| 1853 | } |
| 1854 | if (model) { |
| 1855 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1856 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1857 | continue; |
| 1858 | } |
| 1859 | } else { |
| 1860 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1861 | continue; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | /* match */ |
| 1866 | break; |
| 1867 | } |
| 1868 | |
| 1869 | if (i == list->keys_size) { |
| 1870 | return 1; |
| 1871 | } |
| 1872 | } |
| 1873 | } while (has_predicate); |
| 1874 | |
| 1875 | if (!nodeid[0]) { |
| 1876 | *nodeid_end = 1; |
| 1877 | } |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1881 | /* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1882 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1883 | int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1884 | resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module, |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1885 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1886 | { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1887 | const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL; |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1888 | const struct lys_node *sibling, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1889 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1890 | int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1891 | int yang_data_name_len, backup_mod_name_len = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1892 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 1893 | const struct lys_module *start_mod, *aux_mod = NULL; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1894 | char *str; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1895 | struct ly_ctx *ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1896 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1897 | assert(nodeid && (start_parent || cur_module) && ret); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1898 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1899 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1900 | if (!cur_module) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1901 | cur_module = lys_node_module(start_parent); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1902 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1903 | ctx = cur_module->ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1904 | id = nodeid; |
| 1905 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 1906 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1); |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1907 | if (r < 1) { |
| 1908 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | if (name[0] == '#') { |
| 1913 | if (is_relative) { |
| 1914 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 1915 | return -1; |
| 1916 | } |
| 1917 | yang_data_name = name + 1; |
| 1918 | yang_data_name_len = nam_len - 1; |
| 1919 | backup_mod_name = mod_name; |
| 1920 | backup_mod_name_len = mod_name_len; |
| 1921 | id += r; |
| 1922 | } else { |
| 1923 | is_relative = -1; |
| 1924 | } |
| 1925 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1926 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1927 | (extended ? &all_desc : NULL), extended); |
| 1928 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1929 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1930 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1931 | } |
| 1932 | id += r; |
| 1933 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1934 | if (backup_mod_name) { |
| 1935 | mod_name = backup_mod_name; |
| 1936 | mod_name_len = backup_mod_name_len; |
| 1937 | } |
| 1938 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1939 | if (is_relative && !start_parent) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1940 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths."); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1941 | return -1; |
| 1942 | } |
| 1943 | |
| 1944 | /* descendant-schema-nodeid */ |
| 1945 | if (is_relative) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1946 | cur_module = start_mod = lys_node_module(start_parent); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1947 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1948 | /* absolute-schema-nodeid */ |
| 1949 | } else { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1950 | start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1951 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1952 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1953 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1954 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1955 | return -1; |
| 1956 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1957 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1958 | if (yang_data_name) { |
| 1959 | start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len); |
| 1960 | if (!start_parent) { |
| 1961 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 1962 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1963 | free(str); |
| 1964 | return -1; |
| 1965 | } |
| 1966 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | while (1) { |
| 1970 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1971 | last_aug = NULL; |
| 1972 | |
| 1973 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1974 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1975 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1976 | /* we are getting into another module (augment) */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1977 | aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1978 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1979 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1980 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1981 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1982 | return -1; |
| 1983 | } |
| 1984 | } else { |
Michal Vasko | 201c339 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1985 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1986 | * because this module may be not implemented and it augments something in another module and |
| 1987 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1988 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Michal Vasko | 3b2ad46 | 2018-07-10 15:40:53 +0200 | [diff] [blame] | 1991 | /* look into augments */ |
| 1992 | if (!extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1993 | get_next_augment: |
| 1994 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod, |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 1999 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2000 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 2001 | |
| 2002 | /* resolve predicate */ |
| 2003 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 2004 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 2005 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2006 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2007 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2008 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2009 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2010 | } else if (!id[0]) { |
| 2011 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2012 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2013 | |
| 2014 | if (r == 0) { |
| 2015 | /* one matching result */ |
| 2016 | if (nodeid_end) { |
| 2017 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2018 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2019 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2020 | } else { |
| 2021 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2022 | return -1; |
| 2023 | } |
| 2024 | start_parent = sibling; |
| 2025 | } |
| 2026 | break; |
| 2027 | } else if (r == 1) { |
| 2028 | continue; |
| 2029 | } else if (r == 2) { |
| 2030 | /* "*" */ |
| 2031 | if (!*ret) { |
| 2032 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2033 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2034 | } |
| 2035 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2036 | if (all_desc) { |
| 2037 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 2038 | if (elem != sibling) { |
| 2039 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 2040 | } |
| 2041 | |
| 2042 | LY_TREE_DFS_END(sibling, next, elem); |
| 2043 | } |
| 2044 | } |
| 2045 | } else if (r == 3) { |
| 2046 | /* "." */ |
| 2047 | if (!*ret) { |
| 2048 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2049 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2050 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 2051 | } |
| 2052 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2053 | if (all_desc) { |
| 2054 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 2055 | if (elem != sibling) { |
| 2056 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 2057 | } |
| 2058 | |
| 2059 | LY_TREE_DFS_END(sibling, next, elem); |
| 2060 | } |
| 2061 | } |
| 2062 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2063 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2064 | return -1; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /* skip predicate */ |
| 2069 | if (extended && has_predicate) { |
| 2070 | while (id[0] == '[') { |
| 2071 | id = strchr(id, ']'); |
| 2072 | if (!id) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2073 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2074 | return -1; |
| 2075 | } |
| 2076 | ++id; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 2081 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* no match */ |
| 2085 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 2086 | if (last_aug) { |
| 2087 | /* it still could be in another augment */ |
| 2088 | goto get_next_augment; |
| 2089 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2090 | if (no_node_error) { |
| 2091 | str = strndup(nodeid, (name - nodeid) + nam_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2092 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2093 | free(str); |
| 2094 | return -1; |
| 2095 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2096 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2097 | return EXIT_SUCCESS; |
| 2098 | } |
| 2099 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2100 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 2101 | (extended ? &all_desc : NULL), extended); |
| 2102 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2103 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2104 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2105 | } |
| 2106 | id += r; |
| 2107 | } |
| 2108 | |
| 2109 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2110 | LOGINT(ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2111 | return -1; |
| 2112 | } |
| 2113 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2114 | /* unique, refine, |
| 2115 | * >0 - unexpected char on position (ret - 1), |
| 2116 | * 0 - ok (but ret can still be NULL), |
| 2117 | * -1 - error, |
| 2118 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2119 | int |
| 2120 | 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] | 2121 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2122 | { |
| 2123 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2124 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2125 | int r, nam_len, mod_name_len, is_relative = -1; |
| 2126 | /* 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] | 2127 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2128 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2129 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 2130 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2131 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2132 | if (!start) { |
| 2133 | /* leaf not found */ |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2137 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2138 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2139 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2140 | 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] | 2141 | return ((id - nodeid) - r) + 1; |
| 2142 | } |
| 2143 | id += r; |
| 2144 | |
| 2145 | if (!is_relative) { |
| 2146 | return -1; |
| 2147 | } |
| 2148 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2149 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 2150 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2151 | start_parent = lys_parent(start_parent); |
| 2152 | } |
| 2153 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2154 | while (1) { |
| 2155 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2156 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 2157 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2158 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2159 | if (r == 0) { |
| 2160 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2161 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2162 | /* wrong node type, too bad */ |
| 2163 | continue; |
| 2164 | } |
| 2165 | *ret = sibling; |
| 2166 | return EXIT_SUCCESS; |
| 2167 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2168 | start_parent = sibling; |
| 2169 | break; |
| 2170 | } else if (r == 1) { |
| 2171 | continue; |
| 2172 | } else { |
| 2173 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | /* no match */ |
| 2178 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2179 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2180 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2181 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 2182 | *ret = NULL; |
| 2183 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2186 | 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] | 2187 | return ((id - nodeid) - r) + 1; |
| 2188 | } |
| 2189 | id += r; |
| 2190 | } |
| 2191 | |
| 2192 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2193 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2194 | return -1; |
| 2195 | } |
| 2196 | |
| 2197 | /* choice default */ |
| 2198 | int |
| 2199 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2200 | { |
| 2201 | /* cannot actually be a path */ |
| 2202 | if (strchr(nodeid, '/')) { |
| 2203 | return -1; |
| 2204 | } |
| 2205 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2206 | 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] | 2207 | } |
| 2208 | |
| 2209 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2210 | static int |
| 2211 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2212 | { |
| 2213 | const struct lys_module *module; |
| 2214 | const char *mod_prefix, *name; |
| 2215 | int i, mod_prefix_len, nam_len; |
| 2216 | |
| 2217 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2218 | 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] | 2219 | return -i + 1; |
| 2220 | } |
| 2221 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2222 | module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2223 | if (!module) { |
| 2224 | return -1; |
| 2225 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2226 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2227 | start = module->data; |
| 2228 | } |
| 2229 | |
| 2230 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2231 | |
| 2232 | return EXIT_SUCCESS; |
| 2233 | } |
| 2234 | |
| 2235 | int |
| 2236 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2237 | const struct lys_node **ret) |
| 2238 | { |
| 2239 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2240 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2241 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2242 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2243 | |
| 2244 | assert(nodeid && module && ret); |
| 2245 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2246 | |
| 2247 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2248 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2249 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2250 | 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] | 2251 | return ((id - nodeid) - r) + 1; |
| 2252 | } |
| 2253 | id += r; |
| 2254 | |
| 2255 | if (is_relative) { |
| 2256 | return -1; |
| 2257 | } |
| 2258 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2259 | abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 2260 | if (!abs_start_mod) { |
| 2261 | return -1; |
| 2262 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2263 | |
| 2264 | while (1) { |
| 2265 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2266 | while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 2267 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2268 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2269 | if (r == 0) { |
| 2270 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2271 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2272 | /* wrong node type, too bad */ |
| 2273 | continue; |
| 2274 | } |
| 2275 | *ret = sibling; |
| 2276 | return EXIT_SUCCESS; |
| 2277 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2278 | start_parent = sibling; |
| 2279 | break; |
| 2280 | } else if (r == 1) { |
| 2281 | continue; |
| 2282 | } else { |
| 2283 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | /* no match */ |
| 2288 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2289 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2290 | return EXIT_SUCCESS; |
| 2291 | } |
| 2292 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2293 | 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] | 2294 | return ((id - nodeid) - r) + 1; |
| 2295 | } |
| 2296 | id += r; |
| 2297 | } |
| 2298 | |
| 2299 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2300 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2301 | return -1; |
| 2302 | } |
| 2303 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2304 | static int |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2305 | 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] | 2306 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2307 | const char *mod_name, *name; |
| 2308 | int mod_name_len, nam_len, has_predicate, i; |
| 2309 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2310 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2311 | 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] | 2312 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2313 | LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2314 | return -1; |
| 2315 | } |
| 2316 | |
| 2317 | predicate += i; |
| 2318 | *parsed += i; |
| 2319 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2320 | if (!isdigit(name[0])) { |
| 2321 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2322 | key = (struct lys_node *)list->keys[i]; |
| 2323 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2324 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2325 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2326 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2327 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2328 | if (i == list->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2329 | LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2330 | return -1; |
| 2331 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | /* more predicates? */ |
| 2335 | if (has_predicate) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2336 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2342 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2343 | const struct lys_node * |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2344 | resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2345 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2346 | char *str; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2347 | const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2348 | const struct lys_node *sibling, *start_parent, *parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2349 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2350 | int yang_data_name_len, backup_mod_name_len; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2351 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2352 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2353 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2354 | assert(nodeid && (ctx || start)); |
| 2355 | if (!ctx) { |
| 2356 | ctx = start->module->ctx; |
| 2357 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2358 | |
| 2359 | id = nodeid; |
| 2360 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2361 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2362 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2363 | return NULL; |
| 2364 | } |
| 2365 | |
| 2366 | if (name[0] == '#') { |
| 2367 | if (is_relative) { |
| 2368 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 2369 | return NULL; |
| 2370 | } |
| 2371 | yang_data_name = name + 1; |
| 2372 | yang_data_name_len = nam_len - 1; |
| 2373 | backup_mod_name = mod_name; |
| 2374 | backup_mod_name_len = mod_name_len; |
| 2375 | id += r; |
| 2376 | } else { |
| 2377 | is_relative = -1; |
| 2378 | } |
| 2379 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2380 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2381 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2382 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2383 | } |
| 2384 | id += r; |
| 2385 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2386 | if (backup_mod_name) { |
| 2387 | mod_name = backup_mod_name; |
| 2388 | mod_name_len = backup_mod_name_len; |
| 2389 | } |
| 2390 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2391 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2392 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2393 | start_parent = start; |
| 2394 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2395 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2396 | } |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2397 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2398 | } else { |
| 2399 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2400 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2401 | LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2402 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2403 | return NULL; |
| 2404 | } |
| 2405 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2406 | str = strndup(mod_name, mod_name_len); |
| 2407 | module = ly_ctx_get_module(ctx, str, NULL, 1); |
| 2408 | free(str); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2409 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2410 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2411 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2412 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2413 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2414 | return NULL; |
| 2415 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2416 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2417 | if (yang_data_name) { |
| 2418 | start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len); |
| 2419 | if (!start_parent) { |
| 2420 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 2421 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2422 | free(str); |
| 2423 | return NULL; |
| 2424 | } |
| 2425 | } |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2426 | |
| 2427 | /* now it's as if there was no module name */ |
| 2428 | mod_name = NULL; |
| 2429 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2430 | } |
| 2431 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2432 | prev_mod = module; |
| 2433 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2434 | while (1) { |
| 2435 | sibling = NULL; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2436 | while ((sibling = lys_getnext(sibling, start_parent, module, 0))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2437 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2438 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2439 | /* output check */ |
| 2440 | for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2441 | if (parent) { |
| 2442 | if (output && (parent->nodetype == LYS_INPUT)) { |
| 2443 | continue; |
| 2444 | } else if (!output && (parent->nodetype == LYS_OUTPUT)) { |
| 2445 | continue; |
| 2446 | } |
| 2447 | } |
| 2448 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2449 | /* module check */ |
| 2450 | if (mod_name) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2451 | /* will also find an augment module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2452 | prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2453 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2454 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2455 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2456 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2457 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2458 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2459 | } |
| 2460 | } else { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2461 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2462 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2463 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2464 | continue; |
| 2465 | } |
| 2466 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2467 | /* do we have some predicates on it? */ |
| 2468 | if (has_predicate) { |
| 2469 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2470 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2471 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2472 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2473 | return NULL; |
| 2474 | } |
| 2475 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2476 | 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] | 2477 | return NULL; |
| 2478 | } |
| 2479 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2480 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2481 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2482 | } |
| 2483 | id += r; |
| 2484 | } |
| 2485 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2486 | /* the result node? */ |
| 2487 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2488 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2489 | } |
| 2490 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2491 | /* move down the tree, if possible */ |
| 2492 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2493 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2494 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2495 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2496 | start_parent = sibling; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2497 | |
| 2498 | /* update prev mod */ |
| 2499 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | /* no match */ |
| 2505 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2506 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2507 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2508 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2509 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2512 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2513 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2514 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2515 | } |
| 2516 | id += r; |
| 2517 | } |
| 2518 | |
| 2519 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2520 | LOGINT(ctx); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2521 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2524 | static int |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2525 | resolve_partial_json_data_list_predicate(struct parsed_pred pp, struct lyd_node *node, int position) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2526 | { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2527 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2528 | struct lyd_node_leaf_list *key; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2529 | struct lys_node_list *slist; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2530 | struct ly_ctx *ctx; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2531 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2532 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2533 | assert(node->schema->nodetype == LYS_LIST); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2534 | assert(pp.len); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2535 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2536 | ctx = node->schema->module->ctx; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2537 | slist = (struct lys_node_list *)node->schema; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2538 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2539 | /* is the predicate a number? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2540 | if (isdigit(pp.pred[0].name[0])) { |
| 2541 | if (position == atoi(pp.pred[0].name)) { |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2542 | /* match */ |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2543 | return 0; |
| 2544 | } else { |
| 2545 | /* not a match */ |
| 2546 | return 1; |
| 2547 | } |
| 2548 | } |
| 2549 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2550 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2551 | if (!key) { |
| 2552 | /* it is not a position, so we need a key for it to be a match */ |
| 2553 | return 1; |
| 2554 | } |
| 2555 | |
| 2556 | /* go through all the keys */ |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2557 | for (i = 0; i < slist->keys_size; ++i) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2558 | if (strncmp(key->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || key->schema->name[pp.pred[i].nam_len]) { |
| 2559 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2560 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2561 | } |
| 2562 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2563 | if (pp.pred[i].mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2564 | /* specific module, check that the found key is from that module */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2565 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len) |
| 2566 | || lyd_node_module((struct lyd_node *)key)->name[pp.pred[i].mod_name_len]) { |
| 2567 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2568 | return -1; |
| 2569 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2570 | |
| 2571 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2572 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2573 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2574 | return -1; |
| 2575 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2576 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2577 | /* no module, so it must be the same as the list (parent) */ |
| 2578 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2579 | LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2580 | return -1; |
| 2581 | } |
| 2582 | } |
| 2583 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2584 | /* value does not match */ |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2585 | if (strncmp(key->value_str, pp.pred[i].value, pp.pred[i].val_len) || key->value_str[pp.pred[i].val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2586 | return 1; |
| 2587 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2588 | |
| 2589 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2592 | return 0; |
| 2593 | } |
| 2594 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2595 | /** |
| 2596 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2597 | * |
| 2598 | * @param[in] nodeid Node data path to find |
| 2599 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2600 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2601 | * @param[out] parsed Number of characters processed in \p id |
| 2602 | * @return The closes parent (or the node itself) from the path |
| 2603 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2604 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2605 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2606 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2607 | { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2608 | const char *id, *mod_name, *name, *data_val, *llval; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2609 | int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2610 | int has_predicate, last_parsed = 0, llval_len; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2611 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2612 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2613 | const struct lys_module *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2614 | struct ly_ctx *ctx; |
Michal Vasko | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2615 | const struct lys_node *ssibling, *sparent; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2616 | struct lys_node_list *slist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2617 | struct parsed_pred pp; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2618 | |
| 2619 | assert(nodeid && start && parsed); |
| 2620 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2621 | memset(&pp, 0, sizeof pp); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2622 | ctx = start->schema->module->ctx; |
| 2623 | id = nodeid; |
| 2624 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2625 | /* parse first nodeid in case it is yang-data extension */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2626 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2627 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2628 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | if (name[0] == '#') { |
| 2632 | if (is_relative) { |
| 2633 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2634 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2635 | } |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2636 | id += r; |
| 2637 | last_parsed = r; |
| 2638 | } else { |
| 2639 | is_relative = -1; |
| 2640 | } |
| 2641 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2642 | /* parse first nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2643 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2644 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2645 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2646 | } |
| 2647 | id += r; |
| 2648 | /* add it to parsed only after the data node was actually found */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2649 | last_parsed += r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2650 | |
| 2651 | if (is_relative) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2652 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2653 | start = start->child; |
| 2654 | } else { |
| 2655 | for (; start->parent; start = start->parent); |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2656 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2659 | /* do not duplicate code, use predicate parsing from the loop */ |
| 2660 | goto parse_predicates; |
| 2661 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2662 | while (1) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2663 | /* find the correct schema node first */ |
| 2664 | ssibling = NULL; |
Michal Vasko | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2665 | sparent = (start && start->parent) ? start->parent->schema : NULL; |
| 2666 | while ((ssibling = lys_getnext(ssibling, sparent, prev_mod, 0))) { |
| 2667 | /* skip invalid input/output nodes */ |
| 2668 | if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 2669 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2670 | if (lys_parent(ssibling)->nodetype == LYS_INPUT) { |
| 2671 | continue; |
| 2672 | } |
| 2673 | } else { |
| 2674 | if (lys_parent(ssibling)->nodetype == LYS_OUTPUT) { |
| 2675 | continue; |
| 2676 | } |
| 2677 | } |
| 2678 | } |
| 2679 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2680 | if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) { |
| 2681 | break; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2682 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2683 | } |
| 2684 | if (!ssibling) { |
| 2685 | /* there is not even such a schema node */ |
| 2686 | free(pp.pred); |
| 2687 | return last_match; |
| 2688 | } |
| 2689 | pp.schema = ssibling; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2690 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2691 | /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if |
| 2692 | * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */ |
| 2693 | if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) { |
| 2694 | pp.len = 1; |
| 2695 | pp.pred = calloc(1, sizeof *pp.pred); |
| 2696 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2697 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2698 | pp.pred[0].name = "."; |
| 2699 | pp.pred[0].nam_len = 1; |
| 2700 | pp.pred[0].value = (llist_value ? llist_value : ""); |
| 2701 | pp.pred[0].val_len = strlen(pp.pred[0].value); |
| 2702 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2703 | |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2704 | if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) { |
| 2705 | /* check leaf/leaf-list predicate */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2706 | if (pp.len > 1) { |
| 2707 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2708 | goto error; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2709 | } else if (pp.len) { |
| 2710 | if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) { |
| 2711 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name); |
| 2712 | goto error; |
| 2713 | } |
| 2714 | if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) { |
| 2715 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value); |
| 2716 | goto error; |
| 2717 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2718 | } |
| 2719 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2720 | /* list should have predicates for all the keys or position */ |
| 2721 | slist = (struct lys_node_list *)ssibling; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2722 | if (!pp.len) { |
| 2723 | /* none match */ |
| 2724 | return last_match; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2725 | } else if (!isdigit(pp.pred[0].name[0])) { |
| 2726 | /* list predicate is not a position, so there must be all the keys */ |
| 2727 | if (pp.len > slist->keys_size) { |
| 2728 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2729 | goto error; |
| 2730 | } else if (pp.len < slist->keys_size) { |
| 2731 | LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name); |
| 2732 | goto error; |
| 2733 | } |
| 2734 | /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */ |
| 2735 | for (r = 0; r < pp.len; ++r) { |
| 2736 | if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) { |
| 2737 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value); |
| 2738 | goto error; |
| 2739 | } |
| 2740 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2741 | } |
| 2742 | } else if (pp.pred) { |
| 2743 | /* no other nodes allow predicates */ |
| 2744 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2745 | goto error; |
| 2746 | } |
| 2747 | |
| 2748 | #ifdef LY_ENABLED_CACHE |
| 2749 | /* we will not be matching keyless lists or state leaf-lists this way */ |
| 2750 | if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size) |
| 2751 | && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) { |
| 2752 | sibling = resolve_json_data_node_hash(start->parent, pp); |
| 2753 | } else |
| 2754 | #endif |
| 2755 | { |
| 2756 | list_instance_position = 0; |
| 2757 | LY_TREE_FOR(start, sibling) { |
| 2758 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
| 2759 | if (lys_parent(sibling->schema)) { |
| 2760 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2761 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
| 2762 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
| 2763 | goto error; |
| 2764 | } |
| 2765 | } else { |
| 2766 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
| 2767 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
| 2768 | goto error; |
| 2769 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2770 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2771 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2772 | |
| 2773 | if (sibling->schema != ssibling) { |
| 2774 | /* wrong schema node */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2775 | continue; |
| 2776 | } |
| 2777 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2778 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2779 | if (ssibling->nodetype == LYS_LEAFLIST) { |
| 2780 | if (ssibling->flags & LYS_CONFIG_R) { |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 2781 | /* state leaf-lists will never match */ |
| 2782 | continue; |
| 2783 | } |
| 2784 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2785 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2786 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2787 | /* get the expected leaf-list value */ |
| 2788 | llval = NULL; |
| 2789 | llval_len = 0; |
| 2790 | if (pp.pred) { |
| 2791 | /* it was already checked that it is correct */ |
| 2792 | llval = pp.pred[0].value; |
| 2793 | llval_len = pp.pred[0].val_len; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2794 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Michal Vasko | 21b90ce | 2017-09-19 09:38:27 +0200 | [diff] [blame] | 2797 | /* make value canonical (remove module name prefix) unless it was specified with it */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2798 | if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2799 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2800 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2801 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2802 | } else { |
| 2803 | data_val = llist->value_str; |
| 2804 | } |
| 2805 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2806 | if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len) |
| 2807 | || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2808 | continue; |
| 2809 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2810 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2811 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2812 | /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */ |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2813 | ++list_instance_position; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2814 | ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2815 | if (ret == -1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2816 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2817 | } else if (ret == 1) { |
| 2818 | /* this list instance does not match */ |
| 2819 | continue; |
| 2820 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2821 | } |
| 2822 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2823 | break; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | /* no match, return last match */ |
| 2828 | if (!sibling) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2829 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2830 | return last_match; |
| 2831 | } |
| 2832 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2833 | /* we found a next matching node */ |
| 2834 | *parsed += last_parsed; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2835 | last_match = sibling; |
| 2836 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2837 | |
| 2838 | /* the result node? */ |
| 2839 | if (!id[0]) { |
| 2840 | free(pp.pred); |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2841 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2844 | /* move down the tree, if possible, and continue */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2845 | if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2846 | /* there can be no children even through expected, error */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2847 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2848 | goto error; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2849 | } else if (!sibling->child) { |
| 2850 | /* there could be some children, but are not, return what we found so far */ |
| 2851 | free(pp.pred); |
| 2852 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2853 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2854 | start = sibling->child; |
| 2855 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2856 | /* parse nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2857 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2858 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2859 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2860 | } |
| 2861 | id += r; |
| 2862 | last_parsed = r; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2863 | |
| 2864 | parse_predicates: |
| 2865 | /* parse all the predicates */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2866 | free(pp.pred); |
| 2867 | pp.schema = NULL; |
| 2868 | pp.len = 0; |
| 2869 | pp.pred = NULL; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2870 | while (has_predicate) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2871 | ++pp.len; |
| 2872 | pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred); |
| 2873 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
| 2874 | if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len, |
| 2875 | &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value, |
| 2876 | &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2877 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2878 | goto error; |
| 2879 | } |
| 2880 | |
| 2881 | id += r; |
| 2882 | last_parsed += r; |
| 2883 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2884 | } |
| 2885 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2886 | error: |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2887 | *parsed = -1; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2888 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2889 | return NULL; |
| 2890 | } |
| 2891 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2892 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2893 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2894 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2895 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2896 | * @param[in] ctx Context for errors. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2897 | * @param[in] str_restr Restriction as a string. |
| 2898 | * @param[in] type Type of the restriction. |
| 2899 | * @param[out] ret Final interval structure that starts with |
| 2900 | * the interval of the initial type, continues with intervals |
| 2901 | * of any superior types derived from the initial one, and |
| 2902 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2903 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2904 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2905 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2906 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2907 | resolve_len_ran_interval(struct ly_ctx *ctx, 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] | 2908 | { |
| 2909 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2910 | int kind; |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 2911 | int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax; |
| 2912 | uint64_t local_umin, local_umax = 0; |
| 2913 | uint8_t local_fdig = 0; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2914 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2915 | 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] | 2916 | |
| 2917 | switch (type->base) { |
| 2918 | case LY_TYPE_BINARY: |
| 2919 | kind = 0; |
| 2920 | local_umin = 0; |
| 2921 | local_umax = 18446744073709551615UL; |
| 2922 | |
| 2923 | if (!str_restr && type->info.binary.length) { |
| 2924 | str_restr = type->info.binary.length->expr; |
| 2925 | } |
| 2926 | break; |
| 2927 | case LY_TYPE_DEC64: |
| 2928 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2929 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2930 | local_fmax = __INT64_C(9223372036854775807); |
| 2931 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2932 | |
| 2933 | if (!str_restr && type->info.dec64.range) { |
| 2934 | str_restr = type->info.dec64.range->expr; |
| 2935 | } |
| 2936 | break; |
| 2937 | case LY_TYPE_INT8: |
| 2938 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2939 | local_smin = __INT64_C(-128); |
| 2940 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2941 | |
| 2942 | if (!str_restr && type->info.num.range) { |
| 2943 | str_restr = type->info.num.range->expr; |
| 2944 | } |
| 2945 | break; |
| 2946 | case LY_TYPE_INT16: |
| 2947 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2948 | local_smin = __INT64_C(-32768); |
| 2949 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2950 | |
| 2951 | if (!str_restr && type->info.num.range) { |
| 2952 | str_restr = type->info.num.range->expr; |
| 2953 | } |
| 2954 | break; |
| 2955 | case LY_TYPE_INT32: |
| 2956 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2957 | local_smin = __INT64_C(-2147483648); |
| 2958 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2959 | |
| 2960 | if (!str_restr && type->info.num.range) { |
| 2961 | str_restr = type->info.num.range->expr; |
| 2962 | } |
| 2963 | break; |
| 2964 | case LY_TYPE_INT64: |
| 2965 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2966 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2967 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2968 | |
| 2969 | if (!str_restr && type->info.num.range) { |
| 2970 | str_restr = type->info.num.range->expr; |
| 2971 | } |
| 2972 | break; |
| 2973 | case LY_TYPE_UINT8: |
| 2974 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2975 | local_umin = __UINT64_C(0); |
| 2976 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2977 | |
| 2978 | if (!str_restr && type->info.num.range) { |
| 2979 | str_restr = type->info.num.range->expr; |
| 2980 | } |
| 2981 | break; |
| 2982 | case LY_TYPE_UINT16: |
| 2983 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2984 | local_umin = __UINT64_C(0); |
| 2985 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2986 | |
| 2987 | if (!str_restr && type->info.num.range) { |
| 2988 | str_restr = type->info.num.range->expr; |
| 2989 | } |
| 2990 | break; |
| 2991 | case LY_TYPE_UINT32: |
| 2992 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2993 | local_umin = __UINT64_C(0); |
| 2994 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2995 | |
| 2996 | if (!str_restr && type->info.num.range) { |
| 2997 | str_restr = type->info.num.range->expr; |
| 2998 | } |
| 2999 | break; |
| 3000 | case LY_TYPE_UINT64: |
| 3001 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 3002 | local_umin = __UINT64_C(0); |
| 3003 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3004 | |
| 3005 | if (!str_restr && type->info.num.range) { |
| 3006 | str_restr = type->info.num.range->expr; |
| 3007 | } |
| 3008 | break; |
| 3009 | case LY_TYPE_STRING: |
| 3010 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 3011 | local_umin = __UINT64_C(0); |
| 3012 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3013 | |
| 3014 | if (!str_restr && type->info.str.length) { |
| 3015 | str_restr = type->info.str.length->expr; |
| 3016 | } |
| 3017 | break; |
| 3018 | default: |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3019 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3023 | if (type->der) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3024 | if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3025 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 3026 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3027 | assert(!intv || (intv->kind == kind)); |
| 3028 | } |
| 3029 | |
| 3030 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3031 | /* we do not have any restriction, return superior ones */ |
| 3032 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3033 | return EXIT_SUCCESS; |
| 3034 | } |
| 3035 | |
| 3036 | /* adjust local min and max */ |
| 3037 | if (intv) { |
| 3038 | tmp_intv = intv; |
| 3039 | |
| 3040 | if (kind == 0) { |
| 3041 | local_umin = tmp_intv->value.uval.min; |
| 3042 | } else if (kind == 1) { |
| 3043 | local_smin = tmp_intv->value.sval.min; |
| 3044 | } else if (kind == 2) { |
| 3045 | local_fmin = tmp_intv->value.fval.min; |
| 3046 | } |
| 3047 | |
| 3048 | while (tmp_intv->next) { |
| 3049 | tmp_intv = tmp_intv->next; |
| 3050 | } |
| 3051 | |
| 3052 | if (kind == 0) { |
| 3053 | local_umax = tmp_intv->value.uval.max; |
| 3054 | } else if (kind == 1) { |
| 3055 | local_smax = tmp_intv->value.sval.max; |
| 3056 | } else if (kind == 2) { |
| 3057 | local_fmax = tmp_intv->value.fval.max; |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* finally parse our restriction */ |
| 3062 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3063 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3064 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3065 | if (!tmp_local_intv) { |
| 3066 | assert(!local_intv); |
| 3067 | local_intv = malloc(sizeof *local_intv); |
| 3068 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3069 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3070 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3071 | tmp_local_intv = tmp_local_intv->next; |
| 3072 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3073 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3074 | |
| 3075 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3076 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3077 | tmp_local_intv->next = NULL; |
| 3078 | |
| 3079 | /* min */ |
| 3080 | ptr = seg_ptr; |
| 3081 | while (isspace(ptr[0])) { |
| 3082 | ++ptr; |
| 3083 | } |
| 3084 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3085 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3086 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3087 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3088 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3089 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3090 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3091 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3092 | goto error; |
| 3093 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3094 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3095 | } else if (!strncmp(ptr, "min", 3)) { |
| 3096 | if (kind == 0) { |
| 3097 | tmp_local_intv->value.uval.min = local_umin; |
| 3098 | } else if (kind == 1) { |
| 3099 | tmp_local_intv->value.sval.min = local_smin; |
| 3100 | } else if (kind == 2) { |
| 3101 | tmp_local_intv->value.fval.min = local_fmin; |
| 3102 | } |
| 3103 | |
| 3104 | ptr += 3; |
| 3105 | } else if (!strncmp(ptr, "max", 3)) { |
| 3106 | if (kind == 0) { |
| 3107 | tmp_local_intv->value.uval.min = local_umax; |
| 3108 | } else if (kind == 1) { |
| 3109 | tmp_local_intv->value.sval.min = local_smax; |
| 3110 | } else if (kind == 2) { |
| 3111 | tmp_local_intv->value.fval.min = local_fmax; |
| 3112 | } |
| 3113 | |
| 3114 | ptr += 3; |
| 3115 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3116 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | while (isspace(ptr[0])) { |
| 3120 | ptr++; |
| 3121 | } |
| 3122 | |
| 3123 | /* no interval or interval */ |
| 3124 | if ((ptr[0] == '|') || !ptr[0]) { |
| 3125 | if (kind == 0) { |
| 3126 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 3127 | } else if (kind == 1) { |
| 3128 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 3129 | } else if (kind == 2) { |
| 3130 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 3131 | } |
| 3132 | } else if (!strncmp(ptr, "..", 2)) { |
| 3133 | /* skip ".." */ |
| 3134 | ptr += 2; |
| 3135 | while (isspace(ptr[0])) { |
| 3136 | ++ptr; |
| 3137 | } |
| 3138 | |
| 3139 | /* max */ |
| 3140 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3141 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3142 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3143 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3144 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3145 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3146 | if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3147 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3148 | goto error; |
| 3149 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3150 | } |
| 3151 | } else if (!strncmp(ptr, "max", 3)) { |
| 3152 | if (kind == 0) { |
| 3153 | tmp_local_intv->value.uval.max = local_umax; |
| 3154 | } else if (kind == 1) { |
| 3155 | tmp_local_intv->value.sval.max = local_smax; |
| 3156 | } else if (kind == 2) { |
| 3157 | tmp_local_intv->value.fval.max = local_fmax; |
| 3158 | } |
| 3159 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3160 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3161 | } |
| 3162 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3163 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3166 | /* check min and max in correct order*/ |
| 3167 | if (kind == 0) { |
| 3168 | /* current segment */ |
| 3169 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 3170 | goto error; |
| 3171 | } |
| 3172 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 3173 | goto error; |
| 3174 | } |
| 3175 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3176 | 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] | 3177 | goto error; |
| 3178 | } |
| 3179 | } else if (kind == 1) { |
| 3180 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 3181 | goto error; |
| 3182 | } |
| 3183 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 3184 | goto error; |
| 3185 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3186 | 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] | 3187 | goto error; |
| 3188 | } |
| 3189 | } else if (kind == 2) { |
| 3190 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 3191 | goto error; |
| 3192 | } |
| 3193 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 3194 | goto error; |
| 3195 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3196 | 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] | 3197 | /* 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] | 3198 | goto error; |
| 3199 | } |
| 3200 | } |
| 3201 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3202 | /* next segment (next OR) */ |
| 3203 | seg_ptr = strchr(seg_ptr, '|'); |
| 3204 | if (!seg_ptr) { |
| 3205 | break; |
| 3206 | } |
| 3207 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3208 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | /* check local restrictions against superior ones */ |
| 3212 | if (intv) { |
| 3213 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3214 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3215 | |
| 3216 | while (tmp_local_intv && tmp_intv) { |
| 3217 | /* reuse local variables */ |
| 3218 | if (kind == 0) { |
| 3219 | local_umin = tmp_local_intv->value.uval.min; |
| 3220 | local_umax = tmp_local_intv->value.uval.max; |
| 3221 | |
| 3222 | /* it must be in this interval */ |
| 3223 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 3224 | /* this interval is covered, next one */ |
| 3225 | if (local_umax <= tmp_intv->value.uval.max) { |
| 3226 | tmp_local_intv = tmp_local_intv->next; |
| 3227 | continue; |
| 3228 | /* ascending order of restrictions -> fail */ |
| 3229 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3230 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | } else if (kind == 1) { |
| 3234 | local_smin = tmp_local_intv->value.sval.min; |
| 3235 | local_smax = tmp_local_intv->value.sval.max; |
| 3236 | |
| 3237 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 3238 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3239 | tmp_local_intv = tmp_local_intv->next; |
| 3240 | continue; |
| 3241 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3242 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3243 | } |
| 3244 | } |
| 3245 | } else if (kind == 2) { |
| 3246 | local_fmin = tmp_local_intv->value.fval.min; |
| 3247 | local_fmax = tmp_local_intv->value.fval.max; |
| 3248 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3249 | 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] | 3250 | && (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] | 3251 | 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] | 3252 | tmp_local_intv = tmp_local_intv->next; |
| 3253 | continue; |
| 3254 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3255 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | tmp_intv = tmp_intv->next; |
| 3261 | } |
| 3262 | |
| 3263 | /* some interval left uncovered -> fail */ |
| 3264 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3265 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3266 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3267 | } |
| 3268 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3269 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3270 | if (intv) { |
| 3271 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3272 | tmp_intv->next = local_intv; |
| 3273 | } else { |
| 3274 | intv = local_intv; |
| 3275 | } |
| 3276 | *ret = intv; |
| 3277 | |
| 3278 | return EXIT_SUCCESS; |
| 3279 | |
| 3280 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3281 | while (intv) { |
| 3282 | tmp_intv = intv->next; |
| 3283 | free(intv); |
| 3284 | intv = tmp_intv; |
| 3285 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3286 | while (local_intv) { |
| 3287 | tmp_local_intv = local_intv->next; |
| 3288 | free(local_intv); |
| 3289 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3292 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3293 | } |
| 3294 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3295 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3296 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3297 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3298 | * |
| 3299 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3300 | * @param[in] mod_name Typedef name module name. |
| 3301 | * @param[in] module Main module. |
| 3302 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3303 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3304 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3305 | * @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] | 3306 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3307 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3308 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3309 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3310 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3311 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3312 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3313 | int tpdf_size; |
| 3314 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3315 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3316 | /* no prefix, try built-in types */ |
| 3317 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3318 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3319 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3320 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3321 | } |
| 3322 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3326 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3327 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3328 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3332 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3333 | /* search in local typedefs */ |
| 3334 | while (parent) { |
| 3335 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3336 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3337 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3338 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3339 | break; |
| 3340 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3341 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3342 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3343 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3344 | break; |
| 3345 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3346 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3347 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3348 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3349 | break; |
| 3350 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3351 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3352 | case LYS_ACTION: |
| 3353 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3354 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3355 | break; |
| 3356 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3357 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3358 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3359 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3360 | break; |
| 3361 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3362 | case LYS_INPUT: |
| 3363 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3364 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3365 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | break; |
| 3367 | |
| 3368 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3369 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3370 | continue; |
| 3371 | } |
| 3372 | |
| 3373 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3374 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3375 | match = &tpdf[i]; |
| 3376 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3380 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3381 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3382 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3383 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3384 | module = lyp_get_module(module, NULL, 0, mod_name, 0, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3385 | if (!module) { |
| 3386 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | /* search in top level typedefs */ |
| 3391 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3392 | 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] | 3393 | match = &module->tpdf[i]; |
| 3394 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3399 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3400 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3401 | 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] | 3402 | match = &module->inc[i].submodule->tpdf[j]; |
| 3403 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3404 | } |
| 3405 | } |
| 3406 | } |
| 3407 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3408 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3409 | |
| 3410 | check_leafref: |
| 3411 | if (ret) { |
| 3412 | *ret = match; |
| 3413 | } |
| 3414 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3415 | while (!match->type.info.lref.path) { |
| 3416 | match = match->type.der; |
| 3417 | assert(match); |
| 3418 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3419 | } |
| 3420 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3423 | /** |
| 3424 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3425 | * |
| 3426 | * @param[in] type Type definition to use. |
| 3427 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3428 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3429 | * |
| 3430 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3431 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3433 | 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] | 3434 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3435 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3436 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3437 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3438 | char *s; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3439 | int ret = EXIT_SUCCESS, r; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3440 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3441 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3442 | assert(value); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3443 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3444 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3445 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3446 | /* the type was not resolved yet, nothing to do for now */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3447 | ret = EXIT_FAILURE; |
| 3448 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3449 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3450 | /* do not check defaults in not implemented module's data */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3451 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3452 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3453 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3454 | * but in typedef the value has to be modified to include the prefix */ |
| 3455 | if (*value) { |
| 3456 | if (strchr(*value, ':')) { |
| 3457 | dflt = transform_schema2json(module, *value); |
| 3458 | } else { |
| 3459 | /* default prefix of the module where the typedef is defined */ |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3460 | if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) { |
| 3461 | LOGMEM(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3462 | ret = -1; |
| 3463 | goto cleanup; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3464 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3465 | dflt = lydict_insert_zc(ctx, s); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3466 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3467 | lydict_remove(ctx, *value); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3468 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3469 | dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3470 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3471 | goto cleanup; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3472 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3473 | /* leafref in typedef cannot be checked */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3474 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3475 | } |
| 3476 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3477 | dflt = lydict_insert(ctx, *value, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3478 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3479 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3480 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3481 | if (base_tpdf->dflt) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3482 | dflt = lydict_insert(ctx, base_tpdf->dflt, 0); |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3483 | break; |
| 3484 | } |
| 3485 | } |
| 3486 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3487 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3488 | /* no default value, nothing to check, all is well */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3489 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | /* 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)? */ |
| 3493 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3494 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3495 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3496 | goto cleanup; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3497 | } else { |
| 3498 | /* check the default value from typedef, but use also the typedef's module |
| 3499 | * due to possible searching in imported modules which is expected in |
| 3500 | * typedef's module instead of module where the typedef is used */ |
| 3501 | module = base_tpdf->module; |
| 3502 | } |
| 3503 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3504 | case LY_TYPE_INST: |
| 3505 | case LY_TYPE_LEAFREF: |
| 3506 | case LY_TYPE_BOOL: |
| 3507 | case LY_TYPE_EMPTY: |
| 3508 | /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3509 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3510 | case LY_TYPE_BITS: |
| 3511 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3512 | if (type->info.bits.count) { |
| 3513 | break; |
| 3514 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3515 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3516 | case LY_TYPE_ENUM: |
| 3517 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3518 | if (type->info.enums.count) { |
| 3519 | break; |
| 3520 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3521 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3522 | case LY_TYPE_DEC64: |
| 3523 | if (type->info.dec64.range) { |
| 3524 | break; |
| 3525 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3526 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3527 | case LY_TYPE_BINARY: |
| 3528 | if (type->info.binary.length) { |
| 3529 | break; |
| 3530 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3531 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3532 | case LY_TYPE_INT8: |
| 3533 | case LY_TYPE_INT16: |
| 3534 | case LY_TYPE_INT32: |
| 3535 | case LY_TYPE_INT64: |
| 3536 | case LY_TYPE_UINT8: |
| 3537 | case LY_TYPE_UINT16: |
| 3538 | case LY_TYPE_UINT32: |
| 3539 | case LY_TYPE_UINT64: |
| 3540 | if (type->info.num.range) { |
| 3541 | break; |
| 3542 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3543 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3544 | case LY_TYPE_STRING: |
| 3545 | if (type->info.str.length || type->info.str.patterns) { |
| 3546 | break; |
| 3547 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3548 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3549 | case LY_TYPE_UNION: |
| 3550 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3551 | break; |
| 3552 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3553 | LOGINT(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3554 | ret = -1; |
| 3555 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3556 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3557 | } else if (type->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3558 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3559 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value."); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3560 | ret = -1; |
| 3561 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3562 | } |
| 3563 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3564 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3565 | memset(&node, 0, sizeof node); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3566 | node.value_str = lydict_insert(ctx, dflt, 0); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3567 | node.value_type = type->base; |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3568 | |
| 3569 | if (tpdf) { |
| 3570 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3571 | if (!node.schema) { |
| 3572 | LOGMEM(ctx); |
| 3573 | ret = -1; |
| 3574 | goto cleanup; |
| 3575 | } |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3576 | r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3577 | if (r == -1) { |
| 3578 | LOGMEM(ctx); |
| 3579 | ret = -1; |
| 3580 | goto cleanup; |
| 3581 | } |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3582 | node.schema->module = module; |
| 3583 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
| 3584 | } else { |
| 3585 | node.schema = (struct lys_node *)type->parent; |
| 3586 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3587 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3588 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3589 | if (!type->info.lref.target) { |
| 3590 | ret = EXIT_FAILURE; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3591 | goto cleanup; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3592 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3593 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3594 | if (!ret) { |
| 3595 | /* adopt possibly changed default value to its canonical form */ |
| 3596 | if (*value) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3597 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3598 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3599 | dflt = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3600 | } |
| 3601 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3602 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 3603 | if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1, 0)) { |
Radek Krejci | 5dca593 | 2016-11-04 14:30:47 +0100 | [diff] [blame] | 3604 | /* possible forward reference */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3605 | ret = EXIT_FAILURE; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3606 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3607 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3608 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3609 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3610 | /* we have refined bits/enums */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3611 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3612 | "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] | 3613 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3614 | } |
| 3615 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3616 | } else { |
| 3617 | /* success - adopt canonical form from the node into the default value */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3618 | if (!ly_strequal(dflt, node.value_str, 1)) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3619 | /* this can happen only if we have non-inherited default value, |
| 3620 | * inherited default values are already in canonical form */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3621 | assert(ly_strequal(dflt, *value, 1)); |
| 3622 | |
| 3623 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3624 | *value = node.value_str; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3625 | node.value_str = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3626 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3627 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3628 | } |
| 3629 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3630 | cleanup: |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame^] | 3631 | lyd_free_value(node.value, node.value_type, node.value_flags, type, NULL, NULL, NULL); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3632 | lydict_remove(ctx, node.value_str); |
| 3633 | if (tpdf && node.schema) { |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3634 | free((char *)node.schema->name); |
| 3635 | free(node.schema); |
| 3636 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3637 | lydict_remove(ctx, dflt); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3638 | |
| 3639 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3640 | } |
| 3641 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3642 | /** |
| 3643 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3644 | * |
| 3645 | * @param[in] key The key to check. |
| 3646 | * @param[in] flags What flags to check. |
| 3647 | * @param[in] list The list of all the keys. |
| 3648 | * @param[in] index Index of the key in the key list. |
| 3649 | * @param[in] name The name of the keys. |
| 3650 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3651 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3652 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3653 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3655 | 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] | 3656 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3657 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3658 | char *dup = NULL; |
| 3659 | int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3660 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3661 | |
| 3662 | /* existence */ |
| 3663 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3664 | if (name[len] != '\0') { |
| 3665 | dup = strdup(name); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3666 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3667 | dup[len] = '\0'; |
| 3668 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3669 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3670 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3671 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3672 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | /* uniqueness */ |
| 3676 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3677 | if (key == list->keys[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3678 | LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3679 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3684 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3685 | LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3686 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | /* type of the leaf is not built-in empty */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 3690 | if (key->type.base == LY_TYPE_EMPTY && key->module->version < LYS_VERSION_1_1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3691 | LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3692 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /* config attribute is the same as of the list */ |
Michal Vasko | 627016e | 2018-10-24 09:25:55 +0200 | [diff] [blame] | 3696 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) |
| 3697 | && ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3698 | LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3699 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3702 | /* key is not placed from augment */ |
| 3703 | if (key->parent->nodetype == LYS_AUGMENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3704 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3705 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3706 | return -1; |
| 3707 | } |
| 3708 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3709 | /* key is not when/if-feature -conditional */ |
| 3710 | j = 0; |
| 3711 | if (key->when || (key->iffeature_size && (j = 1))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3712 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3713 | LOGVAL(ctx, 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] | 3714 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3715 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3718 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3719 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3720 | |
| 3721 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3722 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3723 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3724 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3725 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3726 | * |
| 3727 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3728 | */ |
| 3729 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3730 | 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] | 3731 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3732 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3733 | const struct lys_node *leaf = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3734 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3735 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3736 | 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] | 3737 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3738 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3739 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3740 | if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3741 | LOGVAL(ctx, 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] | 3742 | } else if (rc == -2) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3743 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3744 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3745 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3746 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3747 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3748 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3749 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3750 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3751 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3752 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3753 | if (leaf->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3754 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3755 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3756 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3757 | } |
| 3758 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3759 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3760 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3761 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3762 | return -1; |
| 3763 | } |
| 3764 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3765 | /* check that all unique's targets are of the same config type */ |
| 3766 | if (*trg_type) { |
| 3767 | if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3768 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3769 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3770 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3771 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3772 | return -1; |
| 3773 | } |
| 3774 | } else { |
| 3775 | /* first unique */ |
| 3776 | if (leaf->flags & LYS_CONFIG_W) { |
| 3777 | *trg_type = 1; |
| 3778 | } else { |
| 3779 | *trg_type = 2; |
| 3780 | } |
| 3781 | } |
| 3782 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3783 | /* set leaf's unique flag */ |
| 3784 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3785 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3786 | return EXIT_SUCCESS; |
| 3787 | |
| 3788 | error: |
| 3789 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3790 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3791 | } |
| 3792 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3793 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3794 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3795 | { |
| 3796 | /* there are items after the one deleted */ |
| 3797 | if (i+1 < unres->count) { |
| 3798 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3799 | 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] | 3800 | |
| 3801 | /* deleting the last item */ |
| 3802 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3803 | free(unres->node); |
| 3804 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3808 | --unres->count; |
| 3809 | } |
| 3810 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3811 | /** |
| 3812 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3813 | * |
| 3814 | * @param[in] mod Module to search in. |
| 3815 | * @param[in] name Name of the data node. |
| 3816 | * @param[in] nam_len Length of the name. |
| 3817 | * @param[in] start Data node to start the search from. |
| 3818 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3819 | * they are replaced (!!) with the resolvents. |
| 3820 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3821 | * @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] | 3822 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3823 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3824 | 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] | 3825 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3826 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3827 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3828 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3829 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3830 | if (!parents->count) { |
| 3831 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3832 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3833 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3834 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3835 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3836 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3837 | 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] | 3838 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3839 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3840 | continue; |
| 3841 | } |
| 3842 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3843 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3844 | 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] | 3845 | && node->schema->name[nam_len] == '\0') { |
| 3846 | /* matching target */ |
| 3847 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3848 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3849 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3850 | flag = 1; |
| 3851 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3852 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3853 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3854 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3855 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3856 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3857 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3858 | } |
| 3859 | } |
| 3860 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3861 | |
| 3862 | if (!flag) { |
| 3863 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3864 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3865 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3866 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3867 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3868 | } |
| 3869 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3870 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3873 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3874 | 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] | 3875 | { |
| 3876 | int dep1, dep2; |
| 3877 | const struct lys_node *node; |
| 3878 | |
| 3879 | if (lys_parent(op_node)) { |
| 3880 | /* inner operation (notif/action) */ |
| 3881 | if (abs_path) { |
| 3882 | return 1; |
| 3883 | } else { |
| 3884 | /* compare depth of both nodes */ |
| 3885 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3886 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3887 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3888 | return 1; |
| 3889 | } |
| 3890 | } |
| 3891 | } else { |
| 3892 | /* top-level operation (notif/rpc) */ |
| 3893 | if (op_node != first_node) { |
| 3894 | return 1; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3901 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3902 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3903 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3904 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3905 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3906 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3907 | * @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] | 3908 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3909 | * @return 0 on forward reference, otherwise the number |
| 3910 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3911 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3912 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3913 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3914 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3915 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3916 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3917 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3918 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3919 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3920 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3921 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3922 | struct ly_ctx *ctx = context_node->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3923 | |
| 3924 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3925 | 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] | 3926 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3927 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3928 | return -parsed+i; |
| 3929 | } |
| 3930 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3931 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3932 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3933 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3934 | if (sour_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3935 | trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0); |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3936 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3937 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3938 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3939 | 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] | 3940 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3941 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3942 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3946 | dest_parent_times = 0; |
| 3947 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3948 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3949 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3950 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3951 | return -parsed; |
| 3952 | } |
| 3953 | pke_parsed += i; |
| 3954 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3955 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3956 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3957 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3958 | /* we are in an unresolved augment, cannot evaluate */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3959 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3960 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3964 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3965 | * all schema nodes that cannot be instantiated in data tree */ |
| 3966 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3967 | 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] | 3968 | dst_node = lys_parent(dst_node)); |
| 3969 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3970 | if (!dst_node) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3971 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3972 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3973 | } |
| 3974 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3975 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3976 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3977 | if (dest_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3978 | trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0); |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3979 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3980 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3981 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3982 | 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] | 3983 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3984 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3985 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3988 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3989 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3990 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3991 | } |
| 3992 | first_iter = 0; |
| 3993 | } |
| 3994 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3995 | if (pke_len == pke_parsed) { |
| 3996 | break; |
| 3997 | } |
| 3998 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3999 | 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] | 4000 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 4001 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4002 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4003 | return -parsed; |
| 4004 | } |
| 4005 | pke_parsed += i; |
| 4006 | } |
| 4007 | |
| 4008 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 4009 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 4010 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4011 | LOGVAL(ctx, 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] | 4012 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4013 | return -parsed; |
| 4014 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4015 | } while (has_predicate); |
| 4016 | |
| 4017 | return parsed; |
| 4018 | } |
| 4019 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4020 | static int |
| 4021 | check_leafref_features(struct lys_type *type) |
| 4022 | { |
| 4023 | struct lys_node *iter; |
| 4024 | struct ly_set *src_parents, *trg_parents, *features; |
| 4025 | struct lys_node_augment *aug; |
| 4026 | struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx; |
| 4027 | unsigned int i, j, size, x; |
| 4028 | int ret = EXIT_SUCCESS; |
| 4029 | |
| 4030 | assert(type->parent); |
| 4031 | |
| 4032 | src_parents = ly_set_new(); |
| 4033 | trg_parents = ly_set_new(); |
| 4034 | features = ly_set_new(); |
| 4035 | |
| 4036 | /* get parents chain of source (leafref) */ |
| 4037 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
| 4038 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4039 | continue; |
| 4040 | } |
| 4041 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4042 | aug = (struct lys_node_augment *)iter->parent; |
| 4043 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4044 | /* unresolved augment, wait until it's resolved */ |
| 4045 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4046 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4047 | ret = EXIT_FAILURE; |
| 4048 | goto cleanup; |
| 4049 | } |
Michal Vasko | d42871e | 2018-07-12 09:06:53 +0200 | [diff] [blame] | 4050 | /* also add this augment */ |
| 4051 | ly_set_add(src_parents, aug, LY_SET_OPT_USEASLIST); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4052 | } |
| 4053 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 4054 | } |
| 4055 | /* get parents chain of target */ |
| 4056 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
| 4057 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4058 | continue; |
| 4059 | } |
| 4060 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4061 | aug = (struct lys_node_augment *)iter->parent; |
| 4062 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4063 | /* unresolved augment, wait until it's resolved */ |
| 4064 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4065 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4066 | ret = EXIT_FAILURE; |
| 4067 | goto cleanup; |
| 4068 | } |
| 4069 | } |
| 4070 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 4071 | } |
| 4072 | |
| 4073 | /* compare the features used in if-feature statements in the rest of both |
| 4074 | * chains of parents. The set of features used for target must be a subset |
| 4075 | * of features used for the leafref. This is not a perfect, we should compare |
| 4076 | * the truth tables but it could require too much resources, so we simplify that */ |
| 4077 | for (i = 0; i < src_parents->number; i++) { |
| 4078 | iter = src_parents->set.s[i]; /* shortcut */ |
| 4079 | if (!iter->iffeature_size) { |
| 4080 | continue; |
| 4081 | } |
| 4082 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4083 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4084 | for (; size; size--) { |
| 4085 | if (!iter->iffeature[j].features[size - 1]) { |
| 4086 | /* not yet resolved feature, postpone this check */ |
| 4087 | ret = EXIT_FAILURE; |
| 4088 | goto cleanup; |
| 4089 | } |
| 4090 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 4091 | } |
| 4092 | } |
| 4093 | } |
| 4094 | x = features->number; |
| 4095 | for (i = 0; i < trg_parents->number; i++) { |
| 4096 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 4097 | if (!iter->iffeature_size) { |
| 4098 | continue; |
| 4099 | } |
| 4100 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4101 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4102 | for (; size; size--) { |
| 4103 | if (!iter->iffeature[j].features[size - 1]) { |
| 4104 | /* not yet resolved feature, postpone this check */ |
| 4105 | ret = EXIT_FAILURE; |
| 4106 | goto cleanup; |
| 4107 | } |
| 4108 | if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 4109 | /* the feature is not present in features set of target's parents chain */ |
| 4110 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 4111 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
| 4112 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 4113 | iter->iffeature[j].features[size - 1]->name); |
| 4114 | ret = -1; |
| 4115 | goto cleanup; |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | cleanup: |
| 4122 | ly_set_free(features); |
| 4123 | ly_set_free(src_parents); |
| 4124 | ly_set_free(trg_parents); |
| 4125 | |
| 4126 | return ret; |
| 4127 | } |
| 4128 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4129 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4130 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4131 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4132 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4133 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4134 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4135 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4136 | * @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] | 4137 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4138 | static int |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4139 | resolve_schema_leafref(struct lys_type *type, struct lys_node *parent, struct unres_schema *unres) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4140 | { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4141 | const struct lys_node *node, *op_node = NULL, *tmp_parent; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4142 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 4143 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4144 | const char *id, *prefix, *name; |
| 4145 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4146 | int i, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4147 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4148 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4149 | if (!type->info.lref.target) { |
| 4150 | first_iter = 1; |
| 4151 | parent_times = 0; |
| 4152 | id = type->info.lref.path; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4153 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4154 | /* find operation schema we are in */ |
| 4155 | for (op_node = lys_parent(parent); |
| 4156 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4157 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 4158 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4159 | cur_module = lys_node_module(parent); |
| 4160 | do { |
| 4161 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
| 4162 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4163 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4164 | } |
| 4165 | id += i; |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4166 | |
| 4167 | /* get the current module */ |
| 4168 | tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module; |
| 4169 | if (!tmp_mod) { |
| 4170 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4171 | return EXIT_FAILURE; |
| 4172 | } |
| 4173 | last_aug = NULL; |
| 4174 | |
| 4175 | if (first_iter) { |
| 4176 | if (parent_times == -1) { |
| 4177 | /* use module data */ |
| 4178 | node = NULL; |
| 4179 | |
| 4180 | } else if (parent_times > 0) { |
| 4181 | /* we are looking for the right parent */ |
| 4182 | for (i = 0, node = parent; i < parent_times; i++) { |
| 4183 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 4184 | && !((struct lys_node_augment *)node->parent)->target) { |
| 4185 | /* we are in an unresolved augment, cannot evaluate */ |
| 4186 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 4187 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path); |
| 4188 | return EXIT_FAILURE; |
| 4189 | } |
| 4190 | |
| 4191 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 4192 | * all schema nodes that cannot be instantiated in data tree */ |
| 4193 | for (node = lys_parent(node); |
| 4194 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4195 | node = lys_parent(node)); |
| 4196 | |
| 4197 | if (!node) { |
| 4198 | if (i == parent_times - 1) { |
| 4199 | /* top-level */ |
| 4200 | break; |
| 4201 | } |
| 4202 | |
| 4203 | /* higher than top-level */ |
| 4204 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4205 | return EXIT_FAILURE; |
| 4206 | } |
| 4207 | } |
| 4208 | } else { |
| 4209 | LOGINT(ctx); |
| 4210 | return -1; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 4215 | * - useless to search for that in augments) */ |
| 4216 | if (!tmp_mod->implemented && node) { |
| 4217 | get_next_augment: |
| 4218 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
| 4219 | } |
| 4220 | |
| 4221 | tmp_parent = (last_aug ? (struct lys_node *)last_aug : node); |
| 4222 | node = NULL; |
| 4223 | while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) { |
| 4224 | if (lys_node_module(node) != lys_main_module(tmp_mod)) { |
| 4225 | continue; |
| 4226 | } |
| 4227 | if (strncmp(node->name, name, nam_len) || node->name[nam_len]) { |
| 4228 | continue; |
| 4229 | } |
| 4230 | /* match */ |
| 4231 | break; |
| 4232 | } |
| 4233 | if (!node) { |
| 4234 | if (last_aug) { |
| 4235 | /* restore the correct augment target */ |
| 4236 | node = last_aug->target; |
| 4237 | goto get_next_augment; |
| 4238 | } |
| 4239 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4240 | return EXIT_FAILURE; |
| 4241 | } |
| 4242 | |
| 4243 | if (first_iter) { |
| 4244 | /* set external dependency flag, we can decide based on the first found node */ |
| 4245 | if (op_node && parent_times && |
| 4246 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 4247 | parent->flags |= LYS_LEAFREF_DEP; |
| 4248 | } |
| 4249 | first_iter = 0; |
| 4250 | } |
| 4251 | |
| 4252 | if (has_predicate) { |
| 4253 | /* we have predicate, so the current result must be list */ |
| 4254 | if (node->nodetype != LYS_LIST) { |
| 4255 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4256 | return -1; |
| 4257 | } |
| 4258 | |
| 4259 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
| 4260 | if (!i) { |
| 4261 | return EXIT_FAILURE; |
| 4262 | } else if (i < 0) { |
| 4263 | return -1; |
| 4264 | } |
| 4265 | id += i; |
| 4266 | has_predicate = 0; |
| 4267 | } |
| 4268 | } while (id[0]); |
| 4269 | |
| 4270 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 4271 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
| 4272 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4273 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path); |
| 4274 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4275 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4276 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4277 | /* check status */ |
| 4278 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
| 4279 | node->flags, node->module, node->name, node)) { |
| 4280 | return -1; |
| 4281 | } |
| 4282 | |
| 4283 | /* assign */ |
| 4284 | type->info.lref.target = (struct lys_node_leaf *)node; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4287 | /* as the last thing traverse this leafref and make targets on the path implemented */ |
| 4288 | if (lys_node_module(parent)->implemented) { |
| 4289 | /* make all the modules in the path implemented */ |
| 4290 | for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) { |
| 4291 | if (!lys_node_module(node)->implemented) { |
| 4292 | lys_node_module(node)->implemented = 1; |
| 4293 | if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4294 | return -1; |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | /* store the backlink from leafref target */ |
| 4300 | if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) { |
| 4301 | return -1; |
| 4302 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4305 | /* check if leafref and its target are under common if-features */ |
| 4306 | return check_leafref_features(type); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4309 | /** |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4310 | * @brief Compare 2 data node values. |
| 4311 | * |
| 4312 | * Comparison performed on canonical forms, the first value |
| 4313 | * is first transformed into canonical form. |
| 4314 | * |
| 4315 | * @param[in] node Leaf/leaf-list with these values. |
| 4316 | * @param[in] noncan_val Non-canonical value. |
| 4317 | * @param[in] noncan_val_len Length of \p noncal_val. |
| 4318 | * @param[in] can_val Canonical value. |
| 4319 | * @return 1 if equal, 0 if not, -1 on error (logged). |
| 4320 | */ |
| 4321 | static int |
| 4322 | valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val) |
| 4323 | { |
| 4324 | int ret; |
| 4325 | struct lyd_node_leaf_list leaf; |
| 4326 | struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node; |
| 4327 | |
| 4328 | /* dummy leaf */ |
| 4329 | memset(&leaf, 0, sizeof leaf); |
| 4330 | leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len); |
| 4331 | |
| 4332 | repeat: |
| 4333 | leaf.value_type = sleaf->type.base; |
| 4334 | leaf.schema = node; |
| 4335 | |
| 4336 | if (leaf.value_type == LY_TYPE_LEAFREF) { |
| 4337 | if (!sleaf->type.info.lref.target) { |
| 4338 | /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4339 | LOGINT(node->module->ctx); |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4340 | ret = -1; |
| 4341 | goto finish; |
| 4342 | } |
| 4343 | sleaf = sleaf->type.info.lref.target; |
| 4344 | goto repeat; |
| 4345 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 4346 | if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0, 0)) { |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4347 | ret = -1; |
| 4348 | goto finish; |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | if (!strcmp(leaf.value_str, can_val)) { |
| 4353 | ret = 1; |
| 4354 | } else { |
| 4355 | ret = 0; |
| 4356 | } |
| 4357 | |
| 4358 | finish: |
| 4359 | lydict_remove(node->module->ctx, leaf.value_str); |
| 4360 | return ret; |
| 4361 | } |
| 4362 | |
| 4363 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4364 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4365 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4366 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 4367 | * @param[in] prev_mod Previous module to use in case there is no prefix. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4368 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4369 | * @param[in,out] node Node matching the restriction without |
| 4370 | * the predicate. If it does not satisfy the predicate, |
| 4371 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4372 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4373 | * @return Number of characters successfully parsed, |
| 4374 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4375 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4376 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4377 | resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct lyd_node **node, int cur_idx) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4378 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4379 | /* ... /node[key=value] ... */ |
| 4380 | struct lyd_node_leaf_list *key; |
| 4381 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 4382 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4383 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4384 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4385 | struct ly_ctx *ctx = prev_mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4386 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4387 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4388 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4389 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4390 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4391 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 4392 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4393 | } |
| 4394 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4395 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4396 | if (!(*node)) { |
| 4397 | /* just parse it all */ |
| 4398 | continue; |
| 4399 | } |
| 4400 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4401 | /* target */ |
| 4402 | if (name[0] == '.') { |
| 4403 | /* leaf-list value */ |
| 4404 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4405 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4406 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4407 | parsed = -1; |
| 4408 | goto cleanup; |
| 4409 | } |
| 4410 | |
| 4411 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4412 | if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4413 | *node = NULL; |
| 4414 | goto cleanup; |
| 4415 | } |
| 4416 | |
| 4417 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4418 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4419 | |
| 4420 | /* keyless list position */ |
| 4421 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4422 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4423 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4424 | parsed = -1; |
| 4425 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4426 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4427 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4428 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4429 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4430 | (*node)->schema->name); |
| 4431 | parsed = -1; |
| 4432 | goto cleanup; |
| 4433 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4434 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4435 | /* check the index */ |
| 4436 | if (atoi(name) != cur_idx) { |
| 4437 | *node = NULL; |
| 4438 | goto cleanup; |
| 4439 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4440 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4441 | } else { |
| 4442 | /* list key value */ |
| 4443 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4444 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4445 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4446 | parsed = -1; |
| 4447 | goto cleanup; |
| 4448 | } |
| 4449 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4450 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4451 | /* prepare key array */ |
| 4452 | if (!list_keys) { |
| 4453 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4454 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4455 | for (i = 0; i < slist->keys_size; ++i) { |
| 4456 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4457 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4458 | } |
| 4459 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4460 | /* find the schema key leaf */ |
| 4461 | for (i = 0; i < slist->keys_size; ++i) { |
| 4462 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4463 | break; |
| 4464 | } |
| 4465 | } |
| 4466 | if (i == slist->keys_size) { |
| 4467 | /* this list has no such key */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4468 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\"," |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4469 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4470 | parsed = -1; |
| 4471 | goto cleanup; |
| 4472 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4473 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4474 | /* check module */ |
| 4475 | if (model) { |
| 4476 | if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4477 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4478 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4479 | parsed = -1; |
| 4480 | goto cleanup; |
| 4481 | } |
| 4482 | } else { |
| 4483 | if (list_keys[i]->module != prev_mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4484 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".", |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4485 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4486 | parsed = -1; |
| 4487 | goto cleanup; |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | /* find the actual data key */ |
| 4492 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4493 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4494 | break; |
| 4495 | } |
| 4496 | } |
| 4497 | if (!key) { |
| 4498 | /* list instance is missing a key? definitely should not happen */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4499 | LOGINT(ctx); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4500 | parsed = -1; |
| 4501 | goto cleanup; |
| 4502 | } |
| 4503 | |
| 4504 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4505 | if (!valequal(key->schema, value, val_len, key->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4506 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4507 | /* we still want to parse the whole predicate */ |
| 4508 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | /* everything is fine, mark this key as resolved */ |
| 4512 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4513 | } |
| 4514 | } while (has_predicate); |
| 4515 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4516 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4517 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4518 | for (i = 0; i < slist->keys_size; ++i) { |
| 4519 | if (list_keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4520 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4521 | parsed = -1; |
| 4522 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4523 | } |
| 4524 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4525 | } |
| 4526 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4527 | cleanup: |
| 4528 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4529 | return parsed; |
| 4530 | } |
| 4531 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4532 | static int |
| 4533 | check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4534 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4535 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4536 | struct lyxp_set set; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4537 | enum int_log_opts prev_ilo; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4538 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4539 | if (check_place) { |
| 4540 | parent = node; |
| 4541 | while (parent) { |
| 4542 | if (parent->nodetype == LYS_GROUPING) { |
| 4543 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4544 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4545 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4546 | if (parent->nodetype == LYS_AUGMENT) { |
| 4547 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4548 | /* unresolved augment, skip for now (will be checked later) */ |
| 4549 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4550 | } else { |
| 4551 | parent = ((struct lys_node_augment *)parent)->target; |
| 4552 | continue; |
| 4553 | } |
| 4554 | } |
| 4555 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4556 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4557 | } |
| 4558 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4559 | memset(&set, 0, sizeof set); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4560 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4561 | /* produce just warnings */ |
| 4562 | ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL); |
| 4563 | lyxp_node_atomize(node, &set, 1); |
| 4564 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
| 4565 | |
| 4566 | if (set.val.snodes) { |
| 4567 | free(set.val.snodes); |
| 4568 | } |
| 4569 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4570 | } |
| 4571 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4572 | static int |
| 4573 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4574 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4575 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4576 | |
| 4577 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4578 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4579 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4580 | LOGVAL(leaf->module->ctx, 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] | 4581 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4582 | return -1; |
| 4583 | } |
| 4584 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4585 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4586 | } else if (type->base == LY_TYPE_UNION) { |
| 4587 | for (i = 0; i < type->info.uni.count; i++) { |
| 4588 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4589 | return -1; |
| 4590 | } |
| 4591 | } |
| 4592 | } |
| 4593 | return 0; |
| 4594 | } |
| 4595 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4596 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4597 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4598 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4599 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4600 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4601 | * @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] | 4602 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4603 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4604 | * |
| 4605 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4606 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4607 | int |
| 4608 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4609 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4610 | struct lys_node_leaf *leaf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4611 | struct ly_ctx *ctx; |
| 4612 | |
| 4613 | if (!node) { |
| 4614 | return 0; |
| 4615 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4616 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4617 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4618 | ctx = node->module->ctx; |
| 4619 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4620 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4621 | if (clear) { |
| 4622 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4623 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4624 | } else { |
| 4625 | if (node->flags & LYS_CONFIG_SET) { |
| 4626 | /* skip nodes with an explicit config value */ |
| 4627 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4628 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4629 | LOGVAL(ctx, 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] | 4630 | return -1; |
| 4631 | } |
| 4632 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4633 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4634 | |
| 4635 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4636 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4637 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4638 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4639 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4640 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4641 | return -1; |
| 4642 | } |
| 4643 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4644 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4645 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4646 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4647 | return -1; |
| 4648 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4649 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4650 | leaf = (struct lys_node_leaf *)node; |
| 4651 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4652 | return -1; |
| 4653 | } |
| 4654 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4655 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4656 | |
| 4657 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4658 | } |
| 4659 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4660 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4661 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4662 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4663 | * @param[in] aug Augment to use. |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4664 | * @param[in] uses Parent 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] | 4665 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4666 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4667 | * @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] | 4668 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4669 | static int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4670 | resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4671 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4672 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4673 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4674 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4675 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4676 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4677 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4678 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4679 | mod = lys_main_module(aug->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4680 | ctx = mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4681 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4682 | /* set it as not applied for now */ |
| 4683 | aug->flags |= LYS_NOTAPPLIED; |
| 4684 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4685 | /* 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] | 4686 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4687 | /* resolve target node */ |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4688 | rc = resolve_schema_nodeid(aug->target_name, uses, (uses ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4689 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4690 | LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4691 | return -1; |
| 4692 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4693 | if (!set) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4694 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4695 | return EXIT_FAILURE; |
| 4696 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4697 | aug->target = set->set.s[0]; |
| 4698 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4699 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4700 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4701 | /* make this module implemented if the target module is (if the target is in an unimplemented module, |
| 4702 | * it is fine because when we will be making that module implemented, its augment will be applied |
| 4703 | * and that augment target module made implemented, recursively) */ |
| 4704 | if (mod->implemented && !lys_node_module(aug->target)->implemented) { |
| 4705 | lys_node_module(aug->target)->implemented = 1; |
| 4706 | if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4707 | return -1; |
| 4708 | } |
| 4709 | } |
| 4710 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4711 | /* check for mandatory nodes - if the target node is in another module |
| 4712 | * the added nodes cannot be mandatory |
| 4713 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4714 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4715 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4716 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4717 | } |
| 4718 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4719 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4720 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4721 | LY_TREE_FOR(aug->child, sub) { |
| 4722 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4723 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4724 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4725 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4726 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4727 | return -1; |
| 4728 | } |
| 4729 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4730 | } 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] | 4731 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4732 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4733 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4734 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4735 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4736 | return -1; |
| 4737 | } |
| 4738 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4739 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4740 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4741 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4742 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4743 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4744 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4745 | return -1; |
| 4746 | } |
| 4747 | } |
| 4748 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4749 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 4750 | LOGVAL(ctx, 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] | 4751 | return -1; |
| 4752 | } |
| 4753 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4754 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4755 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4756 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4757 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4758 | } |
| 4759 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4760 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4761 | if (!aug->child) { |
| 4762 | /* empty augment, nothing to connect, but it is techincally applied */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4763 | LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4764 | aug->flags &= ~LYS_NOTAPPLIED; |
Radek Krejci | aa6b2a1 | 2017-10-26 15:52:39 +0200 | [diff] [blame] | 4765 | } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) { |
| 4766 | /* we try to connect the augment only in case the module is implemented or |
| 4767 | * the augment applies on the used grouping, anyway we failed here */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4768 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4769 | } |
| 4770 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4771 | return EXIT_SUCCESS; |
| 4772 | } |
| 4773 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4774 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4775 | 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] | 4776 | { |
| 4777 | enum LY_VLOG_ELEM vlog_type; |
| 4778 | void *vlog_node; |
| 4779 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4780 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4781 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4782 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4783 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4784 | struct lys_ext_instance *tmp_ext; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4785 | struct ly_ctx *ctx = NULL; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4786 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4787 | |
| 4788 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4789 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4790 | vlog_node = info->parent; |
| 4791 | vlog_type = LY_VLOG_LYS; |
| 4792 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4793 | case LYEXT_PAR_MODULE: |
| 4794 | case LYEXT_PAR_IMPORT: |
| 4795 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4796 | vlog_node = NULL; |
| 4797 | vlog_type = LY_VLOG_LYS; |
| 4798 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4799 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4800 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4801 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4802 | break; |
| 4803 | } |
| 4804 | |
| 4805 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4806 | /* YIN */ |
| 4807 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4808 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4809 | mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4810 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4811 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4812 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4813 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4814 | ctx = mod->ctx; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4815 | |
| 4816 | /* find the extension definition */ |
| 4817 | e = NULL; |
| 4818 | for (i = 0; i < mod->extensions_size; i++) { |
| 4819 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4820 | e = &mod->extensions[i]; |
| 4821 | break; |
| 4822 | } |
| 4823 | } |
| 4824 | /* try submodules */ |
| 4825 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4826 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4827 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4828 | e = &mod->inc[j].submodule->extensions[i]; |
| 4829 | break; |
| 4830 | } |
| 4831 | } |
| 4832 | } |
| 4833 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4834 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4835 | return EXIT_FAILURE; |
| 4836 | } |
| 4837 | |
| 4838 | /* 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] | 4839 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4840 | if (e->plugin && e->plugin->check_position) { |
| 4841 | /* common part - we have plugin with position checking function, use it first */ |
| 4842 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4843 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4844 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4845 | return -1; |
| 4846 | } |
| 4847 | } |
| 4848 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4849 | /* extension type-specific part - allocation */ |
| 4850 | if (e->plugin) { |
| 4851 | etype = e->plugin->type; |
| 4852 | } else { |
| 4853 | /* default type */ |
| 4854 | etype = LYEXT_FLAG; |
| 4855 | } |
| 4856 | switch (etype) { |
| 4857 | case LYEXT_FLAG: |
| 4858 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4859 | break; |
| 4860 | case LYEXT_COMPLEX: |
| 4861 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4862 | break; |
| 4863 | case LYEXT_ERR: |
| 4864 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4865 | LOGINT(ctx); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4866 | return -1; |
| 4867 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4868 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4869 | |
| 4870 | /* common part for all extension types */ |
| 4871 | (*ext)->def = e; |
| 4872 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4873 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4874 | (*ext)->insubstmt = info->substmt; |
| 4875 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4876 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4877 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4878 | |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4879 | if (e->argument) { |
| 4880 | if (!(e->flags & LYS_YINELEM)) { |
| 4881 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4882 | if (!(*ext)->arg_value) { |
PavolVican | e7a1fc6 | 2018-02-19 16:50:27 +0100 | [diff] [blame] | 4883 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name); |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4884 | return -1; |
| 4885 | } |
| 4886 | |
| 4887 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4888 | } else { |
| 4889 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4890 | if (ly_strequal(yin->name, e->argument, 1)) { |
| 4891 | (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0); |
| 4892 | lyxml_free(mod->ctx, yin); |
| 4893 | break; |
| 4894 | } |
| 4895 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4896 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4899 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 4900 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 4901 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4902 | } |
| 4903 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4904 | (*ext)->nodetype = LYS_EXT; |
| 4905 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4906 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4907 | /* extension type-specific part - parsing content */ |
| 4908 | switch (etype) { |
| 4909 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4910 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4911 | if (!yin->ns) { |
| 4912 | /* garbage */ |
| 4913 | lyxml_free(mod->ctx, yin); |
| 4914 | continue; |
| 4915 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4916 | /* standard YANG statements are not expected here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4917 | LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4918 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4919 | } else if (yin->ns == info->data.yin->ns && |
| 4920 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4921 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4922 | if ((*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4923 | LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4924 | return -1; |
| 4925 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4926 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4927 | yin->content = NULL; |
| 4928 | lyxml_free(mod->ctx, yin); |
| 4929 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4930 | /* extension instance */ |
| 4931 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4932 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4933 | return -1; |
| 4934 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4935 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4936 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4937 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4938 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4939 | break; |
| 4940 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4941 | ((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] | 4942 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4943 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4944 | return -1; |
| 4945 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4946 | break; |
| 4947 | default: |
| 4948 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4949 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4950 | |
| 4951 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4952 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4953 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4954 | /* YANG */ |
| 4955 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4956 | ext_prefix = (char *)(*ext)->def; |
| 4957 | tmp = strchr(ext_prefix, ':'); |
| 4958 | if (!tmp) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4959 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4960 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4961 | } |
| 4962 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4963 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4964 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4965 | mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4966 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4967 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4968 | return EXIT_FAILURE; |
| 4969 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4970 | ctx = mod->ctx; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4971 | |
| 4972 | /* find the extension definition */ |
| 4973 | e = NULL; |
| 4974 | for (i = 0; i < mod->extensions_size; i++) { |
| 4975 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4976 | e = &mod->extensions[i]; |
| 4977 | break; |
| 4978 | } |
| 4979 | } |
| 4980 | /* try submodules */ |
| 4981 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4982 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4983 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4984 | e = &mod->inc[j].submodule->extensions[i]; |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | } |
| 4989 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4990 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4991 | return EXIT_FAILURE; |
| 4992 | } |
| 4993 | |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 4994 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
| 4995 | (*ext)->def = NULL; |
| 4996 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4997 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4998 | |
| 4999 | if (e->plugin && e->plugin->check_position) { |
| 5000 | /* common part - we have plugin with position checking function, use it first */ |
| 5001 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 5002 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5003 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5004 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5005 | } |
| 5006 | } |
| 5007 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5008 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5009 | (*ext)->def = e; |
| 5010 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 5011 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5012 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5013 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5014 | if (e->argument && !(*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5015 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5016 | goto error; |
| 5017 | } |
| 5018 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5019 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 5020 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 5021 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5022 | } |
| 5023 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 5024 | (*ext)->module = info->mod; |
| 5025 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5026 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5027 | /* extension type-specific part */ |
| 5028 | if (e->plugin) { |
| 5029 | etype = e->plugin->type; |
| 5030 | } else { |
| 5031 | /* default type */ |
| 5032 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5033 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5034 | switch (etype) { |
| 5035 | case LYEXT_FLAG: |
| 5036 | /* nothing change */ |
| 5037 | break; |
| 5038 | case LYEXT_COMPLEX: |
| 5039 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5040 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5041 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 5042 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5043 | ((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] | 5044 | if (info->data.yang) { |
| 5045 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5046 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 5047 | (struct lys_ext_instance_complex*)(*ext))) { |
| 5048 | goto error; |
| 5049 | } |
| 5050 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 5051 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5052 | goto error; |
| 5053 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 5054 | } |
| 5055 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 5056 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5057 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5058 | break; |
| 5059 | case LYEXT_ERR: |
| 5060 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5061 | LOGINT(ctx); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5062 | goto error; |
| 5063 | } |
| 5064 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5065 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 5066 | goto error; |
| 5067 | } |
| 5068 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5072 | error: |
| 5073 | free(ext_prefix); |
| 5074 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5075 | } |
| 5076 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5077 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5078 | * @brief Resolve (find) choice default case. Does not log. |
| 5079 | * |
| 5080 | * @param[in] choic Choice to use. |
| 5081 | * @param[in] dflt Name of the default case. |
| 5082 | * |
| 5083 | * @return Pointer to the default node or NULL. |
| 5084 | */ |
| 5085 | static struct lys_node * |
| 5086 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 5087 | { |
| 5088 | struct lys_node *child, *ret; |
| 5089 | |
| 5090 | LY_TREE_FOR(choic->child, child) { |
| 5091 | if (child->nodetype == LYS_USES) { |
| 5092 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 5093 | if (ret) { |
| 5094 | return ret; |
| 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | 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] | 5099 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5100 | return child; |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | return NULL; |
| 5105 | } |
| 5106 | |
| 5107 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5108 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 5109 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5110 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5111 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5112 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5113 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5114 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 5115 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5116 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5117 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5118 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5119 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5120 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5121 | struct lys_node_leaflist *llist; |
| 5122 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 5123 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5124 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5125 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5126 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5127 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5128 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5129 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5130 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5131 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5132 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 5133 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5134 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5135 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5136 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 5137 | if (node_aux->nodetype & LYS_GROUPING) { |
| 5138 | /* do not instantiate groupings from groupings */ |
| 5139 | continue; |
| 5140 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5141 | 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] | 5142 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5143 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 5144 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed."); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5145 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5146 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5147 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5148 | 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] | 5149 | 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] | 5150 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5151 | } |
| 5152 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5153 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 5154 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5155 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 5156 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5157 | if (uses->refine_size) { |
| 5158 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5159 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5160 | } |
| 5161 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5162 | /* apply refines */ |
| 5163 | for (i = 0; i < uses->refine_size; i++) { |
| 5164 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 5165 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 5166 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 5167 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 5168 | if (rc || !node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5169 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5170 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5171 | } |
| 5172 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5173 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5174 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 5175 | LOGVAL(ctx, 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] | 5176 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5177 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5178 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5179 | |
| 5180 | /* description on any nodetype */ |
| 5181 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5182 | lydict_remove(ctx, node->dsc); |
| 5183 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | /* reference on any nodetype */ |
| 5187 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5188 | lydict_remove(ctx, node->ref); |
| 5189 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5190 | } |
| 5191 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5192 | /* config on any nodetype, |
| 5193 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 5194 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5195 | node->flags &= ~LYS_CONFIG_MASK; |
| 5196 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5200 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5201 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5202 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5203 | leaf = (struct lys_node_leaf *)node; |
| 5204 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5205 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5206 | lydict_remove(ctx, leaf->dflt); |
| 5207 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 5208 | |
| 5209 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5210 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 5211 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5212 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5213 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5214 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 5215 | /* leaf-list */ |
| 5216 | llist = (struct lys_node_leaflist *)node; |
| 5217 | |
| 5218 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5219 | for (j = 0; j < llist->dflt_size; j++) { |
| 5220 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5221 | } |
| 5222 | free(llist->dflt); |
| 5223 | |
| 5224 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5225 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5226 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5227 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5228 | for (j = 0; j < llist->dflt_size; j++) { |
| 5229 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5233 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5234 | 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] | 5235 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5236 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5237 | } |
| 5238 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 5243 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5244 | /* remove current value */ |
| 5245 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5246 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5247 | /* set new value */ |
| 5248 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 5249 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5250 | if (rfn->flags & LYS_MAND_TRUE) { |
| 5251 | /* check if node has default value */ |
| 5252 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5253 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5254 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5255 | goto fail; |
| 5256 | } |
| 5257 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5258 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5259 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5260 | goto fail; |
| 5261 | } |
| 5262 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5266 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 5267 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 5268 | ((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] | 5269 | } |
| 5270 | |
| 5271 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5272 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5273 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5274 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5275 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5276 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5277 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5278 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5279 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5280 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5281 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5282 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5283 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5284 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 5289 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5290 | switch (node->nodetype) { |
| 5291 | case LYS_LEAF: |
| 5292 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 5293 | old_must = &((struct lys_node_leaf *)node)->must; |
| 5294 | break; |
| 5295 | case LYS_LEAFLIST: |
| 5296 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 5297 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 5298 | break; |
| 5299 | case LYS_LIST: |
| 5300 | old_size = &((struct lys_node_list *)node)->must_size; |
| 5301 | old_must = &((struct lys_node_list *)node)->must; |
| 5302 | break; |
| 5303 | case LYS_CONTAINER: |
| 5304 | old_size = &((struct lys_node_container *)node)->must_size; |
| 5305 | old_must = &((struct lys_node_container *)node)->must; |
| 5306 | break; |
| 5307 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5308 | case LYS_ANYDATA: |
| 5309 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 5310 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5311 | break; |
| 5312 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5313 | LOGINT(ctx); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5314 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | size = *old_size + rfn->must_size; |
| 5318 | must = realloc(*old_must, size * sizeof *rfn->must); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5319 | LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5320 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 5321 | must[j].ext_size = rfn->must[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5322 | lys_ext_dup(ctx, 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] | 5323 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5324 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 5325 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 5326 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 5327 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 5328 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Radek Krejci | cfcd8a5 | 2017-09-04 13:19:57 +0200 | [diff] [blame] | 5329 | must[j].flags = rfn->must[k].flags; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5330 | } |
| 5331 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5332 | *old_must = must; |
| 5333 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* check XPath dependencies again */ |
| 5336 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 5337 | goto fail; |
| 5338 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5339 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5340 | |
| 5341 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 5342 | if (rfn->iffeature_size) { |
| 5343 | old_size = &node->iffeature_size; |
| 5344 | old_iff = &node->iffeature; |
| 5345 | |
| 5346 | size = *old_size + rfn->iffeature_size; |
| 5347 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5348 | LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail); |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5349 | *old_iff = iff; |
| 5350 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5351 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 5352 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5353 | if (usize1) { |
| 5354 | /* there is something to duplicate */ |
| 5355 | /* duplicate compiled expression */ |
| 5356 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 5357 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5358 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5359 | 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] | 5360 | |
| 5361 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5362 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5363 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5364 | 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] | 5365 | |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5366 | /* duplicate extensions */ |
| 5367 | iff[j].ext_size = rfn->iffeature[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5368 | lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size, |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5369 | &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres); |
| 5370 | } |
| 5371 | (*old_size)++; |
| 5372 | } |
| 5373 | assert(*old_size == size); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5374 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | /* apply augments */ |
| 5378 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 5379 | rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5380 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5381 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5382 | } |
| 5383 | } |
| 5384 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5385 | /* check refines */ |
| 5386 | for (i = 0; i < uses->refine_size; i++) { |
| 5387 | node = refine_nodes[i]; |
| 5388 | rfn = &uses->refine[i]; |
| 5389 | |
| 5390 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5391 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5392 | 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] | 5393 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5394 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 5395 | (rfn->flags & LYS_CONFIG_W)) { |
| 5396 | /* setting config true under config false is prohibited */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5397 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5398 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5399 | "changing config from 'false' to 'true' is prohibited while " |
| 5400 | "the target's parent is still config 'false'."); |
| 5401 | goto fail; |
| 5402 | } |
| 5403 | |
| 5404 | /* inherit config change to the target children */ |
| 5405 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 5406 | if (rfn->flags & LYS_CONFIG_W) { |
| 5407 | if (iter->flags & LYS_CONFIG_SET) { |
| 5408 | /* config is set explicitely, go to next sibling */ |
| 5409 | next = NULL; |
| 5410 | goto nextsibling; |
| 5411 | } |
| 5412 | } else { /* LYS_CONFIG_R */ |
| 5413 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 5414 | /* error - we would have config data under status data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5415 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5416 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5417 | "changing config from 'true' to 'false' is prohibited while the target " |
| 5418 | "has still a children with explicit config 'true'."); |
| 5419 | goto fail; |
| 5420 | } |
| 5421 | } |
| 5422 | /* change config */ |
| 5423 | iter->flags &= ~LYS_CONFIG_MASK; |
| 5424 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 5425 | |
| 5426 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5427 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5428 | next = NULL; |
| 5429 | } else { |
| 5430 | next = iter->child; |
| 5431 | } |
| 5432 | nextsibling: |
| 5433 | if (!next) { |
| 5434 | /* try siblings */ |
| 5435 | next = iter->next; |
| 5436 | } |
| 5437 | while (!next) { |
| 5438 | /* parent is already processed, go to its sibling */ |
| 5439 | iter = lys_parent(iter); |
| 5440 | |
| 5441 | /* no siblings, go back through parents */ |
| 5442 | if (iter == node) { |
| 5443 | /* we are done, no next element to process */ |
| 5444 | break; |
| 5445 | } |
| 5446 | next = iter->next; |
| 5447 | } |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5452 | if (rfn->dflt_size) { |
| 5453 | if (node->nodetype == LYS_CHOICE) { |
| 5454 | /* choice */ |
| 5455 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5456 | rfn->dflt[0]); |
| 5457 | if (!((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5458 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5459 | goto fail; |
| 5460 | } |
| 5461 | if (lyp_check_mandatory_choice(node)) { |
| 5462 | goto fail; |
| 5463 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5468 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5469 | if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5470 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5471 | LOGVAL(ctx, 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] | 5472 | goto fail; |
| 5473 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5474 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5475 | if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5476 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5477 | LOGVAL(ctx, 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] | 5478 | goto fail; |
| 5479 | } |
| 5480 | } |
| 5481 | |
| 5482 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5483 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5484 | if (node->nodetype == LYS_LEAFLIST) { |
| 5485 | llist = (struct lys_node_leaflist *)node; |
| 5486 | if (llist->dflt_size && llist->min) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5487 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5488 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5489 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5490 | goto fail; |
| 5491 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5492 | } else if (node->nodetype == LYS_LEAF) { |
| 5493 | leaf = (struct lys_node_leaf *)node; |
| 5494 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5495 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5496 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5497 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5498 | goto fail; |
| 5499 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5500 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5501 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5502 | /* 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] | 5503 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5504 | for (parent = node->parent; |
| 5505 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5506 | parent = parent->parent) { |
| 5507 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5508 | /* stop also on presence containers */ |
| 5509 | break; |
| 5510 | } |
| 5511 | } |
| 5512 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5513 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5514 | if (lyp_check_mandatory_choice(parent)) { |
| 5515 | goto fail; |
| 5516 | } |
| 5517 | } |
| 5518 | } |
| 5519 | } |
| 5520 | free(refine_nodes); |
| 5521 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5522 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5523 | |
| 5524 | fail: |
| 5525 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5526 | lys_node_free(iter, NULL, 0); |
| 5527 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5528 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5529 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5530 | } |
| 5531 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5532 | void |
| 5533 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5534 | { |
| 5535 | int i; |
| 5536 | |
| 5537 | assert(der && base); |
| 5538 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5539 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5540 | /* create a set for backlinks if it does not exist */ |
| 5541 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5542 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5543 | /* store backlink */ |
| 5544 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5545 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5546 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5547 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5548 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5549 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5552 | /** |
| 5553 | * @brief Resolve base identity recursively. Does not log. |
| 5554 | * |
| 5555 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5556 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5557 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5558 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5559 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5560 | * @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] | 5561 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5562 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5563 | 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] | 5564 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5565 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5566 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5567 | struct lys_ident *base = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5568 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5569 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5570 | assert(ret); |
| 5571 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5572 | /* search module */ |
| 5573 | for (i = 0; i < module->ident_size; i++) { |
| 5574 | if (!strcmp(basename, module->ident[i].name)) { |
| 5575 | |
| 5576 | if (!ident) { |
| 5577 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5578 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5579 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5580 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5581 | } |
| 5582 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5583 | base = &module->ident[i]; |
| 5584 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5585 | } |
| 5586 | } |
| 5587 | |
| 5588 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5589 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5590 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5591 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5592 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5593 | if (!ident) { |
| 5594 | *ret = &module->inc[j].submodule->ident[i]; |
| 5595 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5596 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5597 | |
| 5598 | base = &module->inc[j].submodule->ident[i]; |
| 5599 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | } |
| 5603 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5604 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5605 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5606 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5607 | /* is it already completely resolved? */ |
| 5608 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5609 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5610 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5611 | |
| 5612 | /* simple check for circular reference, |
| 5613 | * the complete check is done as a side effect of using only completely |
| 5614 | * resolved identities (previous check of unres content) */ |
| 5615 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5616 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5617 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5618 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5619 | } |
| 5620 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5621 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5622 | } |
| 5623 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5624 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5625 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5626 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5627 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5628 | } |
| 5629 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5630 | /* base not found (maybe a forward reference) */ |
| 5631 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5632 | } |
| 5633 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5634 | /** |
| 5635 | * @brief Resolve base identity. Logs directly. |
| 5636 | * |
| 5637 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5638 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5639 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5640 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5641 | * @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] | 5642 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5643 | * @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] | 5644 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5645 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5646 | 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] | 5647 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5648 | { |
| 5649 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5650 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5651 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5652 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5653 | struct lys_module *mod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5654 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5655 | |
| 5656 | assert((ident && !type) || (!ident && type)); |
| 5657 | |
| 5658 | if (!type) { |
| 5659 | /* have ident to resolve */ |
| 5660 | ret = ⌖ |
| 5661 | flags = ident->flags; |
| 5662 | mod = ident->module; |
| 5663 | } else { |
| 5664 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5665 | ++type->info.ident.count; |
| 5666 | type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5667 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5668 | |
| 5669 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5670 | flags = type->parent->flags; |
| 5671 | mod = type->parent->module; |
| 5672 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5673 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5674 | |
| 5675 | /* search for the base identity */ |
| 5676 | name = strchr(basename, ':'); |
| 5677 | if (name) { |
| 5678 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5679 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5680 | name++; |
| 5681 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5682 | 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] | 5683 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5684 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5685 | } |
| 5686 | } else { |
| 5687 | name = basename; |
| 5688 | } |
| 5689 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5690 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 5691 | module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5692 | if (!module) { |
| 5693 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5694 | LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5695 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5696 | } |
| 5697 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5698 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5699 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5700 | if (!rc) { |
| 5701 | assert(*ret); |
| 5702 | |
| 5703 | /* check status */ |
| 5704 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5705 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5706 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5707 | } else if (ident) { |
| 5708 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5709 | if (lys_main_module(mod)->implemented) { |
| 5710 | /* in case of the implemented identity, maintain backlinks to it |
| 5711 | * from the base identities to make it available when resolving |
| 5712 | * data with the identity values (not implemented identity is not |
| 5713 | * allowed as an identityref value). */ |
| 5714 | resolve_identity_backlink_update(ident, *ret); |
| 5715 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5716 | } |
| 5717 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5718 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5719 | if (type) { |
| 5720 | --type->info.ident.count; |
| 5721 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5722 | } |
| 5723 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5724 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5725 | } |
| 5726 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5727 | /* |
| 5728 | * 1 - true (der is derived from base) |
| 5729 | * 0 - false (der is not derived from base) |
| 5730 | */ |
| 5731 | static int |
| 5732 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5733 | { |
| 5734 | int i; |
| 5735 | |
| 5736 | if (der == base) { |
| 5737 | return 1; |
| 5738 | } else { |
| 5739 | for(i = 0; i < der->base_size; i++) { |
| 5740 | if (search_base_identity(der->base[i], base) == 1) { |
| 5741 | return 1; |
| 5742 | } |
| 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | return 0; |
| 5747 | } |
| 5748 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5749 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5750 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5751 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5752 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5753 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5754 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5755 | * @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] | 5756 | * |
| 5757 | * @return Pointer to the identity resolvent, NULL on error. |
| 5758 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5759 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5760 | 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] | 5761 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5762 | const char *mod_name, *name; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5763 | char *str; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5764 | int mod_name_len, nam_len, rc; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5765 | int need_implemented = 0; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5766 | unsigned int i, j; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5767 | struct lys_ident *der, *cur; |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5768 | struct lys_module *imod = NULL, *m, *tmod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5769 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5770 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5771 | assert(type && ident_name && mod); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5772 | ctx = mod->ctx; |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5773 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5774 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5775 | return NULL; |
| 5776 | } |
| 5777 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5778 | 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] | 5779 | if (rc < 1) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5780 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5781 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5782 | } else if (rc < (signed)strlen(ident_name)) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5783 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5784 | return NULL; |
| 5785 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5786 | |
| 5787 | m = lys_main_module(mod); /* shortcut */ |
| 5788 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5789 | /* identity is defined in the same module as node */ |
| 5790 | imod = m; |
| 5791 | } else if (dflt) { |
| 5792 | /* solving identityref in default definition in schema - |
| 5793 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5794 | for (i = 0; i < mod->imp_size; i++) { |
| 5795 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5796 | imod = mod->imp[i].module; |
| 5797 | break; |
| 5798 | } |
| 5799 | } |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5800 | |
| 5801 | /* We may need to pull it from the module that the typedef came from */ |
| 5802 | if (!imod && type && type->der) { |
| 5803 | tmod = type->der->module; |
| 5804 | for (i = 0; i < tmod->imp_size; i++) { |
| 5805 | if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) { |
| 5806 | imod = tmod->imp[i].module; |
| 5807 | break; |
| 5808 | } |
| 5809 | } |
| 5810 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5811 | } else { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5812 | /* solving identityref in data - get the module from the context */ |
| 5813 | for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) { |
| 5814 | imod = mod->ctx->models.list[i]; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5815 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5816 | break; |
| 5817 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5818 | imod = NULL; |
| 5819 | } |
Radek Krejci | 5ba0510 | 2017-10-26 15:02:52 +0200 | [diff] [blame] | 5820 | if (!imod && mod->ctx->models.parsing_sub_modules_count) { |
| 5821 | /* we are currently parsing some module and checking XPath or a default value, |
| 5822 | * so take this module into account */ |
| 5823 | for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) { |
| 5824 | imod = mod->ctx->models.parsing_sub_modules[i]; |
| 5825 | if (imod->type) { |
| 5826 | /* skip submodules */ |
| 5827 | continue; |
| 5828 | } |
| 5829 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5830 | break; |
| 5831 | } |
| 5832 | imod = NULL; |
| 5833 | } |
| 5834 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5835 | } |
| 5836 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5837 | if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5838 | /* the needed module was not found, but it may have been expected so call the data callback */ |
| 5839 | if (imod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5840 | ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
Radek Krejci | 58523dc | 2017-10-27 10:00:17 +0200 | [diff] [blame] | 5841 | } else if (mod_name) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5842 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5843 | imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5844 | free(str); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5845 | } |
| 5846 | } |
| 5847 | if (!imod) { |
| 5848 | goto fail; |
| 5849 | } |
| 5850 | |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5851 | if (m != imod || lys_main_module(type->parent->module) != mod) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5852 | /* the type is not referencing the same schema, |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5853 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5854 | * contains the identity */ |
| 5855 | if (!imod->implemented) { |
| 5856 | cur = NULL; |
| 5857 | /* get the identity in the module */ |
| 5858 | for (i = 0; i < imod->ident_size; i++) { |
| 5859 | if (!strcmp(name, imod->ident[i].name)) { |
| 5860 | cur = &imod->ident[i]; |
| 5861 | break; |
| 5862 | } |
| 5863 | } |
| 5864 | if (!cur) { |
| 5865 | /* go through includes */ |
| 5866 | for (j = 0; j < imod->inc_size; j++) { |
| 5867 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5868 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5869 | cur = &imod->inc[j].submodule->ident[i]; |
| 5870 | break; |
| 5871 | } |
| 5872 | } |
| 5873 | } |
| 5874 | if (!cur) { |
| 5875 | goto fail; |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | /* check that identity is derived from one of the type's base */ |
| 5880 | while (type->der) { |
| 5881 | for (i = 0; i < type->info.ident.count; i++) { |
| 5882 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5883 | /* cur's base matches the type's base */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5884 | need_implemented = 1; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5885 | goto match; |
| 5886 | } |
| 5887 | } |
| 5888 | type = &type->der->type; |
| 5889 | } |
| 5890 | /* matching base not found */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5891 | LOGVAL(ctx, LYE_SPEC, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "Identity used as identityref value is not implemented."); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5892 | goto fail; |
| 5893 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5894 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5895 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5896 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5897 | while (type->der) { |
| 5898 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5899 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5900 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5901 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5902 | /* there are some derived identities */ |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5903 | for (j = 0; j < cur->der->number; j++) { |
| 5904 | der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5905 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5906 | /* we have match */ |
| 5907 | cur = der; |
| 5908 | goto match; |
| 5909 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5910 | } |
| 5911 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5912 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5913 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5914 | } |
| 5915 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5916 | fail: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5917 | LOGVAL(ctx, LYE_INRESOLV, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5918 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5919 | |
| 5920 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5921 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5922 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5923 | if (node) { |
| 5924 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5925 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5926 | LOGVAL(ctx, 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] | 5927 | return NULL; |
| 5928 | } |
| 5929 | } |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5930 | if (need_implemented) { |
| 5931 | if (dflt) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5932 | /* later try to make the module implemented */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5933 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5934 | imod->name, cur->name, mod->name); |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5935 | /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of |
| 5936 | * several functions with little gain */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5937 | if (lys_set_implemented(imod)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5938 | LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.", |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5939 | imod->name, cur->name); |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5940 | goto fail; |
| 5941 | } |
| 5942 | } else { |
| 5943 | /* just say that it was found, but in a non-implemented module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5944 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".", |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5945 | lys_main_module(cur->module)->name); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5946 | goto fail; |
| 5947 | } |
| 5948 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5949 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5950 | } |
| 5951 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5952 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5953 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5954 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5955 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5956 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5957 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5958 | * @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] | 5959 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5960 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5961 | 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] | 5962 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5963 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5964 | struct lys_node *par_grp; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5965 | struct ly_ctx *ctx = uses->module->ctx; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5966 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5967 | /* 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] | 5968 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5969 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5970 | * 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] | 5971 | for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame] | 5972 | if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) { |
| 5973 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
| 5974 | return -1; |
| 5975 | } |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5976 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5977 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5978 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5979 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5980 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5981 | return -1; |
| 5982 | } else if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5983 | LOGVAL(ctx, 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] | 5984 | return -1; |
| 5985 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5986 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5987 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5988 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5989 | return -1; |
| 5990 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5991 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5992 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5993 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5994 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5995 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5996 | } |
| 5997 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5998 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5999 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6000 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6001 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6002 | return -1; |
| 6003 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6004 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6005 | } else { |
| 6006 | /* instantiate grouping only when it is completely resolved */ |
| 6007 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 6008 | } |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame] | 6009 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6010 | return EXIT_FAILURE; |
| 6011 | } |
| 6012 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6013 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6014 | if (!rc) { |
| 6015 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6016 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6017 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 6018 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6019 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6020 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6021 | |
| 6022 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6023 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 6024 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6025 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6026 | return -1; |
| 6027 | } |
| 6028 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6029 | return EXIT_SUCCESS; |
| 6030 | } |
| 6031 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6032 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6033 | } |
| 6034 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6035 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 6036 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6037 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6038 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6039 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6040 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6041 | * @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] | 6042 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6043 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6044 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6045 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6046 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 6047 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6048 | char *s = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6049 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6050 | |
| 6051 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 6052 | assert(keys_str); |
| 6053 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6054 | if (!list->child) { |
| 6055 | /* no child, possible forward reference */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6056 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6057 | return EXIT_FAILURE; |
| 6058 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6059 | /* get the key name */ |
| 6060 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 6061 | len = value - keys_str; |
| 6062 | while (isspace(value[0])) { |
| 6063 | value++; |
| 6064 | } |
| 6065 | } else { |
| 6066 | len = strlen(keys_str); |
| 6067 | } |
| 6068 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6069 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 6070 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6071 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6072 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 6073 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6074 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6075 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6076 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6077 | /* check_key logs */ |
| 6078 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6079 | } |
| 6080 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6081 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6082 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6083 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 6084 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6085 | return -1; |
| 6086 | } |
| 6087 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6088 | /* default value - is ignored, keep it but print a warning */ |
| 6089 | if (list->keys[i]->dflt) { |
| 6090 | /* log is not hidden only in case this resolving fails and in such a case |
| 6091 | * we cannot get here |
| 6092 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6093 | assert(log_opt == ILO_STORE); |
| 6094 | log_opt = ILO_LOG; |
| 6095 | LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt, |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 6096 | list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX)); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6097 | log_opt = ILO_STORE; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6098 | free(s); |
| 6099 | } |
| 6100 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6101 | /* prepare for next iteration */ |
| 6102 | while (value && isspace(value[0])) { |
| 6103 | value++; |
| 6104 | } |
| 6105 | keys_str = value; |
| 6106 | } |
| 6107 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6108 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6109 | } |
| 6110 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6111 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6112 | * @brief Resolve (check) all must conditions of \p node. |
| 6113 | * Logs directly. |
| 6114 | * |
| 6115 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6116 | * @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] | 6117 | * |
| 6118 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 6119 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6120 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6121 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6122 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6123 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6124 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6125 | struct lys_restr *must; |
| 6126 | struct lyxp_set set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6127 | struct ly_ctx *ctx = node->schema->module->ctx; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6128 | |
| 6129 | assert(node); |
| 6130 | memset(&set, 0, sizeof set); |
| 6131 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6132 | if (inout_parent) { |
| 6133 | for (schema = lys_parent(node->schema); |
| 6134 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 6135 | schema = lys_parent(schema)); |
| 6136 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6137 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6138 | return -1; |
| 6139 | } |
| 6140 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 6141 | must = ((struct lys_node_inout *)schema)->must; |
| 6142 | |
| 6143 | /* context node is the RPC/action */ |
| 6144 | node = node->parent; |
| 6145 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6146 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6147 | return -1; |
| 6148 | } |
| 6149 | } else { |
| 6150 | switch (node->schema->nodetype) { |
| 6151 | case LYS_CONTAINER: |
| 6152 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 6153 | must = ((struct lys_node_container *)node->schema)->must; |
| 6154 | break; |
| 6155 | case LYS_LEAF: |
| 6156 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 6157 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 6158 | break; |
| 6159 | case LYS_LEAFLIST: |
| 6160 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 6161 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 6162 | break; |
| 6163 | case LYS_LIST: |
| 6164 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 6165 | must = ((struct lys_node_list *)node->schema)->must; |
| 6166 | break; |
| 6167 | case LYS_ANYXML: |
| 6168 | case LYS_ANYDATA: |
| 6169 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 6170 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 6171 | break; |
| 6172 | case LYS_NOTIF: |
| 6173 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 6174 | must = ((struct lys_node_notif *)node->schema)->must; |
| 6175 | break; |
| 6176 | default: |
| 6177 | must_size = 0; |
| 6178 | break; |
| 6179 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6183 | 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] | 6184 | return -1; |
| 6185 | } |
| 6186 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6187 | 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] | 6188 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6189 | if (!set.val.bool) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6190 | if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6191 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 6192 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6193 | LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6194 | if (must[i].emsg) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6195 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6196 | } |
| 6197 | if (must[i].eapptag) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6198 | ly_err_last_set_apptag(ctx, must[i].eapptag); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6199 | } |
| 6200 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6201 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6202 | } |
| 6203 | } |
| 6204 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6205 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6206 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6207 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6208 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6209 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 6210 | * |
| 6211 | * @param[in] schema Schema node with the when condition. |
| 6212 | * @param[out] ctx_snode When schema context node. |
| 6213 | * @param[out] ctx_snode_type Schema context node type. |
| 6214 | */ |
| 6215 | void |
| 6216 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 6217 | { |
| 6218 | const struct lys_node *sparent; |
| 6219 | |
| 6220 | /* find a not schema-only node */ |
| 6221 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 6222 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 6223 | if (schema->nodetype == LYS_AUGMENT) { |
| 6224 | sparent = ((struct lys_node_augment *)schema)->target; |
| 6225 | } else { |
| 6226 | sparent = schema->parent; |
| 6227 | } |
| 6228 | if (!sparent) { |
| 6229 | /* context node is the document root (fake root in our case) */ |
| 6230 | if (schema->flags & LYS_CONFIG_W) { |
| 6231 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 6232 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 6233 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6234 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 6235 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 6236 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6237 | break; |
| 6238 | } |
| 6239 | schema = sparent; |
| 6240 | } |
| 6241 | |
| 6242 | *ctx_snode = (struct lys_node *)schema; |
| 6243 | } |
| 6244 | |
| 6245 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6246 | * @brief Resolve (find) when condition context node. Does not log. |
| 6247 | * |
| 6248 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6249 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6250 | * @param[out] ctx_node Context node. |
| 6251 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6252 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6253 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6254 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6255 | static int |
| 6256 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 6257 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6258 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6259 | struct lyd_node *parent; |
| 6260 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6261 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6262 | uint16_t i, data_depth, schema_depth; |
| 6263 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6264 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6265 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6266 | if (node_type == LYXP_NODE_ELEM) { |
| 6267 | /* standard element context node */ |
| 6268 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 6269 | for (sparent = schema, schema_depth = 0; |
| 6270 | sparent; |
| 6271 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 6272 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 6273 | ++schema_depth; |
| 6274 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6275 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6276 | if (data_depth < schema_depth) { |
| 6277 | return -1; |
| 6278 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6279 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 6280 | /* find the corresponding data node */ |
| 6281 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 6282 | node = node->parent; |
| 6283 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6284 | if (node->schema != schema) { |
| 6285 | return -1; |
| 6286 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6287 | } else { |
| 6288 | /* root context node */ |
| 6289 | while (node->parent) { |
| 6290 | node = node->parent; |
| 6291 | } |
| 6292 | while (node->prev->next) { |
| 6293 | node = node->prev; |
| 6294 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6295 | } |
| 6296 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6297 | *ctx_node = node; |
| 6298 | *ctx_node_type = node_type; |
| 6299 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6300 | } |
| 6301 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6302 | /** |
| 6303 | * @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] | 6304 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6305 | * |
| 6306 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 6307 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 6308 | * it is moved to point to another sibling still in the original tree. |
| 6309 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 6310 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 6311 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 6312 | * Ordering may change, but there will be no semantic change. |
| 6313 | * |
| 6314 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6315 | */ |
| 6316 | static int |
| 6317 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 6318 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 6319 | { |
| 6320 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6321 | const struct lys_node *slast; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6322 | struct ly_ctx *ctx = snode->module->ctx; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6323 | |
| 6324 | switch (snode->nodetype) { |
| 6325 | case LYS_AUGMENT: |
| 6326 | case LYS_USES: |
| 6327 | case LYS_CHOICE: |
| 6328 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6329 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 6330 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6331 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 6332 | continue; |
| 6333 | } |
| 6334 | |
| 6335 | 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] | 6336 | return -1; |
| 6337 | } |
| 6338 | } |
| 6339 | break; |
| 6340 | case LYS_CONTAINER: |
| 6341 | case LYS_LIST: |
| 6342 | case LYS_LEAF: |
| 6343 | case LYS_LEAFLIST: |
| 6344 | case LYS_ANYXML: |
| 6345 | case LYS_ANYDATA: |
| 6346 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 6347 | if (elem->schema == snode) { |
| 6348 | |
| 6349 | if (elem == *ctx_node) { |
| 6350 | /* We are going to unlink our context node! This normally cannot happen, |
| 6351 | * but we use normal top-level data nodes for faking a document root node, |
| 6352 | * so if this is the context node, we just use the next top-level node. |
| 6353 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 6354 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 6355 | * lyxp_eval() can handle this special situation. |
| 6356 | */ |
| 6357 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6358 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6359 | return -1; |
| 6360 | } |
| 6361 | |
| 6362 | if (elem->prev == elem) { |
| 6363 | /* unlinking last top-level element, use an empty data tree */ |
| 6364 | *ctx_node = NULL; |
| 6365 | } else { |
| 6366 | /* in this case just use the previous/last top-level data node */ |
| 6367 | *ctx_node = elem->prev; |
| 6368 | } |
| 6369 | } else if (elem == *node) { |
| 6370 | /* We are going to unlink the currently processed node. This does not matter that |
| 6371 | * much, but we would lose access to the original data tree, so just move our |
| 6372 | * pointer somewhere still inside it. |
| 6373 | */ |
| 6374 | if ((*node)->prev != *node) { |
| 6375 | *node = (*node)->prev; |
| 6376 | } else { |
| 6377 | /* the processed node with sibings were all unlinked, oh well */ |
| 6378 | *node = NULL; |
| 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6383 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6384 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6385 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6386 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6387 | return -1; |
| 6388 | } |
| 6389 | } else { |
| 6390 | *unlinked_nodes = elem; |
| 6391 | } |
| 6392 | |
| 6393 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 6394 | /* there can be only one instance */ |
| 6395 | break; |
| 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | break; |
| 6400 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6401 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6402 | return -1; |
| 6403 | } |
| 6404 | |
| 6405 | return EXIT_SUCCESS; |
| 6406 | } |
| 6407 | |
| 6408 | /** |
| 6409 | * @brief Relink the unlinked nodes back. |
| 6410 | * |
| 6411 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 6412 | * we simply need a sibling from the original data tree. |
| 6413 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 6414 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 6415 | * or the sibling of \p unlinked_nodes. |
| 6416 | * |
| 6417 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6418 | */ |
| 6419 | static int |
| 6420 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 6421 | { |
| 6422 | struct lyd_node *elem; |
| 6423 | |
| 6424 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6425 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6426 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6427 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6428 | return -1; |
| 6429 | } |
| 6430 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6431 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6432 | return -1; |
| 6433 | } |
| 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | return EXIT_SUCCESS; |
| 6438 | } |
| 6439 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6440 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6441 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6442 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6443 | int ret = 0; |
| 6444 | uint8_t must_size; |
| 6445 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6446 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6447 | assert(node); |
| 6448 | |
| 6449 | schema = node->schema; |
| 6450 | |
| 6451 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6452 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6453 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6454 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 6455 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6456 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6457 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 6458 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6459 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6460 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 6461 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6462 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6463 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 6464 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6465 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 6466 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6467 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 6468 | break; |
| 6469 | case LYS_NOTIF: |
| 6470 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 6471 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6472 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6473 | must_size = 0; |
| 6474 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6475 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6476 | |
| 6477 | if (must_size) { |
| 6478 | ++ret; |
| 6479 | } |
| 6480 | |
| 6481 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 6482 | if (!node->prev->next) { |
| 6483 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 6484 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 6485 | ret += 0x2; |
| 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6490 | } |
| 6491 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6492 | static struct lys_when * |
| 6493 | snode_get_when(const struct lys_node *schema) |
| 6494 | { |
| 6495 | switch (schema->nodetype) { |
| 6496 | case LYS_CONTAINER: |
| 6497 | return ((struct lys_node_container *)schema)->when; |
| 6498 | case LYS_CHOICE: |
| 6499 | return ((struct lys_node_choice *)schema)->when; |
| 6500 | case LYS_LEAF: |
| 6501 | return ((struct lys_node_leaf *)schema)->when; |
| 6502 | case LYS_LEAFLIST: |
| 6503 | return ((struct lys_node_leaflist *)schema)->when; |
| 6504 | case LYS_LIST: |
| 6505 | return ((struct lys_node_list *)schema)->when; |
| 6506 | case LYS_ANYDATA: |
| 6507 | case LYS_ANYXML: |
| 6508 | return ((struct lys_node_anydata *)schema)->when; |
| 6509 | case LYS_CASE: |
| 6510 | return ((struct lys_node_case *)schema)->when; |
| 6511 | case LYS_USES: |
| 6512 | return ((struct lys_node_uses *)schema)->when; |
| 6513 | case LYS_AUGMENT: |
| 6514 | return ((struct lys_node_augment *)schema)->when; |
| 6515 | default: |
| 6516 | return NULL; |
| 6517 | } |
| 6518 | } |
| 6519 | |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6520 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6521 | 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] | 6522 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6523 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6524 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6525 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6526 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6527 | if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && snode_get_when(schema)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6528 | return 1; |
| 6529 | } |
| 6530 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6531 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6532 | goto check_augment; |
| 6533 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6534 | while (parent) { |
| 6535 | /* stop conditions */ |
| 6536 | if (!mode) { |
| 6537 | /* stop on node that can be instantiated in data tree */ |
| 6538 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6539 | break; |
| 6540 | } |
| 6541 | } else { |
| 6542 | /* stop on the specified node */ |
| 6543 | if (parent == stop) { |
| 6544 | break; |
| 6545 | } |
| 6546 | } |
| 6547 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6548 | if (snode_get_when(parent)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6549 | return 1; |
| 6550 | } |
| 6551 | check_augment: |
| 6552 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6553 | if (parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && snode_get_when(parent->parent)) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 6554 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6555 | } |
| 6556 | parent = lys_parent(parent); |
| 6557 | } |
| 6558 | |
| 6559 | return 0; |
| 6560 | } |
| 6561 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6562 | /** |
| 6563 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6564 | * Logs directly. |
| 6565 | * |
| 6566 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 6567 | * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied |
| 6568 | * only when requiring external dependencies. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6569 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6570 | * @return |
| 6571 | * -1 - error, ly_errno is set |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6572 | * 0 - all "when" statements true |
| 6573 | * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6574 | * 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] | 6575 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6576 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6577 | resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6578 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6579 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6580 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6581 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6582 | enum lyxp_node_type ctx_node_type; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6583 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6584 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6585 | |
| 6586 | assert(node); |
| 6587 | memset(&set, 0, sizeof set); |
| 6588 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6589 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && snode_get_when(node->schema)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6590 | /* make the node dummy for the evaluation */ |
| 6591 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6592 | rc = lyxp_eval(snode_get_when(node->schema)->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6593 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6594 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6595 | if (rc) { |
| 6596 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6597 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6598 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6599 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6600 | } |
| 6601 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6602 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6603 | 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] | 6604 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6605 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6606 | if ((ignore_fail == 1) || ((snode_get_when(node->schema)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6607 | && (ignore_fail == 2))) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6608 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(node->schema)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6609 | } else { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6610 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6611 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6612 | *failed_when = snode_get_when(node->schema); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6613 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6614 | goto cleanup; |
| 6615 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6616 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6617 | |
| 6618 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6619 | 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] | 6620 | } |
| 6621 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6622 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6623 | goto check_augment; |
| 6624 | |
| 6625 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6626 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6627 | if (snode_get_when(sparent)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6628 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6629 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6630 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6631 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6632 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6633 | } |
| 6634 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6635 | |
| 6636 | unlinked_nodes = NULL; |
| 6637 | /* we do not want our node pointer to change */ |
| 6638 | tmp_node = node; |
| 6639 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6640 | if (rc) { |
| 6641 | goto cleanup; |
| 6642 | } |
| 6643 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6644 | rc = lyxp_eval(snode_get_when(sparent)->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6645 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6646 | |
| 6647 | if (unlinked_nodes && ctx_node) { |
| 6648 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6649 | rc = -1; |
| 6650 | goto cleanup; |
| 6651 | } |
| 6652 | } |
| 6653 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6654 | if (rc) { |
| 6655 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6656 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6657 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6658 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6659 | } |
| 6660 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6661 | 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] | 6662 | if (!set.val.bool) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6663 | if ((ignore_fail == 1) || ((snode_get_when(sparent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6664 | && (ignore_fail == 2))) { |
| 6665 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(sparent)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6666 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6667 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6668 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6669 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6670 | *failed_when = snode_get_when(sparent); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6671 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6672 | goto cleanup; |
| 6673 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6674 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6675 | |
| 6676 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6677 | 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] | 6678 | } |
| 6679 | |
| 6680 | check_augment: |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6681 | if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && snode_get_when(sparent->parent))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6682 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6683 | 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] | 6684 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6685 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6686 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6687 | } |
| 6688 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6689 | |
| 6690 | unlinked_nodes = NULL; |
| 6691 | tmp_node = node; |
| 6692 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6693 | if (rc) { |
| 6694 | goto cleanup; |
| 6695 | } |
| 6696 | |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6697 | rc = lyxp_eval(snode_get_when(sparent->parent)->cond, ctx_node, ctx_node_type, |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6698 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6699 | |
| 6700 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6701 | * so the tree did not actually change and there is nothing for us to do |
| 6702 | */ |
| 6703 | if (unlinked_nodes && ctx_node) { |
| 6704 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6705 | rc = -1; |
| 6706 | goto cleanup; |
| 6707 | } |
| 6708 | } |
| 6709 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6710 | if (rc) { |
| 6711 | if (rc == 1) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6712 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6713 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6714 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6715 | } |
| 6716 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6717 | 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] | 6718 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6719 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6720 | if ((ignore_fail == 1) || ((snode_get_when(sparent->parent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6721 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6722 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6723 | snode_get_when(sparent->parent)->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6724 | } else { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6725 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6726 | if (failed_when) { |
Michal Vasko | dd96229 | 2018-09-10 08:53:01 +0200 | [diff] [blame] | 6727 | *failed_when = snode_get_when(sparent->parent); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6728 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6729 | goto cleanup; |
| 6730 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6731 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6732 | |
| 6733 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6734 | 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] | 6735 | } |
| 6736 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6737 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6740 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6741 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6742 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6743 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6744 | 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] | 6745 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6746 | } |
| 6747 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6748 | static int |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6749 | check_type_union_leafref(struct lys_type *type) |
| 6750 | { |
| 6751 | uint8_t i; |
| 6752 | |
| 6753 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6754 | /* go through unions and look for leafref */ |
| 6755 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6756 | switch (type->info.uni.types[i].base) { |
| 6757 | case LY_TYPE_LEAFREF: |
| 6758 | return 1; |
| 6759 | case LY_TYPE_UNION: |
| 6760 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6761 | return 1; |
| 6762 | } |
| 6763 | break; |
| 6764 | default: |
| 6765 | break; |
| 6766 | } |
| 6767 | } |
| 6768 | |
| 6769 | return 0; |
| 6770 | } |
| 6771 | |
| 6772 | /* just inherit the flag value */ |
| 6773 | return type->der->has_union_leafref; |
| 6774 | } |
| 6775 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6776 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6777 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6778 | * |
| 6779 | * @param[in] mod Main module. |
| 6780 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6781 | * @param[in] type Type of the unresolved item. |
| 6782 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6783 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6784 | * @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] | 6785 | * |
| 6786 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6787 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6788 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6789 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 6790 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6791 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6792 | /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6793 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6794 | unsigned int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6795 | struct ly_ctx * ctx = mod->ctx; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6796 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6797 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6798 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6799 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6800 | struct ly_set *refs, *procs; |
| 6801 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6802 | struct lys_ident *ident; |
| 6803 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6804 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6805 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6806 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6807 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6808 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6809 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6810 | struct lys_ext_instance *ext, **extlist; |
| 6811 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6812 | |
| 6813 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6814 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6815 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6816 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6817 | ident = item; |
| 6818 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6819 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6820 | break; |
| 6821 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6822 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6823 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6824 | stype = item; |
| 6825 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6826 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6827 | break; |
| 6828 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6829 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6830 | stype = item; |
| 6831 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 6832 | rc = resolve_schema_leafref(stype, node, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6833 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6834 | case UNRES_TYPE_DER_EXT: |
| 6835 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6836 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6837 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6838 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6839 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6840 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6841 | /* parent */ |
| 6842 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6843 | stype = item; |
| 6844 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6845 | /* HACK type->der is temporarily unparsed type statement */ |
| 6846 | yin = (struct lyxml_elem *)stype->der; |
| 6847 | stype->der = NULL; |
| 6848 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6849 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6850 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6851 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6852 | |
| 6853 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6854 | /* may try again later */ |
| 6855 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6856 | } else { |
| 6857 | /* we need to always be able to free this, it's safe only in this case */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6858 | lydict_remove(ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6859 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6860 | } |
| 6861 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6862 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6863 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6864 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6865 | /* we need to always be able to free this, it's safe only in this case */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6866 | lyxml_free(ctx, yin); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6867 | } else { |
| 6868 | /* may try again later, put all back how it was */ |
| 6869 | stype->der = (struct lys_tpdf *)yin; |
| 6870 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6871 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6872 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6873 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6874 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6875 | LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name); |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6876 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6877 | |
| 6878 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6879 | /* fill typedef union leafref flag */ |
| 6880 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6881 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6882 | /* copy the type in case it has union leafref flag */ |
| 6883 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6884 | LOGERR(ctx, LY_EINT, "Failed to duplicate type."); |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6885 | return -1; |
| 6886 | } |
| 6887 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6888 | } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6889 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6890 | * 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] | 6891 | * grouping. The grouping cannot be used unless the unres counter is 0. |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 6892 | * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6893 | * of the type's base member. */ |
| 6894 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6895 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6896 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6897 | LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6898 | return -1; |
| 6899 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6900 | stype->value_flags |= LY_VALUE_UNRESGRP; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6901 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6902 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6903 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6904 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6905 | iff_data = str_snode; |
| 6906 | 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] | 6907 | if (!rc) { |
| 6908 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6909 | if (iff_data->infeature) { |
| 6910 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6911 | feat = *((struct lys_feature **)item); |
| 6912 | if (!feat->depfeatures) { |
| 6913 | feat->depfeatures = ly_set_new(); |
| 6914 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6915 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6916 | } |
| 6917 | /* cleanup temporary data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6918 | lydict_remove(ctx, iff_data->fname); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6919 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6920 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6921 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6922 | case UNRES_FEATURE: |
| 6923 | feat = (struct lys_feature *)item; |
| 6924 | |
| 6925 | if (feat->iffeature_size) { |
| 6926 | refs = ly_set_new(); |
| 6927 | procs = ly_set_new(); |
| 6928 | ly_set_add(procs, feat, 0); |
| 6929 | |
| 6930 | while (procs->number) { |
| 6931 | ref = procs->set.g[procs->number - 1]; |
| 6932 | ly_set_rm_index(procs, procs->number - 1); |
| 6933 | |
| 6934 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6935 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6936 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6937 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6938 | if (ref->iffeature[i].features[j - 1] == feat) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6939 | LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6940 | goto featurecheckdone; |
| 6941 | } |
| 6942 | |
| 6943 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6944 | k = refs->number; |
| 6945 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6946 | /* not yet seen feature, add it for processing */ |
| 6947 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6948 | } |
| 6949 | } |
| 6950 | } else { |
| 6951 | /* forward reference */ |
| 6952 | rc = EXIT_FAILURE; |
| 6953 | goto featurecheckdone; |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | } |
| 6958 | } |
| 6959 | rc = EXIT_SUCCESS; |
| 6960 | |
| 6961 | featurecheckdone: |
| 6962 | ly_set_free(refs); |
| 6963 | ly_set_free(procs); |
| 6964 | } |
| 6965 | |
| 6966 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6967 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6968 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6969 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6970 | case UNRES_TYPEDEF_DFLT: |
| 6971 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6972 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6973 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6974 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6975 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6976 | break; |
| 6977 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6978 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6979 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6980 | choic = item; |
| 6981 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6982 | if (!choic->dflt) { |
| 6983 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6984 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6985 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6986 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6987 | } else { |
| 6988 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6989 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6990 | break; |
| 6991 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6992 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6993 | break; |
| 6994 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6995 | unique_info = (struct unres_list_uniq *)item; |
| 6996 | 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] | 6997 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6998 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6999 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7000 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7001 | case UNRES_XPATH: |
| 7002 | node = (struct lys_node *)item; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 7003 | rc = check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7004 | break; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7005 | case UNRES_MOD_IMPLEMENT: |
| 7006 | rc = lys_make_implemented_r(mod, unres); |
| 7007 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7008 | case UNRES_EXT: |
| 7009 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7010 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 7011 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7012 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7013 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7014 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7015 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7016 | if (eplugin) { |
| 7017 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7018 | u = malloc(sizeof *u); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7019 | LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7020 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 7021 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 7022 | /* something really bad happend since the extension finalization is not actually |
| 7023 | * being resolved while adding into unres, so something more serious with the unres |
| 7024 | * list itself must happened */ |
| 7025 | return -1; |
| 7026 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7027 | } |
| 7028 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7029 | } |
| 7030 | if (!rc || rc == -1) { |
| 7031 | /* cleanup on success or fatal error */ |
| 7032 | if (ext_data->datatype == LYS_IN_YIN) { |
| 7033 | /* YIN */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7034 | lyxml_free(ctx, ext_data->data.yin); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7035 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 7036 | /* YANG */ |
| 7037 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7038 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7039 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7040 | } |
| 7041 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7042 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7043 | u = (uint8_t *)str_snode; |
| 7044 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 7045 | free(u); |
| 7046 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7047 | eplugin = ext->def->plugin; |
| 7048 | |
| 7049 | /* inherit */ |
| 7050 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 7051 | root = (struct lys_node *)ext->parent; |
| 7052 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 7053 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 7054 | /* first, check if the node already contain instance of the same extension, |
| 7055 | * in such a case we won't inherit. In case the node was actually defined as |
| 7056 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 7057 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 7058 | goto inherit_dfs_sibling; |
| 7059 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 7060 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 7061 | goto inherit_dfs_sibling; |
| 7062 | } |
| 7063 | |
| 7064 | if (eplugin->check_inherit) { |
| 7065 | /* we have a callback to check the inheritance, use it */ |
| 7066 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 7067 | case 0: |
| 7068 | /* yes - continue with the inheriting code */ |
| 7069 | break; |
| 7070 | case 1: |
| 7071 | /* no - continue with the node's sibling */ |
| 7072 | goto inherit_dfs_sibling; |
| 7073 | case 2: |
| 7074 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 7075 | goto inherit_dfs_child; |
| 7076 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7077 | LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),", |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7078 | ext->def->module->name, ext->def->name, rc); |
| 7079 | } |
| 7080 | } |
| 7081 | |
| 7082 | /* inherit the extension */ |
| 7083 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7084 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7085 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7086 | LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7087 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 7088 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 7089 | |
| 7090 | node->ext = extlist; |
| 7091 | node->ext_size++; |
| 7092 | |
| 7093 | inherit_dfs_child: |
| 7094 | /* modification of - select element for the next run - children first */ |
| 7095 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 7096 | next = NULL; |
| 7097 | } else { |
| 7098 | next = node->child; |
| 7099 | } |
| 7100 | if (!next) { |
| 7101 | inherit_dfs_sibling: |
| 7102 | /* no children, try siblings */ |
| 7103 | next = node->next; |
| 7104 | } |
| 7105 | while (!next) { |
| 7106 | /* go to the parent */ |
| 7107 | node = lys_parent(node); |
| 7108 | |
| 7109 | /* we are done if we are back in the root (the starter's parent */ |
| 7110 | if (node == root) { |
| 7111 | break; |
| 7112 | } |
| 7113 | |
| 7114 | /* parent is already processed, go to its sibling */ |
| 7115 | next = node->next; |
| 7116 | } |
| 7117 | } |
| 7118 | } |
| 7119 | } |
| 7120 | |
| 7121 | /* final check */ |
| 7122 | if (eplugin->check_result) { |
| 7123 | if ((*eplugin->check_result)(ext)) { |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7124 | LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed."); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7125 | return -1; |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | rc = 0; |
| 7130 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7131 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7132 | LOGINT(ctx); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7133 | break; |
| 7134 | } |
| 7135 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7136 | if (has_str && !rc) { |
| 7137 | /* the string is no more needed in case of success. |
| 7138 | * In case of forward reference, we will try to resolve the string later */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7139 | lydict_remove(ctx, str_snode); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7140 | } |
| 7141 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7142 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7143 | } |
| 7144 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7145 | /* logs directly */ |
| 7146 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7147 | 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] | 7148 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7149 | struct lyxml_elem *xml; |
| 7150 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7151 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7152 | const char *name = NULL; |
| 7153 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7154 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7155 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7156 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7157 | 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] | 7158 | break; |
| 7159 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7160 | 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] | 7161 | break; |
| 7162 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7163 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 7164 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7165 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 7166 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7167 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7168 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7169 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7170 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7171 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7172 | } else { |
| 7173 | LY_TREE_FOR(xml->attr, attr) { |
| 7174 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7175 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7176 | break; |
| 7177 | } |
| 7178 | } |
| 7179 | assert(attr); |
| 7180 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7181 | 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] | 7182 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7183 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7184 | iff_data = str_node; |
| 7185 | 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] | 7186 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7187 | case UNRES_FEATURE: |
| 7188 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 7189 | ((struct lys_feature *)item)->name); |
| 7190 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7191 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7192 | 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] | 7193 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7194 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7195 | case UNRES_TYPE_DFLT: |
Michal Vasko | ba835cd | 2018-02-23 09:25:48 +0100 | [diff] [blame] | 7196 | if (*(char **)str_node) { |
| 7197 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node); |
Radek Krejci | 2e2de83 | 2016-10-13 16:12:26 +0200 | [diff] [blame] | 7198 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 7199 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 7200 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7201 | break; |
| 7202 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7203 | 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] | 7204 | break; |
| 7205 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7206 | 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] | 7207 | break; |
| 7208 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7209 | 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] | 7210 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7211 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7212 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 7213 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7214 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7215 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 7216 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 7217 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7218 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7219 | case UNRES_EXT: |
| 7220 | extinfo = (struct unres_ext *)str_node; |
| 7221 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 7222 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 7223 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7224 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7225 | LOGINT(NULL); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7226 | break; |
| 7227 | } |
| 7228 | } |
| 7229 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7230 | static int |
| 7231 | resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref, |
| 7232 | int print_all_errors, uint32_t *resolved) |
| 7233 | { |
| 7234 | uint32_t i, unres_count, res_count; |
| 7235 | int ret = 0, rc; |
| 7236 | struct ly_err_item *prev_eitem; |
| 7237 | enum int_log_opts prev_ilo; |
| 7238 | LY_ERR prev_ly_errno; |
| 7239 | |
| 7240 | /* if there can be no forward references, every failure is final, so we can print it directly */ |
| 7241 | if (forward_ref) { |
| 7242 | prev_ly_errno = ly_errno; |
| 7243 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 7244 | } |
| 7245 | |
| 7246 | do { |
| 7247 | unres_count = 0; |
| 7248 | res_count = 0; |
| 7249 | |
| 7250 | for (i = 0; i < unres->count; ++i) { |
| 7251 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7252 | * if-features are resolved here to make sure that we will have all if-features for |
| 7253 | * later check of feature circular dependency */ |
| 7254 | if (unres->type[i] & types) { |
| 7255 | ++unres_count; |
| 7256 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7257 | if (unres->type[i] == UNRES_EXT_FINALIZE) { |
| 7258 | /* to avoid double free */ |
| 7259 | unres->type[i] = UNRES_RESOLVED; |
| 7260 | } |
| 7261 | if (!rc || (unres->type[i] == UNRES_XPATH)) { |
| 7262 | /* invalid XPath can never cause an error, only a warning */ |
| 7263 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 7264 | /* free the allocated structure */ |
| 7265 | free(unres->item[i]); |
| 7266 | } |
| 7267 | |
| 7268 | unres->type[i] = UNRES_RESOLVED; |
| 7269 | ++(*resolved); |
| 7270 | ++res_count; |
| 7271 | } else if ((rc == EXIT_FAILURE) && forward_ref) { |
| 7272 | /* forward reference, erase errors */ |
| 7273 | ly_err_free_next(ctx, prev_eitem); |
| 7274 | } else if (print_all_errors) { |
| 7275 | /* just so that we quit the loop */ |
| 7276 | ++res_count; |
| 7277 | ret = -1; |
| 7278 | } else { |
Michal Vasko | ffdf4ce | 2018-08-17 10:31:49 +0200 | [diff] [blame] | 7279 | if (forward_ref) { |
| 7280 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 7281 | } |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7282 | return -1; |
| 7283 | } |
| 7284 | } |
| 7285 | } |
| 7286 | } while (res_count && (res_count < unres_count)); |
| 7287 | |
| 7288 | if (res_count < unres_count) { |
| 7289 | assert(forward_ref); |
| 7290 | /* just print the errors (but we must free the ones we have and get them again :-/ ) */ |
| 7291 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7292 | |
| 7293 | for (i = 0; i < unres->count; ++i) { |
| 7294 | if (unres->type[i] & types) { |
| 7295 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7296 | } |
| 7297 | } |
| 7298 | return -1; |
| 7299 | } |
| 7300 | |
| 7301 | if (forward_ref) { |
| 7302 | /* restore log */ |
| 7303 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7304 | ly_errno = prev_ly_errno; |
| 7305 | } |
| 7306 | |
| 7307 | return ret; |
| 7308 | } |
| 7309 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7310 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7311 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7312 | * |
| 7313 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7314 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7315 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7316 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7317 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7318 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7319 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7320 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7321 | uint32_t resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7322 | |
| 7323 | assert(unres); |
| 7324 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7325 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 7326 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7327 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7328 | * if-features are resolved here to make sure that we will have all if-features for |
| 7329 | * later check of feature circular dependency */ |
| 7330 | if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF |
| 7331 | | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT, |
| 7332 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7333 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7334 | } |
| 7335 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7336 | /* another batch of resolved items */ |
| 7337 | if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT |
| 7338 | | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) { |
| 7339 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7340 | } |
| 7341 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7342 | /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 7343 | if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) { |
| 7344 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7345 | } |
| 7346 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7347 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7348 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7349 | return EXIT_SUCCESS; |
| 7350 | } |
| 7351 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7352 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7353 | * @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] | 7354 | * |
| 7355 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7356 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7357 | * @param[in] item Item to resolve. Type determined by \p type. |
| 7358 | * @param[in] type Type of the unresolved item. |
| 7359 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7360 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7361 | * @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] | 7362 | */ |
| 7363 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7364 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 7365 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7366 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7367 | int rc; |
| 7368 | const char *dictstr; |
| 7369 | |
| 7370 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 7371 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 7372 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 7373 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7374 | lydict_remove(mod->ctx, dictstr); |
| 7375 | } |
| 7376 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7377 | } |
| 7378 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7379 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7380 | * @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] | 7381 | * |
| 7382 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7383 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7384 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7385 | * @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] | 7386 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7387 | * |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7388 | * @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] | 7389 | */ |
| 7390 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7391 | 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] | 7392 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7393 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7394 | int rc; |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7395 | uint32_t u; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7396 | enum int_log_opts prev_ilo; |
| 7397 | struct ly_err_item *prev_eitem; |
| 7398 | LY_ERR prev_ly_errno; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7399 | struct lyxml_elem *yin; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7400 | struct ly_ctx *ctx = mod->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7401 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7402 | assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) |
| 7403 | && (type != UNRES_WHEN) && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7404 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7405 | /* check for duplicities in unres */ |
| 7406 | for (u = 0; u < unres->count; u++) { |
| 7407 | if (unres->type[u] == type && unres->item[u] == item && |
| 7408 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7409 | /* duplication can happen when the node contains multiple statements of the same type to check, |
| 7410 | * this can happen for example when refinement is being applied, so we just postpone the processing |
| 7411 | * and do not duplicate the information */ |
| 7412 | return EXIT_FAILURE; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7413 | } |
| 7414 | } |
| 7415 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7416 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7417 | /* extension finalization is not even tried when adding the item into the inres list, |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7418 | * xpath is not tried because it would hide some potential warnings, |
| 7419 | * implementing module must be deferred because some other nodes can be added that will need to be traversed |
| 7420 | * and their targets made implemented */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7421 | rc = EXIT_FAILURE; |
| 7422 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7423 | prev_ly_errno = ly_errno; |
| 7424 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7425 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7426 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7427 | if (rc != EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7428 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0); |
| 7429 | if (rc != -1) { |
| 7430 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7431 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7432 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7433 | if (type == UNRES_LIST_UNIQ) { |
| 7434 | /* free the allocated structure */ |
| 7435 | free(item); |
| 7436 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7437 | /* free the allocated resources */ |
| 7438 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7439 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7440 | return rc; |
| 7441 | } else { |
| 7442 | /* erase info about validation errors */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7443 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7444 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7445 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7446 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7447 | print_unres_schema_item_fail(item, type, snode); |
| 7448 | |
| 7449 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7450 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7451 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7452 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7453 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7454 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7455 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7456 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7457 | } |
| 7458 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7459 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7460 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7461 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7462 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7463 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7464 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7465 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7466 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7467 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7468 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7469 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7470 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7471 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7472 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7473 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7474 | } |
| 7475 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7476 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7477 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7478 | * |
| 7479 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7480 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7481 | * @param[in] item Old item to be resolved. |
| 7482 | * @param[in] type Type of the old unresolved item. |
| 7483 | * @param[in] new_item New item to use in the duplicate. |
| 7484 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7485 | * @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] | 7486 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7487 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7488 | 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] | 7489 | { |
| 7490 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7491 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7492 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7493 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7494 | 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] | 7495 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7496 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7497 | if (type == UNRES_LIST_UNIQ) { |
| 7498 | aux_uniq.list = item; |
| 7499 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7500 | item = &aux_uniq; |
| 7501 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7502 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7503 | |
| 7504 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7505 | if (type == UNRES_LIST_UNIQ) { |
| 7506 | free(new_item); |
| 7507 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7508 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7509 | } |
| 7510 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7511 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7512 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7513 | if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7514 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7515 | return -1; |
| 7516 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7517 | } else if (type == UNRES_IFFEAT) { |
| 7518 | /* duplicate unres_iffeature_data */ |
| 7519 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7520 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7521 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7522 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7523 | if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7524 | LOGINT(mod->ctx); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7525 | return -1; |
| 7526 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7527 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7528 | if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7529 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7530 | return -1; |
| 7531 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7532 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7533 | |
| 7534 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7535 | } |
| 7536 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7537 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7538 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7539 | 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] | 7540 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7541 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7542 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7543 | |
Michal Vasko | c643f71 | 2018-09-14 08:26:13 +0200 | [diff] [blame] | 7544 | if (!unres->count) { |
| 7545 | return -1; |
| 7546 | } |
| 7547 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7548 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7549 | i = start_on_backwards; |
| 7550 | } else { |
| 7551 | i = unres->count - 1; |
| 7552 | } |
| 7553 | for (; i > -1; i--) { |
| 7554 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7555 | continue; |
| 7556 | } |
| 7557 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7558 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7559 | break; |
| 7560 | } |
| 7561 | } else { |
| 7562 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7563 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7564 | 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] | 7565 | break; |
| 7566 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7567 | } |
| 7568 | } |
| 7569 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7570 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7571 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7572 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7573 | static void |
| 7574 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7575 | { |
| 7576 | struct lyxml_elem *yin; |
| 7577 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7578 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7579 | |
| 7580 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7581 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7582 | case UNRES_TYPE_DER: |
| 7583 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7584 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7585 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7586 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7587 | lydict_remove(ctx, yang->name); |
| 7588 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7589 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7590 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7591 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7592 | } else { |
| 7593 | lyxml_free(ctx, yin); |
| 7594 | } |
| 7595 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7596 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7597 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7598 | lydict_remove(ctx, iff_data->fname); |
| 7599 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7600 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7601 | case UNRES_IDENT: |
| 7602 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7603 | case UNRES_CHOICE_DFLT: |
| 7604 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7605 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7606 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7607 | case UNRES_LIST_UNIQ: |
| 7608 | free(unres->item[i]); |
| 7609 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7610 | case UNRES_EXT: |
| 7611 | free(unres->str_snode[i]); |
| 7612 | break; |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 7613 | case UNRES_EXT_FINALIZE: |
| 7614 | free(unres->str_snode[i]); |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7615 | default: |
| 7616 | break; |
| 7617 | } |
| 7618 | unres->type[i] = UNRES_RESOLVED; |
| 7619 | } |
| 7620 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7621 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7622 | 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] | 7623 | { |
| 7624 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7625 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7626 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7627 | if (!unres || !(*unres)) { |
| 7628 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7629 | } |
| 7630 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7631 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7632 | |
| 7633 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7634 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7635 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7636 | unresolved++; |
| 7637 | } |
| 7638 | continue; |
| 7639 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7640 | |
| 7641 | /* free heap memory for the specific item */ |
| 7642 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7643 | } |
| 7644 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7645 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7646 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7647 | free((*unres)->item); |
| 7648 | free((*unres)->type); |
| 7649 | free((*unres)->str_snode); |
| 7650 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7651 | free((*unres)); |
| 7652 | (*unres) = NULL; |
| 7653 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7654 | } |
| 7655 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7656 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7657 | * outside the operation subtree, otherwise it is a node from a foreign model) */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7658 | static int |
| 7659 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7660 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7661 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7662 | enum int_log_opts prev_ilo; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7663 | char *buf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7664 | int ret = 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7665 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7666 | if (!json_instid || !json_instid[0]) { |
| 7667 | /* no/empty value */ |
| 7668 | return 0; |
| 7669 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7670 | |
| 7671 | for (op_node = lys_parent(sleaf); |
| 7672 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7673 | op_node = lys_parent(op_node)); |
| 7674 | |
| 7675 | if (op_node && lys_parent(op_node)) { |
| 7676 | /* nested operation - any absolute path is external */ |
| 7677 | return 1; |
| 7678 | } |
| 7679 | |
| 7680 | /* get the first node from the instid */ |
| 7681 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7682 | if (!buf) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7683 | /* so that we do not have to bother with logging, say it is not external */ |
| 7684 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7685 | } |
| 7686 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7687 | /* find the first schema node, do not log */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7688 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7689 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7690 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7691 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7692 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7693 | if (!first_node) { |
| 7694 | /* unknown path, say it is not external */ |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7695 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7696 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7697 | |
| 7698 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7699 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7700 | if (op_node && (op_node != first_node)) { |
| 7701 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7702 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7703 | } |
| 7704 | |
| 7705 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7706 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7707 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7708 | } |
| 7709 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7710 | /** |
| 7711 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7712 | * |
| 7713 | * @param[in] data Data node where the path is used |
| 7714 | * @param[in] path Instance-identifier node value. |
| 7715 | * @param[in,out] ret Resolved instance or NULL. |
| 7716 | * |
| 7717 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7718 | */ |
| 7719 | static int |
| 7720 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7721 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7722 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7723 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7724 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7725 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7726 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7727 | char *str; |
| 7728 | int mod_len, name_len, has_predicate; |
| 7729 | struct unres_data node_match; |
| 7730 | |
| 7731 | memset(&node_match, 0, sizeof node_match); |
| 7732 | *ret = NULL; |
| 7733 | |
| 7734 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7735 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7736 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7737 | if (root->prev) { |
| 7738 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7739 | } |
| 7740 | |
| 7741 | /* search for the instance node */ |
| 7742 | while (path[i]) { |
| 7743 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7744 | if (j <= 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7745 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7746 | goto error; |
| 7747 | } |
| 7748 | i += j; |
| 7749 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7750 | if (model) { |
| 7751 | str = strndup(model, mod_len); |
| 7752 | if (!str) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7753 | LOGMEM(ctx); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7754 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7755 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7756 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7757 | if (ctx->data_clb) { |
| 7758 | if (!mod) { |
| 7759 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7760 | } else if (!mod->implemented) { |
| 7761 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7762 | } |
| 7763 | } |
| 7764 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7765 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7766 | if (!mod || !mod->implemented || mod->disabled) { |
| 7767 | break; |
| 7768 | } |
| 7769 | } else if (!prev_mod) { |
| 7770 | /* first iteration and we are missing module name */ |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 7771 | LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name); |
| 7772 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node."); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7773 | goto error; |
| 7774 | } else { |
| 7775 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7776 | } |
| 7777 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7778 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7779 | /* no instance exists */ |
| 7780 | break; |
| 7781 | } |
| 7782 | |
| 7783 | if (has_predicate) { |
| 7784 | /* we have predicate, so the current results must be list or leaf-list */ |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7785 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7786 | /* index of the current node (for lists with position predicates) */ |
| 7787 | cur_idx = 1; |
| 7788 | while (j < (signed)node_match.count) { |
| 7789 | node = node_match.node[j]; |
| 7790 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7791 | if (parsed < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7792 | LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7793 | goto error; |
| 7794 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7795 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7796 | if (!node) { |
| 7797 | /* current node does not satisfy the predicate */ |
| 7798 | unres_data_del(&node_match, j); |
| 7799 | } else { |
| 7800 | ++j; |
| 7801 | } |
| 7802 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7803 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7804 | |
| 7805 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7806 | } else if (node_match.count) { |
| 7807 | /* check that we are not addressing lists */ |
| 7808 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7809 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7810 | unres_data_del(&node_match, j--); |
| 7811 | } |
| 7812 | } |
| 7813 | if (!node_match.count) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7814 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys."); |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7815 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7816 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7817 | |
| 7818 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7819 | } |
| 7820 | |
| 7821 | if (!node_match.count) { |
| 7822 | /* no instance exists */ |
| 7823 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7824 | LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7825 | return EXIT_FAILURE; |
| 7826 | } |
| 7827 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7828 | return EXIT_SUCCESS; |
| 7829 | } else if (node_match.count > 1) { |
| 7830 | /* instance identifier must resolve to a single node */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7831 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7832 | goto error; |
| 7833 | } else { |
| 7834 | /* we have required result, remember it and cleanup */ |
| 7835 | *ret = node_match.node[0]; |
| 7836 | free(node_match.node); |
| 7837 | return EXIT_SUCCESS; |
| 7838 | } |
| 7839 | |
| 7840 | error: |
| 7841 | /* cleanup */ |
| 7842 | free(node_match.node); |
| 7843 | return -1; |
| 7844 | } |
| 7845 | |
| 7846 | static int |
| 7847 | 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] | 7848 | { |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7849 | struct lyxp_set xp_set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7850 | uint32_t i; |
| 7851 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7852 | memset(&xp_set, 0, sizeof xp_set); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7853 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7854 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7855 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7856 | if (lyxp_eval(path, (struct lyd_node *)leaf, LYXP_NODE_ELEM, lyd_node_module((struct lyd_node *)leaf), &xp_set, 0) != EXIT_SUCCESS) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7857 | return -1; |
| 7858 | } |
| 7859 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7860 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 7861 | for (i = 0; i < xp_set.used; ++i) { |
| 7862 | if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7863 | continue; |
| 7864 | } |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7865 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7866 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7867 | * so we can simply compare just the values */ |
| 7868 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) { |
| 7869 | /* we have the match */ |
| 7870 | *ret = xp_set.val.nodes[i].node; |
| 7871 | break; |
| 7872 | } |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7873 | } |
| 7874 | } |
| 7875 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7876 | lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, (struct lyd_node *)leaf, NULL, 0); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7877 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7878 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7879 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7880 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7881 | LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7882 | return EXIT_FAILURE; |
| 7883 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7884 | 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] | 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | return EXIT_SUCCESS; |
| 7889 | } |
| 7890 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7891 | /* 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] | 7892 | int |
| 7893 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7894 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7895 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7896 | struct ly_ctx *ctx = leaf->schema->module->ctx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7897 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7898 | struct lyd_node *ret; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7899 | enum int_log_opts prev_ilo; |
| 7900 | int found, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7901 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7902 | |
| 7903 | assert(type->base == LY_TYPE_UNION); |
| 7904 | |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7905 | if ((leaf->value_type == LY_TYPE_UNION) || ((leaf->value_type == LY_TYPE_INST) && (leaf->value_flags & LY_VALUE_UNRES))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7906 | /* either NULL or instid previously converted to JSON */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7907 | json_val = lydict_insert(ctx, leaf->value.string, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7908 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7909 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7910 | if (store) { |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame^] | 7911 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type, |
| 7912 | NULL, NULL, NULL); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7913 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7914 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7915 | |
| 7916 | /* turn logging off, we are going to try to validate the value with all the types in order */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7917 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7918 | |
| 7919 | t = NULL; |
| 7920 | found = 0; |
| 7921 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7922 | found = 0; |
| 7923 | |
| 7924 | switch (t->base) { |
| 7925 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7926 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7927 | req_inst = -1; |
| 7928 | } else { |
| 7929 | req_inst = t->info.lref.req; |
| 7930 | } |
| 7931 | |
| 7932 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7933 | if (store) { |
| 7934 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7935 | /* valid resolved */ |
| 7936 | leaf->value.leafref = ret; |
| 7937 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7938 | } else { |
| 7939 | /* valid unresolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7940 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7941 | if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7942 | return -1; |
| 7943 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7944 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7945 | } |
| 7946 | } |
| 7947 | |
| 7948 | success = 1; |
| 7949 | } |
| 7950 | break; |
| 7951 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7952 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7953 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7954 | req_inst = -1; |
| 7955 | } else { |
| 7956 | req_inst = t->info.inst.req; |
| 7957 | } |
| 7958 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7959 | 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] | 7960 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7961 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7962 | /* valid resolved */ |
| 7963 | leaf->value.instance = ret; |
| 7964 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7965 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7966 | if (json_val) { |
| 7967 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7968 | leaf->value_str = json_val; |
| 7969 | json_val = NULL; |
| 7970 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7971 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7972 | /* valid unresolved */ |
| 7973 | if (json_val) { |
| 7974 | /* put the JSON val back */ |
| 7975 | leaf->value.string = json_val; |
| 7976 | json_val = NULL; |
| 7977 | } else { |
| 7978 | leaf->value.instance = NULL; |
| 7979 | } |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7980 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7981 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7982 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7983 | } |
| 7984 | |
| 7985 | success = 1; |
| 7986 | } |
| 7987 | break; |
| 7988 | default: |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7989 | if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7990 | success = 1; |
| 7991 | } |
| 7992 | break; |
| 7993 | } |
| 7994 | |
| 7995 | if (success) { |
| 7996 | break; |
| 7997 | } |
| 7998 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7999 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8000 | if (store) { |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame^] | 8001 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t, NULL, NULL, NULL); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8002 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8003 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8004 | } |
| 8005 | |
| 8006 | /* turn logging back on */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8007 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8008 | |
| 8009 | if (json_val) { |
| 8010 | if (!success) { |
| 8011 | /* put the value back for now */ |
| 8012 | assert(leaf->value_type == LY_TYPE_UNION); |
| 8013 | leaf->value.string = json_val; |
| 8014 | } else { |
| 8015 | /* value was ultimately useless, but we could not have known */ |
| 8016 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 8017 | } |
| 8018 | } |
| 8019 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8020 | if (success) { |
| 8021 | if (resolved_type) { |
| 8022 | *resolved_type = t; |
| 8023 | } |
| 8024 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8025 | /* not found and it is required */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8026 | LOGVAL(ctx, 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] | 8027 | return EXIT_FAILURE; |
| 8028 | } |
| 8029 | |
| 8030 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8031 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8032 | } |
| 8033 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8034 | /** |
| 8035 | * @brief Resolve a single unres data item. Logs directly. |
| 8036 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8037 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8038 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8039 | * @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] | 8040 | * |
| 8041 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 8042 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 8043 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8044 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail, struct lys_when **failed_when) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8045 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8046 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8047 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8048 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8049 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8050 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8051 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8052 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8053 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8054 | switch (type) { |
| 8055 | case UNRES_LEAFREF: |
| 8056 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8057 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8058 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 8059 | req_inst = -1; |
| 8060 | } else { |
| 8061 | req_inst = sleaf->type.info.lref.req; |
| 8062 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8063 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 8064 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 8065 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8066 | /* valid resolved */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8067 | if (leaf->value_type == LY_TYPE_BITS) { |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8068 | free(leaf->value.bit); |
| 8069 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8070 | leaf->value.leafref = ret; |
| 8071 | leaf->value_type = LY_TYPE_LEAFREF; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8072 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8073 | } else { |
| 8074 | /* valid unresolved */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8075 | if (!(leaf->value_flags & LY_VALUE_UNRES)) { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 8076 | if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8077 | return -1; |
| 8078 | } |
| 8079 | } |
| 8080 | } |
| 8081 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 8082 | } else { |
| 8083 | return rc; |
| 8084 | } |
| 8085 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8086 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8087 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8088 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8089 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 8090 | if (ext_dep == -1) { |
| 8091 | return -1; |
| 8092 | } |
| 8093 | |
| 8094 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 8095 | req_inst = -1; |
| 8096 | } else { |
| 8097 | req_inst = sleaf->type.info.inst.req; |
| 8098 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8099 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 8100 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8101 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8102 | /* valid resolved */ |
| 8103 | leaf->value.instance = ret; |
| 8104 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8105 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8106 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8107 | /* valid unresolved */ |
| 8108 | leaf->value.instance = NULL; |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8109 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8110 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8111 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8112 | } else { |
| 8113 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8114 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8115 | break; |
| 8116 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8117 | case UNRES_UNION: |
| 8118 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8119 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8120 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8121 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8122 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8123 | return rc; |
| 8124 | } |
| 8125 | break; |
| 8126 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8127 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8128 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 8129 | return rc; |
| 8130 | } |
| 8131 | break; |
| 8132 | |
| 8133 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8134 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8135 | return rc; |
| 8136 | } |
| 8137 | break; |
| 8138 | |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 8139 | case UNRES_UNIQ_LEAVES: |
| 8140 | if (lyv_data_unique(node)) { |
| 8141 | return -1; |
| 8142 | } |
| 8143 | break; |
| 8144 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8145 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8146 | LOGINT(NULL); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8147 | return -1; |
| 8148 | } |
| 8149 | |
| 8150 | return EXIT_SUCCESS; |
| 8151 | } |
| 8152 | |
| 8153 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8154 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8155 | * |
| 8156 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8157 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8158 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8159 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8160 | */ |
| 8161 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8162 | 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] | 8163 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8164 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 8165 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 8166 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_UNIQ_LEAVES)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8167 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8168 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8169 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8170 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8171 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8172 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8173 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8174 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8175 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8176 | if (type == UNRES_WHEN) { |
| 8177 | /* remove previous result */ |
| 8178 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8179 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8180 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8181 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8182 | } |
| 8183 | |
| 8184 | /** |
| 8185 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 8186 | * |
Michal Vasko | 660582a | 2018-03-19 10:10:08 +0100 | [diff] [blame] | 8187 | * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected, |
| 8188 | * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit |
| 8189 | * non-presence containers may need to be deleted). |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8190 | * |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 8191 | * If options includes #LYD_OPT_WHENAUTODEL, the non-default nodes with false when conditions are auto-deleted. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8192 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8193 | * @param[in] ctx Context used. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8194 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8195 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 8196 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8197 | * |
| 8198 | * @return EXIT_SUCCESS on success, -1 on error. |
| 8199 | */ |
| 8200 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8201 | resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8202 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8203 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8204 | int rc, progress, ignore_fail; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8205 | enum int_log_opts prev_ilo; |
| 8206 | struct ly_err_item *prev_eitem; |
mohitarora24 | 89837dc | 2018-05-01 15:09:36 +0530 | [diff] [blame] | 8207 | LY_ERR prev_ly_errno = ly_errno; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8208 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8209 | struct lys_when *when; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8210 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8211 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8212 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8213 | |
| 8214 | if (!unres->count) { |
| 8215 | return EXIT_SUCCESS; |
| 8216 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8217 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8218 | if (options & (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] | 8219 | ignore_fail = 1; |
| 8220 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 8221 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8222 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8223 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8224 | } |
| 8225 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8226 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8227 | if (!ignore_fail) { |
| 8228 | /* remember logging state only if errors are generated and valid */ |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8229 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 8230 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8231 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8232 | /* |
| 8233 | * when-stmt first |
| 8234 | */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8235 | first = 1; |
| 8236 | stmt_count = 0; |
| 8237 | resolved = 0; |
| 8238 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8239 | do { |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8240 | if (!ignore_fail) { |
| 8241 | ly_err_free_next(ctx, prev_eitem); |
| 8242 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8243 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 8244 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8245 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8246 | continue; |
| 8247 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8248 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8249 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8250 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8251 | } |
| 8252 | |
| 8253 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 8254 | for (parent = unres->node[i]->parent; |
| 8255 | parent && LYD_WHEN_DONE(parent->when_status); |
| 8256 | parent = parent->parent) { |
| 8257 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 8258 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8259 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8260 | */ |
| 8261 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 8262 | unres->type[i] = UNRES_RESOLVED; |
| 8263 | resolved++; |
| 8264 | break; |
| 8265 | } |
| 8266 | } |
| 8267 | if (parent) { |
| 8268 | continue; |
| 8269 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8270 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8271 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8272 | if (!rc) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8273 | /* finish with error/delete the node only if when was false, an external dependency was not required, |
| 8274 | * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */ |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8275 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 8276 | && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) { |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 8277 | if (!(options & LYD_OPT_WHENAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8278 | /* false when condition */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8279 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8280 | } /* follows else */ |
| 8281 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8282 | /* auto-delete */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8283 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond); |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8284 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8285 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 8286 | /* if it has parent non-presence containers that would be empty, we should actually |
| 8287 | * remove the container |
| 8288 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8289 | for (parent = unres->node[i]; |
| 8290 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 8291 | parent = parent->parent) { |
| 8292 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 8293 | /* presence container */ |
| 8294 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8295 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8296 | if (parent->next || parent->prev != parent) { |
| 8297 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 8298 | break; |
| 8299 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8300 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8301 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8302 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8303 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8304 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8305 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8306 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8307 | lyd_unlink(unres->node[i]); |
| 8308 | unres->type[i] = UNRES_DELETE; |
| 8309 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8310 | |
| 8311 | /* update the rest of unres items */ |
| 8312 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 8313 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8314 | continue; |
| 8315 | } |
| 8316 | |
| 8317 | /* test if the node is in subtree to be deleted */ |
| 8318 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 8319 | if (parent == unres->node[i]) { |
| 8320 | /* yes, it is */ |
| 8321 | unres->type[j] = UNRES_RESOLVED; |
| 8322 | resolved++; |
| 8323 | break; |
| 8324 | } |
| 8325 | } |
| 8326 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8327 | } else { |
| 8328 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8329 | } |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8330 | if (!ignore_fail) { |
| 8331 | ly_err_free_next(ctx, prev_eitem); |
| 8332 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8333 | resolved++; |
| 8334 | progress = 1; |
| 8335 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8336 | goto error; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 8337 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8338 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8339 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8340 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8341 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8342 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8343 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8344 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8345 | } |
| 8346 | |
| 8347 | for (i = 0; del_items && i < unres->count; i++) { |
| 8348 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 8349 | if (unres->type[i] != UNRES_DELETE) { |
| 8350 | continue; |
| 8351 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8352 | if (!unres->node[i]) { |
| 8353 | unres->type[i] = UNRES_RESOLVED; |
| 8354 | del_items--; |
| 8355 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8356 | } |
| 8357 | |
| 8358 | /* really remove the complete subtree */ |
| 8359 | lyd_free(unres->node[i]); |
| 8360 | unres->type[i] = UNRES_RESOLVED; |
| 8361 | del_items--; |
| 8362 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8363 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8364 | /* |
| 8365 | * now leafrefs |
| 8366 | */ |
| 8367 | if (options & LYD_OPT_TRUSTED) { |
| 8368 | /* we want to attempt to resolve leafrefs */ |
| 8369 | assert(!ignore_fail); |
| 8370 | ignore_fail = 1; |
| 8371 | |
| 8372 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8373 | ly_errno = prev_ly_errno; |
| 8374 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8375 | first = 1; |
| 8376 | stmt_count = 0; |
| 8377 | resolved = 0; |
| 8378 | do { |
| 8379 | progress = 0; |
| 8380 | for (i = 0; i < unres->count; i++) { |
| 8381 | if (unres->type[i] != UNRES_LEAFREF) { |
| 8382 | continue; |
| 8383 | } |
| 8384 | if (first) { |
| 8385 | /* count leafref nodes in unres list */ |
| 8386 | stmt_count++; |
| 8387 | } |
| 8388 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8389 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8390 | if (!rc) { |
| 8391 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8392 | if (!ignore_fail) { |
| 8393 | ly_err_free_next(ctx, prev_eitem); |
| 8394 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8395 | resolved++; |
| 8396 | progress = 1; |
| 8397 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8398 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8399 | } /* else forward reference */ |
| 8400 | } |
| 8401 | first = 0; |
| 8402 | } while (progress && resolved < stmt_count); |
| 8403 | |
| 8404 | /* do we have some unresolved leafrefs? */ |
| 8405 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8406 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8407 | } |
| 8408 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8409 | if (!ignore_fail) { |
| 8410 | /* log normally now, throw away irrelevant errors */ |
| 8411 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8412 | ly_errno = prev_ly_errno; |
| 8413 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8414 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8415 | /* |
| 8416 | * rest |
| 8417 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8418 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8419 | if (unres->type[i] == UNRES_RESOLVED) { |
| 8420 | continue; |
| 8421 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8422 | 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] | 8423 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8424 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8425 | if (rc) { |
| 8426 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8427 | return -1; |
| 8428 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8429 | |
| 8430 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8431 | } |
| 8432 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8433 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8434 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8435 | return EXIT_SUCCESS; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8436 | |
| 8437 | error: |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8438 | if (!ignore_fail) { |
| 8439 | /* print all the new errors */ |
| 8440 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 8441 | /* do not restore ly_errno, it was udpated properly */ |
| 8442 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8443 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8444 | } |