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 | |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1647 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 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 | } |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1656 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 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); |
Michal Vasko | 8801135 | 2018-07-12 08:49:07 +0200 | [diff] [blame] | 1664 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 3b2ad46 | 2018-07-10 15:40:53 +0200 | [diff] [blame] | 1990 | /* look into augments */ |
| 1991 | if (!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 | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2614 | const struct lys_node *ssibling, *sparent; |
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; |
Michal Vasko | 2096478 | 2018-08-01 15:14:30 +0200 | [diff] [blame] | 2664 | sparent = (start && start->parent) ? start->parent->schema : NULL; |
| 2665 | while ((ssibling = lys_getnext(ssibling, sparent, prev_mod, 0))) { |
| 2666 | /* skip invalid input/output nodes */ |
| 2667 | if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 2668 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2669 | if (lys_parent(ssibling)->nodetype == LYS_INPUT) { |
| 2670 | continue; |
| 2671 | } |
| 2672 | } else { |
| 2673 | if (lys_parent(ssibling)->nodetype == LYS_OUTPUT) { |
| 2674 | continue; |
| 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2679 | if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) { |
| 2680 | break; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2681 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2682 | } |
| 2683 | if (!ssibling) { |
| 2684 | /* there is not even such a schema node */ |
| 2685 | free(pp.pred); |
| 2686 | return last_match; |
| 2687 | } |
| 2688 | pp.schema = ssibling; |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 2689 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2690 | /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if |
| 2691 | * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */ |
| 2692 | if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) { |
| 2693 | pp.len = 1; |
| 2694 | pp.pred = calloc(1, sizeof *pp.pred); |
| 2695 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2696 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2697 | pp.pred[0].name = "."; |
| 2698 | pp.pred[0].nam_len = 1; |
| 2699 | pp.pred[0].value = (llist_value ? llist_value : ""); |
| 2700 | pp.pred[0].val_len = strlen(pp.pred[0].value); |
| 2701 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 2702 | |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2703 | if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) { |
| 2704 | /* check leaf/leaf-list predicate */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2705 | if (pp.len > 1) { |
| 2706 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2707 | goto error; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2708 | } else if (pp.len) { |
| 2709 | if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) { |
| 2710 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name); |
| 2711 | goto error; |
| 2712 | } |
| 2713 | if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) { |
| 2714 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value); |
| 2715 | goto error; |
| 2716 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2717 | } |
| 2718 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2719 | /* list should have predicates for all the keys or position */ |
| 2720 | slist = (struct lys_node_list *)ssibling; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2721 | if (!pp.len) { |
| 2722 | /* none match */ |
| 2723 | return last_match; |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 2724 | } else if (!isdigit(pp.pred[0].name[0])) { |
| 2725 | /* list predicate is not a position, so there must be all the keys */ |
| 2726 | if (pp.len > slist->keys_size) { |
| 2727 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2728 | goto error; |
| 2729 | } else if (pp.len < slist->keys_size) { |
| 2730 | LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name); |
| 2731 | goto error; |
| 2732 | } |
| 2733 | /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */ |
| 2734 | for (r = 0; r < pp.len; ++r) { |
| 2735 | if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) { |
| 2736 | LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value); |
| 2737 | goto error; |
| 2738 | } |
| 2739 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2740 | } |
| 2741 | } else if (pp.pred) { |
| 2742 | /* no other nodes allow predicates */ |
| 2743 | LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL); |
| 2744 | goto error; |
| 2745 | } |
| 2746 | |
| 2747 | #ifdef LY_ENABLED_CACHE |
| 2748 | /* we will not be matching keyless lists or state leaf-lists this way */ |
| 2749 | if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size) |
| 2750 | && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) { |
| 2751 | sibling = resolve_json_data_node_hash(start->parent, pp); |
| 2752 | } else |
| 2753 | #endif |
| 2754 | { |
| 2755 | list_instance_position = 0; |
| 2756 | LY_TREE_FOR(start, sibling) { |
| 2757 | /* RPC/action data check, return simply invalid argument, because the data tree is invalid */ |
| 2758 | if (lys_parent(sibling->schema)) { |
| 2759 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 2760 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
| 2761 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
| 2762 | goto error; |
| 2763 | } |
| 2764 | } else { |
| 2765 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
| 2766 | LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
| 2767 | goto error; |
| 2768 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2769 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2770 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2771 | |
| 2772 | if (sibling->schema != ssibling) { |
| 2773 | /* wrong schema node */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2774 | continue; |
| 2775 | } |
| 2776 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2777 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2778 | if (ssibling->nodetype == LYS_LEAFLIST) { |
| 2779 | if (ssibling->flags & LYS_CONFIG_R) { |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 2780 | /* state leaf-lists will never match */ |
| 2781 | continue; |
| 2782 | } |
| 2783 | |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2784 | llist = (struct lyd_node_leaf_list *)sibling; |
| 2785 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2786 | /* get the expected leaf-list value */ |
| 2787 | llval = NULL; |
| 2788 | llval_len = 0; |
| 2789 | if (pp.pred) { |
| 2790 | /* it was already checked that it is correct */ |
| 2791 | llval = pp.pred[0].value; |
| 2792 | llval_len = pp.pred[0].val_len; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2793 | |
Michal Vasko | f0a5097 | 2016-10-19 11:33:55 +0200 | [diff] [blame] | 2794 | } |
| 2795 | |
Michal Vasko | 21b90ce | 2017-09-19 09:38:27 +0200 | [diff] [blame] | 2796 | /* 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] | 2797 | if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT) |
Michal Vasko | 6a938d6 | 2016-12-21 09:21:30 +0100 | [diff] [blame] | 2798 | && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name)) |
| 2799 | && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) { |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2800 | data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1; |
| 2801 | } else { |
| 2802 | data_val = llist->value_str; |
| 2803 | } |
| 2804 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2805 | if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len) |
| 2806 | || data_val[llval_len]))) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 2807 | continue; |
| 2808 | } |
Michal Vasko | 9ba34de | 2016-12-07 12:21:19 +0100 | [diff] [blame] | 2809 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2810 | } else if (ssibling->nodetype == LYS_LIST) { |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 2811 | /* 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] | 2812 | ++list_instance_position; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2813 | ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2814 | if (ret == -1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2815 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2816 | } else if (ret == 1) { |
| 2817 | /* this list instance does not match */ |
| 2818 | continue; |
| 2819 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2822 | break; |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | /* no match, return last match */ |
| 2827 | if (!sibling) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2828 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2829 | return last_match; |
| 2830 | } |
| 2831 | |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2832 | /* we found a next matching node */ |
| 2833 | *parsed += last_parsed; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2834 | last_match = sibling; |
| 2835 | prev_mod = lyd_node_module(sibling); |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2836 | |
| 2837 | /* the result node? */ |
| 2838 | if (!id[0]) { |
| 2839 | free(pp.pred); |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2840 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2841 | } |
| 2842 | |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2843 | /* move down the tree, if possible, and continue */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2844 | if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2845 | /* there can be no children even through expected, error */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2846 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2847 | goto error; |
Michal Vasko | 586831d | 2018-05-22 11:13:16 +0200 | [diff] [blame] | 2848 | } else if (!sibling->child) { |
| 2849 | /* there could be some children, but are not, return what we found so far */ |
| 2850 | free(pp.pred); |
| 2851 | return last_match; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2852 | } |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2853 | start = sibling->child; |
| 2854 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2855 | /* parse nodeid */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 2856 | 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] | 2857 | 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] | 2858 | goto error; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2859 | } |
| 2860 | id += r; |
| 2861 | last_parsed = r; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2862 | |
| 2863 | parse_predicates: |
| 2864 | /* parse all the predicates */ |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2865 | free(pp.pred); |
| 2866 | pp.schema = NULL; |
| 2867 | pp.len = 0; |
| 2868 | pp.pred = NULL; |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2869 | while (has_predicate) { |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2870 | ++pp.len; |
| 2871 | pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred); |
| 2872 | LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error); |
| 2873 | if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len, |
| 2874 | &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value, |
| 2875 | &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) { |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2876 | LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
| 2877 | goto error; |
| 2878 | } |
| 2879 | |
| 2880 | id += r; |
| 2881 | last_parsed += r; |
| 2882 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2883 | } |
| 2884 | |
Michal Vasko | febd13d | 2018-05-17 10:42:24 +0200 | [diff] [blame] | 2885 | error: |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 2886 | *parsed = -1; |
Michal Vasko | 2d44ee0 | 2018-05-18 09:38:51 +0200 | [diff] [blame] | 2887 | free(pp.pred); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 2888 | return NULL; |
| 2889 | } |
| 2890 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2891 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2892 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2893 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2894 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2895 | * @param[in] ctx Context for errors. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2896 | * @param[in] str_restr Restriction as a string. |
| 2897 | * @param[in] type Type of the restriction. |
| 2898 | * @param[out] ret Final interval structure that starts with |
| 2899 | * the interval of the initial type, continues with intervals |
| 2900 | * of any superior types derived from the initial one, and |
| 2901 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2902 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2903 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2904 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2905 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2906 | 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] | 2907 | { |
| 2908 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2909 | int kind; |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 2910 | int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax; |
| 2911 | uint64_t local_umin, local_umax = 0; |
| 2912 | uint8_t local_fdig = 0; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2913 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2914 | 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] | 2915 | |
| 2916 | switch (type->base) { |
| 2917 | case LY_TYPE_BINARY: |
| 2918 | kind = 0; |
| 2919 | local_umin = 0; |
| 2920 | local_umax = 18446744073709551615UL; |
| 2921 | |
| 2922 | if (!str_restr && type->info.binary.length) { |
| 2923 | str_restr = type->info.binary.length->expr; |
| 2924 | } |
| 2925 | break; |
| 2926 | case LY_TYPE_DEC64: |
| 2927 | kind = 2; |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 2928 | local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2929 | local_fmax = __INT64_C(9223372036854775807); |
| 2930 | local_fdig = type->info.dec64.dig; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2931 | |
| 2932 | if (!str_restr && type->info.dec64.range) { |
| 2933 | str_restr = type->info.dec64.range->expr; |
| 2934 | } |
| 2935 | break; |
| 2936 | case LY_TYPE_INT8: |
| 2937 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2938 | local_smin = __INT64_C(-128); |
| 2939 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2940 | |
| 2941 | if (!str_restr && type->info.num.range) { |
| 2942 | str_restr = type->info.num.range->expr; |
| 2943 | } |
| 2944 | break; |
| 2945 | case LY_TYPE_INT16: |
| 2946 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2947 | local_smin = __INT64_C(-32768); |
| 2948 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2949 | |
| 2950 | if (!str_restr && type->info.num.range) { |
| 2951 | str_restr = type->info.num.range->expr; |
| 2952 | } |
| 2953 | break; |
| 2954 | case LY_TYPE_INT32: |
| 2955 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2956 | local_smin = __INT64_C(-2147483648); |
| 2957 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2958 | |
| 2959 | if (!str_restr && type->info.num.range) { |
| 2960 | str_restr = type->info.num.range->expr; |
| 2961 | } |
| 2962 | break; |
| 2963 | case LY_TYPE_INT64: |
| 2964 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2965 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 2966 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2967 | |
| 2968 | if (!str_restr && type->info.num.range) { |
| 2969 | str_restr = type->info.num.range->expr; |
| 2970 | } |
| 2971 | break; |
| 2972 | case LY_TYPE_UINT8: |
| 2973 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2974 | local_umin = __UINT64_C(0); |
| 2975 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2976 | |
| 2977 | if (!str_restr && type->info.num.range) { |
| 2978 | str_restr = type->info.num.range->expr; |
| 2979 | } |
| 2980 | break; |
| 2981 | case LY_TYPE_UINT16: |
| 2982 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2983 | local_umin = __UINT64_C(0); |
| 2984 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2985 | |
| 2986 | if (!str_restr && type->info.num.range) { |
| 2987 | str_restr = type->info.num.range->expr; |
| 2988 | } |
| 2989 | break; |
| 2990 | case LY_TYPE_UINT32: |
| 2991 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 2992 | local_umin = __UINT64_C(0); |
| 2993 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2994 | |
| 2995 | if (!str_restr && type->info.num.range) { |
| 2996 | str_restr = type->info.num.range->expr; |
| 2997 | } |
| 2998 | break; |
| 2999 | case LY_TYPE_UINT64: |
| 3000 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 3001 | local_umin = __UINT64_C(0); |
| 3002 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3003 | |
| 3004 | if (!str_restr && type->info.num.range) { |
| 3005 | str_restr = type->info.num.range->expr; |
| 3006 | } |
| 3007 | break; |
| 3008 | case LY_TYPE_STRING: |
| 3009 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 3010 | local_umin = __UINT64_C(0); |
| 3011 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3012 | |
| 3013 | if (!str_restr && type->info.str.length) { |
| 3014 | str_restr = type->info.str.length->expr; |
| 3015 | } |
| 3016 | break; |
| 3017 | default: |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3018 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3022 | if (type->der) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3023 | if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3024 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 3025 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3026 | assert(!intv || (intv->kind == kind)); |
| 3027 | } |
| 3028 | |
| 3029 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3030 | /* we do not have any restriction, return superior ones */ |
| 3031 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3032 | return EXIT_SUCCESS; |
| 3033 | } |
| 3034 | |
| 3035 | /* adjust local min and max */ |
| 3036 | if (intv) { |
| 3037 | tmp_intv = intv; |
| 3038 | |
| 3039 | if (kind == 0) { |
| 3040 | local_umin = tmp_intv->value.uval.min; |
| 3041 | } else if (kind == 1) { |
| 3042 | local_smin = tmp_intv->value.sval.min; |
| 3043 | } else if (kind == 2) { |
| 3044 | local_fmin = tmp_intv->value.fval.min; |
| 3045 | } |
| 3046 | |
| 3047 | while (tmp_intv->next) { |
| 3048 | tmp_intv = tmp_intv->next; |
| 3049 | } |
| 3050 | |
| 3051 | if (kind == 0) { |
| 3052 | local_umax = tmp_intv->value.uval.max; |
| 3053 | } else if (kind == 1) { |
| 3054 | local_smax = tmp_intv->value.sval.max; |
| 3055 | } else if (kind == 2) { |
| 3056 | local_fmax = tmp_intv->value.fval.max; |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | /* finally parse our restriction */ |
| 3061 | seg_ptr = str_restr; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3062 | tmp_intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3063 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3064 | if (!tmp_local_intv) { |
| 3065 | assert(!local_intv); |
| 3066 | local_intv = malloc(sizeof *local_intv); |
| 3067 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3068 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3069 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3070 | tmp_local_intv = tmp_local_intv->next; |
| 3071 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3072 | LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3073 | |
| 3074 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3075 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3076 | tmp_local_intv->next = NULL; |
| 3077 | |
| 3078 | /* min */ |
| 3079 | ptr = seg_ptr; |
| 3080 | while (isspace(ptr[0])) { |
| 3081 | ++ptr; |
| 3082 | } |
| 3083 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3084 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3085 | tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3086 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3087 | tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3088 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3089 | 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] | 3090 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3091 | goto error; |
| 3092 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3093 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3094 | } else if (!strncmp(ptr, "min", 3)) { |
| 3095 | if (kind == 0) { |
| 3096 | tmp_local_intv->value.uval.min = local_umin; |
| 3097 | } else if (kind == 1) { |
| 3098 | tmp_local_intv->value.sval.min = local_smin; |
| 3099 | } else if (kind == 2) { |
| 3100 | tmp_local_intv->value.fval.min = local_fmin; |
| 3101 | } |
| 3102 | |
| 3103 | ptr += 3; |
| 3104 | } else if (!strncmp(ptr, "max", 3)) { |
| 3105 | if (kind == 0) { |
| 3106 | tmp_local_intv->value.uval.min = local_umax; |
| 3107 | } else if (kind == 1) { |
| 3108 | tmp_local_intv->value.sval.min = local_smax; |
| 3109 | } else if (kind == 2) { |
| 3110 | tmp_local_intv->value.fval.min = local_fmax; |
| 3111 | } |
| 3112 | |
| 3113 | ptr += 3; |
| 3114 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3115 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | while (isspace(ptr[0])) { |
| 3119 | ptr++; |
| 3120 | } |
| 3121 | |
| 3122 | /* no interval or interval */ |
| 3123 | if ((ptr[0] == '|') || !ptr[0]) { |
| 3124 | if (kind == 0) { |
| 3125 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 3126 | } else if (kind == 1) { |
| 3127 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 3128 | } else if (kind == 2) { |
| 3129 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 3130 | } |
| 3131 | } else if (!strncmp(ptr, "..", 2)) { |
| 3132 | /* skip ".." */ |
| 3133 | ptr += 2; |
| 3134 | while (isspace(ptr[0])) { |
| 3135 | ++ptr; |
| 3136 | } |
| 3137 | |
| 3138 | /* max */ |
| 3139 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 3140 | if (kind == 0) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3141 | tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3142 | } else if (kind == 1) { |
Radek Krejci | 2589441 | 2017-07-11 10:53:16 +0200 | [diff] [blame] | 3143 | tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3144 | } else if (kind == 2) { |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3145 | 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] | 3146 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range"); |
Michal Vasko | d24dd01 | 2016-09-30 12:20:22 +0200 | [diff] [blame] | 3147 | goto error; |
| 3148 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3149 | } |
| 3150 | } else if (!strncmp(ptr, "max", 3)) { |
| 3151 | if (kind == 0) { |
| 3152 | tmp_local_intv->value.uval.max = local_umax; |
| 3153 | } else if (kind == 1) { |
| 3154 | tmp_local_intv->value.sval.max = local_smax; |
| 3155 | } else if (kind == 2) { |
| 3156 | tmp_local_intv->value.fval.max = local_fmax; |
| 3157 | } |
| 3158 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3159 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3160 | } |
| 3161 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3162 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3163 | } |
| 3164 | |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3165 | /* check min and max in correct order*/ |
| 3166 | if (kind == 0) { |
| 3167 | /* current segment */ |
| 3168 | if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) { |
| 3169 | goto error; |
| 3170 | } |
| 3171 | if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) { |
| 3172 | goto error; |
| 3173 | } |
| 3174 | /* segments sholud be ascending order */ |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3175 | 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] | 3176 | goto error; |
| 3177 | } |
| 3178 | } else if (kind == 1) { |
| 3179 | if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) { |
| 3180 | goto error; |
| 3181 | } |
| 3182 | if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) { |
| 3183 | goto error; |
| 3184 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3185 | 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] | 3186 | goto error; |
| 3187 | } |
| 3188 | } else if (kind == 2) { |
| 3189 | if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) { |
| 3190 | goto error; |
| 3191 | } |
| 3192 | if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) { |
| 3193 | goto error; |
| 3194 | } |
Pavol Vican | 69f62c9 | 2016-08-30 09:06:25 +0200 | [diff] [blame] | 3195 | 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] | 3196 | /* 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] | 3197 | goto error; |
| 3198 | } |
| 3199 | } |
| 3200 | |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3201 | /* next segment (next OR) */ |
| 3202 | seg_ptr = strchr(seg_ptr, '|'); |
| 3203 | if (!seg_ptr) { |
| 3204 | break; |
| 3205 | } |
| 3206 | seg_ptr++; |
Pavol Vican | 9e7c01d | 2016-08-29 09:36:17 +0200 | [diff] [blame] | 3207 | tmp_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | /* check local restrictions against superior ones */ |
| 3211 | if (intv) { |
| 3212 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3213 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3214 | |
| 3215 | while (tmp_local_intv && tmp_intv) { |
| 3216 | /* reuse local variables */ |
| 3217 | if (kind == 0) { |
| 3218 | local_umin = tmp_local_intv->value.uval.min; |
| 3219 | local_umax = tmp_local_intv->value.uval.max; |
| 3220 | |
| 3221 | /* it must be in this interval */ |
| 3222 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 3223 | /* this interval is covered, next one */ |
| 3224 | if (local_umax <= tmp_intv->value.uval.max) { |
| 3225 | tmp_local_intv = tmp_local_intv->next; |
| 3226 | continue; |
| 3227 | /* ascending order of restrictions -> fail */ |
| 3228 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3229 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | } else if (kind == 1) { |
| 3233 | local_smin = tmp_local_intv->value.sval.min; |
| 3234 | local_smax = tmp_local_intv->value.sval.max; |
| 3235 | |
| 3236 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 3237 | if (local_smax <= tmp_intv->value.sval.max) { |
| 3238 | tmp_local_intv = tmp_local_intv->next; |
| 3239 | continue; |
| 3240 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3241 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | } else if (kind == 2) { |
| 3245 | local_fmin = tmp_local_intv->value.fval.min; |
| 3246 | local_fmax = tmp_local_intv->value.fval.max; |
| 3247 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 3248 | 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] | 3249 | && (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] | 3250 | 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] | 3251 | tmp_local_intv = tmp_local_intv->next; |
| 3252 | continue; |
| 3253 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3254 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3255 | } |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | tmp_intv = tmp_intv->next; |
| 3260 | } |
| 3261 | |
| 3262 | /* some interval left uncovered -> fail */ |
| 3263 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3264 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3265 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3268 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 3269 | if (intv) { |
| 3270 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 3271 | tmp_intv->next = local_intv; |
| 3272 | } else { |
| 3273 | intv = local_intv; |
| 3274 | } |
| 3275 | *ret = intv; |
| 3276 | |
| 3277 | return EXIT_SUCCESS; |
| 3278 | |
| 3279 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3280 | while (intv) { |
| 3281 | tmp_intv = intv->next; |
| 3282 | free(intv); |
| 3283 | intv = tmp_intv; |
| 3284 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3285 | while (local_intv) { |
| 3286 | tmp_local_intv = local_intv->next; |
| 3287 | free(local_intv); |
| 3288 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 3291 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3294 | /** |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3295 | * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be |
| 3296 | * resolved for this function to return it. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3297 | * |
| 3298 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3299 | * @param[in] mod_name Typedef name module name. |
| 3300 | * @param[in] module Main module. |
| 3301 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3302 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3303 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3304 | * @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] | 3305 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3306 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3307 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 3308 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3309 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3310 | int i, j; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3311 | struct lys_tpdf *tpdf, *match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3312 | int tpdf_size; |
| 3313 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3314 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3315 | /* no prefix, try built-in types */ |
| 3316 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3317 | if (!strcmp(ly_types[i]->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3318 | if (ret) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 3319 | *ret = ly_types[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3320 | } |
| 3321 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3325 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3326 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3327 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3331 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3332 | /* search in local typedefs */ |
| 3333 | while (parent) { |
| 3334 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3335 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3336 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 3337 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3338 | break; |
| 3339 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3340 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3341 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 3342 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3343 | break; |
| 3344 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3345 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3346 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 3347 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3348 | break; |
| 3349 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3350 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3351 | case LYS_ACTION: |
| 3352 | tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3353 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3354 | break; |
| 3355 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3356 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 3357 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 3358 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3359 | break; |
| 3360 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3361 | case LYS_INPUT: |
| 3362 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3363 | tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size; |
| 3364 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3365 | break; |
| 3366 | |
| 3367 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3368 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3369 | continue; |
| 3370 | } |
| 3371 | |
| 3372 | for (i = 0; i < tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3373 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3374 | match = &tpdf[i]; |
| 3375 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3379 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3380 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3381 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3382 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3383 | module = lyp_get_module(module, NULL, 0, mod_name, 0, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 3384 | if (!module) { |
| 3385 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | /* search in top level typedefs */ |
| 3390 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3391 | 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] | 3392 | match = &module->tpdf[i]; |
| 3393 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3398 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3400 | 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] | 3401 | match = &module->inc[i].submodule->tpdf[j]; |
| 3402 | goto check_leafref; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | } |
| 3406 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3407 | return EXIT_FAILURE; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3408 | |
| 3409 | check_leafref: |
| 3410 | if (ret) { |
| 3411 | *ret = match; |
| 3412 | } |
| 3413 | if (match->type.base == LY_TYPE_LEAFREF) { |
| 3414 | while (!match->type.info.lref.path) { |
| 3415 | match = match->type.der; |
| 3416 | assert(match); |
| 3417 | } |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3418 | } |
| 3419 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3420 | } |
| 3421 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3422 | /** |
| 3423 | * @brief Check the default \p value of the \p type. Logs directly. |
| 3424 | * |
| 3425 | * @param[in] type Type definition to use. |
| 3426 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 3427 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3428 | * |
| 3429 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3430 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3431 | static int |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3432 | 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] | 3433 | { |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3434 | struct lys_tpdf *base_tpdf = NULL; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3435 | struct lyd_node_leaf_list node; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3436 | const char *dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3437 | char *s; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3438 | int ret = EXIT_SUCCESS, r; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3439 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3440 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3441 | assert(value); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3442 | memset(&node, 0, sizeof node); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3443 | |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 3444 | if (type->base <= LY_TYPE_DER) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3445 | /* the type was not resolved yet, nothing to do for now */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3446 | ret = EXIT_FAILURE; |
| 3447 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3448 | } else if (!tpdf && !module->implemented) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3449 | /* do not check defaults in not implemented module's data */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3450 | goto cleanup; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 3451 | } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3452 | /* identityrefs are checked when instantiated in data instead of typedef, |
| 3453 | * but in typedef the value has to be modified to include the prefix */ |
| 3454 | if (*value) { |
| 3455 | if (strchr(*value, ':')) { |
| 3456 | dflt = transform_schema2json(module, *value); |
| 3457 | } else { |
| 3458 | /* default prefix of the module where the typedef is defined */ |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3459 | if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) { |
| 3460 | LOGMEM(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3461 | ret = -1; |
| 3462 | goto cleanup; |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3463 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3464 | dflt = lydict_insert_zc(ctx, s); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3465 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3466 | lydict_remove(ctx, *value); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3467 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3468 | dflt = NULL; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3469 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3470 | goto cleanup; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3471 | } else if (type->base == LY_TYPE_LEAFREF && tpdf) { |
| 3472 | /* leafref in typedef cannot be checked */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3473 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3474 | } |
| 3475 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3476 | dflt = lydict_insert(ctx, *value, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3477 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3478 | /* we do not have a new default value, so is there any to check even, in some base type? */ |
| 3479 | for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) { |
| 3480 | if (base_tpdf->dflt) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3481 | dflt = lydict_insert(ctx, base_tpdf->dflt, 0); |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3486 | if (!dflt) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3487 | /* no default value, nothing to check, all is well */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3488 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | /* 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)? */ |
| 3492 | switch (type->base) { |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3493 | case LY_TYPE_IDENT: |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3494 | if (lys_main_module(base_tpdf->type.parent->module)->implemented) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3495 | goto cleanup; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 3496 | } else { |
| 3497 | /* check the default value from typedef, but use also the typedef's module |
| 3498 | * due to possible searching in imported modules which is expected in |
| 3499 | * typedef's module instead of module where the typedef is used */ |
| 3500 | module = base_tpdf->module; |
| 3501 | } |
| 3502 | break; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3503 | case LY_TYPE_INST: |
| 3504 | case LY_TYPE_LEAFREF: |
| 3505 | case LY_TYPE_BOOL: |
| 3506 | case LY_TYPE_EMPTY: |
| 3507 | /* 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] | 3508 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3509 | case LY_TYPE_BITS: |
| 3510 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3511 | if (type->info.bits.count) { |
| 3512 | break; |
| 3513 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3514 | goto cleanup; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3515 | case LY_TYPE_ENUM: |
| 3516 | /* the default value must match the restricted list of values, if the type was restricted */ |
| 3517 | if (type->info.enums.count) { |
| 3518 | break; |
| 3519 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3520 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3521 | case LY_TYPE_DEC64: |
| 3522 | if (type->info.dec64.range) { |
| 3523 | break; |
| 3524 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3525 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3526 | case LY_TYPE_BINARY: |
| 3527 | if (type->info.binary.length) { |
| 3528 | break; |
| 3529 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3530 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3531 | case LY_TYPE_INT8: |
| 3532 | case LY_TYPE_INT16: |
| 3533 | case LY_TYPE_INT32: |
| 3534 | case LY_TYPE_INT64: |
| 3535 | case LY_TYPE_UINT8: |
| 3536 | case LY_TYPE_UINT16: |
| 3537 | case LY_TYPE_UINT32: |
| 3538 | case LY_TYPE_UINT64: |
| 3539 | if (type->info.num.range) { |
| 3540 | break; |
| 3541 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3542 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3543 | case LY_TYPE_STRING: |
| 3544 | if (type->info.str.length || type->info.str.patterns) { |
| 3545 | break; |
| 3546 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3547 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3548 | case LY_TYPE_UNION: |
| 3549 | /* way too much trouble learning whether we need to check the default again, so just do it */ |
| 3550 | break; |
| 3551 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3552 | LOGINT(ctx); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3553 | ret = -1; |
| 3554 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3555 | } |
Radek Krejci | 55a161c | 2016-09-05 17:13:25 +0200 | [diff] [blame] | 3556 | } else if (type->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3557 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name); |
| 3558 | 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] | 3559 | ret = -1; |
| 3560 | goto cleanup; |
Michal Vasko | 478c465 | 2016-07-21 12:55:01 +0200 | [diff] [blame] | 3561 | } |
| 3562 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3563 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 3564 | memset(&node, 0, sizeof node); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3565 | node.value_str = lydict_insert(ctx, dflt, 0); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3566 | node.value_type = type->base; |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3567 | |
| 3568 | if (tpdf) { |
| 3569 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3570 | if (!node.schema) { |
| 3571 | LOGMEM(ctx); |
| 3572 | ret = -1; |
| 3573 | goto cleanup; |
| 3574 | } |
Michal Vasko | d1bf7c4 | 2018-02-15 08:38:49 +0100 | [diff] [blame] | 3575 | 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] | 3576 | if (r == -1) { |
| 3577 | LOGMEM(ctx); |
| 3578 | ret = -1; |
| 3579 | goto cleanup; |
| 3580 | } |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3581 | node.schema->module = module; |
| 3582 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
| 3583 | } else { |
| 3584 | node.schema = (struct lys_node *)type->parent; |
| 3585 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3586 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 3587 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3588 | if (!type->info.lref.target) { |
| 3589 | ret = EXIT_FAILURE; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3590 | goto cleanup; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3591 | } |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3592 | ret = check_default(&type->info.lref.target->type, &dflt, module, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3593 | if (!ret) { |
| 3594 | /* adopt possibly changed default value to its canonical form */ |
| 3595 | if (*value) { |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3596 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3597 | *value = dflt; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3598 | dflt = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3599 | } |
| 3600 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3601 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 3602 | 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] | 3603 | /* possible forward reference */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3604 | ret = EXIT_FAILURE; |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3605 | if (base_tpdf) { |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3606 | /* default value is defined in some base typedef */ |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3607 | if ((type->base == LY_TYPE_BITS && type->der->type.der) || |
| 3608 | (type->base == LY_TYPE_ENUM && type->der->type.der)) { |
| 3609 | /* we have refined bits/enums */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3610 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3611 | "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] | 3612 | dflt, type->parent->name, base_tpdf->name); |
Radek Krejci | bad2f17 | 2016-08-02 11:04:15 +0200 | [diff] [blame] | 3613 | } |
| 3614 | } |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3615 | } else { |
| 3616 | /* success - adopt canonical form from the node into the default value */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3617 | if (!ly_strequal(dflt, node.value_str, 1)) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3618 | /* this can happen only if we have non-inherited default value, |
| 3619 | * inherited default values are already in canonical form */ |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3620 | assert(ly_strequal(dflt, *value, 1)); |
| 3621 | |
| 3622 | lydict_remove(ctx, *value); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3623 | *value = node.value_str; |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3624 | node.value_str = NULL; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3625 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 3626 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3629 | cleanup: |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 3630 | lyd_free_value(node.value, node.value_type, node.value_flags, type); |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3631 | lydict_remove(ctx, node.value_str); |
| 3632 | if (tpdf && node.schema) { |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 3633 | free((char *)node.schema->name); |
| 3634 | free(node.schema); |
| 3635 | } |
Michal Vasko | 4b8eb8a | 2018-02-16 11:58:45 +0100 | [diff] [blame] | 3636 | lydict_remove(ctx, dflt); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3637 | |
| 3638 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3641 | /** |
| 3642 | * @brief Check a key for mandatory attributes. Logs directly. |
| 3643 | * |
| 3644 | * @param[in] key The key to check. |
| 3645 | * @param[in] flags What flags to check. |
| 3646 | * @param[in] list The list of all the keys. |
| 3647 | * @param[in] index Index of the key in the key list. |
| 3648 | * @param[in] name The name of the keys. |
| 3649 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3650 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3651 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3652 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3653 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3654 | 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] | 3655 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3656 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3657 | char *dup = NULL; |
| 3658 | int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3659 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3660 | |
| 3661 | /* existence */ |
| 3662 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3663 | if (name[len] != '\0') { |
| 3664 | dup = strdup(name); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3665 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3666 | dup[len] = '\0'; |
| 3667 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3668 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3669 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3670 | free(dup); |
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 | /* uniqueness */ |
| 3675 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3676 | if (key == list->keys[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3677 | LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3678 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3683 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3684 | LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3685 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | /* type of the leaf is not built-in empty */ |
Radek Krejci | 13fde92 | 2018-05-16 10:45:58 +0200 | [diff] [blame] | 3689 | 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] | 3690 | LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3691 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /* config attribute is the same as of the list */ |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3695 | 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] | 3696 | LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3697 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3700 | /* key is not placed from augment */ |
| 3701 | if (key->parent->nodetype == LYS_AUGMENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3702 | LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 3703 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 3704 | return -1; |
| 3705 | } |
| 3706 | |
Radek Krejci | 3f21ada | 2016-08-01 13:34:31 +0200 | [diff] [blame] | 3707 | /* key is not when/if-feature -conditional */ |
| 3708 | j = 0; |
| 3709 | if (key->when || (key->iffeature_size && (j = 1))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3710 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf"); |
| 3711 | 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] | 3712 | j ? "if-feature" : "when"); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3713 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3716 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3717 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3718 | |
| 3719 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3720 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3721 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3722 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3723 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3724 | * |
| 3725 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 3726 | */ |
| 3727 | int |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3728 | 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] | 3729 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3730 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3731 | const struct lys_node *leaf = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3732 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3733 | |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 3734 | 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] | 3735 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3736 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3737 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3738 | if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3739 | 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] | 3740 | } else if (rc == -2) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3741 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3742 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3743 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3744 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3745 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3746 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 3747 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3748 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3749 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3750 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3751 | if (leaf->nodetype != LYS_LEAF) { |
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, "Target is not a leaf."); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3754 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3755 | } |
| 3756 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3757 | /* check status */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3758 | if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name, |
| 3759 | leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3760 | return -1; |
| 3761 | } |
| 3762 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3763 | /* check that all unique's targets are of the same config type */ |
| 3764 | if (*trg_type) { |
| 3765 | 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] | 3766 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 3767 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3768 | "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.", |
| 3769 | uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false"); |
| 3770 | return -1; |
| 3771 | } |
| 3772 | } else { |
| 3773 | /* first unique */ |
| 3774 | if (leaf->flags & LYS_CONFIG_W) { |
| 3775 | *trg_type = 1; |
| 3776 | } else { |
| 3777 | *trg_type = 2; |
| 3778 | } |
| 3779 | } |
| 3780 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 3781 | /* set leaf's unique flag */ |
| 3782 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 3783 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3784 | return EXIT_SUCCESS; |
| 3785 | |
| 3786 | error: |
| 3787 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3788 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3789 | } |
| 3790 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 3791 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3792 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 3793 | { |
| 3794 | /* there are items after the one deleted */ |
| 3795 | if (i+1 < unres->count) { |
| 3796 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3797 | 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] | 3798 | |
| 3799 | /* deleting the last item */ |
| 3800 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3801 | free(unres->node); |
| 3802 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | /* if there are no items after and it is not the last one, just move the counter */ |
| 3806 | --unres->count; |
| 3807 | } |
| 3808 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3809 | /** |
| 3810 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 3811 | * |
| 3812 | * @param[in] mod Module to search in. |
| 3813 | * @param[in] name Name of the data node. |
| 3814 | * @param[in] nam_len Length of the name. |
| 3815 | * @param[in] start Data node to start the search from. |
| 3816 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 3817 | * they are replaced (!!) with the resolvents. |
| 3818 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 3819 | * @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] | 3820 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3821 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3822 | 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] | 3823 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3824 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3825 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3826 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3827 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3828 | if (!parents->count) { |
| 3829 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3830 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3831 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3832 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3833 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3834 | for (i = 0; i < parents->count;) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3835 | 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] | 3836 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3837 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3838 | continue; |
| 3839 | } |
| 3840 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3841 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | 3960835 | 2017-05-11 10:37:10 +0200 | [diff] [blame] | 3842 | 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] | 3843 | && node->schema->name[nam_len] == '\0') { |
| 3844 | /* matching target */ |
| 3845 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3846 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3847 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3848 | flag = 1; |
| 3849 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 3850 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3851 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3852 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3853 | LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3854 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3855 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3859 | |
| 3860 | if (!flag) { |
| 3861 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3862 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3863 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3864 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3865 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3866 | } |
| 3867 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3868 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3871 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3872 | 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] | 3873 | { |
| 3874 | int dep1, dep2; |
| 3875 | const struct lys_node *node; |
| 3876 | |
| 3877 | if (lys_parent(op_node)) { |
| 3878 | /* inner operation (notif/action) */ |
| 3879 | if (abs_path) { |
| 3880 | return 1; |
| 3881 | } else { |
| 3882 | /* compare depth of both nodes */ |
| 3883 | for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node)); |
| 3884 | for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node)); |
| 3885 | if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) { |
| 3886 | return 1; |
| 3887 | } |
| 3888 | } |
| 3889 | } else { |
| 3890 | /* top-level operation (notif/rpc) */ |
| 3891 | if (op_node != first_node) { |
| 3892 | return 1; |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | return 0; |
| 3897 | } |
| 3898 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3899 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3900 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3901 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3902 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3903 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 3904 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3905 | * @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] | 3906 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3907 | * @return 0 on forward reference, otherwise the number |
| 3908 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3909 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3910 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3911 | static int |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3912 | resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node, |
| 3913 | struct lys_node *parent, const struct lys_node *op_node) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3914 | { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3915 | const struct lys_module *trg_mod; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3916 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3917 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3918 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0; |
| 3919 | int has_predicate, dest_parent_times, i, rc, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3920 | struct ly_ctx *ctx = context_node->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3921 | |
| 3922 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3923 | 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] | 3924 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3925 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3926 | return -parsed+i; |
| 3927 | } |
| 3928 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3929 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3930 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3931 | /* source (must be leaf) */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3932 | if (sour_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3933 | 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] | 3934 | } else { |
| 3935 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3936 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3937 | 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] | 3938 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3939 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3940 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | /* destination */ |
Michal Vasko | f9b35d9 | 2016-10-21 15:19:30 +0200 | [diff] [blame] | 3944 | dest_parent_times = 0; |
| 3945 | pke_parsed = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3946 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3947 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3948 | 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] | 3949 | return -parsed; |
| 3950 | } |
| 3951 | pke_parsed += i; |
| 3952 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3953 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3954 | if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT) |
| 3955 | && !((struct lys_node_augment *)dst_node->parent)->target) { |
| 3956 | /* we are in an unresolved augment, cannot evaluate */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3957 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent, |
Michal Vasko | 3ba2d79 | 2017-07-10 15:14:43 +0200 | [diff] [blame] | 3958 | "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr); |
| 3959 | return 0; |
| 3960 | } |
| 3961 | |
Michal Vasko | fbaead7 | 2016-10-07 10:54:48 +0200 | [diff] [blame] | 3962 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 3963 | * all schema nodes that cannot be instantiated in data tree */ |
| 3964 | for (dst_node = lys_parent(dst_node); |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3965 | 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] | 3966 | dst_node = lys_parent(dst_node)); |
| 3967 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3968 | if (!dst_node) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3969 | 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] | 3970 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3971 | } |
| 3972 | } |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3973 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3974 | while (1) { |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 3975 | if (dest_pref) { |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 3976 | 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] | 3977 | } else { |
| 3978 | trg_mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3979 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3980 | 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] | 3981 | if (rc) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3982 | 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] | 3983 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3986 | if (first_iter) { |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 3987 | if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3988 | parent->flags |= LYS_LEAFREF_DEP; |
Michal Vasko | e27516a | 2016-10-10 17:55:31 +0000 | [diff] [blame] | 3989 | } |
| 3990 | first_iter = 0; |
| 3991 | } |
| 3992 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3993 | if (pke_len == pke_parsed) { |
| 3994 | break; |
| 3995 | } |
| 3996 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 3997 | 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] | 3998 | &dest_parent_times)) < 1) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 3999 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4000 | (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4001 | return -parsed; |
| 4002 | } |
| 4003 | pke_parsed += i; |
| 4004 | } |
| 4005 | |
| 4006 | /* check source - dest match */ |
Michal Vasko | 59ad458 | 2016-09-16 13:15:41 +0200 | [diff] [blame] | 4007 | if (dst_node->nodetype != src_node->nodetype) { |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 4008 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4009 | 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] | 4010 | strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4011 | return -parsed; |
| 4012 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4013 | } while (has_predicate); |
| 4014 | |
| 4015 | return parsed; |
| 4016 | } |
| 4017 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4018 | static int |
| 4019 | check_leafref_features(struct lys_type *type) |
| 4020 | { |
| 4021 | struct lys_node *iter; |
| 4022 | struct ly_set *src_parents, *trg_parents, *features; |
| 4023 | struct lys_node_augment *aug; |
| 4024 | struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx; |
| 4025 | unsigned int i, j, size, x; |
| 4026 | int ret = EXIT_SUCCESS; |
| 4027 | |
| 4028 | assert(type->parent); |
| 4029 | |
| 4030 | src_parents = ly_set_new(); |
| 4031 | trg_parents = ly_set_new(); |
| 4032 | features = ly_set_new(); |
| 4033 | |
| 4034 | /* get parents chain of source (leafref) */ |
| 4035 | for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) { |
| 4036 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4037 | continue; |
| 4038 | } |
| 4039 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4040 | aug = (struct lys_node_augment *)iter->parent; |
| 4041 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4042 | /* unresolved augment, wait until it's resolved */ |
| 4043 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4044 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4045 | ret = EXIT_FAILURE; |
| 4046 | goto cleanup; |
| 4047 | } |
Michal Vasko | d42871e | 2018-07-12 09:06:53 +0200 | [diff] [blame] | 4048 | /* also add this augment */ |
| 4049 | ly_set_add(src_parents, aug, LY_SET_OPT_USEASLIST); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4050 | } |
| 4051 | ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST); |
| 4052 | } |
| 4053 | /* get parents chain of target */ |
| 4054 | for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) { |
| 4055 | if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 4056 | continue; |
| 4057 | } |
| 4058 | if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) { |
| 4059 | aug = (struct lys_node_augment *)iter->parent; |
| 4060 | if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) { |
| 4061 | /* unresolved augment, wait until it's resolved */ |
| 4062 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug, |
| 4063 | "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path); |
| 4064 | ret = EXIT_FAILURE; |
| 4065 | goto cleanup; |
| 4066 | } |
| 4067 | } |
| 4068 | ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST); |
| 4069 | } |
| 4070 | |
| 4071 | /* compare the features used in if-feature statements in the rest of both |
| 4072 | * chains of parents. The set of features used for target must be a subset |
| 4073 | * of features used for the leafref. This is not a perfect, we should compare |
| 4074 | * the truth tables but it could require too much resources, so we simplify that */ |
| 4075 | for (i = 0; i < src_parents->number; i++) { |
| 4076 | iter = src_parents->set.s[i]; /* shortcut */ |
| 4077 | if (!iter->iffeature_size) { |
| 4078 | continue; |
| 4079 | } |
| 4080 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4081 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4082 | for (; size; size--) { |
| 4083 | if (!iter->iffeature[j].features[size - 1]) { |
| 4084 | /* not yet resolved feature, postpone this check */ |
| 4085 | ret = EXIT_FAILURE; |
| 4086 | goto cleanup; |
| 4087 | } |
| 4088 | ly_set_add(features, iter->iffeature[j].features[size - 1], 0); |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | x = features->number; |
| 4093 | for (i = 0; i < trg_parents->number; i++) { |
| 4094 | iter = trg_parents->set.s[i]; /* shortcut */ |
| 4095 | if (!iter->iffeature_size) { |
| 4096 | continue; |
| 4097 | } |
| 4098 | for (j = 0; j < iter->iffeature_size; j++) { |
| 4099 | resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size); |
| 4100 | for (; size; size--) { |
| 4101 | if (!iter->iffeature[j].features[size - 1]) { |
| 4102 | /* not yet resolved feature, postpone this check */ |
| 4103 | ret = EXIT_FAILURE; |
| 4104 | goto cleanup; |
| 4105 | } |
| 4106 | if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) { |
| 4107 | /* the feature is not present in features set of target's parents chain */ |
| 4108 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path); |
| 4109 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
| 4110 | "Leafref is not conditional based on \"%s\" feature as its target.", |
| 4111 | iter->iffeature[j].features[size - 1]->name); |
| 4112 | ret = -1; |
| 4113 | goto cleanup; |
| 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | cleanup: |
| 4120 | ly_set_free(features); |
| 4121 | ly_set_free(src_parents); |
| 4122 | ly_set_free(trg_parents); |
| 4123 | |
| 4124 | return ret; |
| 4125 | } |
| 4126 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4127 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4128 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4129 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4130 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4131 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4132 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4133 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4134 | * @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] | 4135 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4136 | static int |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4137 | 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] | 4138 | { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4139 | const struct lys_node *node, *op_node = NULL, *tmp_parent; |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4140 | struct lys_node_augment *last_aug; |
Michal Vasko | 3c60cbb | 2017-07-10 11:50:03 +0200 | [diff] [blame] | 4141 | const struct lys_module *tmp_mod, *cur_module; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4142 | const char *id, *prefix, *name; |
| 4143 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4144 | int i, first_iter; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4145 | struct ly_ctx *ctx = parent->module->ctx; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4146 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4147 | if (!type->info.lref.target) { |
| 4148 | first_iter = 1; |
| 4149 | parent_times = 0; |
| 4150 | id = type->info.lref.path; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4151 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4152 | /* find operation schema we are in */ |
| 4153 | for (op_node = lys_parent(parent); |
| 4154 | op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4155 | op_node = lys_parent(op_node)); |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 4156 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4157 | cur_module = lys_node_module(parent); |
| 4158 | do { |
| 4159 | if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
| 4160 | LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4161 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4162 | } |
| 4163 | id += i; |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4164 | |
| 4165 | /* get the current module */ |
| 4166 | tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module; |
| 4167 | if (!tmp_mod) { |
| 4168 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4169 | return EXIT_FAILURE; |
| 4170 | } |
| 4171 | last_aug = NULL; |
| 4172 | |
| 4173 | if (first_iter) { |
| 4174 | if (parent_times == -1) { |
| 4175 | /* use module data */ |
| 4176 | node = NULL; |
| 4177 | |
| 4178 | } else if (parent_times > 0) { |
| 4179 | /* we are looking for the right parent */ |
| 4180 | for (i = 0, node = parent; i < parent_times; i++) { |
| 4181 | if (node->parent && (node->parent->nodetype == LYS_AUGMENT) |
| 4182 | && !((struct lys_node_augment *)node->parent)->target) { |
| 4183 | /* we are in an unresolved augment, cannot evaluate */ |
| 4184 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent, |
| 4185 | "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path); |
| 4186 | return EXIT_FAILURE; |
| 4187 | } |
| 4188 | |
| 4189 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 4190 | * all schema nodes that cannot be instantiated in data tree */ |
| 4191 | for (node = lys_parent(node); |
| 4192 | node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC)); |
| 4193 | node = lys_parent(node)); |
| 4194 | |
| 4195 | if (!node) { |
| 4196 | if (i == parent_times - 1) { |
| 4197 | /* top-level */ |
| 4198 | break; |
| 4199 | } |
| 4200 | |
| 4201 | /* higher than top-level */ |
| 4202 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4203 | return EXIT_FAILURE; |
| 4204 | } |
| 4205 | } |
| 4206 | } else { |
| 4207 | LOGINT(ctx); |
| 4208 | return -1; |
| 4209 | } |
| 4210 | } |
| 4211 | |
| 4212 | /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node - |
| 4213 | * - useless to search for that in augments) */ |
| 4214 | if (!tmp_mod->implemented && node) { |
| 4215 | get_next_augment: |
| 4216 | last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node); |
| 4217 | } |
| 4218 | |
| 4219 | tmp_parent = (last_aug ? (struct lys_node *)last_aug : node); |
| 4220 | node = NULL; |
| 4221 | while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) { |
| 4222 | if (lys_node_module(node) != lys_main_module(tmp_mod)) { |
| 4223 | continue; |
| 4224 | } |
| 4225 | if (strncmp(node->name, name, nam_len) || node->name[nam_len]) { |
| 4226 | continue; |
| 4227 | } |
| 4228 | /* match */ |
| 4229 | break; |
| 4230 | } |
| 4231 | if (!node) { |
| 4232 | if (last_aug) { |
| 4233 | /* restore the correct augment target */ |
| 4234 | node = last_aug->target; |
| 4235 | goto get_next_augment; |
| 4236 | } |
| 4237 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4238 | return EXIT_FAILURE; |
| 4239 | } |
| 4240 | |
| 4241 | if (first_iter) { |
| 4242 | /* set external dependency flag, we can decide based on the first found node */ |
| 4243 | if (op_node && parent_times && |
| 4244 | resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) { |
| 4245 | parent->flags |= LYS_LEAFREF_DEP; |
| 4246 | } |
| 4247 | first_iter = 0; |
| 4248 | } |
| 4249 | |
| 4250 | if (has_predicate) { |
| 4251 | /* we have predicate, so the current result must be list */ |
| 4252 | if (node->nodetype != LYS_LIST) { |
| 4253 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4254 | return -1; |
| 4255 | } |
| 4256 | |
| 4257 | i = resolve_schema_leafref_predicate(id, node, parent, op_node); |
| 4258 | if (!i) { |
| 4259 | return EXIT_FAILURE; |
| 4260 | } else if (i < 0) { |
| 4261 | return -1; |
| 4262 | } |
| 4263 | id += i; |
| 4264 | has_predicate = 0; |
| 4265 | } |
| 4266 | } while (id[0]); |
| 4267 | |
| 4268 | /* the target must be leaf or leaf-list (in YANG 1.1 only) */ |
| 4269 | if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) { |
| 4270 | LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path); |
| 4271 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path); |
| 4272 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4273 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4274 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4275 | /* check status */ |
| 4276 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
| 4277 | node->flags, node->module, node->name, node)) { |
| 4278 | return -1; |
| 4279 | } |
| 4280 | |
| 4281 | /* assign */ |
| 4282 | type->info.lref.target = (struct lys_node_leaf *)node; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 4283 | } |
| 4284 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4285 | /* as the last thing traverse this leafref and make targets on the path implemented */ |
| 4286 | if (lys_node_module(parent)->implemented) { |
| 4287 | /* make all the modules in the path implemented */ |
| 4288 | for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) { |
| 4289 | if (!lys_node_module(node)->implemented) { |
| 4290 | lys_node_module(node)->implemented = 1; |
| 4291 | if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4292 | return -1; |
| 4293 | } |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | /* store the backlink from leafref target */ |
| 4298 | if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) { |
| 4299 | return -1; |
| 4300 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4301 | } |
| 4302 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4303 | /* check if leafref and its target are under common if-features */ |
| 4304 | return check_leafref_features(type); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4307 | /** |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4308 | * @brief Compare 2 data node values. |
| 4309 | * |
| 4310 | * Comparison performed on canonical forms, the first value |
| 4311 | * is first transformed into canonical form. |
| 4312 | * |
| 4313 | * @param[in] node Leaf/leaf-list with these values. |
| 4314 | * @param[in] noncan_val Non-canonical value. |
| 4315 | * @param[in] noncan_val_len Length of \p noncal_val. |
| 4316 | * @param[in] can_val Canonical value. |
| 4317 | * @return 1 if equal, 0 if not, -1 on error (logged). |
| 4318 | */ |
| 4319 | static int |
| 4320 | valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val) |
| 4321 | { |
| 4322 | int ret; |
| 4323 | struct lyd_node_leaf_list leaf; |
| 4324 | struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node; |
| 4325 | |
| 4326 | /* dummy leaf */ |
| 4327 | memset(&leaf, 0, sizeof leaf); |
| 4328 | leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len); |
| 4329 | |
| 4330 | repeat: |
| 4331 | leaf.value_type = sleaf->type.base; |
| 4332 | leaf.schema = node; |
| 4333 | |
| 4334 | if (leaf.value_type == LY_TYPE_LEAFREF) { |
| 4335 | if (!sleaf->type.info.lref.target) { |
| 4336 | /* 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] | 4337 | LOGINT(node->module->ctx); |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4338 | ret = -1; |
| 4339 | goto finish; |
| 4340 | } |
| 4341 | sleaf = sleaf->type.info.lref.target; |
| 4342 | goto repeat; |
| 4343 | } else { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 4344 | 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] | 4345 | ret = -1; |
| 4346 | goto finish; |
| 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | if (!strcmp(leaf.value_str, can_val)) { |
| 4351 | ret = 1; |
| 4352 | } else { |
| 4353 | ret = 0; |
| 4354 | } |
| 4355 | |
| 4356 | finish: |
| 4357 | lydict_remove(node->module->ctx, leaf.value_str); |
| 4358 | return ret; |
| 4359 | } |
| 4360 | |
| 4361 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 4362 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 4363 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4364 | * |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 4365 | * @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] | 4366 | * @param[in] pred Predicate to use. |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4367 | * @param[in,out] node Node matching the restriction without |
| 4368 | * the predicate. If it does not satisfy the predicate, |
| 4369 | * it is set to NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4370 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4371 | * @return Number of characters successfully parsed, |
| 4372 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4373 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4374 | static int |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4377 | /* ... /node[key=value] ... */ |
| 4378 | struct lyd_node_leaf_list *key; |
| 4379 | struct lys_node_leaf **list_keys = NULL; |
Michal Vasko | ab8adcd | 2017-10-02 13:32:24 +0200 | [diff] [blame] | 4380 | struct lys_node_list *slist = NULL; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4381 | const char *model, *name, *value; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4382 | int mod_len, nam_len, val_len, i, has_predicate, parsed; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4383 | struct ly_ctx *ctx = prev_mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4384 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4385 | assert(pred && node && *node); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 4386 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4387 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4388 | do { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4389 | if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 4390 | return -parsed + i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4391 | } |
| 4392 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4393 | |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4394 | if (!(*node)) { |
| 4395 | /* just parse it all */ |
| 4396 | continue; |
| 4397 | } |
| 4398 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4399 | /* target */ |
| 4400 | if (name[0] == '.') { |
| 4401 | /* leaf-list value */ |
| 4402 | if ((*node)->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4403 | 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] | 4404 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4405 | parsed = -1; |
| 4406 | goto cleanup; |
| 4407 | } |
| 4408 | |
| 4409 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4410 | 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] | 4411 | *node = NULL; |
| 4412 | goto cleanup; |
| 4413 | } |
| 4414 | |
| 4415 | } else if (isdigit(name[0])) { |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4416 | assert(!value); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4417 | |
| 4418 | /* keyless list position */ |
| 4419 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4420 | 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] | 4421 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4422 | parsed = -1; |
| 4423 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4424 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4425 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4426 | if (((struct lys_node_list *)(*node)->schema)->keys) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4427 | 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] | 4428 | (*node)->schema->name); |
| 4429 | parsed = -1; |
| 4430 | goto cleanup; |
| 4431 | } |
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 | /* check the index */ |
| 4434 | if (atoi(name) != cur_idx) { |
| 4435 | *node = NULL; |
| 4436 | goto cleanup; |
| 4437 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4438 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4439 | } else { |
| 4440 | /* list key value */ |
| 4441 | if ((*node)->schema->nodetype != LYS_LIST) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4442 | 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] | 4443 | strnodetype((*node)->schema->nodetype), (*node)->schema->name); |
| 4444 | parsed = -1; |
| 4445 | goto cleanup; |
| 4446 | } |
| 4447 | slist = (struct lys_node_list *)(*node)->schema; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4448 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4449 | /* prepare key array */ |
| 4450 | if (!list_keys) { |
| 4451 | list_keys = malloc(slist->keys_size * sizeof *list_keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4452 | LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4453 | for (i = 0; i < slist->keys_size; ++i) { |
| 4454 | list_keys[i] = slist->keys[i]; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4455 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4456 | } |
| 4457 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4458 | /* find the schema key leaf */ |
| 4459 | for (i = 0; i < slist->keys_size; ++i) { |
| 4460 | if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) { |
| 4461 | break; |
| 4462 | } |
| 4463 | } |
| 4464 | if (i == slist->keys_size) { |
| 4465 | /* this list has no such key */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4466 | 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] | 4467 | " but list \"%s\" does not define it.", nam_len, name, slist->name); |
| 4468 | parsed = -1; |
| 4469 | goto cleanup; |
| 4470 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4471 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4472 | /* check module */ |
| 4473 | if (model) { |
| 4474 | 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] | 4475 | 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] | 4476 | list_keys[i]->name, model, mod_len, list_keys[i]->module->name); |
| 4477 | parsed = -1; |
| 4478 | goto cleanup; |
| 4479 | } |
| 4480 | } else { |
| 4481 | if (list_keys[i]->module != prev_mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4482 | 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] | 4483 | list_keys[i]->name, prev_mod->name, list_keys[i]->module->name); |
| 4484 | parsed = -1; |
| 4485 | goto cleanup; |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | /* find the actual data key */ |
| 4490 | for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) { |
| 4491 | if (key->schema == (struct lys_node *)list_keys[i]) { |
| 4492 | break; |
| 4493 | } |
| 4494 | } |
| 4495 | if (!key) { |
| 4496 | /* list instance is missing a key? definitely should not happen */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4497 | LOGINT(ctx); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4498 | parsed = -1; |
| 4499 | goto cleanup; |
| 4500 | } |
| 4501 | |
| 4502 | /* check the value */ |
Michal Vasko | 718ecdd | 2017-10-03 14:12:39 +0200 | [diff] [blame] | 4503 | if (!valequal(key->schema, value, val_len, key->value_str)) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4504 | *node = NULL; |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4505 | /* we still want to parse the whole predicate */ |
| 4506 | continue; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /* everything is fine, mark this key as resolved */ |
| 4510 | list_keys[i] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4511 | } |
| 4512 | } while (has_predicate); |
| 4513 | |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4514 | /* check that all list keys were specified */ |
Michal Vasko | 88850b7 | 2017-10-02 13:13:21 +0200 | [diff] [blame] | 4515 | if (*node && list_keys) { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4516 | for (i = 0; i < slist->keys_size; ++i) { |
| 4517 | if (list_keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4518 | 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] | 4519 | parsed = -1; |
| 4520 | goto cleanup; |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4521 | } |
| 4522 | } |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 4525 | cleanup: |
| 4526 | free(list_keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4527 | return parsed; |
| 4528 | } |
| 4529 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4530 | static int |
| 4531 | check_xpath(struct lys_node *node, int check_place) |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4532 | { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 4533 | struct lys_node *parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4534 | struct lyxp_set set; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4535 | enum int_log_opts prev_ilo; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4536 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4537 | if (check_place) { |
| 4538 | parent = node; |
| 4539 | while (parent) { |
| 4540 | if (parent->nodetype == LYS_GROUPING) { |
| 4541 | /* unresolved grouping, skip for now (will be checked later) */ |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4542 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4543 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4544 | if (parent->nodetype == LYS_AUGMENT) { |
| 4545 | if (!((struct lys_node_augment *)parent)->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4546 | /* unresolved augment, skip for now (will be checked later) */ |
| 4547 | return EXIT_FAILURE; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4548 | } else { |
| 4549 | parent = ((struct lys_node_augment *)parent)->target; |
| 4550 | continue; |
| 4551 | } |
| 4552 | } |
| 4553 | parent = parent->parent; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4554 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4555 | } |
| 4556 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4557 | memset(&set, 0, sizeof set); |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4558 | |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 4559 | /* produce just warnings */ |
| 4560 | ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL); |
| 4561 | lyxp_node_atomize(node, &set, 1); |
| 4562 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
| 4563 | |
| 4564 | if (set.val.snodes) { |
| 4565 | free(set.val.snodes); |
| 4566 | } |
| 4567 | return EXIT_SUCCESS; |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4570 | static int |
| 4571 | check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type) |
| 4572 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 4573 | unsigned int i; |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4574 | |
| 4575 | if (type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4576 | if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 && |
| 4577 | (type->info.lref.target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4578 | 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] | 4579 | strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype)); |
| 4580 | return -1; |
| 4581 | } |
| 4582 | /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time |
| 4583 | * of leafref resolving (lys_leaf_add_leafref_target()) */ |
| 4584 | } else if (type->base == LY_TYPE_UNION) { |
| 4585 | for (i = 0; i < type->info.uni.count; i++) { |
| 4586 | if (check_leafref_config(leaf, &type->info.uni.types[i])) { |
| 4587 | return -1; |
| 4588 | } |
| 4589 | } |
| 4590 | } |
| 4591 | return 0; |
| 4592 | } |
| 4593 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 4594 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4595 | * @brief Passes config flag down to children, skips nodes without config flags. |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4596 | * Logs. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4597 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4598 | * @param[in] node Siblings and their children to have flags changed. |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4599 | * @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] | 4600 | * @param[in] flags Flags to assign to all the nodes. |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 4601 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4602 | * |
| 4603 | * @return 0 on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4604 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4605 | int |
| 4606 | inherit_config_flag(struct lys_node *node, int flags, int clear) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4607 | { |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4608 | struct lys_node_leaf *leaf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4609 | struct ly_ctx *ctx; |
| 4610 | |
| 4611 | if (!node) { |
| 4612 | return 0; |
| 4613 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4614 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4615 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4616 | ctx = node->module->ctx; |
| 4617 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 4618 | LY_TREE_FOR(node, node) { |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4619 | if (clear) { |
| 4620 | node->flags &= ~LYS_CONFIG_MASK; |
Michal Vasko | c2a8d36 | 2016-09-29 08:50:13 +0200 | [diff] [blame] | 4621 | node->flags &= ~LYS_CONFIG_SET; |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4622 | } else { |
| 4623 | if (node->flags & LYS_CONFIG_SET) { |
| 4624 | /* skip nodes with an explicit config value */ |
| 4625 | if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4626 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 4627 | 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] | 4628 | return -1; |
| 4629 | } |
| 4630 | continue; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4631 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4632 | |
| 4633 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 4634 | node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags; |
| 4635 | /* check that configuration lists have keys */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 4636 | if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W) |
| 4637 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4638 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list"); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 4639 | return -1; |
| 4640 | } |
| 4641 | } |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 4642 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4643 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4644 | if (inherit_config_flag(node->child, flags, clear)) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4645 | return -1; |
| 4646 | } |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 4647 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4648 | leaf = (struct lys_node_leaf *)node; |
| 4649 | if (check_leafref_config(leaf, &leaf->type)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 4650 | return -1; |
| 4651 | } |
| 4652 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4653 | } |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 4654 | |
| 4655 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4656 | } |
| 4657 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4658 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4659 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4660 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4661 | * @param[in] aug Augment to use. |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4662 | * @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] | 4663 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4664 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4665 | * @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] | 4666 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4667 | static int |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4668 | 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] | 4669 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4670 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4671 | struct lys_node *sub; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4672 | struct lys_module *mod; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4673 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4674 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4675 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4676 | assert(aug); |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4677 | mod = lys_main_module(aug->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4678 | ctx = mod->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4679 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4680 | /* set it as not applied for now */ |
| 4681 | aug->flags |= LYS_NOTAPPLIED; |
| 4682 | |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4683 | /* 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] | 4684 | if (!aug->target) { |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4685 | /* resolve target node */ |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 4686 | 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] | 4687 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4688 | LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4689 | return -1; |
| 4690 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4691 | if (!set) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4692 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 2ef7db6 | 2017-06-12 09:24:02 +0200 | [diff] [blame] | 4693 | return EXIT_FAILURE; |
| 4694 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4695 | aug->target = set->set.s[0]; |
| 4696 | ly_set_free(set); |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4697 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4698 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4699 | /* make this module implemented if the target module is (if the target is in an unimplemented module, |
| 4700 | * it is fine because when we will be making that module implemented, its augment will be applied |
| 4701 | * and that augment target module made implemented, recursively) */ |
| 4702 | if (mod->implemented && !lys_node_module(aug->target)->implemented) { |
| 4703 | lys_node_module(aug->target)->implemented = 1; |
| 4704 | if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4705 | return -1; |
| 4706 | } |
| 4707 | } |
| 4708 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4709 | /* check for mandatory nodes - if the target node is in another module |
| 4710 | * the added nodes cannot be mandatory |
| 4711 | */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4712 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 4713 | && (rc = lyp_check_mandatory_augment(aug, aug->target))) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 4714 | return rc; |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 4715 | } |
| 4716 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4717 | /* check augment target type and then augment nodes type */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4718 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4719 | LY_TREE_FOR(aug->child, sub) { |
| 4720 | if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES |
| 4721 | | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4722 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4723 | 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] | 4724 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 4725 | return -1; |
| 4726 | } |
| 4727 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4728 | } 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] | 4729 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4730 | 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] | 4731 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4732 | 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] | 4733 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4734 | return -1; |
| 4735 | } |
| 4736 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4737 | } else if (aug->target->nodetype == LYS_CHOICE) { |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4738 | LY_TREE_FOR(aug->child, sub) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4739 | 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] | 4740 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 4741 | 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] | 4742 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 4743 | return -1; |
| 4744 | } |
| 4745 | } |
| 4746 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4747 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 4748 | 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] | 4749 | return -1; |
| 4750 | } |
| 4751 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4752 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 4753 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4754 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 4755 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 4756 | } |
| 4757 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4758 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4759 | if (!aug->child) { |
| 4760 | /* empty augment, nothing to connect, but it is techincally applied */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4761 | LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4762 | aug->flags &= ~LYS_NOTAPPLIED; |
Radek Krejci | aa6b2a1 | 2017-10-26 15:52:39 +0200 | [diff] [blame] | 4763 | } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) { |
| 4764 | /* we try to connect the augment only in case the module is implemented or |
| 4765 | * the augment applies on the used grouping, anyway we failed here */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4766 | return -1; |
Michal Vasko | 15b3669 | 2016-08-26 15:29:54 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4769 | return EXIT_SUCCESS; |
| 4770 | } |
| 4771 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4772 | static int |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4773 | 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] | 4774 | { |
| 4775 | enum LY_VLOG_ELEM vlog_type; |
| 4776 | void *vlog_node; |
| 4777 | unsigned int i, j; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4778 | struct lys_ext *e; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4779 | char *ext_name, *ext_prefix, *tmp; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4780 | struct lyxml_elem *next_yin, *yin; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 4781 | const struct lys_module *mod; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4782 | struct lys_ext_instance *tmp_ext; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4783 | struct ly_ctx *ctx = NULL; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4784 | LYEXT_TYPE etype; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4785 | |
| 4786 | switch (info->parent_type) { |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4787 | case LYEXT_PAR_NODE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4788 | vlog_node = info->parent; |
| 4789 | vlog_type = LY_VLOG_LYS; |
| 4790 | break; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 4791 | case LYEXT_PAR_MODULE: |
| 4792 | case LYEXT_PAR_IMPORT: |
| 4793 | case LYEXT_PAR_INCLUDE: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4794 | vlog_node = NULL; |
| 4795 | vlog_type = LY_VLOG_LYS; |
| 4796 | break; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 4797 | default: |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4798 | vlog_node = NULL; |
Radek Krejci | 6a7fedf | 2017-02-10 12:38:06 +0100 | [diff] [blame] | 4799 | vlog_type = LY_VLOG_NONE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4800 | break; |
| 4801 | } |
| 4802 | |
| 4803 | if (info->datatype == LYS_IN_YIN) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4804 | /* YIN */ |
| 4805 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4806 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4807 | 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] | 4808 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4809 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 4810 | return EXIT_FAILURE; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4811 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4812 | ctx = mod->ctx; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4813 | |
| 4814 | /* find the extension definition */ |
| 4815 | e = NULL; |
| 4816 | for (i = 0; i < mod->extensions_size; i++) { |
| 4817 | if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) { |
| 4818 | e = &mod->extensions[i]; |
| 4819 | break; |
| 4820 | } |
| 4821 | } |
| 4822 | /* try submodules */ |
| 4823 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4824 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4825 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) { |
| 4826 | e = &mod->inc[j].submodule->extensions[i]; |
| 4827 | break; |
| 4828 | } |
| 4829 | } |
| 4830 | } |
| 4831 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4832 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4833 | return EXIT_FAILURE; |
| 4834 | } |
| 4835 | |
| 4836 | /* 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] | 4837 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4838 | if (e->plugin && e->plugin->check_position) { |
| 4839 | /* common part - we have plugin with position checking function, use it first */ |
| 4840 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 4841 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4842 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4843 | return -1; |
| 4844 | } |
| 4845 | } |
| 4846 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4847 | /* extension type-specific part - allocation */ |
| 4848 | if (e->plugin) { |
| 4849 | etype = e->plugin->type; |
| 4850 | } else { |
| 4851 | /* default type */ |
| 4852 | etype = LYEXT_FLAG; |
| 4853 | } |
| 4854 | switch (etype) { |
| 4855 | case LYEXT_FLAG: |
| 4856 | (*ext) = calloc(1, sizeof(struct lys_ext_instance)); |
| 4857 | break; |
| 4858 | case LYEXT_COMPLEX: |
| 4859 | (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
| 4860 | break; |
| 4861 | case LYEXT_ERR: |
| 4862 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4863 | LOGINT(ctx); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4864 | return -1; |
| 4865 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4866 | LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4867 | |
| 4868 | /* common part for all extension types */ |
| 4869 | (*ext)->def = e; |
| 4870 | (*ext)->parent = info->parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4871 | (*ext)->parent_type = info->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4872 | (*ext)->insubstmt = info->substmt; |
| 4873 | (*ext)->insubstmt_index = info->substmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 4874 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4875 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4876 | |
PavolVican | d86b0d6 | 2017-09-01 11:01:39 +0200 | [diff] [blame] | 4877 | if (e->argument) { |
| 4878 | if (!(e->flags & LYS_YINELEM)) { |
| 4879 | (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL); |
| 4880 | if (!(*ext)->arg_value) { |
PavolVican | e7a1fc6 | 2018-02-19 16:50:27 +0100 | [diff] [blame] | 4881 | 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] | 4882 | return -1; |
| 4883 | } |
| 4884 | |
| 4885 | (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0); |
| 4886 | } else { |
| 4887 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4888 | if (ly_strequal(yin->name, e->argument, 1)) { |
| 4889 | (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0); |
| 4890 | lyxml_free(mod->ctx, yin); |
| 4891 | break; |
| 4892 | } |
| 4893 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4894 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4895 | } |
| 4896 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 4897 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 4898 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 4899 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 4902 | (*ext)->nodetype = LYS_EXT; |
| 4903 | (*ext)->module = info->mod; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4904 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4905 | /* extension type-specific part - parsing content */ |
| 4906 | switch (etype) { |
| 4907 | case LYEXT_FLAG: |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4908 | LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) { |
| 4909 | if (!yin->ns) { |
| 4910 | /* garbage */ |
| 4911 | lyxml_free(mod->ctx, yin); |
| 4912 | continue; |
| 4913 | } else if (!strcmp(yin->ns->value, LY_NSYIN)) { |
| 4914 | /* standard YANG statements are not expected here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4915 | 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] | 4916 | return -1; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4917 | } else if (yin->ns == info->data.yin->ns && |
| 4918 | (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) { |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4919 | /* we have the extension's argument */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4920 | if ((*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4921 | 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] | 4922 | return -1; |
| 4923 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4924 | (*ext)->arg_value = yin->content; |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4925 | yin->content = NULL; |
| 4926 | lyxml_free(mod->ctx, yin); |
| 4927 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4928 | /* extension instance */ |
| 4929 | if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin, |
| 4930 | LYEXT_SUBSTMT_SELF, 0, unres)) { |
| 4931 | return -1; |
| 4932 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4933 | |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4934 | continue; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4935 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4936 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4937 | break; |
| 4938 | case LYEXT_COMPLEX: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4939 | ((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] | 4940 | if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) { |
| 4941 | /* TODO memory cleanup */ |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4942 | return -1; |
| 4943 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4944 | break; |
| 4945 | default: |
| 4946 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4947 | } |
Radek Krejci | 72b3599 | 2017-01-04 16:27:44 +0100 | [diff] [blame] | 4948 | |
| 4949 | /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */ |
| 4950 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4951 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4952 | /* YANG */ |
| 4953 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4954 | ext_prefix = (char *)(*ext)->def; |
| 4955 | tmp = strchr(ext_prefix, ':'); |
| 4956 | if (!tmp) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4957 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4958 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4959 | } |
| 4960 | ext_name = tmp + 1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 4961 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4962 | /* get the module where the extension is supposed to be defined */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 4963 | 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] | 4964 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4965 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4966 | return EXIT_FAILURE; |
| 4967 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4968 | ctx = mod->ctx; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4969 | |
| 4970 | /* find the extension definition */ |
| 4971 | e = NULL; |
| 4972 | for (i = 0; i < mod->extensions_size; i++) { |
| 4973 | if (ly_strequal(mod->extensions[i].name, ext_name, 0)) { |
| 4974 | e = &mod->extensions[i]; |
| 4975 | break; |
| 4976 | } |
| 4977 | } |
| 4978 | /* try submodules */ |
| 4979 | for (j = 0; !e && j < mod->inc_size; j++) { |
| 4980 | for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) { |
| 4981 | if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) { |
| 4982 | e = &mod->inc[j].submodule->extensions[i]; |
| 4983 | break; |
| 4984 | } |
| 4985 | } |
| 4986 | } |
| 4987 | if (!e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4988 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4989 | return EXIT_FAILURE; |
| 4990 | } |
| 4991 | |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 4992 | (*ext)->flags &= ~LYEXT_OPT_YANG; |
| 4993 | (*ext)->def = NULL; |
| 4994 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4995 | /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */ |
| 4996 | |
| 4997 | if (e->plugin && e->plugin->check_position) { |
| 4998 | /* common part - we have plugin with position checking function, use it first */ |
| 4999 | if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) { |
| 5000 | /* extension is not allowed here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5001 | LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5002 | goto error; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5003 | } |
| 5004 | } |
| 5005 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5006 | /* extension common part */ |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5007 | (*ext)->def = e; |
| 5008 | (*ext)->parent = info->parent; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 5009 | (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5010 | (*ext)->flags |= e->plugin ? e->plugin->flags : 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5011 | |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5012 | if (e->argument && !(*ext)->arg_value) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5013 | LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name); |
PavolVican | b0d8410 | 2017-02-15 16:32:42 +0100 | [diff] [blame] | 5014 | goto error; |
| 5015 | } |
| 5016 | |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5017 | if ((*ext)->flags & LYEXT_OPT_VALID && |
| 5018 | (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) { |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 5019 | ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT; |
PavolVican | 92f2362 | 2017-12-12 13:35:56 +0100 | [diff] [blame] | 5020 | } |
| 5021 | |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 5022 | (*ext)->module = info->mod; |
| 5023 | (*ext)->nodetype = LYS_EXT; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5024 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5025 | /* extension type-specific part */ |
| 5026 | if (e->plugin) { |
| 5027 | etype = e->plugin->type; |
| 5028 | } else { |
| 5029 | /* default type */ |
| 5030 | etype = LYEXT_FLAG; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 5031 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5032 | switch (etype) { |
| 5033 | case LYEXT_FLAG: |
| 5034 | /* nothing change */ |
| 5035 | break; |
| 5036 | case LYEXT_COMPLEX: |
| 5037 | tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5038 | LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5039 | memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext); |
| 5040 | (*ext) = tmp_ext; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5041 | ((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] | 5042 | if (info->data.yang) { |
| 5043 | *tmp = ':'; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5044 | if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix, |
| 5045 | (struct lys_ext_instance_complex*)(*ext))) { |
| 5046 | goto error; |
| 5047 | } |
| 5048 | if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix, |
| 5049 | info->data.yang->ext_modules, info->mod->implemented)) { |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5050 | goto error; |
| 5051 | } |
PavolVican | a387667 | 2017-02-21 15:49:51 +0100 | [diff] [blame] | 5052 | } |
| 5053 | if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) { |
| 5054 | goto error; |
PavolVican | a1e291f | 2017-02-19 16:07:12 +0100 | [diff] [blame] | 5055 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5056 | break; |
| 5057 | case LYEXT_ERR: |
| 5058 | /* we never should be here */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5059 | LOGINT(ctx); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5060 | goto error; |
| 5061 | } |
| 5062 | |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5063 | if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) { |
| 5064 | goto error; |
| 5065 | } |
| 5066 | free(ext_prefix); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | return EXIT_SUCCESS; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 5070 | error: |
| 5071 | free(ext_prefix); |
| 5072 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5075 | /** |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5076 | * @brief Resolve (find) choice default case. Does not log. |
| 5077 | * |
| 5078 | * @param[in] choic Choice to use. |
| 5079 | * @param[in] dflt Name of the default case. |
| 5080 | * |
| 5081 | * @return Pointer to the default node or NULL. |
| 5082 | */ |
| 5083 | static struct lys_node * |
| 5084 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 5085 | { |
| 5086 | struct lys_node *child, *ret; |
| 5087 | |
| 5088 | LY_TREE_FOR(choic->child, child) { |
| 5089 | if (child->nodetype == LYS_USES) { |
| 5090 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 5091 | if (ret) { |
| 5092 | return ret; |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | 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] | 5097 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5098 | return child; |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | return NULL; |
| 5103 | } |
| 5104 | |
| 5105 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5106 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 5107 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5108 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5109 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5110 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5111 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5112 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 5113 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5114 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5115 | { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5116 | struct ly_ctx *ctx = uses->module->ctx; /* shortcut */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5117 | struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5118 | struct lys_node *node_aux, *parent, *tmp; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5119 | struct lys_node_leaflist *llist; |
| 5120 | struct lys_node_leaf *leaf; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 5121 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5122 | struct lys_restr *must, **old_must; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5123 | struct lys_iffeature *iff, **old_iff; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5124 | int i, j, k, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5125 | uint8_t size, *old_size; |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5126 | unsigned int usize, usize1, usize2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5127 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5128 | assert(uses->grp); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5129 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5130 | /* check that the grouping is resolved (no unresolved uses inside) */ |
| 5131 | assert(!uses->grp->unres_count); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 5132 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5133 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5134 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 5135 | if (node_aux->nodetype & LYS_GROUPING) { |
| 5136 | /* do not instantiate groupings from groupings */ |
| 5137 | continue; |
| 5138 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5139 | 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] | 5140 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5141 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 5142 | 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] | 5143 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5144 | } |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5145 | /* test the name of siblings */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5146 | 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] | 5147 | 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] | 5148 | goto fail; |
Pavol Vican | 55abd33 | 2016-07-12 15:54:49 +0200 | [diff] [blame] | 5149 | } |
| 5150 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5151 | } |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 5152 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 5153 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 5154 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5155 | if (uses->refine_size) { |
| 5156 | refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5157 | LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5158 | } |
| 5159 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5160 | /* apply refines */ |
| 5161 | for (i = 0; i < uses->refine_size; i++) { |
| 5162 | rfn = &uses->refine[i]; |
Radek Krejci | e207741 | 2017-01-26 16:03:39 +0100 | [diff] [blame] | 5163 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, |
| 5164 | LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF, |
Michal Vasko | dc300b0 | 2017-04-07 14:09:20 +0200 | [diff] [blame] | 5165 | 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 5166 | if (rc || !node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5167 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5168 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5169 | } |
| 5170 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5171 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5172 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 5173 | 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] | 5174 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5175 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5176 | refine_nodes[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5177 | |
| 5178 | /* description on any nodetype */ |
| 5179 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5180 | lydict_remove(ctx, node->dsc); |
| 5181 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /* reference on any nodetype */ |
| 5185 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5186 | lydict_remove(ctx, node->ref); |
| 5187 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5188 | } |
| 5189 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5190 | /* config on any nodetype, |
| 5191 | * in case of notification or rpc/action, the config is not applicable (there is no config status) */ |
| 5192 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5193 | node->flags &= ~LYS_CONFIG_MASK; |
| 5194 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | /* default value ... */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5198 | if (rfn->dflt_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5199 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5200 | /* leaf */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5201 | leaf = (struct lys_node_leaf *)node; |
| 5202 | |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5203 | /* replace default value */ |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5204 | lydict_remove(ctx, leaf->dflt); |
| 5205 | leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0); |
| 5206 | |
| 5207 | /* check the default value */ |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5208 | if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 5209 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5210 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5211 | } |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5212 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 5213 | /* leaf-list */ |
| 5214 | llist = (struct lys_node_leaflist *)node; |
| 5215 | |
| 5216 | /* remove complete set of defaults in target */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5217 | for (j = 0; j < llist->dflt_size; j++) { |
| 5218 | lydict_remove(ctx, llist->dflt[j]); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5219 | } |
| 5220 | free(llist->dflt); |
| 5221 | |
| 5222 | /* copy the default set from refine */ |
Radek Krejci | aa1303c | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 5223 | llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5224 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5225 | llist->dflt_size = rfn->dflt_size; |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5226 | for (j = 0; j < llist->dflt_size; j++) { |
| 5227 | llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0); |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | /* check default value */ |
Radek Krejci | 542ab14 | 2017-01-23 15:57:08 +0100 | [diff] [blame] | 5231 | for (j = 0; j < llist->dflt_size; j++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 5232 | 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] | 5233 | (struct lys_node *)(&llist->dflt[j])) == -1) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5234 | goto fail; |
Radek Krejci | 200bf71 | 2016-08-16 17:11:04 +0200 | [diff] [blame] | 5235 | } |
| 5236 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | |
| 5240 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 5241 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5242 | /* remove current value */ |
| 5243 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5244 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 5245 | /* set new value */ |
| 5246 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
| 5247 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5248 | if (rfn->flags & LYS_MAND_TRUE) { |
| 5249 | /* check if node has default value */ |
| 5250 | if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5251 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5252 | "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5253 | goto fail; |
| 5254 | } |
| 5255 | if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5256 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, |
Radek Krejci | bdcaf24 | 2017-04-19 10:29:47 +0200 | [diff] [blame] | 5257 | "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5258 | goto fail; |
| 5259 | } |
| 5260 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5261 | } |
| 5262 | |
| 5263 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5264 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 5265 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 5266 | ((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] | 5267 | } |
| 5268 | |
| 5269 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5270 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5271 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5272 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5273 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5274 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5275 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5276 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5277 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5278 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5279 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5280 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 5281 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 5282 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 5287 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5288 | switch (node->nodetype) { |
| 5289 | case LYS_LEAF: |
| 5290 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 5291 | old_must = &((struct lys_node_leaf *)node)->must; |
| 5292 | break; |
| 5293 | case LYS_LEAFLIST: |
| 5294 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 5295 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 5296 | break; |
| 5297 | case LYS_LIST: |
| 5298 | old_size = &((struct lys_node_list *)node)->must_size; |
| 5299 | old_must = &((struct lys_node_list *)node)->must; |
| 5300 | break; |
| 5301 | case LYS_CONTAINER: |
| 5302 | old_size = &((struct lys_node_container *)node)->must_size; |
| 5303 | old_must = &((struct lys_node_container *)node)->must; |
| 5304 | break; |
| 5305 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 5306 | case LYS_ANYDATA: |
| 5307 | old_size = &((struct lys_node_anydata *)node)->must_size; |
| 5308 | old_must = &((struct lys_node_anydata *)node)->must; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5309 | break; |
| 5310 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5311 | LOGINT(ctx); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5312 | goto fail; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | size = *old_size + rfn->must_size; |
| 5316 | must = realloc(*old_must, size * sizeof *rfn->must); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5317 | LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5318 | for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) { |
Radek Krejci | 7f0164a | 2017-01-25 17:04:06 +0100 | [diff] [blame] | 5319 | must[j].ext_size = rfn->must[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5320 | 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] | 5321 | &must[j].ext, 0, unres); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5322 | must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0); |
| 5323 | must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0); |
| 5324 | must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0); |
| 5325 | must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0); |
| 5326 | must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0); |
Radek Krejci | cfcd8a5 | 2017-09-04 13:19:57 +0200 | [diff] [blame] | 5327 | must[j].flags = rfn->must[k].flags; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 5330 | *old_must = must; |
| 5331 | *old_size = size; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 5332 | |
| 5333 | /* check XPath dependencies again */ |
| 5334 | if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 5335 | goto fail; |
| 5336 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5337 | } |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5338 | |
| 5339 | /* if-feature in leaf, leaf-list, list, container or anyxml */ |
| 5340 | if (rfn->iffeature_size) { |
| 5341 | old_size = &node->iffeature_size; |
| 5342 | old_iff = &node->iffeature; |
| 5343 | |
| 5344 | size = *old_size + rfn->iffeature_size; |
| 5345 | iff = realloc(*old_iff, size * sizeof *rfn->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5346 | LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail); |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5347 | *old_iff = iff; |
| 5348 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5349 | for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) { |
| 5350 | resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5351 | if (usize1) { |
| 5352 | /* there is something to duplicate */ |
| 5353 | /* duplicate compiled expression */ |
| 5354 | usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0; |
| 5355 | iff[j].expr = malloc(usize * sizeof *iff[j].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5356 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5357 | 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] | 5358 | |
| 5359 | /* duplicate list of feature pointers */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5360 | iff[j].features = malloc(usize2 * sizeof *iff[k].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5361 | LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail); |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5362 | 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] | 5363 | |
Radek Krejci | 3a3b200 | 2017-09-13 16:39:02 +0200 | [diff] [blame] | 5364 | /* duplicate extensions */ |
| 5365 | iff[j].ext_size = rfn->iffeature[k].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 5366 | 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] | 5367 | &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres); |
| 5368 | } |
| 5369 | (*old_size)++; |
| 5370 | } |
| 5371 | assert(*old_size == size); |
Radek Krejci | 363bd4a | 2016-07-29 14:30:20 +0200 | [diff] [blame] | 5372 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | /* apply augments */ |
| 5376 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9723426 | 2018-02-01 09:53:01 +0100 | [diff] [blame] | 5377 | rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5378 | if (rc) { |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5379 | goto fail; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5383 | /* check refines */ |
| 5384 | for (i = 0; i < uses->refine_size; i++) { |
| 5385 | node = refine_nodes[i]; |
| 5386 | rfn = &uses->refine[i]; |
| 5387 | |
| 5388 | /* config on any nodetype */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 5389 | if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5390 | 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] | 5391 | if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) && |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5392 | ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
| 5393 | (rfn->flags & LYS_CONFIG_W)) { |
| 5394 | /* setting config true under config false is prohibited */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5395 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5396 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5397 | "changing config from 'false' to 'true' is prohibited while " |
| 5398 | "the target's parent is still config 'false'."); |
| 5399 | goto fail; |
| 5400 | } |
| 5401 | |
| 5402 | /* inherit config change to the target children */ |
| 5403 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 5404 | if (rfn->flags & LYS_CONFIG_W) { |
| 5405 | if (iter->flags & LYS_CONFIG_SET) { |
| 5406 | /* config is set explicitely, go to next sibling */ |
| 5407 | next = NULL; |
| 5408 | goto nextsibling; |
| 5409 | } |
| 5410 | } else { /* LYS_CONFIG_R */ |
| 5411 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 5412 | /* error - we would have config data under status data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5413 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 5414 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5415 | "changing config from 'true' to 'false' is prohibited while the target " |
| 5416 | "has still a children with explicit config 'true'."); |
| 5417 | goto fail; |
| 5418 | } |
| 5419 | } |
| 5420 | /* change config */ |
| 5421 | iter->flags &= ~LYS_CONFIG_MASK; |
| 5422 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 5423 | |
| 5424 | /* select next iter - modified LY_TREE_DFS_END */ |
| 5425 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5426 | next = NULL; |
| 5427 | } else { |
| 5428 | next = iter->child; |
| 5429 | } |
| 5430 | nextsibling: |
| 5431 | if (!next) { |
| 5432 | /* try siblings */ |
| 5433 | next = iter->next; |
| 5434 | } |
| 5435 | while (!next) { |
| 5436 | /* parent is already processed, go to its sibling */ |
| 5437 | iter = lys_parent(iter); |
| 5438 | |
| 5439 | /* no siblings, go back through parents */ |
| 5440 | if (iter == node) { |
| 5441 | /* we are done, no next element to process */ |
| 5442 | break; |
| 5443 | } |
| 5444 | next = iter->next; |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | /* default value */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5450 | if (rfn->dflt_size) { |
| 5451 | if (node->nodetype == LYS_CHOICE) { |
| 5452 | /* choice */ |
| 5453 | ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node, |
| 5454 | rfn->dflt[0]); |
| 5455 | if (!((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5456 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default"); |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5457 | goto fail; |
| 5458 | } |
| 5459 | if (lyp_check_mandatory_choice(node)) { |
| 5460 | goto fail; |
| 5461 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5466 | if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5467 | 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] | 5468 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5469 | 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] | 5470 | goto fail; |
| 5471 | } |
Radek Krejci | 2d3c811 | 2017-04-19 10:20:50 +0200 | [diff] [blame] | 5472 | } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5473 | 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] | 5474 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements"); |
| 5475 | 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] | 5476 | goto fail; |
| 5477 | } |
| 5478 | } |
| 5479 | |
| 5480 | /* additional checks */ |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5481 | /* default value with mandatory/min-elements */ |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5482 | if (node->nodetype == LYS_LEAFLIST) { |
| 5483 | llist = (struct lys_node_leaflist *)node; |
| 5484 | if (llist->dflt_size && llist->min) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5485 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine"); |
| 5486 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5487 | "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement."); |
| 5488 | goto fail; |
| 5489 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5490 | } else if (node->nodetype == LYS_LEAF) { |
| 5491 | leaf = (struct lys_node_leaf *)node; |
| 5492 | if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5493 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine"); |
| 5494 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5495 | "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement."); |
| 5496 | goto fail; |
| 5497 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5498 | } |
Radek Krejci | 4c5fbd3 | 2016-10-25 15:14:23 +0200 | [diff] [blame] | 5499 | |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5500 | /* 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] | 5501 | if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5502 | for (parent = node->parent; |
| 5503 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES)); |
| 5504 | parent = parent->parent) { |
| 5505 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 5506 | /* stop also on presence containers */ |
| 5507 | break; |
| 5508 | } |
| 5509 | } |
| 5510 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 5511 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 5512 | if (lyp_check_mandatory_choice(parent)) { |
| 5513 | goto fail; |
| 5514 | } |
| 5515 | } |
| 5516 | } |
| 5517 | } |
| 5518 | free(refine_nodes); |
| 5519 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5520 | return EXIT_SUCCESS; |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5521 | |
| 5522 | fail: |
| 5523 | LY_TREE_FOR_SAFE(uses->child, next, iter) { |
| 5524 | lys_node_free(iter, NULL, 0); |
| 5525 | } |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 5526 | free(refine_nodes); |
Michal Vasko | a86508c | 2016-08-26 14:30:19 +0200 | [diff] [blame] | 5527 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5528 | } |
| 5529 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5530 | void |
| 5531 | resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base) |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5532 | { |
| 5533 | int i; |
| 5534 | |
| 5535 | assert(der && base); |
| 5536 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5537 | if (!base->der) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5538 | /* create a set for backlinks if it does not exist */ |
| 5539 | base->der = ly_set_new(); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5540 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5541 | /* store backlink */ |
| 5542 | ly_set_add(base->der, der, LY_SET_OPT_USEASLIST); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5543 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5544 | /* do it recursively */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5545 | for (i = 0; i < base->base_size; i++) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5546 | resolve_identity_backlink_update(der, base->base[i]); |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5547 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5548 | } |
| 5549 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5550 | /** |
| 5551 | * @brief Resolve base identity recursively. Does not log. |
| 5552 | * |
| 5553 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5554 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5555 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5556 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5557 | * |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5558 | * @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] | 5559 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5560 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 5561 | 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] | 5562 | struct unres_schema *unres, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5563 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 5564 | uint32_t i, j; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5565 | struct lys_ident *base = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5566 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5567 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5568 | assert(ret); |
| 5569 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5570 | /* search module */ |
| 5571 | for (i = 0; i < module->ident_size; i++) { |
| 5572 | if (!strcmp(basename, module->ident[i].name)) { |
| 5573 | |
| 5574 | if (!ident) { |
| 5575 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5576 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5577 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5578 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5581 | base = &module->ident[i]; |
| 5582 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | |
| 5586 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5587 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 5588 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 5589 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5590 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5591 | if (!ident) { |
| 5592 | *ret = &module->inc[j].submodule->ident[i]; |
| 5593 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5594 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5595 | |
| 5596 | base = &module->inc[j].submodule->ident[i]; |
| 5597 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | } |
| 5601 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 5602 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5603 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5604 | if (base) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5605 | /* is it already completely resolved? */ |
| 5606 | for (i = 0; i < unres->count; i++) { |
Radek Krejci | ba7b1cc | 2016-08-08 13:44:43 +0200 | [diff] [blame] | 5607 | if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) { |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5608 | /* identity found, but not yet resolved, so do not return it in *res and try it again later */ |
| 5609 | |
| 5610 | /* simple check for circular reference, |
| 5611 | * the complete check is done as a side effect of using only completely |
| 5612 | * resolved identities (previous check of unres content) */ |
| 5613 | if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5614 | LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base"); |
| 5615 | 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] | 5616 | return -1; |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5617 | } |
| 5618 | |
Radek Krejci | 06f64ed | 2016-08-15 11:07:44 +0200 | [diff] [blame] | 5619 | return EXIT_FAILURE; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5620 | } |
| 5621 | } |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5622 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5623 | /* checks done, store the result */ |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 5624 | *ret = base; |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 5625 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5626 | } |
| 5627 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5628 | /* base not found (maybe a forward reference) */ |
| 5629 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5630 | } |
| 5631 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5632 | /** |
| 5633 | * @brief Resolve base identity. Logs directly. |
| 5634 | * |
| 5635 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5636 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5637 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 5638 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5639 | * @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] | 5640 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5641 | * @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] | 5642 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5643 | static int |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5644 | 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] | 5645 | struct lys_type *type, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5646 | { |
| 5647 | const char *name; |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5648 | int mod_name_len = 0, rc; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5649 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 5650 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5651 | struct lys_module *mod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5652 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5653 | |
| 5654 | assert((ident && !type) || (!ident && type)); |
| 5655 | |
| 5656 | if (!type) { |
| 5657 | /* have ident to resolve */ |
| 5658 | ret = ⌖ |
| 5659 | flags = ident->flags; |
| 5660 | mod = ident->module; |
| 5661 | } else { |
| 5662 | /* have type to fill */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5663 | ++type->info.ident.count; |
| 5664 | 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] | 5665 | LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1); |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5666 | |
| 5667 | ret = &type->info.ident.ref[type->info.ident.count - 1]; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 5668 | flags = type->parent->flags; |
| 5669 | mod = type->parent->module; |
| 5670 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 5671 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5672 | |
| 5673 | /* search for the base identity */ |
| 5674 | name = strchr(basename, ':'); |
| 5675 | if (name) { |
| 5676 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5677 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5678 | name++; |
| 5679 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5680 | 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] | 5681 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 5682 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5683 | } |
| 5684 | } else { |
| 5685 | name = basename; |
| 5686 | } |
| 5687 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5688 | /* get module where to search */ |
Michal Vasko | 921eb6b | 2017-10-13 10:01:39 +0200 | [diff] [blame] | 5689 | 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] | 5690 | if (!module) { |
| 5691 | /* identity refers unknown data model */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5692 | LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5693 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5694 | } |
| 5695 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 5696 | /* search in the identified module ... */ |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5697 | rc = resolve_base_ident_sub(module, ident, name, unres, ret); |
| 5698 | if (!rc) { |
| 5699 | assert(*ret); |
| 5700 | |
| 5701 | /* check status */ |
| 5702 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 5703 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
| 5704 | rc = -1; |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 5705 | } else if (ident) { |
| 5706 | ident->base[ident->base_size++] = *ret; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5707 | if (lys_main_module(mod)->implemented) { |
| 5708 | /* in case of the implemented identity, maintain backlinks to it |
| 5709 | * from the base identities to make it available when resolving |
| 5710 | * data with the identity values (not implemented identity is not |
| 5711 | * allowed as an identityref value). */ |
| 5712 | resolve_identity_backlink_update(ident, *ret); |
| 5713 | } |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5714 | } |
| 5715 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5716 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Pavol Vican | 053a288 | 2016-09-05 14:40:33 +0200 | [diff] [blame] | 5717 | if (type) { |
| 5718 | --type->info.ident.count; |
| 5719 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5720 | } |
| 5721 | |
Radek Krejci | 219fa61 | 2016-08-15 10:36:51 +0200 | [diff] [blame] | 5722 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5725 | /* |
| 5726 | * 1 - true (der is derived from base) |
| 5727 | * 0 - false (der is not derived from base) |
| 5728 | */ |
| 5729 | static int |
| 5730 | search_base_identity(struct lys_ident *der, struct lys_ident *base) |
| 5731 | { |
| 5732 | int i; |
| 5733 | |
| 5734 | if (der == base) { |
| 5735 | return 1; |
| 5736 | } else { |
| 5737 | for(i = 0; i < der->base_size; i++) { |
| 5738 | if (search_base_identity(der->base[i], base) == 1) { |
| 5739 | return 1; |
| 5740 | } |
| 5741 | } |
| 5742 | } |
| 5743 | |
| 5744 | return 0; |
| 5745 | } |
| 5746 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5747 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 5748 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5749 | * |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5750 | * @param[in] type Identityref type. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5751 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 5752 | * @param[in] node Node where the identityref is being resolved |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5753 | * @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] | 5754 | * |
| 5755 | * @return Pointer to the identity resolvent, NULL on error. |
| 5756 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 5757 | struct lys_ident * |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5758 | 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] | 5759 | { |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5760 | const char *mod_name, *name; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5761 | char *str; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 5762 | int mod_name_len, nam_len, rc; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5763 | int need_implemented = 0; |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5764 | unsigned int i, j; |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5765 | struct lys_ident *der, *cur; |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5766 | struct lys_module *imod = NULL, *m, *tmod; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5767 | struct ly_ctx *ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5768 | |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5769 | assert(type && ident_name && mod); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5770 | ctx = mod->ctx; |
Radek Krejci | bb1ce0f | 2016-12-05 13:24:33 +0100 | [diff] [blame] | 5771 | |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5772 | if (!type || (!type->info.ident.count && !type->der) || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5773 | return NULL; |
| 5774 | } |
| 5775 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5776 | 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] | 5777 | if (rc < 1) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5778 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5779 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 5780 | } else if (rc < (signed)strlen(ident_name)) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5781 | LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5782 | return NULL; |
| 5783 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5784 | |
| 5785 | m = lys_main_module(mod); /* shortcut */ |
| 5786 | if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) { |
| 5787 | /* identity is defined in the same module as node */ |
| 5788 | imod = m; |
| 5789 | } else if (dflt) { |
| 5790 | /* solving identityref in default definition in schema - |
| 5791 | * find the identity's module in the imported modules list to have a correct revision */ |
| 5792 | for (i = 0; i < mod->imp_size; i++) { |
| 5793 | if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) { |
| 5794 | imod = mod->imp[i].module; |
| 5795 | break; |
| 5796 | } |
| 5797 | } |
Andrew Langefeld | 8f50a2d | 2018-05-23 17:16:12 -0500 | [diff] [blame] | 5798 | |
| 5799 | /* We may need to pull it from the module that the typedef came from */ |
| 5800 | if (!imod && type && type->der) { |
| 5801 | tmod = type->der->module; |
| 5802 | for (i = 0; i < tmod->imp_size; i++) { |
| 5803 | if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) { |
| 5804 | imod = tmod->imp[i].module; |
| 5805 | break; |
| 5806 | } |
| 5807 | } |
| 5808 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5809 | } else { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5810 | /* solving identityref in data - get the module from the context */ |
| 5811 | for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) { |
| 5812 | imod = mod->ctx->models.list[i]; |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5813 | 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] | 5814 | break; |
| 5815 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5816 | imod = NULL; |
| 5817 | } |
Radek Krejci | 5ba0510 | 2017-10-26 15:02:52 +0200 | [diff] [blame] | 5818 | if (!imod && mod->ctx->models.parsing_sub_modules_count) { |
| 5819 | /* we are currently parsing some module and checking XPath or a default value, |
| 5820 | * so take this module into account */ |
| 5821 | for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) { |
| 5822 | imod = mod->ctx->models.parsing_sub_modules[i]; |
| 5823 | if (imod->type) { |
| 5824 | /* skip submodules */ |
| 5825 | continue; |
| 5826 | } |
| 5827 | if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) { |
| 5828 | break; |
| 5829 | } |
| 5830 | imod = NULL; |
| 5831 | } |
| 5832 | } |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5833 | } |
| 5834 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5835 | if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5836 | /* the needed module was not found, but it may have been expected so call the data callback */ |
| 5837 | if (imod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5838 | 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] | 5839 | } else if (mod_name) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5840 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5841 | 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] | 5842 | free(str); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5843 | } |
| 5844 | } |
| 5845 | if (!imod) { |
| 5846 | goto fail; |
| 5847 | } |
| 5848 | |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5849 | if (m != imod || lys_main_module(type->parent->module) != mod) { |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5850 | /* the type is not referencing the same schema, |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5851 | * THEN, we may need to make the module with the identity implemented, but only if it really |
| 5852 | * contains the identity */ |
| 5853 | if (!imod->implemented) { |
| 5854 | cur = NULL; |
| 5855 | /* get the identity in the module */ |
| 5856 | for (i = 0; i < imod->ident_size; i++) { |
| 5857 | if (!strcmp(name, imod->ident[i].name)) { |
| 5858 | cur = &imod->ident[i]; |
| 5859 | break; |
| 5860 | } |
| 5861 | } |
| 5862 | if (!cur) { |
| 5863 | /* go through includes */ |
| 5864 | for (j = 0; j < imod->inc_size; j++) { |
| 5865 | for (i = 0; i < imod->inc[j].submodule->ident_size; i++) { |
| 5866 | if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) { |
| 5867 | cur = &imod->inc[j].submodule->ident[i]; |
| 5868 | break; |
| 5869 | } |
| 5870 | } |
| 5871 | } |
| 5872 | if (!cur) { |
| 5873 | goto fail; |
| 5874 | } |
| 5875 | } |
| 5876 | |
| 5877 | /* check that identity is derived from one of the type's base */ |
| 5878 | while (type->der) { |
| 5879 | for (i = 0; i < type->info.ident.count; i++) { |
| 5880 | if (search_base_identity(cur, type->info.ident.ref[i])) { |
| 5881 | /* cur's base matches the type's base */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5882 | need_implemented = 1; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5883 | goto match; |
| 5884 | } |
| 5885 | } |
| 5886 | type = &type->der->type; |
| 5887 | } |
| 5888 | /* matching base not found */ |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5889 | LOGVAL(ctx, LYE_SPEC, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "Identity used as identityref value is not implemented."); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5890 | goto fail; |
| 5891 | } |
Radek Krejci | f32c5f6 | 2016-12-05 09:27:38 +0100 | [diff] [blame] | 5892 | } |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5893 | |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5894 | /* go through all the derived types of all the bases */ |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5895 | while (type->der) { |
| 5896 | for (i = 0; i < type->info.ident.count; ++i) { |
| 5897 | cur = type->info.ident.ref[i]; |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 5898 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5899 | if (cur->der) { |
Radek Krejci | 98a1e2d | 2017-04-26 14:34:52 +0200 | [diff] [blame] | 5900 | /* there are some derived identities */ |
Michal Vasko | 08767f7 | 2017-10-06 14:38:08 +0200 | [diff] [blame] | 5901 | for (j = 0; j < cur->der->number; j++) { |
| 5902 | der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5903 | if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 5904 | /* we have match */ |
| 5905 | cur = der; |
| 5906 | goto match; |
| 5907 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5908 | } |
| 5909 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5910 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5911 | type = &type->der->type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5912 | } |
| 5913 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5914 | fail: |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5915 | LOGVAL(ctx, LYE_INRESOLV, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5916 | return NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 5917 | |
| 5918 | match: |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5919 | for (i = 0; i < cur->iffeature_size; i++) { |
| 5920 | if (!resolve_iffeature(&cur->iffeature[i])) { |
Michal Vasko | ae4b7d3 | 2018-07-13 12:21:01 +0200 | [diff] [blame] | 5921 | if (node) { |
| 5922 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name); |
| 5923 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5924 | 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] | 5925 | return NULL; |
| 5926 | } |
| 5927 | } |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5928 | if (need_implemented) { |
| 5929 | if (dflt) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5930 | /* later try to make the module implemented */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5931 | LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module", |
| 5932 | imod->name, cur->name, mod->name); |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 5933 | /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of |
| 5934 | * several functions with little gain */ |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5935 | if (lys_set_implemented(imod)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5936 | 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] | 5937 | imod->name, cur->name); |
Michal Vasko | 3f3c6a8 | 2017-10-03 10:23:23 +0200 | [diff] [blame] | 5938 | goto fail; |
| 5939 | } |
| 5940 | } else { |
| 5941 | /* just say that it was found, but in a non-implemented module */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5942 | 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] | 5943 | lys_main_module(cur->module)->name); |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 5944 | goto fail; |
| 5945 | } |
| 5946 | } |
Michal Vasko | f2d4396 | 2016-09-02 11:10:16 +0200 | [diff] [blame] | 5947 | return cur; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5948 | } |
| 5949 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5950 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5951 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5952 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 5953 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5954 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 5955 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5956 | * @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] | 5957 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5958 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5959 | 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] | 5960 | { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5961 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5962 | struct lys_node *par_grp; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5963 | struct ly_ctx *ctx = uses->module->ctx; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5964 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 5965 | /* 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] | 5966 | * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far |
| 5967 | * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember |
| 5968 | * 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] | 5969 | 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] | 5970 | if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) { |
| 5971 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
| 5972 | return -1; |
| 5973 | } |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 5974 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 5975 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5976 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 5977 | if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5978 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5979 | return -1; |
| 5980 | } else if (rc > 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5981 | 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] | 5982 | return -1; |
| 5983 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5984 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5985 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5986 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5987 | return -1; |
| 5988 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5989 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 5990 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5991 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 5992 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 5993 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5994 | } |
| 5995 | |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5996 | if (uses->grp->unres_count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5997 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 5998 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5999 | LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6000 | return -1; |
| 6001 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6002 | uses->flags |= LYS_USESGRP; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6003 | } else { |
| 6004 | /* instantiate grouping only when it is completely resolved */ |
| 6005 | uses->grp = NULL; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 6006 | } |
Michal Vasko | a979272 | 2018-06-28 09:01:02 +0200 | [diff] [blame] | 6007 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6008 | return EXIT_FAILURE; |
| 6009 | } |
| 6010 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6011 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6012 | if (!rc) { |
| 6013 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6014 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6015 | assert(((struct lys_node_grp *)par_grp)->unres_count); |
| 6016 | ((struct lys_node_grp *)par_grp)->unres_count--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 6017 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6018 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6019 | |
| 6020 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6021 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 6022 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6023 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6024 | return -1; |
| 6025 | } |
| 6026 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6027 | return EXIT_SUCCESS; |
| 6028 | } |
| 6029 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6030 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6031 | } |
| 6032 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6033 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 6034 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6035 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6036 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6037 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 6038 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6039 | * @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] | 6040 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6041 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6042 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6043 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6044 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 6045 | const char *value; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6046 | char *s = NULL; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6047 | struct ly_ctx *ctx = list->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6048 | |
| 6049 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | dea17dd | 2017-06-02 15:20:43 +0200 | [diff] [blame] | 6050 | assert(keys_str); |
| 6051 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6052 | if (!list->child) { |
| 6053 | /* no child, possible forward reference */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6054 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6055 | return EXIT_FAILURE; |
| 6056 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6057 | /* get the key name */ |
| 6058 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 6059 | len = value - keys_str; |
| 6060 | while (isspace(value[0])) { |
| 6061 | value++; |
| 6062 | } |
| 6063 | } else { |
| 6064 | len = strlen(keys_str); |
| 6065 | } |
| 6066 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 6067 | rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF, |
| 6068 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6069 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6070 | LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 6071 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6072 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6073 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6074 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6075 | /* check_key logs */ |
| 6076 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6077 | } |
| 6078 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6079 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 6080 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6081 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 6082 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 6083 | return -1; |
| 6084 | } |
| 6085 | |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6086 | /* default value - is ignored, keep it but print a warning */ |
| 6087 | if (list->keys[i]->dflt) { |
| 6088 | /* log is not hidden only in case this resolving fails and in such a case |
| 6089 | * we cannot get here |
| 6090 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6091 | assert(log_opt == ILO_STORE); |
| 6092 | log_opt = ILO_LOG; |
| 6093 | 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] | 6094 | 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] | 6095 | log_opt = ILO_STORE; |
Radek Krejci | a98048c | 2017-05-24 16:35:48 +0200 | [diff] [blame] | 6096 | free(s); |
| 6097 | } |
| 6098 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6099 | /* prepare for next iteration */ |
| 6100 | while (value && isspace(value[0])) { |
| 6101 | value++; |
| 6102 | } |
| 6103 | keys_str = value; |
| 6104 | } |
| 6105 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6106 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6107 | } |
| 6108 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6109 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6110 | * @brief Resolve (check) all must conditions of \p node. |
| 6111 | * Logs directly. |
| 6112 | * |
| 6113 | * @param[in] node Data node with optional must statements. |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6114 | * @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] | 6115 | * |
| 6116 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 6117 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6118 | static int |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6119 | resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6120 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6121 | uint8_t i, must_size; |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6122 | struct lys_node *schema; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6123 | struct lys_restr *must; |
| 6124 | struct lyxp_set set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6125 | struct ly_ctx *ctx = node->schema->module->ctx; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6126 | |
| 6127 | assert(node); |
| 6128 | memset(&set, 0, sizeof set); |
| 6129 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6130 | if (inout_parent) { |
| 6131 | for (schema = lys_parent(node->schema); |
| 6132 | schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); |
| 6133 | schema = lys_parent(schema)); |
| 6134 | if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6135 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6136 | return -1; |
| 6137 | } |
| 6138 | must_size = ((struct lys_node_inout *)schema)->must_size; |
| 6139 | must = ((struct lys_node_inout *)schema)->must; |
| 6140 | |
| 6141 | /* context node is the RPC/action */ |
| 6142 | node = node->parent; |
| 6143 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6144 | LOGINT(ctx); |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6145 | return -1; |
| 6146 | } |
| 6147 | } else { |
| 6148 | switch (node->schema->nodetype) { |
| 6149 | case LYS_CONTAINER: |
| 6150 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 6151 | must = ((struct lys_node_container *)node->schema)->must; |
| 6152 | break; |
| 6153 | case LYS_LEAF: |
| 6154 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 6155 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 6156 | break; |
| 6157 | case LYS_LEAFLIST: |
| 6158 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 6159 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 6160 | break; |
| 6161 | case LYS_LIST: |
| 6162 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 6163 | must = ((struct lys_node_list *)node->schema)->must; |
| 6164 | break; |
| 6165 | case LYS_ANYXML: |
| 6166 | case LYS_ANYDATA: |
| 6167 | must_size = ((struct lys_node_anydata *)node->schema)->must_size; |
| 6168 | must = ((struct lys_node_anydata *)node->schema)->must; |
| 6169 | break; |
| 6170 | case LYS_NOTIF: |
| 6171 | must_size = ((struct lys_node_notif *)node->schema)->must_size; |
| 6172 | must = ((struct lys_node_notif *)node->schema)->must; |
| 6173 | break; |
| 6174 | default: |
| 6175 | must_size = 0; |
| 6176 | break; |
| 6177 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6181 | 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] | 6182 | return -1; |
| 6183 | } |
| 6184 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6185 | 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] | 6186 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 6187 | if (!set.val.bool) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6188 | 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] | 6189 | LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr); |
| 6190 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6191 | LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6192 | if (must[i].emsg) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6193 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6194 | } |
| 6195 | if (must[i].eapptag) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6196 | ly_err_last_set_apptag(ctx, must[i].eapptag); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6197 | } |
| 6198 | return 1; |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 6199 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6200 | } |
| 6201 | } |
| 6202 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6203 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 6204 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6205 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 6206 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6207 | * @brief Resolve (find) when condition schema context node. Does not log. |
| 6208 | * |
| 6209 | * @param[in] schema Schema node with the when condition. |
| 6210 | * @param[out] ctx_snode When schema context node. |
| 6211 | * @param[out] ctx_snode_type Schema context node type. |
| 6212 | */ |
| 6213 | void |
| 6214 | resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type) |
| 6215 | { |
| 6216 | const struct lys_node *sparent; |
| 6217 | |
| 6218 | /* find a not schema-only node */ |
| 6219 | *ctx_snode_type = LYXP_NODE_ELEM; |
| 6220 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 6221 | if (schema->nodetype == LYS_AUGMENT) { |
| 6222 | sparent = ((struct lys_node_augment *)schema)->target; |
| 6223 | } else { |
| 6224 | sparent = schema->parent; |
| 6225 | } |
| 6226 | if (!sparent) { |
| 6227 | /* context node is the document root (fake root in our case) */ |
| 6228 | if (schema->flags & LYS_CONFIG_W) { |
| 6229 | *ctx_snode_type = LYXP_NODE_ROOT_CONFIG; |
| 6230 | } else { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 6231 | *ctx_snode_type = LYXP_NODE_ROOT; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6232 | } |
Michal Vasko | 2d2aec1 | 2016-09-08 11:42:50 +0200 | [diff] [blame] | 6233 | /* we need the first top-level sibling, but no uses or groupings */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 6234 | schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6235 | break; |
| 6236 | } |
| 6237 | schema = sparent; |
| 6238 | } |
| 6239 | |
| 6240 | *ctx_snode = (struct lys_node *)schema; |
| 6241 | } |
| 6242 | |
| 6243 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6244 | * @brief Resolve (find) when condition context node. Does not log. |
| 6245 | * |
| 6246 | * @param[in] node Data node, whose conditional definition is being decided. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6247 | * @param[in] schema Schema node with the when condition. |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6248 | * @param[out] ctx_node Context node. |
| 6249 | * @param[out] ctx_node_type Context node type. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6250 | * |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6251 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6252 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6253 | static int |
| 6254 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node, |
| 6255 | enum lyxp_node_type *ctx_node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6256 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6257 | struct lyd_node *parent; |
| 6258 | struct lys_node *sparent; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6259 | enum lyxp_node_type node_type; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6260 | uint16_t i, data_depth, schema_depth; |
| 6261 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6262 | resolve_when_ctx_snode(schema, &schema, &node_type); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6263 | |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6264 | if (node_type == LYXP_NODE_ELEM) { |
| 6265 | /* standard element context node */ |
| 6266 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 6267 | for (sparent = schema, schema_depth = 0; |
| 6268 | sparent; |
| 6269 | sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) { |
| 6270 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) { |
| 6271 | ++schema_depth; |
| 6272 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6273 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6274 | if (data_depth < schema_depth) { |
| 6275 | return -1; |
| 6276 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6277 | |
Michal Vasko | 956e854 | 2016-08-26 09:43:35 +0200 | [diff] [blame] | 6278 | /* find the corresponding data node */ |
| 6279 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 6280 | node = node->parent; |
| 6281 | } |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6282 | if (node->schema != schema) { |
| 6283 | return -1; |
| 6284 | } |
Michal Vasko | fe98975 | 2016-09-08 08:47:26 +0200 | [diff] [blame] | 6285 | } else { |
| 6286 | /* root context node */ |
| 6287 | while (node->parent) { |
| 6288 | node = node->parent; |
| 6289 | } |
| 6290 | while (node->prev->next) { |
| 6291 | node = node->prev; |
| 6292 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6293 | } |
| 6294 | |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6295 | *ctx_node = node; |
| 6296 | *ctx_node_type = node_type; |
| 6297 | return EXIT_SUCCESS; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6298 | } |
| 6299 | |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6300 | /** |
| 6301 | * @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] | 6302 | * The context node is adjusted if needed. |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6303 | * |
| 6304 | * @param[in] snode Schema node, whose children instances need to be unlinked. |
| 6305 | * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked, |
| 6306 | * it is moved to point to another sibling still in the original tree. |
| 6307 | * @param[in,out] ctx_node When context node, adjusted if needed. |
| 6308 | * @param[in] ctx_node_type Context node type, just for information to detect invalid situations. |
| 6309 | * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards. |
| 6310 | * Ordering may change, but there will be no semantic change. |
| 6311 | * |
| 6312 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6313 | */ |
| 6314 | static int |
| 6315 | resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node, |
| 6316 | enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes) |
| 6317 | { |
| 6318 | struct lyd_node *next, *elem; |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6319 | const struct lys_node *slast; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6320 | struct ly_ctx *ctx = snode->module->ctx; |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6321 | |
| 6322 | switch (snode->nodetype) { |
| 6323 | case LYS_AUGMENT: |
| 6324 | case LYS_USES: |
| 6325 | case LYS_CHOICE: |
| 6326 | case LYS_CASE: |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6327 | slast = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 6328 | while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | eb81fb7 | 2017-02-06 12:11:08 +0100 | [diff] [blame] | 6329 | if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
| 6330 | continue; |
| 6331 | } |
| 6332 | |
| 6333 | 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] | 6334 | return -1; |
| 6335 | } |
| 6336 | } |
| 6337 | break; |
| 6338 | case LYS_CONTAINER: |
| 6339 | case LYS_LIST: |
| 6340 | case LYS_LEAF: |
| 6341 | case LYS_LEAFLIST: |
| 6342 | case LYS_ANYXML: |
| 6343 | case LYS_ANYDATA: |
| 6344 | LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) { |
| 6345 | if (elem->schema == snode) { |
| 6346 | |
| 6347 | if (elem == *ctx_node) { |
| 6348 | /* We are going to unlink our context node! This normally cannot happen, |
| 6349 | * but we use normal top-level data nodes for faking a document root node, |
| 6350 | * so if this is the context node, we just use the next top-level node. |
| 6351 | * Additionally, it can even happen that there are no top-level data nodes left, |
| 6352 | * all were unlinked, so in this case we pass NULL as the context node/data tree, |
| 6353 | * lyxp_eval() can handle this special situation. |
| 6354 | */ |
| 6355 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6356 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6357 | return -1; |
| 6358 | } |
| 6359 | |
| 6360 | if (elem->prev == elem) { |
| 6361 | /* unlinking last top-level element, use an empty data tree */ |
| 6362 | *ctx_node = NULL; |
| 6363 | } else { |
| 6364 | /* in this case just use the previous/last top-level data node */ |
| 6365 | *ctx_node = elem->prev; |
| 6366 | } |
| 6367 | } else if (elem == *node) { |
| 6368 | /* We are going to unlink the currently processed node. This does not matter that |
| 6369 | * much, but we would lose access to the original data tree, so just move our |
| 6370 | * pointer somewhere still inside it. |
| 6371 | */ |
| 6372 | if ((*node)->prev != *node) { |
| 6373 | *node = (*node)->prev; |
| 6374 | } else { |
| 6375 | /* the processed node with sibings were all unlinked, oh well */ |
| 6376 | *node = NULL; |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | /* temporarily unlink the node */ |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6381 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6382 | if (*unlinked_nodes) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6383 | if (lyd_insert_after((*unlinked_nodes)->prev, elem)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6384 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6385 | return -1; |
| 6386 | } |
| 6387 | } else { |
| 6388 | *unlinked_nodes = elem; |
| 6389 | } |
| 6390 | |
| 6391 | if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 6392 | /* there can be only one instance */ |
| 6393 | break; |
| 6394 | } |
| 6395 | } |
| 6396 | } |
| 6397 | break; |
| 6398 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6399 | LOGINT(ctx); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6400 | return -1; |
| 6401 | } |
| 6402 | |
| 6403 | return EXIT_SUCCESS; |
| 6404 | } |
| 6405 | |
| 6406 | /** |
| 6407 | * @brief Relink the unlinked nodes back. |
| 6408 | * |
| 6409 | * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node, |
| 6410 | * we simply need a sibling from the original data tree. |
| 6411 | * @param[in] unlinked_nodes Unlinked nodes to relink to \p node. |
| 6412 | * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent |
| 6413 | * or the sibling of \p unlinked_nodes. |
| 6414 | * |
| 6415 | * @return EXIT_SUCCESS on success, -1 on error. |
| 6416 | */ |
| 6417 | static int |
| 6418 | resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type) |
| 6419 | { |
| 6420 | struct lyd_node *elem; |
| 6421 | |
| 6422 | LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6423 | lyd_unlink_internal(elem, 0); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6424 | if (ctx_node_type == LYXP_NODE_ELEM) { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6425 | if (lyd_insert_common(node, NULL, elem, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6426 | return -1; |
| 6427 | } |
| 6428 | } else { |
Michal Vasko | 2bce30c | 2017-02-06 12:16:39 +0100 | [diff] [blame] | 6429 | if (lyd_insert_nextto(node, elem, 0, 0)) { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6430 | return -1; |
| 6431 | } |
| 6432 | } |
| 6433 | } |
| 6434 | |
| 6435 | return EXIT_SUCCESS; |
| 6436 | } |
| 6437 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6438 | int |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6439 | resolve_applies_must(const struct lyd_node *node) |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6440 | { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6441 | int ret = 0; |
| 6442 | uint8_t must_size; |
| 6443 | struct lys_node *schema, *iter; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6444 | |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6445 | assert(node); |
| 6446 | |
| 6447 | schema = node->schema; |
| 6448 | |
| 6449 | /* their own must */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6450 | switch (schema->nodetype) { |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6451 | case LYS_CONTAINER: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6452 | must_size = ((struct lys_node_container *)schema)->must_size; |
| 6453 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6454 | case LYS_LEAF: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6455 | must_size = ((struct lys_node_leaf *)schema)->must_size; |
| 6456 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6457 | case LYS_LEAFLIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6458 | must_size = ((struct lys_node_leaflist *)schema)->must_size; |
| 6459 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6460 | case LYS_LIST: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6461 | must_size = ((struct lys_node_list *)schema)->must_size; |
| 6462 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6463 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 6464 | case LYS_ANYDATA: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6465 | must_size = ((struct lys_node_anydata *)schema)->must_size; |
| 6466 | break; |
| 6467 | case LYS_NOTIF: |
| 6468 | must_size = ((struct lys_node_notif *)schema)->must_size; |
| 6469 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6470 | default: |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6471 | must_size = 0; |
| 6472 | break; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6473 | } |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 6474 | |
| 6475 | if (must_size) { |
| 6476 | ++ret; |
| 6477 | } |
| 6478 | |
| 6479 | /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */ |
| 6480 | if (!node->prev->next) { |
| 6481 | for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter)); |
| 6482 | if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 6483 | ret += 0x2; |
| 6484 | } |
| 6485 | } |
| 6486 | |
| 6487 | return ret; |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 6488 | } |
| 6489 | |
| 6490 | int |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6491 | 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] | 6492 | { |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6493 | const struct lys_node *parent; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6494 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6495 | assert(schema); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6496 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6497 | 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] | 6498 | return 1; |
| 6499 | } |
| 6500 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6501 | parent = schema; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6502 | goto check_augment; |
| 6503 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6504 | while (parent) { |
| 6505 | /* stop conditions */ |
| 6506 | if (!mode) { |
| 6507 | /* stop on node that can be instantiated in data tree */ |
| 6508 | if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6509 | break; |
| 6510 | } |
| 6511 | } else { |
| 6512 | /* stop on the specified node */ |
| 6513 | if (parent == stop) { |
| 6514 | break; |
| 6515 | } |
| 6516 | } |
| 6517 | |
| 6518 | if (((const struct lys_node_uses *)parent)->when) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6519 | return 1; |
| 6520 | } |
| 6521 | check_augment: |
| 6522 | |
| 6523 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6524 | (((const struct lys_node_augment *)parent->parent)->when))) { |
Michal Vasko | e365556 | 2016-08-24 15:56:17 +0200 | [diff] [blame] | 6525 | return 1; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6526 | } |
| 6527 | parent = lys_parent(parent); |
| 6528 | } |
| 6529 | |
| 6530 | return 0; |
| 6531 | } |
| 6532 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6533 | /** |
| 6534 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 6535 | * Logs directly. |
| 6536 | * |
| 6537 | * @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] | 6538 | * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied |
| 6539 | * only when requiring external dependencies. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6540 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6541 | * @return |
| 6542 | * -1 - error, ly_errno is set |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6543 | * 0 - all "when" statements true |
| 6544 | * 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] | 6545 | * 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] | 6546 | */ |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 6547 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6548 | 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] | 6549 | { |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6550 | struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node; |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6551 | struct lys_node *sparent; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6552 | struct lyxp_set set; |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6553 | enum lyxp_node_type ctx_node_type; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6554 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6555 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6556 | |
| 6557 | assert(node); |
| 6558 | memset(&set, 0, sizeof set); |
| 6559 | |
Michal Vasko | 78d97e2 | 2017-02-21 09:54:38 +0100 | [diff] [blame] | 6560 | 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] | 6561 | /* make the node dummy for the evaluation */ |
| 6562 | node->validity |= LYD_VAL_INUSE; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6563 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node), |
| 6564 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6565 | node->validity &= ~LYD_VAL_INUSE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6566 | if (rc) { |
| 6567 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6568 | 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] | 6569 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6570 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6571 | } |
| 6572 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6573 | /* set boolean result of the condition */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6574 | 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] | 6575 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6576 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6577 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6578 | || ((((struct lys_node_container *)node->schema)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6579 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6580 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6581 | ((struct lys_node_container *)node->schema)->when->cond); |
| 6582 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6583 | 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] | 6584 | if (failed_when) { |
| 6585 | *failed_when = ((struct lys_node_container *)node->schema)->when; |
| 6586 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6587 | goto cleanup; |
| 6588 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6589 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6590 | |
| 6591 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6592 | 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] | 6593 | } |
| 6594 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6595 | sparent = node->schema; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6596 | goto check_augment; |
| 6597 | |
| 6598 | /* check when in every schema node that affects node */ |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6599 | while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 6600 | if (((struct lys_node_uses *)sparent)->when) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6601 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6602 | rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type); |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 6603 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6604 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6605 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6606 | } |
| 6607 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6608 | |
| 6609 | unlinked_nodes = NULL; |
| 6610 | /* we do not want our node pointer to change */ |
| 6611 | tmp_node = node; |
| 6612 | rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6613 | if (rc) { |
| 6614 | goto cleanup; |
| 6615 | } |
| 6616 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6617 | rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent), |
| 6618 | &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6619 | |
| 6620 | if (unlinked_nodes && ctx_node) { |
| 6621 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6622 | rc = -1; |
| 6623 | goto cleanup; |
| 6624 | } |
| 6625 | } |
| 6626 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6627 | if (rc) { |
| 6628 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6629 | 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] | 6630 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6631 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6632 | } |
| 6633 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6634 | 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] | 6635 | if (!set.val.bool) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6636 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6637 | || ((((struct lys_node_uses *)sparent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6638 | || (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6639 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
| 6640 | ((struct lys_node_uses *)sparent)->when->cond); |
| 6641 | } else { |
Michal Vasko | 2cb18e7 | 2017-03-28 14:46:33 +0200 | [diff] [blame] | 6642 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6643 | 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] | 6644 | if (failed_when) { |
| 6645 | *failed_when = ((struct lys_node_uses *)sparent)->when; |
| 6646 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6647 | goto cleanup; |
| 6648 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6649 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6650 | |
| 6651 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6652 | 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] | 6653 | } |
| 6654 | |
| 6655 | check_augment: |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6656 | 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] | 6657 | if (!ctx_node) { |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6658 | 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] | 6659 | if (rc) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6660 | LOGINT(ctx); |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6661 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6662 | } |
| 6663 | } |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6664 | |
| 6665 | unlinked_nodes = NULL; |
| 6666 | tmp_node = node; |
| 6667 | rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes); |
| 6668 | if (rc) { |
| 6669 | goto cleanup; |
| 6670 | } |
| 6671 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6672 | rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, |
| 6673 | lys_node_module(sparent->parent), &set, LYXP_WHEN); |
Michal Vasko | 76c3bd3 | 2016-08-24 16:02:52 +0200 | [diff] [blame] | 6674 | |
| 6675 | /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together, |
| 6676 | * so the tree did not actually change and there is nothing for us to do |
| 6677 | */ |
| 6678 | if (unlinked_nodes && ctx_node) { |
| 6679 | if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) { |
| 6680 | rc = -1; |
| 6681 | goto cleanup; |
| 6682 | } |
| 6683 | } |
| 6684 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6685 | if (rc) { |
| 6686 | if (rc == 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6687 | 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] | 6688 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6689 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6690 | } |
| 6691 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6692 | 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] | 6693 | if (!set.val.bool) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6694 | node->when_status |= LYD_WHEN_FALSE; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 6695 | if ((ignore_fail == 1) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 6696 | || ((((struct lys_node_augment *)sparent->parent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) |
| 6697 | && (ignore_fail == 2))) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6698 | LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 6699 | ((struct lys_node_augment *)sparent->parent)->when->cond); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6700 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6701 | 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] | 6702 | if (failed_when) { |
| 6703 | *failed_when = ((struct lys_node_augment *)sparent->parent)->when; |
| 6704 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6705 | goto cleanup; |
| 6706 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6707 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6708 | |
| 6709 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6710 | 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] | 6711 | } |
| 6712 | |
Michal Vasko | 90fc2a3 | 2016-08-24 15:58:58 +0200 | [diff] [blame] | 6713 | sparent = lys_parent(sparent); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 6714 | } |
| 6715 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 6716 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 6717 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6718 | cleanup: |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 6719 | /* free xpath set content */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 6720 | 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] | 6721 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6722 | } |
| 6723 | |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6724 | static int |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6725 | check_type_union_leafref(struct lys_type *type) |
| 6726 | { |
| 6727 | uint8_t i; |
| 6728 | |
| 6729 | if ((type->base == LY_TYPE_UNION) && type->info.uni.count) { |
| 6730 | /* go through unions and look for leafref */ |
| 6731 | for (i = 0; i < type->info.uni.count; ++i) { |
| 6732 | switch (type->info.uni.types[i].base) { |
| 6733 | case LY_TYPE_LEAFREF: |
| 6734 | return 1; |
| 6735 | case LY_TYPE_UNION: |
| 6736 | if (check_type_union_leafref(&type->info.uni.types[i])) { |
| 6737 | return 1; |
| 6738 | } |
| 6739 | break; |
| 6740 | default: |
| 6741 | break; |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | return 0; |
| 6746 | } |
| 6747 | |
| 6748 | /* just inherit the flag value */ |
| 6749 | return type->der->has_union_leafref; |
| 6750 | } |
| 6751 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6752 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6753 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 6754 | * |
| 6755 | * @param[in] mod Main module. |
| 6756 | * @param[in] item Item to resolve. Type determined by \p type. |
| 6757 | * @param[in] type Type of the unresolved item. |
| 6758 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 6759 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 769f803 | 2017-01-24 13:11:55 +0100 | [diff] [blame] | 6760 | * @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] | 6761 | * |
| 6762 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 6763 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6764 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 6765 | 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] | 6766 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6767 | { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6768 | /* 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] | 6769 | int rc = -1, has_str = 0, parent_type = 0, i, k; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6770 | unsigned int j; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6771 | struct ly_ctx * ctx = mod->ctx; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6772 | struct lys_node *root, *next, *node, *par_grp; |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6773 | const char *expr; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6774 | uint8_t *u; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6775 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6776 | struct ly_set *refs, *procs; |
| 6777 | struct lys_feature *ref, *feat; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6778 | struct lys_ident *ident; |
| 6779 | struct lys_type *stype; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6780 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6781 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6782 | struct yang_type *yang; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6783 | struct unres_list_uniq *unique_info; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6784 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6785 | struct unres_ext *ext_data; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6786 | struct lys_ext_instance *ext, **extlist; |
| 6787 | struct lyext_plugin *eplugin; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6788 | |
| 6789 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6790 | case UNRES_IDENT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6791 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6792 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6793 | ident = item; |
| 6794 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6795 | rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6796 | break; |
| 6797 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6798 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6799 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6800 | stype = item; |
| 6801 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 6802 | rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6803 | break; |
| 6804 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 6805 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6806 | stype = item; |
| 6807 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 6808 | rc = resolve_schema_leafref(stype, node, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6809 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6810 | case UNRES_TYPE_DER_EXT: |
| 6811 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6812 | /* falls through */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 6813 | case UNRES_TYPE_DER_TPDF: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6814 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6815 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6816 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6817 | /* parent */ |
| 6818 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6819 | stype = item; |
| 6820 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6821 | /* HACK type->der is temporarily unparsed type statement */ |
| 6822 | yin = (struct lyxml_elem *)stype->der; |
| 6823 | stype->der = NULL; |
| 6824 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6825 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 6826 | yang = (struct yang_type *)yin; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6827 | rc = yang_check_type(mod, node, yang, stype, parent_type, unres); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6828 | |
| 6829 | if (rc) { |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 6830 | /* may try again later */ |
| 6831 | stype->der = (struct lys_tpdf *)yang; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6832 | } else { |
| 6833 | /* 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] | 6834 | lydict_remove(ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 6835 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6836 | } |
| 6837 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 6838 | } else { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6839 | rc = fill_yin_type(mod, node, yin, stype, parent_type, unres); |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 6840 | if (!rc || rc == -1) { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6841 | /* 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] | 6842 | lyxml_free(ctx, yin); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 6843 | } else { |
| 6844 | /* may try again later, put all back how it was */ |
| 6845 | stype->der = (struct lys_tpdf *)yin; |
| 6846 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6847 | } |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6848 | if (rc == EXIT_SUCCESS) { |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6849 | /* it does not make sense to have leaf-list of empty type */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 6850 | if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6851 | 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] | 6852 | } |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6853 | |
| 6854 | if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) { |
| 6855 | /* fill typedef union leafref flag */ |
| 6856 | ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype); |
| 6857 | } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) { |
| 6858 | /* copy the type in case it has union leafref flag */ |
| 6859 | if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6860 | LOGERR(ctx, LY_EINT, "Failed to duplicate type."); |
Michal Vasko | e1c7a82 | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 6861 | return -1; |
| 6862 | } |
| 6863 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6864 | } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6865 | /* forward reference - in case the type is in grouping, we have to make the grouping unusable |
| 6866 | * 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] | 6867 | * grouping. The grouping cannot be used unless the unres counter is 0. |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 6868 | * 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] | 6869 | * of the type's base member. */ |
| 6870 | for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
| 6871 | if (par_grp) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6872 | if (++((struct lys_node_grp *)par_grp)->unres_count == 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6873 | LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping."); |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 6874 | return -1; |
| 6875 | } |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 6876 | stype->value_flags |= LY_VALUE_UNRESGRP; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 6877 | } |
Radek Krejci | 2c2fce8 | 2016-08-01 13:52:26 +0200 | [diff] [blame] | 6878 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6879 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6880 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6881 | iff_data = str_snode; |
| 6882 | 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] | 6883 | if (!rc) { |
| 6884 | /* success */ |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6885 | if (iff_data->infeature) { |
| 6886 | /* store backlink into the target feature to allow reverse changes in case of changing feature status */ |
| 6887 | feat = *((struct lys_feature **)item); |
| 6888 | if (!feat->depfeatures) { |
| 6889 | feat->depfeatures = ly_set_new(); |
| 6890 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 6891 | ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 6892 | } |
| 6893 | /* cleanup temporary data */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6894 | lydict_remove(ctx, iff_data->fname); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6895 | free(iff_data); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6896 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6897 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6898 | case UNRES_FEATURE: |
| 6899 | feat = (struct lys_feature *)item; |
| 6900 | |
| 6901 | if (feat->iffeature_size) { |
| 6902 | refs = ly_set_new(); |
| 6903 | procs = ly_set_new(); |
| 6904 | ly_set_add(procs, feat, 0); |
| 6905 | |
| 6906 | while (procs->number) { |
| 6907 | ref = procs->set.g[procs->number - 1]; |
| 6908 | ly_set_rm_index(procs, procs->number - 1); |
| 6909 | |
| 6910 | for (i = 0; i < ref->iffeature_size; i++) { |
| 6911 | resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j); |
| 6912 | for (; j > 0 ; j--) { |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 6913 | if (ref->iffeature[i].features[j - 1]) { |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6914 | if (ref->iffeature[i].features[j - 1] == feat) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6915 | LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name); |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 6916 | goto featurecheckdone; |
| 6917 | } |
| 6918 | |
| 6919 | if (ref->iffeature[i].features[j - 1]->iffeature_size) { |
| 6920 | k = refs->number; |
| 6921 | if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) { |
| 6922 | /* not yet seen feature, add it for processing */ |
| 6923 | ly_set_add(procs, ref->iffeature[i].features[j - 1], 0); |
| 6924 | } |
| 6925 | } |
| 6926 | } else { |
| 6927 | /* forward reference */ |
| 6928 | rc = EXIT_FAILURE; |
| 6929 | goto featurecheckdone; |
| 6930 | } |
| 6931 | } |
| 6932 | |
| 6933 | } |
| 6934 | } |
| 6935 | rc = EXIT_SUCCESS; |
| 6936 | |
| 6937 | featurecheckdone: |
| 6938 | ly_set_free(refs); |
| 6939 | ly_set_free(procs); |
| 6940 | } |
| 6941 | |
| 6942 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6943 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 6944 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6945 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6946 | case UNRES_TYPEDEF_DFLT: |
| 6947 | parent_type++; |
Radek Krejci | 3d679d7 | 2017-08-01 10:44:37 +0200 | [diff] [blame] | 6948 | /* falls through */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6949 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6950 | stype = item; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 6951 | rc = check_default(stype, (const char **)str_snode, mod, parent_type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6952 | break; |
| 6953 | case UNRES_CHOICE_DFLT: |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 6954 | expr = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 6955 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6956 | choic = item; |
| 6957 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6958 | if (!choic->dflt) { |
| 6959 | choic->dflt = resolve_choice_dflt(choic, expr); |
| 6960 | } |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6961 | if (choic->dflt) { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 6962 | rc = lyp_check_mandatory_choice((struct lys_node *)choic); |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 6963 | } else { |
| 6964 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 6965 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6966 | break; |
| 6967 | case UNRES_LIST_KEYS: |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 6968 | rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 6969 | break; |
| 6970 | case UNRES_LIST_UNIQ: |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 6971 | unique_info = (struct unres_list_uniq *)item; |
| 6972 | 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] | 6973 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6974 | case UNRES_AUGMENT: |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 6975 | rc = resolve_augment(item, NULL, unres); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 6976 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6977 | case UNRES_XPATH: |
| 6978 | node = (struct lys_node *)item; |
Michal Vasko | 895c11f | 2018-03-12 11:35:58 +0100 | [diff] [blame] | 6979 | rc = check_xpath(node, 1); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 6980 | break; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 6981 | case UNRES_MOD_IMPLEMENT: |
| 6982 | rc = lys_make_implemented_r(mod, unres); |
| 6983 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6984 | case UNRES_EXT: |
| 6985 | ext_data = (struct unres_ext *)str_snode; |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6986 | extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index]; |
Radek Krejci | a7db970 | 2017-01-20 12:55:14 +0100 | [diff] [blame] | 6987 | rc = resolve_extension(ext_data, extlist, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 6988 | if (!rc) { |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 6989 | /* success */ |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6990 | /* is there a callback to be done to finalize the extension? */ |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6991 | eplugin = extlist[0]->def->plugin; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 6992 | if (eplugin) { |
| 6993 | if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) { |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6994 | u = malloc(sizeof *u); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6995 | LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1); |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 6996 | (*u) = ext_data->ext_index; |
Radek Krejci | b08bc17 | 2017-02-27 13:17:14 +0100 | [diff] [blame] | 6997 | if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) { |
| 6998 | /* something really bad happend since the extension finalization is not actually |
| 6999 | * being resolved while adding into unres, so something more serious with the unres |
| 7000 | * list itself must happened */ |
| 7001 | return -1; |
| 7002 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7003 | } |
| 7004 | } |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7005 | } |
| 7006 | if (!rc || rc == -1) { |
| 7007 | /* cleanup on success or fatal error */ |
| 7008 | if (ext_data->datatype == LYS_IN_YIN) { |
| 7009 | /* YIN */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7010 | lyxml_free(ctx, ext_data->data.yin); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7011 | } else { |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 7012 | /* YANG */ |
| 7013 | yang_free_ext_data(ext_data->data.yang); |
Radek Krejci | 79685c9 | 2017-02-17 10:49:43 +0100 | [diff] [blame] | 7014 | } |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7015 | free(ext_data); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7016 | } |
| 7017 | break; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7018 | case UNRES_EXT_FINALIZE: |
Radek Krejci | 2b999ac | 2017-01-18 16:22:12 +0100 | [diff] [blame] | 7019 | u = (uint8_t *)str_snode; |
| 7020 | ext = (*(struct lys_ext_instance ***)item)[*u]; |
| 7021 | free(u); |
| 7022 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7023 | eplugin = ext->def->plugin; |
| 7024 | |
| 7025 | /* inherit */ |
| 7026 | if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) { |
| 7027 | root = (struct lys_node *)ext->parent; |
| 7028 | if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 7029 | LY_TREE_DFS_BEGIN(root->child, next, node) { |
| 7030 | /* first, check if the node already contain instance of the same extension, |
| 7031 | * in such a case we won't inherit. In case the node was actually defined as |
| 7032 | * augment data, we are supposed to check the same way also the augment node itself */ |
| 7033 | if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) { |
| 7034 | goto inherit_dfs_sibling; |
| 7035 | } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT && |
| 7036 | lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) { |
| 7037 | goto inherit_dfs_sibling; |
| 7038 | } |
| 7039 | |
| 7040 | if (eplugin->check_inherit) { |
| 7041 | /* we have a callback to check the inheritance, use it */ |
| 7042 | switch ((rc = (*eplugin->check_inherit)(ext, node))) { |
| 7043 | case 0: |
| 7044 | /* yes - continue with the inheriting code */ |
| 7045 | break; |
| 7046 | case 1: |
| 7047 | /* no - continue with the node's sibling */ |
| 7048 | goto inherit_dfs_sibling; |
| 7049 | case 2: |
| 7050 | /* no, but continue with the children, just skip the inheriting code for this node */ |
| 7051 | goto inherit_dfs_child; |
| 7052 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7053 | 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] | 7054 | ext->def->module->name, ext->def->name, rc); |
| 7055 | } |
| 7056 | } |
| 7057 | |
| 7058 | /* inherit the extension */ |
| 7059 | extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7060 | LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7061 | extlist[node->ext_size] = malloc(sizeof **extlist); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7062 | 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] | 7063 | memcpy(extlist[node->ext_size], ext, sizeof *ext); |
| 7064 | extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT; |
| 7065 | |
| 7066 | node->ext = extlist; |
| 7067 | node->ext_size++; |
| 7068 | |
| 7069 | inherit_dfs_child: |
| 7070 | /* modification of - select element for the next run - children first */ |
| 7071 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 7072 | next = NULL; |
| 7073 | } else { |
| 7074 | next = node->child; |
| 7075 | } |
| 7076 | if (!next) { |
| 7077 | inherit_dfs_sibling: |
| 7078 | /* no children, try siblings */ |
| 7079 | next = node->next; |
| 7080 | } |
| 7081 | while (!next) { |
| 7082 | /* go to the parent */ |
| 7083 | node = lys_parent(node); |
| 7084 | |
| 7085 | /* we are done if we are back in the root (the starter's parent */ |
| 7086 | if (node == root) { |
| 7087 | break; |
| 7088 | } |
| 7089 | |
| 7090 | /* parent is already processed, go to its sibling */ |
| 7091 | next = node->next; |
| 7092 | } |
| 7093 | } |
| 7094 | } |
| 7095 | } |
| 7096 | |
| 7097 | /* final check */ |
| 7098 | if (eplugin->check_result) { |
| 7099 | if ((*eplugin->check_result)(ext)) { |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7100 | LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed."); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7101 | return -1; |
| 7102 | } |
| 7103 | } |
| 7104 | |
| 7105 | rc = 0; |
| 7106 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7107 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7108 | LOGINT(ctx); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7109 | break; |
| 7110 | } |
| 7111 | |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7112 | if (has_str && !rc) { |
| 7113 | /* the string is no more needed in case of success. |
| 7114 | * 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] | 7115 | lydict_remove(ctx, str_snode); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 7116 | } |
| 7117 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7118 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7119 | } |
| 7120 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7121 | /* logs directly */ |
| 7122 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7123 | 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] | 7124 | { |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7125 | struct lyxml_elem *xml; |
| 7126 | struct lyxml_attr *attr; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7127 | struct unres_iffeat_data *iff_data; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7128 | const char *name = NULL; |
| 7129 | struct unres_ext *extinfo; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7130 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7131 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7132 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7133 | 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] | 7134 | break; |
| 7135 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7136 | 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] | 7137 | break; |
| 7138 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7139 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 7140 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7141 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 7142 | case UNRES_TYPE_DER_EXT: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7143 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7144 | case UNRES_TYPE_DER: |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7145 | xml = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7146 | if (xml->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7147 | name = ((struct yang_type *)xml)->name; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7148 | } else { |
| 7149 | LY_TREE_FOR(xml->attr, attr) { |
| 7150 | if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) { |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7151 | name = attr->value; |
Michal Vasko | cb34dc6 | 2016-05-20 14:38:37 +0200 | [diff] [blame] | 7152 | break; |
| 7153 | } |
| 7154 | } |
| 7155 | assert(attr); |
| 7156 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7157 | 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] | 7158 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7159 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7160 | iff_data = str_node; |
| 7161 | 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] | 7162 | break; |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7163 | case UNRES_FEATURE: |
| 7164 | LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later", |
| 7165 | ((struct lys_feature *)item)->name); |
| 7166 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7167 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7168 | 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] | 7169 | break; |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 7170 | case UNRES_TYPEDEF_DFLT: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7171 | case UNRES_TYPE_DFLT: |
Michal Vasko | ba835cd | 2018-02-23 09:25:48 +0100 | [diff] [blame] | 7172 | if (*(char **)str_node) { |
| 7173 | 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] | 7174 | } /* else no default value in the type itself, but we are checking some restrictions against |
| 7175 | * possible default value of some base type. The failure is caused by not resolved base type, |
| 7176 | * so it was already reported */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7177 | break; |
| 7178 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7179 | 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] | 7180 | break; |
| 7181 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7182 | 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] | 7183 | break; |
| 7184 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7185 | 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] | 7186 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7187 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7188 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 7189 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 7190 | break; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7191 | case UNRES_XPATH: |
Michal Vasko | 0d19837 | 2016-11-16 11:40:03 +0100 | [diff] [blame] | 7192 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of", |
| 7193 | ((struct lys_node *)item)->name); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 7194 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 7195 | case UNRES_EXT: |
| 7196 | extinfo = (struct unres_ext *)str_node; |
| 7197 | name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */ |
| 7198 | LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name); |
| 7199 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7200 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7201 | LOGINT(NULL); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7202 | break; |
| 7203 | } |
| 7204 | } |
| 7205 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7206 | static int |
| 7207 | resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref, |
| 7208 | int print_all_errors, uint32_t *resolved) |
| 7209 | { |
| 7210 | uint32_t i, unres_count, res_count; |
| 7211 | int ret = 0, rc; |
| 7212 | struct ly_err_item *prev_eitem; |
| 7213 | enum int_log_opts prev_ilo; |
| 7214 | LY_ERR prev_ly_errno; |
| 7215 | |
| 7216 | /* if there can be no forward references, every failure is final, so we can print it directly */ |
| 7217 | if (forward_ref) { |
| 7218 | prev_ly_errno = ly_errno; |
| 7219 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 7220 | } |
| 7221 | |
| 7222 | do { |
| 7223 | unres_count = 0; |
| 7224 | res_count = 0; |
| 7225 | |
| 7226 | for (i = 0; i < unres->count; ++i) { |
| 7227 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7228 | * if-features are resolved here to make sure that we will have all if-features for |
| 7229 | * later check of feature circular dependency */ |
| 7230 | if (unres->type[i] & types) { |
| 7231 | ++unres_count; |
| 7232 | rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7233 | if (unres->type[i] == UNRES_EXT_FINALIZE) { |
| 7234 | /* to avoid double free */ |
| 7235 | unres->type[i] = UNRES_RESOLVED; |
| 7236 | } |
| 7237 | if (!rc || (unres->type[i] == UNRES_XPATH)) { |
| 7238 | /* invalid XPath can never cause an error, only a warning */ |
| 7239 | if (unres->type[i] == UNRES_LIST_UNIQ) { |
| 7240 | /* free the allocated structure */ |
| 7241 | free(unres->item[i]); |
| 7242 | } |
| 7243 | |
| 7244 | unres->type[i] = UNRES_RESOLVED; |
| 7245 | ++(*resolved); |
| 7246 | ++res_count; |
| 7247 | } else if ((rc == EXIT_FAILURE) && forward_ref) { |
| 7248 | /* forward reference, erase errors */ |
| 7249 | ly_err_free_next(ctx, prev_eitem); |
| 7250 | } else if (print_all_errors) { |
| 7251 | /* just so that we quit the loop */ |
| 7252 | ++res_count; |
| 7253 | ret = -1; |
| 7254 | } else { |
| 7255 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 7256 | return -1; |
| 7257 | } |
| 7258 | } |
| 7259 | } |
| 7260 | } while (res_count && (res_count < unres_count)); |
| 7261 | |
| 7262 | if (res_count < unres_count) { |
| 7263 | assert(forward_ref); |
| 7264 | /* just print the errors (but we must free the ones we have and get them again :-/ ) */ |
| 7265 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7266 | |
| 7267 | for (i = 0; i < unres->count; ++i) { |
| 7268 | if (unres->type[i] & types) { |
| 7269 | resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres); |
| 7270 | } |
| 7271 | } |
| 7272 | return -1; |
| 7273 | } |
| 7274 | |
| 7275 | if (forward_ref) { |
| 7276 | /* restore log */ |
| 7277 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7278 | ly_errno = prev_ly_errno; |
| 7279 | } |
| 7280 | |
| 7281 | return ret; |
| 7282 | } |
| 7283 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7284 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7285 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7286 | * |
| 7287 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7288 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7289 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7290 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7291 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7292 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7293 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7294 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7295 | uint32_t resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7296 | |
| 7297 | assert(unres); |
| 7298 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7299 | LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 7300 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7301 | /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers); |
| 7302 | * if-features are resolved here to make sure that we will have all if-features for |
| 7303 | * later check of feature circular dependency */ |
| 7304 | if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF |
| 7305 | | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT, |
| 7306 | mod->ctx, 1, 0, &resolved)) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 7307 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7308 | } |
| 7309 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7310 | /* another batch of resolved items */ |
| 7311 | if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT |
| 7312 | | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) { |
| 7313 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7314 | } |
| 7315 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7316 | /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */ |
| 7317 | if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) { |
| 7318 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7319 | } |
| 7320 | |
Michal Vasko | e873426 | 2016-09-29 14:12:06 +0200 | [diff] [blame] | 7321 | LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7322 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7323 | return EXIT_SUCCESS; |
| 7324 | } |
| 7325 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7326 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7327 | * @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] | 7328 | * |
| 7329 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7330 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7331 | * @param[in] item Item to resolve. Type determined by \p type. |
| 7332 | * @param[in] type Type of the unresolved item. |
| 7333 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7334 | * |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7335 | * @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] | 7336 | */ |
| 7337 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7338 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 7339 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7340 | { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7341 | int rc; |
| 7342 | const char *dictstr; |
| 7343 | |
| 7344 | dictstr = lydict_insert(mod->ctx, str, 0); |
| 7345 | rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr); |
| 7346 | |
Radek Krejci | d9c0ce2 | 2017-01-20 15:20:16 +0100 | [diff] [blame] | 7347 | if (rc < 0) { |
Radek Krejci | 54081ce | 2016-08-12 15:21:47 +0200 | [diff] [blame] | 7348 | lydict_remove(mod->ctx, dictstr); |
| 7349 | } |
| 7350 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7351 | } |
| 7352 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7353 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7354 | * @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] | 7355 | * |
| 7356 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7357 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7358 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7359 | * @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] | 7360 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7361 | * |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7362 | * @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] | 7363 | */ |
| 7364 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7365 | 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] | 7366 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7367 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7368 | int rc; |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7369 | uint32_t u; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7370 | enum int_log_opts prev_ilo; |
| 7371 | struct ly_err_item *prev_eitem; |
| 7372 | LY_ERR prev_ly_errno; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7373 | struct lyxml_elem *yin; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7374 | struct ly_ctx *ctx = mod->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7375 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7376 | assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) |
| 7377 | && (type != UNRES_WHEN) && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7378 | |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7379 | /* check for duplicities in unres */ |
| 7380 | for (u = 0; u < unres->count; u++) { |
| 7381 | if (unres->type[u] == type && unres->item[u] == item && |
| 7382 | unres->str_snode[u] == snode && unres->module[u] == mod) { |
Radek Krejci | 62f7aad | 2017-10-26 14:53:52 +0200 | [diff] [blame] | 7383 | /* duplication can happen when the node contains multiple statements of the same type to check, |
| 7384 | * this can happen for example when refinement is being applied, so we just postpone the processing |
| 7385 | * and do not duplicate the information */ |
| 7386 | return EXIT_FAILURE; |
Radek Krejci | 850a5de | 2016-11-08 14:06:40 +0100 | [diff] [blame] | 7387 | } |
| 7388 | } |
| 7389 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 7390 | if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) { |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 7391 | /* 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] | 7392 | * xpath is not tried because it would hide some potential warnings, |
| 7393 | * implementing module must be deferred because some other nodes can be added that will need to be traversed |
| 7394 | * and their targets made implemented */ |
Radek Krejci | c293bac | 2017-02-27 11:25:28 +0100 | [diff] [blame] | 7395 | rc = EXIT_FAILURE; |
| 7396 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7397 | prev_ly_errno = ly_errno; |
| 7398 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7399 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7400 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7401 | if (rc != EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7402 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0); |
| 7403 | if (rc != -1) { |
| 7404 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7405 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7406 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7407 | if (type == UNRES_LIST_UNIQ) { |
| 7408 | /* free the allocated structure */ |
| 7409 | free(item); |
| 7410 | } else if (rc == -1 && type == UNRES_IFFEAT) { |
| 7411 | /* free the allocated resources */ |
| 7412 | free(*((char **)item)); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 7413 | } |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7414 | return rc; |
| 7415 | } else { |
| 7416 | /* erase info about validation errors */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7417 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 7418 | ly_errno = prev_ly_errno; |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7419 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7420 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 7421 | print_unres_schema_item_fail(item, type, snode); |
| 7422 | |
| 7423 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 7424 | if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) { |
| 7425 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
| 7426 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 7427 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 7428 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 7429 | } |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 7430 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7431 | } |
| 7432 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7433 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7434 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7435 | LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7436 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7437 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7438 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7439 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 7440 | 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] | 7441 | LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7442 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7443 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7444 | LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7445 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7446 | |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 7447 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7448 | } |
| 7449 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7450 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7451 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7452 | * |
| 7453 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 7454 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7455 | * @param[in] item Old item to be resolved. |
| 7456 | * @param[in] type Type of the old unresolved item. |
| 7457 | * @param[in] new_item New item to use in the duplicate. |
| 7458 | * |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7459 | * @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] | 7460 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7461 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 7462 | 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] | 7463 | { |
| 7464 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7465 | struct unres_list_uniq aux_uniq; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7466 | struct unres_iffeat_data *iff_data; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7467 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 7468 | 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] | 7469 | |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7470 | /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */ |
| 7471 | if (type == UNRES_LIST_UNIQ) { |
| 7472 | aux_uniq.list = item; |
| 7473 | aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr; |
| 7474 | item = &aux_uniq; |
| 7475 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7476 | i = unres_schema_find(unres, -1, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7477 | |
| 7478 | if (i == -1) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7479 | if (type == UNRES_LIST_UNIQ) { |
| 7480 | free(new_item); |
| 7481 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 7482 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7483 | } |
| 7484 | |
Radek Krejci | c79c6b1 | 2016-07-26 15:11:49 +0200 | [diff] [blame] | 7485 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) || |
Radek Krejci | b69f323 | 2016-09-16 17:41:07 +0200 | [diff] [blame] | 7486 | (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7487 | 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] | 7488 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7489 | return -1; |
| 7490 | } |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7491 | } else if (type == UNRES_IFFEAT) { |
| 7492 | /* duplicate unres_iffeature_data */ |
| 7493 | iff_data = malloc(sizeof *iff_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7494 | LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7495 | iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0); |
| 7496 | iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node; |
| 7497 | 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] | 7498 | LOGINT(mod->ctx); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7499 | return -1; |
| 7500 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7501 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 7502 | 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] | 7503 | LOGINT(mod->ctx); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 7504 | return -1; |
| 7505 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7506 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 7507 | |
| 7508 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7509 | } |
| 7510 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 7511 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7512 | int |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7513 | 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] | 7514 | { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7515 | int i; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7516 | struct unres_list_uniq *aux_uniq1, *aux_uniq2; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7517 | |
Radek Krejci | ddddd0d | 2017-01-20 15:20:46 +0100 | [diff] [blame] | 7518 | if (start_on_backwards >= 0) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7519 | i = start_on_backwards; |
| 7520 | } else { |
| 7521 | i = unres->count - 1; |
| 7522 | } |
| 7523 | for (; i > -1; i--) { |
| 7524 | if (unres->type[i] != type) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7525 | continue; |
| 7526 | } |
| 7527 | if (type != UNRES_LIST_UNIQ) { |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7528 | if (unres->item[i] == item) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7529 | break; |
| 7530 | } |
| 7531 | } else { |
| 7532 | aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1]; |
| 7533 | aux_uniq2 = (struct unres_list_uniq *)item; |
| 7534 | 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] | 7535 | break; |
| 7536 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7537 | } |
| 7538 | } |
| 7539 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 7540 | return i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 7541 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 7542 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7543 | static void |
| 7544 | unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i) |
| 7545 | { |
| 7546 | struct lyxml_elem *yin; |
| 7547 | struct yang_type *yang; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7548 | struct unres_iffeat_data *iff_data; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7549 | |
| 7550 | switch (unres->type[i]) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 7551 | case UNRES_TYPE_DER_TPDF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7552 | case UNRES_TYPE_DER: |
| 7553 | yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der; |
| 7554 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 7555 | yang =(struct yang_type *)yin; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7556 | ((struct lys_type *)unres->item[i])->base = yang->base; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7557 | lydict_remove(ctx, yang->name); |
| 7558 | free(yang); |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 7559 | if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) { |
| 7560 | yang_free_type_union(ctx, (struct lys_type *)unres->item[i]); |
| 7561 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7562 | } else { |
| 7563 | lyxml_free(ctx, yin); |
| 7564 | } |
| 7565 | break; |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7566 | case UNRES_IFFEAT: |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 7567 | iff_data = (struct unres_iffeat_data *)unres->str_snode[i]; |
| 7568 | lydict_remove(ctx, iff_data->fname); |
| 7569 | free(unres->str_snode[i]); |
Pavol Vican | 88e16c9 | 2016-09-07 15:41:50 +0200 | [diff] [blame] | 7570 | break; |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7571 | case UNRES_IDENT: |
| 7572 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7573 | case UNRES_CHOICE_DFLT: |
| 7574 | case UNRES_LIST_KEYS: |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7575 | lydict_remove(ctx, (const char *)unres->str_snode[i]); |
| 7576 | break; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 7577 | case UNRES_LIST_UNIQ: |
| 7578 | free(unres->item[i]); |
| 7579 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 7580 | case UNRES_EXT: |
| 7581 | free(unres->str_snode[i]); |
| 7582 | break; |
PavolVican | fcc9876 | 2017-09-01 15:51:39 +0200 | [diff] [blame] | 7583 | case UNRES_EXT_FINALIZE: |
| 7584 | free(unres->str_snode[i]); |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7585 | default: |
| 7586 | break; |
| 7587 | } |
| 7588 | unres->type[i] = UNRES_RESOLVED; |
| 7589 | } |
| 7590 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7591 | void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7592 | 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] | 7593 | { |
| 7594 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7595 | unsigned int unresolved = 0; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7596 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7597 | if (!unres || !(*unres)) { |
| 7598 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7599 | } |
| 7600 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7601 | assert(module || ((*unres)->count == 0)); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7602 | |
| 7603 | for (i = 0; i < (*unres)->count; ++i) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7604 | if (!all && ((*unres)->module[i] != module)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7605 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 7606 | unresolved++; |
| 7607 | } |
| 7608 | continue; |
| 7609 | } |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7610 | |
| 7611 | /* free heap memory for the specific item */ |
| 7612 | unres_schema_free_item(module->ctx, *unres, i); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7613 | } |
| 7614 | |
Michal Vasko | ede9c47 | 2016-06-07 09:38:15 +0200 | [diff] [blame] | 7615 | /* free it all */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 7616 | if (!module || all || (!unresolved && !module->type)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7617 | free((*unres)->item); |
| 7618 | free((*unres)->type); |
| 7619 | free((*unres)->str_snode); |
| 7620 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 7621 | free((*unres)); |
| 7622 | (*unres) = NULL; |
| 7623 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 7624 | } |
| 7625 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7626 | /* check whether instance-identifier points outside its data subtree (for operation it is any node |
| 7627 | * 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] | 7628 | static int |
| 7629 | check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid) |
| 7630 | { |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7631 | const struct lys_node *op_node, *first_node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7632 | enum int_log_opts prev_ilo; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7633 | char *buf; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7634 | int ret = 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7635 | |
Radek Krejci | 034cb10 | 2017-08-01 15:45:13 +0200 | [diff] [blame] | 7636 | if (!json_instid || !json_instid[0]) { |
| 7637 | /* no/empty value */ |
| 7638 | return 0; |
| 7639 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7640 | |
| 7641 | for (op_node = lys_parent(sleaf); |
| 7642 | op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)); |
| 7643 | op_node = lys_parent(op_node)); |
| 7644 | |
| 7645 | if (op_node && lys_parent(op_node)) { |
| 7646 | /* nested operation - any absolute path is external */ |
| 7647 | return 1; |
| 7648 | } |
| 7649 | |
| 7650 | /* get the first node from the instid */ |
| 7651 | buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid); |
| 7652 | if (!buf) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7653 | /* so that we do not have to bother with logging, say it is not external */ |
| 7654 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7655 | } |
| 7656 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7657 | /* find the first schema node, do not log */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7658 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7659 | first_node = ly_ctx_get_node(NULL, sleaf, buf, 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7660 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7661 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7662 | free(buf); |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7663 | if (!first_node) { |
| 7664 | /* unknown path, say it is not external */ |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7665 | return 0; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7666 | } |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7667 | |
| 7668 | /* based on the first schema node in the path we can decide whether it points to an external tree or not */ |
| 7669 | |
Michal Vasko | ff690e7 | 2017-08-03 14:25:07 +0200 | [diff] [blame] | 7670 | if (op_node && (op_node != first_node)) { |
| 7671 | /* it is a top-level operation, so we're good if it points somewhere inside it */ |
| 7672 | ret = 1; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7673 | } |
| 7674 | |
| 7675 | /* we cannot know whether it points to a tree that is going to be unlinked (application must handle |
| 7676 | * this itself), so we say it's not external */ |
Radek Krejci | 81c38b8 | 2017-06-02 15:04:16 +0200 | [diff] [blame] | 7677 | return ret; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7678 | } |
| 7679 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7680 | /** |
| 7681 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
| 7682 | * |
| 7683 | * @param[in] data Data node where the path is used |
| 7684 | * @param[in] path Instance-identifier node value. |
| 7685 | * @param[in,out] ret Resolved instance or NULL. |
| 7686 | * |
| 7687 | * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error. |
| 7688 | */ |
| 7689 | static int |
| 7690 | resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret) |
| 7691 | { |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7692 | int i = 0, j, parsed, cur_idx; |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7693 | const struct lys_module *mod, *prev_mod = NULL; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7694 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7695 | struct lyd_node *root, *node; |
Radek Krejci | daa547a | 2017-09-22 15:56:27 +0200 | [diff] [blame] | 7696 | const char *model = NULL, *name; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7697 | char *str; |
| 7698 | int mod_len, name_len, has_predicate; |
| 7699 | struct unres_data node_match; |
| 7700 | |
| 7701 | memset(&node_match, 0, sizeof node_match); |
| 7702 | *ret = NULL; |
| 7703 | |
| 7704 | /* we need root to resolve absolute path */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7705 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7706 | /* we're still parsing it and the pointer is not correct yet */ |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7707 | if (root->prev) { |
| 7708 | for (; root->prev->next; root = root->prev); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7709 | } |
| 7710 | |
| 7711 | /* search for the instance node */ |
| 7712 | while (path[i]) { |
| 7713 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
| 7714 | if (j <= 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7715 | 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] | 7716 | goto error; |
| 7717 | } |
| 7718 | i += j; |
| 7719 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7720 | if (model) { |
| 7721 | str = strndup(model, mod_len); |
| 7722 | if (!str) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7723 | LOGMEM(ctx); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7724 | goto error; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7725 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 7726 | mod = ly_ctx_get_module(ctx, str, NULL, 1); |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7727 | if (ctx->data_clb) { |
| 7728 | if (!mod) { |
| 7729 | mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data); |
| 7730 | } else if (!mod->implemented) { |
| 7731 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 7732 | } |
| 7733 | } |
| 7734 | free(str); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7735 | |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7736 | if (!mod || !mod->implemented || mod->disabled) { |
| 7737 | break; |
| 7738 | } |
| 7739 | } else if (!prev_mod) { |
| 7740 | /* first iteration and we are missing module name */ |
Michal Vasko | af8ec36 | 2018-03-28 09:08:09 +0200 | [diff] [blame] | 7741 | LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name); |
| 7742 | 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] | 7743 | goto error; |
| 7744 | } else { |
| 7745 | mod = prev_mod; |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 7746 | } |
| 7747 | |
Radek Krejci | 2c822ed | 2017-08-03 14:23:36 +0200 | [diff] [blame] | 7748 | if (resolve_data(mod, name, name_len, root, &node_match)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7749 | /* no instance exists */ |
| 7750 | break; |
| 7751 | } |
| 7752 | |
| 7753 | if (has_predicate) { |
| 7754 | /* 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] | 7755 | parsed = j = 0; |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7756 | /* index of the current node (for lists with position predicates) */ |
| 7757 | cur_idx = 1; |
| 7758 | while (j < (signed)node_match.count) { |
| 7759 | node = node_match.node[j]; |
| 7760 | parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx); |
| 7761 | if (parsed < 1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7762 | LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]); |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7763 | goto error; |
| 7764 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7765 | |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7766 | if (!node) { |
| 7767 | /* current node does not satisfy the predicate */ |
| 7768 | unres_data_del(&node_match, j); |
| 7769 | } else { |
| 7770 | ++j; |
| 7771 | } |
| 7772 | ++cur_idx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7773 | } |
Michal Vasko | a3ca4b9 | 2017-09-15 12:43:01 +0200 | [diff] [blame] | 7774 | |
| 7775 | i += parsed; |
Michal Vasko | 6f28e0f | 2017-04-18 15:14:13 +0200 | [diff] [blame] | 7776 | } else if (node_match.count) { |
| 7777 | /* check that we are not addressing lists */ |
| 7778 | for (j = 0; (unsigned)j < node_match.count; ++j) { |
| 7779 | if (node_match.node[j]->schema->nodetype == LYS_LIST) { |
| 7780 | unres_data_del(&node_match, j--); |
| 7781 | } |
| 7782 | } |
| 7783 | if (!node_match.count) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7784 | 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] | 7785 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7786 | } |
Michal Vasko | 1b6ca96 | 2017-08-03 14:23:09 +0200 | [diff] [blame] | 7787 | |
| 7788 | prev_mod = mod; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | if (!node_match.count) { |
| 7792 | /* no instance exists */ |
| 7793 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7794 | LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7795 | return EXIT_FAILURE; |
| 7796 | } |
| 7797 | LOGVRB("There is no instance of \"%s\", but it is not required.", path); |
| 7798 | return EXIT_SUCCESS; |
| 7799 | } else if (node_match.count > 1) { |
| 7800 | /* instance identifier must resolve to a single node */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7801 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7802 | goto error; |
| 7803 | } else { |
| 7804 | /* we have required result, remember it and cleanup */ |
| 7805 | *ret = node_match.node[0]; |
| 7806 | free(node_match.node); |
| 7807 | return EXIT_SUCCESS; |
| 7808 | } |
| 7809 | |
| 7810 | error: |
| 7811 | /* cleanup */ |
| 7812 | free(node_match.node); |
| 7813 | return -1; |
| 7814 | } |
| 7815 | |
| 7816 | static int |
| 7817 | 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] | 7818 | { |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7819 | struct lyxp_set xp_set; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7820 | uint32_t i; |
| 7821 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7822 | memset(&xp_set, 0, sizeof xp_set); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7823 | *ret = NULL; |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7824 | |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7825 | /* syntax was already checked, so just evaluate the path using standard XPath */ |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7826 | if (lyxp_eval(path, (struct lyd_node *)leaf, LYXP_NODE_ELEM, lyd_node_module((struct lyd_node *)leaf), &xp_set, 0) != EXIT_SUCCESS) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7827 | return -1; |
| 7828 | } |
| 7829 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7830 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 7831 | for (i = 0; i < xp_set.used; ++i) { |
| 7832 | if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7833 | continue; |
| 7834 | } |
Michal Vasko | ca16cb3 | 2017-07-10 11:50:33 +0200 | [diff] [blame] | 7835 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7836 | /* not that the value is already in canonical form since the parsers does the conversion, |
| 7837 | * so we can simply compare just the values */ |
| 7838 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) { |
| 7839 | /* we have the match */ |
| 7840 | *ret = xp_set.val.nodes[i].node; |
| 7841 | break; |
| 7842 | } |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7843 | } |
| 7844 | } |
| 7845 | |
Michal Vasko | b5f9ffb | 2018-08-06 09:01:27 +0200 | [diff] [blame] | 7846 | lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, (struct lyd_node *)leaf, NULL, 0); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7847 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7848 | if (!*ret) { |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 7849 | /* reference not found */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7850 | if (req_inst > -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7851 | 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] | 7852 | return EXIT_FAILURE; |
| 7853 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7854 | 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] | 7855 | } |
| 7856 | } |
| 7857 | |
| 7858 | return EXIT_SUCCESS; |
| 7859 | } |
| 7860 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7861 | /* 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] | 7862 | int |
| 7863 | resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail, |
| 7864 | struct lys_type **resolved_type) |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7865 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7866 | struct ly_ctx *ctx = leaf->schema->module->ctx; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7867 | struct lys_type *t; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7868 | struct lyd_node *ret; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7869 | enum int_log_opts prev_ilo; |
| 7870 | int found, success = 0, ext_dep, req_inst; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7871 | const char *json_val = NULL; |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 7872 | |
| 7873 | assert(type->base == LY_TYPE_UNION); |
| 7874 | |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7875 | 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] | 7876 | /* either NULL or instid previously converted to JSON */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 7877 | json_val = lydict_insert(ctx, leaf->value.string, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7878 | } |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7879 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7880 | if (store) { |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7881 | 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] | 7882 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 7883 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7884 | |
| 7885 | /* 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] | 7886 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7887 | |
| 7888 | t = NULL; |
| 7889 | found = 0; |
| 7890 | while ((t = lyp_get_next_union_type(type, t, &found))) { |
| 7891 | found = 0; |
| 7892 | |
| 7893 | switch (t->base) { |
| 7894 | case LY_TYPE_LEAFREF: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7895 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 7896 | req_inst = -1; |
| 7897 | } else { |
| 7898 | req_inst = t->info.lref.req; |
| 7899 | } |
| 7900 | |
| 7901 | if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7902 | if (store) { |
| 7903 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
| 7904 | /* valid resolved */ |
| 7905 | leaf->value.leafref = ret; |
| 7906 | leaf->value_type = LY_TYPE_LEAFREF; |
| 7907 | } else { |
| 7908 | /* valid unresolved */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7909 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7910 | 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] | 7911 | return -1; |
| 7912 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7913 | ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7914 | } |
| 7915 | } |
| 7916 | |
| 7917 | success = 1; |
| 7918 | } |
| 7919 | break; |
| 7920 | case LY_TYPE_INST: |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7921 | ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str)); |
| 7922 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 7923 | req_inst = -1; |
| 7924 | } else { |
| 7925 | req_inst = t->info.inst.req; |
| 7926 | } |
| 7927 | |
Michal Vasko | d3a0311 | 2017-01-23 09:56:02 +0100 | [diff] [blame] | 7928 | 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] | 7929 | if (store) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 7930 | if (ret && !ext_dep) { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7931 | /* valid resolved */ |
| 7932 | leaf->value.instance = ret; |
| 7933 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7934 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7935 | if (json_val) { |
| 7936 | lydict_remove(leaf->schema->module->ctx, leaf->value_str); |
| 7937 | leaf->value_str = json_val; |
| 7938 | json_val = NULL; |
| 7939 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7940 | } else { |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7941 | /* valid unresolved */ |
| 7942 | if (json_val) { |
| 7943 | /* put the JSON val back */ |
| 7944 | leaf->value.string = json_val; |
| 7945 | json_val = NULL; |
| 7946 | } else { |
| 7947 | leaf->value.instance = NULL; |
| 7948 | } |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7949 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 7950 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7951 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7952 | } |
| 7953 | |
| 7954 | success = 1; |
| 7955 | } |
| 7956 | break; |
| 7957 | default: |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 7958 | 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] | 7959 | success = 1; |
| 7960 | } |
| 7961 | break; |
| 7962 | } |
| 7963 | |
| 7964 | if (success) { |
| 7965 | break; |
| 7966 | } |
| 7967 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7968 | /* erase possible present and invalid value data */ |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7969 | if (store) { |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 7970 | lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7971 | memset(&leaf->value, 0, sizeof leaf->value); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7972 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | /* turn logging back on */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7976 | ly_ilo_restore(NULL, prev_ilo, NULL, 0); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7977 | |
| 7978 | if (json_val) { |
| 7979 | if (!success) { |
| 7980 | /* put the value back for now */ |
| 7981 | assert(leaf->value_type == LY_TYPE_UNION); |
| 7982 | leaf->value.string = json_val; |
| 7983 | } else { |
| 7984 | /* value was ultimately useless, but we could not have known */ |
| 7985 | lydict_remove(leaf->schema->module->ctx, json_val); |
| 7986 | } |
| 7987 | } |
| 7988 | |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 7989 | if (success) { |
| 7990 | if (resolved_type) { |
| 7991 | *resolved_type = t; |
| 7992 | } |
| 7993 | } else if (!ignore_fail || !type->info.uni.has_ptr_type) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 7994 | /* not found and it is required */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 7995 | 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] | 7996 | return EXIT_FAILURE; |
| 7997 | } |
| 7998 | |
| 7999 | return EXIT_SUCCESS; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8000 | |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 8001 | } |
| 8002 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8003 | /** |
| 8004 | * @brief Resolve a single unres data item. Logs directly. |
| 8005 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8006 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8007 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8008 | * @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] | 8009 | * |
| 8010 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 8011 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 8012 | int |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8013 | 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] | 8014 | { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8015 | int rc, req_inst, ext_dep; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8016 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8017 | struct lyd_node *ret; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8018 | struct lys_node_leaf *sleaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8019 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 8020 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8021 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8022 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8023 | switch (type) { |
| 8024 | case UNRES_LEAFREF: |
| 8025 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8026 | assert(leaf->validity & LYD_VAL_LEAFREF); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8027 | if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) { |
| 8028 | req_inst = -1; |
| 8029 | } else { |
| 8030 | req_inst = sleaf->type.info.lref.req; |
| 8031 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8032 | rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret); |
| 8033 | if (!rc) { |
Michal Vasko | b1ac872 | 2017-01-02 13:04:25 +0100 | [diff] [blame] | 8034 | if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8035 | /* valid resolved */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8036 | if (leaf->value_type == LY_TYPE_BITS) { |
Michal Vasko | 1c8567a | 2017-01-05 13:42:27 +0100 | [diff] [blame] | 8037 | free(leaf->value.bit); |
| 8038 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8039 | leaf->value.leafref = ret; |
| 8040 | leaf->value_type = LY_TYPE_LEAFREF; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8041 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8042 | } else { |
| 8043 | /* valid unresolved */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8044 | if (!(leaf->value_flags & LY_VALUE_UNRES)) { |
Michal Vasko | 35f46a8 | 2018-05-30 10:44:11 +0200 | [diff] [blame] | 8045 | 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] | 8046 | return -1; |
| 8047 | } |
| 8048 | } |
| 8049 | } |
| 8050 | leaf->validity &= ~LYD_VAL_LEAFREF; |
| 8051 | } else { |
| 8052 | return rc; |
| 8053 | } |
| 8054 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8055 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8056 | case UNRES_INSTID: |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8057 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8058 | ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str); |
| 8059 | if (ext_dep == -1) { |
| 8060 | return -1; |
| 8061 | } |
| 8062 | |
| 8063 | if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) { |
| 8064 | req_inst = -1; |
| 8065 | } else { |
| 8066 | req_inst = sleaf->type.info.inst.req; |
| 8067 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8068 | rc = resolve_instid(node, leaf->value_str, req_inst, &ret); |
| 8069 | if (!rc) { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8070 | if (ret && !ext_dep) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8071 | /* valid resolved */ |
| 8072 | leaf->value.instance = ret; |
| 8073 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8074 | leaf->value_flags &= ~LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8075 | } else { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8076 | /* valid unresolved */ |
| 8077 | leaf->value.instance = NULL; |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 8078 | leaf->value_type = LY_TYPE_INST; |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 8079 | leaf->value_flags |= LY_VALUE_UNRES; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8080 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8081 | } else { |
| 8082 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8083 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8084 | break; |
| 8085 | |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8086 | case UNRES_UNION: |
| 8087 | assert(sleaf->type.base == LY_TYPE_UNION); |
Michal Vasko | fd6c650 | 2017-01-06 12:15:41 +0100 | [diff] [blame] | 8088 | return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL); |
Radek Krejci | 7de36cf | 2016-09-12 16:18:50 +0200 | [diff] [blame] | 8089 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8090 | case UNRES_WHEN: |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8091 | if ((rc = resolve_when(node, ignore_fail, failed_when))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8092 | return rc; |
| 8093 | } |
| 8094 | break; |
| 8095 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8096 | case UNRES_MUST: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8097 | if ((rc = resolve_must(node, 0, ignore_fail))) { |
Michal Vasko | c8c810c | 2016-09-15 14:02:00 +0200 | [diff] [blame] | 8098 | return rc; |
| 8099 | } |
| 8100 | break; |
| 8101 | |
| 8102 | case UNRES_MUST_INOUT: |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8103 | if ((rc = resolve_must(node, 1, ignore_fail))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 8104 | return rc; |
| 8105 | } |
| 8106 | break; |
| 8107 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8108 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8109 | LOGINT(NULL); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8110 | return -1; |
| 8111 | } |
| 8112 | |
| 8113 | return EXIT_SUCCESS; |
| 8114 | } |
| 8115 | |
| 8116 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8117 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8118 | * |
| 8119 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8120 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8121 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8122 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8123 | */ |
| 8124 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8125 | 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] | 8126 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8127 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 8128 | 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] | 8129 | || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8130 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8131 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8132 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8133 | LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8134 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 8135 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8136 | LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 8137 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 8138 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8139 | if (type == UNRES_WHEN) { |
| 8140 | /* remove previous result */ |
| 8141 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8142 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8143 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8144 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8145 | } |
| 8146 | |
| 8147 | /** |
| 8148 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 8149 | * |
Michal Vasko | 660582a | 2018-03-19 10:10:08 +0100 | [diff] [blame] | 8150 | * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected, |
| 8151 | * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit |
| 8152 | * non-presence containers may need to be deleted). |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8153 | * |
| 8154 | * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised. |
| 8155 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8156 | * @param[in] ctx Context used. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8157 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8158 | * @param[in,out] root Root node of the data tree, can be changed due to autodeletion. |
| 8159 | * @param[in] options Data options as described above. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8160 | * |
| 8161 | * @return EXIT_SUCCESS on success, -1 on error. |
| 8162 | */ |
| 8163 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8164 | 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] | 8165 | { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8166 | uint32_t i, j, first, resolved, del_items, stmt_count; |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8167 | int rc, progress, ignore_fail; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8168 | enum int_log_opts prev_ilo; |
| 8169 | struct ly_err_item *prev_eitem; |
mohitarora24 | 89837dc | 2018-05-01 15:09:36 +0530 | [diff] [blame] | 8170 | LY_ERR prev_ly_errno = ly_errno; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8171 | struct lyd_node *parent; |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8172 | struct lys_when *when; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8173 | |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8174 | assert(root); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8175 | assert(unres); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8176 | |
| 8177 | if (!unres->count) { |
| 8178 | return EXIT_SUCCESS; |
| 8179 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8180 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8181 | 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] | 8182 | ignore_fail = 1; |
| 8183 | } else if (options & LYD_OPT_NOEXTDEPS) { |
| 8184 | ignore_fail = 2; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8185 | } else { |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 8186 | ignore_fail = 0; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8187 | } |
| 8188 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8189 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8190 | if (!ignore_fail) { |
| 8191 | /* remember logging state only if errors are generated and valid */ |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8192 | ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem); |
| 8193 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8194 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8195 | /* |
| 8196 | * when-stmt first |
| 8197 | */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8198 | first = 1; |
| 8199 | stmt_count = 0; |
| 8200 | resolved = 0; |
| 8201 | del_items = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8202 | do { |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8203 | if (!ignore_fail) { |
| 8204 | ly_err_free_next(ctx, prev_eitem); |
| 8205 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8206 | progress = 0; |
Michal Vasko | 6df9413 | 2016-09-22 11:08:09 +0200 | [diff] [blame] | 8207 | for (i = 0; i < unres->count; i++) { |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8208 | if (unres->type[i] != UNRES_WHEN) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8209 | continue; |
| 8210 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8211 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8212 | /* count when-stmt nodes in unres list */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8213 | stmt_count++; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8214 | } |
| 8215 | |
| 8216 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 8217 | for (parent = unres->node[i]->parent; |
| 8218 | parent && LYD_WHEN_DONE(parent->when_status); |
| 8219 | parent = parent->parent) { |
| 8220 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 8221 | /* the parent node was already unlinked, do not resolve this node, |
Michal Vasko | e446b09 | 2017-08-11 10:58:09 +0200 | [diff] [blame] | 8222 | * it will be removed anyway, so just mark it as resolved |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8223 | */ |
| 8224 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 8225 | unres->type[i] = UNRES_RESOLVED; |
| 8226 | resolved++; |
| 8227 | break; |
| 8228 | } |
| 8229 | } |
| 8230 | if (parent) { |
| 8231 | continue; |
| 8232 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8233 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8234 | 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] | 8235 | if (!rc) { |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8236 | /* finish with error/delete the node only if when was false, an external dependency was not required, |
| 8237 | * 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] | 8238 | if ((unres->node[i]->when_status & LYD_WHEN_FALSE) |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 8239 | && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) { |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8240 | if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8241 | /* false when condition */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8242 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8243 | } /* follows else */ |
| 8244 | |
Michal Vasko | e31d34a | 2017-03-28 14:50:38 +0200 | [diff] [blame] | 8245 | /* auto-delete */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8246 | 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] | 8247 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8248 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 8249 | /* if it has parent non-presence containers that would be empty, we should actually |
| 8250 | * remove the container |
| 8251 | */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8252 | for (parent = unres->node[i]; |
| 8253 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 8254 | parent = parent->parent) { |
| 8255 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 8256 | /* presence container */ |
| 8257 | break; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8258 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8259 | if (parent->next || parent->prev != parent) { |
| 8260 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 8261 | break; |
| 8262 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8263 | } |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 8264 | unres->node[i] = parent; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8265 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8266 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8267 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8268 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8269 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8270 | lyd_unlink(unres->node[i]); |
| 8271 | unres->type[i] = UNRES_DELETE; |
| 8272 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8273 | |
| 8274 | /* update the rest of unres items */ |
| 8275 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 8276 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 8277 | continue; |
| 8278 | } |
| 8279 | |
| 8280 | /* test if the node is in subtree to be deleted */ |
| 8281 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 8282 | if (parent == unres->node[i]) { |
| 8283 | /* yes, it is */ |
| 8284 | unres->type[j] = UNRES_RESOLVED; |
| 8285 | resolved++; |
| 8286 | break; |
| 8287 | } |
| 8288 | } |
| 8289 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8290 | } else { |
| 8291 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 8292 | } |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8293 | if (!ignore_fail) { |
| 8294 | ly_err_free_next(ctx, prev_eitem); |
| 8295 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8296 | resolved++; |
| 8297 | progress = 1; |
| 8298 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8299 | goto error; |
Radek Krejci | 2467a49 | 2016-10-24 15:16:59 +0200 | [diff] [blame] | 8300 | } /* else forward reference */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8301 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8302 | first = 0; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8303 | } while (progress && resolved < stmt_count); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8304 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8305 | /* do we have some unresolved when-stmt? */ |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8306 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8307 | goto error; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8308 | } |
| 8309 | |
| 8310 | for (i = 0; del_items && i < unres->count; i++) { |
| 8311 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 8312 | if (unres->type[i] != UNRES_DELETE) { |
| 8313 | continue; |
| 8314 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 8315 | if (!unres->node[i]) { |
| 8316 | unres->type[i] = UNRES_RESOLVED; |
| 8317 | del_items--; |
| 8318 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 8319 | } |
| 8320 | |
| 8321 | /* really remove the complete subtree */ |
| 8322 | lyd_free(unres->node[i]); |
| 8323 | unres->type[i] = UNRES_RESOLVED; |
| 8324 | del_items--; |
| 8325 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8326 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8327 | /* |
| 8328 | * now leafrefs |
| 8329 | */ |
| 8330 | if (options & LYD_OPT_TRUSTED) { |
| 8331 | /* we want to attempt to resolve leafrefs */ |
| 8332 | assert(!ignore_fail); |
| 8333 | ignore_fail = 1; |
| 8334 | |
| 8335 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8336 | ly_errno = prev_ly_errno; |
| 8337 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8338 | first = 1; |
| 8339 | stmt_count = 0; |
| 8340 | resolved = 0; |
| 8341 | do { |
| 8342 | progress = 0; |
| 8343 | for (i = 0; i < unres->count; i++) { |
| 8344 | if (unres->type[i] != UNRES_LEAFREF) { |
| 8345 | continue; |
| 8346 | } |
| 8347 | if (first) { |
| 8348 | /* count leafref nodes in unres list */ |
| 8349 | stmt_count++; |
| 8350 | } |
| 8351 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8352 | 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] | 8353 | if (!rc) { |
| 8354 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8355 | if (!ignore_fail) { |
| 8356 | ly_err_free_next(ctx, prev_eitem); |
| 8357 | } |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8358 | resolved++; |
| 8359 | progress = 1; |
| 8360 | } else if (rc == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8361 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8362 | } /* else forward reference */ |
| 8363 | } |
| 8364 | first = 0; |
| 8365 | } while (progress && resolved < stmt_count); |
| 8366 | |
| 8367 | /* do we have some unresolved leafrefs? */ |
| 8368 | if (stmt_count > resolved) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8369 | goto error; |
Michal Vasko | c35d2a7 | 2017-05-09 14:21:30 +0200 | [diff] [blame] | 8370 | } |
| 8371 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8372 | if (!ignore_fail) { |
| 8373 | /* log normally now, throw away irrelevant errors */ |
| 8374 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0); |
| 8375 | ly_errno = prev_ly_errno; |
| 8376 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8377 | |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8378 | /* |
| 8379 | * rest |
| 8380 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8381 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8382 | if (unres->type[i] == UNRES_RESOLVED) { |
| 8383 | continue; |
| 8384 | } |
Radek Krejci | 082c84f | 2016-10-17 16:33:06 +0200 | [diff] [blame] | 8385 | 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] | 8386 | |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 8387 | 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] | 8388 | if (rc) { |
| 8389 | /* since when was already resolved, a forward reference is an error */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8390 | return -1; |
| 8391 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 8392 | |
| 8393 | unres->type[i] = UNRES_RESOLVED; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8394 | } |
| 8395 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 8396 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 8397 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8398 | return EXIT_SUCCESS; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8399 | |
| 8400 | error: |
Michal Vasko | 7fa9314 | 2018-03-19 09:59:10 +0100 | [diff] [blame] | 8401 | if (!ignore_fail) { |
| 8402 | /* print all the new errors */ |
| 8403 | ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1); |
| 8404 | /* do not restore ly_errno, it was udpated properly */ |
| 8405 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 8406 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 8407 | } |