blob: cc6edb9444caf94abbf6618a143c2acb52a00b16 [file] [log] [blame]
Michal Vasko730dfdf2015-08-11 14:48:05 +02001/**
2 * @file resolve.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang resolve functions
5 *
Michal Vasko53b7da02018-02-13 15:28:42 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Michal Vasko730dfdf2015-08-11 14:48:05 +02007 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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 Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko730dfdf2015-08-11 14:48:05 +020013 */
14
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
16
17#include <stdlib.h>
18#include <assert.h>
19#include <string.h>
20#include <ctype.h>
Michal Vaskoe7fc19c2015-08-05 16:24:39 +020021#include <limits.h>
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020022
23#include "libyang.h"
24#include "resolve.h"
25#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020026#include "xpath.h"
Michal Vasko1dca6882015-10-22 14:29:42 +020027#include "parser.h"
Pavol Vicana0e4e672016-02-24 12:20:04 +010028#include "parser_yang.h"
Michal Vasko88c29542015-11-27 14:57:53 +010029#include "xml_internal.h"
Michal Vasko6c810702018-03-14 16:23:21 +010030#include "hash_table.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020031#include "tree_internal.h"
Radek Krejcie534c132016-11-23 13:32:31 +010032#include "extensions.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020033
Michal Vasko2d44ee02018-05-18 09:38:51 +020034/* internal parsed predicate structure */
35struct 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 Vaskod24dd012016-09-30 12:20:22 +020048int
49parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num)
Michal Vasko4d1f0482016-09-19 14:35:06 +020050{
51 const char *ptr;
52 int minus = 0;
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020053 int64_t ret = 0, prev_ret;
Radek Krejcibf47a822016-11-04 10:06:08 +010054 int8_t str_exp, str_dig = -1, trailing_zeros = 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +020055
56 ptr = *str_num;
57
58 if (ptr[0] == '-') {
59 minus = 1;
60 ++ptr;
Radek Krejci51673202016-11-01 17:00:32 +010061 } else if (ptr[0] == '+') {
62 ++ptr;
Michal Vasko4d1f0482016-09-19 14:35:06 +020063 }
64
Michal Vaskod24dd012016-09-30 12:20:22 +020065 if (!isdigit(ptr[0])) {
66 /* there must be at least one */
67 return 1;
68 }
69
Michal Vasko4d1f0482016-09-19 14:35:06 +020070 for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) {
71 if (str_exp > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020072 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020073 }
74
75 if (ptr[0] == '.') {
76 if (ptr[1] == '.') {
77 /* it's the next interval */
78 break;
79 }
80 ++str_dig;
81 } else {
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020082 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 Vasko4d1f0482016-09-19 14:35:06 +020094 if (str_dig > -1) {
95 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010096 if (ptr[0] == '0') {
97 /* possibly trailing zero */
98 trailing_zeros++;
99 } else {
100 trailing_zeros = 0;
101 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200102 }
103 ++str_exp;
104 }
105 }
Michal Vaskod24dd012016-09-30 12:20:22 +0200106 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 Vasko4d1f0482016-09-19 14:35:06 +0200111 str_dig = 0;
112 }
Radek Krejcibf47a822016-11-04 10:06:08 +0100113 /* remove trailing zeros */
114 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +0100115 str_dig -= trailing_zeros;
116 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +0100117 ret = ret / dec_pow(trailing_zeros);
118 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200119
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 Vaskod24dd012016-09-30 12:20:22 +0200123 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200124 }
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200125 prev_ret = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200126 ret *= dec_pow(dig - str_dig);
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200127 if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) {
128 return 1;
129 }
130
Michal Vasko4d1f0482016-09-19 14:35:06 +0200131 }
132 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200133 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200134 }
135
Michal Vasko4d1f0482016-09-19 14:35:06 +0200136 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200137 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200138
Michal Vaskod24dd012016-09-30 12:20:22 +0200139 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200140}
141
142/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200143 * @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 Vaskobb211122015-08-19 14:03:11 +0200149 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200150 *
151 * @return Number of characters successfully parsed.
152 */
Radek Krejcidce5f972017-09-12 15:47:49 +0200153unsigned int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200154parse_identifier(const char *id)
155{
Radek Krejcidce5f972017-09-12 15:47:49 +0200156 unsigned int parsed = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200157
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100158 assert(id);
159
Radek Krejci6dc53a22015-08-17 13:27:59 +0200160 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 Vasko723e50c2015-10-20 15:20:29 +0200178 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200179 *
Michal Vaskobb211122015-08-19 14:03:11 +0200180 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200181 * @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 Krejci6dc53a22015-08-17 13:27:59 +0200183 * @param[out] name Points to the node name.
184 * @param[out] nam_len Length of the node name.
Michal Vasko50576712017-07-28 12:28:33 +0200185 * @param[out] all_desc Whether the path starts with '/', only supported in extended paths.
PavolVicanb28bbff2018-02-21 00:44:02 +0100186 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200187 *
188 * @return Number of characters successfully parsed,
189 * positive on success, negative on failure.
190 */
191static int
Michal Vasko50576712017-07-28 12:28:33 +0200192parse_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 Krejci6dc53a22015-08-17 13:27:59 +0200194{
PavolVican195cf392018-02-23 13:24:45 +0100195 int parsed = 0, ret, all_desc_local = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200196
197 assert(id);
Michal Vasko50576712017-07-28 12:28:33 +0200198 assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len));
199 assert((name && nam_len) || (!name && !nam_len));
Michal Vasko50576712017-07-28 12:28:33 +0200200
Michal Vasko723e50c2015-10-20 15:20:29 +0200201 if (mod_name) {
202 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200203 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200204 }
205 if (name) {
206 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200207 *nam_len = 0;
208 }
209
Michal Vasko50576712017-07-28 12:28:33 +0200210 if (extended) {
211 /* try to parse only the extended expressions */
212 if (id[parsed] == '/') {
PavolVican195cf392018-02-23 13:24:45 +0100213 if (all_desc) {
214 *all_desc = 1;
215 }
216 all_desc_local = 1;
Michal Vasko50576712017-07-28 12:28:33 +0200217 } else {
PavolVican195cf392018-02-23 13:24:45 +0100218 if (all_desc) {
219 *all_desc = 0;
220 }
Michal Vasko50576712017-07-28 12:28:33 +0200221 }
222
223 /* is there a prefix? */
PavolVican195cf392018-02-23 13:24:45 +0100224 ret = parse_identifier(id + all_desc_local);
Michal Vasko50576712017-07-28 12:28:33 +0200225 if (ret > 0) {
PavolVican195cf392018-02-23 13:24:45 +0100226 if (id[all_desc_local + ret] != ':') {
Michal Vasko50576712017-07-28 12:28:33 +0200227 /* this is not a prefix, so not an extended id */
228 goto standard_id;
229 }
230
231 if (mod_name) {
PavolVican195cf392018-02-23 13:24:45 +0100232 *mod_name = id + all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200233 *mod_name_len = ret;
234 }
235
236 /* "/" and ":" */
PavolVican195cf392018-02-23 13:24:45 +0100237 ret += all_desc_local + 1;
Michal Vasko50576712017-07-28 12:28:33 +0200238 } else {
PavolVican195cf392018-02-23 13:24:45 +0100239 ret = all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200240 }
241
242 /* parse either "*" or "." */
PavolVicanb28bbff2018-02-21 00:44:02 +0100243 if (*(id + ret) == '*') {
Michal Vasko50576712017-07-28 12:28:33 +0200244 if (name) {
245 *name = id + ret;
246 *nam_len = 1;
247 }
248 ++ret;
249
250 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100251 } else if (*(id + ret) == '.') {
PavolVican195cf392018-02-23 13:24:45 +0100252 if (!all_desc_local) {
Michal Vasko50576712017-07-28 12:28:33 +0200253 /* /. 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;
PavolVicanb28bbff2018-02-21 00:44:02 +0100264 } else if (*(id + ret) == '#') {
PavolVican195cf392018-02-23 13:24:45 +0100265 if (all_desc_local || !ret) {
PavolVicanb28bbff2018-02-21 00:44:02 +0100266 /* 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 Vasko50576712017-07-28 12:28:33 +0200276 }
277 /* else a standard id, parse it all again */
278 }
279
280standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200281 if ((ret = parse_identifier(id)) < 1) {
282 return ret;
283 }
284
Michal Vasko723e50c2015-10-20 15:20:29 +0200285 if (mod_name) {
286 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200287 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200288 }
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 Vasko723e50c2015-10-20 15:20:29 +0200300 if (name && mod_name) {
301 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200302 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200303 if (mod_name) {
304 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200305 }
306
Michal Vasko723e50c2015-10-20 15:20:29 +0200307 if (nam_len && mod_name_len) {
308 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200309 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200310 if (mod_name_len) {
311 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200312 }
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 Krejci6dc53a22015-08-17 13:27:59 +0200324 *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 Vaskobb211122015-08-19 14:03:11 +0200336 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200337 * @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 */
348static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200349parse_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 Krejci6dc53a22015-08-17 13:27:59 +0200351{
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 Vasko50576712017-07-28 12:28:33 +0200390 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200391 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 Vaskobb211122015-08-19 14:03:11 +0200460 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200461 * @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 */
470static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200471parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
472 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200473{
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 Vasko50576712017-07-28 12:28:33 +0200577 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
578 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200579 }
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 Krejcif7ed4c32016-10-27 16:20:03 +0200594 * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified
Michal Vaskobb211122015-08-19 14:03:11 +0200595 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200596 * @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 */
608static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200609parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200610 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200611{
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 Vasko50576712017-07-28 12:28:33 +0200663 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
664 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200665 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200666 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200667 /* actually we always need prefix even it is not specified */
668 *prefix = lys_main_module(mod)->name;
669 *pref_len = strlen(*prefix);
670 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200671
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 Vaskof39142b2015-10-21 11:40:05 +0200690 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200691 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200692 *
693 * instance-identifier = 1*("/" (node-identifier *predicate))
694 *
Michal Vaskobb211122015-08-19 14:03:11 +0200695 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200696 * @param[out] model Points to the model name.
697 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200698 * @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 */
705static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200706parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
707 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200708{
709 int parsed = 0, ret;
710
Michal Vasko1b6ca962017-08-03 14:23:09 +0200711 assert(id && model && mod_len && name && nam_len);
712
Radek Krejci6dc53a22015-08-17 13:27:59 +0200713 if (has_predicate) {
714 *has_predicate = 0;
715 }
716
717 if (id[0] != '/') {
718 return -parsed;
719 }
720
721 ++parsed;
722 ++id;
723
Michal Vaskob2f40be2016-09-08 16:03:48 +0200724 if ((ret = parse_identifier(id)) < 1) {
725 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200726 }
727
Michal Vaskob2f40be2016-09-08 16:03:48 +0200728 *name = id;
729 *nam_len = ret;
730
731 parsed += ret;
732 id += ret;
733
Michal Vasko1b6ca962017-08-03 14:23:09 +0200734 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 Krejci4967cb62016-09-14 16:40:28 +0200753 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200754 *has_predicate = 1;
755 }
756
757 return parsed;
758}
759
760/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200761 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200762 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200763 *
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 Vaskobb211122015-08-19 14:03:11 +0200770 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200771 * @param[out] model Points to the model name.
772 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200773 * @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 */
783static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200784parse_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 Krejci6dc53a22015-08-17 13:27:59 +0200786{
787 const char *ptr;
788 int parsed = 0, ret;
789 char quote;
790
791 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200792 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200793 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200794 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200795 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200796 }
797 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200798 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200799 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200800 *nam_len = 0;
801 }
802 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200803 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200804 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200805 *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 Vaskof2f28a12016-09-09 12:43:06 +0200830 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200831 }
832
833 while (isdigit(id[0])) {
834 ++parsed;
835 ++id;
836 }
837
838 if (nam_len) {
839 *nam_len = id-(*name);
840 }
841
Michal Vaskof2f28a12016-09-09 12:43:06 +0200842 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200843 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200844 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 Vasko50576712017-07-28 12:28:33 +0200856 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200857 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200858 }
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 Vasko1f2cc332015-08-19 11:18:32 +0200870 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200871 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200872
Radek Krejci6dc53a22015-08-17 13:27:59 +0200873 ++parsed;
874 ++id;
875
Michal Vaskof2f28a12016-09-09 12:43:06 +0200876 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 Vasko1b6ca962017-08-03 14:23:09 +0200891 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200892
893 if (value) {
894 *value = id;
895 }
896 if (val_len) {
897 *val_len = ret;
898 }
899
Michal Vasko1b6ca962017-08-03 14:23:09 +0200900 parsed += ret + 1;
901 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200902 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200903 return -parsed;
904 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200905 }
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 Vasko48935352016-03-29 11:52:36 +0200932 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200933 * node-identifier
934 * absolute-schema-nodeid
935 *
Michal Vaskobb211122015-08-19 14:03:11 +0200936 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200937 * @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 Vasko48935352016-03-29 11:52:36 +0200939 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200940 * @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 Vasko83e8e5b2016-03-11 14:29:10 +0100943 * @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 Vasko50576712017-07-28 12:28:33 +0200945 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200946 *
947 * @return Number of characters successfully parsed,
948 * positive on success, negative on failure.
949 */
Michal Vasko22448d32016-03-16 13:17:29 +0100950int
Michal Vasko723e50c2015-10-20 15:20:29 +0200951parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
Michal Vasko50576712017-07-28 12:28:33 +0200952 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200953{
954 int parsed = 0, ret;
955
956 assert(id);
957 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200958
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100959 if (has_predicate) {
960 *has_predicate = 0;
961 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200962
963 if (id[0] != '/') {
964 if (*is_relative != -1) {
965 return -parsed;
966 } else {
967 *is_relative = 1;
968 }
Michal Vasko48935352016-03-29 11:52:36 +0200969 if (!strncmp(id, "./", 2)) {
970 parsed += 2;
971 id += 2;
972 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200973 } else {
974 if (*is_relative == -1) {
975 *is_relative = 0;
976 }
977 ++parsed;
978 ++id;
979 }
980
Michal Vasko50576712017-07-28 12:28:33 +0200981 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
982 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200983 }
984
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100985 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 Vasko9fbb6e82017-07-04 13:50:04 +0200999 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001000 * key-with-value = identifier *WSP "=" *WSP
1001 * ((DQUOTE string DQUOTE) /
1002 * (SQUOTE string SQUOTE))
1003 *
1004 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001005 * @param[out] mod_name Points to the list key module name.
1006 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001007 * @param[out] name Points to the list key name.
1008 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +01001009 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001010 * @param[out] val_len Length of \p value.
1011 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
1012 */
Michal Vasko22448d32016-03-16 13:17:29 +01001013int
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001014parse_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 Vasko83e8e5b2016-03-11 14:29:10 +01001016{
1017 const char *ptr;
1018 int parsed = 0, ret;
1019 char quote;
1020
1021 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001022 if (mod_name) {
1023 *mod_name = NULL;
1024 }
1025 if (mod_name_len) {
1026 *mod_name_len = 0;
1027 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001028 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 Vasko22448d32016-03-16 13:17:29 +01001056 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001057 if (id[0] == '.') {
1058 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001059
1060 if (name) {
1061 *name = id;
1062 }
1063 if (nam_len) {
1064 *nam_len = ret;
1065 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001066 } 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 Vasko9fbb6e82017-07-04 13:50:04 +02001074
1075 if (name) {
1076 *name = id;
1077 }
1078 if (nam_len) {
1079 *nam_len = ret;
1080 }
Michal Vasko50576712017-07-28 12:28:33 +02001081 } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001082 return -parsed + ret;
1083 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001084
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 Vasko58c2aab2017-01-05 10:02:05 +01001095 if (name && isdigit(**name)) {
1096 return -parsed;
1097 }
1098
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001099 ++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 Krejci6dc53a22015-08-17 13:27:59 +02001150}
1151
Michal Vasko2d44ee02018-05-18 09:38:51 +02001152#ifdef LY_ENABLED_CACHE
1153
1154static int
1155resolve_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 Vasko87e29a82018-05-21 13:50:43 +02001163 (void)mod;
Michal Vasko2d44ee02018-05-18 09:38:51 +02001164
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
1230static struct lyd_node *
1231resolve_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 Vaskod60a1a32018-05-23 16:31:22 +02001259 i = lyht_find(parent->ht, &pp, hash, (void **)&ret);
1260 assert(i || *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001261
1262 /* restore the original callback */
1263 lyht_set_cb(parent->ht, prev_cb);
1264
Michal Vaskod60a1a32018-05-23 16:31:22 +02001265 return (i ? NULL : *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001266}
1267
1268#endif
1269
Radek Krejci6dc53a22015-08-17 13:27:59 +02001270/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001271 * @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 Krejci9ff0a922016-07-14 13:08:05 +02001276 * @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 Vaskoc5c26b02016-06-29 11:10:29 +02001278 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001279 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001280 */
1281static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001282resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001283{
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 Krejci9ff0a922016-07-14 13:08:05 +02001289 assert(feature);
1290
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001291 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001292 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001293 LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001294 return -1;
1295 }
1296
Michal Vasko921eb6b2017-10-13 10:01:39 +02001297 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001298 if (!module) {
1299 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01001300 LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001301 return -1;
1302 }
1303
Radek Krejci9ff0a922016-07-14 13:08:05 +02001304 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 Vicanfdab9f92016-09-07 15:23:27 +02001310 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001311 return -1;
1312 }
1313 *feature = &node->module->features[j];
1314 return 0;
1315 }
1316 }
1317 }
1318
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001319 /* search in the identified module ... */
1320 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001321 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001322 /* check status */
1323 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001324 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001325 return -1;
1326 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001327 *feature = &module->features[j];
1328 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001329 }
1330 }
1331 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001332 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001333 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001334 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1335 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001336 /* 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 Vicanfdab9f92016-09-07 15:23:27 +02001340 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001341 return -1;
1342 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001343 *feature = &module->inc[i].submodule->features[j];
1344 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001345 }
1346 }
1347 }
1348
1349 /* not found */
1350 str = strndup(feat_name, len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001351 LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001352 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001353 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001354}
1355
Radek Krejci9ff0a922016-07-14 13:08:05 +02001356/*
1357 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001358 * - 1 if enabled
1359 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001360 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001361static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001362resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001363{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001364 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001365
Radek Krejci9ff0a922016-07-14 13:08:05 +02001366 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001367 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001368 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001369 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001370 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001371
Radek Krejci69b8d922016-07-27 13:13:41 +02001372 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001373}
1374
1375static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001376resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001377{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001378 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001379 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001380
Radek Krejci9ff0a922016-07-14 13:08:05 +02001381 op = iff_getop(expr->expr, *index_e);
1382 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001383
Radek Krejci9ff0a922016-07-14 13:08:05 +02001384 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 Krejciaf566332017-02-07 15:56:59 +01001389 /* invert result */
1390 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001391 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 Krejciaf566332017-02-07 15:56:59 +01001395 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001396 return a && b;
1397 } else { /* LYS_IFF_OR */
1398 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001399 }
1400 }
1401
Radek Krejciaf566332017-02-07 15:56:59 +01001402 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001403}
1404
1405int
1406resolve_iffeature(struct lys_iffeature *expr)
1407{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001408 int index_e = 0, index_f = 0;
1409
1410 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001411 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001412 }
Radek Krejciaf566332017-02-07 15:56:59 +01001413 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001414}
1415
1416struct iff_stack {
1417 int size;
1418 int index; /* first empty item */
1419 uint8_t *stack;
1420};
1421
1422static int
1423iff_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 Vasko53b7da02018-02-13 15:28:42 +01001428 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001429 }
1430
1431 stack->stack[stack->index++] = value;
1432 return EXIT_SUCCESS;
1433}
1434
1435static uint8_t
1436iff_stack_pop(struct iff_stack *stack)
1437{
1438 stack->index--;
1439 return stack->stack[stack->index];
1440}
1441
1442static void
1443iff_stack_clean(struct iff_stack *stack)
1444{
1445 stack->size = 0;
1446 free(stack->stack);
1447}
1448
1449static void
1450iff_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
1464uint8_t
1465iff_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 Krejcicbb473e2016-09-16 14:48:32 +02001480/* internal structure for passing data for UNRES_IFFEAT */
1481struct unres_iffeat_data {
1482 struct lys_node *node;
1483 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001484 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001485};
1486
Radek Krejci9ff0a922016-07-14 13:08:05 +02001487void
1488resolve_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 Vaskoc5c26b02016-06-29 11:10:29 +02001505 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001506 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 Vaskoc5c26b02016-06-29 11:10:29 +02001521 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001522 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001523
Radek Krejci9ff0a922016-07-14 13:08:05 +02001524result:
1525 if (expr_size) {
1526 *expr_size = e;
1527 }
1528 if (feat_size) {
1529 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001530 }
1531}
1532
1533int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001534resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001535 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001536{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001537 const char *c = value;
1538 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001539 int i, j, last_not, checkversion = 0;
1540 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001541 uint8_t op;
1542 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001543 struct unres_iffeat_data *iff_data;
Michal Vasko53b7da02018-02-13 15:28:42 +01001544 struct ly_ctx *ctx = node->module->ctx;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001545
Radek Krejci9ff0a922016-07-14 13:08:05 +02001546 assert(c);
1547
1548 if (isspace(c[0])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001549 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001550 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001551 }
1552
Radek Krejci9ff0a922016-07-14 13:08:05 +02001553 /* 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 Krejci69b8d922016-07-27 13:13:41 +02001556 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001557 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 Vasko53b7da02018-02-13 15:28:42 +01001568 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001569 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 Krejci69b8d922016-07-27 13:13:41 +02001582 } else { /* and, or */
1583 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001584 /* 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 Vaskoc5c26b02016-06-29 11:10:29 +02001600 }
1601 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001602 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001603 /* not matching count of ( and ) */
Michal Vasko53b7da02018-02-13 15:28:42 +01001604 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001605 return EXIT_FAILURE;
1606 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001607
Radek Krejci69b8d922016-07-27 13:13:41 +02001608 if (checkversion || expr_size > 1) {
1609 /* check that we have 1.1 module */
Radek Krejci13fde922018-05-16 10:45:58 +02001610 if (node->module->version != LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001611 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 Krejci69b8d922016-07-27 13:13:41 +02001613 return EXIT_FAILURE;
1614 }
1615 }
1616
Radek Krejci9ff0a922016-07-14 13:08:05 +02001617 /* allocate the memory */
1618 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001619 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001620 stack.stack = malloc(expr_size * sizeof *stack.stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001621 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001622 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001623 f_size--; expr_size--; /* used as indexes from now */
1624
1625 for (i--; i >= 0; i--) {
1626 if (c[i] == ')') {
1627 /* push it on stack */
1628 iff_stack_push(&stack, LYS_IFF_RP);
1629 continue;
1630 } else if (c[i] == '(') {
1631 /* pop from the stack into result all operators until ) */
1632 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1633 iff_setop(iffeat_expr->expr, op, expr_size--);
1634 }
1635 continue;
1636 } else if (isspace(c[i])) {
1637 continue;
1638 }
1639
1640 /* end operator or operand -> find beginning and get what is it */
1641 j = i + 1;
1642 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1643 i--;
1644 }
1645 i++; /* get back by one step */
1646
1647 if (!strncmp(&c[i], "not ", 4)) {
1648 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1649 /* double not */
1650 iff_stack_pop(&stack);
1651 } else {
1652 /* not has the highest priority, so do not pop from the stack
1653 * as in case of AND and OR */
1654 iff_stack_push(&stack, LYS_IFF_NOT);
1655 }
1656 } else if (!strncmp(&c[i], "and ", 4)) {
1657 /* as for OR - pop from the stack all operators with the same or higher
1658 * priority and store them to the result, then push the AND to the stack */
1659 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1660 op = iff_stack_pop(&stack);
1661 iff_setop(iffeat_expr->expr, op, expr_size--);
1662 }
1663 iff_stack_push(&stack, LYS_IFF_AND);
1664 } else if (!strncmp(&c[i], "or ", 3)) {
1665 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1666 op = iff_stack_pop(&stack);
1667 iff_setop(iffeat_expr->expr, op, expr_size--);
1668 }
1669 iff_stack_push(&stack, LYS_IFF_OR);
1670 } else {
1671 /* feature name, length is j - i */
1672
1673 /* add it to the result */
1674 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1675
1676 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001677 * forward referenced, we have to keep the feature name in auxiliary
1678 * structure passed into unres */
1679 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001680 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001681 iff_data->node = node;
1682 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001683 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001684 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1685 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001686 f_size--;
1687
1688 if (r == -1) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01001689 lydict_remove(node->module->ctx, iff_data->fname);
Pavol Vican4d084512016-09-29 16:38:12 +02001690 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001691 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001692 }
1693 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001694 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001695 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 Vasko53b7da02018-02-13 15:28:42 +01001702 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001703 rc = EXIT_FAILURE;
1704 } else {
1705 rc = EXIT_SUCCESS;
1706 }
1707
1708error:
1709 /* cleanup */
1710 iff_stack_clean(&stack);
1711
1712 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001713}
1714
1715/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001716 * @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 */
1722struct lyd_node *
1723resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1724{
1725 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001726 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001727 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 Vasko53b7da02018-02-13 15:28:42 +01001736 LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001737
Michal Vasko3edeaf72016-02-11 13:17:43 +01001738 while (p) {
1739 token = p;
1740 p = strchr(p, '/');
1741 if (p) {
1742 *p = '\0';
1743 p++;
1744 }
1745
Radek Krejci5da4eb62016-04-08 14:45:51 +02001746 if (p) {
1747 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001748 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001749 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001750 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001751 result = NULL;
1752 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001753 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001754
Radek Krejci5da4eb62016-04-08 14:45:51 +02001755 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1756 continue;
1757 }
1758 } else {
1759 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001760 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001761 || !schema) {
1762 result = NULL;
1763 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001764 }
1765 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001766 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 Vasko3edeaf72016-02-11 13:17:43 +01001772 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001773 if (!iter) {
1774 /* instance not found */
1775 result = NULL;
1776 break;
1777 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001778 }
1779 free(str);
1780
1781 return result;
1782}
1783
Radek Krejci1a9c3612017-04-24 14:49:43 +02001784int
Michal Vasko50576712017-07-28 12:28:33 +02001785schema_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 Krejcibdf92362016-04-08 14:43:34 +02001787{
1788 const struct lys_module *prefix_mod;
1789
Michal Vaskocdb3f062018-02-01 09:55:06 +01001790 /* handle special names */
1791 if (name[0] == '*') {
1792 return 2;
1793 } else if (name[0] == '.') {
1794 return 3;
1795 }
1796
Michal Vasko50576712017-07-28 12:28:33 +02001797 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001798 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001799 return 1;
1800 }
1801
Radek Krejcibdf92362016-04-08 14:43:34 +02001802 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001803 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001804 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001805 if (!prefix_mod) {
1806 return -1;
1807 }
1808 } else {
1809 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001810 }
1811 if (prefix_mod != lys_node_module(sibling)) {
1812 return 1;
1813 }
1814
Michal Vasko50576712017-07-28 12:28:33 +02001815 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001816 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001817}
1818
Michal Vasko50576712017-07-28 12:28:33 +02001819/* keys do not have to be ordered and do not have to be all of them */
1820static int
1821resolve_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 Vasko53b7da02018-02-13 15:28:42 +01001836 LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001837 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 Vasko97234262018-02-01 09:53:01 +01001880/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001881 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001882int
Michal Vasko97234262018-02-01 09:53:01 +01001883resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001884 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001885{
PavolVicanb28bbff2018-02-21 00:44:02 +01001886 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vasko97234262018-02-01 09:53:01 +01001887 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001888 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001889 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
PavolVicanb28bbff2018-02-21 00:44:02 +01001890 int yang_data_name_len, backup_mod_name_len = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001891 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001892 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001893 char *str;
Michal Vasko53b7da02018-02-13 15:28:42 +01001894 struct ly_ctx *ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001895
Michal Vasko97234262018-02-01 09:53:01 +01001896 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001897 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001898
Michal Vasko50576712017-07-28 12:28:33 +02001899 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001900 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001901 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001902 ctx = cur_module->ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001903 id = nodeid;
1904
PavolVican195cf392018-02-23 13:24:45 +01001905 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1);
PavolVicanb28bbff2018-02-21 00:44:02 +01001906 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 Vasko50576712017-07-28 12:28:33 +02001925 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 Vasko53b7da02018-02-13 15:28:42 +01001928 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001929 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001930 }
1931 id += r;
1932
PavolVicanb28bbff2018-02-21 00:44:02 +01001933 if (backup_mod_name) {
1934 mod_name = backup_mod_name;
1935 mod_name_len = backup_mod_name_len;
1936 }
1937
Michal Vasko97234262018-02-01 09:53:01 +01001938 if (is_relative && !start_parent) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001939 LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths.");
Michal Vasko3edeaf72016-02-11 13:17:43 +01001940 return -1;
1941 }
1942
1943 /* descendant-schema-nodeid */
1944 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001945 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001946
Michal Vasko3edeaf72016-02-11 13:17:43 +01001947 /* absolute-schema-nodeid */
1948 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001949 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001950 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001951 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001952 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001953 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001954 return -1;
1955 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001956 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01001957 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 Vasko3edeaf72016-02-11 13:17:43 +01001966 }
1967
1968 while (1) {
1969 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001970 last_aug = NULL;
1971
1972 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001973 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1974 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001975 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001976 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001977 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001978 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001979 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001980 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001981 return -1;
1982 }
1983 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001984 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001985 * 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 Vasko17315772017-07-10 15:15:39 +02001987 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001988 }
1989
1990 /* if the module is implemented, all the augments will be connected */
Michal Vasko50576712017-07-28 12:28:33 +02001991 if (!aux_mod->implemented && !extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001992get_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 Vaskocb45f472018-02-12 10:47:42 +01001998 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02001999 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 Krejcibdf92362016-04-08 14:43:34 +02002005 continue;
Michal Vasko50576712017-07-28 12:28:33 +02002006 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002007 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002008 }
Michal Vasko50576712017-07-28 12:28:33 +02002009 } else if (!id[0]) {
2010 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002011 }
Michal Vasko50576712017-07-28 12:28:33 +02002012
2013 if (r == 0) {
2014 /* one matching result */
2015 if (nodeid_end) {
2016 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002017 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002018 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 Vasko53b7da02018-02-13 15:28:42 +01002032 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002033 }
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 Vasko53b7da02018-02-13 15:28:42 +01002048 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002049 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 Vasko53b7da02018-02-13 15:28:42 +01002062 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002063 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 Vasko53b7da02018-02-13 15:28:42 +01002072 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002073 return -1;
2074 }
2075 ++id;
2076 }
2077 }
2078
2079 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
2080 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002081 }
2082
2083 /* no match */
2084 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02002085 if (last_aug) {
2086 /* it still could be in another augment */
2087 goto get_next_augment;
2088 }
Michal Vasko50576712017-07-28 12:28:33 +02002089 if (no_node_error) {
2090 str = strndup(nodeid, (name - nodeid) + nam_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01002091 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02002092 free(str);
2093 return -1;
2094 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01002095 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002096 return EXIT_SUCCESS;
2097 }
2098
Michal Vasko50576712017-07-28 12:28:33 +02002099 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 Vasko53b7da02018-02-13 15:28:42 +01002102 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02002103 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002104 }
2105 id += r;
2106 }
2107
2108 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002109 LOGINT(ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002110 return -1;
2111}
2112
Radek Krejcif3c71de2016-04-11 12:45:46 +02002113/* 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 Vasko3edeaf72016-02-11 13:17:43 +01002118int
2119resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02002120 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002121{
2122 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002123 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002124 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 Krejcibdf92362016-04-08 14:43:34 +02002126 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002127
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002128 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01002129 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01002130
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002131 if (!start) {
2132 /* leaf not found */
2133 return 0;
2134 }
2135
Michal Vasko3edeaf72016-02-11 13:17:43 +01002136 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02002137 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002138
Michal Vasko50576712017-07-28 12:28:33 +02002139 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002140 return ((id - nodeid) - r) + 1;
2141 }
2142 id += r;
2143
2144 if (!is_relative) {
2145 return -1;
2146 }
2147
Michal Vasko24476fa2017-03-08 12:33:48 +01002148 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02002149 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01002150 start_parent = lys_parent(start_parent);
2151 }
2152
Michal Vasko3edeaf72016-02-11 13:17:43 +01002153 while (1) {
2154 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002155 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vaskocb45f472018-02-12 10:47:42 +01002156 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002157 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2158 if (r == 0) {
2159 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002160 if (!(sibling->nodetype & ret_nodetype)) {
2161 /* wrong node type, too bad */
2162 continue;
2163 }
2164 *ret = sibling;
2165 return EXIT_SUCCESS;
2166 }
Michal Vasko50576712017-07-28 12:28:33 +02002167 start_parent = sibling;
2168 break;
2169 } else if (r == 1) {
2170 continue;
2171 } else {
2172 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002173 }
2174 }
2175
2176 /* no match */
2177 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002178 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002179 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02002180 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
2181 *ret = NULL;
2182 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002183 }
2184
Michal Vasko50576712017-07-28 12:28:33 +02002185 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002186 return ((id - nodeid) - r) + 1;
2187 }
2188 id += r;
2189 }
2190
2191 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002192 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002193 return -1;
2194}
2195
2196/* choice default */
2197int
2198resolve_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 Vaskodc300b02017-04-07 14:09:20 +02002205 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002206}
2207
2208/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2209static int
2210resolve_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 Vasko50576712017-07-28 12:28:33 +02002217 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len, NULL, 0)) < 1) || nodeid[i]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002218 return -i + 1;
2219 }
2220
Michal Vasko921eb6b2017-10-13 10:01:39 +02002221 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002222 if (!module) {
2223 return -1;
2224 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002225 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002226 start = module->data;
2227 }
2228
2229 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2230
2231 return EXIT_SUCCESS;
2232}
2233
2234int
2235resolve_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 Vasko24476fa2017-03-08 12:33:48 +01002239 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002240 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002241 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002242
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 Vasko24476fa2017-03-08 12:33:48 +01002247 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002248
Michal Vasko50576712017-07-28 12:28:33 +02002249 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002250 return ((id - nodeid) - r) + 1;
2251 }
2252 id += r;
2253
2254 if (is_relative) {
2255 return -1;
2256 }
2257
Michal Vasko921eb6b2017-10-13 10:01:39 +02002258 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002259 if (!abs_start_mod) {
2260 return -1;
2261 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002262
2263 while (1) {
2264 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002265 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vaskocb45f472018-02-12 10:47:42 +01002266 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002267 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2268 if (r == 0) {
2269 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002270 if (!(sibling->nodetype & ret_nodetype)) {
2271 /* wrong node type, too bad */
2272 continue;
2273 }
2274 *ret = sibling;
2275 return EXIT_SUCCESS;
2276 }
Michal Vasko50576712017-07-28 12:28:33 +02002277 start_parent = sibling;
2278 break;
2279 } else if (r == 1) {
2280 continue;
2281 } else {
2282 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002283 }
2284 }
2285
2286 /* no match */
2287 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002288 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002289 return EXIT_SUCCESS;
2290 }
2291
Michal Vasko50576712017-07-28 12:28:33 +02002292 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002293 return ((id - nodeid) - r) + 1;
2294 }
2295 id += r;
2296 }
2297
2298 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002299 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002300 return -1;
2301}
2302
Michal Vaskoe733d682016-03-14 09:08:27 +01002303static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002304resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002305{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002306 const char *mod_name, *name;
2307 int mod_name_len, nam_len, has_predicate, i;
2308 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002309
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002310 if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002311 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002312 LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002313 return -1;
2314 }
2315
2316 predicate += i;
2317 *parsed += i;
2318
Michal Vasko58c2aab2017-01-05 10:02:05 +01002319 if (!isdigit(name[0])) {
2320 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002321 key = (struct lys_node *)list->keys[i];
2322 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002323 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002324 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002325 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002326
Michal Vasko58c2aab2017-01-05 10:02:05 +01002327 if (i == list->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002328 LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko58c2aab2017-01-05 10:02:05 +01002329 return -1;
2330 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002331 }
2332
2333 /* more predicates? */
2334 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002335 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002336 }
2337
2338 return 0;
2339}
2340
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002341/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002342const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002343resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002344{
Michal Vasko53b7da02018-02-13 15:28:42 +01002345 char *str;
PavolVicanb28bbff2018-02-21 00:44:02 +01002346 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vaskob3744402017-08-03 14:23:58 +02002347 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002348 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
PavolVicanb28bbff2018-02-21 00:44:02 +01002349 int yang_data_name_len, backup_mod_name_len;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002350 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002351 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002352
Michal Vasko3547c532016-03-14 09:40:50 +01002353 assert(nodeid && (ctx || start));
2354 if (!ctx) {
2355 ctx = start->module->ctx;
2356 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002357
2358 id = nodeid;
2359
PavolVican195cf392018-02-23 13:24:45 +01002360 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) {
PavolVicanb28bbff2018-02-21 00:44:02 +01002361 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 Vasko50576712017-07-28 12:28:33 +02002379 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002380 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002381 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002382 }
2383 id += r;
2384
PavolVicanb28bbff2018-02-21 00:44:02 +01002385 if (backup_mod_name) {
2386 mod_name = backup_mod_name;
2387 mod_name_len = backup_mod_name_len;
2388 }
2389
Michal Vasko3edeaf72016-02-11 13:17:43 +01002390 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002391 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002392 start_parent = start;
2393 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2394 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002395 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002396 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002397 } else {
2398 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002399 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002400 LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
Michal Vasko10728b52016-04-07 14:26:29 +02002401 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002402 return NULL;
2403 }
2404
Michal Vasko53b7da02018-02-13 15:28:42 +01002405 str = strndup(mod_name, mod_name_len);
2406 module = ly_ctx_get_module(ctx, str, NULL, 1);
2407 free(str);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002408
Michal Vaskof68a49e2017-08-14 13:23:37 +02002409 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002410 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002411 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002412 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002413 return NULL;
2414 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002415 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01002416 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 Vasko3547c532016-03-14 09:40:50 +01002425
2426 /* now it's as if there was no module name */
2427 mod_name = NULL;
2428 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002429 }
2430
Michal Vaskof68a49e2017-08-14 13:23:37 +02002431 prev_mod = module;
2432
Michal Vasko3edeaf72016-02-11 13:17:43 +01002433 while (1) {
2434 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002435 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002436 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002437 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002438 /* 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 Vasko3edeaf72016-02-11 13:17:43 +01002448 /* module check */
2449 if (mod_name) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002450 /* will also find an augment module */
Michal Vasko53b7da02018-02-13 15:28:42 +01002451 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002452
Michal Vasko3edeaf72016-02-11 13:17:43 +01002453 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002454 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002455 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002456 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002457 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002458 }
2459 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002460 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002461 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002462 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002463 continue;
2464 }
2465
Michal Vaskoe733d682016-03-14 09:08:27 +01002466 /* do we have some predicates on it? */
2467 if (has_predicate) {
2468 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002469 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002470 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002471 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002472 return NULL;
2473 }
2474 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002475 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002476 return NULL;
2477 }
2478 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002479 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002480 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002481 }
2482 id += r;
2483 }
2484
Michal Vasko3edeaf72016-02-11 13:17:43 +01002485 /* the result node? */
2486 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002487 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002488 }
2489
Michal Vaskodc300b02017-04-07 14:09:20 +02002490 /* move down the tree, if possible */
2491 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002492 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskodc300b02017-04-07 14:09:20 +02002493 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002494 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002495 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002496
2497 /* update prev mod */
2498 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002499 break;
2500 }
2501 }
2502
2503 /* no match */
2504 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002505 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002506 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002507 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002508 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002509 }
2510
Michal Vasko50576712017-07-28 12:28:33 +02002511 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002512 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002513 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002514 }
2515 id += r;
2516 }
2517
2518 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002519 LOGINT(ctx);
Michal Vaskoe733d682016-03-14 09:08:27 +01002520 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002521}
2522
Michal Vasko22448d32016-03-16 13:17:29 +01002523static int
Michal Vasko2d44ee02018-05-18 09:38:51 +02002524resolve_partial_json_data_list_predicate(struct parsed_pred pp, struct lyd_node *node, int position)
Michal Vasko22448d32016-03-16 13:17:29 +01002525{
Michal Vasko22448d32016-03-16 13:17:29 +01002526 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002527 struct lyd_node_leaf_list *key;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002528 struct lys_node_list *slist;
Michal Vasko53b7da02018-02-13 15:28:42 +01002529 struct ly_ctx *ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002530
Radek Krejci61a86c62016-03-24 11:06:44 +01002531 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002532 assert(node->schema->nodetype == LYS_LIST);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002533 assert(pp.len);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002534
Michal Vasko53b7da02018-02-13 15:28:42 +01002535 ctx = node->schema->module->ctx;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002536 slist = (struct lys_node_list *)node->schema;
Michal Vasko22448d32016-03-16 13:17:29 +01002537
Michal Vasko53adfc72017-01-06 10:39:10 +01002538 /* is the predicate a number? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002539 if (isdigit(pp.pred[0].name[0])) {
2540 if (position == atoi(pp.pred[0].name)) {
Michal Vasko53adfc72017-01-06 10:39:10 +01002541 /* match */
Michal Vasko53adfc72017-01-06 10:39:10 +01002542 return 0;
2543 } else {
2544 /* not a match */
2545 return 1;
2546 }
2547 }
2548
Michal Vaskof29903d2016-04-18 13:13:10 +02002549 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002550 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 Vaskofebd13d2018-05-17 10:42:24 +02002556 for (i = 0; i < slist->keys_size; ++i) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002557 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 Vaskof29903d2016-04-18 13:13:10 +02002559 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002560 }
2561
Michal Vasko2d44ee02018-05-18 09:38:51 +02002562 if (pp.pred[i].mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002563 /* specific module, check that the found key is from that module */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002564 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 Vasko9fbb6e82017-07-04 13:50:04 +02002567 return -1;
2568 }
Michal Vasko50576712017-07-28 12:28:33 +02002569
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 Vasko2d44ee02018-05-18 09:38:51 +02002572 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko50576712017-07-28 12:28:33 +02002573 return -1;
2574 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002575 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002576 /* 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 Vasko2d44ee02018-05-18 09:38:51 +02002578 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002579 return -1;
2580 }
2581 }
2582
Michal Vasko22448d32016-03-16 13:17:29 +01002583 /* value does not match */
Michal Vasko310bc582018-05-22 10:47:59 +02002584 if (strncmp(key->value_str, pp.pred[i].value, pp.pred[i].val_len) || key->value_str[pp.pred[i].val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002585 return 1;
2586 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002587
2588 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002589 }
2590
Michal Vasko22448d32016-03-16 13:17:29 +01002591 return 0;
2592}
2593
Radek Krejci45826012016-08-24 15:07:57 +02002594/**
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 Vasko22448d32016-03-16 13:17:29 +01002603struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002604resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2605 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002606{
Michal Vasko2d44ee02018-05-18 09:38:51 +02002607 const char *id, *mod_name, *name, *data_val, *llval;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002608 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002609 int has_predicate, last_parsed = 0, llval_len;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002610 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002611 struct lyd_node_leaf_list *llist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002612 const struct lys_module *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002613 struct ly_ctx *ctx;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002614 const struct lys_node *ssibling;
Michal Vasko310bc582018-05-22 10:47:59 +02002615 struct lys_node_list *slist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002616 struct parsed_pred pp;
Michal Vasko22448d32016-03-16 13:17:29 +01002617
2618 assert(nodeid && start && parsed);
2619
Michal Vasko2d44ee02018-05-18 09:38:51 +02002620 memset(&pp, 0, sizeof pp);
Michal Vasko22448d32016-03-16 13:17:29 +01002621 ctx = start->schema->module->ctx;
2622 id = nodeid;
2623
Michal Vasko2d44ee02018-05-18 09:38:51 +02002624 /* parse first nodeid in case it is yang-data extension */
PavolVican195cf392018-02-23 13:24:45 +01002625 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) {
PavolVicanb28bbff2018-02-21 00:44:02 +01002626 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002627 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002628 }
2629
2630 if (name[0] == '#') {
2631 if (is_relative) {
2632 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002633 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002634 }
PavolVicanb28bbff2018-02-21 00:44:02 +01002635 id += r;
2636 last_parsed = r;
2637 } else {
2638 is_relative = -1;
2639 }
2640
Michal Vasko2d44ee02018-05-18 09:38:51 +02002641 /* parse first nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002642 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002643 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002644 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002645 }
2646 id += r;
2647 /* add it to parsed only after the data node was actually found */
PavolVicanb28bbff2018-02-21 00:44:02 +01002648 last_parsed += r;
Michal Vasko22448d32016-03-16 13:17:29 +01002649
2650 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002651 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002652 start = start->child;
2653 } else {
2654 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002655 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002656 }
2657
Michal Vaskofebd13d2018-05-17 10:42:24 +02002658 /* do not duplicate code, use predicate parsing from the loop */
2659 goto parse_predicates;
2660
Michal Vasko22448d32016-03-16 13:17:29 +01002661 while (1) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002662 /* find the correct schema node first */
2663 ssibling = NULL;
2664 while ((ssibling = lys_getnext(ssibling, (start && start->parent) ? start->parent->schema : NULL, prev_mod, 0))) {
2665 if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) {
2666 break;
Michal Vasko2411b942016-03-23 13:50:03 +01002667 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002668 }
2669 if (!ssibling) {
2670 /* there is not even such a schema node */
2671 free(pp.pred);
2672 return last_match;
2673 }
2674 pp.schema = ssibling;
Michal Vasko2411b942016-03-23 13:50:03 +01002675
Michal Vasko2d44ee02018-05-18 09:38:51 +02002676 /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if
2677 * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */
2678 if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) {
2679 pp.len = 1;
2680 pp.pred = calloc(1, sizeof *pp.pred);
2681 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
Michal Vasko22448d32016-03-16 13:17:29 +01002682
Michal Vasko2d44ee02018-05-18 09:38:51 +02002683 pp.pred[0].name = ".";
2684 pp.pred[0].nam_len = 1;
2685 pp.pred[0].value = (llist_value ? llist_value : "");
2686 pp.pred[0].val_len = strlen(pp.pred[0].value);
2687 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002688
Michal Vasko310bc582018-05-22 10:47:59 +02002689 if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) {
2690 /* check leaf/leaf-list predicate */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002691 if (pp.len > 1) {
2692 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2693 goto error;
Michal Vasko310bc582018-05-22 10:47:59 +02002694 } else if (pp.len) {
2695 if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) {
2696 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name);
2697 goto error;
2698 }
2699 if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) {
2700 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value);
2701 goto error;
2702 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002703 }
2704 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko310bc582018-05-22 10:47:59 +02002705 /* list should have predicates for all the keys or position */
2706 slist = (struct lys_node_list *)ssibling;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002707 if (!pp.len) {
2708 /* none match */
2709 return last_match;
Michal Vasko310bc582018-05-22 10:47:59 +02002710 } else if (!isdigit(pp.pred[0].name[0])) {
2711 /* list predicate is not a position, so there must be all the keys */
2712 if (pp.len > slist->keys_size) {
2713 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2714 goto error;
2715 } else if (pp.len < slist->keys_size) {
2716 LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name);
2717 goto error;
2718 }
2719 /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */
2720 for (r = 0; r < pp.len; ++r) {
2721 if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) {
2722 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value);
2723 goto error;
2724 }
2725 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002726 }
2727 } else if (pp.pred) {
2728 /* no other nodes allow predicates */
2729 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2730 goto error;
2731 }
2732
2733#ifdef LY_ENABLED_CACHE
2734 /* we will not be matching keyless lists or state leaf-lists this way */
2735 if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size)
2736 && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) {
2737 sibling = resolve_json_data_node_hash(start->parent, pp);
2738 } else
2739#endif
2740 {
2741 list_instance_position = 0;
2742 LY_TREE_FOR(start, sibling) {
2743 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
2744 if (lys_parent(sibling->schema)) {
2745 if (options & LYD_PATH_OPT_OUTPUT) {
2746 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
2747 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
2748 goto error;
2749 }
2750 } else {
2751 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
2752 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
2753 goto error;
2754 }
Michal Vasko22448d32016-03-16 13:17:29 +01002755 }
Michal Vasko22448d32016-03-16 13:17:29 +01002756 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002757
2758 if (sibling->schema != ssibling) {
2759 /* wrong schema node */
Michal Vasko22448d32016-03-16 13:17:29 +01002760 continue;
2761 }
2762
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002763 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002764 if (ssibling->nodetype == LYS_LEAFLIST) {
2765 if (ssibling->flags & LYS_CONFIG_R) {
Michal Vasko24affa02018-04-03 09:06:06 +02002766 /* state leaf-lists will never match */
2767 continue;
2768 }
2769
Michal Vasko9ba34de2016-12-07 12:21:19 +01002770 llist = (struct lyd_node_leaf_list *)sibling;
2771
Michal Vasko2d44ee02018-05-18 09:38:51 +02002772 /* get the expected leaf-list value */
2773 llval = NULL;
2774 llval_len = 0;
2775 if (pp.pred) {
2776 /* it was already checked that it is correct */
2777 llval = pp.pred[0].value;
2778 llval_len = pp.pred[0].val_len;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002779
Michal Vaskof0a50972016-10-19 11:33:55 +02002780 }
2781
Michal Vasko21b90ce2017-09-19 09:38:27 +02002782 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002783 if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002784 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2785 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002786 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2787 } else {
2788 data_val = llist->value_str;
2789 }
2790
Michal Vasko2d44ee02018-05-18 09:38:51 +02002791 if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len)
2792 || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002793 continue;
2794 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002795
Michal Vasko2d44ee02018-05-18 09:38:51 +02002796 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002797 /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */
Michal Vasko58c2aab2017-01-05 10:02:05 +01002798 ++list_instance_position;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002799 ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position);
Michal Vasko22448d32016-03-16 13:17:29 +01002800 if (ret == -1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002801 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002802 } else if (ret == 1) {
2803 /* this list instance does not match */
2804 continue;
2805 }
Michal Vasko22448d32016-03-16 13:17:29 +01002806 }
2807
Michal Vasko22448d32016-03-16 13:17:29 +01002808 break;
2809 }
2810 }
2811
2812 /* no match, return last match */
2813 if (!sibling) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002814 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002815 return last_match;
2816 }
2817
Michal Vasko2d44ee02018-05-18 09:38:51 +02002818 /* we found a next matching node */
2819 *parsed += last_parsed;
Michal Vasko586831d2018-05-22 11:13:16 +02002820 last_match = sibling;
2821 prev_mod = lyd_node_module(sibling);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002822
2823 /* the result node? */
2824 if (!id[0]) {
2825 free(pp.pred);
Michal Vasko586831d2018-05-22 11:13:16 +02002826 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002827 }
2828
Michal Vasko586831d2018-05-22 11:13:16 +02002829 /* move down the tree, if possible, and continue */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002830 if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko586831d2018-05-22 11:13:16 +02002831 /* there can be no children even through expected, error */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002832 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2833 goto error;
Michal Vasko586831d2018-05-22 11:13:16 +02002834 } else if (!sibling->child) {
2835 /* there could be some children, but are not, return what we found so far */
2836 free(pp.pred);
2837 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002838 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002839 start = sibling->child;
2840
Michal Vaskofebd13d2018-05-17 10:42:24 +02002841 /* parse nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002842 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002843 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002844 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002845 }
2846 id += r;
2847 last_parsed = r;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002848
2849parse_predicates:
2850 /* parse all the predicates */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002851 free(pp.pred);
2852 pp.schema = NULL;
2853 pp.len = 0;
2854 pp.pred = NULL;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002855 while (has_predicate) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002856 ++pp.len;
2857 pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred);
2858 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
2859 if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len,
2860 &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value,
2861 &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002862 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2863 goto error;
2864 }
2865
2866 id += r;
2867 last_parsed += r;
2868 }
Michal Vasko22448d32016-03-16 13:17:29 +01002869 }
2870
Michal Vaskofebd13d2018-05-17 10:42:24 +02002871error:
Michal Vasko238bd2f2016-03-23 09:39:01 +01002872 *parsed = -1;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002873 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002874 return NULL;
2875}
2876
Michal Vasko3edeaf72016-02-11 13:17:43 +01002877/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002878 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002879 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002880 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002881 * @param[in] ctx Context for errors.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002882 * @param[in] str_restr Restriction as a string.
2883 * @param[in] type Type of the restriction.
2884 * @param[out] ret Final interval structure that starts with
2885 * the interval of the initial type, continues with intervals
2886 * of any superior types derived from the initial one, and
2887 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002888 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002889 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002890 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002891int
Michal Vasko53b7da02018-02-13 15:28:42 +01002892resolve_len_ran_interval(struct ly_ctx *ctx, const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002893{
2894 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002895 int kind;
Michal Vaskof75b2772018-03-14 09:55:33 +01002896 int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax;
2897 uint64_t local_umin, local_umax = 0;
2898 uint8_t local_fdig = 0;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002899 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002900 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002901
2902 switch (type->base) {
2903 case LY_TYPE_BINARY:
2904 kind = 0;
2905 local_umin = 0;
2906 local_umax = 18446744073709551615UL;
2907
2908 if (!str_restr && type->info.binary.length) {
2909 str_restr = type->info.binary.length->expr;
2910 }
2911 break;
2912 case LY_TYPE_DEC64:
2913 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002914 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2915 local_fmax = __INT64_C(9223372036854775807);
2916 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002917
2918 if (!str_restr && type->info.dec64.range) {
2919 str_restr = type->info.dec64.range->expr;
2920 }
2921 break;
2922 case LY_TYPE_INT8:
2923 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002924 local_smin = __INT64_C(-128);
2925 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002926
2927 if (!str_restr && type->info.num.range) {
2928 str_restr = type->info.num.range->expr;
2929 }
2930 break;
2931 case LY_TYPE_INT16:
2932 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002933 local_smin = __INT64_C(-32768);
2934 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002935
2936 if (!str_restr && type->info.num.range) {
2937 str_restr = type->info.num.range->expr;
2938 }
2939 break;
2940 case LY_TYPE_INT32:
2941 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002942 local_smin = __INT64_C(-2147483648);
2943 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002944
2945 if (!str_restr && type->info.num.range) {
2946 str_restr = type->info.num.range->expr;
2947 }
2948 break;
2949 case LY_TYPE_INT64:
2950 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002951 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2952 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002953
2954 if (!str_restr && type->info.num.range) {
2955 str_restr = type->info.num.range->expr;
2956 }
2957 break;
2958 case LY_TYPE_UINT8:
2959 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002960 local_umin = __UINT64_C(0);
2961 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002962
2963 if (!str_restr && type->info.num.range) {
2964 str_restr = type->info.num.range->expr;
2965 }
2966 break;
2967 case LY_TYPE_UINT16:
2968 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002969 local_umin = __UINT64_C(0);
2970 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002971
2972 if (!str_restr && type->info.num.range) {
2973 str_restr = type->info.num.range->expr;
2974 }
2975 break;
2976 case LY_TYPE_UINT32:
2977 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002978 local_umin = __UINT64_C(0);
2979 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002980
2981 if (!str_restr && type->info.num.range) {
2982 str_restr = type->info.num.range->expr;
2983 }
2984 break;
2985 case LY_TYPE_UINT64:
2986 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002987 local_umin = __UINT64_C(0);
2988 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002989
2990 if (!str_restr && type->info.num.range) {
2991 str_restr = type->info.num.range->expr;
2992 }
2993 break;
2994 case LY_TYPE_STRING:
2995 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002996 local_umin = __UINT64_C(0);
2997 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002998
2999 if (!str_restr && type->info.str.length) {
3000 str_restr = type->info.str.length->expr;
3001 }
3002 break;
3003 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003004 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003005 }
3006
3007 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02003008 if (type->der) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003009 if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003010 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02003011 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003012 assert(!intv || (intv->kind == kind));
3013 }
3014
3015 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003016 /* we do not have any restriction, return superior ones */
3017 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003018 return EXIT_SUCCESS;
3019 }
3020
3021 /* adjust local min and max */
3022 if (intv) {
3023 tmp_intv = intv;
3024
3025 if (kind == 0) {
3026 local_umin = tmp_intv->value.uval.min;
3027 } else if (kind == 1) {
3028 local_smin = tmp_intv->value.sval.min;
3029 } else if (kind == 2) {
3030 local_fmin = tmp_intv->value.fval.min;
3031 }
3032
3033 while (tmp_intv->next) {
3034 tmp_intv = tmp_intv->next;
3035 }
3036
3037 if (kind == 0) {
3038 local_umax = tmp_intv->value.uval.max;
3039 } else if (kind == 1) {
3040 local_smax = tmp_intv->value.sval.max;
3041 } else if (kind == 2) {
3042 local_fmax = tmp_intv->value.fval.max;
3043 }
3044 }
3045
3046 /* finally parse our restriction */
3047 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003048 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003049 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003050 if (!tmp_local_intv) {
3051 assert(!local_intv);
3052 local_intv = malloc(sizeof *local_intv);
3053 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003054 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003055 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003056 tmp_local_intv = tmp_local_intv->next;
3057 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003058 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003059
3060 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003061 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003062 tmp_local_intv->next = NULL;
3063
3064 /* min */
3065 ptr = seg_ptr;
3066 while (isspace(ptr[0])) {
3067 ++ptr;
3068 }
3069 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3070 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003071 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003072 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003073 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003074 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003075 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003076 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003077 goto error;
3078 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003079 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003080 } else if (!strncmp(ptr, "min", 3)) {
3081 if (kind == 0) {
3082 tmp_local_intv->value.uval.min = local_umin;
3083 } else if (kind == 1) {
3084 tmp_local_intv->value.sval.min = local_smin;
3085 } else if (kind == 2) {
3086 tmp_local_intv->value.fval.min = local_fmin;
3087 }
3088
3089 ptr += 3;
3090 } else if (!strncmp(ptr, "max", 3)) {
3091 if (kind == 0) {
3092 tmp_local_intv->value.uval.min = local_umax;
3093 } else if (kind == 1) {
3094 tmp_local_intv->value.sval.min = local_smax;
3095 } else if (kind == 2) {
3096 tmp_local_intv->value.fval.min = local_fmax;
3097 }
3098
3099 ptr += 3;
3100 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003101 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003102 }
3103
3104 while (isspace(ptr[0])) {
3105 ptr++;
3106 }
3107
3108 /* no interval or interval */
3109 if ((ptr[0] == '|') || !ptr[0]) {
3110 if (kind == 0) {
3111 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
3112 } else if (kind == 1) {
3113 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
3114 } else if (kind == 2) {
3115 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
3116 }
3117 } else if (!strncmp(ptr, "..", 2)) {
3118 /* skip ".." */
3119 ptr += 2;
3120 while (isspace(ptr[0])) {
3121 ++ptr;
3122 }
3123
3124 /* max */
3125 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3126 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003127 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003128 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003129 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003130 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003131 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003132 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003133 goto error;
3134 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003135 }
3136 } else if (!strncmp(ptr, "max", 3)) {
3137 if (kind == 0) {
3138 tmp_local_intv->value.uval.max = local_umax;
3139 } else if (kind == 1) {
3140 tmp_local_intv->value.sval.max = local_smax;
3141 } else if (kind == 2) {
3142 tmp_local_intv->value.fval.max = local_fmax;
3143 }
3144 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003145 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003146 }
3147 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003148 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003149 }
3150
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003151 /* check min and max in correct order*/
3152 if (kind == 0) {
3153 /* current segment */
3154 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
3155 goto error;
3156 }
3157 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
3158 goto error;
3159 }
3160 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02003161 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003162 goto error;
3163 }
3164 } else if (kind == 1) {
3165 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
3166 goto error;
3167 }
3168 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
3169 goto error;
3170 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003171 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003172 goto error;
3173 }
3174 } else if (kind == 2) {
3175 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
3176 goto error;
3177 }
3178 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
3179 goto error;
3180 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003181 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003182 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003183 goto error;
3184 }
3185 }
3186
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003187 /* next segment (next OR) */
3188 seg_ptr = strchr(seg_ptr, '|');
3189 if (!seg_ptr) {
3190 break;
3191 }
3192 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003193 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003194 }
3195
3196 /* check local restrictions against superior ones */
3197 if (intv) {
3198 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003199 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003200
3201 while (tmp_local_intv && tmp_intv) {
3202 /* reuse local variables */
3203 if (kind == 0) {
3204 local_umin = tmp_local_intv->value.uval.min;
3205 local_umax = tmp_local_intv->value.uval.max;
3206
3207 /* it must be in this interval */
3208 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
3209 /* this interval is covered, next one */
3210 if (local_umax <= tmp_intv->value.uval.max) {
3211 tmp_local_intv = tmp_local_intv->next;
3212 continue;
3213 /* ascending order of restrictions -> fail */
3214 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003215 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003216 }
3217 }
3218 } else if (kind == 1) {
3219 local_smin = tmp_local_intv->value.sval.min;
3220 local_smax = tmp_local_intv->value.sval.max;
3221
3222 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
3223 if (local_smax <= tmp_intv->value.sval.max) {
3224 tmp_local_intv = tmp_local_intv->next;
3225 continue;
3226 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003227 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003228 }
3229 }
3230 } else if (kind == 2) {
3231 local_fmin = tmp_local_intv->value.fval.min;
3232 local_fmax = tmp_local_intv->value.fval.max;
3233
Michal Vasko4d1f0482016-09-19 14:35:06 +02003234 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02003235 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003236 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003237 tmp_local_intv = tmp_local_intv->next;
3238 continue;
3239 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003240 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003241 }
3242 }
3243 }
3244
3245 tmp_intv = tmp_intv->next;
3246 }
3247
3248 /* some interval left uncovered -> fail */
3249 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003250 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003251 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003252 }
3253
Michal Vaskoaeb51802016-04-11 10:58:47 +02003254 /* append the local intervals to all the intervals of the superior types, return it all */
3255 if (intv) {
3256 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
3257 tmp_intv->next = local_intv;
3258 } else {
3259 intv = local_intv;
3260 }
3261 *ret = intv;
3262
3263 return EXIT_SUCCESS;
3264
3265error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003266 while (intv) {
3267 tmp_intv = intv->next;
3268 free(intv);
3269 intv = tmp_intv;
3270 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003271 while (local_intv) {
3272 tmp_local_intv = local_intv->next;
3273 free(local_intv);
3274 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003275 }
3276
Michal Vaskoaeb51802016-04-11 10:58:47 +02003277 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003278}
3279
Michal Vasko730dfdf2015-08-11 14:48:05 +02003280/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003281 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3282 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003283 *
3284 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003285 * @param[in] mod_name Typedef name module name.
3286 * @param[in] module Main module.
3287 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003288 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003289 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003290 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003291 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003292int
Michal Vasko1e62a092015-12-01 12:27:20 +01003293resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3294 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003295{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003296 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003297 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003298 int tpdf_size;
3299
Michal Vasko1dca6882015-10-22 14:29:42 +02003300 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003301 /* no prefix, try built-in types */
3302 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003303 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003304 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003305 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003306 }
3307 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003308 }
3309 }
3310 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003311 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003312 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003313 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003314 }
3315 }
3316
Michal Vasko1dca6882015-10-22 14:29:42 +02003317 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003318 /* search in local typedefs */
3319 while (parent) {
3320 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003321 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003322 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3323 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003324 break;
3325
Radek Krejci76512572015-08-04 09:47:08 +02003326 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003327 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3328 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003329 break;
3330
Radek Krejci76512572015-08-04 09:47:08 +02003331 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003332 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3333 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003334 break;
3335
Radek Krejci76512572015-08-04 09:47:08 +02003336 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003337 case LYS_ACTION:
3338 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3339 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003340 break;
3341
Radek Krejci76512572015-08-04 09:47:08 +02003342 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003343 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3344 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003345 break;
3346
Radek Krejci76512572015-08-04 09:47:08 +02003347 case LYS_INPUT:
3348 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003349 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3350 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003351 break;
3352
3353 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003354 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003355 continue;
3356 }
3357
3358 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003359 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003360 match = &tpdf[i];
3361 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003362 }
3363 }
3364
Michal Vaskodcf98e62016-05-05 17:53:53 +02003365 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003366 }
Radek Krejcic071c542016-01-27 14:57:51 +01003367 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003368 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003369 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003370 if (!module) {
3371 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003372 }
3373 }
3374
3375 /* search in top level typedefs */
3376 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003377 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003378 match = &module->tpdf[i];
3379 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003380 }
3381 }
3382
3383 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003384 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003385 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003386 if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003387 match = &module->inc[i].submodule->tpdf[j];
3388 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003389 }
3390 }
3391 }
3392
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003393 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003394
3395check_leafref:
3396 if (ret) {
3397 *ret = match;
3398 }
3399 if (match->type.base == LY_TYPE_LEAFREF) {
3400 while (!match->type.info.lref.path) {
3401 match = match->type.der;
3402 assert(match);
3403 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003404 }
3405 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003406}
3407
Michal Vasko1dca6882015-10-22 14:29:42 +02003408/**
3409 * @brief Check the default \p value of the \p type. Logs directly.
3410 *
3411 * @param[in] type Type definition to use.
3412 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003413 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003414 *
3415 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3416 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003417static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003418check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003419{
Radek Krejcibad2f172016-08-02 11:04:15 +02003420 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003421 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003422 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003423 char *s;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003424 int ret = EXIT_SUCCESS, r;
Michal Vasko53b7da02018-02-13 15:28:42 +01003425 struct ly_ctx *ctx = module->ctx;
Michal Vasko1dca6882015-10-22 14:29:42 +02003426
Radek Krejci51673202016-11-01 17:00:32 +01003427 assert(value);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003428 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003429
Radek Krejcic13db382016-08-16 10:52:42 +02003430 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003431 /* the type was not resolved yet, nothing to do for now */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003432 ret = EXIT_FAILURE;
3433 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003434 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003435 /* do not check defaults in not implemented module's data */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003436 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003437 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003438 /* identityrefs are checked when instantiated in data instead of typedef,
3439 * but in typedef the value has to be modified to include the prefix */
3440 if (*value) {
3441 if (strchr(*value, ':')) {
3442 dflt = transform_schema2json(module, *value);
3443 } else {
3444 /* default prefix of the module where the typedef is defined */
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003445 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
3446 LOGMEM(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003447 ret = -1;
3448 goto cleanup;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003449 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003450 dflt = lydict_insert_zc(ctx, s);
Radek Krejci9e6af732017-04-27 14:40:25 +02003451 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003452 lydict_remove(ctx, *value);
Radek Krejci9e6af732017-04-27 14:40:25 +02003453 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003454 dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003455 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003456 goto cleanup;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003457 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3458 /* leafref in typedef cannot be checked */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003459 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003460 }
3461
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003462 dflt = lydict_insert(ctx, *value, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003463 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003464 /* we do not have a new default value, so is there any to check even, in some base type? */
3465 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3466 if (base_tpdf->dflt) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003467 dflt = lydict_insert(ctx, base_tpdf->dflt, 0);
Michal Vasko478c4652016-07-21 12:55:01 +02003468 break;
3469 }
3470 }
3471
Radek Krejci51673202016-11-01 17:00:32 +01003472 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003473 /* no default value, nothing to check, all is well */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003474 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003475 }
3476
3477 /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */
3478 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003479 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003480 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003481 goto cleanup;
Radek Krejci9e6af732017-04-27 14:40:25 +02003482 } else {
3483 /* check the default value from typedef, but use also the typedef's module
3484 * due to possible searching in imported modules which is expected in
3485 * typedef's module instead of module where the typedef is used */
3486 module = base_tpdf->module;
3487 }
3488 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003489 case LY_TYPE_INST:
3490 case LY_TYPE_LEAFREF:
3491 case LY_TYPE_BOOL:
3492 case LY_TYPE_EMPTY:
3493 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003494 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003495 case LY_TYPE_BITS:
3496 /* the default value must match the restricted list of values, if the type was restricted */
3497 if (type->info.bits.count) {
3498 break;
3499 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003500 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003501 case LY_TYPE_ENUM:
3502 /* the default value must match the restricted list of values, if the type was restricted */
3503 if (type->info.enums.count) {
3504 break;
3505 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003506 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003507 case LY_TYPE_DEC64:
3508 if (type->info.dec64.range) {
3509 break;
3510 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003511 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003512 case LY_TYPE_BINARY:
3513 if (type->info.binary.length) {
3514 break;
3515 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003516 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003517 case LY_TYPE_INT8:
3518 case LY_TYPE_INT16:
3519 case LY_TYPE_INT32:
3520 case LY_TYPE_INT64:
3521 case LY_TYPE_UINT8:
3522 case LY_TYPE_UINT16:
3523 case LY_TYPE_UINT32:
3524 case LY_TYPE_UINT64:
3525 if (type->info.num.range) {
3526 break;
3527 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003528 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003529 case LY_TYPE_STRING:
3530 if (type->info.str.length || type->info.str.patterns) {
3531 break;
3532 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003533 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003534 case LY_TYPE_UNION:
3535 /* way too much trouble learning whether we need to check the default again, so just do it */
3536 break;
3537 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01003538 LOGINT(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003539 ret = -1;
3540 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003541 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003542 } else if (type->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003543 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3544 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003545 ret = -1;
3546 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003547 }
3548
Michal Vasko1dca6882015-10-22 14:29:42 +02003549 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003550 memset(&node, 0, sizeof node);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003551 node.value_str = lydict_insert(ctx, dflt, 0);
Michal Vasko1dca6882015-10-22 14:29:42 +02003552 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003553
3554 if (tpdf) {
3555 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003556 if (!node.schema) {
3557 LOGMEM(ctx);
3558 ret = -1;
3559 goto cleanup;
3560 }
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003561 r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003562 if (r == -1) {
3563 LOGMEM(ctx);
3564 ret = -1;
3565 goto cleanup;
3566 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003567 node.schema->module = module;
3568 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3569 } else {
3570 node.schema = (struct lys_node *)type->parent;
3571 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003572
Radek Krejci37b756f2016-01-18 10:15:03 +01003573 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003574 if (!type->info.lref.target) {
3575 ret = EXIT_FAILURE;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003576 goto cleanup;
Michal Vasko1dca6882015-10-22 14:29:42 +02003577 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003578 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003579 if (!ret) {
3580 /* adopt possibly changed default value to its canonical form */
3581 if (*value) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003582 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003583 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003584 dflt = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003585 }
3586 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003587 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02003588 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1, 0)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003589 /* possible forward reference */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003590 ret = EXIT_FAILURE;
Radek Krejcibad2f172016-08-02 11:04:15 +02003591 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003592 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003593 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3594 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3595 /* we have refined bits/enums */
Michal Vasko53b7da02018-02-13 15:28:42 +01003596 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
Radek Krejcibad2f172016-08-02 11:04:15 +02003597 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003598 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003599 }
3600 }
Radek Krejci51673202016-11-01 17:00:32 +01003601 } else {
3602 /* success - adopt canonical form from the node into the default value */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003603 if (!ly_strequal(dflt, node.value_str, 1)) {
Radek Krejci51673202016-11-01 17:00:32 +01003604 /* this can happen only if we have non-inherited default value,
3605 * inherited default values are already in canonical form */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003606 assert(ly_strequal(dflt, *value, 1));
3607
3608 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003609 *value = node.value_str;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003610 node.value_str = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003611 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003612 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003613 }
3614
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003615cleanup:
Michal Vasko70bf8e52018-03-26 11:32:33 +02003616 lyd_free_value(node.value, node.value_type, node.value_flags, type);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003617 lydict_remove(ctx, node.value_str);
3618 if (tpdf && node.schema) {
Michal Vasko31a2d322018-01-12 13:36:12 +01003619 free((char *)node.schema->name);
3620 free(node.schema);
3621 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003622 lydict_remove(ctx, dflt);
Michal Vasko1dca6882015-10-22 14:29:42 +02003623
3624 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003625}
3626
Michal Vasko730dfdf2015-08-11 14:48:05 +02003627/**
3628 * @brief Check a key for mandatory attributes. Logs directly.
3629 *
3630 * @param[in] key The key to check.
3631 * @param[in] flags What flags to check.
3632 * @param[in] list The list of all the keys.
3633 * @param[in] index Index of the key in the key list.
3634 * @param[in] name The name of the keys.
3635 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003636 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003637 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003638 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003639static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003640check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003641{
Radek Krejciadb57612016-02-16 13:34:34 +01003642 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003643 char *dup = NULL;
3644 int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01003645 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003646
3647 /* existence */
3648 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003649 if (name[len] != '\0') {
3650 dup = strdup(name);
Michal Vasko53b7da02018-02-13 15:28:42 +01003651 LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003652 dup[len] = '\0';
3653 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003654 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003655 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003656 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003657 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003658 }
3659
3660 /* uniqueness */
3661 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003662 if (key == list->keys[j]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003663 LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003664 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003665 }
3666 }
3667
3668 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003669 if (key->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003670 LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003671 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003672 }
3673
3674 /* type of the leaf is not built-in empty */
Radek Krejci13fde922018-05-16 10:45:58 +02003675 if (key->type.base == LY_TYPE_EMPTY && key->module->version < LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003676 LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003677 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003678 }
3679
3680 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003681 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003682 LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003683 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003684 }
3685
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003686 /* key is not placed from augment */
3687 if (key->parent->nodetype == LYS_AUGMENT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003688 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3689 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003690 return -1;
3691 }
3692
Radek Krejci3f21ada2016-08-01 13:34:31 +02003693 /* key is not when/if-feature -conditional */
3694 j = 0;
3695 if (key->when || (key->iffeature_size && (j = 1))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003696 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3697 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003698 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003699 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003700 }
3701
Michal Vasko0b85aa82016-03-07 14:37:43 +01003702 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003703}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003704
3705/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003706 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003707 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003708 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003709 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003710 *
3711 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3712 */
3713int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003714resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003715{
Radek Krejci581ce772015-11-10 17:22:40 +01003716 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003717 const struct lys_node *leaf = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01003718 struct ly_ctx *ctx = parent->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003719
Michal Vaskodc300b02017-04-07 14:09:20 +02003720 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003721 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003722 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003723 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003724 if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003725 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003726 } else if (rc == -2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003727 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003728 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003729 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003730 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003731 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3732 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003733 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003734 }
Radek Krejci581ce772015-11-10 17:22:40 +01003735 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003736 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003737 if (leaf->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003738 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3739 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003740 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003741 }
3742
Radek Krejcicf509982015-12-15 09:22:44 +01003743 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003744 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3745 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003746 return -1;
3747 }
3748
Radek Krejcid09d1a52016-08-11 14:05:45 +02003749 /* check that all unique's targets are of the same config type */
3750 if (*trg_type) {
3751 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003752 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3753 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003754 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3755 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3756 return -1;
3757 }
3758 } else {
3759 /* first unique */
3760 if (leaf->flags & LYS_CONFIG_W) {
3761 *trg_type = 1;
3762 } else {
3763 *trg_type = 2;
3764 }
3765 }
3766
Radek Krejcica7efb72016-01-18 13:06:01 +01003767 /* set leaf's unique flag */
3768 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3769
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003770 return EXIT_SUCCESS;
3771
3772error:
3773
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003774 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003775}
3776
Radek Krejci0c0086a2016-03-24 15:20:28 +01003777void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003778unres_data_del(struct unres_data *unres, uint32_t i)
3779{
3780 /* there are items after the one deleted */
3781 if (i+1 < unres->count) {
3782 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003783 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003784
3785 /* deleting the last item */
3786 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003787 free(unres->node);
3788 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003789 }
3790
3791 /* if there are no items after and it is not the last one, just move the counter */
3792 --unres->count;
3793}
3794
Michal Vasko0491ab32015-08-19 14:28:29 +02003795/**
3796 * @brief Resolve (find) a data node from a specific module. Does not log.
3797 *
3798 * @param[in] mod Module to search in.
3799 * @param[in] name Name of the data node.
3800 * @param[in] nam_len Length of the name.
3801 * @param[in] start Data node to start the search from.
3802 * @param[in,out] parents Resolved nodes. If there are some parents,
3803 * they are replaced (!!) with the resolvents.
3804 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003805 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003806 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003807static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003808resolve_data(const struct lys_module *mod, const char *name, int nam_len, struct lyd_node *start, struct unres_data *parents)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003809{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003810 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003811 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003812 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003813
Michal Vasko23b61ec2015-08-19 11:19:50 +02003814 if (!parents->count) {
3815 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003816 parents->node = malloc(sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003817 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003818 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003819 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003820 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003821 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003822 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003823 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003824 continue;
3825 }
3826 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003827 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003828 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003829 && node->schema->name[nam_len] == '\0') {
3830 /* matching target */
3831 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003832 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003833 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003834 flag = 1;
3835 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003836 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003837 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003838 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003839 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003840 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003841 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003842 }
3843 }
3844 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003845
3846 if (!flag) {
3847 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003848 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003849 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003850 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003851 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003852 }
3853
Michal Vasko0491ab32015-08-19 14:28:29 +02003854 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003855}
3856
Michal Vaskoe27516a2016-10-10 17:55:31 +00003857static int
Michal Vasko1c007172017-03-10 10:20:44 +01003858resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path)
Michal Vaskoe27516a2016-10-10 17:55:31 +00003859{
3860 int dep1, dep2;
3861 const struct lys_node *node;
3862
3863 if (lys_parent(op_node)) {
3864 /* inner operation (notif/action) */
3865 if (abs_path) {
3866 return 1;
3867 } else {
3868 /* compare depth of both nodes */
3869 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3870 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3871 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3872 return 1;
3873 }
3874 }
3875 } else {
3876 /* top-level operation (notif/rpc) */
3877 if (op_node != first_node) {
3878 return 1;
3879 }
3880 }
3881
3882 return 0;
3883}
3884
Michal Vasko730dfdf2015-08-11 14:48:05 +02003885/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003886 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003887 *
Michal Vaskobb211122015-08-19 14:03:11 +02003888 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003889 * @param[in] context_node Predicate context node (where the predicate is placed).
3890 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003891 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003892 *
Michal Vasko184521f2015-09-24 13:14:26 +02003893 * @return 0 on forward reference, otherwise the number
3894 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003895 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003896 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003897static int
Michal Vasko1c007172017-03-10 10:20:44 +01003898resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3899 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003900{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003901 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003902 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003903 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003904 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3905 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003906 struct ly_ctx *ctx = context_node->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003907
3908 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003909 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003910 &pke_len, &has_predicate)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003911 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003912 return -parsed+i;
3913 }
3914 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003915 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003916
Michal Vasko58090902015-08-13 14:04:15 +02003917 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003918 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003919 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003920 } else {
3921 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003922 }
Michal Vaskobb520442017-05-23 10:55:18 +02003923 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003924 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003925 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003926 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003927 }
3928
3929 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003930 dest_parent_times = 0;
3931 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003932 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3933 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003934 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003935 return -parsed;
3936 }
3937 pke_parsed += i;
3938
Radek Krejciadb57612016-02-16 13:34:34 +01003939 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003940 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3941 && !((struct lys_node_augment *)dst_node->parent)->target) {
3942 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003943 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003944 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3945 return 0;
3946 }
3947
Michal Vaskofbaead72016-10-07 10:54:48 +02003948 /* path is supposed to be evaluated in data tree, so we have to skip
3949 * all schema nodes that cannot be instantiated in data tree */
3950 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003951 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003952 dst_node = lys_parent(dst_node));
3953
Michal Vasko1f76a282015-08-04 16:16:53 +02003954 if (!dst_node) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003955 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003956 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003957 }
3958 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003959 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003960 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003961 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003962 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003963 } else {
3964 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003965 }
Michal Vaskobb520442017-05-23 10:55:18 +02003966 rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003967 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003968 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003969 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003970 }
3971
Michal Vaskoe27516a2016-10-10 17:55:31 +00003972 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003973 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003974 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003975 }
3976 first_iter = 0;
3977 }
3978
Michal Vasko1f76a282015-08-04 16:16:53 +02003979 if (pke_len == pke_parsed) {
3980 break;
3981 }
3982
Michal Vaskobb520442017-05-23 10:55:18 +02003983 if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
Michal Vasko1f76a282015-08-04 16:16:53 +02003984 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003985 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02003986 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003987 return -parsed;
3988 }
3989 pke_parsed += i;
3990 }
3991
3992 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003993 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003994 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed);
Michal Vasko53b7da02018-02-13 15:28:42 +01003995 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003996 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003997 return -parsed;
3998 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003999 } while (has_predicate);
4000
4001 return parsed;
4002}
4003
Michal Vasko74083ec2018-06-15 10:00:12 +02004004static int
4005check_leafref_features(struct lys_type *type)
4006{
4007 struct lys_node *iter;
4008 struct ly_set *src_parents, *trg_parents, *features;
4009 struct lys_node_augment *aug;
4010 struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx;
4011 unsigned int i, j, size, x;
4012 int ret = EXIT_SUCCESS;
4013
4014 assert(type->parent);
4015
4016 src_parents = ly_set_new();
4017 trg_parents = ly_set_new();
4018 features = ly_set_new();
4019
4020 /* get parents chain of source (leafref) */
4021 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
4022 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4023 continue;
4024 }
4025 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4026 aug = (struct lys_node_augment *)iter->parent;
4027 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4028 /* unresolved augment, wait until it's resolved */
4029 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4030 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4031 ret = EXIT_FAILURE;
4032 goto cleanup;
4033 }
4034 }
4035 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
4036 }
4037 /* get parents chain of target */
4038 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
4039 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4040 continue;
4041 }
4042 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4043 aug = (struct lys_node_augment *)iter->parent;
4044 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4045 /* unresolved augment, wait until it's resolved */
4046 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4047 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4048 ret = EXIT_FAILURE;
4049 goto cleanup;
4050 }
4051 }
4052 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
4053 }
4054
4055 /* compare the features used in if-feature statements in the rest of both
4056 * chains of parents. The set of features used for target must be a subset
4057 * of features used for the leafref. This is not a perfect, we should compare
4058 * the truth tables but it could require too much resources, so we simplify that */
4059 for (i = 0; i < src_parents->number; i++) {
4060 iter = src_parents->set.s[i]; /* shortcut */
4061 if (!iter->iffeature_size) {
4062 continue;
4063 }
4064 for (j = 0; j < iter->iffeature_size; j++) {
4065 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4066 for (; size; size--) {
4067 if (!iter->iffeature[j].features[size - 1]) {
4068 /* not yet resolved feature, postpone this check */
4069 ret = EXIT_FAILURE;
4070 goto cleanup;
4071 }
4072 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
4073 }
4074 }
4075 }
4076 x = features->number;
4077 for (i = 0; i < trg_parents->number; i++) {
4078 iter = trg_parents->set.s[i]; /* shortcut */
4079 if (!iter->iffeature_size) {
4080 continue;
4081 }
4082 for (j = 0; j < iter->iffeature_size; j++) {
4083 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4084 for (; size; size--) {
4085 if (!iter->iffeature[j].features[size - 1]) {
4086 /* not yet resolved feature, postpone this check */
4087 ret = EXIT_FAILURE;
4088 goto cleanup;
4089 }
4090 if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
4091 /* the feature is not present in features set of target's parents chain */
4092 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
4093 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
4094 "Leafref is not conditional based on \"%s\" feature as its target.",
4095 iter->iffeature[j].features[size - 1]->name);
4096 ret = -1;
4097 goto cleanup;
4098 }
4099 }
4100 }
4101 }
4102
4103cleanup:
4104 ly_set_free(features);
4105 ly_set_free(src_parents);
4106 ly_set_free(trg_parents);
4107
4108 return ret;
4109}
4110
Michal Vasko730dfdf2015-08-11 14:48:05 +02004111/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004112 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004113 *
Michal Vaskobb211122015-08-19 14:03:11 +02004114 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004115 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004116 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004117 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004118 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004119 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004120static int
Michal Vasko74083ec2018-06-15 10:00:12 +02004121resolve_schema_leafref(struct lys_type *type, struct lys_node *parent, struct unres_schema *unres)
Michal Vasko1f76a282015-08-04 16:16:53 +02004122{
Michal Vaskocb45f472018-02-12 10:47:42 +01004123 const struct lys_node *node, *op_node = NULL, *tmp_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02004124 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02004125 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02004126 const char *id, *prefix, *name;
4127 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskocb45f472018-02-12 10:47:42 +01004128 int i, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01004129 struct ly_ctx *ctx = parent->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02004130
Michal Vasko74083ec2018-06-15 10:00:12 +02004131 if (!type->info.lref.target) {
4132 first_iter = 1;
4133 parent_times = 0;
4134 id = type->info.lref.path;
Michal Vasko1f76a282015-08-04 16:16:53 +02004135
Michal Vasko74083ec2018-06-15 10:00:12 +02004136 /* find operation schema we are in */
4137 for (op_node = lys_parent(parent);
4138 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
4139 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02004140
Michal Vasko74083ec2018-06-15 10:00:12 +02004141 cur_module = lys_node_module(parent);
4142 do {
4143 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
4144 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vaskobb520442017-05-23 10:55:18 +02004145 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004146 }
4147 id += i;
Michal Vasko74083ec2018-06-15 10:00:12 +02004148
4149 /* get the current module */
4150 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
4151 if (!tmp_mod) {
4152 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4153 return EXIT_FAILURE;
4154 }
4155 last_aug = NULL;
4156
4157 if (first_iter) {
4158 if (parent_times == -1) {
4159 /* use module data */
4160 node = NULL;
4161
4162 } else if (parent_times > 0) {
4163 /* we are looking for the right parent */
4164 for (i = 0, node = parent; i < parent_times; i++) {
4165 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
4166 && !((struct lys_node_augment *)node->parent)->target) {
4167 /* we are in an unresolved augment, cannot evaluate */
4168 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent,
4169 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path);
4170 return EXIT_FAILURE;
4171 }
4172
4173 /* path is supposed to be evaluated in data tree, so we have to skip
4174 * all schema nodes that cannot be instantiated in data tree */
4175 for (node = lys_parent(node);
4176 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
4177 node = lys_parent(node));
4178
4179 if (!node) {
4180 if (i == parent_times - 1) {
4181 /* top-level */
4182 break;
4183 }
4184
4185 /* higher than top-level */
4186 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4187 return EXIT_FAILURE;
4188 }
4189 }
4190 } else {
4191 LOGINT(ctx);
4192 return -1;
4193 }
4194 }
4195
4196 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
4197 * - useless to search for that in augments) */
4198 if (!tmp_mod->implemented && node) {
4199 get_next_augment:
4200 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
4201 }
4202
4203 tmp_parent = (last_aug ? (struct lys_node *)last_aug : node);
4204 node = NULL;
4205 while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) {
4206 if (lys_node_module(node) != lys_main_module(tmp_mod)) {
4207 continue;
4208 }
4209 if (strncmp(node->name, name, nam_len) || node->name[nam_len]) {
4210 continue;
4211 }
4212 /* match */
4213 break;
4214 }
4215 if (!node) {
4216 if (last_aug) {
4217 /* restore the correct augment target */
4218 node = last_aug->target;
4219 goto get_next_augment;
4220 }
4221 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4222 return EXIT_FAILURE;
4223 }
4224
4225 if (first_iter) {
4226 /* set external dependency flag, we can decide based on the first found node */
4227 if (op_node && parent_times &&
4228 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
4229 parent->flags |= LYS_LEAFREF_DEP;
4230 }
4231 first_iter = 0;
4232 }
4233
4234 if (has_predicate) {
4235 /* we have predicate, so the current result must be list */
4236 if (node->nodetype != LYS_LIST) {
4237 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4238 return -1;
4239 }
4240
4241 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
4242 if (!i) {
4243 return EXIT_FAILURE;
4244 } else if (i < 0) {
4245 return -1;
4246 }
4247 id += i;
4248 has_predicate = 0;
4249 }
4250 } while (id[0]);
4251
4252 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
4253 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
4254 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4255 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path);
4256 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004257 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004258
Michal Vasko74083ec2018-06-15 10:00:12 +02004259 /* check status */
4260 if (lyp_check_status(parent->flags, parent->module, parent->name,
4261 node->flags, node->module, node->name, node)) {
4262 return -1;
4263 }
4264
4265 /* assign */
4266 type->info.lref.target = (struct lys_node_leaf *)node;
Radek Krejcib1c12512015-08-11 11:22:04 +02004267 }
4268
Michal Vasko74083ec2018-06-15 10:00:12 +02004269 /* as the last thing traverse this leafref and make targets on the path implemented */
4270 if (lys_node_module(parent)->implemented) {
4271 /* make all the modules in the path implemented */
4272 for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) {
4273 if (!lys_node_module(node)->implemented) {
4274 lys_node_module(node)->implemented = 1;
4275 if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4276 return -1;
4277 }
4278 }
4279 }
4280
4281 /* store the backlink from leafref target */
4282 if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
4283 return -1;
4284 }
Radek Krejcicf509982015-12-15 09:22:44 +01004285 }
4286
Michal Vasko74083ec2018-06-15 10:00:12 +02004287 /* check if leafref and its target are under common if-features */
4288 return check_leafref_features(type);
Michal Vasko1f76a282015-08-04 16:16:53 +02004289}
4290
Michal Vasko730dfdf2015-08-11 14:48:05 +02004291/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02004292 * @brief Compare 2 data node values.
4293 *
4294 * Comparison performed on canonical forms, the first value
4295 * is first transformed into canonical form.
4296 *
4297 * @param[in] node Leaf/leaf-list with these values.
4298 * @param[in] noncan_val Non-canonical value.
4299 * @param[in] noncan_val_len Length of \p noncal_val.
4300 * @param[in] can_val Canonical value.
4301 * @return 1 if equal, 0 if not, -1 on error (logged).
4302 */
4303static int
4304valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
4305{
4306 int ret;
4307 struct lyd_node_leaf_list leaf;
4308 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
4309
4310 /* dummy leaf */
4311 memset(&leaf, 0, sizeof leaf);
4312 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
4313
4314repeat:
4315 leaf.value_type = sleaf->type.base;
4316 leaf.schema = node;
4317
4318 if (leaf.value_type == LY_TYPE_LEAFREF) {
4319 if (!sleaf->type.info.lref.target) {
4320 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01004321 LOGINT(node->module->ctx);
Michal Vasko718ecdd2017-10-03 14:12:39 +02004322 ret = -1;
4323 goto finish;
4324 }
4325 sleaf = sleaf->type.info.lref.target;
4326 goto repeat;
4327 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02004328 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02004329 ret = -1;
4330 goto finish;
4331 }
4332 }
4333
4334 if (!strcmp(leaf.value_str, can_val)) {
4335 ret = 1;
4336 } else {
4337 ret = 0;
4338 }
4339
4340finish:
4341 lydict_remove(node->module->ctx, leaf.value_str);
4342 return ret;
4343}
4344
4345/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004346 * @brief Resolve instance-identifier predicate in JSON data format.
4347 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004348 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02004349 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02004350 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004351 * @param[in,out] node Node matching the restriction without
4352 * the predicate. If it does not satisfy the predicate,
4353 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004354 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004355 * @return Number of characters successfully parsed,
4356 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004357 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004358static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004359resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct lyd_node **node, int cur_idx)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004360{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004361 /* ... /node[key=value] ... */
4362 struct lyd_node_leaf_list *key;
4363 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02004364 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004365 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004366 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vasko53b7da02018-02-13 15:28:42 +01004367 struct ly_ctx *ctx = prev_mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004368
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004369 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02004370
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004371 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004372 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004373 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
4374 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004375 }
4376 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004377
Michal Vasko88850b72017-10-02 13:13:21 +02004378 if (!(*node)) {
4379 /* just parse it all */
4380 continue;
4381 }
4382
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004383 /* target */
4384 if (name[0] == '.') {
4385 /* leaf-list value */
4386 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004387 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004388 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4389 parsed = -1;
4390 goto cleanup;
4391 }
4392
4393 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004394 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004395 *node = NULL;
4396 goto cleanup;
4397 }
4398
4399 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004400 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004401
4402 /* keyless list position */
4403 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004404 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004405 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4406 parsed = -1;
4407 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004408 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004409
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004410 if (((struct lys_node_list *)(*node)->schema)->keys) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004411 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004412 (*node)->schema->name);
4413 parsed = -1;
4414 goto cleanup;
4415 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004416
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004417 /* check the index */
4418 if (atoi(name) != cur_idx) {
4419 *node = NULL;
4420 goto cleanup;
4421 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004422
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004423 } else {
4424 /* list key value */
4425 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004426 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004427 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4428 parsed = -1;
4429 goto cleanup;
4430 }
4431 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004432
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004433 /* prepare key array */
4434 if (!list_keys) {
4435 list_keys = malloc(slist->keys_size * sizeof *list_keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01004436 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004437 for (i = 0; i < slist->keys_size; ++i) {
4438 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004439 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004440 }
4441
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004442 /* find the schema key leaf */
4443 for (i = 0; i < slist->keys_size; ++i) {
4444 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4445 break;
4446 }
4447 }
4448 if (i == slist->keys_size) {
4449 /* this list has no such key */
Michal Vasko53b7da02018-02-13 15:28:42 +01004450 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004451 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4452 parsed = -1;
4453 goto cleanup;
4454 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004455
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004456 /* check module */
4457 if (model) {
4458 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004459 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004460 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4461 parsed = -1;
4462 goto cleanup;
4463 }
4464 } else {
4465 if (list_keys[i]->module != prev_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004466 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004467 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4468 parsed = -1;
4469 goto cleanup;
4470 }
4471 }
4472
4473 /* find the actual data key */
4474 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4475 if (key->schema == (struct lys_node *)list_keys[i]) {
4476 break;
4477 }
4478 }
4479 if (!key) {
4480 /* list instance is missing a key? definitely should not happen */
Michal Vasko53b7da02018-02-13 15:28:42 +01004481 LOGINT(ctx);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004482 parsed = -1;
4483 goto cleanup;
4484 }
4485
4486 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004487 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004488 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004489 /* we still want to parse the whole predicate */
4490 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004491 }
4492
4493 /* everything is fine, mark this key as resolved */
4494 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004495 }
4496 } while (has_predicate);
4497
Michal Vaskob2f40be2016-09-08 16:03:48 +02004498 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004499 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004500 for (i = 0; i < slist->keys_size; ++i) {
4501 if (list_keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004502 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004503 parsed = -1;
4504 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004505 }
4506 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004507 }
4508
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004509cleanup:
4510 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004511 return parsed;
4512}
4513
Michal Vasko895c11f2018-03-12 11:35:58 +01004514static int
4515check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004516{
Michal Vasko0b963112017-08-11 12:45:36 +02004517 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004518 struct lyxp_set set;
Michal Vasko895c11f2018-03-12 11:35:58 +01004519 enum int_log_opts prev_ilo;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004520
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004521 if (check_place) {
4522 parent = node;
4523 while (parent) {
4524 if (parent->nodetype == LYS_GROUPING) {
4525 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004526 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004527 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004528 if (parent->nodetype == LYS_AUGMENT) {
4529 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004530 /* unresolved augment, skip for now (will be checked later) */
4531 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004532 } else {
4533 parent = ((struct lys_node_augment *)parent)->target;
4534 continue;
4535 }
4536 }
4537 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004538 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004539 }
4540
Michal Vasko895c11f2018-03-12 11:35:58 +01004541 memset(&set, 0, sizeof set);
Michal Vasko9e635ac2016-10-17 11:44:09 +02004542
Michal Vasko895c11f2018-03-12 11:35:58 +01004543 /* produce just warnings */
4544 ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL);
4545 lyxp_node_atomize(node, &set, 1);
4546 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
4547
4548 if (set.val.snodes) {
4549 free(set.val.snodes);
4550 }
4551 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004552}
4553
Radek Krejcif71f48f2016-10-25 16:37:24 +02004554static int
4555check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4556{
Radek Krejcidce5f972017-09-12 15:47:49 +02004557 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004558
4559 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004560 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4561 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004562 LOGVAL(leaf->module->ctx, LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.",
Radek Krejcif71f48f2016-10-25 16:37:24 +02004563 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4564 return -1;
4565 }
4566 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4567 * of leafref resolving (lys_leaf_add_leafref_target()) */
4568 } else if (type->base == LY_TYPE_UNION) {
4569 for (i = 0; i < type->info.uni.count; i++) {
4570 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4571 return -1;
4572 }
4573 }
4574 }
4575 return 0;
4576}
4577
Michal Vasko9e635ac2016-10-17 11:44:09 +02004578/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004579 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004580 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004581 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004582 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004583 * @param[in] clear Flag to clear all config flags if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC.
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004584 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004585 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004586 *
4587 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004588 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004589int
4590inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004591{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004592 struct lys_node_leaf *leaf;
Michal Vasko53b7da02018-02-13 15:28:42 +01004593 struct ly_ctx *ctx;
4594
4595 if (!node) {
4596 return 0;
4597 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004598
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004599 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Michal Vasko53b7da02018-02-13 15:28:42 +01004600 ctx = node->module->ctx;
4601
Radek Krejci1d82ef62015-08-07 14:44:40 +02004602 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004603 if (clear) {
4604 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004605 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004606 } else {
4607 if (node->flags & LYS_CONFIG_SET) {
4608 /* skip nodes with an explicit config value */
4609 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004610 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config");
4611 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004612 return -1;
4613 }
4614 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004615 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004616
4617 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4618 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4619 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004620 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4621 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004622 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
Michal Vaskoe022a562016-09-27 14:24:15 +02004623 return -1;
4624 }
4625 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004626 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004627 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004628 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004629 return -1;
4630 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004631 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4632 leaf = (struct lys_node_leaf *)node;
4633 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004634 return -1;
4635 }
4636 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004637 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004638
4639 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004640}
4641
Michal Vasko730dfdf2015-08-11 14:48:05 +02004642/**
Michal Vasko7178e692016-02-12 15:58:05 +01004643 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004644 *
Michal Vaskobb211122015-08-19 14:03:11 +02004645 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004646 * @param[in] uses Parent where to start the search in. If set, uses augment, if not, standalone augment.
Radek Krejcib3142312016-11-09 11:04:12 +01004647 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004648 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004649 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004650 */
Michal Vasko7178e692016-02-12 15:58:05 +01004651static int
Michal Vasko97234262018-02-01 09:53:01 +01004652resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004653{
Michal Vasko44ab1462017-05-18 13:18:36 +02004654 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004655 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004656 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004657 struct ly_set *set;
Michal Vasko53b7da02018-02-13 15:28:42 +01004658 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004659
Michal Vasko2ef7db62017-06-12 09:24:02 +02004660 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004661 mod = lys_main_module(aug->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01004662 ctx = mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004663
Michal Vaskobb520442017-05-23 10:55:18 +02004664 /* set it as not applied for now */
4665 aug->flags |= LYS_NOTAPPLIED;
4666
Michal Vasko2ef7db62017-06-12 09:24:02 +02004667 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004668 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004669 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004670 rc = resolve_schema_nodeid(aug->target_name, uses, (uses ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004671 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004672 LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004673 return -1;
4674 }
Michal Vasko50576712017-07-28 12:28:33 +02004675 if (!set) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004676 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004677 return EXIT_FAILURE;
4678 }
Michal Vasko50576712017-07-28 12:28:33 +02004679 aug->target = set->set.s[0];
4680 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004681 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004682
Michal Vasko74083ec2018-06-15 10:00:12 +02004683 /* make this module implemented if the target module is (if the target is in an unimplemented module,
4684 * it is fine because when we will be making that module implemented, its augment will be applied
4685 * and that augment target module made implemented, recursively) */
4686 if (mod->implemented && !lys_node_module(aug->target)->implemented) {
4687 lys_node_module(aug->target)->implemented = 1;
4688 if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4689 return -1;
4690 }
4691 }
4692
Michal Vaskod58d5962016-03-02 14:29:41 +01004693 /* check for mandatory nodes - if the target node is in another module
4694 * the added nodes cannot be mandatory
4695 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004696 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4697 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004698 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004699 }
4700
Michal Vasko07e89ef2016-03-03 13:28:57 +01004701 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004702 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004703 LY_TREE_FOR(aug->child, sub) {
4704 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4705 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004706 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4707 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004708 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004709 return -1;
4710 }
4711 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004712 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004713 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004714 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004715 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4716 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004717 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004718 return -1;
4719 }
4720 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004721 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004722 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004723 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004724 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4725 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004726 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004727 return -1;
4728 }
4729 }
4730 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004731 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
4732 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004733 return -1;
4734 }
4735
Radek Krejcic071c542016-01-27 14:57:51 +01004736 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004737 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004738 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004739 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004740 }
4741 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004742
Michal Vasko44ab1462017-05-18 13:18:36 +02004743 if (!aug->child) {
4744 /* empty augment, nothing to connect, but it is techincally applied */
Michal Vasko53b7da02018-02-13 15:28:42 +01004745 LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name);
Michal Vasko44ab1462017-05-18 13:18:36 +02004746 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004747 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4748 /* we try to connect the augment only in case the module is implemented or
4749 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004750 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004751 }
4752
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004753 return EXIT_SUCCESS;
4754}
4755
Radek Krejcie534c132016-11-23 13:32:31 +01004756static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004757resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004758{
4759 enum LY_VLOG_ELEM vlog_type;
4760 void *vlog_node;
4761 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004762 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004763 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004764 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004765 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004766 struct lys_ext_instance *tmp_ext;
Michal Vasko53b7da02018-02-13 15:28:42 +01004767 struct ly_ctx *ctx = NULL;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004768 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004769
4770 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004771 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004772 vlog_node = info->parent;
4773 vlog_type = LY_VLOG_LYS;
4774 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004775 case LYEXT_PAR_MODULE:
4776 case LYEXT_PAR_IMPORT:
4777 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004778 vlog_node = NULL;
4779 vlog_type = LY_VLOG_LYS;
4780 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004781 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004782 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004783 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004784 break;
4785 }
4786
4787 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004788 /* YIN */
4789
Radek Krejcie534c132016-11-23 13:32:31 +01004790 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004791 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004792 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004793 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004794 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004795 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004796 ctx = mod->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01004797
4798 /* find the extension definition */
4799 e = NULL;
4800 for (i = 0; i < mod->extensions_size; i++) {
4801 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4802 e = &mod->extensions[i];
4803 break;
4804 }
4805 }
4806 /* try submodules */
4807 for (j = 0; !e && j < mod->inc_size; j++) {
4808 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4809 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4810 e = &mod->inc[j].submodule->extensions[i];
4811 break;
4812 }
4813 }
4814 }
4815 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004816 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004817 return EXIT_FAILURE;
4818 }
4819
4820 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004821
Radek Krejci72b35992017-01-04 16:27:44 +01004822 if (e->plugin && e->plugin->check_position) {
4823 /* common part - we have plugin with position checking function, use it first */
4824 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4825 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004826 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004827 return -1;
4828 }
4829 }
4830
Radek Krejci8d6b7422017-02-03 14:42:13 +01004831 /* extension type-specific part - allocation */
4832 if (e->plugin) {
4833 etype = e->plugin->type;
4834 } else {
4835 /* default type */
4836 etype = LYEXT_FLAG;
4837 }
4838 switch (etype) {
4839 case LYEXT_FLAG:
4840 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4841 break;
4842 case LYEXT_COMPLEX:
4843 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4844 break;
4845 case LYEXT_ERR:
4846 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004847 LOGINT(ctx);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004848 return -1;
4849 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004850 LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004851
4852 /* common part for all extension types */
4853 (*ext)->def = e;
4854 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004855 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004856 (*ext)->insubstmt = info->substmt;
4857 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004858 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004859 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004860
PavolVicand86b0d62017-09-01 11:01:39 +02004861 if (e->argument) {
4862 if (!(e->flags & LYS_YINELEM)) {
4863 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4864 if (!(*ext)->arg_value) {
PavolVicane7a1fc62018-02-19 16:50:27 +01004865 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
PavolVicand86b0d62017-09-01 11:01:39 +02004866 return -1;
4867 }
4868
4869 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4870 } else {
4871 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4872 if (ly_strequal(yin->name, e->argument, 1)) {
4873 (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0);
4874 lyxml_free(mod->ctx, yin);
4875 break;
4876 }
4877 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004878 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004879 }
4880
PavolVican92f23622017-12-12 13:35:56 +01004881 if ((*ext)->flags & LYEXT_OPT_VALID &&
4882 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01004883 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004884 }
4885
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004886 (*ext)->nodetype = LYS_EXT;
4887 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004888
Radek Krejci8d6b7422017-02-03 14:42:13 +01004889 /* extension type-specific part - parsing content */
4890 switch (etype) {
4891 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004892 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4893 if (!yin->ns) {
4894 /* garbage */
4895 lyxml_free(mod->ctx, yin);
4896 continue;
4897 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4898 /* standard YANG statements are not expected here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004899 LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004900 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004901 } else if (yin->ns == info->data.yin->ns &&
4902 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004903 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004904 if ((*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004905 LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004906 return -1;
4907 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004908 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004909 yin->content = NULL;
4910 lyxml_free(mod->ctx, yin);
4911 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004912 /* extension instance */
4913 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4914 LYEXT_SUBSTMT_SELF, 0, unres)) {
4915 return -1;
4916 }
Radek Krejci72b35992017-01-04 16:27:44 +01004917
Radek Krejci72b35992017-01-04 16:27:44 +01004918 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004919 }
Radek Krejci72b35992017-01-04 16:27:44 +01004920 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004921 break;
4922 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004923 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004924 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4925 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004926 return -1;
4927 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004928 break;
4929 default:
4930 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004931 }
Radek Krejci72b35992017-01-04 16:27:44 +01004932
4933 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4934
Radek Krejcie534c132016-11-23 13:32:31 +01004935 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004936 /* YANG */
4937
PavolVicanc1807262017-01-31 18:00:27 +01004938 ext_prefix = (char *)(*ext)->def;
4939 tmp = strchr(ext_prefix, ':');
4940 if (!tmp) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004941 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004942 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004943 }
4944 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004945
PavolVicanc1807262017-01-31 18:00:27 +01004946 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004947 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004948 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004949 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004950 return EXIT_FAILURE;
4951 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004952 ctx = mod->ctx;
PavolVicanc1807262017-01-31 18:00:27 +01004953
4954 /* find the extension definition */
4955 e = NULL;
4956 for (i = 0; i < mod->extensions_size; i++) {
4957 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4958 e = &mod->extensions[i];
4959 break;
4960 }
4961 }
4962 /* try submodules */
4963 for (j = 0; !e && j < mod->inc_size; j++) {
4964 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4965 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4966 e = &mod->inc[j].submodule->extensions[i];
4967 break;
4968 }
4969 }
4970 }
4971 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004972 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004973 return EXIT_FAILURE;
4974 }
4975
PavolVicanfcc98762017-09-01 15:51:39 +02004976 (*ext)->flags &= ~LYEXT_OPT_YANG;
4977 (*ext)->def = NULL;
4978
PavolVicanc1807262017-01-31 18:00:27 +01004979 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4980
4981 if (e->plugin && e->plugin->check_position) {
4982 /* common part - we have plugin with position checking function, use it first */
4983 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4984 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004985 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01004986 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004987 }
4988 }
4989
PavolVican22e88682017-02-14 22:38:18 +01004990 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01004991 (*ext)->def = e;
4992 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01004993 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004994 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
PavolVican22e88682017-02-14 22:38:18 +01004995
PavolVicanb0d84102017-02-15 16:32:42 +01004996 if (e->argument && !(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004997 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
PavolVicanb0d84102017-02-15 16:32:42 +01004998 goto error;
4999 }
5000
PavolVican92f23622017-12-12 13:35:56 +01005001 if ((*ext)->flags & LYEXT_OPT_VALID &&
5002 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01005003 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
PavolVican92f23622017-12-12 13:35:56 +01005004 }
5005
Radek Krejci7f1d47e2017-04-12 15:29:02 +02005006 (*ext)->module = info->mod;
5007 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02005008
PavolVican22e88682017-02-14 22:38:18 +01005009 /* extension type-specific part */
5010 if (e->plugin) {
5011 etype = e->plugin->type;
5012 } else {
5013 /* default type */
5014 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01005015 }
PavolVican22e88682017-02-14 22:38:18 +01005016 switch (etype) {
5017 case LYEXT_FLAG:
5018 /* nothing change */
5019 break;
5020 case LYEXT_COMPLEX:
5021 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Michal Vasko53b7da02018-02-13 15:28:42 +01005022 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error);
PavolVican22e88682017-02-14 22:38:18 +01005023 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
5024 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01005025 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01005026 if (info->data.yang) {
5027 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01005028 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
5029 (struct lys_ext_instance_complex*)(*ext))) {
5030 goto error;
5031 }
5032 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
5033 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01005034 goto error;
5035 }
PavolVicana3876672017-02-21 15:49:51 +01005036 }
5037 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
5038 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01005039 }
PavolVican22e88682017-02-14 22:38:18 +01005040 break;
5041 case LYEXT_ERR:
5042 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01005043 LOGINT(ctx);
PavolVican22e88682017-02-14 22:38:18 +01005044 goto error;
5045 }
5046
PavolVican22e88682017-02-14 22:38:18 +01005047 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
5048 goto error;
5049 }
5050 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01005051 }
5052
5053 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01005054error:
5055 free(ext_prefix);
5056 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01005057}
5058
Michal Vasko730dfdf2015-08-11 14:48:05 +02005059/**
Pavol Vican855ca622016-09-05 13:07:54 +02005060 * @brief Resolve (find) choice default case. Does not log.
5061 *
5062 * @param[in] choic Choice to use.
5063 * @param[in] dflt Name of the default case.
5064 *
5065 * @return Pointer to the default node or NULL.
5066 */
5067static struct lys_node *
5068resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
5069{
5070 struct lys_node *child, *ret;
5071
5072 LY_TREE_FOR(choic->child, child) {
5073 if (child->nodetype == LYS_USES) {
5074 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
5075 if (ret) {
5076 return ret;
5077 }
5078 }
5079
5080 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02005081 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02005082 return child;
5083 }
5084 }
5085
5086 return NULL;
5087}
5088
5089/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02005090 * @brief Resolve uses, apply augments, refines. Logs directly.
5091 *
Michal Vaskobb211122015-08-19 14:03:11 +02005092 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005093 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005094 *
Michal Vaskodef0db12015-10-07 13:22:48 +02005095 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005096 */
Michal Vasko184521f2015-09-24 13:14:26 +02005097static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005098resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005099{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005100 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02005101 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02005102 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02005103 struct lys_node_leaflist *llist;
5104 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02005105 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005106 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005107 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005108 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005109 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005110 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005111
Michal Vasko71e1aa82015-08-12 12:17:51 +02005112 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01005113
Radek Krejci93def382017-05-24 15:33:48 +02005114 /* check that the grouping is resolved (no unresolved uses inside) */
5115 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02005116
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005117 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01005118 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01005119 if (node_aux->nodetype & LYS_GROUPING) {
5120 /* do not instantiate groupings from groupings */
5121 continue;
5122 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01005123 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01005124 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005125 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
5126 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005127 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005128 }
Pavol Vican55abd332016-07-12 15:54:49 +02005129 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01005130 LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02005131 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005132 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02005133 }
5134 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005135 }
Michal Vaskoe022a562016-09-27 14:24:15 +02005136
Michal Vaskodef0db12015-10-07 13:22:48 +02005137 /* we managed to copy the grouping, the rest must be possible to resolve */
5138
Pavol Vican855ca622016-09-05 13:07:54 +02005139 if (uses->refine_size) {
5140 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01005141 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005142 }
5143
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005144 /* apply refines */
5145 for (i = 0; i < uses->refine_size; i++) {
5146 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01005147 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
5148 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02005149 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01005150 if (rc || !node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005151 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005152 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005153 }
5154
Radek Krejci1d82ef62015-08-07 14:44:40 +02005155 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005156 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
5157 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005158 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005159 }
Pavol Vican855ca622016-09-05 13:07:54 +02005160 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005161
5162 /* description on any nodetype */
5163 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005164 lydict_remove(ctx, node->dsc);
5165 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005166 }
5167
5168 /* reference on any nodetype */
5169 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005170 lydict_remove(ctx, node->ref);
5171 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005172 }
5173
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005174 /* config on any nodetype,
5175 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
5176 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005177 node->flags &= ~LYS_CONFIG_MASK;
5178 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005179 }
5180
5181 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02005182 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005183 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005184 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02005185 leaf = (struct lys_node_leaf *)node;
5186
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005187 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02005188 lydict_remove(ctx, leaf->dflt);
5189 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
5190
5191 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01005192 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
5193 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005194 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005195 }
Radek Krejci200bf712016-08-16 17:11:04 +02005196 } else if (node->nodetype == LYS_LEAFLIST) {
5197 /* leaf-list */
5198 llist = (struct lys_node_leaflist *)node;
5199
5200 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01005201 for (j = 0; j < llist->dflt_size; j++) {
5202 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02005203 }
5204 free(llist->dflt);
5205
5206 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02005207 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01005208 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail);
Radek Krejci200bf712016-08-16 17:11:04 +02005209 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01005210 for (j = 0; j < llist->dflt_size; j++) {
5211 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02005212 }
5213
5214 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01005215 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01005216 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01005217 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02005218 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02005219 }
5220 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005221 }
5222 }
5223
5224 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02005225 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01005226 /* remove current value */
5227 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005228
Radek Krejcibf285832017-01-26 16:05:41 +01005229 /* set new value */
5230 node->flags |= (rfn->flags & LYS_MAND_MASK);
5231
Pavol Vican855ca622016-09-05 13:07:54 +02005232 if (rfn->flags & LYS_MAND_TRUE) {
5233 /* check if node has default value */
5234 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005235 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005236 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005237 goto fail;
5238 }
5239 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005240 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005241 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005242 goto fail;
5243 }
5244 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005245 }
5246
5247 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005248 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
5249 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
5250 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005251 }
5252
5253 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005254 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005255 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005256 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005257 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005258 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005259 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005260 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005261 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005262 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005263 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005264 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005265 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005266 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005267 }
5268 }
5269
5270 /* must in leaf, leaf-list, list, container or anyxml */
5271 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005272 switch (node->nodetype) {
5273 case LYS_LEAF:
5274 old_size = &((struct lys_node_leaf *)node)->must_size;
5275 old_must = &((struct lys_node_leaf *)node)->must;
5276 break;
5277 case LYS_LEAFLIST:
5278 old_size = &((struct lys_node_leaflist *)node)->must_size;
5279 old_must = &((struct lys_node_leaflist *)node)->must;
5280 break;
5281 case LYS_LIST:
5282 old_size = &((struct lys_node_list *)node)->must_size;
5283 old_must = &((struct lys_node_list *)node)->must;
5284 break;
5285 case LYS_CONTAINER:
5286 old_size = &((struct lys_node_container *)node)->must_size;
5287 old_must = &((struct lys_node_container *)node)->must;
5288 break;
5289 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005290 case LYS_ANYDATA:
5291 old_size = &((struct lys_node_anydata *)node)->must_size;
5292 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005293 break;
5294 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01005295 LOGINT(ctx);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005296 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005297 }
5298
5299 size = *old_size + rfn->must_size;
5300 must = realloc(*old_must, size * sizeof *rfn->must);
Michal Vasko53b7da02018-02-13 15:28:42 +01005301 LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005302 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01005303 must[j].ext_size = rfn->must[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005304 lys_ext_dup(ctx, rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR,
Radek Krejci5138e9f2017-04-12 13:10:46 +02005305 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02005306 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
5307 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
5308 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
5309 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
5310 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02005311 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005312 }
5313
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005314 *old_must = must;
5315 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02005316
5317 /* check XPath dependencies again */
5318 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
5319 goto fail;
5320 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005321 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02005322
5323 /* if-feature in leaf, leaf-list, list, container or anyxml */
5324 if (rfn->iffeature_size) {
5325 old_size = &node->iffeature_size;
5326 old_iff = &node->iffeature;
5327
5328 size = *old_size + rfn->iffeature_size;
5329 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01005330 LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02005331 *old_iff = iff;
5332
Pavol Vican855ca622016-09-05 13:07:54 +02005333 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
5334 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005335 if (usize1) {
5336 /* there is something to duplicate */
5337 /* duplicate compiled expression */
5338 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
5339 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01005340 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005341 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005342
5343 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02005344 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01005345 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005346 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005347
Radek Krejci3a3b2002017-09-13 16:39:02 +02005348 /* duplicate extensions */
5349 iff[j].ext_size = rfn->iffeature[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005350 lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
Radek Krejci3a3b2002017-09-13 16:39:02 +02005351 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
5352 }
5353 (*old_size)++;
5354 }
5355 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005356 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005357 }
5358
5359 /* apply augments */
5360 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01005361 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005362 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005363 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005364 }
5365 }
5366
Pavol Vican855ca622016-09-05 13:07:54 +02005367 /* check refines */
5368 for (i = 0; i < uses->refine_size; i++) {
5369 node = refine_nodes[i];
5370 rfn = &uses->refine[i];
5371
5372 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005373 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02005374 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01005375 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02005376 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
5377 (rfn->flags & LYS_CONFIG_W)) {
5378 /* setting config true under config false is prohibited */
Michal Vasko53b7da02018-02-13 15:28:42 +01005379 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5380 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005381 "changing config from 'false' to 'true' is prohibited while "
5382 "the target's parent is still config 'false'.");
5383 goto fail;
5384 }
5385
5386 /* inherit config change to the target children */
5387 LY_TREE_DFS_BEGIN(node->child, next, iter) {
5388 if (rfn->flags & LYS_CONFIG_W) {
5389 if (iter->flags & LYS_CONFIG_SET) {
5390 /* config is set explicitely, go to next sibling */
5391 next = NULL;
5392 goto nextsibling;
5393 }
5394 } else { /* LYS_CONFIG_R */
5395 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
5396 /* error - we would have config data under status data */
Michal Vasko53b7da02018-02-13 15:28:42 +01005397 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5398 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005399 "changing config from 'true' to 'false' is prohibited while the target "
5400 "has still a children with explicit config 'true'.");
5401 goto fail;
5402 }
5403 }
5404 /* change config */
5405 iter->flags &= ~LYS_CONFIG_MASK;
5406 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
5407
5408 /* select next iter - modified LY_TREE_DFS_END */
5409 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5410 next = NULL;
5411 } else {
5412 next = iter->child;
5413 }
5414nextsibling:
5415 if (!next) {
5416 /* try siblings */
5417 next = iter->next;
5418 }
5419 while (!next) {
5420 /* parent is already processed, go to its sibling */
5421 iter = lys_parent(iter);
5422
5423 /* no siblings, go back through parents */
5424 if (iter == node) {
5425 /* we are done, no next element to process */
5426 break;
5427 }
5428 next = iter->next;
5429 }
5430 }
5431 }
5432
5433 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005434 if (rfn->dflt_size) {
5435 if (node->nodetype == LYS_CHOICE) {
5436 /* choice */
5437 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5438 rfn->dflt[0]);
5439 if (!((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005440 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005441 goto fail;
5442 }
5443 if (lyp_check_mandatory_choice(node)) {
5444 goto fail;
5445 }
Pavol Vican855ca622016-09-05 13:07:54 +02005446 }
5447 }
5448
5449 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02005450 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005451 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005452 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5453 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005454 goto fail;
5455 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005456 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005457 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005458 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5459 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005460 goto fail;
5461 }
5462 }
5463
5464 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005465 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005466 if (node->nodetype == LYS_LEAFLIST) {
5467 llist = (struct lys_node_leaflist *)node;
5468 if (llist->dflt_size && llist->min) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005469 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5470 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005471 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5472 goto fail;
5473 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005474 } else if (node->nodetype == LYS_LEAF) {
5475 leaf = (struct lys_node_leaf *)node;
5476 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005477 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5478 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005479 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5480 goto fail;
5481 }
Pavol Vican855ca622016-09-05 13:07:54 +02005482 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005483
Pavol Vican855ca622016-09-05 13:07:54 +02005484 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005485 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005486 for (parent = node->parent;
5487 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5488 parent = parent->parent) {
5489 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5490 /* stop also on presence containers */
5491 break;
5492 }
5493 }
5494 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5495 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5496 if (lyp_check_mandatory_choice(parent)) {
5497 goto fail;
5498 }
5499 }
5500 }
5501 }
5502 free(refine_nodes);
5503
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005504 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005505
5506fail:
5507 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5508 lys_node_free(iter, NULL, 0);
5509 }
Pavol Vican855ca622016-09-05 13:07:54 +02005510 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005511 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005512}
5513
Radek Krejci83a4bac2017-02-07 15:53:04 +01005514void
5515resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005516{
5517 int i;
5518
5519 assert(der && base);
5520
Radek Krejci018f1f52016-08-03 16:01:20 +02005521 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005522 /* create a set for backlinks if it does not exist */
5523 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005524 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005525 /* store backlink */
5526 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005527
Radek Krejci85a54be2016-10-20 12:39:56 +02005528 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005529 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005530 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005531 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005532}
5533
Michal Vasko730dfdf2015-08-11 14:48:05 +02005534/**
5535 * @brief Resolve base identity recursively. Does not log.
5536 *
5537 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005538 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005539 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005540 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005541 *
Radek Krejci219fa612016-08-15 10:36:51 +02005542 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005543 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005544static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005545resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005546 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005547{
Michal Vaskof02e3742015-08-05 16:27:02 +02005548 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005549 struct lys_ident *base = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005550 struct ly_ctx *ctx = module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005551
Radek Krejcicf509982015-12-15 09:22:44 +01005552 assert(ret);
5553
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005554 /* search module */
5555 for (i = 0; i < module->ident_size; i++) {
5556 if (!strcmp(basename, module->ident[i].name)) {
5557
5558 if (!ident) {
5559 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005560 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005561 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005562 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005563 }
5564
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005565 base = &module->ident[i];
5566 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005567 }
5568 }
5569
5570 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005571 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5572 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5573 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005574
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005575 if (!ident) {
5576 *ret = &module->inc[j].submodule->ident[i];
5577 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005578 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005579
5580 base = &module->inc[j].submodule->ident[i];
5581 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005582 }
5583 }
5584 }
5585
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005586matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005587 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005588 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005589 /* is it already completely resolved? */
5590 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005591 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005592 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5593
5594 /* simple check for circular reference,
5595 * the complete check is done as a side effect of using only completely
5596 * resolved identities (previous check of unres content) */
5597 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005598 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5599 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005600 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005601 }
5602
Radek Krejci06f64ed2016-08-15 11:07:44 +02005603 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005604 }
5605 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005606
Radek Krejcibabbff82016-02-19 13:31:37 +01005607 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005608 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005609 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005610 }
5611
Radek Krejci219fa612016-08-15 10:36:51 +02005612 /* base not found (maybe a forward reference) */
5613 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005614}
5615
Michal Vasko730dfdf2015-08-11 14:48:05 +02005616/**
5617 * @brief Resolve base identity. Logs directly.
5618 *
5619 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005620 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005621 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005622 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005623 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005624 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005625 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005626 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005627static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005628resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005629 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005630{
5631 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005632 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005633 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005634 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005635 struct lys_module *mod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005636 struct ly_ctx *ctx = module->ctx;
Radek Krejcicf509982015-12-15 09:22:44 +01005637
5638 assert((ident && !type) || (!ident && type));
5639
5640 if (!type) {
5641 /* have ident to resolve */
5642 ret = &target;
5643 flags = ident->flags;
5644 mod = ident->module;
5645 } else {
5646 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005647 ++type->info.ident.count;
5648 type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref);
Michal Vasko53b7da02018-02-13 15:28:42 +01005649 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005650
5651 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005652 flags = type->parent->flags;
5653 mod = type->parent->module;
5654 }
Michal Vaskof2006002016-04-21 16:28:15 +02005655 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005656
5657 /* search for the base identity */
5658 name = strchr(basename, ':');
5659 if (name) {
5660 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005661 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005662 name++;
5663
Michal Vasko2d851a92015-10-20 16:16:36 +02005664 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005665 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005666 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005667 }
5668 } else {
5669 name = basename;
5670 }
5671
Radek Krejcic071c542016-01-27 14:57:51 +01005672 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005673 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005674 if (!module) {
5675 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01005676 LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005677 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005678 }
5679
Radek Krejcic071c542016-01-27 14:57:51 +01005680 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005681 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5682 if (!rc) {
5683 assert(*ret);
5684
5685 /* check status */
5686 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5687 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5688 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005689 } else if (ident) {
5690 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005691 if (lys_main_module(mod)->implemented) {
5692 /* in case of the implemented identity, maintain backlinks to it
5693 * from the base identities to make it available when resolving
5694 * data with the identity values (not implemented identity is not
5695 * allowed as an identityref value). */
5696 resolve_identity_backlink_update(ident, *ret);
5697 }
Radek Krejci219fa612016-08-15 10:36:51 +02005698 }
5699 } else if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005700 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005701 if (type) {
5702 --type->info.ident.count;
5703 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005704 }
5705
Radek Krejci219fa612016-08-15 10:36:51 +02005706 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005707}
5708
Radek Krejci9e6af732017-04-27 14:40:25 +02005709/*
5710 * 1 - true (der is derived from base)
5711 * 0 - false (der is not derived from base)
5712 */
5713static int
5714search_base_identity(struct lys_ident *der, struct lys_ident *base)
5715{
5716 int i;
5717
5718 if (der == base) {
5719 return 1;
5720 } else {
5721 for(i = 0; i < der->base_size; i++) {
5722 if (search_base_identity(der->base[i], base) == 1) {
5723 return 1;
5724 }
5725 }
5726 }
5727
5728 return 0;
5729}
5730
Michal Vasko730dfdf2015-08-11 14:48:05 +02005731/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005732 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005733 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005734 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005735 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005736 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005737 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005738 *
5739 * @return Pointer to the identity resolvent, NULL on error.
5740 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005741struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005742resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005743{
Radek Krejci9e6af732017-04-27 14:40:25 +02005744 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005745 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005746 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005747 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005748 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005749 struct lys_ident *der, *cur;
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005750 struct lys_module *imod = NULL, *m, *tmod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005751 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005752
Radek Krejci9e6af732017-04-27 14:40:25 +02005753 assert(type && ident_name && node && mod);
Michal Vasko53b7da02018-02-13 15:28:42 +01005754 ctx = mod->ctx;
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005755
Michal Vaskof2d43962016-09-02 11:10:16 +02005756 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005757 return NULL;
5758 }
5759
Michal Vasko50576712017-07-28 12:28:33 +02005760 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005761 if (rc < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005762 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005763 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005764 } else if (rc < (signed)strlen(ident_name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005765 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005766 return NULL;
5767 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005768
5769 m = lys_main_module(mod); /* shortcut */
5770 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5771 /* identity is defined in the same module as node */
5772 imod = m;
5773 } else if (dflt) {
5774 /* solving identityref in default definition in schema -
5775 * find the identity's module in the imported modules list to have a correct revision */
5776 for (i = 0; i < mod->imp_size; i++) {
5777 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5778 imod = mod->imp[i].module;
5779 break;
5780 }
5781 }
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005782
5783 /* We may need to pull it from the module that the typedef came from */
5784 if (!imod && type && type->der) {
5785 tmod = type->der->module;
5786 for (i = 0; i < tmod->imp_size; i++) {
5787 if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) {
5788 imod = tmod->imp[i].module;
5789 break;
5790 }
5791 }
5792 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005793 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005794 /* solving identityref in data - get the module from the context */
5795 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5796 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005797 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005798 break;
5799 }
Michal Vasko08767f72017-10-06 14:38:08 +02005800 imod = NULL;
5801 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005802 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5803 /* we are currently parsing some module and checking XPath or a default value,
5804 * so take this module into account */
5805 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5806 imod = mod->ctx->models.parsing_sub_modules[i];
5807 if (imod->type) {
5808 /* skip submodules */
5809 continue;
5810 }
5811 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5812 break;
5813 }
5814 imod = NULL;
5815 }
5816 }
Michal Vasko08767f72017-10-06 14:38:08 +02005817 }
5818
Michal Vasko53b7da02018-02-13 15:28:42 +01005819 if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) {
Michal Vasko08767f72017-10-06 14:38:08 +02005820 /* the needed module was not found, but it may have been expected so call the data callback */
5821 if (imod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005822 ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005823 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005824 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01005825 imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
Michal Vasko08767f72017-10-06 14:38:08 +02005826 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005827 }
5828 }
5829 if (!imod) {
5830 goto fail;
5831 }
5832
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005833 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005834 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005835 * THEN, we may need to make the module with the identity implemented, but only if it really
5836 * contains the identity */
5837 if (!imod->implemented) {
5838 cur = NULL;
5839 /* get the identity in the module */
5840 for (i = 0; i < imod->ident_size; i++) {
5841 if (!strcmp(name, imod->ident[i].name)) {
5842 cur = &imod->ident[i];
5843 break;
5844 }
5845 }
5846 if (!cur) {
5847 /* go through includes */
5848 for (j = 0; j < imod->inc_size; j++) {
5849 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5850 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5851 cur = &imod->inc[j].submodule->ident[i];
5852 break;
5853 }
5854 }
5855 }
5856 if (!cur) {
5857 goto fail;
5858 }
5859 }
5860
5861 /* check that identity is derived from one of the type's base */
5862 while (type->der) {
5863 for (i = 0; i < type->info.ident.count; i++) {
5864 if (search_base_identity(cur, type->info.ident.ref[i])) {
5865 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005866 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005867 goto match;
5868 }
5869 }
5870 type = &type->der->type;
5871 }
5872 /* matching base not found */
Michal Vasko53b7da02018-02-13 15:28:42 +01005873 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
Radek Krejci9e6af732017-04-27 14:40:25 +02005874 goto fail;
5875 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005876 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005877
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005878 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005879 while (type->der) {
5880 for (i = 0; i < type->info.ident.count; ++i) {
5881 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005882
Radek Krejci85a54be2016-10-20 12:39:56 +02005883 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005884 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005885 for (j = 0; j < cur->der->number; j++) {
5886 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005887 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005888 /* we have match */
5889 cur = der;
5890 goto match;
5891 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005892 }
5893 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005894 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005895 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005896 }
5897
Radek Krejci9e6af732017-04-27 14:40:25 +02005898fail:
Michal Vasko53b7da02018-02-13 15:28:42 +01005899 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005900 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005901
5902match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005903 for (i = 0; i < cur->iffeature_size; i++) {
5904 if (!resolve_iffeature(&cur->iffeature[i])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005905 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
5906 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005907 return NULL;
5908 }
5909 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005910 if (need_implemented) {
5911 if (dflt) {
Michal Vasko0f437062018-06-08 15:52:39 +02005912 /* later try to make the module implemented */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005913 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5914 imod->name, cur->name, mod->name);
Michal Vasko0f437062018-06-08 15:52:39 +02005915 /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of
5916 * several functions with little gain */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005917 if (lys_set_implemented(imod)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005918 LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005919 imod->name, cur->name);
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005920 goto fail;
5921 }
5922 } else {
5923 /* just say that it was found, but in a non-implemented module */
Michal Vasko53b7da02018-02-13 15:28:42 +01005924 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005925 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005926 goto fail;
5927 }
5928 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005929 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005930}
5931
Michal Vasko730dfdf2015-08-11 14:48:05 +02005932/**
Michal Vaskobb211122015-08-19 14:03:11 +02005933 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005934 *
Michal Vaskobb211122015-08-19 14:03:11 +02005935 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005936 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005937 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005938 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005939 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005940static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005941resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005942{
Radek Krejci93def382017-05-24 15:33:48 +02005943 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005944 struct lys_node *par_grp;
Michal Vasko53b7da02018-02-13 15:28:42 +01005945 struct ly_ctx *ctx = uses->module->ctx;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005946
Radek Krejci6ff885d2017-01-03 14:06:22 +01005947 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used
Radek Krejci93def382017-05-24 15:33:48 +02005948 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5949 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5950 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005951 for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
Michal Vaskoe91afce2015-08-12 12:21:00 +02005952
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005953 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005954 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5955 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005956 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005957 return -1;
5958 } else if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005959 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005960 return -1;
5961 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005962 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005963 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005964 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005965 return -1;
5966 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005967 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005968 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005969 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005970 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005971 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005972 }
5973
Radek Krejci93def382017-05-24 15:33:48 +02005974 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005975 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005976 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005977 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005978 return -1;
5979 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005980 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005981 } else {
5982 /* instantiate grouping only when it is completely resolved */
5983 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005984 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005985 return EXIT_FAILURE;
5986 }
5987
Radek Krejci48464ed2016-03-17 15:44:09 +01005988 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005989 if (!rc) {
5990 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005991 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005992 assert(((struct lys_node_grp *)par_grp)->unres_count);
5993 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01005994 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005995 }
Radek Krejcicf509982015-12-15 09:22:44 +01005996
5997 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005998 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005999 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006000 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01006001 return -1;
6002 }
6003
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006004 return EXIT_SUCCESS;
6005 }
6006
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006007 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006008}
6009
Michal Vasko730dfdf2015-08-11 14:48:05 +02006010/**
Michal Vasko9957e592015-08-17 15:04:09 +02006011 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006012 *
Michal Vaskobb211122015-08-19 14:03:11 +02006013 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006014 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006015 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006016 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006017 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006018static int
Radek Krejci48464ed2016-03-17 15:44:09 +01006019resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006020{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006021 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01006022 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02006023 char *s = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01006024 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006025
6026 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02006027 assert(keys_str);
6028
Radek Krejci5c08a992016-11-02 13:30:04 +01006029 if (!list->child) {
6030 /* no child, possible forward reference */
Michal Vasko53b7da02018-02-13 15:28:42 +01006031 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Radek Krejci5c08a992016-11-02 13:30:04 +01006032 return EXIT_FAILURE;
6033 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006034 /* get the key name */
6035 if ((value = strpbrk(keys_str, " \t\n"))) {
6036 len = value - keys_str;
6037 while (isspace(value[0])) {
6038 value++;
6039 }
6040 } else {
6041 len = strlen(keys_str);
6042 }
6043
Radek Krejcia98048c2017-05-24 16:35:48 +02006044 if (list->keys[i]) {
6045 /* skip already resolved keys */
6046 goto next;
6047 }
6048
Michal Vaskobb520442017-05-23 10:55:18 +02006049 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
6050 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006051 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006052 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02006053 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006054 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006055
Radek Krejci48464ed2016-03-17 15:44:09 +01006056 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006057 /* check_key logs */
6058 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006059 }
6060
Radek Krejcicf509982015-12-15 09:22:44 +01006061 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01006062 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006063 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
6064 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01006065 return -1;
6066 }
6067
Radek Krejcia98048c2017-05-24 16:35:48 +02006068 /* default value - is ignored, keep it but print a warning */
6069 if (list->keys[i]->dflt) {
6070 /* log is not hidden only in case this resolving fails and in such a case
6071 * we cannot get here
6072 */
Michal Vasko53b7da02018-02-13 15:28:42 +01006073 assert(log_opt == ILO_STORE);
6074 log_opt = ILO_LOG;
6075 LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01006076 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Michal Vasko53b7da02018-02-13 15:28:42 +01006077 log_opt = ILO_STORE;
Radek Krejcia98048c2017-05-24 16:35:48 +02006078 free(s);
6079 }
6080
6081next:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006082 /* prepare for next iteration */
6083 while (value && isspace(value[0])) {
6084 value++;
6085 }
6086 keys_str = value;
6087 }
6088
Michal Vaskof02e3742015-08-05 16:27:02 +02006089 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006090}
6091
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006092/**
Michal Vaskobf19d252015-10-08 15:39:17 +02006093 * @brief Resolve (check) all must conditions of \p node.
6094 * Logs directly.
6095 *
6096 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006097 * @param[in] inout_parent If set, must in input or output parent of node->schema will be resolved.
Michal Vaskobf19d252015-10-08 15:39:17 +02006098 *
6099 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
6100 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006101static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006102resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02006103{
Michal Vaskobf19d252015-10-08 15:39:17 +02006104 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006105 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02006106 struct lys_restr *must;
6107 struct lyxp_set set;
Michal Vasko53b7da02018-02-13 15:28:42 +01006108 struct ly_ctx *ctx = node->schema->module->ctx;
Michal Vaskobf19d252015-10-08 15:39:17 +02006109
6110 assert(node);
6111 memset(&set, 0, sizeof set);
6112
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006113 if (inout_parent) {
6114 for (schema = lys_parent(node->schema);
6115 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
6116 schema = lys_parent(schema));
6117 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006118 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006119 return -1;
6120 }
6121 must_size = ((struct lys_node_inout *)schema)->must_size;
6122 must = ((struct lys_node_inout *)schema)->must;
6123
6124 /* context node is the RPC/action */
6125 node = node->parent;
6126 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006127 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006128 return -1;
6129 }
6130 } else {
6131 switch (node->schema->nodetype) {
6132 case LYS_CONTAINER:
6133 must_size = ((struct lys_node_container *)node->schema)->must_size;
6134 must = ((struct lys_node_container *)node->schema)->must;
6135 break;
6136 case LYS_LEAF:
6137 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
6138 must = ((struct lys_node_leaf *)node->schema)->must;
6139 break;
6140 case LYS_LEAFLIST:
6141 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
6142 must = ((struct lys_node_leaflist *)node->schema)->must;
6143 break;
6144 case LYS_LIST:
6145 must_size = ((struct lys_node_list *)node->schema)->must_size;
6146 must = ((struct lys_node_list *)node->schema)->must;
6147 break;
6148 case LYS_ANYXML:
6149 case LYS_ANYDATA:
6150 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
6151 must = ((struct lys_node_anydata *)node->schema)->must;
6152 break;
6153 case LYS_NOTIF:
6154 must_size = ((struct lys_node_notif *)node->schema)->must_size;
6155 must = ((struct lys_node_notif *)node->schema)->must;
6156 break;
6157 default:
6158 must_size = 0;
6159 break;
6160 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006161 }
6162
6163 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006164 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02006165 return -1;
6166 }
6167
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006168 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02006169
Michal Vasko8146d4c2016-05-09 15:50:29 +02006170 if (!set.val.bool) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01006171 if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006172 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
6173 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006174 LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006175 if (must[i].emsg) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006176 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006177 }
6178 if (must[i].eapptag) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006179 ly_err_last_set_apptag(ctx, must[i].eapptag);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006180 }
6181 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02006182 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006183 }
6184 }
6185
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006186 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02006187}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006188
Michal Vaskobf19d252015-10-08 15:39:17 +02006189/**
Michal Vasko508a50d2016-09-07 14:50:33 +02006190 * @brief Resolve (find) when condition schema context node. Does not log.
6191 *
6192 * @param[in] schema Schema node with the when condition.
6193 * @param[out] ctx_snode When schema context node.
6194 * @param[out] ctx_snode_type Schema context node type.
6195 */
6196void
6197resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
6198{
6199 const struct lys_node *sparent;
6200
6201 /* find a not schema-only node */
6202 *ctx_snode_type = LYXP_NODE_ELEM;
6203 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
6204 if (schema->nodetype == LYS_AUGMENT) {
6205 sparent = ((struct lys_node_augment *)schema)->target;
6206 } else {
6207 sparent = schema->parent;
6208 }
6209 if (!sparent) {
6210 /* context node is the document root (fake root in our case) */
6211 if (schema->flags & LYS_CONFIG_W) {
6212 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
6213 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02006214 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02006215 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02006216 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskocb45f472018-02-12 10:47:42 +01006217 schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko508a50d2016-09-07 14:50:33 +02006218 break;
6219 }
6220 schema = sparent;
6221 }
6222
6223 *ctx_snode = (struct lys_node *)schema;
6224}
6225
6226/**
Michal Vaskocf024702015-10-08 15:01:42 +02006227 * @brief Resolve (find) when condition context node. Does not log.
6228 *
6229 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02006230 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02006231 * @param[out] ctx_node Context node.
6232 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02006233 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02006234 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02006235 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02006236static int
6237resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
6238 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006239{
Michal Vaskocf024702015-10-08 15:01:42 +02006240 struct lyd_node *parent;
6241 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006242 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02006243 uint16_t i, data_depth, schema_depth;
6244
Michal Vasko508a50d2016-09-07 14:50:33 +02006245 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02006246
Michal Vaskofe989752016-09-08 08:47:26 +02006247 if (node_type == LYXP_NODE_ELEM) {
6248 /* standard element context node */
6249 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
6250 for (sparent = schema, schema_depth = 0;
6251 sparent;
6252 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
6253 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
6254 ++schema_depth;
6255 }
Michal Vaskocf024702015-10-08 15:01:42 +02006256 }
Michal Vaskofe989752016-09-08 08:47:26 +02006257 if (data_depth < schema_depth) {
6258 return -1;
6259 }
Michal Vaskocf024702015-10-08 15:01:42 +02006260
Michal Vasko956e8542016-08-26 09:43:35 +02006261 /* find the corresponding data node */
6262 for (i = 0; i < data_depth - schema_depth; ++i) {
6263 node = node->parent;
6264 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02006265 if (node->schema != schema) {
6266 return -1;
6267 }
Michal Vaskofe989752016-09-08 08:47:26 +02006268 } else {
6269 /* root context node */
6270 while (node->parent) {
6271 node = node->parent;
6272 }
6273 while (node->prev->next) {
6274 node = node->prev;
6275 }
Michal Vaskocf024702015-10-08 15:01:42 +02006276 }
6277
Michal Vaskoa59495d2016-08-22 09:18:58 +02006278 *ctx_node = node;
6279 *ctx_node_type = node_type;
6280 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02006281}
6282
Michal Vasko76c3bd32016-08-24 16:02:52 +02006283/**
6284 * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation.
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006285 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02006286 *
6287 * @param[in] snode Schema node, whose children instances need to be unlinked.
6288 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
6289 * it is moved to point to another sibling still in the original tree.
6290 * @param[in,out] ctx_node When context node, adjusted if needed.
6291 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
6292 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
6293 * Ordering may change, but there will be no semantic change.
6294 *
6295 * @return EXIT_SUCCESS on success, -1 on error.
6296 */
6297static int
6298resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
6299 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
6300{
6301 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006302 const struct lys_node *slast;
Michal Vasko53b7da02018-02-13 15:28:42 +01006303 struct ly_ctx *ctx = snode->module->ctx;
Michal Vasko76c3bd32016-08-24 16:02:52 +02006304
6305 switch (snode->nodetype) {
6306 case LYS_AUGMENT:
6307 case LYS_USES:
6308 case LYS_CHOICE:
6309 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006310 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01006311 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006312 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
6313 continue;
6314 }
6315
6316 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006317 return -1;
6318 }
6319 }
6320 break;
6321 case LYS_CONTAINER:
6322 case LYS_LIST:
6323 case LYS_LEAF:
6324 case LYS_LEAFLIST:
6325 case LYS_ANYXML:
6326 case LYS_ANYDATA:
6327 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
6328 if (elem->schema == snode) {
6329
6330 if (elem == *ctx_node) {
6331 /* We are going to unlink our context node! This normally cannot happen,
6332 * but we use normal top-level data nodes for faking a document root node,
6333 * so if this is the context node, we just use the next top-level node.
6334 * Additionally, it can even happen that there are no top-level data nodes left,
6335 * all were unlinked, so in this case we pass NULL as the context node/data tree,
6336 * lyxp_eval() can handle this special situation.
6337 */
6338 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006339 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006340 return -1;
6341 }
6342
6343 if (elem->prev == elem) {
6344 /* unlinking last top-level element, use an empty data tree */
6345 *ctx_node = NULL;
6346 } else {
6347 /* in this case just use the previous/last top-level data node */
6348 *ctx_node = elem->prev;
6349 }
6350 } else if (elem == *node) {
6351 /* We are going to unlink the currently processed node. This does not matter that
6352 * much, but we would lose access to the original data tree, so just move our
6353 * pointer somewhere still inside it.
6354 */
6355 if ((*node)->prev != *node) {
6356 *node = (*node)->prev;
6357 } else {
6358 /* the processed node with sibings were all unlinked, oh well */
6359 *node = NULL;
6360 }
6361 }
6362
6363 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01006364 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006365 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006366 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006367 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006368 return -1;
6369 }
6370 } else {
6371 *unlinked_nodes = elem;
6372 }
6373
6374 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
6375 /* there can be only one instance */
6376 break;
6377 }
6378 }
6379 }
6380 break;
6381 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006382 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006383 return -1;
6384 }
6385
6386 return EXIT_SUCCESS;
6387}
6388
6389/**
6390 * @brief Relink the unlinked nodes back.
6391 *
6392 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
6393 * we simply need a sibling from the original data tree.
6394 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
6395 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
6396 * or the sibling of \p unlinked_nodes.
6397 *
6398 * @return EXIT_SUCCESS on success, -1 on error.
6399 */
6400static int
6401resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
6402{
6403 struct lyd_node *elem;
6404
6405 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006406 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006407 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006408 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006409 return -1;
6410 }
6411 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006412 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006413 return -1;
6414 }
6415 }
6416 }
6417
6418 return EXIT_SUCCESS;
6419}
6420
Radek Krejci03b71f72016-03-16 11:10:09 +01006421int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006422resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01006423{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006424 int ret = 0;
6425 uint8_t must_size;
6426 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02006427
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006428 assert(node);
6429
6430 schema = node->schema;
6431
6432 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02006433 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01006434 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006435 must_size = ((struct lys_node_container *)schema)->must_size;
6436 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006437 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006438 must_size = ((struct lys_node_leaf *)schema)->must_size;
6439 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006440 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006441 must_size = ((struct lys_node_leaflist *)schema)->must_size;
6442 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006443 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006444 must_size = ((struct lys_node_list *)schema)->must_size;
6445 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006446 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02006447 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006448 must_size = ((struct lys_node_anydata *)schema)->must_size;
6449 break;
6450 case LYS_NOTIF:
6451 must_size = ((struct lys_node_notif *)schema)->must_size;
6452 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006453 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006454 must_size = 0;
6455 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006456 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006457
6458 if (must_size) {
6459 ++ret;
6460 }
6461
6462 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6463 if (!node->prev->next) {
6464 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6465 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6466 ret += 0x2;
6467 }
6468 }
6469
6470 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006471}
6472
6473int
Radek Krejci46165822016-08-26 14:06:27 +02006474resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006475{
Radek Krejci46165822016-08-26 14:06:27 +02006476 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006477
Radek Krejci46165822016-08-26 14:06:27 +02006478 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006479
Radek Krejci46165822016-08-26 14:06:27 +02006480 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006481 return 1;
6482 }
6483
Radek Krejci46165822016-08-26 14:06:27 +02006484 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006485 goto check_augment;
6486
Radek Krejci46165822016-08-26 14:06:27 +02006487 while (parent) {
6488 /* stop conditions */
6489 if (!mode) {
6490 /* stop on node that can be instantiated in data tree */
6491 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6492 break;
6493 }
6494 } else {
6495 /* stop on the specified node */
6496 if (parent == stop) {
6497 break;
6498 }
6499 }
6500
6501 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006502 return 1;
6503 }
6504check_augment:
6505
6506 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02006507 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006508 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006509 }
6510 parent = lys_parent(parent);
6511 }
6512
6513 return 0;
6514}
6515
Michal Vaskocf024702015-10-08 15:01:42 +02006516/**
6517 * @brief Resolve (check) all when conditions relevant for \p node.
6518 * Logs directly.
6519 *
6520 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006521 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6522 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006523 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006524 * @return
6525 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006526 * 0 - all "when" statements true
6527 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006528 * 1, ly_vecode = LYVE_INWHEN - nodes needed to resolve are conditional and not yet resolved (under another "when")
Michal Vaskocf024702015-10-08 15:01:42 +02006529 */
Radek Krejci46165822016-08-26 14:06:27 +02006530int
Michal Vasko0b963112017-08-11 12:45:36 +02006531resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006532{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006533 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006534 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006535 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006536 enum lyxp_node_type ctx_node_type;
Michal Vasko53b7da02018-02-13 15:28:42 +01006537 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci51093642016-03-29 10:14:59 +02006538 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006539
6540 assert(node);
6541 memset(&set, 0, sizeof set);
6542
Michal Vasko78d97e22017-02-21 09:54:38 +01006543 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006544 /* make the node dummy for the evaluation */
6545 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006546 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
6547 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006548 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006549 if (rc) {
6550 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006551 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006552 }
Radek Krejci51093642016-03-29 10:14:59 +02006553 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006554 }
6555
Radek Krejci03b71f72016-03-16 11:10:09 +01006556 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006557 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006558 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006559 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006560 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006561 || ((((struct lys_node_container *)node->schema)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6562 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006563 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6564 ((struct lys_node_container *)node->schema)->when->cond);
6565 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006566 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006567 if (failed_when) {
6568 *failed_when = ((struct lys_node_container *)node->schema)->when;
6569 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006570 goto cleanup;
6571 }
Michal Vaskocf024702015-10-08 15:01:42 +02006572 }
Radek Krejci51093642016-03-29 10:14:59 +02006573
6574 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006575 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006576 }
6577
Michal Vasko90fc2a32016-08-24 15:58:58 +02006578 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006579 goto check_augment;
6580
6581 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006582 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6583 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006584 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006585 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006586 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006587 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006588 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006589 }
6590 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006591
6592 unlinked_nodes = NULL;
6593 /* we do not want our node pointer to change */
6594 tmp_node = node;
6595 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6596 if (rc) {
6597 goto cleanup;
6598 }
6599
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006600 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6601 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006602
6603 if (unlinked_nodes && ctx_node) {
6604 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6605 rc = -1;
6606 goto cleanup;
6607 }
6608 }
6609
Radek Krejci03b71f72016-03-16 11:10:09 +01006610 if (rc) {
6611 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006612 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006613 }
Radek Krejci51093642016-03-29 10:14:59 +02006614 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006615 }
6616
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006617 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006618 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02006619 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006620 || ((((struct lys_node_uses *)sparent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6621 || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006622 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6623 ((struct lys_node_uses *)sparent)->when->cond);
6624 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006625 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko53b7da02018-02-13 15:28:42 +01006626 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006627 if (failed_when) {
6628 *failed_when = ((struct lys_node_uses *)sparent)->when;
6629 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006630 goto cleanup;
6631 }
Michal Vaskocf024702015-10-08 15:01:42 +02006632 }
Radek Krejci51093642016-03-29 10:14:59 +02006633
6634 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006635 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006636 }
6637
6638check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006639 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006640 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006641 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006642 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006643 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006644 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006645 }
6646 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006647
6648 unlinked_nodes = NULL;
6649 tmp_node = node;
6650 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6651 if (rc) {
6652 goto cleanup;
6653 }
6654
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006655 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6656 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006657
6658 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6659 * so the tree did not actually change and there is nothing for us to do
6660 */
6661 if (unlinked_nodes && ctx_node) {
6662 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6663 rc = -1;
6664 goto cleanup;
6665 }
6666 }
6667
Radek Krejci03b71f72016-03-16 11:10:09 +01006668 if (rc) {
6669 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006670 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006671 }
Radek Krejci51093642016-03-29 10:14:59 +02006672 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006673 }
6674
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006675 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006676 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006677 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006678 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006679 || ((((struct lys_node_augment *)sparent->parent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6680 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006681 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006682 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006683 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006684 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006685 if (failed_when) {
6686 *failed_when = ((struct lys_node_augment *)sparent->parent)->when;
6687 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006688 goto cleanup;
6689 }
Michal Vaskocf024702015-10-08 15:01:42 +02006690 }
Radek Krejci51093642016-03-29 10:14:59 +02006691
6692 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006693 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006694 }
6695
Michal Vasko90fc2a32016-08-24 15:58:58 +02006696 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006697 }
6698
Radek Krejci0b7704f2016-03-18 12:16:14 +01006699 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006700
Radek Krejci51093642016-03-29 10:14:59 +02006701cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006702 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006703 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006704 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006705}
6706
Radek Krejcicbb473e2016-09-16 14:48:32 +02006707static int
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006708check_type_union_leafref(struct lys_type *type)
6709{
6710 uint8_t i;
6711
6712 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6713 /* go through unions and look for leafref */
6714 for (i = 0; i < type->info.uni.count; ++i) {
6715 switch (type->info.uni.types[i].base) {
6716 case LY_TYPE_LEAFREF:
6717 return 1;
6718 case LY_TYPE_UNION:
6719 if (check_type_union_leafref(&type->info.uni.types[i])) {
6720 return 1;
6721 }
6722 break;
6723 default:
6724 break;
6725 }
6726 }
6727
6728 return 0;
6729 }
6730
6731 /* just inherit the flag value */
6732 return type->der->has_union_leafref;
6733}
6734
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006735/**
Michal Vaskobb211122015-08-19 14:03:11 +02006736 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006737 *
6738 * @param[in] mod Main module.
6739 * @param[in] item Item to resolve. Type determined by \p type.
6740 * @param[in] type Type of the unresolved item.
6741 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006742 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006743 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006744 *
6745 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6746 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006747static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006748resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006749 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006750{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006751 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vasko53b7da02018-02-13 15:28:42 +01006752 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006753 unsigned int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01006754 struct ly_ctx * ctx = mod->ctx;
Radek Krejci80056d52017-01-05 13:13:33 +01006755 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006756 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006757 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006758
Radek Krejcic79c6b12016-07-26 15:11:49 +02006759 struct ly_set *refs, *procs;
6760 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006761 struct lys_ident *ident;
6762 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006763 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006764 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006765 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006766 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006767 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006768 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006769 struct lys_ext_instance *ext, **extlist;
6770 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006771
6772 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006773 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006774 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006775 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006776 ident = item;
6777
Radek Krejci018f1f52016-08-03 16:01:20 +02006778 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006779 break;
6780 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006781 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006782 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006783 stype = item;
6784
Radek Krejci018f1f52016-08-03 16:01:20 +02006785 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006786 break;
6787 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006788 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006789 stype = item;
6790
Michal Vasko74083ec2018-06-15 10:00:12 +02006791 rc = resolve_schema_leafref(stype, node, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006792 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006793 case UNRES_TYPE_DER_EXT:
6794 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006795 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006796 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006797 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006798 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006799 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006800 /* parent */
6801 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006802 stype = item;
6803
Michal Vasko88c29542015-11-27 14:57:53 +01006804 /* HACK type->der is temporarily unparsed type statement */
6805 yin = (struct lyxml_elem *)stype->der;
6806 stype->der = NULL;
6807
Pavol Vicana0e4e672016-02-24 12:20:04 +01006808 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6809 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006810 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006811
6812 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006813 /* may try again later */
6814 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006815 } else {
6816 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006817 lydict_remove(ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006818 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006819 }
6820
Michal Vasko88c29542015-11-27 14:57:53 +01006821 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006822 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006823 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006824 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006825 lyxml_free(ctx, yin);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006826 } else {
6827 /* may try again later, put all back how it was */
6828 stype->der = (struct lys_tpdf *)yin;
6829 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006830 }
Radek Krejcic13db382016-08-16 10:52:42 +02006831 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006832 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006833 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006834 LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
Radek Krejci2c2fce82016-08-01 13:52:26 +02006835 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006836
6837 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6838 /* fill typedef union leafref flag */
6839 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6840 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6841 /* copy the type in case it has union leafref flag */
6842 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006843 LOGERR(ctx, LY_EINT, "Failed to duplicate type.");
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006844 return -1;
6845 }
6846 }
Michal Vasko101658e2018-06-05 15:05:54 +02006847 } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) {
Radek Krejcic13db382016-08-16 10:52:42 +02006848 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6849 * by uses statement until the type is resolved. We do that the same way as uses statements inside
Radek Krejci93def382017-05-24 15:33:48 +02006850 * grouping. The grouping cannot be used unless the unres counter is 0.
Michal Vasko70bf8e52018-03-26 11:32:33 +02006851 * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006852 * of the type's base member. */
6853 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6854 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006855 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006856 LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006857 return -1;
6858 }
Michal Vasko101658e2018-06-05 15:05:54 +02006859 stype->value_flags |= LY_VALUE_UNRESGRP;
Radek Krejcic13db382016-08-16 10:52:42 +02006860 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006861 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006862 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006863 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006864 iff_data = str_snode;
6865 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006866 if (!rc) {
6867 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006868 if (iff_data->infeature) {
6869 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6870 feat = *((struct lys_feature **)item);
6871 if (!feat->depfeatures) {
6872 feat->depfeatures = ly_set_new();
6873 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006874 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006875 }
6876 /* cleanup temporary data */
Michal Vasko53b7da02018-02-13 15:28:42 +01006877 lydict_remove(ctx, iff_data->fname);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006878 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006879 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006880 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006881 case UNRES_FEATURE:
6882 feat = (struct lys_feature *)item;
6883
6884 if (feat->iffeature_size) {
6885 refs = ly_set_new();
6886 procs = ly_set_new();
6887 ly_set_add(procs, feat, 0);
6888
6889 while (procs->number) {
6890 ref = procs->set.g[procs->number - 1];
6891 ly_set_rm_index(procs, procs->number - 1);
6892
6893 for (i = 0; i < ref->iffeature_size; i++) {
6894 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6895 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006896 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006897 if (ref->iffeature[i].features[j - 1] == feat) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006898 LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006899 goto featurecheckdone;
6900 }
6901
6902 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6903 k = refs->number;
6904 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6905 /* not yet seen feature, add it for processing */
6906 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6907 }
6908 }
6909 } else {
6910 /* forward reference */
6911 rc = EXIT_FAILURE;
6912 goto featurecheckdone;
6913 }
6914 }
6915
6916 }
6917 }
6918 rc = EXIT_SUCCESS;
6919
6920featurecheckdone:
6921 ly_set_free(refs);
6922 ly_set_free(procs);
6923 }
6924
6925 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006926 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006927 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006928 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006929 case UNRES_TYPEDEF_DFLT:
6930 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006931 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006932 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006933 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006934 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006935 break;
6936 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006937 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006938 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006939 choic = item;
6940
Radek Krejcie00d2312016-08-12 15:27:49 +02006941 if (!choic->dflt) {
6942 choic->dflt = resolve_choice_dflt(choic, expr);
6943 }
Michal Vasko7955b362015-09-04 14:18:15 +02006944 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006945 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006946 } else {
6947 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006948 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006949 break;
6950 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006951 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006952 break;
6953 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006954 unique_info = (struct unres_list_uniq *)item;
6955 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006956 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006957 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006958 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006959 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006960 case UNRES_XPATH:
6961 node = (struct lys_node *)item;
Michal Vasko895c11f2018-03-12 11:35:58 +01006962 rc = check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006963 break;
Michal Vasko0f437062018-06-08 15:52:39 +02006964 case UNRES_MOD_IMPLEMENT:
6965 rc = lys_make_implemented_r(mod, unres);
6966 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006967 case UNRES_EXT:
6968 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006969 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006970 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006971 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006972 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006973 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006974 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006975 if (eplugin) {
6976 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006977 u = malloc(sizeof *u);
Michal Vasko53b7da02018-02-13 15:28:42 +01006978 LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006979 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006980 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6981 /* something really bad happend since the extension finalization is not actually
6982 * being resolved while adding into unres, so something more serious with the unres
6983 * list itself must happened */
6984 return -1;
6985 }
Radek Krejci80056d52017-01-05 13:13:33 +01006986 }
6987 }
Radek Krejci79685c92017-02-17 10:49:43 +01006988 }
6989 if (!rc || rc == -1) {
6990 /* cleanup on success or fatal error */
6991 if (ext_data->datatype == LYS_IN_YIN) {
6992 /* YIN */
Michal Vasko53b7da02018-02-13 15:28:42 +01006993 lyxml_free(ctx, ext_data->data.yin);
Radek Krejci79685c92017-02-17 10:49:43 +01006994 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01006995 /* YANG */
6996 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01006997 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006998 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006999 }
7000 break;
Radek Krejci80056d52017-01-05 13:13:33 +01007001 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01007002 u = (uint8_t *)str_snode;
7003 ext = (*(struct lys_ext_instance ***)item)[*u];
7004 free(u);
7005
Radek Krejci80056d52017-01-05 13:13:33 +01007006 eplugin = ext->def->plugin;
7007
7008 /* inherit */
7009 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
7010 root = (struct lys_node *)ext->parent;
7011 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
7012 LY_TREE_DFS_BEGIN(root->child, next, node) {
7013 /* first, check if the node already contain instance of the same extension,
7014 * in such a case we won't inherit. In case the node was actually defined as
7015 * augment data, we are supposed to check the same way also the augment node itself */
7016 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
7017 goto inherit_dfs_sibling;
7018 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
7019 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
7020 goto inherit_dfs_sibling;
7021 }
7022
7023 if (eplugin->check_inherit) {
7024 /* we have a callback to check the inheritance, use it */
7025 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
7026 case 0:
7027 /* yes - continue with the inheriting code */
7028 break;
7029 case 1:
7030 /* no - continue with the node's sibling */
7031 goto inherit_dfs_sibling;
7032 case 2:
7033 /* no, but continue with the children, just skip the inheriting code for this node */
7034 goto inherit_dfs_child;
7035 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007036 LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
Radek Krejci80056d52017-01-05 13:13:33 +01007037 ext->def->module->name, ext->def->name, rc);
7038 }
7039 }
7040
7041 /* inherit the extension */
7042 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01007043 LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007044 extlist[node->ext_size] = malloc(sizeof **extlist);
Michal Vasko53b7da02018-02-13 15:28:42 +01007045 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007046 memcpy(extlist[node->ext_size], ext, sizeof *ext);
7047 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
7048
7049 node->ext = extlist;
7050 node->ext_size++;
7051
7052inherit_dfs_child:
7053 /* modification of - select element for the next run - children first */
7054 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
7055 next = NULL;
7056 } else {
7057 next = node->child;
7058 }
7059 if (!next) {
7060inherit_dfs_sibling:
7061 /* no children, try siblings */
7062 next = node->next;
7063 }
7064 while (!next) {
7065 /* go to the parent */
7066 node = lys_parent(node);
7067
7068 /* we are done if we are back in the root (the starter's parent */
7069 if (node == root) {
7070 break;
7071 }
7072
7073 /* parent is already processed, go to its sibling */
7074 next = node->next;
7075 }
7076 }
7077 }
7078 }
7079
7080 /* final check */
7081 if (eplugin->check_result) {
7082 if ((*eplugin->check_result)(ext)) {
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007083 LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed.");
Radek Krejci80056d52017-01-05 13:13:33 +01007084 return -1;
7085 }
7086 }
7087
7088 rc = 0;
7089 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007090 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007091 LOGINT(ctx);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007092 break;
7093 }
7094
Radek Krejci54081ce2016-08-12 15:21:47 +02007095 if (has_str && !rc) {
7096 /* the string is no more needed in case of success.
7097 * In case of forward reference, we will try to resolve the string later */
Michal Vasko53b7da02018-02-13 15:28:42 +01007098 lydict_remove(ctx, str_snode);
Radek Krejci4f78b532016-02-17 13:43:00 +01007099 }
7100
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007101 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007102}
7103
Michal Vaskof02e3742015-08-05 16:27:02 +02007104/* logs directly */
7105static void
Radek Krejci48464ed2016-03-17 15:44:09 +01007106print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007107{
Michal Vaskocb34dc62016-05-20 14:38:37 +02007108 struct lyxml_elem *xml;
7109 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007110 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01007111 const char *name = NULL;
7112 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007113
Michal Vaskof02e3742015-08-05 16:27:02 +02007114 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02007115 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007116 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007117 break;
7118 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007119 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007120 break;
7121 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007122 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
7123 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02007124 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01007125 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02007126 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02007127 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02007128 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
7129 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01007130 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007131 } else {
7132 LY_TREE_FOR(xml->attr, attr) {
7133 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01007134 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007135 break;
7136 }
7137 }
7138 assert(attr);
7139 }
Radek Krejcie534c132016-11-23 13:32:31 +01007140 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007141 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007142 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007143 iff_data = str_node;
7144 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02007145 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02007146 case UNRES_FEATURE:
7147 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
7148 ((struct lys_feature *)item)->name);
7149 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007150 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01007151 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007152 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01007153 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02007154 case UNRES_TYPE_DFLT:
Michal Vaskoba835cd2018-02-23 09:25:48 +01007155 if (*(char **)str_node) {
7156 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node);
Radek Krejci2e2de832016-10-13 16:12:26 +02007157 } /* else no default value in the type itself, but we are checking some restrictions against
7158 * possible default value of some base type. The failure is caused by not resolved base type,
7159 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02007160 break;
7161 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007162 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007163 break;
7164 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01007165 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007166 break;
7167 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01007168 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007169 break;
Michal Vasko7178e692016-02-12 15:58:05 +01007170 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007171 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
7172 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01007173 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02007174 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01007175 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
7176 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02007177 break;
Radek Krejcie534c132016-11-23 13:32:31 +01007178 case UNRES_EXT:
7179 extinfo = (struct unres_ext *)str_node;
7180 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
7181 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
7182 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007183 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007184 LOGINT(NULL);
Michal Vaskof02e3742015-08-05 16:27:02 +02007185 break;
7186 }
7187}
7188
Michal Vasko0f437062018-06-08 15:52:39 +02007189static int
7190resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref,
7191 int print_all_errors, uint32_t *resolved)
7192{
7193 uint32_t i, unres_count, res_count;
7194 int ret = 0, rc;
7195 struct ly_err_item *prev_eitem;
7196 enum int_log_opts prev_ilo;
7197 LY_ERR prev_ly_errno;
7198
7199 /* if there can be no forward references, every failure is final, so we can print it directly */
7200 if (forward_ref) {
7201 prev_ly_errno = ly_errno;
7202 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
7203 }
7204
7205 do {
7206 unres_count = 0;
7207 res_count = 0;
7208
7209 for (i = 0; i < unres->count; ++i) {
7210 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7211 * if-features are resolved here to make sure that we will have all if-features for
7212 * later check of feature circular dependency */
7213 if (unres->type[i] & types) {
7214 ++unres_count;
7215 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7216 if (unres->type[i] == UNRES_EXT_FINALIZE) {
7217 /* to avoid double free */
7218 unres->type[i] = UNRES_RESOLVED;
7219 }
7220 if (!rc || (unres->type[i] == UNRES_XPATH)) {
7221 /* invalid XPath can never cause an error, only a warning */
7222 if (unres->type[i] == UNRES_LIST_UNIQ) {
7223 /* free the allocated structure */
7224 free(unres->item[i]);
7225 }
7226
7227 unres->type[i] = UNRES_RESOLVED;
7228 ++(*resolved);
7229 ++res_count;
7230 } else if ((rc == EXIT_FAILURE) && forward_ref) {
7231 /* forward reference, erase errors */
7232 ly_err_free_next(ctx, prev_eitem);
7233 } else if (print_all_errors) {
7234 /* just so that we quit the loop */
7235 ++res_count;
7236 ret = -1;
7237 } else {
7238 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
7239 return -1;
7240 }
7241 }
7242 }
7243 } while (res_count && (res_count < unres_count));
7244
7245 if (res_count < unres_count) {
7246 assert(forward_ref);
7247 /* just print the errors (but we must free the ones we have and get them again :-/ ) */
7248 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7249
7250 for (i = 0; i < unres->count; ++i) {
7251 if (unres->type[i] & types) {
7252 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7253 }
7254 }
7255 return -1;
7256 }
7257
7258 if (forward_ref) {
7259 /* restore log */
7260 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7261 ly_errno = prev_ly_errno;
7262 }
7263
7264 return ret;
7265}
7266
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007267/**
Michal Vaskobb211122015-08-19 14:03:11 +02007268 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007269 *
7270 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007271 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007272 *
Michal Vasko92b8a382015-08-19 14:03:49 +02007273 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007274 */
Michal Vaskof02e3742015-08-05 16:27:02 +02007275int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007276resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02007277{
Michal Vasko0f437062018-06-08 15:52:39 +02007278 uint32_t resolved = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007279
7280 assert(unres);
7281
Michal Vaskoe8734262016-09-29 14:12:06 +02007282 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko51054ca2015-08-12 12:20:00 +02007283
Michal Vasko0f437062018-06-08 15:52:39 +02007284 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7285 * if-features are resolved here to make sure that we will have all if-features for
7286 * later check of feature circular dependency */
7287 if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF
7288 | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT,
7289 mod->ctx, 1, 0, &resolved)) {
Michal Vasko92b8a382015-08-19 14:03:49 +02007290 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007291 }
7292
Michal Vasko0f437062018-06-08 15:52:39 +02007293 /* another batch of resolved items */
7294 if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT
7295 | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) {
7296 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007297 }
7298
Michal Vasko0f437062018-06-08 15:52:39 +02007299 /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
7300 if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) {
7301 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007302 }
7303
Michal Vaskoe8734262016-09-29 14:12:06 +02007304 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01007305 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007306 return EXIT_SUCCESS;
7307}
7308
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007309/**
Michal Vaskobb211122015-08-19 14:03:11 +02007310 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007311 *
7312 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007313 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007314 * @param[in] item Item to resolve. Type determined by \p type.
7315 * @param[in] type Type of the unresolved item.
7316 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007317 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007318 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007319 */
7320int
Radek Krejci48464ed2016-03-17 15:44:09 +01007321unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7322 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007323{
Radek Krejci54081ce2016-08-12 15:21:47 +02007324 int rc;
7325 const char *dictstr;
7326
7327 dictstr = lydict_insert(mod->ctx, str, 0);
7328 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7329
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007330 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007331 lydict_remove(mod->ctx, dictstr);
7332 }
7333 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007334}
7335
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007336/**
Michal Vaskobb211122015-08-19 14:03:11 +02007337 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007338 *
7339 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007340 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007341 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007342 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007343 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007344 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007345 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007346 */
7347int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007348unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007349 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007350{
Michal Vasko53b7da02018-02-13 15:28:42 +01007351 int rc;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007352 uint32_t u;
Michal Vasko53b7da02018-02-13 15:28:42 +01007353 enum int_log_opts prev_ilo;
7354 struct ly_err_item *prev_eitem;
7355 LY_ERR prev_ly_errno;
Michal Vasko88c29542015-11-27 14:57:53 +01007356 struct lyxml_elem *yin;
Michal Vasko53b7da02018-02-13 15:28:42 +01007357 struct ly_ctx *ctx = mod->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007358
Michal Vasko0f437062018-06-08 15:52:39 +02007359 assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID)
7360 && (type != UNRES_WHEN) && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007361
Radek Krejci850a5de2016-11-08 14:06:40 +01007362 /* check for duplicities in unres */
7363 for (u = 0; u < unres->count; u++) {
7364 if (unres->type[u] == type && unres->item[u] == item &&
7365 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007366 /* duplication can happen when the node contains multiple statements of the same type to check,
7367 * this can happen for example when refinement is being applied, so we just postpone the processing
7368 * and do not duplicate the information */
7369 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007370 }
7371 }
7372
Michal Vasko0f437062018-06-08 15:52:39 +02007373 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02007374 /* extension finalization is not even tried when adding the item into the inres list,
Michal Vasko0f437062018-06-08 15:52:39 +02007375 * xpath is not tried because it would hide some potential warnings,
7376 * implementing module must be deferred because some other nodes can be added that will need to be traversed
7377 * and their targets made implemented */
Radek Krejcic293bac2017-02-27 11:25:28 +01007378 rc = EXIT_FAILURE;
7379 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007380 prev_ly_errno = ly_errno;
7381 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007382
Michal Vasko53b7da02018-02-13 15:28:42 +01007383 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007384 if (rc != EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007385 ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0);
7386 if (rc != -1) {
7387 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007388 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007389
Radek Krejci80056d52017-01-05 13:13:33 +01007390 if (type == UNRES_LIST_UNIQ) {
7391 /* free the allocated structure */
7392 free(item);
7393 } else if (rc == -1 && type == UNRES_IFFEAT) {
7394 /* free the allocated resources */
7395 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007396 }
Radek Krejci80056d52017-01-05 13:13:33 +01007397 return rc;
7398 } else {
7399 /* erase info about validation errors */
Michal Vasko53b7da02018-02-13 15:28:42 +01007400 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7401 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007402 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007403
Radek Krejci80056d52017-01-05 13:13:33 +01007404 print_unres_schema_item_fail(item, type, snode);
7405
7406 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7407 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7408 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7409 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7410 lyxml_unlink_elem(mod->ctx, yin, 1);
7411 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7412 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007413 }
Michal Vasko88c29542015-11-27 14:57:53 +01007414 }
7415
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007416 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007417 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Michal Vasko53b7da02018-02-13 15:28:42 +01007418 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007419 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007420 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007421 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007422 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007423 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Michal Vasko53b7da02018-02-13 15:28:42 +01007424 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007425 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007426 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01007427 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007428 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007429
Michal Vasko3767fb22016-07-21 12:10:57 +02007430 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007431}
7432
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007433/**
Michal Vaskobb211122015-08-19 14:03:11 +02007434 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007435 *
7436 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007437 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007438 * @param[in] item Old item to be resolved.
7439 * @param[in] type Type of the old unresolved item.
7440 * @param[in] new_item New item to use in the duplicate.
7441 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007442 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007443 */
Michal Vaskodad19402015-08-06 09:51:53 +02007444int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007445unres_schema_dup(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, void *new_item)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007446{
7447 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007448 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007449 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007450
Michal Vaskocf024702015-10-08 15:01:42 +02007451 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007452
Radek Krejcid09d1a52016-08-11 14:05:45 +02007453 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7454 if (type == UNRES_LIST_UNIQ) {
7455 aux_uniq.list = item;
7456 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7457 item = &aux_uniq;
7458 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007459 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007460
7461 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007462 if (type == UNRES_LIST_UNIQ) {
7463 free(new_item);
7464 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007465 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007466 }
7467
Radek Krejcic79c6b12016-07-26 15:11:49 +02007468 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007469 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007470 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007471 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007472 return -1;
7473 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007474 } else if (type == UNRES_IFFEAT) {
7475 /* duplicate unres_iffeature_data */
7476 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01007477 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007478 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7479 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7480 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007481 LOGINT(mod->ctx);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007482 return -1;
7483 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007484 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007485 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007486 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007487 return -1;
7488 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007489 }
Michal Vaskodad19402015-08-06 09:51:53 +02007490
7491 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007492}
7493
Michal Vaskof02e3742015-08-05 16:27:02 +02007494/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007495int
Michal Vasko878e38d2016-09-05 12:17:53 +02007496unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007497{
Michal Vasko878e38d2016-09-05 12:17:53 +02007498 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007499 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007500
Radek Krejciddddd0d2017-01-20 15:20:46 +01007501 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007502 i = start_on_backwards;
7503 } else {
7504 i = unres->count - 1;
7505 }
7506 for (; i > -1; i--) {
7507 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007508 continue;
7509 }
7510 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007511 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007512 break;
7513 }
7514 } else {
7515 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7516 aux_uniq2 = (struct unres_list_uniq *)item;
7517 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007518 break;
7519 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007520 }
7521 }
7522
Michal Vasko878e38d2016-09-05 12:17:53 +02007523 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007524}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007525
Michal Vaskoede9c472016-06-07 09:38:15 +02007526static void
7527unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7528{
7529 struct lyxml_elem *yin;
7530 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007531 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007532
7533 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007534 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007535 case UNRES_TYPE_DER:
7536 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7537 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7538 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007539 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007540 lydict_remove(ctx, yang->name);
7541 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007542 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7543 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7544 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007545 } else {
7546 lyxml_free(ctx, yin);
7547 }
7548 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007549 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007550 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7551 lydict_remove(ctx, iff_data->fname);
7552 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007553 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007554 case UNRES_IDENT:
7555 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007556 case UNRES_CHOICE_DFLT:
7557 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007558 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7559 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007560 case UNRES_LIST_UNIQ:
7561 free(unres->item[i]);
7562 break;
PavolVicanc1807262017-01-31 18:00:27 +01007563 case UNRES_EXT:
7564 free(unres->str_snode[i]);
7565 break;
PavolVicanfcc98762017-09-01 15:51:39 +02007566 case UNRES_EXT_FINALIZE:
7567 free(unres->str_snode[i]);
Michal Vaskoede9c472016-06-07 09:38:15 +02007568 default:
7569 break;
7570 }
7571 unres->type[i] = UNRES_RESOLVED;
7572}
7573
Michal Vasko88c29542015-11-27 14:57:53 +01007574void
Michal Vasko44ab1462017-05-18 13:18:36 +02007575unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007576{
7577 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007578 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007579
Radek Krejcic071c542016-01-27 14:57:51 +01007580 if (!unres || !(*unres)) {
7581 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007582 }
7583
Michal Vasko44ab1462017-05-18 13:18:36 +02007584 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007585
7586 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007587 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007588 if ((*unres)->type[i] != UNRES_RESOLVED) {
7589 unresolved++;
7590 }
7591 continue;
7592 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007593
7594 /* free heap memory for the specific item */
7595 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007596 }
7597
Michal Vaskoede9c472016-06-07 09:38:15 +02007598 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007599 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007600 free((*unres)->item);
7601 free((*unres)->type);
7602 free((*unres)->str_snode);
7603 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007604 free((*unres));
7605 (*unres) = NULL;
7606 }
Michal Vasko88c29542015-11-27 14:57:53 +01007607}
7608
Michal Vaskoff690e72017-08-03 14:25:07 +02007609/* check whether instance-identifier points outside its data subtree (for operation it is any node
7610 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007611static int
7612check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7613{
Michal Vaskoff690e72017-08-03 14:25:07 +02007614 const struct lys_node *op_node, *first_node;
Michal Vasko53b7da02018-02-13 15:28:42 +01007615 enum int_log_opts prev_ilo;
Michal Vasko2e80aff2018-12-07 08:41:07 +01007616 char *buf, *tmp;
Michal Vasko53b7da02018-02-13 15:28:42 +01007617 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007618
Radek Krejci034cb102017-08-01 15:45:13 +02007619 if (!json_instid || !json_instid[0]) {
7620 /* no/empty value */
7621 return 0;
7622 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007623
7624 for (op_node = lys_parent(sleaf);
7625 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7626 op_node = lys_parent(op_node));
7627
7628 if (op_node && lys_parent(op_node)) {
7629 /* nested operation - any absolute path is external */
7630 return 1;
7631 }
7632
7633 /* get the first node from the instid */
Michal Vasko2e80aff2018-12-07 08:41:07 +01007634 tmp = strchr(json_instid + 1, '/');
7635 if (!tmp) {
7636 return 0;
7637 }
7638 buf = strndup(json_instid, tmp - json_instid);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007639 if (!buf) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007640 /* so that we do not have to bother with logging, say it is not external */
7641 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007642 }
7643
Michal Vaskoff690e72017-08-03 14:25:07 +02007644 /* find the first schema node, do not log */
Michal Vasko53b7da02018-02-13 15:28:42 +01007645 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoff690e72017-08-03 14:25:07 +02007646 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
Michal Vasko53b7da02018-02-13 15:28:42 +01007647 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007648
Michal Vasko53b7da02018-02-13 15:28:42 +01007649 free(buf);
Michal Vaskoff690e72017-08-03 14:25:07 +02007650 if (!first_node) {
7651 /* unknown path, say it is not external */
Michal Vaskoff690e72017-08-03 14:25:07 +02007652 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007653 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007654
7655 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7656
Michal Vaskoff690e72017-08-03 14:25:07 +02007657 if (op_node && (op_node != first_node)) {
7658 /* it is a top-level operation, so we're good if it points somewhere inside it */
7659 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007660 }
7661
7662 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7663 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007664 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007665}
7666
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007667/**
7668 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7669 *
7670 * @param[in] data Data node where the path is used
7671 * @param[in] path Instance-identifier node value.
7672 * @param[in,out] ret Resolved instance or NULL.
7673 *
7674 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7675 */
7676static int
7677resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7678{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007679 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007680 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007681 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007682 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007683 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007684 char *str;
7685 int mod_len, name_len, has_predicate;
7686 struct unres_data node_match;
7687
7688 memset(&node_match, 0, sizeof node_match);
7689 *ret = NULL;
7690
7691 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007692 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007693 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007694 if (root->prev) {
7695 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007696 }
7697
7698 /* search for the instance node */
7699 while (path[i]) {
7700 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7701 if (j <= 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007702 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007703 goto error;
7704 }
7705 i += j;
7706
Michal Vasko1b6ca962017-08-03 14:23:09 +02007707 if (model) {
7708 str = strndup(model, mod_len);
7709 if (!str) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007710 LOGMEM(ctx);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007711 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007712 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007713 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007714 if (ctx->data_clb) {
7715 if (!mod) {
7716 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7717 } else if (!mod->implemented) {
7718 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7719 }
7720 }
7721 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007722
Michal Vasko1b6ca962017-08-03 14:23:09 +02007723 if (!mod || !mod->implemented || mod->disabled) {
7724 break;
7725 }
7726 } else if (!prev_mod) {
7727 /* first iteration and we are missing module name */
Michal Vaskoaf8ec362018-03-28 09:08:09 +02007728 LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name);
7729 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node.");
Michal Vasko1b6ca962017-08-03 14:23:09 +02007730 goto error;
7731 } else {
7732 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007733 }
7734
Radek Krejci2c822ed2017-08-03 14:23:36 +02007735 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007736 /* no instance exists */
7737 break;
7738 }
7739
7740 if (has_predicate) {
7741 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007742 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007743 /* index of the current node (for lists with position predicates) */
7744 cur_idx = 1;
7745 while (j < (signed)node_match.count) {
7746 node = node_match.node[j];
7747 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7748 if (parsed < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007749 LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007750 goto error;
7751 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007752
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007753 if (!node) {
7754 /* current node does not satisfy the predicate */
7755 unres_data_del(&node_match, j);
7756 } else {
7757 ++j;
7758 }
7759 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007760 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007761
7762 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007763 } else if (node_match.count) {
7764 /* check that we are not addressing lists */
7765 for (j = 0; (unsigned)j < node_match.count; ++j) {
7766 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7767 unres_data_del(&node_match, j--);
7768 }
7769 }
7770 if (!node_match.count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007771 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007772 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007773 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007774
7775 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007776 }
7777
7778 if (!node_match.count) {
7779 /* no instance exists */
7780 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007781 LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007782 return EXIT_FAILURE;
7783 }
7784 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7785 return EXIT_SUCCESS;
7786 } else if (node_match.count > 1) {
7787 /* instance identifier must resolve to a single node */
Michal Vasko53b7da02018-02-13 15:28:42 +01007788 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007789 goto error;
7790 } else {
7791 /* we have required result, remember it and cleanup */
7792 *ret = node_match.node[0];
7793 free(node_match.node);
7794 return EXIT_SUCCESS;
7795 }
7796
7797error:
7798 /* cleanup */
7799 free(node_match.node);
7800 return -1;
7801}
7802
7803static int
7804resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007805{
Michal Vaskoca16cb32017-07-10 11:50:33 +02007806 struct ly_set *set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007807 uint32_t i;
7808
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007809 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007810
Michal Vaskoca16cb32017-07-10 11:50:33 +02007811 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vasko50576712017-07-28 12:28:33 +02007812 set = lyd_find_path((struct lyd_node *)leaf, path);
Michal Vaskoca16cb32017-07-10 11:50:33 +02007813 if (!set) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007814 return -1;
7815 }
7816
Michal Vaskoca16cb32017-07-10 11:50:33 +02007817 for (i = 0; i < set->number; ++i) {
7818 if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7819 continue;
7820 }
7821
Radek Krejci1899d6a2016-11-03 13:48:07 +01007822 /* not that the value is already in canonical form since the parsers does the conversion,
7823 * so we can simply compare just the values */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007824 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) {
Radek Krejci1899d6a2016-11-03 13:48:07 +01007825 /* we have the match */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007826 *ret = set->set.d[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007827 break;
7828 }
7829 }
7830
Michal Vaskoca16cb32017-07-10 11:50:33 +02007831 ly_set_free(set);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007832
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007833 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007834 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007835 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007836 LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007837 return EXIT_FAILURE;
7838 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007839 LOGVRB("There is no leafref \"%s\" with the value \"%s\", but it is not required.", path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007840 }
7841 }
7842
7843 return EXIT_SUCCESS;
7844}
7845
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007846/* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007847int
7848resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7849 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007850{
Michal Vasko53b7da02018-02-13 15:28:42 +01007851 struct ly_ctx *ctx = leaf->schema->module->ctx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007852 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007853 struct lyd_node *ret;
Michal Vasko53b7da02018-02-13 15:28:42 +01007854 enum int_log_opts prev_ilo;
7855 int found, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007856 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007857
7858 assert(type->base == LY_TYPE_UNION);
7859
Michal Vasko101658e2018-06-05 15:05:54 +02007860 if ((leaf->value_type == LY_TYPE_UNION) || ((leaf->value_type == LY_TYPE_INST) && (leaf->value_flags & LY_VALUE_UNRES))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007861 /* either NULL or instid previously converted to JSON */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007862 json_val = lydict_insert(ctx, leaf->value.string, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007863 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007864
Michal Vaskofd6c6502017-01-06 12:15:41 +01007865 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007866 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007867 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007868 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007869
7870 /* turn logging off, we are going to try to validate the value with all the types in order */
Michal Vasko53b7da02018-02-13 15:28:42 +01007871 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007872
7873 t = NULL;
7874 found = 0;
7875 while ((t = lyp_get_next_union_type(type, t, &found))) {
7876 found = 0;
7877
7878 switch (t->base) {
7879 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007880 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7881 req_inst = -1;
7882 } else {
7883 req_inst = t->info.lref.req;
7884 }
7885
7886 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007887 if (store) {
7888 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7889 /* valid resolved */
7890 leaf->value.leafref = ret;
7891 leaf->value_type = LY_TYPE_LEAFREF;
7892 } else {
7893 /* valid unresolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01007894 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko35f46a82018-05-30 10:44:11 +02007895 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007896 return -1;
7897 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007898 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007899 }
7900 }
7901
7902 success = 1;
7903 }
7904 break;
7905 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007906 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7907 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7908 req_inst = -1;
7909 } else {
7910 req_inst = t->info.inst.req;
7911 }
7912
Michal Vaskod3a03112017-01-23 09:56:02 +01007913 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007914 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007915 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007916 /* valid resolved */
7917 leaf->value.instance = ret;
7918 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007919
Michal Vaskofd6c6502017-01-06 12:15:41 +01007920 if (json_val) {
7921 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7922 leaf->value_str = json_val;
7923 json_val = NULL;
7924 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007925 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007926 /* valid unresolved */
7927 if (json_val) {
7928 /* put the JSON val back */
7929 leaf->value.string = json_val;
7930 json_val = NULL;
7931 } else {
7932 leaf->value.instance = NULL;
7933 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02007934 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02007935 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007936 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007937 }
7938
7939 success = 1;
7940 }
7941 break;
7942 default:
Michal Vasko35f46a82018-05-30 10:44:11 +02007943 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007944 success = 1;
7945 }
7946 break;
7947 }
7948
7949 if (success) {
7950 break;
7951 }
7952
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007953 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007954 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007955 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007956 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007957 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007958 }
7959
7960 /* turn logging back on */
Michal Vasko53b7da02018-02-13 15:28:42 +01007961 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007962
7963 if (json_val) {
7964 if (!success) {
7965 /* put the value back for now */
7966 assert(leaf->value_type == LY_TYPE_UNION);
7967 leaf->value.string = json_val;
7968 } else {
7969 /* value was ultimately useless, but we could not have known */
7970 lydict_remove(leaf->schema->module->ctx, json_val);
7971 }
7972 }
7973
Michal Vaskofd6c6502017-01-06 12:15:41 +01007974 if (success) {
7975 if (resolved_type) {
7976 *resolved_type = t;
7977 }
7978 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007979 /* not found and it is required */
Michal Vasko53b7da02018-02-13 15:28:42 +01007980 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007981 return EXIT_FAILURE;
7982 }
7983
7984 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007985
Radek Krejci9b6aad22016-09-20 15:55:51 +02007986}
7987
Michal Vasko8bcdf292015-08-19 14:04:43 +02007988/**
7989 * @brief Resolve a single unres data item. Logs directly.
7990 *
Michal Vaskocf024702015-10-08 15:01:42 +02007991 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007992 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007993 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007994 *
7995 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7996 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007997int
Michal Vasko0b963112017-08-11 12:45:36 +02007998resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail, struct lys_when **failed_when)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007999{
Michal Vasko3cfa3182017-01-17 10:00:58 +01008000 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02008001 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008002 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008003 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008004
Michal Vasko83a6c462015-10-08 16:43:53 +02008005 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02008006 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008007
Michal Vaskocf024702015-10-08 15:01:42 +02008008 switch (type) {
8009 case UNRES_LEAFREF:
8010 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008011 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008012 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
8013 req_inst = -1;
8014 } else {
8015 req_inst = sleaf->type.info.lref.req;
8016 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008017 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
8018 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01008019 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008020 /* valid resolved */
Michal Vasko70bf8e52018-03-26 11:32:33 +02008021 if (leaf->value_type == LY_TYPE_BITS) {
Michal Vasko1c8567a2017-01-05 13:42:27 +01008022 free(leaf->value.bit);
8023 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008024 leaf->value.leafref = ret;
8025 leaf->value_type = LY_TYPE_LEAFREF;
Michal Vasko101658e2018-06-05 15:05:54 +02008026 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008027 } else {
8028 /* valid unresolved */
Michal Vasko101658e2018-06-05 15:05:54 +02008029 if (!(leaf->value_flags & LY_VALUE_UNRES)) {
Michal Vasko35f46a82018-05-30 10:44:11 +02008030 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008031 return -1;
8032 }
8033 }
8034 }
8035 leaf->validity &= ~LYD_VAL_LEAFREF;
8036 } else {
8037 return rc;
8038 }
8039 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008040
Michal Vaskocf024702015-10-08 15:01:42 +02008041 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02008042 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008043 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
8044 if (ext_dep == -1) {
8045 return -1;
8046 }
8047
8048 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
8049 req_inst = -1;
8050 } else {
8051 req_inst = sleaf->type.info.inst.req;
8052 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008053 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
8054 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008055 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008056 /* valid resolved */
8057 leaf->value.instance = ret;
8058 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008059 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008060 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008061 /* valid unresolved */
8062 leaf->value.instance = NULL;
Michal Vasko70bf8e52018-03-26 11:32:33 +02008063 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008064 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008065 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008066 } else {
8067 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008068 }
Michal Vaskocf024702015-10-08 15:01:42 +02008069 break;
8070
Radek Krejci7de36cf2016-09-12 16:18:50 +02008071 case UNRES_UNION:
8072 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01008073 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02008074
Michal Vaskocf024702015-10-08 15:01:42 +02008075 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02008076 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02008077 return rc;
8078 }
8079 break;
8080
Michal Vaskobf19d252015-10-08 15:39:17 +02008081 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008082 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02008083 return rc;
8084 }
8085 break;
8086
8087 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008088 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02008089 return rc;
8090 }
8091 break;
8092
Michal Vaskocf024702015-10-08 15:01:42 +02008093 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01008094 LOGINT(NULL);
Michal Vasko8bcdf292015-08-19 14:04:43 +02008095 return -1;
8096 }
8097
8098 return EXIT_SUCCESS;
8099}
8100
8101/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01008102 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02008103 *
8104 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02008105 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008106 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01008107 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008108 */
8109int
Radek Krejci0b7704f2016-03-18 12:16:14 +01008110unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02008111{
Radek Krejci03b71f72016-03-16 11:10:09 +01008112 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02008113 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02008114 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02008115
Radek Krejci03b71f72016-03-16 11:10:09 +01008116 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01008117 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01008118 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008119 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01008120 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01008121 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008122 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008123
Radek Krejci0b7704f2016-03-18 12:16:14 +01008124 if (type == UNRES_WHEN) {
8125 /* remove previous result */
8126 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008127 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008128
Michal Vasko53b7da02018-02-13 15:28:42 +01008129 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008130}
8131
8132/**
8133 * @brief Resolve every unres data item in the structure. Logs directly.
8134 *
Michal Vasko660582a2018-03-19 10:10:08 +01008135 * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected,
8136 * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit
8137 * non-presence containers may need to be deleted).
Radek Krejci082c84f2016-10-17 16:33:06 +02008138 *
8139 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
8140 *
Michal Vasko53b7da02018-02-13 15:28:42 +01008141 * @param[in] ctx Context used.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008142 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02008143 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
8144 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008145 *
8146 * @return EXIT_SUCCESS on success, -1 on error.
8147 */
8148int
Michal Vasko53b7da02018-02-13 15:28:42 +01008149resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008150{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008151 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008152 int rc, progress, ignore_fail;
Michal Vasko53b7da02018-02-13 15:28:42 +01008153 enum int_log_opts prev_ilo;
8154 struct ly_err_item *prev_eitem;
mohitarora2489837dc2018-05-01 15:09:36 +05308155 LY_ERR prev_ly_errno = ly_errno;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008156 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02008157 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008158
Radek Krejci082c84f2016-10-17 16:33:06 +02008159 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01008160 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01008161
8162 if (!unres->count) {
8163 return EXIT_SUCCESS;
8164 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008165
Michal Vasko7fa93142018-03-19 09:59:10 +01008166 if (options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008167 ignore_fail = 1;
8168 } else if (options & LYD_OPT_NOEXTDEPS) {
8169 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008170 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008171 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008172 }
8173
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008174 LOGVRB("Resolving unresolved data nodes and their constraints...");
Michal Vasko7fa93142018-03-19 09:59:10 +01008175 if (!ignore_fail) {
8176 /* remember logging state only if errors are generated and valid */
Michal Vasko7fa93142018-03-19 09:59:10 +01008177 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
8178 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008179
Michal Vasko7fa93142018-03-19 09:59:10 +01008180 /*
8181 * when-stmt first
8182 */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008183 first = 1;
8184 stmt_count = 0;
8185 resolved = 0;
8186 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01008187 do {
Michal Vasko7fa93142018-03-19 09:59:10 +01008188 if (!ignore_fail) {
8189 ly_err_free_next(ctx, prev_eitem);
8190 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008191 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02008192 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008193 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008194 continue;
8195 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008196 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008197 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008198 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008199 }
8200
8201 /* resolve when condition only when all parent when conditions are already resolved */
8202 for (parent = unres->node[i]->parent;
8203 parent && LYD_WHEN_DONE(parent->when_status);
8204 parent = parent->parent) {
8205 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
8206 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02008207 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01008208 */
8209 unres->node[i]->when_status |= LYD_WHEN_FALSE;
8210 unres->type[i] = UNRES_RESOLVED;
8211 resolved++;
8212 break;
8213 }
8214 }
8215 if (parent) {
8216 continue;
8217 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008218
Michal Vasko0b963112017-08-11 12:45:36 +02008219 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01008220 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02008221 /* finish with error/delete the node only if when was false, an external dependency was not required,
8222 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02008223 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vaskoc04173b2018-03-09 10:43:22 +01008224 && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) {
Radek Krejci082c84f2016-10-17 16:33:06 +02008225 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01008226 /* false when condition */
Michal Vasko53b7da02018-02-13 15:28:42 +01008227 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008228 } /* follows else */
8229
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008230 /* auto-delete */
Michal Vasko53b7da02018-02-13 15:28:42 +01008231 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond);
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008232
Radek Krejci0c0086a2016-03-24 15:20:28 +01008233 /* only unlink now, the subtree can contain another nodes stored in the unres list */
8234 /* if it has parent non-presence containers that would be empty, we should actually
8235 * remove the container
8236 */
Radek Krejci2537fd32016-09-07 16:22:41 +02008237 for (parent = unres->node[i];
8238 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
8239 parent = parent->parent) {
8240 if (((struct lys_node_container *)parent->parent->schema)->presence) {
8241 /* presence container */
8242 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008243 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008244 if (parent->next || parent->prev != parent) {
8245 /* non empty (the child we are in and we are going to remove is not the only child) */
8246 break;
8247 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008248 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008249 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008250
Radek Krejci0c0086a2016-03-24 15:20:28 +01008251 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008252 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01008253 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008254
Radek Krejci0b7704f2016-03-18 12:16:14 +01008255 lyd_unlink(unres->node[i]);
8256 unres->type[i] = UNRES_DELETE;
8257 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01008258
8259 /* update the rest of unres items */
8260 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01008261 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01008262 continue;
8263 }
8264
8265 /* test if the node is in subtree to be deleted */
8266 for (parent = unres->node[j]; parent; parent = parent->parent) {
8267 if (parent == unres->node[i]) {
8268 /* yes, it is */
8269 unres->type[j] = UNRES_RESOLVED;
8270 resolved++;
8271 break;
8272 }
8273 }
8274 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008275 } else {
8276 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01008277 }
Michal Vasko7fa93142018-03-19 09:59:10 +01008278 if (!ignore_fail) {
8279 ly_err_free_next(ctx, prev_eitem);
8280 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008281 resolved++;
8282 progress = 1;
8283 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008284 goto error;
Radek Krejci2467a492016-10-24 15:16:59 +02008285 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01008286 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008287 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008288 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01008289
Radek Krejci0b7704f2016-03-18 12:16:14 +01008290 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008291 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008292 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008293 }
8294
8295 for (i = 0; del_items && i < unres->count; i++) {
8296 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
8297 if (unres->type[i] != UNRES_DELETE) {
8298 continue;
8299 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008300 if (!unres->node[i]) {
8301 unres->type[i] = UNRES_RESOLVED;
8302 del_items--;
8303 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008304 }
8305
8306 /* really remove the complete subtree */
8307 lyd_free(unres->node[i]);
8308 unres->type[i] = UNRES_RESOLVED;
8309 del_items--;
8310 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008311
Michal Vasko7fa93142018-03-19 09:59:10 +01008312 /*
8313 * now leafrefs
8314 */
8315 if (options & LYD_OPT_TRUSTED) {
8316 /* we want to attempt to resolve leafrefs */
8317 assert(!ignore_fail);
8318 ignore_fail = 1;
8319
8320 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8321 ly_errno = prev_ly_errno;
8322 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008323 first = 1;
8324 stmt_count = 0;
8325 resolved = 0;
8326 do {
8327 progress = 0;
8328 for (i = 0; i < unres->count; i++) {
8329 if (unres->type[i] != UNRES_LEAFREF) {
8330 continue;
8331 }
8332 if (first) {
8333 /* count leafref nodes in unres list */
8334 stmt_count++;
8335 }
8336
Michal Vasko0b963112017-08-11 12:45:36 +02008337 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008338 if (!rc) {
8339 unres->type[i] = UNRES_RESOLVED;
Michal Vasko7fa93142018-03-19 09:59:10 +01008340 if (!ignore_fail) {
8341 ly_err_free_next(ctx, prev_eitem);
8342 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008343 resolved++;
8344 progress = 1;
8345 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008346 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008347 } /* else forward reference */
8348 }
8349 first = 0;
8350 } while (progress && resolved < stmt_count);
8351
8352 /* do we have some unresolved leafrefs? */
8353 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008354 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008355 }
8356
Michal Vasko7fa93142018-03-19 09:59:10 +01008357 if (!ignore_fail) {
8358 /* log normally now, throw away irrelevant errors */
8359 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8360 ly_errno = prev_ly_errno;
8361 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008362
Michal Vasko7fa93142018-03-19 09:59:10 +01008363 /*
8364 * rest
8365 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008366 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008367 if (unres->type[i] == UNRES_RESOLVED) {
8368 continue;
8369 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008370 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008371
Michal Vasko0b963112017-08-11 12:45:36 +02008372 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008373 if (rc) {
8374 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008375 return -1;
8376 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008377
8378 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008379 }
8380
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008381 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008382 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008383 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01008384
8385error:
Michal Vasko7fa93142018-03-19 09:59:10 +01008386 if (!ignore_fail) {
8387 /* print all the new errors */
8388 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
8389 /* do not restore ly_errno, it was udpated properly */
8390 }
Michal Vasko53b7da02018-02-13 15:28:42 +01008391 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008392}