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 | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 33 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 34 | /* internal parsed predicate structure */ |
| 35 | struct parsed_pred { |
| 36 | const struct lys_node *schema; |
| 37 | int len; |
| 38 | struct { |
| 39 | const char *mod_name; |
| 40 | int mod_name_len; |
| 41 | const char *name; |
| 42 | int nam_len; |
| 43 | const char *value; |
| 44 | int val_len; |
| 45 | } *pred; |
| 46 | }; |
| 47 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 48 | int |
| 49 | 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] | 50 | { |
| 51 | const char *ptr; |
| 52 | int minus = 0; |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 53 | int64_t ret = 0, prev_ret; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 54 | int8_t str_exp, str_dig = -1, trailing_zeros = 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 55 | |
| 56 | ptr = *str_num; |
| 57 | |
| 58 | if (ptr[0] == '-') { |
| 59 | minus = 1; |
| 60 | ++ptr; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 61 | } else if (ptr[0] == '+') { |
| 62 | ++ptr; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 65 | if (!isdigit(ptr[0])) { |
| 66 | /* there must be at least one */ |
| 67 | return 1; |
| 68 | } |
| 69 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 70 | for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) { |
| 71 | if (str_exp > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 72 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (ptr[0] == '.') { |
| 76 | if (ptr[1] == '.') { |
| 77 | /* it's the next interval */ |
| 78 | break; |
| 79 | } |
| 80 | ++str_dig; |
| 81 | } else { |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 82 | prev_ret = ret; |
| 83 | if (minus) { |
| 84 | ret = ret * 10 - (ptr[0] - '0'); |
| 85 | if (ret > prev_ret) { |
| 86 | return 1; |
| 87 | } |
| 88 | } else { |
| 89 | ret = ret * 10 + (ptr[0] - '0'); |
| 90 | if (ret < prev_ret) { |
| 91 | return 1; |
| 92 | } |
| 93 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 94 | if (str_dig > -1) { |
| 95 | ++str_dig; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 96 | if (ptr[0] == '0') { |
| 97 | /* possibly trailing zero */ |
| 98 | trailing_zeros++; |
| 99 | } else { |
| 100 | trailing_zeros = 0; |
| 101 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 102 | } |
| 103 | ++str_exp; |
| 104 | } |
| 105 | } |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 106 | if (str_dig == 0) { |
| 107 | /* no digits after '.' */ |
| 108 | return 1; |
| 109 | } else if (str_dig == -1) { |
| 110 | /* there are 0 numbers after the floating point */ |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 111 | str_dig = 0; |
| 112 | } |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 113 | /* remove trailing zeros */ |
| 114 | if (trailing_zeros) { |
Michal Vasko | 6ca5ca7 | 2016-11-28 09:21:51 +0100 | [diff] [blame] | 115 | str_dig -= trailing_zeros; |
| 116 | str_exp -= trailing_zeros; |
Radek Krejci | bf47a82 | 2016-11-04 10:06:08 +0100 | [diff] [blame] | 117 | ret = ret / dec_pow(trailing_zeros); |
| 118 | } |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 119 | |
| 120 | /* it's parsed, now adjust the number based on fraction-digits, if needed */ |
| 121 | if (str_dig < dig) { |
| 122 | if ((str_exp - 1) + (dig - str_dig) > 18) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 123 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 124 | } |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 125 | prev_ret = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 126 | ret *= dec_pow(dig - str_dig); |
Michal Vasko | e2ea45a | 2017-08-07 13:15:07 +0200 | [diff] [blame] | 127 | if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) { |
| 128 | return 1; |
| 129 | } |
| 130 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 131 | } |
| 132 | if (str_dig > dig) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 133 | return 1; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 136 | *str_num = ptr; |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 137 | *num = ret; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 138 | |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 139 | return 0; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 143 | * @brief Parse an identifier. |
| 144 | * |
| 145 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 146 | * identifier = (ALPHA / "_") |
| 147 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 148 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 149 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 150 | * |
| 151 | * @return Number of characters successfully parsed. |
| 152 | */ |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 153 | unsigned int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 154 | parse_identifier(const char *id) |
| 155 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 156 | unsigned int parsed = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 157 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 158 | assert(id); |
| 159 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 160 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 161 | return -parsed; |
| 162 | } |
| 163 | |
| 164 | ++parsed; |
| 165 | ++id; |
| 166 | |
| 167 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 168 | ++parsed; |
| 169 | ++id; |
| 170 | } |
| 171 | |
| 172 | return parsed; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @brief Parse a node-identifier. |
| 177 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 178 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 179 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 180 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 181 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 182 | * @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] | 183 | * @param[out] name Points to the node name. |
| 184 | * @param[out] nam_len Length of the node name. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 185 | * @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] | 186 | * @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] | 187 | * |
| 188 | * @return Number of characters successfully parsed, |
| 189 | * positive on success, negative on failure. |
| 190 | */ |
| 191 | static int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 192 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 193 | int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 194 | { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 195 | int parsed = 0, ret, all_desc_local = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 196 | |
| 197 | assert(id); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 198 | assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len)); |
| 199 | assert((name && nam_len) || (!name && !nam_len)); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 200 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 201 | if (mod_name) { |
| 202 | *mod_name = NULL; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 203 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 204 | } |
| 205 | if (name) { |
| 206 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 207 | *nam_len = 0; |
| 208 | } |
| 209 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 210 | if (extended) { |
| 211 | /* try to parse only the extended expressions */ |
| 212 | if (id[parsed] == '/') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 213 | if (all_desc) { |
| 214 | *all_desc = 1; |
| 215 | } |
| 216 | all_desc_local = 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 217 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 218 | if (all_desc) { |
| 219 | *all_desc = 0; |
| 220 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* is there a prefix? */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 224 | ret = parse_identifier(id + all_desc_local); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 225 | if (ret > 0) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 226 | if (id[all_desc_local + ret] != ':') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 227 | /* this is not a prefix, so not an extended id */ |
| 228 | goto standard_id; |
| 229 | } |
| 230 | |
| 231 | if (mod_name) { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 232 | *mod_name = id + all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 233 | *mod_name_len = ret; |
| 234 | } |
| 235 | |
| 236 | /* "/" and ":" */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 237 | ret += all_desc_local + 1; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 238 | } else { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 239 | ret = all_desc_local; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* parse either "*" or "." */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 243 | if (*(id + ret) == '*') { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 244 | if (name) { |
| 245 | *name = id + ret; |
| 246 | *nam_len = 1; |
| 247 | } |
| 248 | ++ret; |
| 249 | |
| 250 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 251 | } else if (*(id + ret) == '.') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 252 | if (!all_desc_local) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 253 | /* /. is redundant expression, we do not accept it */ |
| 254 | return -ret; |
| 255 | } |
| 256 | |
| 257 | if (name) { |
| 258 | *name = id + ret; |
| 259 | *nam_len = 1; |
| 260 | } |
| 261 | ++ret; |
| 262 | |
| 263 | return ret; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 264 | } else if (*(id + ret) == '#') { |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 265 | if (all_desc_local || !ret) { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 266 | /* no prefix */ |
| 267 | return 0; |
| 268 | } |
| 269 | parsed = ret + 1; |
| 270 | if ((ret = parse_identifier(id + parsed)) < 1) { |
| 271 | return -parsed + ret; |
| 272 | } |
| 273 | *name = id + parsed - 1; |
| 274 | *nam_len = ret + 1; |
| 275 | return parsed + ret; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 276 | } |
| 277 | /* else a standard id, parse it all again */ |
| 278 | } |
| 279 | |
| 280 | standard_id: |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 281 | if ((ret = parse_identifier(id)) < 1) { |
| 282 | return ret; |
| 283 | } |
| 284 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 285 | if (mod_name) { |
| 286 | *mod_name = id; |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 287 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | parsed += ret; |
| 291 | id += ret; |
| 292 | |
| 293 | /* there is prefix */ |
| 294 | if (id[0] == ':') { |
| 295 | ++parsed; |
| 296 | ++id; |
| 297 | |
| 298 | /* there isn't */ |
| 299 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 300 | if (name && mod_name) { |
| 301 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 302 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 303 | if (mod_name) { |
| 304 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 307 | if (nam_len && mod_name_len) { |
| 308 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 309 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 310 | if (mod_name_len) { |
| 311 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return parsed; |
| 315 | } |
| 316 | |
| 317 | /* identifier (node name) */ |
| 318 | if ((ret = parse_identifier(id)) < 1) { |
| 319 | return -parsed+ret; |
| 320 | } |
| 321 | |
| 322 | if (name) { |
| 323 | *name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 324 | *nam_len = ret; |
| 325 | } |
| 326 | |
| 327 | return parsed+ret; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * @brief Parse a path-predicate (leafref). |
| 332 | * |
| 333 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 334 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 335 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 336 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 337 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 338 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 339 | * @param[out] name Points to the node name. |
| 340 | * @param[out] nam_len Length of the node name. |
| 341 | * @param[out] path_key_expr Points to the path-key-expr. |
| 342 | * @param[out] pke_len Length of the path-key-expr. |
| 343 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 344 | * |
| 345 | * @return Number of characters successfully parsed, |
| 346 | * positive on success, negative on failure. |
| 347 | */ |
| 348 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 349 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 350 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 351 | { |
| 352 | const char *ptr; |
| 353 | int parsed = 0, ret; |
| 354 | |
| 355 | assert(id); |
| 356 | if (prefix) { |
| 357 | *prefix = NULL; |
| 358 | } |
| 359 | if (pref_len) { |
| 360 | *pref_len = 0; |
| 361 | } |
| 362 | if (name) { |
| 363 | *name = NULL; |
| 364 | } |
| 365 | if (nam_len) { |
| 366 | *nam_len = 0; |
| 367 | } |
| 368 | if (path_key_expr) { |
| 369 | *path_key_expr = NULL; |
| 370 | } |
| 371 | if (pke_len) { |
| 372 | *pke_len = 0; |
| 373 | } |
| 374 | if (has_predicate) { |
| 375 | *has_predicate = 0; |
| 376 | } |
| 377 | |
| 378 | if (id[0] != '[') { |
| 379 | return -parsed; |
| 380 | } |
| 381 | |
| 382 | ++parsed; |
| 383 | ++id; |
| 384 | |
| 385 | while (isspace(id[0])) { |
| 386 | ++parsed; |
| 387 | ++id; |
| 388 | } |
| 389 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 390 | 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] | 391 | return -parsed+ret; |
| 392 | } |
| 393 | |
| 394 | parsed += ret; |
| 395 | id += ret; |
| 396 | |
| 397 | while (isspace(id[0])) { |
| 398 | ++parsed; |
| 399 | ++id; |
| 400 | } |
| 401 | |
| 402 | if (id[0] != '=') { |
| 403 | return -parsed; |
| 404 | } |
| 405 | |
| 406 | ++parsed; |
| 407 | ++id; |
| 408 | |
| 409 | while (isspace(id[0])) { |
| 410 | ++parsed; |
| 411 | ++id; |
| 412 | } |
| 413 | |
| 414 | if ((ptr = strchr(id, ']')) == NULL) { |
| 415 | return -parsed; |
| 416 | } |
| 417 | |
| 418 | --ptr; |
| 419 | while (isspace(ptr[0])) { |
| 420 | --ptr; |
| 421 | } |
| 422 | ++ptr; |
| 423 | |
| 424 | ret = ptr-id; |
| 425 | if (path_key_expr) { |
| 426 | *path_key_expr = id; |
| 427 | } |
| 428 | if (pke_len) { |
| 429 | *pke_len = ret; |
| 430 | } |
| 431 | |
| 432 | parsed += ret; |
| 433 | id += ret; |
| 434 | |
| 435 | while (isspace(id[0])) { |
| 436 | ++parsed; |
| 437 | ++id; |
| 438 | } |
| 439 | |
| 440 | assert(id[0] == ']'); |
| 441 | |
| 442 | if (id[1] == '[') { |
| 443 | *has_predicate = 1; |
| 444 | } |
| 445 | |
| 446 | return parsed+1; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 451 | * the ".." and the first node-identifier, other calls parse a single |
| 452 | * node-identifier each. |
| 453 | * |
| 454 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 455 | * rel-path-keyexpr |
| 456 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 457 | * *(node-identifier *WSP "/" *WSP) |
| 458 | * node-identifier |
| 459 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 460 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 461 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 462 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 463 | * @param[out] name Points to the node name. |
| 464 | * @param[out] nam_len Length of the node name. |
| 465 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 466 | * must not be changed between consecutive calls. |
| 467 | * @return Number of characters successfully parsed, |
| 468 | * positive on success, negative on failure. |
| 469 | */ |
| 470 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 471 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 472 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 473 | { |
| 474 | int parsed = 0, ret, par_times = 0; |
| 475 | |
| 476 | assert(id); |
| 477 | assert(parent_times); |
| 478 | if (prefix) { |
| 479 | *prefix = NULL; |
| 480 | } |
| 481 | if (pref_len) { |
| 482 | *pref_len = 0; |
| 483 | } |
| 484 | if (name) { |
| 485 | *name = NULL; |
| 486 | } |
| 487 | if (nam_len) { |
| 488 | *nam_len = 0; |
| 489 | } |
| 490 | |
| 491 | if (!*parent_times) { |
| 492 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 493 | if (strncmp(id, "current()", 9)) { |
| 494 | return -parsed; |
| 495 | } |
| 496 | |
| 497 | parsed += 9; |
| 498 | id += 9; |
| 499 | |
| 500 | while (isspace(id[0])) { |
| 501 | ++parsed; |
| 502 | ++id; |
| 503 | } |
| 504 | |
| 505 | if (id[0] != '/') { |
| 506 | return -parsed; |
| 507 | } |
| 508 | |
| 509 | ++parsed; |
| 510 | ++id; |
| 511 | |
| 512 | while (isspace(id[0])) { |
| 513 | ++parsed; |
| 514 | ++id; |
| 515 | } |
| 516 | |
| 517 | /* rel-path-keyexpr */ |
| 518 | if (strncmp(id, "..", 2)) { |
| 519 | return -parsed; |
| 520 | } |
| 521 | ++par_times; |
| 522 | |
| 523 | parsed += 2; |
| 524 | id += 2; |
| 525 | |
| 526 | while (isspace(id[0])) { |
| 527 | ++parsed; |
| 528 | ++id; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 533 | * |
| 534 | * first parent reference with whitespaces already parsed |
| 535 | */ |
| 536 | if (id[0] != '/') { |
| 537 | return -parsed; |
| 538 | } |
| 539 | |
| 540 | ++parsed; |
| 541 | ++id; |
| 542 | |
| 543 | while (isspace(id[0])) { |
| 544 | ++parsed; |
| 545 | ++id; |
| 546 | } |
| 547 | |
| 548 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 549 | ++par_times; |
| 550 | |
| 551 | parsed += 2; |
| 552 | id += 2; |
| 553 | |
| 554 | while (isspace(id[0])) { |
| 555 | ++parsed; |
| 556 | ++id; |
| 557 | } |
| 558 | |
| 559 | if (id[0] != '/') { |
| 560 | return -parsed; |
| 561 | } |
| 562 | |
| 563 | ++parsed; |
| 564 | ++id; |
| 565 | |
| 566 | while (isspace(id[0])) { |
| 567 | ++parsed; |
| 568 | ++id; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (!*parent_times) { |
| 573 | *parent_times = par_times; |
| 574 | } |
| 575 | |
| 576 | /* all parent references must be parsed at this point */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 577 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 578 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | parsed += ret; |
| 582 | id += ret; |
| 583 | |
| 584 | return parsed; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * @brief Parse path-arg (leafref). |
| 589 | * |
| 590 | * path-arg = absolute-path / relative-path |
| 591 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 592 | * relative-path = 1*(".." "/") descendant-path |
| 593 | * |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 594 | * @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] | 595 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 596 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 597 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 598 | * @param[out] name Points to the node name. |
| 599 | * @param[out] nam_len Length of the node name. |
| 600 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 601 | * must not be changed between consecutive calls. -1 if the |
| 602 | * path is relative. |
| 603 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 604 | * |
| 605 | * @return Number of characters successfully parsed, |
| 606 | * positive on success, negative on failure. |
| 607 | */ |
| 608 | static int |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 609 | 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] | 610 | const char **name, int *nam_len, int *parent_times, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 611 | { |
| 612 | int parsed = 0, ret, par_times = 0; |
| 613 | |
| 614 | assert(id); |
| 615 | assert(parent_times); |
| 616 | if (prefix) { |
| 617 | *prefix = NULL; |
| 618 | } |
| 619 | if (pref_len) { |
| 620 | *pref_len = 0; |
| 621 | } |
| 622 | if (name) { |
| 623 | *name = NULL; |
| 624 | } |
| 625 | if (nam_len) { |
| 626 | *nam_len = 0; |
| 627 | } |
| 628 | if (has_predicate) { |
| 629 | *has_predicate = 0; |
| 630 | } |
| 631 | |
| 632 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 633 | ++par_times; |
| 634 | |
| 635 | parsed += 2; |
| 636 | id += 2; |
| 637 | |
| 638 | while (!strncmp(id, "/..", 3)) { |
| 639 | ++par_times; |
| 640 | |
| 641 | parsed += 3; |
| 642 | id += 3; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (!*parent_times) { |
| 647 | if (par_times) { |
| 648 | *parent_times = par_times; |
| 649 | } else { |
| 650 | *parent_times = -1; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (id[0] != '/') { |
| 655 | return -parsed; |
| 656 | } |
| 657 | |
| 658 | /* skip '/' */ |
| 659 | ++parsed; |
| 660 | ++id; |
| 661 | |
| 662 | /* node-identifier ([prefix:]identifier) */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 663 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) { |
| 664 | return -parsed - ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 665 | } |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 666 | if (prefix && !(*prefix)) { |
Radek Krejci | f7ed4c3 | 2016-10-27 16:20:03 +0200 | [diff] [blame] | 667 | /* actually we always need prefix even it is not specified */ |
| 668 | *prefix = lys_main_module(mod)->name; |
| 669 | *pref_len = strlen(*prefix); |
| 670 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 671 | |
| 672 | parsed += ret; |
| 673 | id += ret; |
| 674 | |
| 675 | /* there is no predicate */ |
| 676 | if ((id[0] == '/') || !id[0]) { |
| 677 | return parsed; |
| 678 | } else if (id[0] != '[') { |
| 679 | return -parsed; |
| 680 | } |
| 681 | |
| 682 | if (has_predicate) { |
| 683 | *has_predicate = 1; |
| 684 | } |
| 685 | |
| 686 | return parsed; |
| 687 | } |
| 688 | |
| 689 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 690 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 691 | * are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 692 | * |
| 693 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 694 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 695 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 696 | * @param[out] model Points to the model name. |
| 697 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 698 | * @param[out] name Points to the node name. |
| 699 | * @param[out] nam_len Length of the node name. |
| 700 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 701 | * |
| 702 | * @return Number of characters successfully parsed, |
| 703 | * positive on success, negative on failure. |
| 704 | */ |
| 705 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 706 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 707 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 708 | { |
| 709 | int parsed = 0, ret; |
| 710 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 711 | assert(id && model && mod_len && name && nam_len); |
| 712 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 713 | if (has_predicate) { |
| 714 | *has_predicate = 0; |
| 715 | } |
| 716 | |
| 717 | if (id[0] != '/') { |
| 718 | return -parsed; |
| 719 | } |
| 720 | |
| 721 | ++parsed; |
| 722 | ++id; |
| 723 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 724 | if ((ret = parse_identifier(id)) < 1) { |
| 725 | return ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 728 | *name = id; |
| 729 | *nam_len = ret; |
| 730 | |
| 731 | parsed += ret; |
| 732 | id += ret; |
| 733 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 734 | if (id[0] == ':') { |
| 735 | /* we have prefix */ |
| 736 | *model = *name; |
| 737 | *mod_len = *nam_len; |
| 738 | |
| 739 | ++parsed; |
| 740 | ++id; |
| 741 | |
| 742 | if ((ret = parse_identifier(id)) < 1) { |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | *name = id; |
| 747 | *nam_len = ret; |
| 748 | |
| 749 | parsed += ret; |
| 750 | id += ret; |
| 751 | } |
| 752 | |
Radek Krejci | 4967cb6 | 2016-09-14 16:40:28 +0200 | [diff] [blame] | 753 | if (id[0] == '[' && has_predicate) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 754 | *has_predicate = 1; |
| 755 | } |
| 756 | |
| 757 | return parsed; |
| 758 | } |
| 759 | |
| 760 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 761 | * @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] | 762 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 763 | * |
| 764 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 765 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 766 | * ((DQUOTE string DQUOTE) / |
| 767 | * (SQUOTE string SQUOTE)) |
| 768 | * pos = non-negative-integer-value |
| 769 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 770 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 771 | * @param[out] model Points to the model name. |
| 772 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 773 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 774 | * @param[out] nam_len Length of the node name. |
| 775 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 776 | * NULL if there is not any. |
| 777 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 778 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 779 | * |
| 780 | * @return Number of characters successfully parsed, |
| 781 | * positive on success, negative on failure. |
| 782 | */ |
| 783 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 784 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 785 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 786 | { |
| 787 | const char *ptr; |
| 788 | int parsed = 0, ret; |
| 789 | char quote; |
| 790 | |
| 791 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 792 | if (model) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 793 | assert(mod_len); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 794 | *model = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 795 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 796 | } |
| 797 | if (name) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 798 | assert(nam_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 799 | *name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 800 | *nam_len = 0; |
| 801 | } |
| 802 | if (value) { |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 803 | assert(val_len); |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 804 | *value = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 805 | *val_len = 0; |
| 806 | } |
| 807 | if (has_predicate) { |
| 808 | *has_predicate = 0; |
| 809 | } |
| 810 | |
| 811 | if (id[0] != '[') { |
| 812 | return -parsed; |
| 813 | } |
| 814 | |
| 815 | ++parsed; |
| 816 | ++id; |
| 817 | |
| 818 | while (isspace(id[0])) { |
| 819 | ++parsed; |
| 820 | ++id; |
| 821 | } |
| 822 | |
| 823 | /* pos */ |
| 824 | if (isdigit(id[0])) { |
| 825 | if (name) { |
| 826 | *name = id; |
| 827 | } |
| 828 | |
| 829 | if (id[0] == '0') { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 830 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | while (isdigit(id[0])) { |
| 834 | ++parsed; |
| 835 | ++id; |
| 836 | } |
| 837 | |
| 838 | if (nam_len) { |
| 839 | *nam_len = id-(*name); |
| 840 | } |
| 841 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 842 | /* "." or node-identifier */ |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 843 | } else { |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 844 | if (id[0] == '.') { |
| 845 | if (name) { |
| 846 | *name = id; |
| 847 | } |
| 848 | if (nam_len) { |
| 849 | *nam_len = 1; |
| 850 | } |
| 851 | |
| 852 | ++parsed; |
| 853 | ++id; |
| 854 | |
| 855 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 856 | 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] | 857 | return -parsed + ret; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | parsed += ret; |
| 861 | id += ret; |
| 862 | } |
| 863 | |
| 864 | while (isspace(id[0])) { |
| 865 | ++parsed; |
| 866 | ++id; |
| 867 | } |
| 868 | |
| 869 | if (id[0] != '=') { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 870 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 871 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 872 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 873 | ++parsed; |
| 874 | ++id; |
| 875 | |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 876 | while (isspace(id[0])) { |
| 877 | ++parsed; |
| 878 | ++id; |
| 879 | } |
| 880 | |
| 881 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 882 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 883 | quote = id[0]; |
| 884 | |
| 885 | ++parsed; |
| 886 | ++id; |
| 887 | |
| 888 | if ((ptr = strchr(id, quote)) == NULL) { |
| 889 | return -parsed; |
| 890 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 891 | ret = ptr - id; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 892 | |
| 893 | if (value) { |
| 894 | *value = id; |
| 895 | } |
| 896 | if (val_len) { |
| 897 | *val_len = ret; |
| 898 | } |
| 899 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 900 | parsed += ret + 1; |
| 901 | id += ret + 1; |
Michal Vasko | f2f28a1 | 2016-09-09 12:43:06 +0200 | [diff] [blame] | 902 | } else { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 903 | return -parsed; |
| 904 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | while (isspace(id[0])) { |
| 908 | ++parsed; |
| 909 | ++id; |
| 910 | } |
| 911 | |
| 912 | if (id[0] != ']') { |
| 913 | return -parsed; |
| 914 | } |
| 915 | |
| 916 | ++parsed; |
| 917 | ++id; |
| 918 | |
| 919 | if ((id[0] == '[') && has_predicate) { |
| 920 | *has_predicate = 1; |
| 921 | } |
| 922 | |
| 923 | return parsed; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * @brief Parse schema-nodeid. |
| 928 | * |
| 929 | * schema-nodeid = absolute-schema-nodeid / |
| 930 | * descendant-schema-nodeid |
| 931 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 932 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 933 | * node-identifier |
| 934 | * absolute-schema-nodeid |
| 935 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 936 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 937 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 938 | * @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] | 939 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 940 | * @param[out] nam_len Length of the node name. |
| 941 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 942 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 943 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 944 | * based on the grammar, in those cases use NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 945 | * @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] | 946 | * |
| 947 | * @return Number of characters successfully parsed, |
| 948 | * positive on success, negative on failure. |
| 949 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 950 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 951 | 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] | 952 | int *is_relative, int *has_predicate, int *all_desc, int extended) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 953 | { |
| 954 | int parsed = 0, ret; |
| 955 | |
| 956 | assert(id); |
| 957 | assert(is_relative); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 958 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 959 | if (has_predicate) { |
| 960 | *has_predicate = 0; |
| 961 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 962 | |
| 963 | if (id[0] != '/') { |
| 964 | if (*is_relative != -1) { |
| 965 | return -parsed; |
| 966 | } else { |
| 967 | *is_relative = 1; |
| 968 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 969 | if (!strncmp(id, "./", 2)) { |
| 970 | parsed += 2; |
| 971 | id += 2; |
| 972 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 973 | } else { |
| 974 | if (*is_relative == -1) { |
| 975 | *is_relative = 0; |
| 976 | } |
| 977 | ++parsed; |
| 978 | ++id; |
| 979 | } |
| 980 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 981 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) { |
| 982 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 985 | parsed += ret; |
| 986 | id += ret; |
| 987 | |
| 988 | if ((id[0] == '[') && has_predicate) { |
| 989 | *has_predicate = 1; |
| 990 | } |
| 991 | |
| 992 | return parsed; |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * @brief Parse schema predicate (special format internally used). |
| 997 | * |
| 998 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 999 | * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1000 | * key-with-value = identifier *WSP "=" *WSP |
| 1001 | * ((DQUOTE string DQUOTE) / |
| 1002 | * (SQUOTE string SQUOTE)) |
| 1003 | * |
| 1004 | * @param[in] id Identifier to use. |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1005 | * @param[out] mod_name Points to the list key module name. |
| 1006 | * @param[out] mod_name_len Length of \p mod_name. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1007 | * @param[out] name Points to the list key name. |
| 1008 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1009 | * @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] | 1010 | * @param[out] val_len Length of \p value. |
| 1011 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 1012 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1013 | int |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1014 | parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
| 1015 | const char **value, int *val_len, int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1016 | { |
| 1017 | const char *ptr; |
| 1018 | int parsed = 0, ret; |
| 1019 | char quote; |
| 1020 | |
| 1021 | assert(id); |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1022 | if (mod_name) { |
| 1023 | *mod_name = NULL; |
| 1024 | } |
| 1025 | if (mod_name_len) { |
| 1026 | *mod_name_len = 0; |
| 1027 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1028 | if (name) { |
| 1029 | *name = NULL; |
| 1030 | } |
| 1031 | if (nam_len) { |
| 1032 | *nam_len = 0; |
| 1033 | } |
| 1034 | if (value) { |
| 1035 | *value = NULL; |
| 1036 | } |
| 1037 | if (val_len) { |
| 1038 | *val_len = 0; |
| 1039 | } |
| 1040 | if (has_predicate) { |
| 1041 | *has_predicate = 0; |
| 1042 | } |
| 1043 | |
| 1044 | if (id[0] != '[') { |
| 1045 | return -parsed; |
| 1046 | } |
| 1047 | |
| 1048 | ++parsed; |
| 1049 | ++id; |
| 1050 | |
| 1051 | while (isspace(id[0])) { |
| 1052 | ++parsed; |
| 1053 | ++id; |
| 1054 | } |
| 1055 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1056 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1057 | if (id[0] == '.') { |
| 1058 | ret = 1; |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1059 | |
| 1060 | if (name) { |
| 1061 | *name = id; |
| 1062 | } |
| 1063 | if (nam_len) { |
| 1064 | *nam_len = ret; |
| 1065 | } |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1066 | } else if (isdigit(id[0])) { |
| 1067 | if (id[0] == '0') { |
| 1068 | return -parsed; |
| 1069 | } |
| 1070 | ret = 1; |
| 1071 | while (isdigit(id[ret])) { |
| 1072 | ++ret; |
| 1073 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 1074 | |
| 1075 | if (name) { |
| 1076 | *name = id; |
| 1077 | } |
| 1078 | if (nam_len) { |
| 1079 | *nam_len = ret; |
| 1080 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1081 | } 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] | 1082 | return -parsed + ret; |
| 1083 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1084 | |
| 1085 | parsed += ret; |
| 1086 | id += ret; |
| 1087 | |
| 1088 | while (isspace(id[0])) { |
| 1089 | ++parsed; |
| 1090 | ++id; |
| 1091 | } |
| 1092 | |
| 1093 | /* there is value as well */ |
| 1094 | if (id[0] == '=') { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 1095 | if (name && isdigit(**name)) { |
| 1096 | return -parsed; |
| 1097 | } |
| 1098 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1099 | ++parsed; |
| 1100 | ++id; |
| 1101 | |
| 1102 | while (isspace(id[0])) { |
| 1103 | ++parsed; |
| 1104 | ++id; |
| 1105 | } |
| 1106 | |
| 1107 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 1108 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 1109 | quote = id[0]; |
| 1110 | |
| 1111 | ++parsed; |
| 1112 | ++id; |
| 1113 | |
| 1114 | if ((ptr = strchr(id, quote)) == NULL) { |
| 1115 | return -parsed; |
| 1116 | } |
| 1117 | ret = ptr - id; |
| 1118 | |
| 1119 | if (value) { |
| 1120 | *value = id; |
| 1121 | } |
| 1122 | if (val_len) { |
| 1123 | *val_len = ret; |
| 1124 | } |
| 1125 | |
| 1126 | parsed += ret + 1; |
| 1127 | id += ret + 1; |
| 1128 | } else { |
| 1129 | return -parsed; |
| 1130 | } |
| 1131 | |
| 1132 | while (isspace(id[0])) { |
| 1133 | ++parsed; |
| 1134 | ++id; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | if (id[0] != ']') { |
| 1139 | return -parsed; |
| 1140 | } |
| 1141 | |
| 1142 | ++parsed; |
| 1143 | ++id; |
| 1144 | |
| 1145 | if ((id[0] == '[') && has_predicate) { |
| 1146 | *has_predicate = 1; |
| 1147 | } |
| 1148 | |
| 1149 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1152 | #ifdef LY_ENABLED_CACHE |
| 1153 | |
| 1154 | static int |
| 1155 | resolve_hash_table_find_equal(void *val1_p, void *val2_p, int mod, void *UNUSED(cb_data)) |
| 1156 | { |
| 1157 | struct lyd_node *val2, *elem2; |
| 1158 | struct parsed_pred pp; |
| 1159 | const char *str; |
| 1160 | int i; |
| 1161 | |
| 1162 | assert(!mod); |
Michal Vasko | 87e29a8 | 2018-05-21 13:50:43 +0200 | [diff] [blame] | 1163 | (void)mod; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1164 | |
| 1165 | pp = *((struct parsed_pred *)val1_p); |
| 1166 | val2 = *((struct lyd_node **)val2_p); |
| 1167 | |
| 1168 | if (val2->schema != pp.schema) { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | switch (val2->schema->nodetype) { |
| 1173 | case LYS_CONTAINER: |
| 1174 | case LYS_LEAF: |
| 1175 | case LYS_ANYXML: |
| 1176 | case LYS_ANYDATA: |
| 1177 | return 1; |
| 1178 | case LYS_LEAFLIST: |
| 1179 | str = ((struct lyd_node_leaf_list *)val2)->value_str; |
| 1180 | if (!strncmp(str, pp.pred[0].value, pp.pred[0].val_len) && !str[pp.pred[0].val_len]) { |
| 1181 | return 1; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | case LYS_LIST: |
| 1185 | assert(((struct lys_node_list *)val2->schema)->keys_size); |
| 1186 | assert(((struct lys_node_list *)val2->schema)->keys_size == pp.len); |
| 1187 | |
| 1188 | /* lists with keys, their equivalence is based on their keys */ |
| 1189 | elem2 = val2->child; |
| 1190 | /* the exact data order is guaranteed */ |
| 1191 | for (i = 0; elem2 && (i < pp.len); ++i) { |
| 1192 | /* module check */ |
| 1193 | if (pp.pred[i].mod_name) { |
| 1194 | if (strncmp(lyd_node_module(elem2)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len) |
| 1195 | || lyd_node_module(elem2)->name[pp.pred[i].mod_name_len]) { |
| 1196 | break; |
| 1197 | } |
| 1198 | } else { |
| 1199 | if (lyd_node_module(elem2) != lys_node_module(pp.schema)) { |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* name check */ |
| 1205 | if (strncmp(elem2->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || elem2->schema->name[pp.pred[i].nam_len]) { |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | /* value check */ |
| 1210 | str = ((struct lyd_node_leaf_list *)elem2)->value_str; |
| 1211 | if (strncmp(str, pp.pred[i].value, pp.pred[i].val_len) || str[pp.pred[i].val_len]) { |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | /* next key */ |
| 1216 | elem2 = elem2->next; |
| 1217 | } |
| 1218 | if (i == pp.len) { |
| 1219 | return 1; |
| 1220 | } |
| 1221 | return 0; |
| 1222 | default: |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | LOGINT(val2->schema->module->ctx); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | static struct lyd_node * |
| 1231 | resolve_json_data_node_hash(struct lyd_node *parent, struct parsed_pred pp) |
| 1232 | { |
| 1233 | values_equal_cb prev_cb; |
| 1234 | struct lyd_node **ret = NULL; |
| 1235 | uint32_t hash; |
| 1236 | int i; |
| 1237 | |
| 1238 | assert(parent && parent->hash); |
| 1239 | |
| 1240 | /* set our value equivalence callback that does not require data nodes */ |
| 1241 | prev_cb = lyht_set_cb(parent->ht, resolve_hash_table_find_equal); |
| 1242 | |
| 1243 | /* get the hash of the searched node */ |
| 1244 | hash = dict_hash_multi(0, lys_node_module(pp.schema)->name, strlen(lys_node_module(pp.schema)->name)); |
| 1245 | hash = dict_hash_multi(hash, pp.schema->name, strlen(pp.schema->name)); |
| 1246 | if (pp.schema->nodetype == LYS_LEAFLIST) { |
| 1247 | assert((pp.len == 1) && (pp.pred[0].name[0] == '.') && (pp.pred[0].nam_len == 1)); |
| 1248 | /* leaf-list value in predicate */ |
| 1249 | hash = dict_hash_multi(hash, pp.pred[0].value, pp.pred[0].val_len); |
| 1250 | } else if (pp.schema->nodetype == LYS_LIST) { |
| 1251 | /* list keys in predicates */ |
| 1252 | for (i = 0; i < pp.len; ++i) { |
| 1253 | hash = dict_hash_multi(hash, pp.pred[i].value, pp.pred[i].val_len); |
| 1254 | } |
| 1255 | } |
| 1256 | hash = dict_hash_multi(hash, NULL, 0); |
| 1257 | |
| 1258 | /* try to find the node */ |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 1259 | i = lyht_find(parent->ht, &pp, hash, (void **)&ret); |
| 1260 | assert(i || *ret); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1261 | |
| 1262 | /* restore the original callback */ |
| 1263 | lyht_set_cb(parent->ht, prev_cb); |
| 1264 | |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 1265 | return (i ? NULL : *ret); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | #endif |
| 1269 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 1270 | /** |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1271 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1272 | * |
| 1273 | * @param[in] feat_name Feature name to resolve. |
| 1274 | * @param[in] len Length of \p feat_name. |
| 1275 | * @param[in] node Node with the if-feature expression. |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1276 | * @param[out] feature Pointer to be set to point to the feature definition, if feature not found |
| 1277 | * (return code 1), the pointer is untouched. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1278 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1279 | * @return 0 on success, 1 on forward reference, -1 on error. |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1280 | */ |
| 1281 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1282 | 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] | 1283 | { |
| 1284 | char *str; |
| 1285 | const char *mod_name, *name; |
| 1286 | int mod_name_len, nam_len, i, j; |
| 1287 | const struct lys_module *module; |
| 1288 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1289 | assert(feature); |
| 1290 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1291 | /* check prefix */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1292 | 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] | 1293 | 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] | 1294 | return -1; |
| 1295 | } |
| 1296 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1297 | 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] | 1298 | if (!module) { |
| 1299 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1300 | 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] | 1301 | return -1; |
| 1302 | } |
| 1303 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1304 | if (module != node->module && module == lys_node_module(node)) { |
| 1305 | /* first, try to search directly in submodule where the feature was mentioned */ |
| 1306 | for (j = 0; j < node->module->features_size; j++) { |
| 1307 | if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) { |
| 1308 | /* check status */ |
| 1309 | 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] | 1310 | node->module->features[j].module, node->module->features[j].name, NULL)) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | *feature = &node->module->features[j]; |
| 1314 | return 0; |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1319 | /* search in the identified module ... */ |
| 1320 | for (j = 0; j < module->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1321 | 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] | 1322 | /* check status */ |
| 1323 | 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] | 1324 | module->features[j].module, module->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1325 | return -1; |
| 1326 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1327 | *feature = &module->features[j]; |
| 1328 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | /* ... and all its submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 1332 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1333 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
Michal Vasko | 3def867 | 2016-07-01 11:43:09 +0200 | [diff] [blame] | 1334 | if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len) |
| 1335 | && !module->inc[i].submodule->features[j].name[nam_len]) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1336 | /* check status */ |
| 1337 | if (lyp_check_status(node->flags, lys_node_module(node), node->name, |
| 1338 | module->inc[i].submodule->features[j].flags, |
| 1339 | module->inc[i].submodule->features[j].module, |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 1340 | module->inc[i].submodule->features[j].name, NULL)) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1341 | return -1; |
| 1342 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1343 | *feature = &module->inc[i].submodule->features[j]; |
| 1344 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /* not found */ |
| 1350 | str = strndup(feat_name, len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1351 | LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1352 | free(str); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1353 | return 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1356 | /* |
| 1357 | * @return |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1358 | * - 1 if enabled |
| 1359 | * - 0 if disabled |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1360 | */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1361 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1362 | resolve_feature_value(const struct lys_feature *feat) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1363 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1364 | int i; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1365 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1366 | for (i = 0; i < feat->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1367 | if (!resolve_iffeature(&feat->iffeature[i])) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1368 | return 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1369 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1370 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1371 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1372 | return feat->flags & LYS_FENABLED ? 1 : 0; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1376 | 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] | 1377 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1378 | uint8_t op; |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1379 | int a, b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1380 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1381 | op = iff_getop(expr->expr, *index_e); |
| 1382 | (*index_e)++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1383 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1384 | switch (op) { |
| 1385 | case LYS_IFF_F: |
| 1386 | /* resolve feature */ |
| 1387 | return resolve_feature_value(expr->features[(*index_f)++]); |
| 1388 | case LYS_IFF_NOT: |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1389 | /* invert result */ |
| 1390 | return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1391 | case LYS_IFF_AND: |
| 1392 | case LYS_IFF_OR: |
| 1393 | a = resolve_iffeature_recursive(expr, index_e, index_f); |
| 1394 | b = resolve_iffeature_recursive(expr, index_e, index_f); |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1395 | if (op == LYS_IFF_AND) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1396 | return a && b; |
| 1397 | } else { /* LYS_IFF_OR */ |
| 1398 | return a || b; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1402 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | int |
| 1406 | resolve_iffeature(struct lys_iffeature *expr) |
| 1407 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1408 | int index_e = 0, index_f = 0; |
| 1409 | |
| 1410 | if (expr->expr) { |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1411 | return resolve_iffeature_recursive(expr, &index_e, &index_f); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1412 | } |
Radek Krejci | af56633 | 2017-02-07 15:56:59 +0100 | [diff] [blame] | 1413 | return 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | struct iff_stack { |
| 1417 | int size; |
| 1418 | int index; /* first empty item */ |
| 1419 | uint8_t *stack; |
| 1420 | }; |
| 1421 | |
| 1422 | static int |
| 1423 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 1424 | { |
| 1425 | if (stack->index == stack->size) { |
| 1426 | stack->size += 4; |
| 1427 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1428 | 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] | 1429 | } |
| 1430 | |
| 1431 | stack->stack[stack->index++] = value; |
| 1432 | return EXIT_SUCCESS; |
| 1433 | } |
| 1434 | |
| 1435 | static uint8_t |
| 1436 | iff_stack_pop(struct iff_stack *stack) |
| 1437 | { |
| 1438 | stack->index--; |
| 1439 | return stack->stack[stack->index]; |
| 1440 | } |
| 1441 | |
| 1442 | static void |
| 1443 | iff_stack_clean(struct iff_stack *stack) |
| 1444 | { |
| 1445 | stack->size = 0; |
| 1446 | free(stack->stack); |
| 1447 | } |
| 1448 | |
| 1449 | static void |
| 1450 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 1451 | { |
| 1452 | uint8_t *item; |
| 1453 | uint8_t mask = 3; |
| 1454 | |
| 1455 | assert(pos >= 0); |
| 1456 | assert(op <= 3); /* max 2 bits */ |
| 1457 | |
| 1458 | item = &list[pos / 4]; |
| 1459 | mask = mask << 2 * (pos % 4); |
| 1460 | *item = (*item) & ~mask; |
| 1461 | *item = (*item) | (op << 2 * (pos % 4)); |
| 1462 | } |
| 1463 | |
| 1464 | uint8_t |
| 1465 | iff_getop(uint8_t *list, int pos) |
| 1466 | { |
| 1467 | uint8_t *item; |
| 1468 | uint8_t mask = 3, result; |
| 1469 | |
| 1470 | assert(pos >= 0); |
| 1471 | |
| 1472 | item = &list[pos / 4]; |
| 1473 | result = (*item) & (mask << 2 * (pos % 4)); |
| 1474 | return result >> 2 * (pos % 4); |
| 1475 | } |
| 1476 | |
| 1477 | #define LYS_IFF_LP 0x04 /* ( */ |
| 1478 | #define LYS_IFF_RP 0x08 /* ) */ |
| 1479 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1480 | /* internal structure for passing data for UNRES_IFFEAT */ |
| 1481 | struct unres_iffeat_data { |
| 1482 | struct lys_node *node; |
| 1483 | const char *fname; |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1484 | int infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1485 | }; |
| 1486 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1487 | void |
| 1488 | resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size) |
| 1489 | { |
| 1490 | unsigned int e = 0, f = 0, r = 0; |
| 1491 | uint8_t op; |
| 1492 | |
| 1493 | assert(iffeat); |
| 1494 | |
| 1495 | if (!iffeat->expr) { |
| 1496 | goto result; |
| 1497 | } |
| 1498 | |
| 1499 | do { |
| 1500 | op = iff_getop(iffeat->expr, e++); |
| 1501 | switch (op) { |
| 1502 | case LYS_IFF_NOT: |
| 1503 | if (!r) { |
| 1504 | r += 1; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1505 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1506 | break; |
| 1507 | case LYS_IFF_AND: |
| 1508 | case LYS_IFF_OR: |
| 1509 | if (!r) { |
| 1510 | r += 2; |
| 1511 | } else { |
| 1512 | r += 1; |
| 1513 | } |
| 1514 | break; |
| 1515 | case LYS_IFF_F: |
| 1516 | f++; |
| 1517 | if (r) { |
| 1518 | r--; |
| 1519 | } |
| 1520 | break; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1521 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1522 | } while(r); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1523 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1524 | result: |
| 1525 | if (expr_size) { |
| 1526 | *expr_size = e; |
| 1527 | } |
| 1528 | if (feat_size) { |
| 1529 | *feat_size = f; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | int |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1534 | 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] | 1535 | int infeature, struct unres_schema *unres) |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1536 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1537 | const char *c = value; |
| 1538 | int r, rc = EXIT_FAILURE; |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1539 | int i, j, last_not, checkversion = 0; |
| 1540 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1541 | uint8_t op; |
| 1542 | struct iff_stack stack = {0, 0, NULL}; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1543 | struct unres_iffeat_data *iff_data; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1544 | struct ly_ctx *ctx = node->module->ctx; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1545 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1546 | assert(c); |
| 1547 | |
| 1548 | if (isspace(c[0])) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1549 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1550 | return EXIT_FAILURE; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1553 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 1554 | for (i = j = last_not = 0; c[i]; i++) { |
| 1555 | if (c[i] == '(') { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1556 | checkversion = 1; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1557 | j++; |
| 1558 | continue; |
| 1559 | } else if (c[i] == ')') { |
| 1560 | j--; |
| 1561 | continue; |
| 1562 | } else if (isspace(c[i])) { |
| 1563 | continue; |
| 1564 | } |
| 1565 | |
| 1566 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 1567 | if (c[i + r] == '\0') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1568 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1569 | return EXIT_FAILURE; |
| 1570 | } else if (!isspace(c[i + r])) { |
| 1571 | /* feature name starting with the not/and/or */ |
| 1572 | last_not = 0; |
| 1573 | f_size++; |
| 1574 | } else if (c[i] == 'n') { /* not operation */ |
| 1575 | if (last_not) { |
| 1576 | /* double not */ |
| 1577 | expr_size = expr_size - 2; |
| 1578 | last_not = 0; |
| 1579 | } else { |
| 1580 | last_not = 1; |
| 1581 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1582 | } else { /* and, or */ |
| 1583 | f_exp++; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1584 | /* not a not operation */ |
| 1585 | last_not = 0; |
| 1586 | } |
| 1587 | i += r; |
| 1588 | } else { |
| 1589 | f_size++; |
| 1590 | last_not = 0; |
| 1591 | } |
| 1592 | expr_size++; |
| 1593 | |
| 1594 | while (!isspace(c[i])) { |
| 1595 | if (!c[i] || c[i] == ')') { |
| 1596 | i--; |
| 1597 | break; |
| 1598 | } |
| 1599 | i++; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1600 | } |
| 1601 | } |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1602 | if (j || f_exp != f_size) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1603 | /* not matching count of ( and ) */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1604 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1605 | return EXIT_FAILURE; |
| 1606 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1607 | |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 1608 | if (checkversion || expr_size > 1) { |
| 1609 | /* check that we have 1.1 module */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 1610 | if (node->module->version != LYS_VERSION_1_1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1611 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 1612 | 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] | 1613 | return EXIT_FAILURE; |
| 1614 | } |
| 1615 | } |
| 1616 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1617 | /* allocate the memory */ |
| 1618 | 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] | 1619 | iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1620 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1621 | 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] | 1622 | stack.size = expr_size; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1623 | f_size--; expr_size--; /* used as indexes from now */ |
| 1624 | |
| 1625 | for (i--; i >= 0; i--) { |
| 1626 | if (c[i] == ')') { |
| 1627 | /* push it on stack */ |
| 1628 | iff_stack_push(&stack, LYS_IFF_RP); |
| 1629 | continue; |
| 1630 | } else if (c[i] == '(') { |
| 1631 | /* pop from the stack into result all operators until ) */ |
| 1632 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 1633 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1634 | } |
| 1635 | continue; |
| 1636 | } else if (isspace(c[i])) { |
| 1637 | continue; |
| 1638 | } |
| 1639 | |
| 1640 | /* end operator or operand -> find beginning and get what is it */ |
| 1641 | j = i + 1; |
| 1642 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 1643 | i--; |
| 1644 | } |
| 1645 | i++; /* get back by one step */ |
| 1646 | |
| 1647 | if (!strncmp(&c[i], "not ", 4)) { |
| 1648 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 1649 | /* double not */ |
| 1650 | iff_stack_pop(&stack); |
| 1651 | } else { |
| 1652 | /* not has the highest priority, so do not pop from the stack |
| 1653 | * as in case of AND and OR */ |
| 1654 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 1655 | } |
| 1656 | } else if (!strncmp(&c[i], "and ", 4)) { |
| 1657 | /* as for OR - pop from the stack all operators with the same or higher |
| 1658 | * priority and store them to the result, then push the AND to the stack */ |
| 1659 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 1660 | op = iff_stack_pop(&stack); |
| 1661 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1662 | } |
| 1663 | iff_stack_push(&stack, LYS_IFF_AND); |
| 1664 | } else if (!strncmp(&c[i], "or ", 3)) { |
| 1665 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 1666 | op = iff_stack_pop(&stack); |
| 1667 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1668 | } |
| 1669 | iff_stack_push(&stack, LYS_IFF_OR); |
| 1670 | } else { |
| 1671 | /* feature name, length is j - i */ |
| 1672 | |
| 1673 | /* add it to the result */ |
| 1674 | iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--); |
| 1675 | |
| 1676 | /* now get the link to the feature definition. Since it can be |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1677 | * forward referenced, we have to keep the feature name in auxiliary |
| 1678 | * structure passed into unres */ |
| 1679 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1680 | LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1681 | iff_data->node = node; |
| 1682 | iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1683 | iff_data->infeature = infeature; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 1684 | r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, |
| 1685 | (struct lys_node *)iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1686 | f_size--; |
| 1687 | |
| 1688 | if (r == -1) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 1689 | lydict_remove(node->module->ctx, iff_data->fname); |
Pavol Vican | 4d08451 | 2016-09-29 16:38:12 +0200 | [diff] [blame] | 1690 | free(iff_data); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1691 | goto error; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1692 | } |
| 1693 | } |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1694 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1695 | while (stack.index) { |
| 1696 | op = iff_stack_pop(&stack); |
| 1697 | iff_setop(iffeat_expr->expr, op, expr_size--); |
| 1698 | } |
| 1699 | |
| 1700 | if (++expr_size || ++f_size) { |
| 1701 | /* not all expected operators and operands found */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1702 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1703 | rc = EXIT_FAILURE; |
| 1704 | } else { |
| 1705 | rc = EXIT_SUCCESS; |
| 1706 | } |
| 1707 | |
| 1708 | error: |
| 1709 | /* cleanup */ |
| 1710 | iff_stack_clean(&stack); |
| 1711 | |
| 1712 | return rc; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1716 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 1717 | * |
| 1718 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 1719 | * module). |
| 1720 | * |
| 1721 | */ |
| 1722 | struct lyd_node * |
| 1723 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 1724 | { |
| 1725 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1726 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1727 | const struct lys_node *schema = NULL; |
| 1728 | |
| 1729 | assert(nodeid && start); |
| 1730 | |
| 1731 | if (nodeid[0] == '/') { |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | |
| 1735 | str = p = strdup(nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1736 | LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL); |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1737 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1738 | while (p) { |
| 1739 | token = p; |
| 1740 | p = strchr(p, '/'); |
| 1741 | if (p) { |
| 1742 | *p = '\0'; |
| 1743 | p++; |
| 1744 | } |
| 1745 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1746 | if (p) { |
| 1747 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1748 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1749 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1750 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1751 | result = NULL; |
| 1752 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1753 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1754 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1755 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1756 | continue; |
| 1757 | } |
| 1758 | } else { |
| 1759 | /* final node */ |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 1760 | 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] | 1761 | || !schema) { |
| 1762 | result = NULL; |
| 1763 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1764 | } |
| 1765 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1766 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1767 | if (iter->schema == schema) { |
| 1768 | /* move in data tree according to returned schema */ |
| 1769 | result = iter; |
| 1770 | break; |
| 1771 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1772 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1773 | if (!iter) { |
| 1774 | /* instance not found */ |
| 1775 | result = NULL; |
| 1776 | break; |
| 1777 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1778 | } |
| 1779 | free(str); |
| 1780 | |
| 1781 | return result; |
| 1782 | } |
| 1783 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 1784 | int |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1785 | schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name, |
| 1786 | int mod_name_len, const char *name, int nam_len) |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1787 | { |
| 1788 | const struct lys_module *prefix_mod; |
| 1789 | |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1790 | /* handle special names */ |
| 1791 | if (name[0] == '*') { |
| 1792 | return 2; |
| 1793 | } else if (name[0] == '.') { |
| 1794 | return 3; |
| 1795 | } |
| 1796 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1797 | /* name check */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1798 | if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1799 | return 1; |
| 1800 | } |
| 1801 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1802 | /* module check */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1803 | if (mod_name) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1804 | 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] | 1805 | if (!prefix_mod) { |
| 1806 | return -1; |
| 1807 | } |
| 1808 | } else { |
| 1809 | prefix_mod = cur_module; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1810 | } |
| 1811 | if (prefix_mod != lys_node_module(sibling)) { |
| 1812 | return 1; |
| 1813 | } |
| 1814 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1815 | /* match */ |
Michal Vasko | cdb3f06 | 2018-02-01 09:55:06 +0100 | [diff] [blame] | 1816 | return 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1819 | /* keys do not have to be ordered and do not have to be all of them */ |
| 1820 | static int |
| 1821 | resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node, |
| 1822 | const struct lys_module *cur_module, int *nodeid_end) |
| 1823 | { |
| 1824 | int mod_len, nam_len, has_predicate, r, i; |
| 1825 | const char *model, *name; |
| 1826 | struct lys_node_list *list; |
| 1827 | |
| 1828 | if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1829 | return 1; |
| 1830 | } |
| 1831 | |
| 1832 | list = (struct lys_node_list *)node; |
| 1833 | do { |
| 1834 | r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate); |
| 1835 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1836 | 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] | 1837 | return -1; |
| 1838 | } |
| 1839 | nodeid += r; |
| 1840 | |
| 1841 | if (node->nodetype == LYS_LEAFLIST) { |
| 1842 | /* just check syntax */ |
| 1843 | if (model || !name || (name[0] != '.') || has_predicate) { |
| 1844 | return 1; |
| 1845 | } |
| 1846 | break; |
| 1847 | } else { |
| 1848 | /* check the key */ |
| 1849 | for (i = 0; i < list->keys_size; ++i) { |
| 1850 | if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) { |
| 1851 | continue; |
| 1852 | } |
| 1853 | if (model) { |
| 1854 | if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len) |
| 1855 | || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) { |
| 1856 | continue; |
| 1857 | } |
| 1858 | } else { |
| 1859 | if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) { |
| 1860 | continue; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* match */ |
| 1865 | break; |
| 1866 | } |
| 1867 | |
| 1868 | if (i == list->keys_size) { |
| 1869 | return 1; |
| 1870 | } |
| 1871 | } |
| 1872 | } while (has_predicate); |
| 1873 | |
| 1874 | if (!nodeid[0]) { |
| 1875 | *nodeid_end = 1; |
| 1876 | } |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1880 | /* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1881 | */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1882 | int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1883 | 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] | 1884 | struct ly_set **ret, int extended, int no_node_error) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1885 | { |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1886 | 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] | 1887 | const struct lys_node *sibling, *next, *elem; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1888 | struct lys_node_augment *last_aug; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1889 | 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] | 1890 | int yang_data_name_len, backup_mod_name_len = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1891 | /* 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] | 1892 | const struct lys_module *start_mod, *aux_mod = NULL; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1893 | char *str; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1894 | struct ly_ctx *ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1895 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1896 | assert(nodeid && (start_parent || cur_module) && ret); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1897 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1898 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1899 | if (!cur_module) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1900 | cur_module = lys_node_module(start_parent); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1901 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1902 | ctx = cur_module->ctx; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1903 | id = nodeid; |
| 1904 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 1905 | 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] | 1906 | if (r < 1) { |
| 1907 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]); |
| 1908 | return -1; |
| 1909 | } |
| 1910 | |
| 1911 | if (name[0] == '#') { |
| 1912 | if (is_relative) { |
| 1913 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | yang_data_name = name + 1; |
| 1917 | yang_data_name_len = nam_len - 1; |
| 1918 | backup_mod_name = mod_name; |
| 1919 | backup_mod_name_len = mod_name_len; |
| 1920 | id += r; |
| 1921 | } else { |
| 1922 | is_relative = -1; |
| 1923 | } |
| 1924 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1925 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 1926 | (extended ? &all_desc : NULL), extended); |
| 1927 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1928 | 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] | 1929 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1930 | } |
| 1931 | id += r; |
| 1932 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1933 | if (backup_mod_name) { |
| 1934 | mod_name = backup_mod_name; |
| 1935 | mod_name_len = backup_mod_name_len; |
| 1936 | } |
| 1937 | |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1938 | if (is_relative && !start_parent) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1939 | 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] | 1940 | return -1; |
| 1941 | } |
| 1942 | |
| 1943 | /* descendant-schema-nodeid */ |
| 1944 | if (is_relative) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 1945 | cur_module = start_mod = lys_node_module(start_parent); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1946 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1947 | /* absolute-schema-nodeid */ |
| 1948 | } else { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1949 | 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] | 1950 | if (!start_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1951 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1952 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1953 | free(str); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1954 | return -1; |
| 1955 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 1956 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 1957 | if (yang_data_name) { |
| 1958 | start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len); |
| 1959 | if (!start_parent) { |
| 1960 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 1961 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1962 | free(str); |
| 1963 | return -1; |
| 1964 | } |
| 1965 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | while (1) { |
| 1969 | sibling = NULL; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1970 | last_aug = NULL; |
| 1971 | |
| 1972 | if (start_parent) { |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1973 | if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len) |
| 1974 | || (mod_name_len != (signed)strlen(cur_module->name)))) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1975 | /* we are getting into another module (augment) */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 1976 | 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] | 1977 | if (!aux_mod) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1978 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1979 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1980 | free(str); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1981 | return -1; |
| 1982 | } |
| 1983 | } else { |
Michal Vasko | 201c339 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1984 | /* there is no mod_name, so why are we checking augments again? |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1985 | * because this module may be not implemented and it augments something in another module and |
| 1986 | * there is another augment augmenting that previous one */ |
Michal Vasko | 1731577 | 2017-07-10 15:15:39 +0200 | [diff] [blame] | 1987 | aux_mod = cur_module; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* if the module is implemented, all the augments will be connected */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1991 | if (!aux_mod->implemented && !extended) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 1992 | get_next_augment: |
| 1993 | last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent); |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | 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] | 1998 | 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] | 1999 | r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len); |
| 2000 | |
| 2001 | /* resolve predicate */ |
| 2002 | if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) { |
| 2003 | r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end); |
| 2004 | if (r == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2005 | continue; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2006 | } else if (r == -1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2007 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2008 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2009 | } else if (!id[0]) { |
| 2010 | nodeid_end = 1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2011 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2012 | |
| 2013 | if (r == 0) { |
| 2014 | /* one matching result */ |
| 2015 | if (nodeid_end) { |
| 2016 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2017 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2018 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2019 | } else { |
| 2020 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 2021 | return -1; |
| 2022 | } |
| 2023 | start_parent = sibling; |
| 2024 | } |
| 2025 | break; |
| 2026 | } else if (r == 1) { |
| 2027 | continue; |
| 2028 | } else if (r == 2) { |
| 2029 | /* "*" */ |
| 2030 | if (!*ret) { |
| 2031 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2032 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2033 | } |
| 2034 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2035 | if (all_desc) { |
| 2036 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 2037 | if (elem != sibling) { |
| 2038 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 2039 | } |
| 2040 | |
| 2041 | LY_TREE_DFS_END(sibling, next, elem); |
| 2042 | } |
| 2043 | } |
| 2044 | } else if (r == 3) { |
| 2045 | /* "." */ |
| 2046 | if (!*ret) { |
| 2047 | *ret = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2048 | LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2049 | ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST); |
| 2050 | } |
| 2051 | ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST); |
| 2052 | if (all_desc) { |
| 2053 | LY_TREE_DFS_BEGIN(sibling, next, elem) { |
| 2054 | if (elem != sibling) { |
| 2055 | ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST); |
| 2056 | } |
| 2057 | |
| 2058 | LY_TREE_DFS_END(sibling, next, elem); |
| 2059 | } |
| 2060 | } |
| 2061 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2062 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2063 | return -1; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* skip predicate */ |
| 2068 | if (extended && has_predicate) { |
| 2069 | while (id[0] == '[') { |
| 2070 | id = strchr(id, ']'); |
| 2071 | if (!id) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2072 | LOGINT(ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2073 | return -1; |
| 2074 | } |
| 2075 | ++id; |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) { |
| 2080 | return EXIT_SUCCESS; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | /* no match */ |
| 2084 | if (!sibling) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 2085 | if (last_aug) { |
| 2086 | /* it still could be in another augment */ |
| 2087 | goto get_next_augment; |
| 2088 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2089 | if (no_node_error) { |
| 2090 | str = strndup(nodeid, (name - nodeid) + nam_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2091 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2092 | free(str); |
| 2093 | return -1; |
| 2094 | } |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2095 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2096 | return EXIT_SUCCESS; |
| 2097 | } |
| 2098 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2099 | r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, |
| 2100 | (extended ? &all_desc : NULL), extended); |
| 2101 | if (r < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2102 | 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] | 2103 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2104 | } |
| 2105 | id += r; |
| 2106 | } |
| 2107 | |
| 2108 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2109 | LOGINT(ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2110 | return -1; |
| 2111 | } |
| 2112 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2113 | /* unique, refine, |
| 2114 | * >0 - unexpected char on position (ret - 1), |
| 2115 | * 0 - ok (but ret can still be NULL), |
| 2116 | * -1 - error, |
| 2117 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2118 | int |
| 2119 | 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] | 2120 | int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2121 | { |
| 2122 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2123 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2124 | int r, nam_len, mod_name_len, is_relative = -1; |
| 2125 | /* 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] | 2126 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2127 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2128 | assert(nodeid && ret); |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 2129 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING))); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2130 | |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 2131 | if (!start) { |
| 2132 | /* leaf not found */ |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2136 | id = nodeid; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2137 | module = lys_node_module(start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2138 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2139 | 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] | 2140 | return ((id - nodeid) - r) + 1; |
| 2141 | } |
| 2142 | id += r; |
| 2143 | |
| 2144 | if (!is_relative) { |
| 2145 | return -1; |
| 2146 | } |
| 2147 | |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2148 | start_parent = lys_parent(start); |
Michal Vasko | 74a991b | 2017-03-31 09:17:22 +0200 | [diff] [blame] | 2149 | while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) { |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2150 | start_parent = lys_parent(start_parent); |
| 2151 | } |
| 2152 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2153 | while (1) { |
| 2154 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2155 | while ((sibling = lys_getnext(sibling, start_parent, module, |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 2156 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2157 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2158 | if (r == 0) { |
| 2159 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2160 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2161 | /* wrong node type, too bad */ |
| 2162 | continue; |
| 2163 | } |
| 2164 | *ret = sibling; |
| 2165 | return EXIT_SUCCESS; |
| 2166 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2167 | start_parent = sibling; |
| 2168 | break; |
| 2169 | } else if (r == 1) { |
| 2170 | continue; |
| 2171 | } else { |
| 2172 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | /* no match */ |
| 2177 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2178 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2179 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2180 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 2181 | *ret = NULL; |
| 2182 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2185 | 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] | 2186 | return ((id - nodeid) - r) + 1; |
| 2187 | } |
| 2188 | id += r; |
| 2189 | } |
| 2190 | |
| 2191 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2192 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2193 | return -1; |
| 2194 | } |
| 2195 | |
| 2196 | /* choice default */ |
| 2197 | int |
| 2198 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 2199 | { |
| 2200 | /* cannot actually be a path */ |
| 2201 | if (strchr(nodeid, '/')) { |
| 2202 | return -1; |
| 2203 | } |
| 2204 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2205 | 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] | 2206 | } |
| 2207 | |
| 2208 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 2209 | static int |
| 2210 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 2211 | { |
| 2212 | const struct lys_module *module; |
| 2213 | const char *mod_prefix, *name; |
| 2214 | int i, mod_prefix_len, nam_len; |
| 2215 | |
| 2216 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2217 | 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] | 2218 | return -i + 1; |
| 2219 | } |
| 2220 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2221 | 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] | 2222 | if (!module) { |
| 2223 | return -1; |
| 2224 | } |
Radek Krejci | 0a8205d | 2017-03-01 16:25:29 +0100 | [diff] [blame] | 2225 | if (module != lys_main_module(start->module)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2226 | start = module->data; |
| 2227 | } |
| 2228 | |
| 2229 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 2230 | |
| 2231 | return EXIT_SUCCESS; |
| 2232 | } |
| 2233 | |
| 2234 | int |
| 2235 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 2236 | const struct lys_node **ret) |
| 2237 | { |
| 2238 | const char *name, *mod_name, *id; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2239 | const struct lys_node *sibling, *start_parent; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2240 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 2241 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2242 | |
| 2243 | assert(nodeid && module && ret); |
| 2244 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 2245 | |
| 2246 | id = nodeid; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2247 | start_parent = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2248 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2249 | 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] | 2250 | return ((id - nodeid) - r) + 1; |
| 2251 | } |
| 2252 | id += r; |
| 2253 | |
| 2254 | if (is_relative) { |
| 2255 | return -1; |
| 2256 | } |
| 2257 | |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 2258 | 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] | 2259 | if (!abs_start_mod) { |
| 2260 | return -1; |
| 2261 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2262 | |
| 2263 | while (1) { |
| 2264 | sibling = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2265 | 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] | 2266 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2267 | r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len); |
| 2268 | if (r == 0) { |
| 2269 | if (!id[0]) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2270 | if (!(sibling->nodetype & ret_nodetype)) { |
| 2271 | /* wrong node type, too bad */ |
| 2272 | continue; |
| 2273 | } |
| 2274 | *ret = sibling; |
| 2275 | return EXIT_SUCCESS; |
| 2276 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2277 | start_parent = sibling; |
| 2278 | break; |
| 2279 | } else if (r == 1) { |
| 2280 | continue; |
| 2281 | } else { |
| 2282 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* no match */ |
| 2287 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 2288 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2289 | return EXIT_SUCCESS; |
| 2290 | } |
| 2291 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2292 | 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] | 2293 | return ((id - nodeid) - r) + 1; |
| 2294 | } |
| 2295 | id += r; |
| 2296 | } |
| 2297 | |
| 2298 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2299 | LOGINT(module->ctx); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2300 | return -1; |
| 2301 | } |
| 2302 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2303 | static int |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2304 | 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] | 2305 | { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2306 | const char *mod_name, *name; |
| 2307 | int mod_name_len, nam_len, has_predicate, i; |
| 2308 | struct lys_node *key; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2309 | |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2310 | 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] | 2311 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2312 | 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] | 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | predicate += i; |
| 2317 | *parsed += i; |
| 2318 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2319 | if (!isdigit(name[0])) { |
| 2320 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2321 | key = (struct lys_node *)list->keys[i]; |
| 2322 | if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2323 | break; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2324 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2325 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2326 | |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2327 | if (i == list->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2328 | LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2329 | return -1; |
| 2330 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* more predicates? */ |
| 2334 | if (has_predicate) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2335 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 2341 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2342 | const struct lys_node * |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 2343 | 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] | 2344 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2345 | char *str; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2346 | 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] | 2347 | const struct lys_node *sibling, *start_parent, *parent; |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2348 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2349 | int yang_data_name_len, backup_mod_name_len; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2350 | /* 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] | 2351 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2352 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2353 | assert(nodeid && (ctx || start)); |
| 2354 | if (!ctx) { |
| 2355 | ctx = start->module->ctx; |
| 2356 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2357 | |
| 2358 | id = nodeid; |
| 2359 | |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2360 | 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] | 2361 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 2362 | return NULL; |
| 2363 | } |
| 2364 | |
| 2365 | if (name[0] == '#') { |
| 2366 | if (is_relative) { |
| 2367 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | yang_data_name = name + 1; |
| 2371 | yang_data_name_len = nam_len - 1; |
| 2372 | backup_mod_name = mod_name; |
| 2373 | backup_mod_name_len = mod_name_len; |
| 2374 | id += r; |
| 2375 | } else { |
| 2376 | is_relative = -1; |
| 2377 | } |
| 2378 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2379 | 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] | 2380 | 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] | 2381 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2382 | } |
| 2383 | id += r; |
| 2384 | |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2385 | if (backup_mod_name) { |
| 2386 | mod_name = backup_mod_name; |
| 2387 | mod_name_len = backup_mod_name_len; |
| 2388 | } |
| 2389 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2390 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2391 | assert(start); |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2392 | start_parent = start; |
| 2393 | while (start_parent && (start_parent->nodetype == LYS_USES)) { |
| 2394 | start_parent = lys_parent(start_parent); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2395 | } |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2396 | module = start->module; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2397 | } else { |
| 2398 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2399 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2400 | LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2401 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2402 | return NULL; |
| 2403 | } |
| 2404 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2405 | str = strndup(mod_name, mod_name_len); |
| 2406 | module = ly_ctx_get_module(ctx, str, NULL, 1); |
| 2407 | free(str); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2408 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2409 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2410 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2411 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2412 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2413 | return NULL; |
| 2414 | } |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 2415 | start_parent = NULL; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2416 | if (yang_data_name) { |
| 2417 | start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len); |
| 2418 | if (!start_parent) { |
| 2419 | str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid); |
| 2420 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 2421 | free(str); |
| 2422 | return NULL; |
| 2423 | } |
| 2424 | } |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 2425 | |
| 2426 | /* now it's as if there was no module name */ |
| 2427 | mod_name = NULL; |
| 2428 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2431 | prev_mod = module; |
| 2432 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2433 | while (1) { |
| 2434 | sibling = NULL; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2435 | while ((sibling = lys_getnext(sibling, start_parent, module, 0))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2436 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2437 | 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] | 2438 | /* output check */ |
| 2439 | for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2440 | if (parent) { |
| 2441 | if (output && (parent->nodetype == LYS_INPUT)) { |
| 2442 | continue; |
| 2443 | } else if (!output && (parent->nodetype == LYS_OUTPUT)) { |
| 2444 | continue; |
| 2445 | } |
| 2446 | } |
| 2447 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2448 | /* module check */ |
| 2449 | if (mod_name) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 2450 | /* will also find an augment module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2451 | 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] | 2452 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2453 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2454 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2455 | LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2456 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2457 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2458 | } |
| 2459 | } else { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2460 | prefix_mod = prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2461 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 2462 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2463 | continue; |
| 2464 | } |
| 2465 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2466 | /* do we have some predicates on it? */ |
| 2467 | if (has_predicate) { |
| 2468 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 2469 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2470 | 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] | 2471 | 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] | 2472 | return NULL; |
| 2473 | } |
| 2474 | } else if (sibling->nodetype == LYS_LIST) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2475 | 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] | 2476 | return NULL; |
| 2477 | } |
| 2478 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2479 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2480 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2481 | } |
| 2482 | id += r; |
| 2483 | } |
| 2484 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2485 | /* the result node? */ |
| 2486 | if (!id[0]) { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2487 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2490 | /* move down the tree, if possible */ |
| 2491 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2492 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2493 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2494 | } |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 2495 | start_parent = sibling; |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2496 | |
| 2497 | /* update prev mod */ |
| 2498 | prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2499 | break; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /* no match */ |
| 2504 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2505 | str = strndup(nodeid, (name + nam_len) - nodeid); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2506 | LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 2507 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2508 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2509 | } |
| 2510 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2511 | 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] | 2512 | 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] | 2513 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2514 | } |
| 2515 | id += r; |
| 2516 | } |
| 2517 | |
| 2518 | /* cannot get here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2519 | LOGINT(ctx); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 2520 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2523 | static int |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2524 | 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] | 2525 | { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2526 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2527 | struct lyd_node_leaf_list *key; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2528 | struct lys_node_list *slist; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2529 | struct ly_ctx *ctx; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2530 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 2531 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2532 | assert(node->schema->nodetype == LYS_LIST); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2533 | assert(pp.len); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2534 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2535 | ctx = node->schema->module->ctx; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2536 | slist = (struct lys_node_list *)node->schema; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2537 | |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2538 | /* is the predicate a number? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2539 | if (isdigit(pp.pred[0].name[0])) { |
| 2540 | if (position == atoi(pp.pred[0].name)) { |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2541 | /* match */ |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2542 | return 0; |
| 2543 | } else { |
| 2544 | /* not a match */ |
| 2545 | return 1; |
| 2546 | } |
| 2547 | } |
| 2548 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2549 | key = (struct lyd_node_leaf_list *)node->child; |
Michal Vasko | 53adfc7 | 2017-01-06 10:39:10 +0100 | [diff] [blame] | 2550 | if (!key) { |
| 2551 | /* it is not a position, so we need a key for it to be a match */ |
| 2552 | return 1; |
| 2553 | } |
| 2554 | |
| 2555 | /* go through all the keys */ |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2556 | for (i = 0; i < slist->keys_size; ++i) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2557 | if (strncmp(key->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || key->schema->name[pp.pred[i].nam_len]) { |
| 2558 | 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] | 2559 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2560 | } |
| 2561 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2562 | if (pp.pred[i].mod_name) { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2563 | /* specific module, check that the found key is from that module */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2564 | if (strncmp(lyd_node_module((struct lyd_node *)key)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len) |
| 2565 | || lyd_node_module((struct lyd_node *)key)->name[pp.pred[i].mod_name_len]) { |
| 2566 | 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] | 2567 | return -1; |
| 2568 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2569 | |
| 2570 | /* but if the module is the same as the parent, it should have been omitted */ |
| 2571 | if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2572 | 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] | 2573 | return -1; |
| 2574 | } |
Michal Vasko | 9fbb6e8 | 2017-07-04 13:50:04 +0200 | [diff] [blame] | 2575 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2576 | /* no module, so it must be the same as the list (parent) */ |
| 2577 | if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2578 | 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] | 2579 | return -1; |
| 2580 | } |
| 2581 | } |
| 2582 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2583 | /* value does not match */ |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2584 | 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] | 2585 | return 1; |
| 2586 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 2587 | |
| 2588 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2591 | return 0; |
| 2592 | } |
| 2593 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 2594 | /** |
| 2595 | * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path) |
| 2596 | * |
| 2597 | * @param[in] nodeid Node data path to find |
| 2598 | * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance. |
| 2599 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 2600 | * @param[out] parsed Number of characters processed in \p id |
| 2601 | * @return The closes parent (or the node itself) from the path |
| 2602 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2603 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2604 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 2605 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2606 | { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2607 | const char *id, *mod_name, *name, *data_val, *llval; |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2608 | 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] | 2609 | int has_predicate, last_parsed = 0, llval_len; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2610 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2611 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2612 | const struct lys_module *prev_mod; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2613 | struct ly_ctx *ctx; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2614 | const struct lys_node *ssibling; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2615 | struct lys_node_list *slist; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2616 | struct parsed_pred pp; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2617 | |
| 2618 | assert(nodeid && start && parsed); |
| 2619 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2620 | memset(&pp, 0, sizeof pp); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2621 | ctx = start->schema->module->ctx; |
| 2622 | id = nodeid; |
| 2623 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2624 | /* parse first nodeid in case it is yang-data extension */ |
PavolVican | 195cf39 | 2018-02-23 13:24:45 +0100 | [diff] [blame] | 2625 | 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] | 2626 | 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] | 2627 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | if (name[0] == '#') { |
| 2631 | if (is_relative) { |
| 2632 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name); |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2633 | goto error; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2634 | } |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2635 | id += r; |
| 2636 | last_parsed = r; |
| 2637 | } else { |
| 2638 | is_relative = -1; |
| 2639 | } |
| 2640 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2641 | /* parse first nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2642 | 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] | 2643 | 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] | 2644 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2645 | } |
| 2646 | id += r; |
| 2647 | /* add it to parsed only after the data node was actually found */ |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 2648 | last_parsed += r; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2649 | |
| 2650 | if (is_relative) { |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2651 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2652 | start = start->child; |
| 2653 | } else { |
| 2654 | for (; start->parent; start = start->parent); |
Michal Vasko | f68a49e | 2017-08-14 13:23:37 +0200 | [diff] [blame] | 2655 | prev_mod = lyd_node_module(start); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2658 | /* do not duplicate code, use predicate parsing from the loop */ |
| 2659 | goto parse_predicates; |
| 2660 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2661 | while (1) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2662 | /* find the correct schema node first */ |
| 2663 | ssibling = NULL; |
| 2664 | while ((ssibling = lys_getnext(ssibling, (start && start->parent) ? start->parent->schema : NULL, prev_mod, 0))) { |
| 2665 | if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) { |
| 2666 | break; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2667 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2668 | } |
| 2669 | if (!ssibling) { |
| 2670 | /* there is not even such a schema node */ |
| 2671 | free(pp.pred); |
| 2672 | return last_match; |
| 2673 | } |
| 2674 | pp.schema = ssibling; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2675 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2676 | /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if |
| 2677 | * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */ |
| 2678 | if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) { |
| 2679 | pp.len = 1; |
| 2680 | pp.pred = calloc(1, sizeof *pp.pred); |
| 2681 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2682 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2683 | pp.pred[0].name = "."; |
| 2684 | pp.pred[0].nam_len = 1; |
| 2685 | pp.pred[0].value = (llist_value ? llist_value : ""); |
| 2686 | pp.pred[0].val_len = strlen(pp.pred[0].value); |
| 2687 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2688 | |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2689 | if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) { |
| 2690 | /* check leaf/leaf-list predicate */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2691 | if (pp.len > 1) { |
| 2692 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2693 | goto error; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2694 | } else if (pp.len) { |
| 2695 | if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) { |
| 2696 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name); |
| 2697 | goto error; |
| 2698 | } |
| 2699 | if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) { |
| 2700 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value); |
| 2701 | goto error; |
| 2702 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2703 | } |
| 2704 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2705 | /* list should have predicates for all the keys or position */ |
| 2706 | slist = (struct lys_node_list *)ssibling; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2707 | if (!pp.len) { |
| 2708 | /* none match */ |
| 2709 | return last_match; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2710 | } else if (!isdigit(pp.pred[0].name[0])) { |
| 2711 | /* list predicate is not a position, so there must be all the keys */ |
| 2712 | if (pp.len > slist->keys_size) { |
| 2713 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2714 | goto error; |
| 2715 | } else if (pp.len < slist->keys_size) { |
| 2716 | LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name); |
| 2717 | goto error; |
| 2718 | } |
| 2719 | /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */ |
| 2720 | for (r = 0; r < pp.len; ++r) { |
| 2721 | if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) { |
| 2722 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value); |
| 2723 | goto error; |
| 2724 | } |
| 2725 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2726 | } |
| 2727 | } else if (pp.pred) { |
| 2728 | /* no other nodes allow predicates */ |
| 2729 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2730 | goto error; |
| 2731 | } |
| 2732 | |
| 2733 | #ifdef LY_ENABLED_CACHE |
| 2734 | /* we will not be matching keyless lists or state leaf-lists this way */ |
| 2735 | if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size) |
| 2736 | && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) { |
| 2737 | sibling = resolve_json_data_node_hash(start->parent, pp); |
| 2738 | } else |
| 2739 | #endif |
| 2740 | { |
| 2741 | list_instance_position = 0; |
| 2742 | LY_TREE_FOR(start, sibling) { |
| 2743 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
| 2744 | if (lys_parent(sibling->schema)) { |
| 2745 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2746 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
| 2747 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
| 2748 | goto error; |
| 2749 | } |
| 2750 | } else { |
| 2751 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
| 2752 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
| 2753 | goto error; |
| 2754 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2755 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2756 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2757 | |
| 2758 | if (sibling->schema != ssibling) { |
| 2759 | /* wrong schema node */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2760 | continue; |
| 2761 | } |
| 2762 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2763 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2764 | if (ssibling->nodetype == LYS_LEAFLIST) { |
| 2765 | if (ssibling->flags & LYS_CONFIG_R) { |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 2766 | /* state leaf-lists will never match */ |
| 2767 | continue; |
| 2768 | } |
| 2769 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2770 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2771 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2772 | /* get the expected leaf-list value */ |
| 2773 | llval = NULL; |
| 2774 | llval_len = 0; |
| 2775 | if (pp.pred) { |
| 2776 | /* it was already checked that it is correct */ |
| 2777 | llval = pp.pred[0].value; |
| 2778 | llval_len = pp.pred[0].val_len; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2779 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
Michal Vasko | 21b90ce | 2017-09-19 09:38:27 +0200 | [diff] [blame] | 2782 | /* 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] | 2783 | if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2784 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2785 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2786 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2787 | } else { |
| 2788 | data_val = llist->value_str; |
| 2789 | } |
| 2790 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2791 | if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len) |
| 2792 | || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2793 | continue; |
| 2794 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2795 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2796 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2797 | /* 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] | 2798 | ++list_instance_position; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2799 | ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2800 | if (ret == -1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2801 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2802 | } else if (ret == 1) { |
| 2803 | /* this list instance does not match */ |
| 2804 | continue; |
| 2805 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2806 | } |
| 2807 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2808 | break; |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | /* no match, return last match */ |
| 2813 | if (!sibling) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2814 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2815 | return last_match; |
| 2816 | } |
| 2817 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2818 | /* we found a next matching node */ |
| 2819 | *parsed += last_parsed; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2820 | last_match = sibling; |
| 2821 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2822 | |
| 2823 | /* the result node? */ |
| 2824 | if (!id[0]) { |
| 2825 | free(pp.pred); |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2826 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2829 | /* move down the tree, if possible, and continue */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2830 | if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2831 | /* there can be no children even through expected, error */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2832 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2833 | goto error; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2834 | } else if (!sibling->child) { |
| 2835 | /* there could be some children, but are not, return what we found so far */ |
| 2836 | free(pp.pred); |
| 2837 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2838 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2839 | start = sibling->child; |
| 2840 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2841 | /* parse nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2842 | 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] | 2843 | 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] | 2844 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2845 | } |
| 2846 | id += r; |
| 2847 | last_parsed = r; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2848 | |
| 2849 | parse_predicates: |
| 2850 | /* parse all the predicates */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2851 | free(pp.pred); |
| 2852 | pp.schema = NULL; |
| 2853 | pp.len = 0; |
| 2854 | pp.pred = NULL; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2855 | while (has_predicate) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2856 | ++pp.len; |
| 2857 | pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred); |
| 2858 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
| 2859 | if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len, |
| 2860 | &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value, |
| 2861 | &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2862 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2863 | goto error; |
| 2864 | } |
| 2865 | |
| 2866 | id += r; |
| 2867 | last_parsed += r; |
| 2868 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2871 | error: |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2872 | *parsed = -1; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2873 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2874 | return NULL; |
| 2875 | } |
| 2876 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2877 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2878 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2879 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2880 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2881 | * @param[in] ctx Context for errors. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2882 | * @param[in] str_restr Restriction as a string. |
| 2883 | * @param[in] type Type of the restriction. |
| 2884 | * @param[out] ret Final interval structure that starts with |
| 2885 | * the interval of the initial type, continues with intervals |
| 2886 | * of any superior types derived from the initial one, and |
| 2887 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2888 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2889 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2890 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2891 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2892 | 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] | 2893 | { |
| 2894 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2895 | int kind; |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 2896 | int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax; |
| 2897 | uint64_t local_umin, local_umax = 0; |
| 2898 | uint8_t local_fdig = 0; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2899 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2900 | 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] | 2901 | |
| 2902 | switch (type->base) { |
| 2903 | case LY_TYPE_BINARY: |
| 2904 | kind = 0; |
| 2905 | local_umin = 0; |
| 2906 | local_umax = 18446744073709551615UL; |
| 2907 | |
| 2908 | if (!str_restr && type->info.binary.length) { |
| 2909 | str_restr = type->info.binary.length->expr; |
| 2910 | } |
| 2911 | break; |
| 2912 | case LY_TYPE_DEC64: |
| 2913 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2914 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2915 | local_fmax = __INT64_C(9223372036854775807); |
| 2916 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2917 | |
| 2918 | if (!str_restr && type->info.dec64.range) { |
| 2919 | str_restr = type->info.dec64.range->expr; |
| 2920 | } |
| 2921 | break; |
| 2922 | case LY_TYPE_INT8: |
| 2923 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2924 | local_smin = __INT64_C(-128); |
| 2925 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2926 | |
| 2927 | if (!str_restr && type->info.num.range) { |
| 2928 | str_restr = type->info.num.range->expr; |
| 2929 | } |
| 2930 | break; |
| 2931 | case LY_TYPE_INT16: |
| 2932 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2933 | local_smin = __INT64_C(-32768); |
| 2934 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2935 | |
| 2936 | if (!str_restr && type->info.num.range) { |
| 2937 | str_restr = type->info.num.range->expr; |
| 2938 | } |
| 2939 | break; |
| 2940 | case LY_TYPE_INT32: |
| 2941 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2942 | local_smin = __INT64_C(-2147483648); |
| 2943 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2944 | |
| 2945 | if (!str_restr && type->info.num.range) { |
| 2946 | str_restr = type->info.num.range->expr; |
| 2947 | } |
| 2948 | break; |
| 2949 | case LY_TYPE_INT64: |
| 2950 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2951 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2952 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2953 | |
| 2954 | if (!str_restr && type->info.num.range) { |
| 2955 | str_restr = type->info.num.range->expr; |
| 2956 | } |
| 2957 | break; |
| 2958 | case LY_TYPE_UINT8: |
| 2959 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2960 | local_umin = __UINT64_C(0); |
| 2961 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2962 | |
| 2963 | if (!str_restr && type->info.num.range) { |
| 2964 | str_restr = type->info.num.range->expr; |
| 2965 | } |
| 2966 | break; |
| 2967 | case LY_TYPE_UINT16: |
| 2968 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2969 | local_umin = __UINT64_C(0); |
| 2970 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2971 | |
| 2972 | if (!str_restr && type->info.num.range) { |
| 2973 | str_restr = type->info.num.range->expr; |
| 2974 | } |
| 2975 | break; |
| 2976 | case LY_TYPE_UINT32: |
| 2977 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2978 | local_umin = __UINT64_C(0); |
| 2979 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2980 | |
| 2981 | if (!str_restr && type->info.num.range) { |
| 2982 | str_restr = type->info.num.range->expr; |
| 2983 | } |
| 2984 | break; |
| 2985 | case LY_TYPE_UINT64: |
| 2986 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2987 | local_umin = __UINT64_C(0); |
| 2988 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2989 | |
| 2990 | if (!str_restr && type->info.num.range) { |
| 2991 | str_restr = type->info.num.range->expr; |
| 2992 | } |
| 2993 | break; |
| 2994 | case LY_TYPE_STRING: |
| 2995 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2996 | local_umin = __UINT64_C(0); |
| 2997 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2998 | |
| 2999 | if (!str_restr && type->info.str.length) { |
| 3000 | str_restr = type->info.str.length->expr; |
| 3001 | } |
| 3002 | break; |
| 3003 | default: |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3004 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3008 | if (type->der) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3009 | if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3010 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 3011 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3012 | assert(!intv || (intv->kind == kind)); |
| 3013 | } |
| 3014 | |
| 3015 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3016 | /* we do not have any restriction, return superior ones */ |
| 3017 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3018 | return EXIT_SUCCESS; |
| 3019 | } |
| 3020 | |
| 3021 | /* adjust local min and max */ |
| 3022 | if (intv) { |
| 3023 | tmp_intv = intv; |
| 3024 | |
| 3025 | if (kind == 0) { |
| 3026 | local_umin = tmp_intv->value.uval.min; |
| 3027 | } else if (kind == 1) { |
| 3028 | local_smin = tmp_intv->value.sval.min; |
| 3029 | } else if (kind == 2) { |
| 3030 | local_fmin = tmp_intv->value.fval.min; |
| 3031 | } |
| 3032 | |
| 3033 | while (tmp_intv->next) { |
| 3034 | tmp_intv = tmp_intv->next; |
| 3035 | } |
| 3036 | |
| 3037 | if (kind == 0) { |
| 3038 | local_umax = tmp_intv->value.uval.max; |
| 3039 | } else if (kind == 1) { |
| 3040 | local_smax = tmp_intv->value.sval.max; |
| 3041 | } else if (kind == 2) { |
| 3042 | local_fmax = tmp_intv->value.fval.max; |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | /* finally parse our restriction */ |
| 3047 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3048 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3049 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3050 | if (!tmp_local_intv) { |
| 3051 | assert(!local_intv); |
| 3052 | local_intv = malloc(sizeof *local_intv); |
| 3053 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3054 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3055 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3056 | tmp_local_intv = tmp_local_intv->next; |
| 3057 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3058 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3059 | |
| 3060 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3061 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3062 | tmp_local_intv->next = NULL; |
| 3063 | |
| 3064 | /* min */ |
| 3065 | ptr = seg_ptr; |
| 3066 | while (isspace(ptr[0])) { |
| 3067 | ++ptr; |
| 3068 | } |
| 3069 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3070 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3071 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3072 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3073 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3074 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3075 | 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] | 3076 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3077 | goto error; |
| 3078 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3079 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3080 | } else if (!strncmp(ptr, "min", 3)) { |
| 3081 | if (kind == 0) { |
| 3082 | tmp_local_intv->value.uval.min = local_umin; |
| 3083 | } else if (kind == 1) { |
| 3084 | tmp_local_intv->value.sval.min = local_smin; |
| 3085 | } else if (kind == 2) { |
| 3086 | tmp_local_intv->value.fval.min = local_fmin; |
| 3087 | } |
| 3088 | |
| 3089 | ptr += 3; |
| 3090 | } else if (!strncmp(ptr, "max", 3)) { |
| 3091 | if (kind == 0) { |
| 3092 | tmp_local_intv->value.uval.min = local_umax; |
| 3093 | } else if (kind == 1) { |
| 3094 | tmp_local_intv->value.sval.min = local_smax; |
| 3095 | } else if (kind == 2) { |
| 3096 | tmp_local_intv->value.fval.min = local_fmax; |
| 3097 | } |
| 3098 | |
| 3099 | ptr += 3; |
| 3100 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3101 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | while (isspace(ptr[0])) { |
| 3105 | ptr++; |
| 3106 | } |
| 3107 | |
| 3108 | /* no interval or interval */ |
| 3109 | if ((ptr[0] == '|') || !ptr[0]) { |
| 3110 | if (kind == 0) { |
| 3111 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 3112 | } else if (kind == 1) { |
| 3113 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 3114 | } else if (kind == 2) { |
| 3115 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 3116 | } |
| 3117 | } else if (!strncmp(ptr, "..", 2)) { |
| 3118 | /* skip ".." */ |
| 3119 | ptr += 2; |
| 3120 | while (isspace(ptr[0])) { |
| 3121 | ++ptr; |
| 3122 | } |
| 3123 | |
| 3124 | /* max */ |
| 3125 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3126 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3127 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3128 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3129 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3130 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3131 | 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] | 3132 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3133 | goto error; |
| 3134 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3135 | } |
| 3136 | } else if (!strncmp(ptr, "max", 3)) { |
| 3137 | if (kind == 0) { |
| 3138 | tmp_local_intv->value.uval.max = local_umax; |
| 3139 | } else if (kind == 1) { |
| 3140 | tmp_local_intv->value.sval.max = local_smax; |
| 3141 | } else if (kind == 2) { |
| 3142 | tmp_local_intv->value.fval.max = local_fmax; |
| 3143 | } |
| 3144 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3145 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3146 | } |
| 3147 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3148 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3151 | /* check min and max in correct order*/ |
| 3152 | if (kind == 0) { |
| 3153 | /* current segment */ |
| 3154 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 3155 | goto error; |
| 3156 | } |
| 3157 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 3158 | goto error; |
| 3159 | } |
| 3160 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3161 | 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] | 3162 | goto error; |
| 3163 | } |
| 3164 | } else if (kind == 1) { |
| 3165 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 3166 | goto error; |
| 3167 | } |
| 3168 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 3169 | goto error; |
| 3170 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3171 | 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] | 3172 | goto error; |
| 3173 | } |
| 3174 | } else if (kind == 2) { |
| 3175 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 3176 | goto error; |
| 3177 | } |
| 3178 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 3179 | goto error; |
| 3180 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3181 | 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] | 3182 | /* 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] | 3183 | goto error; |
| 3184 | } |
| 3185 | } |
| 3186 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3187 | /* next segment (next OR) */ |
| 3188 | seg_ptr = strchr(seg_ptr, '|'); |
| 3189 | if (!seg_ptr) { |
| 3190 | break; |
| 3191 | } |
| 3192 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3193 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* check local restrictions against superior ones */ |
| 3197 | if (intv) { |
| 3198 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3199 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3200 | |
| 3201 | while (tmp_local_intv && tmp_intv) { |
| 3202 | /* reuse local variables */ |
| 3203 | if (kind == 0) { |
| 3204 | local_umin = tmp_local_intv->value.uval.min; |
| 3205 | local_umax = tmp_local_intv->value.uval.max; |
| 3206 | |
| 3207 | /* it must be in this interval */ |
| 3208 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 3209 | /* this interval is covered, next one */ |
| 3210 | if (local_umax <= tmp_intv->value.uval.max) { |
| 3211 | tmp_local_intv = tmp_local_intv->next; |
| 3212 | continue; |
| 3213 | /* ascending order of restrictions -> fail */ |
| 3214 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3215 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | } else if (kind == 1) { |
| 3219 | local_smin = tmp_local_intv->value.sval.min; |
| 3220 | local_smax = tmp_local_intv->value.sval.max; |
| 3221 | |
| 3222 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 3223 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3224 | tmp_local_intv = tmp_local_intv->next; |
| 3225 | continue; |
| 3226 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3227 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3228 | } |
| 3229 | } |
| 3230 | } else if (kind == 2) { |
| 3231 | local_fmin = tmp_local_intv->value.fval.min; |
| 3232 | local_fmax = tmp_local_intv->value.fval.max; |
| 3233 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3234 | 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] | 3235 | && (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] | 3236 | 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] | 3237 | tmp_local_intv = tmp_local_intv->next; |
| 3238 | continue; |
| 3239 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3240 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | tmp_intv = tmp_intv->next; |
| 3246 | } |
| 3247 | |
| 3248 | /* some interval left uncovered -> fail */ |
| 3249 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3250 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3251 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3252 | } |
| 3253 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3254 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3255 | if (intv) { |
| 3256 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3257 | tmp_intv->next = local_intv; |
| 3258 | } else { |
| 3259 | intv = local_intv; |
| 3260 | } |
| 3261 | *ret = intv; |
| 3262 | |
| 3263 | return EXIT_SUCCESS; |
| 3264 | |
| 3265 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3266 | while (intv) { |
| 3267 | tmp_intv = intv->next; |
| 3268 | free(intv); |
| 3269 | intv = tmp_intv; |
| 3270 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3271 | while (local_intv) { |
| 3272 | tmp_local_intv = local_intv->next; |
| 3273 | free(local_intv); |
| 3274 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3275 | } |
| 3276 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3277 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3278 | } |
| 3279 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3280 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3281 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3282 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3283 | * |
| 3284 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3285 | * @param[in] mod_name Typedef name module name. |
| 3286 | * @param[in] module Main module. |
| 3287 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3288 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3289 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3290 | * @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] | 3291 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3292 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3293 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3294 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3295 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3296 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3297 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3298 | int tpdf_size; |
| 3299 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3300 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3301 | /* no prefix, try built-in types */ |
| 3302 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3303 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3304 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3305 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3306 | } |
| 3307 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3311 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3312 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3313 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3317 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3318 | /* search in local typedefs */ |
| 3319 | while (parent) { |
| 3320 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3321 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3322 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3323 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3324 | break; |
| 3325 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3326 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3327 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3328 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3329 | break; |
| 3330 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3331 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3332 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3333 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3334 | break; |
| 3335 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3336 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3337 | case LYS_ACTION: |
| 3338 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3339 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3340 | break; |
| 3341 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3342 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3343 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3344 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | break; |
| 3346 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3347 | case LYS_INPUT: |
| 3348 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3349 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3350 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3351 | break; |
| 3352 | |
| 3353 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3354 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3355 | continue; |
| 3356 | } |
| 3357 | |
| 3358 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3359 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3360 | match = &tpdf[i]; |
| 3361 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3365 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3367 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3368 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3369 | module = lyp_get_module(module, NULL, 0, mod_name, 0, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3370 | if (!module) { |
| 3371 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /* search in top level typedefs */ |
| 3376 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3377 | 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] | 3378 | match = &module->tpdf[i]; |
| 3379 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3384 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3385 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3386 | 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] | 3387 | match = &module->inc[i].submodule->tpdf[j]; |
| 3388 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3393 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3394 | |
| 3395 | check_leafref: |
| 3396 | if (ret) { |
| 3397 | *ret = match; |
| 3398 | } |
| 3399 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3400 | while (!match->type.info.lref.path) { |
| 3401 | match = match->type.der; |
| 3402 | assert(match); |
| 3403 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3404 | } |
| 3405 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3406 | } |
| 3407 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3408 | /** |
| 3409 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3410 | * |
| 3411 | * @param[in] type Type definition to use. |
| 3412 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3413 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3414 | * |
| 3415 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3416 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3417 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3418 | 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] | 3419 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3420 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3421 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3422 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3423 | char *s; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3424 | int ret = EXIT_SUCCESS, r; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3425 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3426 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3427 | assert(value); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3428 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3429 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3430 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3431 | /* the type was not resolved yet, nothing to do for now */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3432 | ret = EXIT_FAILURE; |
| 3433 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3434 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3435 | /* do not check defaults in not implemented module's data */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3436 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3437 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3438 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3439 | * but in typedef the value has to be modified to include the prefix */ |
| 3440 | if (*value) { |
| 3441 | if (strchr(*value, ':')) { |
| 3442 | dflt = transform_schema2json(module, *value); |
| 3443 | } else { |
| 3444 | /* default prefix of the module where the typedef is defined */ |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3445 | if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) { |
| 3446 | LOGMEM(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3447 | ret = -1; |
| 3448 | goto cleanup; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3449 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3450 | dflt = lydict_insert_zc(ctx, s); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3451 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3452 | lydict_remove(ctx, *value); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3453 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3454 | dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3455 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3456 | goto cleanup; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3457 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3458 | /* leafref in typedef cannot be checked */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3459 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3460 | } |
| 3461 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3462 | dflt = lydict_insert(ctx, *value, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3463 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3464 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3465 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3466 | if (base_tpdf->dflt) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3467 | dflt = lydict_insert(ctx, base_tpdf->dflt, 0); |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3468 | break; |
| 3469 | } |
| 3470 | } |
| 3471 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3472 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3473 | /* no default value, nothing to check, all is well */ |
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 | |
| 3477 | /* 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)? */ |
| 3478 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3479 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3480 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3481 | goto cleanup; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3482 | } else { |
| 3483 | /* check the default value from typedef, but use also the typedef's module |
| 3484 | * due to possible searching in imported modules which is expected in |
| 3485 | * typedef's module instead of module where the typedef is used */ |
| 3486 | module = base_tpdf->module; |
| 3487 | } |
| 3488 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3489 | case LY_TYPE_INST: |
| 3490 | case LY_TYPE_LEAFREF: |
| 3491 | case LY_TYPE_BOOL: |
| 3492 | case LY_TYPE_EMPTY: |
| 3493 | /* 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] | 3494 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3495 | case LY_TYPE_BITS: |
| 3496 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3497 | if (type->info.bits.count) { |
| 3498 | break; |
| 3499 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3500 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3501 | case LY_TYPE_ENUM: |
| 3502 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3503 | if (type->info.enums.count) { |
| 3504 | break; |
| 3505 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3506 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3507 | case LY_TYPE_DEC64: |
| 3508 | if (type->info.dec64.range) { |
| 3509 | break; |
| 3510 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3511 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3512 | case LY_TYPE_BINARY: |
| 3513 | if (type->info.binary.length) { |
| 3514 | break; |
| 3515 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3516 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3517 | case LY_TYPE_INT8: |
| 3518 | case LY_TYPE_INT16: |
| 3519 | case LY_TYPE_INT32: |
| 3520 | case LY_TYPE_INT64: |
| 3521 | case LY_TYPE_UINT8: |
| 3522 | case LY_TYPE_UINT16: |
| 3523 | case LY_TYPE_UINT32: |
| 3524 | case LY_TYPE_UINT64: |
| 3525 | if (type->info.num.range) { |
| 3526 | break; |
| 3527 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3528 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3529 | case LY_TYPE_STRING: |
| 3530 | if (type->info.str.length || type->info.str.patterns) { |
| 3531 | break; |
| 3532 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3533 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3534 | case LY_TYPE_UNION: |
| 3535 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3536 | break; |
| 3537 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3538 | LOGINT(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3539 | ret = -1; |
| 3540 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3541 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3542 | } else if (type->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3543 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3544 | 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] | 3545 | ret = -1; |
| 3546 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3547 | } |
| 3548 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3549 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3550 | memset(&node, 0, sizeof node); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3551 | node.value_str = lydict_insert(ctx, dflt, 0); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3552 | node.value_type = type->base; |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3553 | |
| 3554 | if (tpdf) { |
| 3555 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3556 | if (!node.schema) { |
| 3557 | LOGMEM(ctx); |
| 3558 | ret = -1; |
| 3559 | goto cleanup; |
| 3560 | } |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3561 | 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] | 3562 | if (r == -1) { |
| 3563 | LOGMEM(ctx); |
| 3564 | ret = -1; |
| 3565 | goto cleanup; |
| 3566 | } |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3567 | node.schema->module = module; |
| 3568 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
| 3569 | } else { |
| 3570 | node.schema = (struct lys_node *)type->parent; |
| 3571 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3572 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3573 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3574 | if (!type->info.lref.target) { |
| 3575 | ret = EXIT_FAILURE; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3576 | goto cleanup; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3577 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3578 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3579 | if (!ret) { |
| 3580 | /* adopt possibly changed default value to its canonical form */ |
| 3581 | if (*value) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3582 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3583 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3584 | dflt = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3585 | } |
| 3586 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3587 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 3588 | 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] | 3589 | /* possible forward reference */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3590 | ret = EXIT_FAILURE; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3591 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3592 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3593 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3594 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3595 | /* we have refined bits/enums */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3596 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3597 | "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] | 3598 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3599 | } |
| 3600 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3601 | } else { |
| 3602 | /* success - adopt canonical form from the node into the default value */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3603 | if (!ly_strequal(dflt, node.value_str, 1)) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3604 | /* this can happen only if we have non-inherited default value, |
| 3605 | * inherited default values are already in canonical form */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3606 | assert(ly_strequal(dflt, *value, 1)); |
| 3607 | |
| 3608 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3609 | *value = node.value_str; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3610 | node.value_str = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3611 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3612 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3613 | } |
| 3614 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3615 | cleanup: |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 3616 | lyd_free_value(node.value, node.value_type, node.value_flags, type); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3617 | lydict_remove(ctx, node.value_str); |
| 3618 | if (tpdf && node.schema) { |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3619 | free((char *)node.schema->name); |
| 3620 | free(node.schema); |
| 3621 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3622 | lydict_remove(ctx, dflt); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3623 | |
| 3624 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3627 | /** |
| 3628 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3629 | * |
| 3630 | * @param[in] key The key to check. |
| 3631 | * @param[in] flags What flags to check. |
| 3632 | * @param[in] list The list of all the keys. |
| 3633 | * @param[in] index Index of the key in the key list. |
| 3634 | * @param[in] name The name of the keys. |
| 3635 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3636 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3637 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3638 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3639 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3640 | 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] | 3641 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3642 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3643 | char *dup = NULL; |
| 3644 | int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3645 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3646 | |
| 3647 | /* existence */ |
| 3648 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3649 | if (name[len] != '\0') { |
| 3650 | dup = strdup(name); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3651 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3652 | dup[len] = '\0'; |
| 3653 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3654 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3655 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3656 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3657 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | /* uniqueness */ |
| 3661 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3662 | if (key == list->keys[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3663 | LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3664 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3669 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3670 | LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3671 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | /* type of the leaf is not built-in empty */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 3675 | 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] | 3676 | LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3677 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3681 | if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3682 | LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3683 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3684 | } |
| 3685 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3686 | /* key is not placed from augment */ |
| 3687 | if (key->parent->nodetype == LYS_AUGMENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3688 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3689 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3690 | return -1; |
| 3691 | } |
| 3692 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3693 | /* key is not when/if-feature -conditional */ |
| 3694 | j = 0; |
| 3695 | if (key->when || (key->iffeature_size && (j = 1))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3696 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3697 | 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] | 3698 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3699 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3702 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3703 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3704 | |
| 3705 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3706 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3707 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3708 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3709 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3710 | * |
| 3711 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3712 | */ |
| 3713 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3714 | 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] | 3715 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3716 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3717 | const struct lys_node *leaf = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3718 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3719 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3720 | 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] | 3721 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3722 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3723 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3724 | if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3725 | 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] | 3726 | } else if (rc == -2) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3727 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3728 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3729 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3730 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3731 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3732 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3733 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3734 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3735 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3736 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3737 | if (leaf->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3738 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3739 | 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] | 3740 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3741 | } |
| 3742 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3743 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3744 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3745 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3746 | return -1; |
| 3747 | } |
| 3748 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3749 | /* check that all unique's targets are of the same config type */ |
| 3750 | if (*trg_type) { |
| 3751 | 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] | 3752 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3753 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3754 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3755 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3756 | return -1; |
| 3757 | } |
| 3758 | } else { |
| 3759 | /* first unique */ |
| 3760 | if (leaf->flags & LYS_CONFIG_W) { |
| 3761 | *trg_type = 1; |
| 3762 | } else { |
| 3763 | *trg_type = 2; |
| 3764 | } |
| 3765 | } |
| 3766 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3767 | /* set leaf's unique flag */ |
| 3768 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3769 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3770 | return EXIT_SUCCESS; |
| 3771 | |
| 3772 | error: |
| 3773 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3774 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3775 | } |
| 3776 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3777 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3778 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3779 | { |
| 3780 | /* there are items after the one deleted */ |
| 3781 | if (i+1 < unres->count) { |
| 3782 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3783 | 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] | 3784 | |
| 3785 | /* deleting the last item */ |
| 3786 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3787 | free(unres->node); |
| 3788 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3792 | --unres->count; |
| 3793 | } |
| 3794 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3795 | /** |
| 3796 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3797 | * |
| 3798 | * @param[in] mod Module to search in. |
| 3799 | * @param[in] name Name of the data node. |
| 3800 | * @param[in] nam_len Length of the name. |
| 3801 | * @param[in] start Data node to start the search from. |
| 3802 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3803 | * they are replaced (!!) with the resolvents. |
| 3804 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3805 | * @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] | 3806 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3807 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3808 | 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] | 3809 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3810 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3811 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3812 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3813 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3814 | if (!parents->count) { |
| 3815 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3816 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3817 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3818 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3819 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3820 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3821 | 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] | 3822 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3823 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3824 | continue; |
| 3825 | } |
| 3826 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3827 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3828 | 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] | 3829 | && node->schema->name[nam_len] == '\0') { |
| 3830 | /* matching target */ |
| 3831 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3832 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3833 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3834 | flag = 1; |
| 3835 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3836 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3837 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3838 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3839 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3840 | parents->node[parents->count-1] = node; |
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 | } |
| 3843 | } |
| 3844 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3845 | |
| 3846 | if (!flag) { |
| 3847 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3848 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3849 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3850 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3851 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3852 | } |
| 3853 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3854 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3855 | } |
| 3856 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3857 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3858 | resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path) |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3859 | { |
| 3860 | int dep1, dep2; |
| 3861 | const struct lys_node *node; |
| 3862 | |
| 3863 | if (lys_parent(op_node)) { |
| 3864 | /* inner operation (notif/action) */ |
| 3865 | if (abs_path) { |
| 3866 | return 1; |
| 3867 | } else { |
| 3868 | /* compare depth of both nodes */ |
| 3869 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3870 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3871 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3872 | return 1; |
| 3873 | } |
| 3874 | } |
| 3875 | } else { |
| 3876 | /* top-level operation (notif/rpc) */ |
| 3877 | if (op_node != first_node) { |
| 3878 | return 1; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3885 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3886 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3887 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3888 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3889 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3890 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3891 | * @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] | 3892 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3893 | * @return 0 on forward reference, otherwise the number |
| 3894 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3895 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3896 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3897 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3898 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3899 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3900 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3901 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3902 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3903 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3904 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3905 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3906 | struct ly_ctx *ctx = context_node->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3907 | |
| 3908 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3909 | 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] | 3910 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3911 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3912 | return -parsed+i; |
| 3913 | } |
| 3914 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3915 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3916 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3917 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3918 | if (sour_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3919 | 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] | 3920 | } else { |
| 3921 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3922 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3923 | 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] | 3924 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3925 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3926 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3930 | dest_parent_times = 0; |
| 3931 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3932 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3933 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3934 | 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] | 3935 | return -parsed; |
| 3936 | } |
| 3937 | pke_parsed += i; |
| 3938 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3939 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3940 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3941 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3942 | /* we are in an unresolved augment, cannot evaluate */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3943 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3944 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3945 | return 0; |
| 3946 | } |
| 3947 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3948 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3949 | * all schema nodes that cannot be instantiated in data tree */ |
| 3950 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3951 | 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] | 3952 | dst_node = lys_parent(dst_node)); |
| 3953 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3954 | if (!dst_node) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3955 | 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] | 3956 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3957 | } |
| 3958 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3959 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3960 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3961 | if (dest_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3962 | 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] | 3963 | } else { |
| 3964 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3965 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3966 | 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] | 3967 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3968 | 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] | 3969 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3970 | } |
| 3971 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3972 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3973 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3974 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3975 | } |
| 3976 | first_iter = 0; |
| 3977 | } |
| 3978 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3979 | if (pke_len == pke_parsed) { |
| 3980 | break; |
| 3981 | } |
| 3982 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3983 | 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] | 3984 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3985 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3986 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3987 | return -parsed; |
| 3988 | } |
| 3989 | pke_parsed += i; |
| 3990 | } |
| 3991 | |
| 3992 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 3993 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3994 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3995 | 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] | 3996 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3997 | return -parsed; |
| 3998 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3999 | } while (has_predicate); |
| 4000 | |
| 4001 | return parsed; |
| 4002 | } |
| 4003 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4004 | static int |
| 4005 | check_leafref_features(struct lys_type *type) |
| 4006 | { |
| 4007 | struct lys_node *iter; |
| 4008 | struct ly_set *src_parents, *trg_parents, *features; |
| 4009 | struct lys_node_augment *aug; |
| 4010 | struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx; |
| 4011 | unsigned int i, j, size, x; |
| 4012 | int ret = EXIT_SUCCESS; |
| 4013 | |
| 4014 | assert(type->parent); |
| 4015 | |
| 4016 | src_parents = ly_set_new(); |
| 4017 | trg_parents = ly_set_new(); |
| 4018 | features = ly_set_new(); |
| 4019 | |
| 4020 | /* get parents chain of source (leafref) */ |
| 4021 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
| 4022 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4023 | continue; |
| 4024 | } |
| 4025 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4026 | aug = (struct lys_node_augment *)iter->parent; |
| 4027 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4028 | /* unresolved augment, wait until it's resolved */ |
| 4029 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4030 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4031 | ret = EXIT_FAILURE; |
| 4032 | goto cleanup; |
| 4033 | } |
| 4034 | } |
| 4035 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 4036 | } |
| 4037 | /* get parents chain of target */ |
| 4038 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
| 4039 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4040 | continue; |
| 4041 | } |
| 4042 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4043 | aug = (struct lys_node_augment *)iter->parent; |
| 4044 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4045 | /* unresolved augment, wait until it's resolved */ |
| 4046 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4047 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4048 | ret = EXIT_FAILURE; |
| 4049 | goto cleanup; |
| 4050 | } |
| 4051 | } |
| 4052 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 4053 | } |
| 4054 | |
| 4055 | /* compare the features used in if-feature statements in the rest of both |
| 4056 | * chains of parents. The set of features used for target must be a subset |
| 4057 | * of features used for the leafref. This is not a perfect, we should compare |
| 4058 | * the truth tables but it could require too much resources, so we simplify that */ |
| 4059 | for (i = 0; i < src_parents->number; i++) { |
| 4060 | iter = src_parents->set.s[i]; /* shortcut */ |
| 4061 | if (!iter->iffeature_size) { |
| 4062 | continue; |
| 4063 | } |
| 4064 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4065 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4066 | for (; size; size--) { |
| 4067 | if (!iter->iffeature[j].features[size - 1]) { |
| 4068 | /* not yet resolved feature, postpone this check */ |
| 4069 | ret = EXIT_FAILURE; |
| 4070 | goto cleanup; |
| 4071 | } |
| 4072 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | x = features->number; |
| 4077 | for (i = 0; i < trg_parents->number; i++) { |
| 4078 | iter = trg_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 | if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 4091 | /* the feature is not present in features set of target's parents chain */ |
| 4092 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 4093 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
| 4094 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 4095 | iter->iffeature[j].features[size - 1]->name); |
| 4096 | ret = -1; |
| 4097 | goto cleanup; |
| 4098 | } |
| 4099 | } |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | cleanup: |
| 4104 | ly_set_free(features); |
| 4105 | ly_set_free(src_parents); |
| 4106 | ly_set_free(trg_parents); |
| 4107 | |
| 4108 | return ret; |
| 4109 | } |
| 4110 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4111 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4112 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4113 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4114 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4115 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4116 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4117 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4118 | * @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] | 4119 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4120 | static int |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4121 | 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] | 4122 | { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4123 | const struct lys_node *node, *op_node = NULL, *tmp_parent; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4124 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 4125 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4126 | const char *id, *prefix, *name; |
| 4127 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4128 | int i, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4129 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4130 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4131 | if (!type->info.lref.target) { |
| 4132 | first_iter = 1; |
| 4133 | parent_times = 0; |
| 4134 | id = type->info.lref.path; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4135 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4136 | /* find operation schema we are in */ |
| 4137 | for (op_node = lys_parent(parent); |
| 4138 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4139 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 4140 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4141 | cur_module = lys_node_module(parent); |
| 4142 | do { |
| 4143 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
| 4144 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4145 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4146 | } |
| 4147 | id += i; |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4148 | |
| 4149 | /* get the current module */ |
| 4150 | tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module; |
| 4151 | if (!tmp_mod) { |
| 4152 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4153 | return EXIT_FAILURE; |
| 4154 | } |
| 4155 | last_aug = NULL; |
| 4156 | |
| 4157 | if (first_iter) { |
| 4158 | if (parent_times == -1) { |
| 4159 | /* use module data */ |
| 4160 | node = NULL; |
| 4161 | |
| 4162 | } else if (parent_times > 0) { |
| 4163 | /* we are looking for the right parent */ |
| 4164 | for (i = 0, node = parent; i < parent_times; i++) { |
| 4165 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 4166 | && !((struct lys_node_augment *)node->parent)->target) { |
| 4167 | /* we are in an unresolved augment, cannot evaluate */ |
| 4168 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 4169 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path); |
| 4170 | return EXIT_FAILURE; |
| 4171 | } |
| 4172 | |
| 4173 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 4174 | * all schema nodes that cannot be instantiated in data tree */ |
| 4175 | for (node = lys_parent(node); |
| 4176 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4177 | node = lys_parent(node)); |
| 4178 | |
| 4179 | if (!node) { |
| 4180 | if (i == parent_times - 1) { |
| 4181 | /* top-level */ |
| 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | /* higher than top-level */ |
| 4186 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4187 | return EXIT_FAILURE; |
| 4188 | } |
| 4189 | } |
| 4190 | } else { |
| 4191 | LOGINT(ctx); |
| 4192 | return -1; |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 4197 | * - useless to search for that in augments) */ |
| 4198 | if (!tmp_mod->implemented && node) { |
| 4199 | get_next_augment: |
| 4200 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
| 4201 | } |
| 4202 | |
| 4203 | tmp_parent = (last_aug ? (struct lys_node *)last_aug : node); |
| 4204 | node = NULL; |
| 4205 | while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) { |
| 4206 | if (lys_node_module(node) != lys_main_module(tmp_mod)) { |
| 4207 | continue; |
| 4208 | } |
| 4209 | if (strncmp(node->name, name, nam_len) || node->name[nam_len]) { |
| 4210 | continue; |
| 4211 | } |
| 4212 | /* match */ |
| 4213 | break; |
| 4214 | } |
| 4215 | if (!node) { |
| 4216 | if (last_aug) { |
| 4217 | /* restore the correct augment target */ |
| 4218 | node = last_aug->target; |
| 4219 | goto get_next_augment; |
| 4220 | } |
| 4221 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4222 | return EXIT_FAILURE; |
| 4223 | } |
| 4224 | |
| 4225 | if (first_iter) { |
| 4226 | /* set external dependency flag, we can decide based on the first found node */ |
| 4227 | if (op_node && parent_times && |
| 4228 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 4229 | parent->flags |= LYS_LEAFREF_DEP; |
| 4230 | } |
| 4231 | first_iter = 0; |
| 4232 | } |
| 4233 | |
| 4234 | if (has_predicate) { |
| 4235 | /* we have predicate, so the current result must be list */ |
| 4236 | if (node->nodetype != LYS_LIST) { |
| 4237 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4238 | return -1; |
| 4239 | } |
| 4240 | |
| 4241 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
| 4242 | if (!i) { |
| 4243 | return EXIT_FAILURE; |
| 4244 | } else if (i < 0) { |
| 4245 | return -1; |
| 4246 | } |
| 4247 | id += i; |
| 4248 | has_predicate = 0; |
| 4249 | } |
| 4250 | } while (id[0]); |
| 4251 | |
| 4252 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 4253 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
| 4254 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4255 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path); |
| 4256 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4257 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4258 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4259 | /* check status */ |
| 4260 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
| 4261 | node->flags, node->module, node->name, node)) { |
| 4262 | return -1; |
| 4263 | } |
| 4264 | |
| 4265 | /* assign */ |
| 4266 | type->info.lref.target = (struct lys_node_leaf *)node; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4267 | } |
| 4268 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4269 | /* as the last thing traverse this leafref and make targets on the path implemented */ |
| 4270 | if (lys_node_module(parent)->implemented) { |
| 4271 | /* make all the modules in the path implemented */ |
| 4272 | for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) { |
| 4273 | if (!lys_node_module(node)->implemented) { |
| 4274 | lys_node_module(node)->implemented = 1; |
| 4275 | if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4276 | return -1; |
| 4277 | } |
| 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | /* store the backlink from leafref target */ |
| 4282 | if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) { |
| 4283 | return -1; |
| 4284 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4287 | /* check if leafref and its target are under common if-features */ |
| 4288 | return check_leafref_features(type); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4291 | /** |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4292 | * @brief Compare 2 data node values. |
| 4293 | * |
| 4294 | * Comparison performed on canonical forms, the first value |
| 4295 | * is first transformed into canonical form. |
| 4296 | * |
| 4297 | * @param[in] node Leaf/leaf-list with these values. |
| 4298 | * @param[in] noncan_val Non-canonical value. |
| 4299 | * @param[in] noncan_val_len Length of \p noncal_val. |
| 4300 | * @param[in] can_val Canonical value. |
| 4301 | * @return 1 if equal, 0 if not, -1 on error (logged). |
| 4302 | */ |
| 4303 | static int |
| 4304 | valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val) |
| 4305 | { |
| 4306 | int ret; |
| 4307 | struct lyd_node_leaf_list leaf; |
| 4308 | struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node; |
| 4309 | |
| 4310 | /* dummy leaf */ |
| 4311 | memset(&leaf, 0, sizeof leaf); |
| 4312 | leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len); |
| 4313 | |
| 4314 | repeat: |
| 4315 | leaf.value_type = sleaf->type.base; |
| 4316 | leaf.schema = node; |
| 4317 | |
| 4318 | if (leaf.value_type == LY_TYPE_LEAFREF) { |
| 4319 | if (!sleaf->type.info.lref.target) { |
| 4320 | /* 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] | 4321 | LOGINT(node->module->ctx); |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4322 | ret = -1; |
| 4323 | goto finish; |
| 4324 | } |
| 4325 | sleaf = sleaf->type.info.lref.target; |
| 4326 | goto repeat; |
| 4327 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 4328 | 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] | 4329 | ret = -1; |
| 4330 | goto finish; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | if (!strcmp(leaf.value_str, can_val)) { |
| 4335 | ret = 1; |
| 4336 | } else { |
| 4337 | ret = 0; |
| 4338 | } |
| 4339 | |
| 4340 | finish: |
| 4341 | lydict_remove(node->module->ctx, leaf.value_str); |
| 4342 | return ret; |
| 4343 | } |
| 4344 | |
| 4345 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4346 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4347 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4348 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 4349 | * @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] | 4350 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4351 | * @param[in,out] node Node matching the restriction without |
| 4352 | * the predicate. If it does not satisfy the predicate, |
| 4353 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4354 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4355 | * @return Number of characters successfully parsed, |
| 4356 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4357 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4358 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4359 | 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] | 4360 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4361 | /* ... /node[key=value] ... */ |
| 4362 | struct lyd_node_leaf_list *key; |
| 4363 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 4364 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4365 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4366 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4367 | struct ly_ctx *ctx = prev_mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4368 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4369 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4370 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4371 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4372 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4373 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 4374 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4375 | } |
| 4376 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4377 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4378 | if (!(*node)) { |
| 4379 | /* just parse it all */ |
| 4380 | continue; |
| 4381 | } |
| 4382 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4383 | /* target */ |
| 4384 | if (name[0] == '.') { |
| 4385 | /* leaf-list value */ |
| 4386 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4387 | 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] | 4388 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4389 | parsed = -1; |
| 4390 | goto cleanup; |
| 4391 | } |
| 4392 | |
| 4393 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4394 | 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] | 4395 | *node = NULL; |
| 4396 | goto cleanup; |
| 4397 | } |
| 4398 | |
| 4399 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4400 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4401 | |
| 4402 | /* keyless list position */ |
| 4403 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4404 | 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] | 4405 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4406 | parsed = -1; |
| 4407 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4408 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4409 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4410 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4411 | 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] | 4412 | (*node)->schema->name); |
| 4413 | parsed = -1; |
| 4414 | goto cleanup; |
| 4415 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4416 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4417 | /* check the index */ |
| 4418 | if (atoi(name) != cur_idx) { |
| 4419 | *node = NULL; |
| 4420 | goto cleanup; |
| 4421 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4422 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4423 | } else { |
| 4424 | /* list key value */ |
| 4425 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4426 | 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] | 4427 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4428 | parsed = -1; |
| 4429 | goto cleanup; |
| 4430 | } |
| 4431 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4432 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4433 | /* prepare key array */ |
| 4434 | if (!list_keys) { |
| 4435 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4436 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4437 | for (i = 0; i < slist->keys_size; ++i) { |
| 4438 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4439 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4440 | } |
| 4441 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4442 | /* find the schema key leaf */ |
| 4443 | for (i = 0; i < slist->keys_size; ++i) { |
| 4444 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4445 | break; |
| 4446 | } |
| 4447 | } |
| 4448 | if (i == slist->keys_size) { |
| 4449 | /* this list has no such key */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4450 | 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] | 4451 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4452 | parsed = -1; |
| 4453 | goto cleanup; |
| 4454 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4455 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4456 | /* check module */ |
| 4457 | if (model) { |
| 4458 | 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] | 4459 | 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] | 4460 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4461 | parsed = -1; |
| 4462 | goto cleanup; |
| 4463 | } |
| 4464 | } else { |
| 4465 | if (list_keys[i]->module != prev_mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4466 | 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] | 4467 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4468 | parsed = -1; |
| 4469 | goto cleanup; |
| 4470 | } |
| 4471 | } |
| 4472 | |
| 4473 | /* find the actual data key */ |
| 4474 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4475 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4476 | break; |
| 4477 | } |
| 4478 | } |
| 4479 | if (!key) { |
| 4480 | /* list instance is missing a key? definitely should not happen */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4481 | LOGINT(ctx); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4482 | parsed = -1; |
| 4483 | goto cleanup; |
| 4484 | } |
| 4485 | |
| 4486 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4487 | if (!valequal(key->schema, value, val_len, key->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4488 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4489 | /* we still want to parse the whole predicate */ |
| 4490 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | /* everything is fine, mark this key as resolved */ |
| 4494 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4495 | } |
| 4496 | } while (has_predicate); |
| 4497 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4498 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4499 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4500 | for (i = 0; i < slist->keys_size; ++i) { |
| 4501 | if (list_keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4502 | 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] | 4503 | parsed = -1; |
| 4504 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4505 | } |
| 4506 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4507 | } |
| 4508 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4509 | cleanup: |
| 4510 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4511 | return parsed; |
| 4512 | } |
| 4513 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4514 | static int |
| 4515 | check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4516 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4517 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4518 | struct lyxp_set set; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4519 | enum int_log_opts prev_ilo; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4520 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4521 | if (check_place) { |
| 4522 | parent = node; |
| 4523 | while (parent) { |
| 4524 | if (parent->nodetype == LYS_GROUPING) { |
| 4525 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4526 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4527 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4528 | if (parent->nodetype == LYS_AUGMENT) { |
| 4529 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4530 | /* unresolved augment, skip for now (will be checked later) */ |
| 4531 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4532 | } else { |
| 4533 | parent = ((struct lys_node_augment *)parent)->target; |
| 4534 | continue; |
| 4535 | } |
| 4536 | } |
| 4537 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4538 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4539 | } |
| 4540 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4541 | memset(&set, 0, sizeof set); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4542 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4543 | /* produce just warnings */ |
| 4544 | ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL); |
| 4545 | lyxp_node_atomize(node, &set, 1); |
| 4546 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
| 4547 | |
| 4548 | if (set.val.snodes) { |
| 4549 | free(set.val.snodes); |
| 4550 | } |
| 4551 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4552 | } |
| 4553 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4554 | static int |
| 4555 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4556 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4557 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4558 | |
| 4559 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4560 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4561 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4562 | 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] | 4563 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4564 | return -1; |
| 4565 | } |
| 4566 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4567 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4568 | } else if (type->base == LY_TYPE_UNION) { |
| 4569 | for (i = 0; i < type->info.uni.count; i++) { |
| 4570 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4571 | return -1; |
| 4572 | } |
| 4573 | } |
| 4574 | } |
| 4575 | return 0; |
| 4576 | } |
| 4577 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4578 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4579 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4580 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4581 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4582 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4583 | * @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] | 4584 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4585 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4586 | * |
| 4587 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4588 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4589 | int |
| 4590 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4591 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4592 | struct lys_node_leaf *leaf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4593 | struct ly_ctx *ctx; |
| 4594 | |
| 4595 | if (!node) { |
| 4596 | return 0; |
| 4597 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4598 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4599 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4600 | ctx = node->module->ctx; |
| 4601 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4602 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4603 | if (clear) { |
| 4604 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4605 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4606 | } else { |
| 4607 | if (node->flags & LYS_CONFIG_SET) { |
| 4608 | /* skip nodes with an explicit config value */ |
| 4609 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4610 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4611 | 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] | 4612 | return -1; |
| 4613 | } |
| 4614 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4615 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4616 | |
| 4617 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4618 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4619 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4620 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4621 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4622 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4623 | return -1; |
| 4624 | } |
| 4625 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4626 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4627 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4628 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4629 | return -1; |
| 4630 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4631 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4632 | leaf = (struct lys_node_leaf *)node; |
| 4633 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4634 | return -1; |
| 4635 | } |
| 4636 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4637 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4638 | |
| 4639 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4640 | } |
| 4641 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4642 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4643 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4644 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4645 | * @param[in] aug Augment to use. |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4646 | * @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] | 4647 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4648 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4649 | * @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] | 4650 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4651 | static int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4652 | 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] | 4653 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4654 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4655 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4656 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4657 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4658 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4659 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4660 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4661 | mod = lys_main_module(aug->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4662 | ctx = mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4663 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4664 | /* set it as not applied for now */ |
| 4665 | aug->flags |= LYS_NOTAPPLIED; |
| 4666 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4667 | /* 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] | 4668 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4669 | /* resolve target node */ |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4670 | 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] | 4671 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4672 | LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4673 | return -1; |
| 4674 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4675 | if (!set) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4676 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4677 | return EXIT_FAILURE; |
| 4678 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4679 | aug->target = set->set.s[0]; |
| 4680 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4681 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4682 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4683 | /* make this module implemented if the target module is (if the target is in an unimplemented module, |
| 4684 | * it is fine because when we will be making that module implemented, its augment will be applied |
| 4685 | * and that augment target module made implemented, recursively) */ |
| 4686 | if (mod->implemented && !lys_node_module(aug->target)->implemented) { |
| 4687 | lys_node_module(aug->target)->implemented = 1; |
| 4688 | if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4689 | return -1; |
| 4690 | } |
| 4691 | } |
| 4692 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4693 | /* check for mandatory nodes - if the target node is in another module |
| 4694 | * the added nodes cannot be mandatory |
| 4695 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4696 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4697 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4698 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4699 | } |
| 4700 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4701 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4702 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4703 | LY_TREE_FOR(aug->child, sub) { |
| 4704 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4705 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4706 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4707 | 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] | 4708 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4709 | return -1; |
| 4710 | } |
| 4711 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4712 | } 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] | 4713 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4714 | 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] | 4715 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4716 | 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] | 4717 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4718 | return -1; |
| 4719 | } |
| 4720 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4721 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4722 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4723 | 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] | 4724 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4725 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4726 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4727 | return -1; |
| 4728 | } |
| 4729 | } |
| 4730 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4731 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 4732 | 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] | 4733 | return -1; |
| 4734 | } |
| 4735 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4736 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4737 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4738 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4739 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4740 | } |
| 4741 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4742 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4743 | if (!aug->child) { |
| 4744 | /* empty augment, nothing to connect, but it is techincally applied */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4745 | LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4746 | aug->flags &= ~LYS_NOTAPPLIED; |
Radek Krejci | aa6b2a1 | 2017-10-26 15:52:39 +0200 | [diff] [blame] | 4747 | } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) { |
| 4748 | /* we try to connect the augment only in case the module is implemented or |
| 4749 | * the augment applies on the used grouping, anyway we failed here */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4750 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4751 | } |
| 4752 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4753 | return EXIT_SUCCESS; |
| 4754 | } |
| 4755 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4756 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4757 | 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] | 4758 | { |
| 4759 | enum LY_VLOG_ELEM vlog_type; |
| 4760 | void *vlog_node; |
| 4761 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4762 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4763 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4764 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4765 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4766 | struct lys_ext_instance *tmp_ext; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4767 | struct ly_ctx *ctx = NULL; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4768 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4769 | |
| 4770 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4771 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4772 | vlog_node = info->parent; |
| 4773 | vlog_type = LY_VLOG_LYS; |
| 4774 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4775 | case LYEXT_PAR_MODULE: |
| 4776 | case LYEXT_PAR_IMPORT: |
| 4777 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4778 | vlog_node = NULL; |
| 4779 | vlog_type = LY_VLOG_LYS; |
| 4780 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4781 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4782 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4783 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4784 | break; |
| 4785 | } |
| 4786 | |
| 4787 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4788 | /* YIN */ |
| 4789 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4790 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4791 | 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] | 4792 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4793 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4794 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4795 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4796 | ctx = mod->ctx; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4797 | |
| 4798 | /* find the extension definition */ |
| 4799 | e = NULL; |
| 4800 | for (i = 0; i < mod->extensions_size; i++) { |
| 4801 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4802 | e = &mod->extensions[i]; |
| 4803 | break; |
| 4804 | } |
| 4805 | } |
| 4806 | /* try submodules */ |
| 4807 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4808 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4809 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4810 | e = &mod->inc[j].submodule->extensions[i]; |
| 4811 | break; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4816 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4817 | return EXIT_FAILURE; |
| 4818 | } |
| 4819 | |
| 4820 | /* 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] | 4821 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4822 | if (e->plugin && e->plugin->check_position) { |
| 4823 | /* common part - we have plugin with position checking function, use it first */ |
| 4824 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4825 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4826 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4827 | return -1; |
| 4828 | } |
| 4829 | } |
| 4830 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4831 | /* extension type-specific part - allocation */ |
| 4832 | if (e->plugin) { |
| 4833 | etype = e->plugin->type; |
| 4834 | } else { |
| 4835 | /* default type */ |
| 4836 | etype = LYEXT_FLAG; |
| 4837 | } |
| 4838 | switch (etype) { |
| 4839 | case LYEXT_FLAG: |
| 4840 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4841 | break; |
| 4842 | case LYEXT_COMPLEX: |
| 4843 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4844 | break; |
| 4845 | case LYEXT_ERR: |
| 4846 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4847 | LOGINT(ctx); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4848 | return -1; |
| 4849 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4850 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4851 | |
| 4852 | /* common part for all extension types */ |
| 4853 | (*ext)->def = e; |
| 4854 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4855 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4856 | (*ext)->insubstmt = info->substmt; |
| 4857 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4858 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4859 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4860 | |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4861 | if (e->argument) { |
| 4862 | if (!(e->flags & LYS_YINELEM)) { |
| 4863 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4864 | if (!(*ext)->arg_value) { |
PavolVican | e7a1fc6 | 2018-02-19 16:50:27 +0100 | [diff] [blame] | 4865 | 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] | 4866 | return -1; |
| 4867 | } |
| 4868 | |
| 4869 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4870 | } else { |
| 4871 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4872 | if (ly_strequal(yin->name, e->argument, 1)) { |
| 4873 | (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0); |
| 4874 | lyxml_free(mod->ctx, yin); |
| 4875 | break; |
| 4876 | } |
| 4877 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4878 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4881 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 4882 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 4883 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4886 | (*ext)->nodetype = LYS_EXT; |
| 4887 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4888 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4889 | /* extension type-specific part - parsing content */ |
| 4890 | switch (etype) { |
| 4891 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4892 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4893 | if (!yin->ns) { |
| 4894 | /* garbage */ |
| 4895 | lyxml_free(mod->ctx, yin); |
| 4896 | continue; |
| 4897 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4898 | /* standard YANG statements are not expected here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4899 | 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] | 4900 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4901 | } else if (yin->ns == info->data.yin->ns && |
| 4902 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4903 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4904 | if ((*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4905 | 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] | 4906 | return -1; |
| 4907 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4908 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4909 | yin->content = NULL; |
| 4910 | lyxml_free(mod->ctx, yin); |
| 4911 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4912 | /* extension instance */ |
| 4913 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4914 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4915 | return -1; |
| 4916 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4917 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4918 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4919 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4920 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4921 | break; |
| 4922 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4923 | ((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] | 4924 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4925 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4926 | return -1; |
| 4927 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4928 | break; |
| 4929 | default: |
| 4930 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4931 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4932 | |
| 4933 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4934 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4935 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4936 | /* YANG */ |
| 4937 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4938 | ext_prefix = (char *)(*ext)->def; |
| 4939 | tmp = strchr(ext_prefix, ':'); |
| 4940 | if (!tmp) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4941 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4942 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4943 | } |
| 4944 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4945 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4946 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4947 | 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] | 4948 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4949 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4950 | return EXIT_FAILURE; |
| 4951 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4952 | ctx = mod->ctx; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4953 | |
| 4954 | /* find the extension definition */ |
| 4955 | e = NULL; |
| 4956 | for (i = 0; i < mod->extensions_size; i++) { |
| 4957 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4958 | e = &mod->extensions[i]; |
| 4959 | break; |
| 4960 | } |
| 4961 | } |
| 4962 | /* try submodules */ |
| 4963 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4964 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4965 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4966 | e = &mod->inc[j].submodule->extensions[i]; |
| 4967 | break; |
| 4968 | } |
| 4969 | } |
| 4970 | } |
| 4971 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4972 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4973 | return EXIT_FAILURE; |
| 4974 | } |
| 4975 | |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 4976 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
| 4977 | (*ext)->def = NULL; |
| 4978 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4979 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4980 | |
| 4981 | if (e->plugin && e->plugin->check_position) { |
| 4982 | /* common part - we have plugin with position checking function, use it first */ |
| 4983 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4984 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4985 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4986 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4990 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4991 | (*ext)->def = e; |
| 4992 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4993 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4994 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4995 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4996 | if (e->argument && !(*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4997 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 4998 | goto error; |
| 4999 | } |
| 5000 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5001 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 5002 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 5003 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 5006 | (*ext)->module = info->mod; |
| 5007 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5008 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5009 | /* extension type-specific part */ |
| 5010 | if (e->plugin) { |
| 5011 | etype = e->plugin->type; |
| 5012 | } else { |
| 5013 | /* default type */ |
| 5014 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5015 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5016 | switch (etype) { |
| 5017 | case LYEXT_FLAG: |
| 5018 | /* nothing change */ |
| 5019 | break; |
| 5020 | case LYEXT_COMPLEX: |
| 5021 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5022 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5023 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 5024 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5025 | ((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] | 5026 | if (info->data.yang) { |
| 5027 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5028 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 5029 | (struct lys_ext_instance_complex*)(*ext))) { |
| 5030 | goto error; |
| 5031 | } |
| 5032 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 5033 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5034 | goto error; |
| 5035 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 5036 | } |
| 5037 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 5038 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5039 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5040 | break; |
| 5041 | case LYEXT_ERR: |
| 5042 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5043 | LOGINT(ctx); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5044 | goto error; |
| 5045 | } |
| 5046 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5047 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 5048 | goto error; |
| 5049 | } |
| 5050 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5054 | error: |
| 5055 | free(ext_prefix); |
| 5056 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5059 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5060 | * @brief Resolve (find) choice default case. Does not log. |
| 5061 | * |
| 5062 | * @param[in] choic Choice to use. |
| 5063 | * @param[in] dflt Name of the default case. |
| 5064 | * |
| 5065 | * @return Pointer to the default node or NULL. |
| 5066 | */ |
| 5067 | static struct lys_node * |
| 5068 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 5069 | { |
| 5070 | struct lys_node *child, *ret; |
| 5071 | |
| 5072 | LY_TREE_FOR(choic->child, child) { |
| 5073 | if (child->nodetype == LYS_USES) { |
| 5074 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 5075 | if (ret) { |
| 5076 | return ret; |
| 5077 | } |
| 5078 | } |
| 5079 | |
| 5080 | 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] | 5081 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5082 | return child; |
| 5083 | } |
| 5084 | } |
| 5085 | |
| 5086 | return NULL; |
| 5087 | } |
| 5088 | |
| 5089 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5090 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 5091 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5092 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5093 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5094 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5095 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5096 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 5097 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5098 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5099 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5100 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5101 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5102 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5103 | struct lys_node_leaflist *llist; |
| 5104 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 5105 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5106 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5107 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5108 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5109 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5110 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5111 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5112 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5113 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5114 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 5115 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5116 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5117 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5118 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 5119 | if (node_aux->nodetype & LYS_GROUPING) { |
| 5120 | /* do not instantiate groupings from groupings */ |
| 5121 | continue; |
| 5122 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5123 | 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] | 5124 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5125 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 5126 | 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] | 5127 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5128 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5129 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5130 | 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] | 5131 | 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] | 5132 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5133 | } |
| 5134 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5135 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 5136 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5137 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 5138 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5139 | if (uses->refine_size) { |
| 5140 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5141 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5142 | } |
| 5143 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5144 | /* apply refines */ |
| 5145 | for (i = 0; i < uses->refine_size; i++) { |
| 5146 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 5147 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 5148 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 5149 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 5150 | if (rc || !node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5151 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5152 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5153 | } |
| 5154 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5155 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5156 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 5157 | 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] | 5158 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5159 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5160 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5161 | |
| 5162 | /* description on any nodetype */ |
| 5163 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5164 | lydict_remove(ctx, node->dsc); |
| 5165 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | /* reference on any nodetype */ |
| 5169 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5170 | lydict_remove(ctx, node->ref); |
| 5171 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5172 | } |
| 5173 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5174 | /* config on any nodetype, |
| 5175 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 5176 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5177 | node->flags &= ~LYS_CONFIG_MASK; |
| 5178 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5182 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5183 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5184 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5185 | leaf = (struct lys_node_leaf *)node; |
| 5186 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5187 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5188 | lydict_remove(ctx, leaf->dflt); |
| 5189 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 5190 | |
| 5191 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5192 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 5193 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5194 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5195 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5196 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 5197 | /* leaf-list */ |
| 5198 | llist = (struct lys_node_leaflist *)node; |
| 5199 | |
| 5200 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5201 | for (j = 0; j < llist->dflt_size; j++) { |
| 5202 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5203 | } |
| 5204 | free(llist->dflt); |
| 5205 | |
| 5206 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5207 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5208 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5209 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5210 | for (j = 0; j < llist->dflt_size; j++) { |
| 5211 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5215 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5216 | 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] | 5217 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5218 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5219 | } |
| 5220 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5221 | } |
| 5222 | } |
| 5223 | |
| 5224 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 5225 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5226 | /* remove current value */ |
| 5227 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5228 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5229 | /* set new value */ |
| 5230 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 5231 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5232 | if (rfn->flags & LYS_MAND_TRUE) { |
| 5233 | /* check if node has default value */ |
| 5234 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5235 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5236 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5237 | goto fail; |
| 5238 | } |
| 5239 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5240 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5241 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5242 | goto fail; |
| 5243 | } |
| 5244 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5248 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 5249 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 5250 | ((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] | 5251 | } |
| 5252 | |
| 5253 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5254 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5255 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5256 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5257 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5258 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5259 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5260 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5261 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5262 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5263 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5264 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5265 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5266 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 5271 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5272 | switch (node->nodetype) { |
| 5273 | case LYS_LEAF: |
| 5274 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 5275 | old_must = &((struct lys_node_leaf *)node)->must; |
| 5276 | break; |
| 5277 | case LYS_LEAFLIST: |
| 5278 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 5279 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 5280 | break; |
| 5281 | case LYS_LIST: |
| 5282 | old_size = &((struct lys_node_list *)node)->must_size; |
| 5283 | old_must = &((struct lys_node_list *)node)->must; |
| 5284 | break; |
| 5285 | case LYS_CONTAINER: |
| 5286 | old_size = &((struct lys_node_container *)node)->must_size; |
| 5287 | old_must = &((struct lys_node_container *)node)->must; |
| 5288 | break; |
| 5289 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5290 | case LYS_ANYDATA: |
| 5291 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 5292 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5293 | break; |
| 5294 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5295 | LOGINT(ctx); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5296 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | size = *old_size + rfn->must_size; |
| 5300 | must = realloc(*old_must, size * sizeof *rfn->must); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5301 | LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5302 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 5303 | must[j].ext_size = rfn->must[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5304 | 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] | 5305 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5306 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 5307 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 5308 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 5309 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 5310 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Radek Krejci | cfcd8a5 | 2017-09-04 13:19:57 +0200 | [diff] [blame] | 5311 | must[j].flags = rfn->must[k].flags; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5312 | } |
| 5313 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5314 | *old_must = must; |
| 5315 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5316 | |
| 5317 | /* check XPath dependencies again */ |
| 5318 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 5319 | goto fail; |
| 5320 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5321 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5322 | |
| 5323 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 5324 | if (rfn->iffeature_size) { |
| 5325 | old_size = &node->iffeature_size; |
| 5326 | old_iff = &node->iffeature; |
| 5327 | |
| 5328 | size = *old_size + rfn->iffeature_size; |
| 5329 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5330 | LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail); |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5331 | *old_iff = iff; |
| 5332 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5333 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 5334 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5335 | if (usize1) { |
| 5336 | /* there is something to duplicate */ |
| 5337 | /* duplicate compiled expression */ |
| 5338 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 5339 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5340 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5341 | 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] | 5342 | |
| 5343 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5344 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5345 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5346 | 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] | 5347 | |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5348 | /* duplicate extensions */ |
| 5349 | iff[j].ext_size = rfn->iffeature[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5350 | 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] | 5351 | &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres); |
| 5352 | } |
| 5353 | (*old_size)++; |
| 5354 | } |
| 5355 | assert(*old_size == size); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5356 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | /* apply augments */ |
| 5360 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 5361 | rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5362 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5363 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5364 | } |
| 5365 | } |
| 5366 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5367 | /* check refines */ |
| 5368 | for (i = 0; i < uses->refine_size; i++) { |
| 5369 | node = refine_nodes[i]; |
| 5370 | rfn = &uses->refine[i]; |
| 5371 | |
| 5372 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5373 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5374 | 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] | 5375 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5376 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 5377 | (rfn->flags & LYS_CONFIG_W)) { |
| 5378 | /* setting config true under config false is prohibited */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5379 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5380 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5381 | "changing config from 'false' to 'true' is prohibited while " |
| 5382 | "the target's parent is still config 'false'."); |
| 5383 | goto fail; |
| 5384 | } |
| 5385 | |
| 5386 | /* inherit config change to the target children */ |
| 5387 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 5388 | if (rfn->flags & LYS_CONFIG_W) { |
| 5389 | if (iter->flags & LYS_CONFIG_SET) { |
| 5390 | /* config is set explicitely, go to next sibling */ |
| 5391 | next = NULL; |
| 5392 | goto nextsibling; |
| 5393 | } |
| 5394 | } else { /* LYS_CONFIG_R */ |
| 5395 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 5396 | /* error - we would have config data under status data */ |
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 'true' to 'false' is prohibited while the target " |
| 5400 | "has still a children with explicit config 'true'."); |
| 5401 | goto fail; |
| 5402 | } |
| 5403 | } |
| 5404 | /* change config */ |
| 5405 | iter->flags &= ~LYS_CONFIG_MASK; |
| 5406 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 5407 | |
| 5408 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5409 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5410 | next = NULL; |
| 5411 | } else { |
| 5412 | next = iter->child; |
| 5413 | } |
| 5414 | nextsibling: |
| 5415 | if (!next) { |
| 5416 | /* try siblings */ |
| 5417 | next = iter->next; |
| 5418 | } |
| 5419 | while (!next) { |
| 5420 | /* parent is already processed, go to its sibling */ |
| 5421 | iter = lys_parent(iter); |
| 5422 | |
| 5423 | /* no siblings, go back through parents */ |
| 5424 | if (iter == node) { |
| 5425 | /* we are done, no next element to process */ |
| 5426 | break; |
| 5427 | } |
| 5428 | next = iter->next; |
| 5429 | } |
| 5430 | } |
| 5431 | } |
| 5432 | |
| 5433 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5434 | if (rfn->dflt_size) { |
| 5435 | if (node->nodetype == LYS_CHOICE) { |
| 5436 | /* choice */ |
| 5437 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5438 | rfn->dflt[0]); |
| 5439 | if (!((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5440 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5441 | goto fail; |
| 5442 | } |
| 5443 | if (lyp_check_mandatory_choice(node)) { |
| 5444 | goto fail; |
| 5445 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5450 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5451 | 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] | 5452 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5453 | 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] | 5454 | goto fail; |
| 5455 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5456 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5457 | 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] | 5458 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5459 | 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] | 5460 | goto fail; |
| 5461 | } |
| 5462 | } |
| 5463 | |
| 5464 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5465 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5466 | if (node->nodetype == LYS_LEAFLIST) { |
| 5467 | llist = (struct lys_node_leaflist *)node; |
| 5468 | if (llist->dflt_size && llist->min) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5469 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5470 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5471 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5472 | goto fail; |
| 5473 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5474 | } else if (node->nodetype == LYS_LEAF) { |
| 5475 | leaf = (struct lys_node_leaf *)node; |
| 5476 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5477 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5478 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5479 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5480 | goto fail; |
| 5481 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5482 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5483 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5484 | /* 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] | 5485 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5486 | for (parent = node->parent; |
| 5487 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5488 | parent = parent->parent) { |
| 5489 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5490 | /* stop also on presence containers */ |
| 5491 | break; |
| 5492 | } |
| 5493 | } |
| 5494 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5495 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5496 | if (lyp_check_mandatory_choice(parent)) { |
| 5497 | goto fail; |
| 5498 | } |
| 5499 | } |
| 5500 | } |
| 5501 | } |
| 5502 | free(refine_nodes); |
| 5503 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5504 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5505 | |
| 5506 | fail: |
| 5507 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5508 | lys_node_free(iter, NULL, 0); |
| 5509 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5510 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5511 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5512 | } |
| 5513 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5514 | void |
| 5515 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5516 | { |
| 5517 | int i; |
| 5518 | |
| 5519 | assert(der && base); |
| 5520 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5521 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5522 | /* create a set for backlinks if it does not exist */ |
| 5523 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5524 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5525 | /* store backlink */ |
| 5526 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5527 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5528 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5529 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5530 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5531 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5532 | } |
| 5533 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5534 | /** |
| 5535 | * @brief Resolve base identity recursively. Does not log. |
| 5536 | * |
| 5537 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5538 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5539 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5540 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5541 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5542 | * @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] | 5543 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5544 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5545 | 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] | 5546 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5547 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5548 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5549 | struct lys_ident *base = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5550 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5551 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5552 | assert(ret); |
| 5553 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5554 | /* search module */ |
| 5555 | for (i = 0; i < module->ident_size; i++) { |
| 5556 | if (!strcmp(basename, module->ident[i].name)) { |
| 5557 | |
| 5558 | if (!ident) { |
| 5559 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5560 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5561 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5562 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5563 | } |
| 5564 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5565 | base = &module->ident[i]; |
| 5566 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5571 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5572 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5573 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5574 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5575 | if (!ident) { |
| 5576 | *ret = &module->inc[j].submodule->ident[i]; |
| 5577 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5578 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5579 | |
| 5580 | base = &module->inc[j].submodule->ident[i]; |
| 5581 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5582 | } |
| 5583 | } |
| 5584 | } |
| 5585 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5586 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5587 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5588 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5589 | /* is it already completely resolved? */ |
| 5590 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5591 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5592 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5593 | |
| 5594 | /* simple check for circular reference, |
| 5595 | * the complete check is done as a side effect of using only completely |
| 5596 | * resolved identities (previous check of unres content) */ |
| 5597 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5598 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5599 | 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] | 5600 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5601 | } |
| 5602 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5603 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5604 | } |
| 5605 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5606 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5607 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5608 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5609 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5612 | /* base not found (maybe a forward reference) */ |
| 5613 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5614 | } |
| 5615 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5616 | /** |
| 5617 | * @brief Resolve base identity. Logs directly. |
| 5618 | * |
| 5619 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5620 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5621 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5622 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5623 | * @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] | 5624 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5625 | * @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] | 5626 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5627 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5628 | 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] | 5629 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5630 | { |
| 5631 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5632 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5633 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5634 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5635 | struct lys_module *mod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5636 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5637 | |
| 5638 | assert((ident && !type) || (!ident && type)); |
| 5639 | |
| 5640 | if (!type) { |
| 5641 | /* have ident to resolve */ |
| 5642 | ret = ⌖ |
| 5643 | flags = ident->flags; |
| 5644 | mod = ident->module; |
| 5645 | } else { |
| 5646 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5647 | ++type->info.ident.count; |
| 5648 | 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] | 5649 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5650 | |
| 5651 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5652 | flags = type->parent->flags; |
| 5653 | mod = type->parent->module; |
| 5654 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5655 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5656 | |
| 5657 | /* search for the base identity */ |
| 5658 | name = strchr(basename, ':'); |
| 5659 | if (name) { |
| 5660 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5661 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5662 | name++; |
| 5663 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5664 | 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] | 5665 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5666 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5667 | } |
| 5668 | } else { |
| 5669 | name = basename; |
| 5670 | } |
| 5671 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5672 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 5673 | 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] | 5674 | if (!module) { |
| 5675 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5676 | LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5677 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5678 | } |
| 5679 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5680 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5681 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5682 | if (!rc) { |
| 5683 | assert(*ret); |
| 5684 | |
| 5685 | /* check status */ |
| 5686 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5687 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5688 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5689 | } else if (ident) { |
| 5690 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5691 | if (lys_main_module(mod)->implemented) { |
| 5692 | /* in case of the implemented identity, maintain backlinks to it |
| 5693 | * from the base identities to make it available when resolving |
| 5694 | * data with the identity values (not implemented identity is not |
| 5695 | * allowed as an identityref value). */ |
| 5696 | resolve_identity_backlink_update(ident, *ret); |
| 5697 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5698 | } |
| 5699 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5700 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5701 | if (type) { |
| 5702 | --type->info.ident.count; |
| 5703 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5706 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5707 | } |
| 5708 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5709 | /* |
| 5710 | * 1 - true (der is derived from base) |
| 5711 | * 0 - false (der is not derived from base) |
| 5712 | */ |
| 5713 | static int |
| 5714 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5715 | { |
| 5716 | int i; |
| 5717 | |
| 5718 | if (der == base) { |
| 5719 | return 1; |
| 5720 | } else { |
| 5721 | for(i = 0; i < der->base_size; i++) { |
| 5722 | if (search_base_identity(der->base[i], base) == 1) { |
| 5723 | return 1; |
| 5724 | } |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | return 0; |
| 5729 | } |
| 5730 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5731 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5732 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5733 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5734 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5735 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5736 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5737 | * @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] | 5738 | * |
| 5739 | * @return Pointer to the identity resolvent, NULL on error. |
| 5740 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5741 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5742 | 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] | 5743 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5744 | const char *mod_name, *name; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5745 | char *str; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5746 | int mod_name_len, nam_len, rc; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5747 | int need_implemented = 0; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5748 | unsigned int i, j; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5749 | struct lys_ident *der, *cur; |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5750 | struct lys_module *imod = NULL, *m, *tmod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5751 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5752 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5753 | assert(type && ident_name && node && mod); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5754 | ctx = mod->ctx; |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5755 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5756 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5757 | return NULL; |
| 5758 | } |
| 5759 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5760 | 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] | 5761 | if (rc < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5762 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5763 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5764 | } else if (rc < (signed)strlen(ident_name)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5765 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5766 | return NULL; |
| 5767 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5768 | |
| 5769 | m = lys_main_module(mod); /* shortcut */ |
| 5770 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5771 | /* identity is defined in the same module as node */ |
| 5772 | imod = m; |
| 5773 | } else if (dflt) { |
| 5774 | /* solving identityref in default definition in schema - |
| 5775 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5776 | for (i = 0; i < mod->imp_size; i++) { |
| 5777 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5778 | imod = mod->imp[i].module; |
| 5779 | break; |
| 5780 | } |
| 5781 | } |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5782 | |
| 5783 | /* We may need to pull it from the module that the typedef came from */ |
| 5784 | if (!imod && type && type->der) { |
| 5785 | tmod = type->der->module; |
| 5786 | for (i = 0; i < tmod->imp_size; i++) { |
| 5787 | if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) { |
| 5788 | imod = tmod->imp[i].module; |
| 5789 | break; |
| 5790 | } |
| 5791 | } |
| 5792 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5793 | } else { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5794 | /* solving identityref in data - get the module from the context */ |
| 5795 | for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) { |
| 5796 | imod = mod->ctx->models.list[i]; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5797 | 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] | 5798 | break; |
| 5799 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5800 | imod = NULL; |
| 5801 | } |
Radek Krejci | 5ba0510 | 2017-10-26 15:02:52 +0200 | [diff] [blame] | 5802 | if (!imod && mod->ctx->models.parsing_sub_modules_count) { |
| 5803 | /* we are currently parsing some module and checking XPath or a default value, |
| 5804 | * so take this module into account */ |
| 5805 | for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) { |
| 5806 | imod = mod->ctx->models.parsing_sub_modules[i]; |
| 5807 | if (imod->type) { |
| 5808 | /* skip submodules */ |
| 5809 | continue; |
| 5810 | } |
| 5811 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5812 | break; |
| 5813 | } |
| 5814 | imod = NULL; |
| 5815 | } |
| 5816 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5817 | } |
| 5818 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5819 | if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5820 | /* the needed module was not found, but it may have been expected so call the data callback */ |
| 5821 | if (imod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5822 | 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] | 5823 | } else if (mod_name) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5824 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5825 | 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] | 5826 | free(str); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5827 | } |
| 5828 | } |
| 5829 | if (!imod) { |
| 5830 | goto fail; |
| 5831 | } |
| 5832 | |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5833 | if (m != imod || lys_main_module(type->parent->module) != mod) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5834 | /* the type is not referencing the same schema, |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5835 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5836 | * contains the identity */ |
| 5837 | if (!imod->implemented) { |
| 5838 | cur = NULL; |
| 5839 | /* get the identity in the module */ |
| 5840 | for (i = 0; i < imod->ident_size; i++) { |
| 5841 | if (!strcmp(name, imod->ident[i].name)) { |
| 5842 | cur = &imod->ident[i]; |
| 5843 | break; |
| 5844 | } |
| 5845 | } |
| 5846 | if (!cur) { |
| 5847 | /* go through includes */ |
| 5848 | for (j = 0; j < imod->inc_size; j++) { |
| 5849 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5850 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5851 | cur = &imod->inc[j].submodule->ident[i]; |
| 5852 | break; |
| 5853 | } |
| 5854 | } |
| 5855 | } |
| 5856 | if (!cur) { |
| 5857 | goto fail; |
| 5858 | } |
| 5859 | } |
| 5860 | |
| 5861 | /* check that identity is derived from one of the type's base */ |
| 5862 | while (type->der) { |
| 5863 | for (i = 0; i < type->info.ident.count; i++) { |
| 5864 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5865 | /* cur's base matches the type's base */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5866 | need_implemented = 1; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5867 | goto match; |
| 5868 | } |
| 5869 | } |
| 5870 | type = &type->der->type; |
| 5871 | } |
| 5872 | /* matching base not found */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5873 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented."); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5874 | goto fail; |
| 5875 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5876 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5877 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5878 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5879 | while (type->der) { |
| 5880 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5881 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5882 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5883 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5884 | /* there are some derived identities */ |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5885 | for (j = 0; j < cur->der->number; j++) { |
| 5886 | der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5887 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5888 | /* we have match */ |
| 5889 | cur = der; |
| 5890 | goto match; |
| 5891 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5892 | } |
| 5893 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5894 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5895 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5896 | } |
| 5897 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5898 | fail: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5899 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5900 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5901 | |
| 5902 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5903 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5904 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5905 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5906 | 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] | 5907 | return NULL; |
| 5908 | } |
| 5909 | } |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5910 | if (need_implemented) { |
| 5911 | if (dflt) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5912 | /* later try to make the module implemented */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5913 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5914 | imod->name, cur->name, mod->name); |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5915 | /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of |
| 5916 | * several functions with little gain */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5917 | if (lys_set_implemented(imod)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5918 | 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] | 5919 | imod->name, cur->name); |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5920 | goto fail; |
| 5921 | } |
| 5922 | } else { |
| 5923 | /* just say that it was found, but in a non-implemented module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5924 | 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] | 5925 | lys_main_module(cur->module)->name); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5926 | goto fail; |
| 5927 | } |
| 5928 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5929 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5930 | } |
| 5931 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5932 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5933 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5934 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5935 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5936 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5937 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5938 | * @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] | 5939 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5940 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5941 | 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] | 5942 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5943 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5944 | struct lys_node *par_grp; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5945 | struct ly_ctx *ctx = uses->module->ctx; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5946 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5947 | /* 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] | 5948 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5949 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5950 | * 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] | 5951 | 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^] | 5952 | if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) { |
| 5953 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
| 5954 | return -1; |
| 5955 | } |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5956 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5957 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5958 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5959 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5960 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5961 | return -1; |
| 5962 | } else if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5963 | 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] | 5964 | return -1; |
| 5965 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5966 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5967 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5968 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5969 | return -1; |
| 5970 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5971 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5972 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5973 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5974 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5975 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5976 | } |
| 5977 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5978 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5979 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5980 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5981 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5982 | return -1; |
| 5983 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5984 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 5985 | } else { |
| 5986 | /* instantiate grouping only when it is completely resolved */ |
| 5987 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5988 | } |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame^] | 5989 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5990 | return EXIT_FAILURE; |
| 5991 | } |
| 5992 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5993 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5994 | if (!rc) { |
| 5995 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5996 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5997 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 5998 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5999 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6000 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6001 | |
| 6002 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6003 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 6004 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6005 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6006 | return -1; |
| 6007 | } |
| 6008 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6009 | return EXIT_SUCCESS; |
| 6010 | } |
| 6011 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6012 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6013 | } |
| 6014 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6015 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 6016 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6017 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6018 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6019 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6020 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6021 | * @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] | 6022 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6023 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6024 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6025 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6026 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 6027 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6028 | char *s = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6029 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6030 | |
| 6031 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 6032 | assert(keys_str); |
| 6033 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6034 | if (!list->child) { |
| 6035 | /* no child, possible forward reference */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6036 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6037 | return EXIT_FAILURE; |
| 6038 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6039 | /* get the key name */ |
| 6040 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 6041 | len = value - keys_str; |
| 6042 | while (isspace(value[0])) { |
| 6043 | value++; |
| 6044 | } |
| 6045 | } else { |
| 6046 | len = strlen(keys_str); |
| 6047 | } |
| 6048 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6049 | if (list->keys[i]) { |
| 6050 | /* skip already resolved keys */ |
| 6051 | goto next; |
| 6052 | } |
| 6053 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6054 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 6055 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6056 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6057 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 6058 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6059 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6060 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6061 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6062 | /* check_key logs */ |
| 6063 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6064 | } |
| 6065 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6066 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6067 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6068 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 6069 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6070 | return -1; |
| 6071 | } |
| 6072 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6073 | /* default value - is ignored, keep it but print a warning */ |
| 6074 | if (list->keys[i]->dflt) { |
| 6075 | /* log is not hidden only in case this resolving fails and in such a case |
| 6076 | * we cannot get here |
| 6077 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6078 | assert(log_opt == ILO_STORE); |
| 6079 | log_opt = ILO_LOG; |
| 6080 | 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] | 6081 | 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] | 6082 | log_opt = ILO_STORE; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6083 | free(s); |
| 6084 | } |
| 6085 | |
| 6086 | next: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6087 | /* prepare for next iteration */ |
| 6088 | while (value && isspace(value[0])) { |
| 6089 | value++; |
| 6090 | } |
| 6091 | keys_str = value; |
| 6092 | } |
| 6093 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6094 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6095 | } |
| 6096 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6097 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6098 | * @brief Resolve (check) all must conditions of \p node. |
| 6099 | * Logs directly. |
| 6100 | * |
| 6101 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6102 | * @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] | 6103 | * |
| 6104 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 6105 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6106 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6107 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6108 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6109 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6110 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6111 | struct lys_restr *must; |
| 6112 | struct lyxp_set set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6113 | struct ly_ctx *ctx = node->schema->module->ctx; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6114 | |
| 6115 | assert(node); |
| 6116 | memset(&set, 0, sizeof set); |
| 6117 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6118 | if (inout_parent) { |
| 6119 | for (schema = lys_parent(node->schema); |
| 6120 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 6121 | schema = lys_parent(schema)); |
| 6122 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6123 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6124 | return -1; |
| 6125 | } |
| 6126 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 6127 | must = ((struct lys_node_inout *)schema)->must; |
| 6128 | |
| 6129 | /* context node is the RPC/action */ |
| 6130 | node = node->parent; |
| 6131 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6132 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6133 | return -1; |
| 6134 | } |
| 6135 | } else { |
| 6136 | switch (node->schema->nodetype) { |
| 6137 | case LYS_CONTAINER: |
| 6138 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 6139 | must = ((struct lys_node_container *)node->schema)->must; |
| 6140 | break; |
| 6141 | case LYS_LEAF: |
| 6142 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 6143 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 6144 | break; |
| 6145 | case LYS_LEAFLIST: |
| 6146 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 6147 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 6148 | break; |
| 6149 | case LYS_LIST: |
| 6150 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 6151 | must = ((struct lys_node_list *)node->schema)->must; |
| 6152 | break; |
| 6153 | case LYS_ANYXML: |
| 6154 | case LYS_ANYDATA: |
| 6155 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 6156 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 6157 | break; |
| 6158 | case LYS_NOTIF: |
| 6159 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 6160 | must = ((struct lys_node_notif *)node->schema)->must; |
| 6161 | break; |
| 6162 | default: |
| 6163 | must_size = 0; |
| 6164 | break; |
| 6165 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6169 | 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] | 6170 | return -1; |
| 6171 | } |
| 6172 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6173 | 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] | 6174 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6175 | if (!set.val.bool) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6176 | 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] | 6177 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 6178 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6179 | LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6180 | if (must[i].emsg) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6181 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6182 | } |
| 6183 | if (must[i].eapptag) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6184 | ly_err_last_set_apptag(ctx, must[i].eapptag); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6185 | } |
| 6186 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6187 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6188 | } |
| 6189 | } |
| 6190 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6191 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6192 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6193 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6194 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6195 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 6196 | * |
| 6197 | * @param[in] schema Schema node with the when condition. |
| 6198 | * @param[out] ctx_snode When schema context node. |
| 6199 | * @param[out] ctx_snode_type Schema context node type. |
| 6200 | */ |
| 6201 | void |
| 6202 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 6203 | { |
| 6204 | const struct lys_node *sparent; |
| 6205 | |
| 6206 | /* find a not schema-only node */ |
| 6207 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 6208 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 6209 | if (schema->nodetype == LYS_AUGMENT) { |
| 6210 | sparent = ((struct lys_node_augment *)schema)->target; |
| 6211 | } else { |
| 6212 | sparent = schema->parent; |
| 6213 | } |
| 6214 | if (!sparent) { |
| 6215 | /* context node is the document root (fake root in our case) */ |
| 6216 | if (schema->flags & LYS_CONFIG_W) { |
| 6217 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 6218 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 6219 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6220 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 6221 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 6222 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6223 | break; |
| 6224 | } |
| 6225 | schema = sparent; |
| 6226 | } |
| 6227 | |
| 6228 | *ctx_snode = (struct lys_node *)schema; |
| 6229 | } |
| 6230 | |
| 6231 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6232 | * @brief Resolve (find) when condition context node. Does not log. |
| 6233 | * |
| 6234 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6235 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6236 | * @param[out] ctx_node Context node. |
| 6237 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6238 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6239 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6240 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6241 | static int |
| 6242 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 6243 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6244 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6245 | struct lyd_node *parent; |
| 6246 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6247 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6248 | uint16_t i, data_depth, schema_depth; |
| 6249 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6250 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6251 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6252 | if (node_type == LYXP_NODE_ELEM) { |
| 6253 | /* standard element context node */ |
| 6254 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 6255 | for (sparent = schema, schema_depth = 0; |
| 6256 | sparent; |
| 6257 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 6258 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 6259 | ++schema_depth; |
| 6260 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6261 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6262 | if (data_depth < schema_depth) { |
| 6263 | return -1; |
| 6264 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6265 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 6266 | /* find the corresponding data node */ |
| 6267 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 6268 | node = node->parent; |
| 6269 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6270 | if (node->schema != schema) { |
| 6271 | return -1; |
| 6272 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6273 | } else { |
| 6274 | /* root context node */ |
| 6275 | while (node->parent) { |
| 6276 | node = node->parent; |
| 6277 | } |
| 6278 | while (node->prev->next) { |
| 6279 | node = node->prev; |
| 6280 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6281 | } |
| 6282 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6283 | *ctx_node = node; |
| 6284 | *ctx_node_type = node_type; |
| 6285 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6286 | } |
| 6287 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6288 | /** |
| 6289 | * @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] | 6290 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6291 | * |
| 6292 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 6293 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 6294 | * it is moved to point to another sibling still in the original tree. |
| 6295 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 6296 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 6297 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 6298 | * Ordering may change, but there will be no semantic change. |
| 6299 | * |
| 6300 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6301 | */ |
| 6302 | static int |
| 6303 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 6304 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 6305 | { |
| 6306 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6307 | const struct lys_node *slast; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6308 | struct ly_ctx *ctx = snode->module->ctx; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6309 | |
| 6310 | switch (snode->nodetype) { |
| 6311 | case LYS_AUGMENT: |
| 6312 | case LYS_USES: |
| 6313 | case LYS_CHOICE: |
| 6314 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6315 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 6316 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6317 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 6318 | continue; |
| 6319 | } |
| 6320 | |
| 6321 | 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] | 6322 | return -1; |
| 6323 | } |
| 6324 | } |
| 6325 | break; |
| 6326 | case LYS_CONTAINER: |
| 6327 | case LYS_LIST: |
| 6328 | case LYS_LEAF: |
| 6329 | case LYS_LEAFLIST: |
| 6330 | case LYS_ANYXML: |
| 6331 | case LYS_ANYDATA: |
| 6332 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 6333 | if (elem->schema == snode) { |
| 6334 | |
| 6335 | if (elem == *ctx_node) { |
| 6336 | /* We are going to unlink our context node! This normally cannot happen, |
| 6337 | * but we use normal top-level data nodes for faking a document root node, |
| 6338 | * so if this is the context node, we just use the next top-level node. |
| 6339 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 6340 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 6341 | * lyxp_eval() can handle this special situation. |
| 6342 | */ |
| 6343 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6344 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6345 | return -1; |
| 6346 | } |
| 6347 | |
| 6348 | if (elem->prev == elem) { |
| 6349 | /* unlinking last top-level element, use an empty data tree */ |
| 6350 | *ctx_node = NULL; |
| 6351 | } else { |
| 6352 | /* in this case just use the previous/last top-level data node */ |
| 6353 | *ctx_node = elem->prev; |
| 6354 | } |
| 6355 | } else if (elem == *node) { |
| 6356 | /* We are going to unlink the currently processed node. This does not matter that |
| 6357 | * much, but we would lose access to the original data tree, so just move our |
| 6358 | * pointer somewhere still inside it. |
| 6359 | */ |
| 6360 | if ((*node)->prev != *node) { |
| 6361 | *node = (*node)->prev; |
| 6362 | } else { |
| 6363 | /* the processed node with sibings were all unlinked, oh well */ |
| 6364 | *node = NULL; |
| 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6369 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6370 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6371 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6372 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6373 | return -1; |
| 6374 | } |
| 6375 | } else { |
| 6376 | *unlinked_nodes = elem; |
| 6377 | } |
| 6378 | |
| 6379 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 6380 | /* there can be only one instance */ |
| 6381 | break; |
| 6382 | } |
| 6383 | } |
| 6384 | } |
| 6385 | break; |
| 6386 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6387 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6388 | return -1; |
| 6389 | } |
| 6390 | |
| 6391 | return EXIT_SUCCESS; |
| 6392 | } |
| 6393 | |
| 6394 | /** |
| 6395 | * @brief Relink the unlinked nodes back. |
| 6396 | * |
| 6397 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 6398 | * we simply need a sibling from the original data tree. |
| 6399 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 6400 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 6401 | * or the sibling of \p unlinked_nodes. |
| 6402 | * |
| 6403 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6404 | */ |
| 6405 | static int |
| 6406 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 6407 | { |
| 6408 | struct lyd_node *elem; |
| 6409 | |
| 6410 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6411 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6412 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6413 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6414 | return -1; |
| 6415 | } |
| 6416 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6417 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6418 | return -1; |
| 6419 | } |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | return EXIT_SUCCESS; |
| 6424 | } |
| 6425 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6426 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6427 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6428 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6429 | int ret = 0; |
| 6430 | uint8_t must_size; |
| 6431 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6432 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6433 | assert(node); |
| 6434 | |
| 6435 | schema = node->schema; |
| 6436 | |
| 6437 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6438 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6439 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6440 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 6441 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6442 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6443 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 6444 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6445 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6446 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 6447 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6448 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6449 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 6450 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6451 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 6452 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6453 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 6454 | break; |
| 6455 | case LYS_NOTIF: |
| 6456 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 6457 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6458 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6459 | must_size = 0; |
| 6460 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6461 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6462 | |
| 6463 | if (must_size) { |
| 6464 | ++ret; |
| 6465 | } |
| 6466 | |
| 6467 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 6468 | if (!node->prev->next) { |
| 6469 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 6470 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 6471 | ret += 0x2; |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6479 | 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] | 6480 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6481 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6482 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6483 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6484 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6485 | if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6486 | return 1; |
| 6487 | } |
| 6488 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6489 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6490 | goto check_augment; |
| 6491 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6492 | while (parent) { |
| 6493 | /* stop conditions */ |
| 6494 | if (!mode) { |
| 6495 | /* stop on node that can be instantiated in data tree */ |
| 6496 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6497 | break; |
| 6498 | } |
| 6499 | } else { |
| 6500 | /* stop on the specified node */ |
| 6501 | if (parent == stop) { |
| 6502 | break; |
| 6503 | } |
| 6504 | } |
| 6505 | |
| 6506 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6507 | return 1; |
| 6508 | } |
| 6509 | check_augment: |
| 6510 | |
| 6511 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6512 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 6513 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6514 | } |
| 6515 | parent = lys_parent(parent); |
| 6516 | } |
| 6517 | |
| 6518 | return 0; |
| 6519 | } |
| 6520 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6521 | /** |
| 6522 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6523 | * Logs directly. |
| 6524 | * |
| 6525 | * @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] | 6526 | * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied |
| 6527 | * only when requiring external dependencies. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6528 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6529 | * @return |
| 6530 | * -1 - error, ly_errno is set |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6531 | * 0 - all "when" statements true |
| 6532 | * 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] | 6533 | * 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] | 6534 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6535 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6536 | 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] | 6537 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6538 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6539 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6540 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6541 | enum lyxp_node_type ctx_node_type; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6542 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6543 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6544 | |
| 6545 | assert(node); |
| 6546 | memset(&set, 0, sizeof set); |
| 6547 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6548 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6549 | /* make the node dummy for the evaluation */ |
| 6550 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6551 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6552 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6553 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6554 | if (rc) { |
| 6555 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6556 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6557 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6558 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6559 | } |
| 6560 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6561 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6562 | 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] | 6563 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6564 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6565 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6566 | || ((((struct lys_node_container *)node->schema)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6567 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6568 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6569 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6570 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6571 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6572 | if (failed_when) { |
| 6573 | *failed_when = ((struct lys_node_container *)node->schema)->when; |
| 6574 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6575 | goto cleanup; |
| 6576 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6577 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6578 | |
| 6579 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6580 | 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] | 6581 | } |
| 6582 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6583 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6584 | goto check_augment; |
| 6585 | |
| 6586 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6587 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6588 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6589 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6590 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6591 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6592 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6593 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6594 | } |
| 6595 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6596 | |
| 6597 | unlinked_nodes = NULL; |
| 6598 | /* we do not want our node pointer to change */ |
| 6599 | tmp_node = node; |
| 6600 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6601 | if (rc) { |
| 6602 | goto cleanup; |
| 6603 | } |
| 6604 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6605 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6606 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6607 | |
| 6608 | if (unlinked_nodes && ctx_node) { |
| 6609 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6610 | rc = -1; |
| 6611 | goto cleanup; |
| 6612 | } |
| 6613 | } |
| 6614 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6615 | if (rc) { |
| 6616 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6617 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6618 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6619 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6620 | } |
| 6621 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6622 | 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] | 6623 | if (!set.val.bool) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6624 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6625 | || ((((struct lys_node_uses *)sparent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6626 | || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6627 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6628 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6629 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6630 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6631 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6632 | if (failed_when) { |
| 6633 | *failed_when = ((struct lys_node_uses *)sparent)->when; |
| 6634 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6635 | goto cleanup; |
| 6636 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6637 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6638 | |
| 6639 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6640 | 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] | 6641 | } |
| 6642 | |
| 6643 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6644 | if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6645 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6646 | 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] | 6647 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6648 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6649 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6650 | } |
| 6651 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6652 | |
| 6653 | unlinked_nodes = NULL; |
| 6654 | tmp_node = node; |
| 6655 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6656 | if (rc) { |
| 6657 | goto cleanup; |
| 6658 | } |
| 6659 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6660 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6661 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6662 | |
| 6663 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6664 | * so the tree did not actually change and there is nothing for us to do |
| 6665 | */ |
| 6666 | if (unlinked_nodes && ctx_node) { |
| 6667 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6668 | rc = -1; |
| 6669 | goto cleanup; |
| 6670 | } |
| 6671 | } |
| 6672 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6673 | if (rc) { |
| 6674 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6675 | LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6676 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6677 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6678 | } |
| 6679 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6680 | 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] | 6681 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6682 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6683 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6684 | || ((((struct lys_node_augment *)sparent->parent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6685 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6686 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6687 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6688 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6689 | LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6690 | if (failed_when) { |
| 6691 | *failed_when = ((struct lys_node_augment *)sparent->parent)->when; |
| 6692 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6693 | goto cleanup; |
| 6694 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6695 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6696 | |
| 6697 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6698 | 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] | 6699 | } |
| 6700 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6701 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6702 | } |
| 6703 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6704 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6705 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6706 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6707 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6708 | 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] | 6709 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6710 | } |
| 6711 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6712 | static int |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6713 | check_type_union_leafref(struct lys_type *type) |
| 6714 | { |
| 6715 | uint8_t i; |
| 6716 | |
| 6717 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6718 | /* go through unions and look for leafref */ |
| 6719 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6720 | switch (type->info.uni.types[i].base) { |
| 6721 | case LY_TYPE_LEAFREF: |
| 6722 | return 1; |
| 6723 | case LY_TYPE_UNION: |
| 6724 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6725 | return 1; |
| 6726 | } |
| 6727 | break; |
| 6728 | default: |
| 6729 | break; |
| 6730 | } |
| 6731 | } |
| 6732 | |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
| 6736 | /* just inherit the flag value */ |
| 6737 | return type->der->has_union_leafref; |
| 6738 | } |
| 6739 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6740 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6741 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6742 | * |
| 6743 | * @param[in] mod Main module. |
| 6744 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6745 | * @param[in] type Type of the unresolved item. |
| 6746 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6747 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6748 | * @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] | 6749 | * |
| 6750 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6751 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6752 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6753 | 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] | 6754 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6755 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6756 | /* 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] | 6757 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6758 | unsigned int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6759 | struct ly_ctx * ctx = mod->ctx; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6760 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6761 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6762 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6763 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6764 | struct ly_set *refs, *procs; |
| 6765 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6766 | struct lys_ident *ident; |
| 6767 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6768 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6769 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6770 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6771 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6772 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6773 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6774 | struct lys_ext_instance *ext, **extlist; |
| 6775 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6776 | |
| 6777 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6778 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6779 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6780 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6781 | ident = item; |
| 6782 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6783 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6784 | break; |
| 6785 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6786 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6787 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6788 | stype = item; |
| 6789 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6790 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6791 | break; |
| 6792 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6793 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6794 | stype = item; |
| 6795 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 6796 | rc = resolve_schema_leafref(stype, node, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6797 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6798 | case UNRES_TYPE_DER_EXT: |
| 6799 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6800 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6801 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6802 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6803 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6804 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6805 | /* parent */ |
| 6806 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6807 | stype = item; |
| 6808 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6809 | /* HACK type->der is temporarily unparsed type statement */ |
| 6810 | yin = (struct lyxml_elem *)stype->der; |
| 6811 | stype->der = NULL; |
| 6812 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6813 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6814 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6815 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6816 | |
| 6817 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6818 | /* may try again later */ |
| 6819 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6820 | } else { |
| 6821 | /* 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] | 6822 | lydict_remove(ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6823 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6824 | } |
| 6825 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6826 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6827 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6828 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6829 | /* 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] | 6830 | lyxml_free(ctx, yin); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6831 | } else { |
| 6832 | /* may try again later, put all back how it was */ |
| 6833 | stype->der = (struct lys_tpdf *)yin; |
| 6834 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6835 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6836 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6837 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6838 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6839 | 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] | 6840 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6841 | |
| 6842 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6843 | /* fill typedef union leafref flag */ |
| 6844 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6845 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6846 | /* copy the type in case it has union leafref flag */ |
| 6847 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6848 | LOGERR(ctx, LY_EINT, "Failed to duplicate type."); |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6849 | return -1; |
| 6850 | } |
| 6851 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6852 | } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6853 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6854 | * 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] | 6855 | * grouping. The grouping cannot be used unless the unres counter is 0. |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 6856 | * 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] | 6857 | * of the type's base member. */ |
| 6858 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6859 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6860 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6861 | LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6862 | return -1; |
| 6863 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6864 | stype->value_flags |= LY_VALUE_UNRESGRP; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6865 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6866 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6867 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6868 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6869 | iff_data = str_snode; |
| 6870 | 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] | 6871 | if (!rc) { |
| 6872 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6873 | if (iff_data->infeature) { |
| 6874 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6875 | feat = *((struct lys_feature **)item); |
| 6876 | if (!feat->depfeatures) { |
| 6877 | feat->depfeatures = ly_set_new(); |
| 6878 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6879 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6880 | } |
| 6881 | /* cleanup temporary data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6882 | lydict_remove(ctx, iff_data->fname); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6883 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6884 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6885 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6886 | case UNRES_FEATURE: |
| 6887 | feat = (struct lys_feature *)item; |
| 6888 | |
| 6889 | if (feat->iffeature_size) { |
| 6890 | refs = ly_set_new(); |
| 6891 | procs = ly_set_new(); |
| 6892 | ly_set_add(procs, feat, 0); |
| 6893 | |
| 6894 | while (procs->number) { |
| 6895 | ref = procs->set.g[procs->number - 1]; |
| 6896 | ly_set_rm_index(procs, procs->number - 1); |
| 6897 | |
| 6898 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6899 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6900 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6901 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6902 | if (ref->iffeature[i].features[j - 1] == feat) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6903 | LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6904 | goto featurecheckdone; |
| 6905 | } |
| 6906 | |
| 6907 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6908 | k = refs->number; |
| 6909 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6910 | /* not yet seen feature, add it for processing */ |
| 6911 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6912 | } |
| 6913 | } |
| 6914 | } else { |
| 6915 | /* forward reference */ |
| 6916 | rc = EXIT_FAILURE; |
| 6917 | goto featurecheckdone; |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | } |
| 6922 | } |
| 6923 | rc = EXIT_SUCCESS; |
| 6924 | |
| 6925 | featurecheckdone: |
| 6926 | ly_set_free(refs); |
| 6927 | ly_set_free(procs); |
| 6928 | } |
| 6929 | |
| 6930 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6931 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6932 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6933 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6934 | case UNRES_TYPEDEF_DFLT: |
| 6935 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6936 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6937 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6938 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6939 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6940 | break; |
| 6941 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6942 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6943 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6944 | choic = item; |
| 6945 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6946 | if (!choic->dflt) { |
| 6947 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6948 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6949 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6950 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6951 | } else { |
| 6952 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6953 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6954 | break; |
| 6955 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6956 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6957 | break; |
| 6958 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6959 | unique_info = (struct unres_list_uniq *)item; |
| 6960 | 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] | 6961 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6962 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6963 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6964 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6965 | case UNRES_XPATH: |
| 6966 | node = (struct lys_node *)item; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 6967 | rc = check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6968 | break; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 6969 | case UNRES_MOD_IMPLEMENT: |
| 6970 | rc = lys_make_implemented_r(mod, unres); |
| 6971 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6972 | case UNRES_EXT: |
| 6973 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6974 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6975 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6976 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6977 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6978 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6979 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6980 | if (eplugin) { |
| 6981 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6982 | u = malloc(sizeof *u); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6983 | LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6984 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6985 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6986 | /* something really bad happend since the extension finalization is not actually |
| 6987 | * being resolved while adding into unres, so something more serious with the unres |
| 6988 | * list itself must happened */ |
| 6989 | return -1; |
| 6990 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6991 | } |
| 6992 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6993 | } |
| 6994 | if (!rc || rc == -1) { |
| 6995 | /* cleanup on success or fatal error */ |
| 6996 | if (ext_data->datatype == LYS_IN_YIN) { |
| 6997 | /* YIN */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6998 | lyxml_free(ctx, ext_data->data.yin); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6999 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 7000 | /* YANG */ |
| 7001 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7002 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7003 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7004 | } |
| 7005 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7006 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7007 | u = (uint8_t *)str_snode; |
| 7008 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 7009 | free(u); |
| 7010 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7011 | eplugin = ext->def->plugin; |
| 7012 | |
| 7013 | /* inherit */ |
| 7014 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 7015 | root = (struct lys_node *)ext->parent; |
| 7016 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 7017 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 7018 | /* first, check if the node already contain instance of the same extension, |
| 7019 | * in such a case we won't inherit. In case the node was actually defined as |
| 7020 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 7021 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 7022 | goto inherit_dfs_sibling; |
| 7023 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 7024 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 7025 | goto inherit_dfs_sibling; |
| 7026 | } |
| 7027 | |
| 7028 | if (eplugin->check_inherit) { |
| 7029 | /* we have a callback to check the inheritance, use it */ |
| 7030 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 7031 | case 0: |
| 7032 | /* yes - continue with the inheriting code */ |
| 7033 | break; |
| 7034 | case 1: |
| 7035 | /* no - continue with the node's sibling */ |
| 7036 | goto inherit_dfs_sibling; |
| 7037 | case 2: |
| 7038 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 7039 | goto inherit_dfs_child; |
| 7040 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7041 | 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] | 7042 | ext->def->module->name, ext->def->name, rc); |
| 7043 | } |
| 7044 | } |
| 7045 | |
| 7046 | /* inherit the extension */ |
| 7047 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7048 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7049 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7050 | 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] | 7051 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 7052 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 7053 | |
| 7054 | node->ext = extlist; |
| 7055 | node->ext_size++; |
| 7056 | |
| 7057 | inherit_dfs_child: |
| 7058 | /* modification of - select element for the next run - children first */ |
| 7059 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 7060 | next = NULL; |
| 7061 | } else { |
| 7062 | next = node->child; |
| 7063 | } |
| 7064 | if (!next) { |
| 7065 | inherit_dfs_sibling: |
| 7066 | /* no children, try siblings */ |
| 7067 | next = node->next; |
| 7068 | } |
| 7069 | while (!next) { |
| 7070 | /* go to the parent */ |
| 7071 | node = lys_parent(node); |
| 7072 | |
| 7073 | /* we are done if we are back in the root (the starter's parent */ |
| 7074 | if (node == root) { |
| 7075 | break; |
| 7076 | } |
| 7077 | |
| 7078 | /* parent is already processed, go to its sibling */ |
| 7079 | next = node->next; |
| 7080 | } |
| 7081 | } |
| 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | /* final check */ |
| 7086 | if (eplugin->check_result) { |
| 7087 | if ((*eplugin->check_result)(ext)) { |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7088 | LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed."); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7089 | return -1; |
| 7090 | } |
| 7091 | } |
| 7092 | |
| 7093 | rc = 0; |
| 7094 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7095 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7096 | LOGINT(ctx); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7097 | break; |
| 7098 | } |
| 7099 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7100 | if (has_str && !rc) { |
| 7101 | /* the string is no more needed in case of success. |
| 7102 | * 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] | 7103 | lydict_remove(ctx, str_snode); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7104 | } |
| 7105 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7106 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7107 | } |
| 7108 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7109 | /* logs directly */ |
| 7110 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7111 | 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] | 7112 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7113 | struct lyxml_elem *xml; |
| 7114 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7115 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7116 | const char *name = NULL; |
| 7117 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7118 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7119 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7120 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7121 | 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] | 7122 | break; |
| 7123 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7124 | 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] | 7125 | break; |
| 7126 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7127 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 7128 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7129 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 7130 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7131 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7132 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7133 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7134 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7135 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7136 | } else { |
| 7137 | LY_TREE_FOR(xml->attr, attr) { |
| 7138 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7139 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7140 | break; |
| 7141 | } |
| 7142 | } |
| 7143 | assert(attr); |
| 7144 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7145 | 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] | 7146 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7147 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7148 | iff_data = str_node; |
| 7149 | 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] | 7150 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7151 | case UNRES_FEATURE: |
| 7152 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 7153 | ((struct lys_feature *)item)->name); |
| 7154 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7155 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7156 | 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] | 7157 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7158 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7159 | case UNRES_TYPE_DFLT: |
Michal Vasko | ba835cd | 2018-02-23 09:25:48 +0100 | [diff] [blame] | 7160 | if (*(char **)str_node) { |
| 7161 | 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] | 7162 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 7163 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 7164 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7165 | break; |
| 7166 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7167 | 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] | 7168 | break; |
| 7169 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7170 | 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] | 7171 | break; |
| 7172 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7173 | 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] | 7174 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7175 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7176 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 7177 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7178 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7179 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 7180 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 7181 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7182 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7183 | case UNRES_EXT: |
| 7184 | extinfo = (struct unres_ext *)str_node; |
| 7185 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 7186 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 7187 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7188 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7189 | LOGINT(NULL); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7190 | break; |
| 7191 | } |
| 7192 | } |
| 7193 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7194 | static int |
| 7195 | resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref, |
| 7196 | int print_all_errors, uint32_t *resolved) |
| 7197 | { |
| 7198 | uint32_t i, unres_count, res_count; |
| 7199 | int ret = 0, rc; |
| 7200 | struct ly_err_item *prev_eitem; |
| 7201 | enum int_log_opts prev_ilo; |
| 7202 | LY_ERR prev_ly_errno; |
| 7203 | |
| 7204 | /* if there can be no forward references, every failure is final, so we can print it directly */ |
| 7205 | if (forward_ref) { |
| 7206 | prev_ly_errno = ly_errno; |
| 7207 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 7208 | } |
| 7209 | |
| 7210 | do { |
| 7211 | unres_count = 0; |
| 7212 | res_count = 0; |
| 7213 | |
| 7214 | for (i = 0; i < unres->count; ++i) { |
| 7215 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7216 | * if-features are resolved here to make sure that we will have all if-features for |
| 7217 | * later check of feature circular dependency */ |
| 7218 | if (unres->type[i] & types) { |
| 7219 | ++unres_count; |
| 7220 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7221 | if (unres->type[i] == UNRES_EXT_FINALIZE) { |
| 7222 | /* to avoid double free */ |
| 7223 | unres->type[i] = UNRES_RESOLVED; |
| 7224 | } |
| 7225 | if (!rc || (unres->type[i] == UNRES_XPATH)) { |
| 7226 | /* invalid XPath can never cause an error, only a warning */ |
| 7227 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 7228 | /* free the allocated structure */ |
| 7229 | free(unres->item[i]); |
| 7230 | } |
| 7231 | |
| 7232 | unres->type[i] = UNRES_RESOLVED; |
| 7233 | ++(*resolved); |
| 7234 | ++res_count; |
| 7235 | } else if ((rc == EXIT_FAILURE) && forward_ref) { |
| 7236 | /* forward reference, erase errors */ |
| 7237 | ly_err_free_next(ctx, prev_eitem); |
| 7238 | } else if (print_all_errors) { |
| 7239 | /* just so that we quit the loop */ |
| 7240 | ++res_count; |
| 7241 | ret = -1; |
| 7242 | } else { |
| 7243 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 7244 | return -1; |
| 7245 | } |
| 7246 | } |
| 7247 | } |
| 7248 | } while (res_count && (res_count < unres_count)); |
| 7249 | |
| 7250 | if (res_count < unres_count) { |
| 7251 | assert(forward_ref); |
| 7252 | /* just print the errors (but we must free the ones we have and get them again :-/ ) */ |
| 7253 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7254 | |
| 7255 | for (i = 0; i < unres->count; ++i) { |
| 7256 | if (unres->type[i] & types) { |
| 7257 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7258 | } |
| 7259 | } |
| 7260 | return -1; |
| 7261 | } |
| 7262 | |
| 7263 | if (forward_ref) { |
| 7264 | /* restore log */ |
| 7265 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7266 | ly_errno = prev_ly_errno; |
| 7267 | } |
| 7268 | |
| 7269 | return ret; |
| 7270 | } |
| 7271 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7272 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7273 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7274 | * |
| 7275 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7276 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7277 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7278 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7279 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7280 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7281 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7282 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7283 | uint32_t resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7284 | |
| 7285 | assert(unres); |
| 7286 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7287 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 7288 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7289 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7290 | * if-features are resolved here to make sure that we will have all if-features for |
| 7291 | * later check of feature circular dependency */ |
| 7292 | if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF |
| 7293 | | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT, |
| 7294 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7295 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7296 | } |
| 7297 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7298 | /* another batch of resolved items */ |
| 7299 | if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT |
| 7300 | | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) { |
| 7301 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7302 | } |
| 7303 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7304 | /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 7305 | if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) { |
| 7306 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7307 | } |
| 7308 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7309 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7310 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7311 | return EXIT_SUCCESS; |
| 7312 | } |
| 7313 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7314 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7315 | * @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] | 7316 | * |
| 7317 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7318 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7319 | * @param[in] item Item to resolve. Type determined by \p type. |
| 7320 | * @param[in] type Type of the unresolved item. |
| 7321 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7322 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7323 | * @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] | 7324 | */ |
| 7325 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7326 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 7327 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7328 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7329 | int rc; |
| 7330 | const char *dictstr; |
| 7331 | |
| 7332 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 7333 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 7334 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 7335 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7336 | lydict_remove(mod->ctx, dictstr); |
| 7337 | } |
| 7338 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7339 | } |
| 7340 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7341 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7342 | * @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] | 7343 | * |
| 7344 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7345 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7346 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7347 | * @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] | 7348 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7349 | * |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7350 | * @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] | 7351 | */ |
| 7352 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7353 | 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] | 7354 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7355 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7356 | int rc; |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7357 | uint32_t u; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7358 | enum int_log_opts prev_ilo; |
| 7359 | struct ly_err_item *prev_eitem; |
| 7360 | LY_ERR prev_ly_errno; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7361 | struct lyxml_elem *yin; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7362 | struct ly_ctx *ctx = mod->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7363 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7364 | assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) |
| 7365 | && (type != UNRES_WHEN) && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7366 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7367 | /* check for duplicities in unres */ |
| 7368 | for (u = 0; u < unres->count; u++) { |
| 7369 | if (unres->type[u] == type && unres->item[u] == item && |
| 7370 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7371 | /* duplication can happen when the node contains multiple statements of the same type to check, |
| 7372 | * this can happen for example when refinement is being applied, so we just postpone the processing |
| 7373 | * and do not duplicate the information */ |
| 7374 | return EXIT_FAILURE; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7375 | } |
| 7376 | } |
| 7377 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7378 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7379 | /* 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] | 7380 | * xpath is not tried because it would hide some potential warnings, |
| 7381 | * implementing module must be deferred because some other nodes can be added that will need to be traversed |
| 7382 | * and their targets made implemented */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7383 | rc = EXIT_FAILURE; |
| 7384 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7385 | prev_ly_errno = ly_errno; |
| 7386 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7387 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7388 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7389 | if (rc != EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7390 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0); |
| 7391 | if (rc != -1) { |
| 7392 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7393 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7394 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7395 | if (type == UNRES_LIST_UNIQ) { |
| 7396 | /* free the allocated structure */ |
| 7397 | free(item); |
| 7398 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7399 | /* free the allocated resources */ |
| 7400 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7401 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7402 | return rc; |
| 7403 | } else { |
| 7404 | /* erase info about validation errors */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7405 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7406 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7407 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7408 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7409 | print_unres_schema_item_fail(item, type, snode); |
| 7410 | |
| 7411 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7412 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7413 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7414 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7415 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7416 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7417 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7418 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7419 | } |
| 7420 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7421 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7422 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7423 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7424 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7425 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7426 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7427 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7428 | 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] | 7429 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7430 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7431 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7432 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7433 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7434 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7435 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7436 | } |
| 7437 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7438 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7439 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7440 | * |
| 7441 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7442 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7443 | * @param[in] item Old item to be resolved. |
| 7444 | * @param[in] type Type of the old unresolved item. |
| 7445 | * @param[in] new_item New item to use in the duplicate. |
| 7446 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7447 | * @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] | 7448 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7449 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7450 | 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] | 7451 | { |
| 7452 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7453 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7454 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7455 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7456 | 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] | 7457 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7458 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7459 | if (type == UNRES_LIST_UNIQ) { |
| 7460 | aux_uniq.list = item; |
| 7461 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7462 | item = &aux_uniq; |
| 7463 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7464 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7465 | |
| 7466 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7467 | if (type == UNRES_LIST_UNIQ) { |
| 7468 | free(new_item); |
| 7469 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7470 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7471 | } |
| 7472 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7473 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7474 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7475 | 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] | 7476 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7477 | return -1; |
| 7478 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7479 | } else if (type == UNRES_IFFEAT) { |
| 7480 | /* duplicate unres_iffeature_data */ |
| 7481 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7482 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7483 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7484 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7485 | 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] | 7486 | LOGINT(mod->ctx); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7487 | return -1; |
| 7488 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7489 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7490 | 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] | 7491 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7492 | return -1; |
| 7493 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7494 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7495 | |
| 7496 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7497 | } |
| 7498 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7499 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7500 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7501 | 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] | 7502 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7503 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7504 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7505 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7506 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7507 | i = start_on_backwards; |
| 7508 | } else { |
| 7509 | i = unres->count - 1; |
| 7510 | } |
| 7511 | for (; i > -1; i--) { |
| 7512 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7513 | continue; |
| 7514 | } |
| 7515 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7516 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7517 | break; |
| 7518 | } |
| 7519 | } else { |
| 7520 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7521 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7522 | 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] | 7523 | break; |
| 7524 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7525 | } |
| 7526 | } |
| 7527 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7528 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7529 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7530 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7531 | static void |
| 7532 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7533 | { |
| 7534 | struct lyxml_elem *yin; |
| 7535 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7536 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7537 | |
| 7538 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7539 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7540 | case UNRES_TYPE_DER: |
| 7541 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7542 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7543 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7544 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7545 | lydict_remove(ctx, yang->name); |
| 7546 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7547 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7548 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7549 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7550 | } else { |
| 7551 | lyxml_free(ctx, yin); |
| 7552 | } |
| 7553 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7554 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7555 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7556 | lydict_remove(ctx, iff_data->fname); |
| 7557 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7558 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7559 | case UNRES_IDENT: |
| 7560 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7561 | case UNRES_CHOICE_DFLT: |
| 7562 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7563 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7564 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7565 | case UNRES_LIST_UNIQ: |
| 7566 | free(unres->item[i]); |
| 7567 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7568 | case UNRES_EXT: |
| 7569 | free(unres->str_snode[i]); |
| 7570 | break; |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 7571 | case UNRES_EXT_FINALIZE: |
| 7572 | free(unres->str_snode[i]); |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7573 | default: |
| 7574 | break; |
| 7575 | } |
| 7576 | unres->type[i] = UNRES_RESOLVED; |
| 7577 | } |
| 7578 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7579 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7580 | 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] | 7581 | { |
| 7582 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7583 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7584 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7585 | if (!unres || !(*unres)) { |
| 7586 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7587 | } |
| 7588 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7589 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7590 | |
| 7591 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7592 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7593 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7594 | unresolved++; |
| 7595 | } |
| 7596 | continue; |
| 7597 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7598 | |
| 7599 | /* free heap memory for the specific item */ |
| 7600 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7601 | } |
| 7602 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7603 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7604 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7605 | free((*unres)->item); |
| 7606 | free((*unres)->type); |
| 7607 | free((*unres)->str_snode); |
| 7608 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7609 | free((*unres)); |
| 7610 | (*unres) = NULL; |
| 7611 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7612 | } |
| 7613 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7614 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7615 | * 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] | 7616 | static int |
| 7617 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7618 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7619 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7620 | enum int_log_opts prev_ilo; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7621 | char *buf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7622 | int ret = 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7623 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7624 | if (!json_instid || !json_instid[0]) { |
| 7625 | /* no/empty value */ |
| 7626 | return 0; |
| 7627 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7628 | |
| 7629 | for (op_node = lys_parent(sleaf); |
| 7630 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7631 | op_node = lys_parent(op_node)); |
| 7632 | |
| 7633 | if (op_node && lys_parent(op_node)) { |
| 7634 | /* nested operation - any absolute path is external */ |
| 7635 | return 1; |
| 7636 | } |
| 7637 | |
| 7638 | /* get the first node from the instid */ |
| 7639 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7640 | if (!buf) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7641 | /* so that we do not have to bother with logging, say it is not external */ |
| 7642 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7643 | } |
| 7644 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7645 | /* find the first schema node, do not log */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7646 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7647 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7648 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7649 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7650 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7651 | if (!first_node) { |
| 7652 | /* unknown path, say it is not external */ |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7653 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7654 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7655 | |
| 7656 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7657 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7658 | if (op_node && (op_node != first_node)) { |
| 7659 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7660 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7661 | } |
| 7662 | |
| 7663 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7664 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7665 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7666 | } |
| 7667 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7668 | /** |
| 7669 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7670 | * |
| 7671 | * @param[in] data Data node where the path is used |
| 7672 | * @param[in] path Instance-identifier node value. |
| 7673 | * @param[in,out] ret Resolved instance or NULL. |
| 7674 | * |
| 7675 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7676 | */ |
| 7677 | static int |
| 7678 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7679 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7680 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7681 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7682 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7683 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7684 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7685 | char *str; |
| 7686 | int mod_len, name_len, has_predicate; |
| 7687 | struct unres_data node_match; |
| 7688 | |
| 7689 | memset(&node_match, 0, sizeof node_match); |
| 7690 | *ret = NULL; |
| 7691 | |
| 7692 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7693 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7694 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7695 | if (root->prev) { |
| 7696 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7697 | } |
| 7698 | |
| 7699 | /* search for the instance node */ |
| 7700 | while (path[i]) { |
| 7701 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7702 | if (j <= 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7703 | 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] | 7704 | goto error; |
| 7705 | } |
| 7706 | i += j; |
| 7707 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7708 | if (model) { |
| 7709 | str = strndup(model, mod_len); |
| 7710 | if (!str) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7711 | LOGMEM(ctx); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7712 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7713 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7714 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7715 | if (ctx->data_clb) { |
| 7716 | if (!mod) { |
| 7717 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7718 | } else if (!mod->implemented) { |
| 7719 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7720 | } |
| 7721 | } |
| 7722 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7723 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7724 | if (!mod || !mod->implemented || mod->disabled) { |
| 7725 | break; |
| 7726 | } |
| 7727 | } else if (!prev_mod) { |
| 7728 | /* first iteration and we are missing module name */ |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 7729 | LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name); |
| 7730 | 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] | 7731 | goto error; |
| 7732 | } else { |
| 7733 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7734 | } |
| 7735 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7736 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7737 | /* no instance exists */ |
| 7738 | break; |
| 7739 | } |
| 7740 | |
| 7741 | if (has_predicate) { |
| 7742 | /* 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] | 7743 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7744 | /* index of the current node (for lists with position predicates) */ |
| 7745 | cur_idx = 1; |
| 7746 | while (j < (signed)node_match.count) { |
| 7747 | node = node_match.node[j]; |
| 7748 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7749 | if (parsed < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7750 | LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7751 | goto error; |
| 7752 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7753 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7754 | if (!node) { |
| 7755 | /* current node does not satisfy the predicate */ |
| 7756 | unres_data_del(&node_match, j); |
| 7757 | } else { |
| 7758 | ++j; |
| 7759 | } |
| 7760 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7761 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7762 | |
| 7763 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7764 | } else if (node_match.count) { |
| 7765 | /* check that we are not addressing lists */ |
| 7766 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7767 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7768 | unres_data_del(&node_match, j--); |
| 7769 | } |
| 7770 | } |
| 7771 | if (!node_match.count) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7772 | 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] | 7773 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7774 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7775 | |
| 7776 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | if (!node_match.count) { |
| 7780 | /* no instance exists */ |
| 7781 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7782 | LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7783 | return EXIT_FAILURE; |
| 7784 | } |
| 7785 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7786 | return EXIT_SUCCESS; |
| 7787 | } else if (node_match.count > 1) { |
| 7788 | /* instance identifier must resolve to a single node */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7789 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7790 | goto error; |
| 7791 | } else { |
| 7792 | /* we have required result, remember it and cleanup */ |
| 7793 | *ret = node_match.node[0]; |
| 7794 | free(node_match.node); |
| 7795 | return EXIT_SUCCESS; |
| 7796 | } |
| 7797 | |
| 7798 | error: |
| 7799 | /* cleanup */ |
| 7800 | free(node_match.node); |
| 7801 | return -1; |
| 7802 | } |
| 7803 | |
| 7804 | static int |
| 7805 | 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] | 7806 | { |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7807 | struct ly_set *set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7808 | uint32_t i; |
| 7809 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7810 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7811 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7812 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 7813 | set = lyd_find_path((struct lyd_node *)leaf, path); |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7814 | if (!set) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7815 | return -1; |
| 7816 | } |
| 7817 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7818 | for (i = 0; i < set->number; ++i) { |
| 7819 | if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7820 | continue; |
| 7821 | } |
| 7822 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7823 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7824 | * so we can simply compare just the values */ |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7825 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 7826 | /* we have the match */ |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7827 | *ret = set->set.d[i]; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7828 | break; |
| 7829 | } |
| 7830 | } |
| 7831 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7832 | ly_set_free(set); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7833 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7834 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7835 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7836 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7837 | 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] | 7838 | return EXIT_FAILURE; |
| 7839 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7840 | 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] | 7841 | } |
| 7842 | } |
| 7843 | |
| 7844 | return EXIT_SUCCESS; |
| 7845 | } |
| 7846 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7847 | /* 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] | 7848 | int |
| 7849 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7850 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7851 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7852 | struct ly_ctx *ctx = leaf->schema->module->ctx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7853 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7854 | struct lyd_node *ret; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7855 | enum int_log_opts prev_ilo; |
| 7856 | int found, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7857 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7858 | |
| 7859 | assert(type->base == LY_TYPE_UNION); |
| 7860 | |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7861 | 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] | 7862 | /* either NULL or instid previously converted to JSON */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7863 | json_val = lydict_insert(ctx, leaf->value.string, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7864 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7865 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7866 | if (store) { |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7867 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7868 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7869 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7870 | |
| 7871 | /* 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] | 7872 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7873 | |
| 7874 | t = NULL; |
| 7875 | found = 0; |
| 7876 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7877 | found = 0; |
| 7878 | |
| 7879 | switch (t->base) { |
| 7880 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7881 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7882 | req_inst = -1; |
| 7883 | } else { |
| 7884 | req_inst = t->info.lref.req; |
| 7885 | } |
| 7886 | |
| 7887 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7888 | if (store) { |
| 7889 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7890 | /* valid resolved */ |
| 7891 | leaf->value.leafref = ret; |
| 7892 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7893 | } else { |
| 7894 | /* valid unresolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7895 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7896 | 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] | 7897 | return -1; |
| 7898 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7899 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7900 | } |
| 7901 | } |
| 7902 | |
| 7903 | success = 1; |
| 7904 | } |
| 7905 | break; |
| 7906 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7907 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7908 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7909 | req_inst = -1; |
| 7910 | } else { |
| 7911 | req_inst = t->info.inst.req; |
| 7912 | } |
| 7913 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7914 | 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] | 7915 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7916 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7917 | /* valid resolved */ |
| 7918 | leaf->value.instance = ret; |
| 7919 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7920 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7921 | if (json_val) { |
| 7922 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7923 | leaf->value_str = json_val; |
| 7924 | json_val = NULL; |
| 7925 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7926 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7927 | /* valid unresolved */ |
| 7928 | if (json_val) { |
| 7929 | /* put the JSON val back */ |
| 7930 | leaf->value.string = json_val; |
| 7931 | json_val = NULL; |
| 7932 | } else { |
| 7933 | leaf->value.instance = NULL; |
| 7934 | } |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7935 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7936 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7937 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | success = 1; |
| 7941 | } |
| 7942 | break; |
| 7943 | default: |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7944 | 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] | 7945 | success = 1; |
| 7946 | } |
| 7947 | break; |
| 7948 | } |
| 7949 | |
| 7950 | if (success) { |
| 7951 | break; |
| 7952 | } |
| 7953 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7954 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7955 | if (store) { |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7956 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7957 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7958 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7959 | } |
| 7960 | |
| 7961 | /* turn logging back on */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7962 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7963 | |
| 7964 | if (json_val) { |
| 7965 | if (!success) { |
| 7966 | /* put the value back for now */ |
| 7967 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7968 | leaf->value.string = json_val; |
| 7969 | } else { |
| 7970 | /* value was ultimately useless, but we could not have known */ |
| 7971 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7972 | } |
| 7973 | } |
| 7974 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7975 | if (success) { |
| 7976 | if (resolved_type) { |
| 7977 | *resolved_type = t; |
| 7978 | } |
| 7979 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7980 | /* not found and it is required */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7981 | 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] | 7982 | return EXIT_FAILURE; |
| 7983 | } |
| 7984 | |
| 7985 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7986 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7987 | } |
| 7988 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7989 | /** |
| 7990 | * @brief Resolve a single unres data item. Logs directly. |
| 7991 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7992 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7993 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7994 | * @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] | 7995 | * |
| 7996 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 7997 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 7998 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 7999 | 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] | 8000 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8001 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8002 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8003 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8004 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8005 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8006 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8007 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8008 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8009 | switch (type) { |
| 8010 | case UNRES_LEAFREF: |
| 8011 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8012 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8013 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 8014 | req_inst = -1; |
| 8015 | } else { |
| 8016 | req_inst = sleaf->type.info.lref.req; |
| 8017 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8018 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 8019 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 8020 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8021 | /* valid resolved */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8022 | if (leaf->value_type == LY_TYPE_BITS) { |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8023 | free(leaf->value.bit); |
| 8024 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8025 | leaf->value.leafref = ret; |
| 8026 | leaf->value_type = LY_TYPE_LEAFREF; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8027 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8028 | } else { |
| 8029 | /* valid unresolved */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8030 | if (!(leaf->value_flags & LY_VALUE_UNRES)) { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 8031 | 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] | 8032 | return -1; |
| 8033 | } |
| 8034 | } |
| 8035 | } |
| 8036 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 8037 | } else { |
| 8038 | return rc; |
| 8039 | } |
| 8040 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8041 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8042 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8043 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8044 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 8045 | if (ext_dep == -1) { |
| 8046 | return -1; |
| 8047 | } |
| 8048 | |
| 8049 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 8050 | req_inst = -1; |
| 8051 | } else { |
| 8052 | req_inst = sleaf->type.info.inst.req; |
| 8053 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8054 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 8055 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8056 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8057 | /* valid resolved */ |
| 8058 | leaf->value.instance = ret; |
| 8059 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8060 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8061 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8062 | /* valid unresolved */ |
| 8063 | leaf->value.instance = NULL; |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8064 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8065 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8066 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8067 | } else { |
| 8068 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8069 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8070 | break; |
| 8071 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8072 | case UNRES_UNION: |
| 8073 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8074 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8075 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8076 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8077 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8078 | return rc; |
| 8079 | } |
| 8080 | break; |
| 8081 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8082 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8083 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 8084 | return rc; |
| 8085 | } |
| 8086 | break; |
| 8087 | |
| 8088 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8089 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8090 | return rc; |
| 8091 | } |
| 8092 | break; |
| 8093 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8094 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8095 | LOGINT(NULL); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8096 | return -1; |
| 8097 | } |
| 8098 | |
| 8099 | return EXIT_SUCCESS; |
| 8100 | } |
| 8101 | |
| 8102 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8103 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8104 | * |
| 8105 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8106 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8107 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8108 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8109 | */ |
| 8110 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8111 | 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] | 8112 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8113 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 8114 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
Radek Krejci | bacc744 | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 8115 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8116 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8117 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8118 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8119 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8120 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8121 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8122 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8123 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8124 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8125 | if (type == UNRES_WHEN) { |
| 8126 | /* remove previous result */ |
| 8127 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8128 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8129 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8130 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8131 | } |
| 8132 | |
| 8133 | /** |
| 8134 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 8135 | * |
Michal Vasko | 660582a | 2018-03-19 10:10:08 +0100 | [diff] [blame] | 8136 | * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected, |
| 8137 | * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit |
| 8138 | * non-presence containers may need to be deleted). |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8139 | * |
| 8140 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 8141 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8142 | * @param[in] ctx Context used. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8143 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8144 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 8145 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8146 | * |
| 8147 | * @return EXIT_SUCCESS on success, -1 on error. |
| 8148 | */ |
| 8149 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8150 | resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8151 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8152 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8153 | int rc, progress, ignore_fail; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8154 | enum int_log_opts prev_ilo; |
| 8155 | struct ly_err_item *prev_eitem; |
mohitarora24 | 89837dc | 2018-05-01 15:09:36 +0530 | [diff] [blame] | 8156 | LY_ERR prev_ly_errno = ly_errno; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8157 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8158 | struct lys_when *when; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8159 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8160 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8161 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8162 | |
| 8163 | if (!unres->count) { |
| 8164 | return EXIT_SUCCESS; |
| 8165 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8166 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8167 | 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] | 8168 | ignore_fail = 1; |
| 8169 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 8170 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8171 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8172 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8173 | } |
| 8174 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8175 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8176 | if (!ignore_fail) { |
| 8177 | /* remember logging state only if errors are generated and valid */ |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8178 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 8179 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8180 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8181 | /* |
| 8182 | * when-stmt first |
| 8183 | */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8184 | first = 1; |
| 8185 | stmt_count = 0; |
| 8186 | resolved = 0; |
| 8187 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8188 | do { |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8189 | if (!ignore_fail) { |
| 8190 | ly_err_free_next(ctx, prev_eitem); |
| 8191 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8192 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 8193 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8194 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8195 | continue; |
| 8196 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8197 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8198 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8199 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8200 | } |
| 8201 | |
| 8202 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 8203 | for (parent = unres->node[i]->parent; |
| 8204 | parent && LYD_WHEN_DONE(parent->when_status); |
| 8205 | parent = parent->parent) { |
| 8206 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 8207 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8208 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8209 | */ |
| 8210 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 8211 | unres->type[i] = UNRES_RESOLVED; |
| 8212 | resolved++; |
| 8213 | break; |
| 8214 | } |
| 8215 | } |
| 8216 | if (parent) { |
| 8217 | continue; |
| 8218 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8219 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8220 | 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] | 8221 | if (!rc) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8222 | /* finish with error/delete the node only if when was false, an external dependency was not required, |
| 8223 | * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */ |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8224 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 8225 | && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8226 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8227 | /* false when condition */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8228 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8229 | } /* follows else */ |
| 8230 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8231 | /* auto-delete */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8232 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond); |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8233 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8234 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 8235 | /* if it has parent non-presence containers that would be empty, we should actually |
| 8236 | * remove the container |
| 8237 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8238 | for (parent = unres->node[i]; |
| 8239 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 8240 | parent = parent->parent) { |
| 8241 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 8242 | /* presence container */ |
| 8243 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8244 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8245 | if (parent->next || parent->prev != parent) { |
| 8246 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 8247 | break; |
| 8248 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8249 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8250 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8251 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8252 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8253 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8254 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8255 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8256 | lyd_unlink(unres->node[i]); |
| 8257 | unres->type[i] = UNRES_DELETE; |
| 8258 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8259 | |
| 8260 | /* update the rest of unres items */ |
| 8261 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 8262 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8263 | continue; |
| 8264 | } |
| 8265 | |
| 8266 | /* test if the node is in subtree to be deleted */ |
| 8267 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 8268 | if (parent == unres->node[i]) { |
| 8269 | /* yes, it is */ |
| 8270 | unres->type[j] = UNRES_RESOLVED; |
| 8271 | resolved++; |
| 8272 | break; |
| 8273 | } |
| 8274 | } |
| 8275 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8276 | } else { |
| 8277 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8278 | } |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8279 | if (!ignore_fail) { |
| 8280 | ly_err_free_next(ctx, prev_eitem); |
| 8281 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8282 | resolved++; |
| 8283 | progress = 1; |
| 8284 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8285 | goto error; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 8286 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8287 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8288 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8289 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8290 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8291 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8292 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8293 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | for (i = 0; del_items && i < unres->count; i++) { |
| 8297 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 8298 | if (unres->type[i] != UNRES_DELETE) { |
| 8299 | continue; |
| 8300 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8301 | if (!unres->node[i]) { |
| 8302 | unres->type[i] = UNRES_RESOLVED; |
| 8303 | del_items--; |
| 8304 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8305 | } |
| 8306 | |
| 8307 | /* really remove the complete subtree */ |
| 8308 | lyd_free(unres->node[i]); |
| 8309 | unres->type[i] = UNRES_RESOLVED; |
| 8310 | del_items--; |
| 8311 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8312 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8313 | /* |
| 8314 | * now leafrefs |
| 8315 | */ |
| 8316 | if (options & LYD_OPT_TRUSTED) { |
| 8317 | /* we want to attempt to resolve leafrefs */ |
| 8318 | assert(!ignore_fail); |
| 8319 | ignore_fail = 1; |
| 8320 | |
| 8321 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8322 | ly_errno = prev_ly_errno; |
| 8323 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8324 | first = 1; |
| 8325 | stmt_count = 0; |
| 8326 | resolved = 0; |
| 8327 | do { |
| 8328 | progress = 0; |
| 8329 | for (i = 0; i < unres->count; i++) { |
| 8330 | if (unres->type[i] != UNRES_LEAFREF) { |
| 8331 | continue; |
| 8332 | } |
| 8333 | if (first) { |
| 8334 | /* count leafref nodes in unres list */ |
| 8335 | stmt_count++; |
| 8336 | } |
| 8337 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8338 | 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] | 8339 | if (!rc) { |
| 8340 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8341 | if (!ignore_fail) { |
| 8342 | ly_err_free_next(ctx, prev_eitem); |
| 8343 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8344 | resolved++; |
| 8345 | progress = 1; |
| 8346 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8347 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8348 | } /* else forward reference */ |
| 8349 | } |
| 8350 | first = 0; |
| 8351 | } while (progress && resolved < stmt_count); |
| 8352 | |
| 8353 | /* do we have some unresolved leafrefs? */ |
| 8354 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8355 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8356 | } |
| 8357 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8358 | if (!ignore_fail) { |
| 8359 | /* log normally now, throw away irrelevant errors */ |
| 8360 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8361 | ly_errno = prev_ly_errno; |
| 8362 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8363 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8364 | /* |
| 8365 | * rest |
| 8366 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8367 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8368 | if (unres->type[i] == UNRES_RESOLVED) { |
| 8369 | continue; |
| 8370 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8371 | 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] | 8372 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8373 | 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] | 8374 | if (rc) { |
| 8375 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8376 | return -1; |
| 8377 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8378 | |
| 8379 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8380 | } |
| 8381 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8382 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8383 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8384 | return EXIT_SUCCESS; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8385 | |
| 8386 | error: |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8387 | if (!ignore_fail) { |
| 8388 | /* print all the new errors */ |
| 8389 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 8390 | /* do not restore ly_errno, it was udpated properly */ |
| 8391 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8392 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8393 | } |