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 | 00f28a7 | 2018-11-12 11:55:36 +0100 | [diff] [blame] | 3591 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Default value \"%s\" cannot be checked in an unresolved leafref.", |
| 3592 | dflt); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3593 | goto cleanup; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3594 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3595 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3596 | if (!ret) { |
| 3597 | /* adopt possibly changed default value to its canonical form */ |
| 3598 | if (*value) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3599 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3600 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3601 | dflt = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3602 | } |
| 3603 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3604 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 3605 | 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] | 3606 | /* possible forward reference */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3607 | ret = EXIT_FAILURE; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3608 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3609 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3610 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3611 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3612 | /* we have refined bits/enums */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3613 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3614 | "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] | 3615 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3616 | } |
| 3617 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3618 | } else { |
| 3619 | /* success - adopt canonical form from the node into the default value */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3620 | if (!ly_strequal(dflt, node.value_str, 1)) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3621 | /* this can happen only if we have non-inherited default value, |
| 3622 | * inherited default values are already in canonical form */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3623 | assert(ly_strequal(dflt, *value, 1)); |
| 3624 | |
| 3625 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3626 | *value = node.value_str; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3627 | node.value_str = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3628 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3629 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3630 | } |
| 3631 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3632 | cleanup: |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame] | 3633 | 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] | 3634 | lydict_remove(ctx, node.value_str); |
| 3635 | if (tpdf && node.schema) { |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3636 | free((char *)node.schema->name); |
| 3637 | free(node.schema); |
| 3638 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3639 | lydict_remove(ctx, dflt); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3640 | |
| 3641 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3644 | /** |
| 3645 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3646 | * |
| 3647 | * @param[in] key The key to check. |
| 3648 | * @param[in] flags What flags to check. |
| 3649 | * @param[in] list The list of all the keys. |
| 3650 | * @param[in] index Index of the key in the key list. |
| 3651 | * @param[in] name The name of the keys. |
| 3652 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3653 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3654 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3655 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3656 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3657 | 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] | 3658 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3659 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3660 | char *dup = NULL; |
| 3661 | int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3662 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3663 | |
| 3664 | /* existence */ |
| 3665 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3666 | if (name[len] != '\0') { |
| 3667 | dup = strdup(name); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3668 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3669 | dup[len] = '\0'; |
| 3670 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3671 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3672 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3673 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3674 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | /* uniqueness */ |
| 3678 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3679 | if (key == list->keys[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3680 | LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3681 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3686 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3687 | LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3688 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /* type of the leaf is not built-in empty */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 3692 | 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] | 3693 | LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3694 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* config attribute is the same as of the list */ |
Michal Vasko | 627016e | 2018-10-24 09:25:55 +0200 | [diff] [blame] | 3698 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) |
| 3699 | && ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3700 | LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3701 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3702 | } |
| 3703 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3704 | /* key is not placed from augment */ |
| 3705 | if (key->parent->nodetype == LYS_AUGMENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3706 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3707 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3708 | return -1; |
| 3709 | } |
| 3710 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3711 | /* key is not when/if-feature -conditional */ |
| 3712 | j = 0; |
| 3713 | if (key->when || (key->iffeature_size && (j = 1))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3714 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3715 | 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] | 3716 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3717 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3718 | } |
| 3719 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3720 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3721 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3722 | |
| 3723 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3724 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3725 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3726 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3727 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3728 | * |
| 3729 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3730 | */ |
| 3731 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3732 | 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] | 3733 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3734 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3735 | const struct lys_node *leaf = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3736 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3737 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3738 | 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] | 3739 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3740 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3741 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3742 | if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3743 | 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] | 3744 | } else if (rc == -2) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3745 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3746 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3747 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3748 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3749 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3750 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3751 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3752 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3753 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3754 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3755 | if (leaf->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3756 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3757 | 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] | 3758 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3761 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3762 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3763 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3764 | return -1; |
| 3765 | } |
| 3766 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3767 | /* check that all unique's targets are of the same config type */ |
| 3768 | if (*trg_type) { |
| 3769 | 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] | 3770 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3771 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3772 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3773 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3774 | return -1; |
| 3775 | } |
| 3776 | } else { |
| 3777 | /* first unique */ |
| 3778 | if (leaf->flags & LYS_CONFIG_W) { |
| 3779 | *trg_type = 1; |
| 3780 | } else { |
| 3781 | *trg_type = 2; |
| 3782 | } |
| 3783 | } |
| 3784 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3785 | /* set leaf's unique flag */ |
| 3786 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3787 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3788 | return EXIT_SUCCESS; |
| 3789 | |
| 3790 | error: |
| 3791 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3792 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3795 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3796 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3797 | { |
| 3798 | /* there are items after the one deleted */ |
| 3799 | if (i+1 < unres->count) { |
| 3800 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3801 | 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] | 3802 | |
| 3803 | /* deleting the last item */ |
| 3804 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3805 | free(unres->node); |
| 3806 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3810 | --unres->count; |
| 3811 | } |
| 3812 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3813 | /** |
| 3814 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3815 | * |
| 3816 | * @param[in] mod Module to search in. |
| 3817 | * @param[in] name Name of the data node. |
| 3818 | * @param[in] nam_len Length of the name. |
| 3819 | * @param[in] start Data node to start the search from. |
| 3820 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3821 | * they are replaced (!!) with the resolvents. |
| 3822 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3823 | * @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] | 3824 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3825 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3826 | 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] | 3827 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3828 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3829 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3830 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3831 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3832 | if (!parents->count) { |
| 3833 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3834 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3835 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3836 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3837 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3838 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3839 | 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] | 3840 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3841 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3842 | continue; |
| 3843 | } |
| 3844 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3845 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3846 | 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] | 3847 | && node->schema->name[nam_len] == '\0') { |
| 3848 | /* matching target */ |
| 3849 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3850 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3851 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3852 | flag = 1; |
| 3853 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3854 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3855 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3856 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3857 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3858 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3859 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3863 | |
| 3864 | if (!flag) { |
| 3865 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3866 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3867 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3868 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3869 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3872 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3873 | } |
| 3874 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3875 | static int |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3876 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_module *local_mod, |
| 3877 | const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3878 | { |
| 3879 | int dep1, dep2; |
| 3880 | const struct lys_node *node; |
| 3881 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3882 | if (!op_node) { |
| 3883 | /* leafref pointing to a different module */ |
| 3884 | if (local_mod != lys_node_module(first_node)) { |
| 3885 | return 1; |
| 3886 | } |
| 3887 | } else if (lys_parent(op_node)) { |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3888 | /* inner operation (notif/action) */ |
| 3889 | if (abs_path) { |
| 3890 | return 1; |
| 3891 | } else { |
| 3892 | /* compare depth of both nodes */ |
| 3893 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3894 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3895 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3896 | return 1; |
| 3897 | } |
| 3898 | } |
| 3899 | } else { |
| 3900 | /* top-level operation (notif/rpc) */ |
| 3901 | if (op_node != first_node) { |
| 3902 | return 1; |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3909 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3910 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3911 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3912 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3913 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3914 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3915 | * @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] | 3916 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3917 | * @return 0 on forward reference, otherwise the number |
| 3918 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3919 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3920 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3921 | static int |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3922 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, struct lys_node *parent) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3923 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3924 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3925 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3926 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3927 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 3928 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3929 | struct ly_ctx *ctx = context_node->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3930 | |
| 3931 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3932 | 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] | 3933 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3934 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3935 | return -parsed+i; |
| 3936 | } |
| 3937 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3938 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3939 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3940 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3941 | if (sour_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3942 | 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] | 3943 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3944 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3945 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3946 | 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] | 3947 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3948 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3949 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3953 | dest_parent_times = 0; |
| 3954 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3955 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3956 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3957 | 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] | 3958 | return -parsed; |
| 3959 | } |
| 3960 | pke_parsed += i; |
| 3961 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3962 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | ec06ed4 | 2018-12-07 08:33:55 +0100 | [diff] [blame] | 3963 | if (!dst_node) { |
| 3964 | /* we went too much into parents, there is no parent anymore */ |
| 3965 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr); |
| 3966 | return 0; |
| 3967 | } |
| 3968 | |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3969 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3970 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3971 | /* we are in an unresolved augment, cannot evaluate */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3972 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3973 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3974 | return 0; |
| 3975 | } |
| 3976 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3977 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3978 | * all schema nodes that cannot be instantiated in data tree */ |
| 3979 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3980 | 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] | 3981 | dst_node = lys_parent(dst_node)); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3982 | } |
| 3983 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3984 | if (dest_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3985 | 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] | 3986 | } else { |
Michal Vasko | a50e60c | 2018-08-22 12:07:21 +0200 | [diff] [blame] | 3987 | trg_mod = lys_node_module(parent); |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3988 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3989 | 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] | 3990 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3991 | 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] | 3992 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3993 | } |
| 3994 | |
| 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 */ |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 4245 | if (resolve_schema_leafref_valid_dep_flag(op_node, cur_module, node, (parent_times == -1 ? 1 : 0))) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4246 | parent->flags |= LYS_LEAFREF_DEP; |
| 4247 | } |
| 4248 | first_iter = 0; |
| 4249 | } |
| 4250 | |
| 4251 | if (has_predicate) { |
| 4252 | /* we have predicate, so the current result must be list */ |
| 4253 | if (node->nodetype != LYS_LIST) { |
| 4254 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4255 | return -1; |
| 4256 | } |
| 4257 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 4258 | i = resolve_schema_leafref_predicate(id, node, parent); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4259 | if (!i) { |
| 4260 | return EXIT_FAILURE; |
| 4261 | } else if (i < 0) { |
| 4262 | return -1; |
| 4263 | } |
| 4264 | id += i; |
| 4265 | has_predicate = 0; |
| 4266 | } |
| 4267 | } while (id[0]); |
| 4268 | |
| 4269 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 4270 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
| 4271 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4272 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path); |
| 4273 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4274 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4275 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4276 | /* check status */ |
| 4277 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
| 4278 | node->flags, node->module, node->name, node)) { |
| 4279 | return -1; |
| 4280 | } |
| 4281 | |
| 4282 | /* assign */ |
| 4283 | type->info.lref.target = (struct lys_node_leaf *)node; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4286 | /* as the last thing traverse this leafref and make targets on the path implemented */ |
| 4287 | if (lys_node_module(parent)->implemented) { |
| 4288 | /* make all the modules in the path implemented */ |
| 4289 | for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) { |
| 4290 | if (!lys_node_module(node)->implemented) { |
| 4291 | lys_node_module(node)->implemented = 1; |
| 4292 | if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4293 | return -1; |
| 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | /* store the backlink from leafref target */ |
| 4299 | if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) { |
| 4300 | return -1; |
| 4301 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4302 | } |
| 4303 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4304 | /* check if leafref and its target are under common if-features */ |
| 4305 | return check_leafref_features(type); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4308 | /** |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4309 | * @brief Compare 2 data node values. |
| 4310 | * |
| 4311 | * Comparison performed on canonical forms, the first value |
| 4312 | * is first transformed into canonical form. |
| 4313 | * |
| 4314 | * @param[in] node Leaf/leaf-list with these values. |
| 4315 | * @param[in] noncan_val Non-canonical value. |
| 4316 | * @param[in] noncan_val_len Length of \p noncal_val. |
| 4317 | * @param[in] can_val Canonical value. |
| 4318 | * @return 1 if equal, 0 if not, -1 on error (logged). |
| 4319 | */ |
| 4320 | static int |
| 4321 | valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val) |
| 4322 | { |
| 4323 | int ret; |
| 4324 | struct lyd_node_leaf_list leaf; |
| 4325 | struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node; |
| 4326 | |
| 4327 | /* dummy leaf */ |
| 4328 | memset(&leaf, 0, sizeof leaf); |
| 4329 | leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len); |
| 4330 | |
| 4331 | repeat: |
| 4332 | leaf.value_type = sleaf->type.base; |
| 4333 | leaf.schema = node; |
| 4334 | |
| 4335 | if (leaf.value_type == LY_TYPE_LEAFREF) { |
| 4336 | if (!sleaf->type.info.lref.target) { |
| 4337 | /* 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] | 4338 | LOGINT(node->module->ctx); |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4339 | ret = -1; |
| 4340 | goto finish; |
| 4341 | } |
| 4342 | sleaf = sleaf->type.info.lref.target; |
| 4343 | goto repeat; |
| 4344 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 4345 | 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] | 4346 | ret = -1; |
| 4347 | goto finish; |
| 4348 | } |
| 4349 | } |
| 4350 | |
| 4351 | if (!strcmp(leaf.value_str, can_val)) { |
| 4352 | ret = 1; |
| 4353 | } else { |
| 4354 | ret = 0; |
| 4355 | } |
| 4356 | |
| 4357 | finish: |
| 4358 | lydict_remove(node->module->ctx, leaf.value_str); |
| 4359 | return ret; |
| 4360 | } |
| 4361 | |
| 4362 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4363 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4364 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4365 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 4366 | * @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] | 4367 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4368 | * @param[in,out] node Node matching the restriction without |
| 4369 | * the predicate. If it does not satisfy the predicate, |
| 4370 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4371 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4372 | * @return Number of characters successfully parsed, |
| 4373 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4374 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4375 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4376 | 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] | 4377 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4378 | /* ... /node[key=value] ... */ |
| 4379 | struct lyd_node_leaf_list *key; |
| 4380 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 4381 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4382 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4383 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4384 | struct ly_ctx *ctx = prev_mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4385 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4386 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4387 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4388 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4389 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4390 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 4391 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4392 | } |
| 4393 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4394 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4395 | if (!(*node)) { |
| 4396 | /* just parse it all */ |
| 4397 | continue; |
| 4398 | } |
| 4399 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4400 | /* target */ |
| 4401 | if (name[0] == '.') { |
| 4402 | /* leaf-list value */ |
| 4403 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4404 | 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] | 4405 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4406 | parsed = -1; |
| 4407 | goto cleanup; |
| 4408 | } |
| 4409 | |
| 4410 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4411 | 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] | 4412 | *node = NULL; |
| 4413 | goto cleanup; |
| 4414 | } |
| 4415 | |
| 4416 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4417 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4418 | |
| 4419 | /* keyless list position */ |
| 4420 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4421 | 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] | 4422 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4423 | parsed = -1; |
| 4424 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4425 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4426 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4427 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4428 | 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] | 4429 | (*node)->schema->name); |
| 4430 | parsed = -1; |
| 4431 | goto cleanup; |
| 4432 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4433 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4434 | /* check the index */ |
| 4435 | if (atoi(name) != cur_idx) { |
| 4436 | *node = NULL; |
| 4437 | goto cleanup; |
| 4438 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4439 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4440 | } else { |
| 4441 | /* list key value */ |
| 4442 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4443 | 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] | 4444 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4445 | parsed = -1; |
| 4446 | goto cleanup; |
| 4447 | } |
| 4448 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4449 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4450 | /* prepare key array */ |
| 4451 | if (!list_keys) { |
| 4452 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4453 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4454 | for (i = 0; i < slist->keys_size; ++i) { |
| 4455 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4456 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4457 | } |
| 4458 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4459 | /* find the schema key leaf */ |
| 4460 | for (i = 0; i < slist->keys_size; ++i) { |
| 4461 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4462 | break; |
| 4463 | } |
| 4464 | } |
| 4465 | if (i == slist->keys_size) { |
| 4466 | /* this list has no such key */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4467 | 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] | 4468 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4469 | parsed = -1; |
| 4470 | goto cleanup; |
| 4471 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4472 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4473 | /* check module */ |
| 4474 | if (model) { |
| 4475 | 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] | 4476 | 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] | 4477 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4478 | parsed = -1; |
| 4479 | goto cleanup; |
| 4480 | } |
| 4481 | } else { |
| 4482 | if (list_keys[i]->module != prev_mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4483 | 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] | 4484 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4485 | parsed = -1; |
| 4486 | goto cleanup; |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | /* find the actual data key */ |
| 4491 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4492 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4493 | break; |
| 4494 | } |
| 4495 | } |
| 4496 | if (!key) { |
| 4497 | /* list instance is missing a key? definitely should not happen */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4498 | LOGINT(ctx); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4499 | parsed = -1; |
| 4500 | goto cleanup; |
| 4501 | } |
| 4502 | |
| 4503 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4504 | if (!valequal(key->schema, value, val_len, key->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4505 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4506 | /* we still want to parse the whole predicate */ |
| 4507 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /* everything is fine, mark this key as resolved */ |
| 4511 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4512 | } |
| 4513 | } while (has_predicate); |
| 4514 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4515 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4516 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4517 | for (i = 0; i < slist->keys_size; ++i) { |
| 4518 | if (list_keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4519 | 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] | 4520 | parsed = -1; |
| 4521 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4522 | } |
| 4523 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4524 | } |
| 4525 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4526 | cleanup: |
| 4527 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4528 | return parsed; |
| 4529 | } |
| 4530 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4531 | static int |
| 4532 | check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4533 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4534 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4535 | struct lyxp_set set; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4536 | enum int_log_opts prev_ilo; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4537 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4538 | if (check_place) { |
| 4539 | parent = node; |
| 4540 | while (parent) { |
| 4541 | if (parent->nodetype == LYS_GROUPING) { |
| 4542 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4543 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4544 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4545 | if (parent->nodetype == LYS_AUGMENT) { |
| 4546 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4547 | /* unresolved augment, skip for now (will be checked later) */ |
| 4548 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4549 | } else { |
| 4550 | parent = ((struct lys_node_augment *)parent)->target; |
| 4551 | continue; |
| 4552 | } |
| 4553 | } |
| 4554 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4555 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4556 | } |
| 4557 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4558 | memset(&set, 0, sizeof set); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4559 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4560 | /* produce just warnings */ |
| 4561 | ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL); |
| 4562 | lyxp_node_atomize(node, &set, 1); |
| 4563 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
| 4564 | |
| 4565 | if (set.val.snodes) { |
| 4566 | free(set.val.snodes); |
| 4567 | } |
| 4568 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4569 | } |
| 4570 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4571 | static int |
| 4572 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4573 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4574 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4575 | |
| 4576 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4577 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4578 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4579 | 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] | 4580 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4581 | return -1; |
| 4582 | } |
| 4583 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4584 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4585 | } else if (type->base == LY_TYPE_UNION) { |
| 4586 | for (i = 0; i < type->info.uni.count; i++) { |
| 4587 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4588 | return -1; |
| 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4595 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4596 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4597 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4598 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4599 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4600 | * @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] | 4601 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4602 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4603 | * |
| 4604 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4605 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4606 | int |
| 4607 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4608 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4609 | struct lys_node_leaf *leaf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4610 | struct ly_ctx *ctx; |
| 4611 | |
| 4612 | if (!node) { |
| 4613 | return 0; |
| 4614 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4615 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4616 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4617 | ctx = node->module->ctx; |
| 4618 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4619 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4620 | if (clear) { |
| 4621 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4622 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4623 | } else { |
| 4624 | if (node->flags & LYS_CONFIG_SET) { |
| 4625 | /* skip nodes with an explicit config value */ |
| 4626 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4627 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4628 | 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] | 4629 | return -1; |
| 4630 | } |
| 4631 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4632 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4633 | |
| 4634 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4635 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4636 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4637 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4638 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4639 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4640 | return -1; |
| 4641 | } |
| 4642 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4643 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4644 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4645 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4646 | return -1; |
| 4647 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4648 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4649 | leaf = (struct lys_node_leaf *)node; |
| 4650 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4651 | return -1; |
| 4652 | } |
| 4653 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4654 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4655 | |
| 4656 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4657 | } |
| 4658 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4659 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4660 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4661 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4662 | * @param[in] aug Augment to use. |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4663 | * @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] | 4664 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4665 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4666 | * @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] | 4667 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4668 | static int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4669 | 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] | 4670 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4671 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4672 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4673 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4674 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4675 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4676 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4677 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4678 | mod = lys_main_module(aug->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4679 | ctx = mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4680 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4681 | /* set it as not applied for now */ |
| 4682 | aug->flags |= LYS_NOTAPPLIED; |
| 4683 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4684 | /* 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] | 4685 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4686 | /* resolve target node */ |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4687 | 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] | 4688 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4689 | LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4690 | return -1; |
| 4691 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4692 | if (!set) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4693 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4694 | return EXIT_FAILURE; |
| 4695 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4696 | aug->target = set->set.s[0]; |
| 4697 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4698 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4699 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4700 | /* make this module implemented if the target module is (if the target is in an unimplemented module, |
| 4701 | * it is fine because when we will be making that module implemented, its augment will be applied |
| 4702 | * and that augment target module made implemented, recursively) */ |
| 4703 | if (mod->implemented && !lys_node_module(aug->target)->implemented) { |
| 4704 | lys_node_module(aug->target)->implemented = 1; |
| 4705 | if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4706 | return -1; |
| 4707 | } |
| 4708 | } |
| 4709 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4710 | /* check for mandatory nodes - if the target node is in another module |
| 4711 | * the added nodes cannot be mandatory |
| 4712 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4713 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4714 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4715 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4716 | } |
| 4717 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4718 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4719 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4720 | LY_TREE_FOR(aug->child, sub) { |
| 4721 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4722 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4723 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4724 | 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] | 4725 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4726 | return -1; |
| 4727 | } |
| 4728 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4729 | } 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] | 4730 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4731 | 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] | 4732 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4733 | 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] | 4734 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4735 | return -1; |
| 4736 | } |
| 4737 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4738 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4739 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4740 | 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] | 4741 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4742 | 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] | 4743 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4744 | return -1; |
| 4745 | } |
| 4746 | } |
| 4747 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4748 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 4749 | 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] | 4750 | return -1; |
| 4751 | } |
| 4752 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4753 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4754 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4755 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4756 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4757 | } |
| 4758 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4759 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4760 | if (!aug->child) { |
| 4761 | /* empty augment, nothing to connect, but it is techincally applied */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4762 | LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4763 | aug->flags &= ~LYS_NOTAPPLIED; |
Radek Krejci | aa6b2a1 | 2017-10-26 15:52:39 +0200 | [diff] [blame] | 4764 | } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) { |
| 4765 | /* we try to connect the augment only in case the module is implemented or |
| 4766 | * the augment applies on the used grouping, anyway we failed here */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4767 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4768 | } |
| 4769 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4770 | return EXIT_SUCCESS; |
| 4771 | } |
| 4772 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4773 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4774 | 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] | 4775 | { |
| 4776 | enum LY_VLOG_ELEM vlog_type; |
| 4777 | void *vlog_node; |
| 4778 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4779 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4780 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4781 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4782 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4783 | struct lys_ext_instance *tmp_ext; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4784 | struct ly_ctx *ctx = NULL; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4785 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4786 | |
| 4787 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4788 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4789 | vlog_node = info->parent; |
| 4790 | vlog_type = LY_VLOG_LYS; |
| 4791 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4792 | case LYEXT_PAR_MODULE: |
| 4793 | case LYEXT_PAR_IMPORT: |
| 4794 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4795 | vlog_node = NULL; |
| 4796 | vlog_type = LY_VLOG_LYS; |
| 4797 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4798 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4799 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4800 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4801 | break; |
| 4802 | } |
| 4803 | |
| 4804 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4805 | /* YIN */ |
| 4806 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4807 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4808 | 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] | 4809 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4810 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4811 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4812 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4813 | ctx = mod->ctx; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4814 | |
| 4815 | /* find the extension definition */ |
| 4816 | e = NULL; |
| 4817 | for (i = 0; i < mod->extensions_size; i++) { |
| 4818 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4819 | e = &mod->extensions[i]; |
| 4820 | break; |
| 4821 | } |
| 4822 | } |
| 4823 | /* try submodules */ |
| 4824 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4825 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4826 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4827 | e = &mod->inc[j].submodule->extensions[i]; |
| 4828 | break; |
| 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4833 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4834 | return EXIT_FAILURE; |
| 4835 | } |
| 4836 | |
| 4837 | /* 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] | 4838 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4839 | if (e->plugin && e->plugin->check_position) { |
| 4840 | /* common part - we have plugin with position checking function, use it first */ |
| 4841 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4842 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4843 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4844 | return -1; |
| 4845 | } |
| 4846 | } |
| 4847 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4848 | /* extension type-specific part - allocation */ |
| 4849 | if (e->plugin) { |
| 4850 | etype = e->plugin->type; |
| 4851 | } else { |
| 4852 | /* default type */ |
| 4853 | etype = LYEXT_FLAG; |
| 4854 | } |
| 4855 | switch (etype) { |
| 4856 | case LYEXT_FLAG: |
| 4857 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4858 | break; |
| 4859 | case LYEXT_COMPLEX: |
| 4860 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4861 | break; |
| 4862 | case LYEXT_ERR: |
| 4863 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4864 | LOGINT(ctx); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4865 | return -1; |
| 4866 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4867 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4868 | |
| 4869 | /* common part for all extension types */ |
| 4870 | (*ext)->def = e; |
| 4871 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4872 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4873 | (*ext)->insubstmt = info->substmt; |
| 4874 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4875 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4876 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4877 | |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4878 | if (e->argument) { |
| 4879 | if (!(e->flags & LYS_YINELEM)) { |
| 4880 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4881 | if (!(*ext)->arg_value) { |
PavolVican | e7a1fc6 | 2018-02-19 16:50:27 +0100 | [diff] [blame] | 4882 | 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] | 4883 | return -1; |
| 4884 | } |
| 4885 | |
| 4886 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4887 | } else { |
| 4888 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4889 | if (ly_strequal(yin->name, e->argument, 1)) { |
| 4890 | (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0); |
| 4891 | lyxml_free(mod->ctx, yin); |
| 4892 | break; |
| 4893 | } |
| 4894 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4895 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4898 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 4899 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 4900 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4903 | (*ext)->nodetype = LYS_EXT; |
| 4904 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4905 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4906 | /* extension type-specific part - parsing content */ |
| 4907 | switch (etype) { |
| 4908 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4909 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4910 | if (!yin->ns) { |
| 4911 | /* garbage */ |
| 4912 | lyxml_free(mod->ctx, yin); |
| 4913 | continue; |
| 4914 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4915 | /* standard YANG statements are not expected here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4916 | 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] | 4917 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4918 | } else if (yin->ns == info->data.yin->ns && |
| 4919 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4920 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4921 | if ((*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4922 | 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] | 4923 | return -1; |
| 4924 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4925 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4926 | yin->content = NULL; |
| 4927 | lyxml_free(mod->ctx, yin); |
| 4928 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4929 | /* extension instance */ |
| 4930 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4931 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4932 | return -1; |
| 4933 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4934 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4935 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4936 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4937 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4938 | break; |
| 4939 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4940 | ((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] | 4941 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4942 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4943 | return -1; |
| 4944 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4945 | break; |
| 4946 | default: |
| 4947 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4948 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4949 | |
| 4950 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4951 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4952 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4953 | /* YANG */ |
| 4954 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4955 | ext_prefix = (char *)(*ext)->def; |
| 4956 | tmp = strchr(ext_prefix, ':'); |
| 4957 | if (!tmp) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4958 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4959 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4960 | } |
| 4961 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4962 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4963 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4964 | 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] | 4965 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4966 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4967 | return EXIT_FAILURE; |
| 4968 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4969 | ctx = mod->ctx; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4970 | |
| 4971 | /* find the extension definition */ |
| 4972 | e = NULL; |
| 4973 | for (i = 0; i < mod->extensions_size; i++) { |
| 4974 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4975 | e = &mod->extensions[i]; |
| 4976 | break; |
| 4977 | } |
| 4978 | } |
| 4979 | /* try submodules */ |
| 4980 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4981 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4982 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4983 | e = &mod->inc[j].submodule->extensions[i]; |
| 4984 | break; |
| 4985 | } |
| 4986 | } |
| 4987 | } |
| 4988 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4989 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4990 | return EXIT_FAILURE; |
| 4991 | } |
| 4992 | |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 4993 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
| 4994 | (*ext)->def = NULL; |
| 4995 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4996 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4997 | |
| 4998 | if (e->plugin && e->plugin->check_position) { |
| 4999 | /* common part - we have plugin with position checking function, use it first */ |
| 5000 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 5001 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5002 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5003 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5004 | } |
| 5005 | } |
| 5006 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5007 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5008 | (*ext)->def = e; |
| 5009 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 5010 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5011 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5012 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5013 | if (e->argument && !(*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5014 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5015 | goto error; |
| 5016 | } |
| 5017 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5018 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 5019 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 5020 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5021 | } |
| 5022 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 5023 | (*ext)->module = info->mod; |
| 5024 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5025 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5026 | /* extension type-specific part */ |
| 5027 | if (e->plugin) { |
| 5028 | etype = e->plugin->type; |
| 5029 | } else { |
| 5030 | /* default type */ |
| 5031 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5032 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5033 | switch (etype) { |
| 5034 | case LYEXT_FLAG: |
| 5035 | /* nothing change */ |
| 5036 | break; |
| 5037 | case LYEXT_COMPLEX: |
Radek Krejci | b728c9f | 2018-12-06 09:42:17 +0100 | [diff] [blame] | 5038 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5039 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error); |
Radek Krejci | b728c9f | 2018-12-06 09:42:17 +0100 | [diff] [blame] | 5040 | memset((char *)tmp_ext + offsetof(struct lys_ext_instance_complex, content), 0, |
| 5041 | ((struct lyext_plugin_complex*)e->plugin)->instance_size - offsetof(struct lys_ext_instance_complex, content)); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 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 | b4b31f6 | 2018-11-23 11:49:20 +0100 | [diff] [blame] | 6195 | ly_vlog_str(ctx, LY_VLOG_PREV, 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 | c40a209 | 2018-11-20 09:32:23 +0100 | [diff] [blame] | 6976 | if ((rc == EXIT_FAILURE) && !parent_type && (stype->base == LY_TYPE_LEAFREF)) { |
Michal Vasko | 7d89492 | 2018-11-12 11:55:55 +0100 | [diff] [blame] | 6977 | for (par_grp = (struct lys_node *)stype->parent; |
| 6978 | par_grp && (par_grp->nodetype != LYS_GROUPING); |
| 6979 | par_grp = lys_parent(par_grp)); |
| 6980 | if (par_grp) { |
| 6981 | /* checking default value in a grouping finished with forward reference means we cannot check the value */ |
| 6982 | rc = EXIT_SUCCESS; |
| 6983 | } |
| 6984 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6985 | break; |
| 6986 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6987 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6988 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6989 | choic = item; |
| 6990 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6991 | if (!choic->dflt) { |
| 6992 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6993 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6994 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6995 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6996 | } else { |
| 6997 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6998 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6999 | break; |
| 7000 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 7001 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7002 | break; |
| 7003 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7004 | unique_info = (struct unres_list_uniq *)item; |
| 7005 | 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] | 7006 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7007 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 7008 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7009 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7010 | case UNRES_XPATH: |
| 7011 | node = (struct lys_node *)item; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 7012 | rc = check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7013 | break; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7014 | case UNRES_MOD_IMPLEMENT: |
| 7015 | rc = lys_make_implemented_r(mod, unres); |
| 7016 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7017 | case UNRES_EXT: |
| 7018 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7019 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 7020 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7021 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7022 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7023 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7024 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7025 | if (eplugin) { |
| 7026 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7027 | u = malloc(sizeof *u); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7028 | LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7029 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 7030 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 7031 | /* something really bad happend since the extension finalization is not actually |
| 7032 | * being resolved while adding into unres, so something more serious with the unres |
| 7033 | * list itself must happened */ |
| 7034 | return -1; |
| 7035 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7036 | } |
| 7037 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7038 | } |
| 7039 | if (!rc || rc == -1) { |
| 7040 | /* cleanup on success or fatal error */ |
| 7041 | if (ext_data->datatype == LYS_IN_YIN) { |
| 7042 | /* YIN */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7043 | lyxml_free(ctx, ext_data->data.yin); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7044 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 7045 | /* YANG */ |
| 7046 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7047 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7048 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7049 | } |
| 7050 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7051 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7052 | u = (uint8_t *)str_snode; |
| 7053 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 7054 | free(u); |
| 7055 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7056 | eplugin = ext->def->plugin; |
| 7057 | |
| 7058 | /* inherit */ |
| 7059 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 7060 | root = (struct lys_node *)ext->parent; |
| 7061 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 7062 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 7063 | /* first, check if the node already contain instance of the same extension, |
| 7064 | * in such a case we won't inherit. In case the node was actually defined as |
| 7065 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 7066 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 7067 | goto inherit_dfs_sibling; |
| 7068 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 7069 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 7070 | goto inherit_dfs_sibling; |
| 7071 | } |
| 7072 | |
| 7073 | if (eplugin->check_inherit) { |
| 7074 | /* we have a callback to check the inheritance, use it */ |
| 7075 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 7076 | case 0: |
| 7077 | /* yes - continue with the inheriting code */ |
| 7078 | break; |
| 7079 | case 1: |
| 7080 | /* no - continue with the node's sibling */ |
| 7081 | goto inherit_dfs_sibling; |
| 7082 | case 2: |
| 7083 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 7084 | goto inherit_dfs_child; |
| 7085 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7086 | 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] | 7087 | ext->def->module->name, ext->def->name, rc); |
| 7088 | } |
| 7089 | } |
| 7090 | |
| 7091 | /* inherit the extension */ |
| 7092 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7093 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7094 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7095 | 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] | 7096 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 7097 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 7098 | |
| 7099 | node->ext = extlist; |
| 7100 | node->ext_size++; |
| 7101 | |
| 7102 | inherit_dfs_child: |
| 7103 | /* modification of - select element for the next run - children first */ |
| 7104 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 7105 | next = NULL; |
| 7106 | } else { |
| 7107 | next = node->child; |
| 7108 | } |
| 7109 | if (!next) { |
| 7110 | inherit_dfs_sibling: |
| 7111 | /* no children, try siblings */ |
| 7112 | next = node->next; |
| 7113 | } |
| 7114 | while (!next) { |
| 7115 | /* go to the parent */ |
| 7116 | node = lys_parent(node); |
| 7117 | |
| 7118 | /* we are done if we are back in the root (the starter's parent */ |
| 7119 | if (node == root) { |
| 7120 | break; |
| 7121 | } |
| 7122 | |
| 7123 | /* parent is already processed, go to its sibling */ |
| 7124 | next = node->next; |
| 7125 | } |
| 7126 | } |
| 7127 | } |
| 7128 | } |
| 7129 | |
| 7130 | /* final check */ |
| 7131 | if (eplugin->check_result) { |
| 7132 | if ((*eplugin->check_result)(ext)) { |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7133 | LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed."); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7134 | return -1; |
| 7135 | } |
| 7136 | } |
| 7137 | |
| 7138 | rc = 0; |
| 7139 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7140 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7141 | LOGINT(ctx); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7142 | break; |
| 7143 | } |
| 7144 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7145 | if (has_str && !rc) { |
| 7146 | /* the string is no more needed in case of success. |
| 7147 | * 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] | 7148 | lydict_remove(ctx, str_snode); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7149 | } |
| 7150 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7151 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7152 | } |
| 7153 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7154 | /* logs directly */ |
| 7155 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7156 | 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] | 7157 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7158 | struct lyxml_elem *xml; |
| 7159 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7160 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7161 | const char *name = NULL; |
| 7162 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7163 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7164 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7165 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7166 | 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] | 7167 | break; |
| 7168 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7169 | 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] | 7170 | break; |
| 7171 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7172 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 7173 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7174 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 7175 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7176 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7177 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7178 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7179 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7180 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7181 | } else { |
| 7182 | LY_TREE_FOR(xml->attr, attr) { |
| 7183 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7184 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7185 | break; |
| 7186 | } |
| 7187 | } |
| 7188 | assert(attr); |
| 7189 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7190 | 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] | 7191 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7192 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7193 | iff_data = str_node; |
| 7194 | 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] | 7195 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7196 | case UNRES_FEATURE: |
| 7197 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 7198 | ((struct lys_feature *)item)->name); |
| 7199 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7200 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7201 | 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] | 7202 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7203 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7204 | case UNRES_TYPE_DFLT: |
Michal Vasko | ba835cd | 2018-02-23 09:25:48 +0100 | [diff] [blame] | 7205 | if (*(char **)str_node) { |
| 7206 | 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] | 7207 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 7208 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 7209 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7210 | break; |
| 7211 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7212 | 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] | 7213 | break; |
| 7214 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7215 | 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] | 7216 | break; |
| 7217 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7218 | 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] | 7219 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7220 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7221 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 7222 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7223 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7224 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 7225 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 7226 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7227 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7228 | case UNRES_EXT: |
| 7229 | extinfo = (struct unres_ext *)str_node; |
| 7230 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 7231 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 7232 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7233 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7234 | LOGINT(NULL); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7235 | break; |
| 7236 | } |
| 7237 | } |
| 7238 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7239 | static int |
| 7240 | resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref, |
| 7241 | int print_all_errors, uint32_t *resolved) |
| 7242 | { |
| 7243 | uint32_t i, unres_count, res_count; |
| 7244 | int ret = 0, rc; |
| 7245 | struct ly_err_item *prev_eitem; |
| 7246 | enum int_log_opts prev_ilo; |
| 7247 | LY_ERR prev_ly_errno; |
| 7248 | |
| 7249 | /* if there can be no forward references, every failure is final, so we can print it directly */ |
| 7250 | if (forward_ref) { |
| 7251 | prev_ly_errno = ly_errno; |
| 7252 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 7253 | } |
| 7254 | |
| 7255 | do { |
| 7256 | unres_count = 0; |
| 7257 | res_count = 0; |
| 7258 | |
| 7259 | for (i = 0; i < unres->count; ++i) { |
| 7260 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7261 | * if-features are resolved here to make sure that we will have all if-features for |
| 7262 | * later check of feature circular dependency */ |
| 7263 | if (unres->type[i] & types) { |
| 7264 | ++unres_count; |
| 7265 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7266 | if (unres->type[i] == UNRES_EXT_FINALIZE) { |
| 7267 | /* to avoid double free */ |
| 7268 | unres->type[i] = UNRES_RESOLVED; |
| 7269 | } |
| 7270 | if (!rc || (unres->type[i] == UNRES_XPATH)) { |
| 7271 | /* invalid XPath can never cause an error, only a warning */ |
| 7272 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 7273 | /* free the allocated structure */ |
| 7274 | free(unres->item[i]); |
| 7275 | } |
| 7276 | |
| 7277 | unres->type[i] = UNRES_RESOLVED; |
| 7278 | ++(*resolved); |
| 7279 | ++res_count; |
| 7280 | } else if ((rc == EXIT_FAILURE) && forward_ref) { |
| 7281 | /* forward reference, erase errors */ |
| 7282 | ly_err_free_next(ctx, prev_eitem); |
| 7283 | } else if (print_all_errors) { |
| 7284 | /* just so that we quit the loop */ |
| 7285 | ++res_count; |
| 7286 | ret = -1; |
| 7287 | } else { |
Michal Vasko | ffdf4ce | 2018-08-17 10:31:49 +0200 | [diff] [blame] | 7288 | if (forward_ref) { |
| 7289 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 7290 | } |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7291 | return -1; |
| 7292 | } |
| 7293 | } |
| 7294 | } |
| 7295 | } while (res_count && (res_count < unres_count)); |
| 7296 | |
| 7297 | if (res_count < unres_count) { |
| 7298 | assert(forward_ref); |
| 7299 | /* just print the errors (but we must free the ones we have and get them again :-/ ) */ |
| 7300 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7301 | |
| 7302 | for (i = 0; i < unres->count; ++i) { |
| 7303 | if (unres->type[i] & types) { |
| 7304 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7305 | } |
| 7306 | } |
| 7307 | return -1; |
| 7308 | } |
| 7309 | |
| 7310 | if (forward_ref) { |
| 7311 | /* restore log */ |
| 7312 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7313 | ly_errno = prev_ly_errno; |
| 7314 | } |
| 7315 | |
| 7316 | return ret; |
| 7317 | } |
| 7318 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7319 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7320 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7321 | * |
| 7322 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7323 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7324 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7325 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7326 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7327 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7328 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7329 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7330 | uint32_t resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7331 | |
| 7332 | assert(unres); |
| 7333 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7334 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 7335 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7336 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7337 | * if-features are resolved here to make sure that we will have all if-features for |
| 7338 | * later check of feature circular dependency */ |
| 7339 | if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF |
| 7340 | | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT, |
| 7341 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7342 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7343 | } |
| 7344 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7345 | /* another batch of resolved items */ |
| 7346 | if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT |
| 7347 | | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) { |
| 7348 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7349 | } |
| 7350 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7351 | /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 7352 | if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) { |
| 7353 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7354 | } |
| 7355 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7356 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7357 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7358 | return EXIT_SUCCESS; |
| 7359 | } |
| 7360 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7361 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7362 | * @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] | 7363 | * |
| 7364 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7365 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7366 | * @param[in] item Item to resolve. Type determined by \p type. |
| 7367 | * @param[in] type Type of the unresolved item. |
| 7368 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7369 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7370 | * @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] | 7371 | */ |
| 7372 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7373 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 7374 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7375 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7376 | int rc; |
| 7377 | const char *dictstr; |
| 7378 | |
| 7379 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 7380 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 7381 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 7382 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7383 | lydict_remove(mod->ctx, dictstr); |
| 7384 | } |
| 7385 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7386 | } |
| 7387 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7388 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7389 | * @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] | 7390 | * |
| 7391 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7392 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7393 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7394 | * @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] | 7395 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7396 | * |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7397 | * @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] | 7398 | */ |
| 7399 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7400 | 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] | 7401 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7402 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7403 | int rc; |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7404 | uint32_t u; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7405 | enum int_log_opts prev_ilo; |
| 7406 | struct ly_err_item *prev_eitem; |
| 7407 | LY_ERR prev_ly_errno; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7408 | struct lyxml_elem *yin; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7409 | struct ly_ctx *ctx = mod->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7410 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7411 | assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) |
| 7412 | && (type != UNRES_WHEN) && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7413 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7414 | /* check for duplicities in unres */ |
| 7415 | for (u = 0; u < unres->count; u++) { |
| 7416 | if (unres->type[u] == type && unres->item[u] == item && |
| 7417 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7418 | /* duplication can happen when the node contains multiple statements of the same type to check, |
| 7419 | * this can happen for example when refinement is being applied, so we just postpone the processing |
| 7420 | * and do not duplicate the information */ |
| 7421 | return EXIT_FAILURE; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7422 | } |
| 7423 | } |
| 7424 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7425 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7426 | /* 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] | 7427 | * xpath is not tried because it would hide some potential warnings, |
| 7428 | * implementing module must be deferred because some other nodes can be added that will need to be traversed |
| 7429 | * and their targets made implemented */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7430 | rc = EXIT_FAILURE; |
| 7431 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7432 | prev_ly_errno = ly_errno; |
| 7433 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7434 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7435 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7436 | if (rc != EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7437 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0); |
| 7438 | if (rc != -1) { |
| 7439 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7440 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7441 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7442 | if (type == UNRES_LIST_UNIQ) { |
| 7443 | /* free the allocated structure */ |
| 7444 | free(item); |
| 7445 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7446 | /* free the allocated resources */ |
| 7447 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7448 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7449 | return rc; |
| 7450 | } else { |
| 7451 | /* erase info about validation errors */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7452 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7453 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7454 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7455 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7456 | print_unres_schema_item_fail(item, type, snode); |
| 7457 | |
| 7458 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7459 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7460 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7461 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7462 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7463 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7464 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7465 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7466 | } |
| 7467 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7468 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7469 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7470 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7471 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7472 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7473 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7474 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7475 | 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] | 7476 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7477 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7478 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7479 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7480 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7481 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7482 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7483 | } |
| 7484 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7485 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7486 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7487 | * |
| 7488 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7489 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7490 | * @param[in] item Old item to be resolved. |
| 7491 | * @param[in] type Type of the old unresolved item. |
| 7492 | * @param[in] new_item New item to use in the duplicate. |
| 7493 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7494 | * @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] | 7495 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7496 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7497 | 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] | 7498 | { |
| 7499 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7500 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7501 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7502 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7503 | 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] | 7504 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7505 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7506 | if (type == UNRES_LIST_UNIQ) { |
| 7507 | aux_uniq.list = item; |
| 7508 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7509 | item = &aux_uniq; |
| 7510 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7511 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7512 | |
| 7513 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7514 | if (type == UNRES_LIST_UNIQ) { |
| 7515 | free(new_item); |
| 7516 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7517 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7518 | } |
| 7519 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7520 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7521 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7522 | 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] | 7523 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7524 | return -1; |
| 7525 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7526 | } else if (type == UNRES_IFFEAT) { |
| 7527 | /* duplicate unres_iffeature_data */ |
| 7528 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7529 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7530 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7531 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7532 | 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] | 7533 | LOGINT(mod->ctx); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7534 | return -1; |
| 7535 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7536 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7537 | 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] | 7538 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7539 | return -1; |
| 7540 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7541 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7542 | |
| 7543 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7544 | } |
| 7545 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7546 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7547 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7548 | 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] | 7549 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7550 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7551 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7552 | |
Michal Vasko | c643f71 | 2018-09-14 08:26:13 +0200 | [diff] [blame] | 7553 | if (!unres->count) { |
| 7554 | return -1; |
| 7555 | } |
| 7556 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7557 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7558 | i = start_on_backwards; |
| 7559 | } else { |
| 7560 | i = unres->count - 1; |
| 7561 | } |
| 7562 | for (; i > -1; i--) { |
| 7563 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7564 | continue; |
| 7565 | } |
| 7566 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7567 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7568 | break; |
| 7569 | } |
| 7570 | } else { |
| 7571 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7572 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7573 | 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] | 7574 | break; |
| 7575 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7576 | } |
| 7577 | } |
| 7578 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7579 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7580 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7581 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7582 | static void |
| 7583 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7584 | { |
| 7585 | struct lyxml_elem *yin; |
| 7586 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7587 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7588 | |
| 7589 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7590 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7591 | case UNRES_TYPE_DER: |
| 7592 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7593 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7594 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7595 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7596 | lydict_remove(ctx, yang->name); |
| 7597 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7598 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7599 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7600 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7601 | } else { |
| 7602 | lyxml_free(ctx, yin); |
| 7603 | } |
| 7604 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7605 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7606 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7607 | lydict_remove(ctx, iff_data->fname); |
| 7608 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7609 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7610 | case UNRES_IDENT: |
| 7611 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7612 | case UNRES_CHOICE_DFLT: |
| 7613 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7614 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7615 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7616 | case UNRES_LIST_UNIQ: |
| 7617 | free(unres->item[i]); |
| 7618 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7619 | case UNRES_EXT: |
| 7620 | free(unres->str_snode[i]); |
| 7621 | break; |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 7622 | case UNRES_EXT_FINALIZE: |
| 7623 | free(unres->str_snode[i]); |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7624 | default: |
| 7625 | break; |
| 7626 | } |
| 7627 | unres->type[i] = UNRES_RESOLVED; |
| 7628 | } |
| 7629 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7630 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7631 | 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] | 7632 | { |
| 7633 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7634 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7635 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7636 | if (!unres || !(*unres)) { |
| 7637 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7638 | } |
| 7639 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7640 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7641 | |
| 7642 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7643 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7644 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7645 | unresolved++; |
| 7646 | } |
| 7647 | continue; |
| 7648 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7649 | |
| 7650 | /* free heap memory for the specific item */ |
| 7651 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7652 | } |
| 7653 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7654 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7655 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7656 | free((*unres)->item); |
| 7657 | free((*unres)->type); |
| 7658 | free((*unres)->str_snode); |
| 7659 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7660 | free((*unres)); |
| 7661 | (*unres) = NULL; |
| 7662 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7663 | } |
| 7664 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7665 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7666 | * 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] | 7667 | static int |
| 7668 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7669 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7670 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7671 | enum int_log_opts prev_ilo; |
Michal Vasko | 2e80aff | 2018-12-07 08:41:07 +0100 | [diff] [blame] | 7672 | char *buf, *tmp; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7673 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7674 | if (!json_instid || !json_instid[0]) { |
| 7675 | /* no/empty value */ |
| 7676 | return 0; |
| 7677 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7678 | |
| 7679 | for (op_node = lys_parent(sleaf); |
| 7680 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7681 | op_node = lys_parent(op_node)); |
| 7682 | |
| 7683 | if (op_node && lys_parent(op_node)) { |
| 7684 | /* nested operation - any absolute path is external */ |
| 7685 | return 1; |
| 7686 | } |
| 7687 | |
| 7688 | /* get the first node from the instid */ |
Michal Vasko | 2e80aff | 2018-12-07 08:41:07 +0100 | [diff] [blame] | 7689 | tmp = strchr(json_instid + 1, '/'); |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7690 | buf = strndup(json_instid, tmp ? (size_t)(tmp - json_instid) : strlen(json_instid)); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7691 | if (!buf) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7692 | /* so that we do not have to bother with logging, say it is not external */ |
| 7693 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7694 | } |
| 7695 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7696 | /* find the first schema node, do not log */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7697 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7698 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7699 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7700 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7701 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7702 | if (!first_node) { |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7703 | /* unknown path, say it is external */ |
| 7704 | return 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7705 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7706 | |
| 7707 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7708 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7709 | if (op_node) { |
| 7710 | if (op_node != first_node) { |
| 7711 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7712 | return 1; |
| 7713 | } |
| 7714 | } else { |
| 7715 | if (lys_node_module(sleaf) != lys_node_module(first_node)) { |
| 7716 | /* modules differ */ |
| 7717 | return 1; |
| 7718 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7719 | } |
| 7720 | |
Michal Vasko | 299aef1 | 2018-12-18 13:13:55 +0100 | [diff] [blame] | 7721 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7722 | } |
| 7723 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7724 | /** |
| 7725 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7726 | * |
| 7727 | * @param[in] data Data node where the path is used |
| 7728 | * @param[in] path Instance-identifier node value. |
| 7729 | * @param[in,out] ret Resolved instance or NULL. |
| 7730 | * |
| 7731 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7732 | */ |
| 7733 | static int |
| 7734 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7735 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7736 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7737 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7738 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7739 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7740 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7741 | char *str; |
| 7742 | int mod_len, name_len, has_predicate; |
| 7743 | struct unres_data node_match; |
| 7744 | |
| 7745 | memset(&node_match, 0, sizeof node_match); |
| 7746 | *ret = NULL; |
| 7747 | |
| 7748 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7749 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7750 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7751 | if (root->prev) { |
| 7752 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7753 | } |
| 7754 | |
| 7755 | /* search for the instance node */ |
| 7756 | while (path[i]) { |
| 7757 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7758 | if (j <= 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7759 | 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] | 7760 | goto error; |
| 7761 | } |
| 7762 | i += j; |
| 7763 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7764 | if (model) { |
| 7765 | str = strndup(model, mod_len); |
| 7766 | if (!str) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7767 | LOGMEM(ctx); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7768 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7769 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7770 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7771 | if (ctx->data_clb) { |
| 7772 | if (!mod) { |
| 7773 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7774 | } else if (!mod->implemented) { |
| 7775 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7776 | } |
| 7777 | } |
| 7778 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7779 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7780 | if (!mod || !mod->implemented || mod->disabled) { |
| 7781 | break; |
| 7782 | } |
| 7783 | } else if (!prev_mod) { |
| 7784 | /* first iteration and we are missing module name */ |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 7785 | LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name); |
| 7786 | 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] | 7787 | goto error; |
| 7788 | } else { |
| 7789 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7790 | } |
| 7791 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7792 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7793 | /* no instance exists */ |
| 7794 | break; |
| 7795 | } |
| 7796 | |
| 7797 | if (has_predicate) { |
| 7798 | /* 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] | 7799 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7800 | /* index of the current node (for lists with position predicates) */ |
| 7801 | cur_idx = 1; |
| 7802 | while (j < (signed)node_match.count) { |
| 7803 | node = node_match.node[j]; |
| 7804 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7805 | if (parsed < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7806 | LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7807 | goto error; |
| 7808 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7809 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7810 | if (!node) { |
| 7811 | /* current node does not satisfy the predicate */ |
| 7812 | unres_data_del(&node_match, j); |
| 7813 | } else { |
| 7814 | ++j; |
| 7815 | } |
| 7816 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7817 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7818 | |
| 7819 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7820 | } else if (node_match.count) { |
| 7821 | /* check that we are not addressing lists */ |
| 7822 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7823 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7824 | unres_data_del(&node_match, j--); |
| 7825 | } |
| 7826 | } |
| 7827 | if (!node_match.count) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7828 | 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] | 7829 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7830 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7831 | |
| 7832 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7833 | } |
| 7834 | |
| 7835 | if (!node_match.count) { |
| 7836 | /* no instance exists */ |
| 7837 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7838 | LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7839 | return EXIT_FAILURE; |
| 7840 | } |
| 7841 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7842 | return EXIT_SUCCESS; |
| 7843 | } else if (node_match.count > 1) { |
| 7844 | /* instance identifier must resolve to a single node */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7845 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7846 | goto error; |
| 7847 | } else { |
| 7848 | /* we have required result, remember it and cleanup */ |
| 7849 | *ret = node_match.node[0]; |
| 7850 | free(node_match.node); |
| 7851 | return EXIT_SUCCESS; |
| 7852 | } |
| 7853 | |
| 7854 | error: |
| 7855 | /* cleanup */ |
| 7856 | free(node_match.node); |
| 7857 | return -1; |
| 7858 | } |
| 7859 | |
| 7860 | static int |
| 7861 | 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] | 7862 | { |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7863 | struct lyxp_set xp_set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7864 | uint32_t i; |
| 7865 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7866 | memset(&xp_set, 0, sizeof xp_set); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7867 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7868 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7869 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7870 | 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] | 7871 | return -1; |
| 7872 | } |
| 7873 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7874 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 7875 | for (i = 0; i < xp_set.used; ++i) { |
| 7876 | if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7877 | continue; |
| 7878 | } |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7879 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7880 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7881 | * so we can simply compare just the values */ |
| 7882 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) { |
| 7883 | /* we have the match */ |
| 7884 | *ret = xp_set.val.nodes[i].node; |
| 7885 | break; |
| 7886 | } |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7887 | } |
| 7888 | } |
| 7889 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7890 | 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] | 7891 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7892 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7893 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7894 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7895 | 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] | 7896 | return EXIT_FAILURE; |
| 7897 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7898 | 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] | 7899 | } |
| 7900 | } |
| 7901 | |
| 7902 | return EXIT_SUCCESS; |
| 7903 | } |
| 7904 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7905 | /* 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] | 7906 | int |
| 7907 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7908 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7909 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7910 | struct ly_ctx *ctx = leaf->schema->module->ctx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7911 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7912 | struct lyd_node *ret; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7913 | enum int_log_opts prev_ilo; |
| 7914 | int found, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7915 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7916 | |
| 7917 | assert(type->base == LY_TYPE_UNION); |
| 7918 | |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7919 | 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] | 7920 | /* either NULL or instid previously converted to JSON */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7921 | json_val = lydict_insert(ctx, leaf->value.string, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7922 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7923 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7924 | if (store) { |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame] | 7925 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type, |
| 7926 | NULL, NULL, NULL); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7927 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7928 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7929 | |
| 7930 | /* 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] | 7931 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7932 | |
| 7933 | t = NULL; |
| 7934 | found = 0; |
| 7935 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7936 | found = 0; |
| 7937 | |
| 7938 | switch (t->base) { |
| 7939 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7940 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7941 | req_inst = -1; |
| 7942 | } else { |
| 7943 | req_inst = t->info.lref.req; |
| 7944 | } |
| 7945 | |
| 7946 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7947 | if (store) { |
| 7948 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7949 | /* valid resolved */ |
| 7950 | leaf->value.leafref = ret; |
| 7951 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7952 | } else { |
| 7953 | /* valid unresolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7954 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7955 | 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] | 7956 | return -1; |
| 7957 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7958 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7959 | } |
| 7960 | } |
| 7961 | |
| 7962 | success = 1; |
| 7963 | } |
| 7964 | break; |
| 7965 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7966 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7967 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7968 | req_inst = -1; |
| 7969 | } else { |
| 7970 | req_inst = t->info.inst.req; |
| 7971 | } |
| 7972 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7973 | 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] | 7974 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7975 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7976 | /* valid resolved */ |
| 7977 | leaf->value.instance = ret; |
| 7978 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7979 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7980 | if (json_val) { |
| 7981 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7982 | leaf->value_str = json_val; |
| 7983 | json_val = NULL; |
| 7984 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7985 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7986 | /* valid unresolved */ |
| 7987 | if (json_val) { |
| 7988 | /* put the JSON val back */ |
| 7989 | leaf->value.string = json_val; |
| 7990 | json_val = NULL; |
| 7991 | } else { |
| 7992 | leaf->value.instance = NULL; |
| 7993 | } |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7994 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7995 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7996 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7997 | } |
| 7998 | |
| 7999 | success = 1; |
| 8000 | } |
| 8001 | break; |
| 8002 | default: |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 8003 | 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] | 8004 | success = 1; |
| 8005 | } |
| 8006 | break; |
| 8007 | } |
| 8008 | |
| 8009 | if (success) { |
| 8010 | break; |
| 8011 | } |
| 8012 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8013 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8014 | if (store) { |
Michal Vasko | 7ba3744 | 2018-11-06 08:59:27 +0100 | [diff] [blame] | 8015 | 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] | 8016 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8017 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8018 | } |
| 8019 | |
| 8020 | /* turn logging back on */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8021 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8022 | |
| 8023 | if (json_val) { |
| 8024 | if (!success) { |
| 8025 | /* put the value back for now */ |
| 8026 | assert(leaf->value_type == LY_TYPE_UNION); |
| 8027 | leaf->value.string = json_val; |
| 8028 | } else { |
| 8029 | /* value was ultimately useless, but we could not have known */ |
| 8030 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 8031 | } |
| 8032 | } |
| 8033 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8034 | if (success) { |
| 8035 | if (resolved_type) { |
| 8036 | *resolved_type = t; |
| 8037 | } |
| 8038 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8039 | /* not found and it is required */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8040 | 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] | 8041 | return EXIT_FAILURE; |
| 8042 | } |
| 8043 | |
| 8044 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8045 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8046 | } |
| 8047 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8048 | /** |
| 8049 | * @brief Resolve a single unres data item. Logs directly. |
| 8050 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8051 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8052 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8053 | * @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] | 8054 | * |
| 8055 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 8056 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 8057 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8058 | 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] | 8059 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8060 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8061 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8062 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8063 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8064 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8065 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8066 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8067 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8068 | switch (type) { |
| 8069 | case UNRES_LEAFREF: |
| 8070 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8071 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8072 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 8073 | req_inst = -1; |
| 8074 | } else { |
| 8075 | req_inst = sleaf->type.info.lref.req; |
| 8076 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8077 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 8078 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 8079 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8080 | /* valid resolved */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8081 | if (leaf->value_type == LY_TYPE_BITS) { |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8082 | free(leaf->value.bit); |
| 8083 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8084 | leaf->value.leafref = ret; |
| 8085 | leaf->value_type = LY_TYPE_LEAFREF; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8086 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8087 | } else { |
| 8088 | /* valid unresolved */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8089 | if (!(leaf->value_flags & LY_VALUE_UNRES)) { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 8090 | 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] | 8091 | return -1; |
| 8092 | } |
| 8093 | } |
| 8094 | } |
| 8095 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 8096 | } else { |
| 8097 | return rc; |
| 8098 | } |
| 8099 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8100 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8101 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8102 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8103 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 8104 | if (ext_dep == -1) { |
| 8105 | return -1; |
| 8106 | } |
| 8107 | |
| 8108 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 8109 | req_inst = -1; |
| 8110 | } else { |
| 8111 | req_inst = sleaf->type.info.inst.req; |
| 8112 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8113 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 8114 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8115 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8116 | /* valid resolved */ |
| 8117 | leaf->value.instance = ret; |
| 8118 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8119 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8120 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8121 | /* valid unresolved */ |
| 8122 | leaf->value.instance = NULL; |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8123 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8124 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8125 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8126 | } else { |
| 8127 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8128 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8129 | break; |
| 8130 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8131 | case UNRES_UNION: |
| 8132 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8133 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8134 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8135 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8136 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8137 | return rc; |
| 8138 | } |
| 8139 | break; |
| 8140 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8141 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8142 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 8143 | return rc; |
| 8144 | } |
| 8145 | break; |
| 8146 | |
| 8147 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8148 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8149 | return rc; |
| 8150 | } |
| 8151 | break; |
| 8152 | |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 8153 | case UNRES_UNIQ_LEAVES: |
| 8154 | if (lyv_data_unique(node)) { |
| 8155 | return -1; |
| 8156 | } |
| 8157 | break; |
| 8158 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8159 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8160 | LOGINT(NULL); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8161 | return -1; |
| 8162 | } |
| 8163 | |
| 8164 | return EXIT_SUCCESS; |
| 8165 | } |
| 8166 | |
| 8167 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8168 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8169 | * |
| 8170 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8171 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8172 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8173 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8174 | */ |
| 8175 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8176 | 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] | 8177 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8178 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 8179 | 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] | 8180 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_UNIQ_LEAVES)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8181 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8182 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8183 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8184 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8185 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8186 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8187 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8188 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8189 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8190 | return 0; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8191 | } |
| 8192 | |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8193 | static void |
| 8194 | resolve_unres_data_autodel_diff(struct unres_data *unres, uint32_t unres_i) |
| 8195 | { |
| 8196 | struct lyd_node *next, *child, *parent; |
| 8197 | uint32_t i; |
| 8198 | |
| 8199 | for (i = 0; i < unres->diff_idx; ++i) { |
| 8200 | if (unres->diff->type[i] == LYD_DIFF_DELETED) { |
| 8201 | /* only leaf(-list) default could be removed and there is nothing to be checked in that case */ |
| 8202 | continue; |
| 8203 | } |
| 8204 | |
| 8205 | if (unres->diff->second[i] == unres->node[unres_i]) { |
| 8206 | /* 1) default value was supposed to be created, but is disabled by when |
| 8207 | * -> remove it from diff altogether |
| 8208 | */ |
| 8209 | unres_data_diff_rem(unres, i); |
| 8210 | /* if diff type is CREATED, the value was just a pointer, it can be freed normally (unlike in 4) */ |
| 8211 | return; |
| 8212 | } else { |
| 8213 | parent = unres->diff->second[i]->parent; |
| 8214 | while (parent && (parent != unres->node[unres_i])) { |
| 8215 | parent = parent->parent; |
| 8216 | } |
| 8217 | if (parent) { |
| 8218 | /* 2) default value was supposed to be created but is disabled by when in some parent |
| 8219 | * -> remove this default subtree and add the rest into diff as deleted instead in 4) |
| 8220 | */ |
| 8221 | unres_data_diff_rem(unres, i); |
| 8222 | break; |
| 8223 | } |
| 8224 | |
| 8225 | LY_TREE_DFS_BEGIN(unres->diff->second[i]->parent, next, child) { |
| 8226 | if (child == unres->node[unres_i]) { |
| 8227 | /* 3) some default child of a default value was supposed to be created but has false when |
| 8228 | * -> the subtree will be freed later and automatically disconnected from the diff parent node |
| 8229 | */ |
| 8230 | return; |
| 8231 | } |
| 8232 | |
| 8233 | LY_TREE_DFS_END(unres->diff->second[i]->parent, next, child); |
| 8234 | } |
| 8235 | } |
| 8236 | } |
| 8237 | |
| 8238 | /* 4) it does not overlap with created default values in any way |
| 8239 | * -> just add it into diff as deleted |
| 8240 | */ |
| 8241 | unres_data_diff_new(unres, unres->node[unres_i], unres->node[unres_i]->parent, 0); |
| 8242 | lyd_unlink(unres->node[unres_i]); |
| 8243 | |
| 8244 | /* should not be freed anymore */ |
| 8245 | unres->node[unres_i] = NULL; |
| 8246 | } |
| 8247 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8248 | /** |
| 8249 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 8250 | * |
Michal Vasko | 660582a | 2018-03-19 10:10:08 +0100 | [diff] [blame] | 8251 | * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected, |
| 8252 | * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit |
| 8253 | * non-presence containers may need to be deleted). |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8254 | * |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 8255 | * 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] | 8256 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8257 | * @param[in] ctx Context used. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8258 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8259 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 8260 | * @param[in] options Data options as described above. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8261 | * |
| 8262 | * @return EXIT_SUCCESS on success, -1 on error. |
| 8263 | */ |
| 8264 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8265 | resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8266 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8267 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8268 | uint8_t prev_when_status; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8269 | int rc, progress, ignore_fail; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8270 | enum int_log_opts prev_ilo; |
| 8271 | struct ly_err_item *prev_eitem; |
mohitarora24 | 89837dc | 2018-05-01 15:09:36 +0530 | [diff] [blame] | 8272 | LY_ERR prev_ly_errno = ly_errno; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8273 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8274 | struct lys_when *when; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8275 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8276 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8277 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8278 | |
| 8279 | if (!unres->count) { |
| 8280 | return EXIT_SUCCESS; |
| 8281 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8282 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8283 | 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] | 8284 | ignore_fail = 1; |
| 8285 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 8286 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8287 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8288 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8289 | } |
| 8290 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8291 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8292 | if (!ignore_fail) { |
| 8293 | /* remember logging state only if errors are generated and valid */ |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8294 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 8295 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8296 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8297 | /* |
| 8298 | * when-stmt first |
| 8299 | */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8300 | first = 1; |
| 8301 | stmt_count = 0; |
| 8302 | resolved = 0; |
| 8303 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8304 | do { |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8305 | if (!ignore_fail) { |
| 8306 | ly_err_free_next(ctx, prev_eitem); |
| 8307 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8308 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 8309 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8310 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8311 | continue; |
| 8312 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8313 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8314 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8315 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 8319 | for (parent = unres->node[i]->parent; |
| 8320 | parent && LYD_WHEN_DONE(parent->when_status); |
| 8321 | parent = parent->parent) { |
| 8322 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 8323 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8324 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8325 | */ |
| 8326 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 8327 | unres->type[i] = UNRES_RESOLVED; |
| 8328 | resolved++; |
| 8329 | break; |
| 8330 | } |
| 8331 | } |
| 8332 | if (parent) { |
| 8333 | continue; |
| 8334 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8335 | |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8336 | prev_when_status = unres->node[i]->when_status; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8337 | 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] | 8338 | if (!rc) { |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8339 | /* finish with error/delete the node only if when was changed from true to false, an external |
| 8340 | * dependency was not required, or it was not provided (the flag would not be passed down otherwise, |
| 8341 | * checked in upper functions) */ |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8342 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 8343 | && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) { |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8344 | if ((!(prev_when_status & LYD_WHEN_TRUE) || !(options & LYD_OPT_WHENAUTODEL)) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8345 | /* false when condition */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8346 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8347 | } /* follows else */ |
| 8348 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8349 | /* auto-delete */ |
Michal Vasko | 8af8220 | 2018-11-14 11:21:01 +0100 | [diff] [blame] | 8350 | LOGVRB("Auto-deleting node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond); |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8351 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8352 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 8353 | /* if it has parent non-presence containers that would be empty, we should actually |
| 8354 | * remove the container |
| 8355 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8356 | for (parent = unres->node[i]; |
| 8357 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 8358 | parent = parent->parent) { |
| 8359 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 8360 | /* presence container */ |
| 8361 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8362 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8363 | if (parent->next || parent->prev != parent) { |
| 8364 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 8365 | break; |
| 8366 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8367 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8368 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8369 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8370 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8371 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8372 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8373 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8374 | lyd_unlink(unres->node[i]); |
| 8375 | unres->type[i] = UNRES_DELETE; |
| 8376 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8377 | |
| 8378 | /* update the rest of unres items */ |
| 8379 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 8380 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8381 | continue; |
| 8382 | } |
| 8383 | |
| 8384 | /* test if the node is in subtree to be deleted */ |
| 8385 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 8386 | if (parent == unres->node[i]) { |
| 8387 | /* yes, it is */ |
| 8388 | unres->type[j] = UNRES_RESOLVED; |
| 8389 | resolved++; |
| 8390 | break; |
| 8391 | } |
| 8392 | } |
| 8393 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8394 | } else { |
| 8395 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8396 | } |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8397 | if (!ignore_fail) { |
| 8398 | ly_err_free_next(ctx, prev_eitem); |
| 8399 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8400 | resolved++; |
| 8401 | progress = 1; |
| 8402 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8403 | goto error; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 8404 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8405 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8406 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8407 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8408 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8409 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8410 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8411 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8412 | } |
| 8413 | |
| 8414 | for (i = 0; del_items && i < unres->count; i++) { |
| 8415 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 8416 | if (unres->type[i] != UNRES_DELETE) { |
| 8417 | continue; |
| 8418 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8419 | if (!unres->node[i]) { |
| 8420 | unres->type[i] = UNRES_RESOLVED; |
| 8421 | del_items--; |
| 8422 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8423 | } |
| 8424 | |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 8425 | if (unres->store_diff) { |
| 8426 | resolve_unres_data_autodel_diff(unres, i); |
| 8427 | } |
| 8428 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8429 | /* really remove the complete subtree */ |
| 8430 | lyd_free(unres->node[i]); |
| 8431 | unres->type[i] = UNRES_RESOLVED; |
| 8432 | del_items--; |
| 8433 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8434 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8435 | /* |
| 8436 | * now leafrefs |
| 8437 | */ |
| 8438 | if (options & LYD_OPT_TRUSTED) { |
| 8439 | /* we want to attempt to resolve leafrefs */ |
| 8440 | assert(!ignore_fail); |
| 8441 | ignore_fail = 1; |
| 8442 | |
| 8443 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8444 | ly_errno = prev_ly_errno; |
| 8445 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8446 | first = 1; |
| 8447 | stmt_count = 0; |
| 8448 | resolved = 0; |
| 8449 | do { |
| 8450 | progress = 0; |
| 8451 | for (i = 0; i < unres->count; i++) { |
| 8452 | if (unres->type[i] != UNRES_LEAFREF) { |
| 8453 | continue; |
| 8454 | } |
| 8455 | if (first) { |
| 8456 | /* count leafref nodes in unres list */ |
| 8457 | stmt_count++; |
| 8458 | } |
| 8459 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8460 | 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] | 8461 | if (!rc) { |
| 8462 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8463 | if (!ignore_fail) { |
| 8464 | ly_err_free_next(ctx, prev_eitem); |
| 8465 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8466 | resolved++; |
| 8467 | progress = 1; |
| 8468 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8469 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8470 | } /* else forward reference */ |
| 8471 | } |
| 8472 | first = 0; |
| 8473 | } while (progress && resolved < stmt_count); |
| 8474 | |
| 8475 | /* do we have some unresolved leafrefs? */ |
| 8476 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8477 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8478 | } |
| 8479 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8480 | if (!ignore_fail) { |
| 8481 | /* log normally now, throw away irrelevant errors */ |
| 8482 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8483 | ly_errno = prev_ly_errno; |
| 8484 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8485 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8486 | /* |
| 8487 | * rest |
| 8488 | */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8489 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8490 | if (unres->type[i] == UNRES_RESOLVED) { |
| 8491 | continue; |
| 8492 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8493 | 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] | 8494 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8495 | 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] | 8496 | if (rc) { |
| 8497 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8498 | return -1; |
| 8499 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8500 | |
| 8501 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8502 | } |
| 8503 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8504 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8505 | unres->count = 0; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8506 | return EXIT_SUCCESS; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8507 | |
| 8508 | error: |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8509 | if (!ignore_fail) { |
| 8510 | /* print all the new errors */ |
| 8511 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 8512 | /* do not restore ly_errno, it was udpated properly */ |
| 8513 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8514 | return -1; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8515 | } |