blob: 1394f4067e86485d7277467e557ba6b99bd78d91 [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 Vaskod24dd012016-09-30 12:20:22 +020034int
35parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num)
Michal Vasko4d1f0482016-09-19 14:35:06 +020036{
37 const char *ptr;
38 int minus = 0;
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020039 int64_t ret = 0, prev_ret;
Radek Krejcibf47a822016-11-04 10:06:08 +010040 int8_t str_exp, str_dig = -1, trailing_zeros = 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +020041
42 ptr = *str_num;
43
44 if (ptr[0] == '-') {
45 minus = 1;
46 ++ptr;
Radek Krejci51673202016-11-01 17:00:32 +010047 } else if (ptr[0] == '+') {
48 ++ptr;
Michal Vasko4d1f0482016-09-19 14:35:06 +020049 }
50
Michal Vaskod24dd012016-09-30 12:20:22 +020051 if (!isdigit(ptr[0])) {
52 /* there must be at least one */
53 return 1;
54 }
55
Michal Vasko4d1f0482016-09-19 14:35:06 +020056 for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) {
57 if (str_exp > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020058 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020059 }
60
61 if (ptr[0] == '.') {
62 if (ptr[1] == '.') {
63 /* it's the next interval */
64 break;
65 }
66 ++str_dig;
67 } else {
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020068 prev_ret = ret;
69 if (minus) {
70 ret = ret * 10 - (ptr[0] - '0');
71 if (ret > prev_ret) {
72 return 1;
73 }
74 } else {
75 ret = ret * 10 + (ptr[0] - '0');
76 if (ret < prev_ret) {
77 return 1;
78 }
79 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020080 if (str_dig > -1) {
81 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010082 if (ptr[0] == '0') {
83 /* possibly trailing zero */
84 trailing_zeros++;
85 } else {
86 trailing_zeros = 0;
87 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020088 }
89 ++str_exp;
90 }
91 }
Michal Vaskod24dd012016-09-30 12:20:22 +020092 if (str_dig == 0) {
93 /* no digits after '.' */
94 return 1;
95 } else if (str_dig == -1) {
96 /* there are 0 numbers after the floating point */
Michal Vasko4d1f0482016-09-19 14:35:06 +020097 str_dig = 0;
98 }
Radek Krejcibf47a822016-11-04 10:06:08 +010099 /* remove trailing zeros */
100 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +0100101 str_dig -= trailing_zeros;
102 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +0100103 ret = ret / dec_pow(trailing_zeros);
104 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200105
106 /* it's parsed, now adjust the number based on fraction-digits, if needed */
107 if (str_dig < dig) {
108 if ((str_exp - 1) + (dig - str_dig) > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200109 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200110 }
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200111 prev_ret = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200112 ret *= dec_pow(dig - str_dig);
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200113 if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) {
114 return 1;
115 }
116
Michal Vasko4d1f0482016-09-19 14:35:06 +0200117 }
118 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200119 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200120 }
121
Michal Vasko4d1f0482016-09-19 14:35:06 +0200122 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200123 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200124
Michal Vaskod24dd012016-09-30 12:20:22 +0200125 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200126}
127
128/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200129 * @brief Parse an identifier.
130 *
131 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
132 * identifier = (ALPHA / "_")
133 * *(ALPHA / DIGIT / "_" / "-" / ".")
134 *
Michal Vaskobb211122015-08-19 14:03:11 +0200135 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200136 *
137 * @return Number of characters successfully parsed.
138 */
Radek Krejcidce5f972017-09-12 15:47:49 +0200139unsigned int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200140parse_identifier(const char *id)
141{
Radek Krejcidce5f972017-09-12 15:47:49 +0200142 unsigned int parsed = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200143
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100144 assert(id);
145
Radek Krejci6dc53a22015-08-17 13:27:59 +0200146 if (!isalpha(id[0]) && (id[0] != '_')) {
147 return -parsed;
148 }
149
150 ++parsed;
151 ++id;
152
153 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
154 ++parsed;
155 ++id;
156 }
157
158 return parsed;
159}
160
161/**
162 * @brief Parse a node-identifier.
163 *
Michal Vasko723e50c2015-10-20 15:20:29 +0200164 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200165 *
Michal Vaskobb211122015-08-19 14:03:11 +0200166 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200167 * @param[out] mod_name Points to the module name, NULL if there is not any.
168 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200169 * @param[out] name Points to the node name.
170 * @param[out] nam_len Length of the node name.
Michal Vasko50576712017-07-28 12:28:33 +0200171 * @param[out] all_desc Whether the path starts with '/', only supported in extended paths.
PavolVicanb28bbff2018-02-21 00:44:02 +0100172 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200173 *
174 * @return Number of characters successfully parsed,
175 * positive on success, negative on failure.
176 */
177static int
Michal Vasko50576712017-07-28 12:28:33 +0200178parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
179 int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200180{
PavolVican195cf392018-02-23 13:24:45 +0100181 int parsed = 0, ret, all_desc_local = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200182
183 assert(id);
Michal Vasko50576712017-07-28 12:28:33 +0200184 assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len));
185 assert((name && nam_len) || (!name && !nam_len));
Michal Vasko50576712017-07-28 12:28:33 +0200186
Michal Vasko723e50c2015-10-20 15:20:29 +0200187 if (mod_name) {
188 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200189 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200190 }
191 if (name) {
192 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200193 *nam_len = 0;
194 }
195
Michal Vasko50576712017-07-28 12:28:33 +0200196 if (extended) {
197 /* try to parse only the extended expressions */
198 if (id[parsed] == '/') {
PavolVican195cf392018-02-23 13:24:45 +0100199 if (all_desc) {
200 *all_desc = 1;
201 }
202 all_desc_local = 1;
Michal Vasko50576712017-07-28 12:28:33 +0200203 } else {
PavolVican195cf392018-02-23 13:24:45 +0100204 if (all_desc) {
205 *all_desc = 0;
206 }
Michal Vasko50576712017-07-28 12:28:33 +0200207 }
208
209 /* is there a prefix? */
PavolVican195cf392018-02-23 13:24:45 +0100210 ret = parse_identifier(id + all_desc_local);
Michal Vasko50576712017-07-28 12:28:33 +0200211 if (ret > 0) {
PavolVican195cf392018-02-23 13:24:45 +0100212 if (id[all_desc_local + ret] != ':') {
Michal Vasko50576712017-07-28 12:28:33 +0200213 /* this is not a prefix, so not an extended id */
214 goto standard_id;
215 }
216
217 if (mod_name) {
PavolVican195cf392018-02-23 13:24:45 +0100218 *mod_name = id + all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200219 *mod_name_len = ret;
220 }
221
222 /* "/" and ":" */
PavolVican195cf392018-02-23 13:24:45 +0100223 ret += all_desc_local + 1;
Michal Vasko50576712017-07-28 12:28:33 +0200224 } else {
PavolVican195cf392018-02-23 13:24:45 +0100225 ret = all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200226 }
227
228 /* parse either "*" or "." */
PavolVicanb28bbff2018-02-21 00:44:02 +0100229 if (*(id + ret) == '*') {
Michal Vasko50576712017-07-28 12:28:33 +0200230 if (name) {
231 *name = id + ret;
232 *nam_len = 1;
233 }
234 ++ret;
235
236 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100237 } else if (*(id + ret) == '.') {
PavolVican195cf392018-02-23 13:24:45 +0100238 if (!all_desc_local) {
Michal Vasko50576712017-07-28 12:28:33 +0200239 /* /. is redundant expression, we do not accept it */
240 return -ret;
241 }
242
243 if (name) {
244 *name = id + ret;
245 *nam_len = 1;
246 }
247 ++ret;
248
249 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100250 } else if (*(id + ret) == '#') {
PavolVican195cf392018-02-23 13:24:45 +0100251 if (all_desc_local || !ret) {
PavolVicanb28bbff2018-02-21 00:44:02 +0100252 /* no prefix */
253 return 0;
254 }
255 parsed = ret + 1;
256 if ((ret = parse_identifier(id + parsed)) < 1) {
257 return -parsed + ret;
258 }
259 *name = id + parsed - 1;
260 *nam_len = ret + 1;
261 return parsed + ret;
Michal Vasko50576712017-07-28 12:28:33 +0200262 }
263 /* else a standard id, parse it all again */
264 }
265
266standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200267 if ((ret = parse_identifier(id)) < 1) {
268 return ret;
269 }
270
Michal Vasko723e50c2015-10-20 15:20:29 +0200271 if (mod_name) {
272 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200273 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200274 }
275
276 parsed += ret;
277 id += ret;
278
279 /* there is prefix */
280 if (id[0] == ':') {
281 ++parsed;
282 ++id;
283
284 /* there isn't */
285 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200286 if (name && mod_name) {
287 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200288 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200289 if (mod_name) {
290 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200291 }
292
Michal Vasko723e50c2015-10-20 15:20:29 +0200293 if (nam_len && mod_name_len) {
294 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200295 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200296 if (mod_name_len) {
297 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200298 }
299
300 return parsed;
301 }
302
303 /* identifier (node name) */
304 if ((ret = parse_identifier(id)) < 1) {
305 return -parsed+ret;
306 }
307
308 if (name) {
309 *name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200310 *nam_len = ret;
311 }
312
313 return parsed+ret;
314}
315
316/**
317 * @brief Parse a path-predicate (leafref).
318 *
319 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
320 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
321 *
Michal Vaskobb211122015-08-19 14:03:11 +0200322 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200323 * @param[out] prefix Points to the prefix, NULL if there is not any.
324 * @param[out] pref_len Length of the prefix, 0 if there is not any.
325 * @param[out] name Points to the node name.
326 * @param[out] nam_len Length of the node name.
327 * @param[out] path_key_expr Points to the path-key-expr.
328 * @param[out] pke_len Length of the path-key-expr.
329 * @param[out] has_predicate Flag to mark whether there is another predicate following.
330 *
331 * @return Number of characters successfully parsed,
332 * positive on success, negative on failure.
333 */
334static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200335parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
336 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200337{
338 const char *ptr;
339 int parsed = 0, ret;
340
341 assert(id);
342 if (prefix) {
343 *prefix = NULL;
344 }
345 if (pref_len) {
346 *pref_len = 0;
347 }
348 if (name) {
349 *name = NULL;
350 }
351 if (nam_len) {
352 *nam_len = 0;
353 }
354 if (path_key_expr) {
355 *path_key_expr = NULL;
356 }
357 if (pke_len) {
358 *pke_len = 0;
359 }
360 if (has_predicate) {
361 *has_predicate = 0;
362 }
363
364 if (id[0] != '[') {
365 return -parsed;
366 }
367
368 ++parsed;
369 ++id;
370
371 while (isspace(id[0])) {
372 ++parsed;
373 ++id;
374 }
375
Michal Vasko50576712017-07-28 12:28:33 +0200376 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200377 return -parsed+ret;
378 }
379
380 parsed += ret;
381 id += ret;
382
383 while (isspace(id[0])) {
384 ++parsed;
385 ++id;
386 }
387
388 if (id[0] != '=') {
389 return -parsed;
390 }
391
392 ++parsed;
393 ++id;
394
395 while (isspace(id[0])) {
396 ++parsed;
397 ++id;
398 }
399
400 if ((ptr = strchr(id, ']')) == NULL) {
401 return -parsed;
402 }
403
404 --ptr;
405 while (isspace(ptr[0])) {
406 --ptr;
407 }
408 ++ptr;
409
410 ret = ptr-id;
411 if (path_key_expr) {
412 *path_key_expr = id;
413 }
414 if (pke_len) {
415 *pke_len = ret;
416 }
417
418 parsed += ret;
419 id += ret;
420
421 while (isspace(id[0])) {
422 ++parsed;
423 ++id;
424 }
425
426 assert(id[0] == ']');
427
428 if (id[1] == '[') {
429 *has_predicate = 1;
430 }
431
432 return parsed+1;
433}
434
435/**
436 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
437 * the ".." and the first node-identifier, other calls parse a single
438 * node-identifier each.
439 *
440 * path-key-expr = current-function-invocation *WSP "/" *WSP
441 * rel-path-keyexpr
442 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
443 * *(node-identifier *WSP "/" *WSP)
444 * node-identifier
445 *
Michal Vaskobb211122015-08-19 14:03:11 +0200446 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200447 * @param[out] prefix Points to the prefix, NULL if there is not any.
448 * @param[out] pref_len Length of the prefix, 0 if there is not any.
449 * @param[out] name Points to the node name.
450 * @param[out] nam_len Length of the node name.
451 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
452 * must not be changed between consecutive calls.
453 * @return Number of characters successfully parsed,
454 * positive on success, negative on failure.
455 */
456static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200457parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
458 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200459{
460 int parsed = 0, ret, par_times = 0;
461
462 assert(id);
463 assert(parent_times);
464 if (prefix) {
465 *prefix = NULL;
466 }
467 if (pref_len) {
468 *pref_len = 0;
469 }
470 if (name) {
471 *name = NULL;
472 }
473 if (nam_len) {
474 *nam_len = 0;
475 }
476
477 if (!*parent_times) {
478 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
479 if (strncmp(id, "current()", 9)) {
480 return -parsed;
481 }
482
483 parsed += 9;
484 id += 9;
485
486 while (isspace(id[0])) {
487 ++parsed;
488 ++id;
489 }
490
491 if (id[0] != '/') {
492 return -parsed;
493 }
494
495 ++parsed;
496 ++id;
497
498 while (isspace(id[0])) {
499 ++parsed;
500 ++id;
501 }
502
503 /* rel-path-keyexpr */
504 if (strncmp(id, "..", 2)) {
505 return -parsed;
506 }
507 ++par_times;
508
509 parsed += 2;
510 id += 2;
511
512 while (isspace(id[0])) {
513 ++parsed;
514 ++id;
515 }
516 }
517
518 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
519 *
520 * first parent reference with whitespaces already parsed
521 */
522 if (id[0] != '/') {
523 return -parsed;
524 }
525
526 ++parsed;
527 ++id;
528
529 while (isspace(id[0])) {
530 ++parsed;
531 ++id;
532 }
533
534 while (!strncmp(id, "..", 2) && !*parent_times) {
535 ++par_times;
536
537 parsed += 2;
538 id += 2;
539
540 while (isspace(id[0])) {
541 ++parsed;
542 ++id;
543 }
544
545 if (id[0] != '/') {
546 return -parsed;
547 }
548
549 ++parsed;
550 ++id;
551
552 while (isspace(id[0])) {
553 ++parsed;
554 ++id;
555 }
556 }
557
558 if (!*parent_times) {
559 *parent_times = par_times;
560 }
561
562 /* all parent references must be parsed at this point */
Michal Vasko50576712017-07-28 12:28:33 +0200563 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
564 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200565 }
566
567 parsed += ret;
568 id += ret;
569
570 return parsed;
571}
572
573/**
574 * @brief Parse path-arg (leafref).
575 *
576 * path-arg = absolute-path / relative-path
577 * absolute-path = 1*("/" (node-identifier *path-predicate))
578 * relative-path = 1*(".." "/") descendant-path
579 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200580 * @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 +0200581 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200582 * @param[out] prefix Points to the prefix, NULL if there is not any.
583 * @param[out] pref_len Length of the prefix, 0 if there is not any.
584 * @param[out] name Points to the node name.
585 * @param[out] nam_len Length of the node name.
586 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
587 * must not be changed between consecutive calls. -1 if the
588 * path is relative.
589 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
590 *
591 * @return Number of characters successfully parsed,
592 * positive on success, negative on failure.
593 */
594static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200595parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200596 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200597{
598 int parsed = 0, ret, par_times = 0;
599
600 assert(id);
601 assert(parent_times);
602 if (prefix) {
603 *prefix = NULL;
604 }
605 if (pref_len) {
606 *pref_len = 0;
607 }
608 if (name) {
609 *name = NULL;
610 }
611 if (nam_len) {
612 *nam_len = 0;
613 }
614 if (has_predicate) {
615 *has_predicate = 0;
616 }
617
618 if (!*parent_times && !strncmp(id, "..", 2)) {
619 ++par_times;
620
621 parsed += 2;
622 id += 2;
623
624 while (!strncmp(id, "/..", 3)) {
625 ++par_times;
626
627 parsed += 3;
628 id += 3;
629 }
630 }
631
632 if (!*parent_times) {
633 if (par_times) {
634 *parent_times = par_times;
635 } else {
636 *parent_times = -1;
637 }
638 }
639
640 if (id[0] != '/') {
641 return -parsed;
642 }
643
644 /* skip '/' */
645 ++parsed;
646 ++id;
647
648 /* node-identifier ([prefix:]identifier) */
Michal Vasko50576712017-07-28 12:28:33 +0200649 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
650 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200651 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200652 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200653 /* actually we always need prefix even it is not specified */
654 *prefix = lys_main_module(mod)->name;
655 *pref_len = strlen(*prefix);
656 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200657
658 parsed += ret;
659 id += ret;
660
661 /* there is no predicate */
662 if ((id[0] == '/') || !id[0]) {
663 return parsed;
664 } else if (id[0] != '[') {
665 return -parsed;
666 }
667
668 if (has_predicate) {
669 *has_predicate = 1;
670 }
671
672 return parsed;
673}
674
675/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200676 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200677 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200678 *
679 * instance-identifier = 1*("/" (node-identifier *predicate))
680 *
Michal Vaskobb211122015-08-19 14:03:11 +0200681 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200682 * @param[out] model Points to the model name.
683 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200684 * @param[out] name Points to the node name.
685 * @param[out] nam_len Length of the node name.
686 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
687 *
688 * @return Number of characters successfully parsed,
689 * positive on success, negative on failure.
690 */
691static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200692parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
693 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200694{
695 int parsed = 0, ret;
696
Michal Vasko1b6ca962017-08-03 14:23:09 +0200697 assert(id && model && mod_len && name && nam_len);
698
Radek Krejci6dc53a22015-08-17 13:27:59 +0200699 if (has_predicate) {
700 *has_predicate = 0;
701 }
702
703 if (id[0] != '/') {
704 return -parsed;
705 }
706
707 ++parsed;
708 ++id;
709
Michal Vaskob2f40be2016-09-08 16:03:48 +0200710 if ((ret = parse_identifier(id)) < 1) {
711 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200712 }
713
Michal Vaskob2f40be2016-09-08 16:03:48 +0200714 *name = id;
715 *nam_len = ret;
716
717 parsed += ret;
718 id += ret;
719
Michal Vasko1b6ca962017-08-03 14:23:09 +0200720 if (id[0] == ':') {
721 /* we have prefix */
722 *model = *name;
723 *mod_len = *nam_len;
724
725 ++parsed;
726 ++id;
727
728 if ((ret = parse_identifier(id)) < 1) {
729 return ret;
730 }
731
732 *name = id;
733 *nam_len = ret;
734
735 parsed += ret;
736 id += ret;
737 }
738
Radek Krejci4967cb62016-09-14 16:40:28 +0200739 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200740 *has_predicate = 1;
741 }
742
743 return parsed;
744}
745
746/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200747 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200748 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200749 *
750 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
751 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
752 * ((DQUOTE string DQUOTE) /
753 * (SQUOTE string SQUOTE))
754 * pos = non-negative-integer-value
755 *
Michal Vaskobb211122015-08-19 14:03:11 +0200756 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200757 * @param[out] model Points to the model name.
758 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200759 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
760 * @param[out] nam_len Length of the node name.
761 * @param[out] value Value the node-identifier must have (string from the grammar),
762 * NULL if there is not any.
763 * @param[out] val_len Length of the value, 0 if there is not any.
764 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
765 *
766 * @return Number of characters successfully parsed,
767 * positive on success, negative on failure.
768 */
769static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200770parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
771 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200772{
773 const char *ptr;
774 int parsed = 0, ret;
775 char quote;
776
777 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200778 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200779 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200780 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200781 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200782 }
783 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200784 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200785 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200786 *nam_len = 0;
787 }
788 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200789 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200790 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200791 *val_len = 0;
792 }
793 if (has_predicate) {
794 *has_predicate = 0;
795 }
796
797 if (id[0] != '[') {
798 return -parsed;
799 }
800
801 ++parsed;
802 ++id;
803
804 while (isspace(id[0])) {
805 ++parsed;
806 ++id;
807 }
808
809 /* pos */
810 if (isdigit(id[0])) {
811 if (name) {
812 *name = id;
813 }
814
815 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200816 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200817 }
818
819 while (isdigit(id[0])) {
820 ++parsed;
821 ++id;
822 }
823
824 if (nam_len) {
825 *nam_len = id-(*name);
826 }
827
Michal Vaskof2f28a12016-09-09 12:43:06 +0200828 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200829 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200830 if (id[0] == '.') {
831 if (name) {
832 *name = id;
833 }
834 if (nam_len) {
835 *nam_len = 1;
836 }
837
838 ++parsed;
839 ++id;
840
841 } else {
Michal Vasko50576712017-07-28 12:28:33 +0200842 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200843 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200844 }
845
846 parsed += ret;
847 id += ret;
848 }
849
850 while (isspace(id[0])) {
851 ++parsed;
852 ++id;
853 }
854
855 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200856 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200857 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200858
Radek Krejci6dc53a22015-08-17 13:27:59 +0200859 ++parsed;
860 ++id;
861
Michal Vaskof2f28a12016-09-09 12:43:06 +0200862 while (isspace(id[0])) {
863 ++parsed;
864 ++id;
865 }
866
867 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
868 if ((id[0] == '\"') || (id[0] == '\'')) {
869 quote = id[0];
870
871 ++parsed;
872 ++id;
873
874 if ((ptr = strchr(id, quote)) == NULL) {
875 return -parsed;
876 }
Michal Vasko1b6ca962017-08-03 14:23:09 +0200877 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200878
879 if (value) {
880 *value = id;
881 }
882 if (val_len) {
883 *val_len = ret;
884 }
885
Michal Vasko1b6ca962017-08-03 14:23:09 +0200886 parsed += ret + 1;
887 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200888 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200889 return -parsed;
890 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200891 }
892
893 while (isspace(id[0])) {
894 ++parsed;
895 ++id;
896 }
897
898 if (id[0] != ']') {
899 return -parsed;
900 }
901
902 ++parsed;
903 ++id;
904
905 if ((id[0] == '[') && has_predicate) {
906 *has_predicate = 1;
907 }
908
909 return parsed;
910}
911
912/**
913 * @brief Parse schema-nodeid.
914 *
915 * schema-nodeid = absolute-schema-nodeid /
916 * descendant-schema-nodeid
917 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200918 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200919 * node-identifier
920 * absolute-schema-nodeid
921 *
Michal Vaskobb211122015-08-19 14:03:11 +0200922 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200923 * @param[out] mod_name Points to the module name, NULL if there is not any.
924 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200925 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200926 * @param[out] nam_len Length of the node name.
927 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
928 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100929 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
930 * based on the grammar, in those cases use NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200931 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200932 *
933 * @return Number of characters successfully parsed,
934 * positive on success, negative on failure.
935 */
Michal Vasko22448d32016-03-16 13:17:29 +0100936int
Michal Vasko723e50c2015-10-20 15:20:29 +0200937parse_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 +0200938 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200939{
940 int parsed = 0, ret;
941
942 assert(id);
943 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200944
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100945 if (has_predicate) {
946 *has_predicate = 0;
947 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200948
949 if (id[0] != '/') {
950 if (*is_relative != -1) {
951 return -parsed;
952 } else {
953 *is_relative = 1;
954 }
Michal Vasko48935352016-03-29 11:52:36 +0200955 if (!strncmp(id, "./", 2)) {
956 parsed += 2;
957 id += 2;
958 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200959 } else {
960 if (*is_relative == -1) {
961 *is_relative = 0;
962 }
963 ++parsed;
964 ++id;
965 }
966
Michal Vasko50576712017-07-28 12:28:33 +0200967 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
968 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200969 }
970
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100971 parsed += ret;
972 id += ret;
973
974 if ((id[0] == '[') && has_predicate) {
975 *has_predicate = 1;
976 }
977
978 return parsed;
979}
980
981/**
982 * @brief Parse schema predicate (special format internally used).
983 *
984 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200985 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100986 * key-with-value = identifier *WSP "=" *WSP
987 * ((DQUOTE string DQUOTE) /
988 * (SQUOTE string SQUOTE))
989 *
990 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200991 * @param[out] mod_name Points to the list key module name.
992 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100993 * @param[out] name Points to the list key name.
994 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100995 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100996 * @param[out] val_len Length of \p value.
997 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
998 */
Michal Vasko22448d32016-03-16 13:17:29 +0100999int
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001000parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
1001 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001002{
1003 const char *ptr;
1004 int parsed = 0, ret;
1005 char quote;
1006
1007 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001008 if (mod_name) {
1009 *mod_name = NULL;
1010 }
1011 if (mod_name_len) {
1012 *mod_name_len = 0;
1013 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001014 if (name) {
1015 *name = NULL;
1016 }
1017 if (nam_len) {
1018 *nam_len = 0;
1019 }
1020 if (value) {
1021 *value = NULL;
1022 }
1023 if (val_len) {
1024 *val_len = 0;
1025 }
1026 if (has_predicate) {
1027 *has_predicate = 0;
1028 }
1029
1030 if (id[0] != '[') {
1031 return -parsed;
1032 }
1033
1034 ++parsed;
1035 ++id;
1036
1037 while (isspace(id[0])) {
1038 ++parsed;
1039 ++id;
1040 }
1041
Michal Vasko22448d32016-03-16 13:17:29 +01001042 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001043 if (id[0] == '.') {
1044 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001045
1046 if (name) {
1047 *name = id;
1048 }
1049 if (nam_len) {
1050 *nam_len = ret;
1051 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001052 } else if (isdigit(id[0])) {
1053 if (id[0] == '0') {
1054 return -parsed;
1055 }
1056 ret = 1;
1057 while (isdigit(id[ret])) {
1058 ++ret;
1059 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001060
1061 if (name) {
1062 *name = id;
1063 }
1064 if (nam_len) {
1065 *nam_len = ret;
1066 }
Michal Vasko50576712017-07-28 12:28:33 +02001067 } 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 +01001068 return -parsed + ret;
1069 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001070
1071 parsed += ret;
1072 id += ret;
1073
1074 while (isspace(id[0])) {
1075 ++parsed;
1076 ++id;
1077 }
1078
1079 /* there is value as well */
1080 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001081 if (name && isdigit(**name)) {
1082 return -parsed;
1083 }
1084
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001085 ++parsed;
1086 ++id;
1087
1088 while (isspace(id[0])) {
1089 ++parsed;
1090 ++id;
1091 }
1092
1093 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1094 if ((id[0] == '\"') || (id[0] == '\'')) {
1095 quote = id[0];
1096
1097 ++parsed;
1098 ++id;
1099
1100 if ((ptr = strchr(id, quote)) == NULL) {
1101 return -parsed;
1102 }
1103 ret = ptr - id;
1104
1105 if (value) {
1106 *value = id;
1107 }
1108 if (val_len) {
1109 *val_len = ret;
1110 }
1111
1112 parsed += ret + 1;
1113 id += ret + 1;
1114 } else {
1115 return -parsed;
1116 }
1117
1118 while (isspace(id[0])) {
1119 ++parsed;
1120 ++id;
1121 }
1122 }
1123
1124 if (id[0] != ']') {
1125 return -parsed;
1126 }
1127
1128 ++parsed;
1129 ++id;
1130
1131 if ((id[0] == '[') && has_predicate) {
1132 *has_predicate = 1;
1133 }
1134
1135 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001136}
1137
1138/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001139 * @brief Resolve (find) a feature definition. Logs directly.
1140 *
1141 * @param[in] feat_name Feature name to resolve.
1142 * @param[in] len Length of \p feat_name.
1143 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001144 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1145 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001146 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001147 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001148 */
1149static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001150resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001151{
1152 char *str;
1153 const char *mod_name, *name;
1154 int mod_name_len, nam_len, i, j;
1155 const struct lys_module *module;
1156
Radek Krejci9ff0a922016-07-14 13:08:05 +02001157 assert(feature);
1158
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001159 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001160 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 +01001161 LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001162 return -1;
1163 }
1164
Michal Vasko921eb6b2017-10-13 10:01:39 +02001165 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001166 if (!module) {
1167 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01001168 LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001169 return -1;
1170 }
1171
Radek Krejci9ff0a922016-07-14 13:08:05 +02001172 if (module != node->module && module == lys_node_module(node)) {
1173 /* first, try to search directly in submodule where the feature was mentioned */
1174 for (j = 0; j < node->module->features_size; j++) {
1175 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1176 /* check status */
1177 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001178 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001179 return -1;
1180 }
1181 *feature = &node->module->features[j];
1182 return 0;
1183 }
1184 }
1185 }
1186
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001187 /* search in the identified module ... */
1188 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001189 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001190 /* check status */
1191 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001192 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001193 return -1;
1194 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001195 *feature = &module->features[j];
1196 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001197 }
1198 }
1199 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001200 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001201 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001202 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1203 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001204 /* check status */
1205 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1206 module->inc[i].submodule->features[j].flags,
1207 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001208 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001209 return -1;
1210 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001211 *feature = &module->inc[i].submodule->features[j];
1212 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001213 }
1214 }
1215 }
1216
1217 /* not found */
1218 str = strndup(feat_name, len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001219 LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001220 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001221 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001222}
1223
Radek Krejci9ff0a922016-07-14 13:08:05 +02001224/*
1225 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001226 * - 1 if enabled
1227 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001228 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001229static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001230resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001231{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001232 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001233
Radek Krejci9ff0a922016-07-14 13:08:05 +02001234 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001235 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001236 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001237 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001238 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001239
Radek Krejci69b8d922016-07-27 13:13:41 +02001240 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001241}
1242
1243static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001244resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001245{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001246 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001247 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001248
Radek Krejci9ff0a922016-07-14 13:08:05 +02001249 op = iff_getop(expr->expr, *index_e);
1250 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001251
Radek Krejci9ff0a922016-07-14 13:08:05 +02001252 switch (op) {
1253 case LYS_IFF_F:
1254 /* resolve feature */
1255 return resolve_feature_value(expr->features[(*index_f)++]);
1256 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001257 /* invert result */
1258 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001259 case LYS_IFF_AND:
1260 case LYS_IFF_OR:
1261 a = resolve_iffeature_recursive(expr, index_e, index_f);
1262 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001263 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001264 return a && b;
1265 } else { /* LYS_IFF_OR */
1266 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001267 }
1268 }
1269
Radek Krejciaf566332017-02-07 15:56:59 +01001270 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001271}
1272
1273int
1274resolve_iffeature(struct lys_iffeature *expr)
1275{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001276 int index_e = 0, index_f = 0;
1277
1278 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001279 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001280 }
Radek Krejciaf566332017-02-07 15:56:59 +01001281 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001282}
1283
1284struct iff_stack {
1285 int size;
1286 int index; /* first empty item */
1287 uint8_t *stack;
1288};
1289
1290static int
1291iff_stack_push(struct iff_stack *stack, uint8_t value)
1292{
1293 if (stack->index == stack->size) {
1294 stack->size += 4;
1295 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001296 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001297 }
1298
1299 stack->stack[stack->index++] = value;
1300 return EXIT_SUCCESS;
1301}
1302
1303static uint8_t
1304iff_stack_pop(struct iff_stack *stack)
1305{
1306 stack->index--;
1307 return stack->stack[stack->index];
1308}
1309
1310static void
1311iff_stack_clean(struct iff_stack *stack)
1312{
1313 stack->size = 0;
1314 free(stack->stack);
1315}
1316
1317static void
1318iff_setop(uint8_t *list, uint8_t op, int pos)
1319{
1320 uint8_t *item;
1321 uint8_t mask = 3;
1322
1323 assert(pos >= 0);
1324 assert(op <= 3); /* max 2 bits */
1325
1326 item = &list[pos / 4];
1327 mask = mask << 2 * (pos % 4);
1328 *item = (*item) & ~mask;
1329 *item = (*item) | (op << 2 * (pos % 4));
1330}
1331
1332uint8_t
1333iff_getop(uint8_t *list, int pos)
1334{
1335 uint8_t *item;
1336 uint8_t mask = 3, result;
1337
1338 assert(pos >= 0);
1339
1340 item = &list[pos / 4];
1341 result = (*item) & (mask << 2 * (pos % 4));
1342 return result >> 2 * (pos % 4);
1343}
1344
1345#define LYS_IFF_LP 0x04 /* ( */
1346#define LYS_IFF_RP 0x08 /* ) */
1347
Radek Krejcicbb473e2016-09-16 14:48:32 +02001348/* internal structure for passing data for UNRES_IFFEAT */
1349struct unres_iffeat_data {
1350 struct lys_node *node;
1351 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001352 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001353};
1354
Radek Krejci9ff0a922016-07-14 13:08:05 +02001355void
1356resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1357{
1358 unsigned int e = 0, f = 0, r = 0;
1359 uint8_t op;
1360
1361 assert(iffeat);
1362
1363 if (!iffeat->expr) {
1364 goto result;
1365 }
1366
1367 do {
1368 op = iff_getop(iffeat->expr, e++);
1369 switch (op) {
1370 case LYS_IFF_NOT:
1371 if (!r) {
1372 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001373 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001374 break;
1375 case LYS_IFF_AND:
1376 case LYS_IFF_OR:
1377 if (!r) {
1378 r += 2;
1379 } else {
1380 r += 1;
1381 }
1382 break;
1383 case LYS_IFF_F:
1384 f++;
1385 if (r) {
1386 r--;
1387 }
1388 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001389 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001390 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001391
Radek Krejci9ff0a922016-07-14 13:08:05 +02001392result:
1393 if (expr_size) {
1394 *expr_size = e;
1395 }
1396 if (feat_size) {
1397 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001398 }
1399}
1400
1401int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001402resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001403 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001404{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001405 const char *c = value;
1406 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001407 int i, j, last_not, checkversion = 0;
1408 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001409 uint8_t op;
1410 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001411 struct unres_iffeat_data *iff_data;
Michal Vasko53b7da02018-02-13 15:28:42 +01001412 struct ly_ctx *ctx = node->module->ctx;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001413
Radek Krejci9ff0a922016-07-14 13:08:05 +02001414 assert(c);
1415
1416 if (isspace(c[0])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001417 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001418 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001419 }
1420
Radek Krejci9ff0a922016-07-14 13:08:05 +02001421 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1422 for (i = j = last_not = 0; c[i]; i++) {
1423 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001424 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001425 j++;
1426 continue;
1427 } else if (c[i] == ')') {
1428 j--;
1429 continue;
1430 } else if (isspace(c[i])) {
1431 continue;
1432 }
1433
1434 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1435 if (c[i + r] == '\0') {
Michal Vasko53b7da02018-02-13 15:28:42 +01001436 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001437 return EXIT_FAILURE;
1438 } else if (!isspace(c[i + r])) {
1439 /* feature name starting with the not/and/or */
1440 last_not = 0;
1441 f_size++;
1442 } else if (c[i] == 'n') { /* not operation */
1443 if (last_not) {
1444 /* double not */
1445 expr_size = expr_size - 2;
1446 last_not = 0;
1447 } else {
1448 last_not = 1;
1449 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001450 } else { /* and, or */
1451 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001452 /* not a not operation */
1453 last_not = 0;
1454 }
1455 i += r;
1456 } else {
1457 f_size++;
1458 last_not = 0;
1459 }
1460 expr_size++;
1461
1462 while (!isspace(c[i])) {
1463 if (!c[i] || c[i] == ')') {
1464 i--;
1465 break;
1466 }
1467 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001468 }
1469 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001470 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001471 /* not matching count of ( and ) */
Michal Vasko53b7da02018-02-13 15:28:42 +01001472 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001473 return EXIT_FAILURE;
1474 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001475
Radek Krejci69b8d922016-07-27 13:13:41 +02001476 if (checkversion || expr_size > 1) {
1477 /* check that we have 1.1 module */
1478 if (node->module->version != 2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001479 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1480 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 +02001481 return EXIT_FAILURE;
1482 }
1483 }
1484
Radek Krejci9ff0a922016-07-14 13:08:05 +02001485 /* allocate the memory */
1486 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001487 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001488 stack.stack = malloc(expr_size * sizeof *stack.stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001489 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001490 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001491 f_size--; expr_size--; /* used as indexes from now */
1492
1493 for (i--; i >= 0; i--) {
1494 if (c[i] == ')') {
1495 /* push it on stack */
1496 iff_stack_push(&stack, LYS_IFF_RP);
1497 continue;
1498 } else if (c[i] == '(') {
1499 /* pop from the stack into result all operators until ) */
1500 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1501 iff_setop(iffeat_expr->expr, op, expr_size--);
1502 }
1503 continue;
1504 } else if (isspace(c[i])) {
1505 continue;
1506 }
1507
1508 /* end operator or operand -> find beginning and get what is it */
1509 j = i + 1;
1510 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1511 i--;
1512 }
1513 i++; /* get back by one step */
1514
1515 if (!strncmp(&c[i], "not ", 4)) {
1516 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1517 /* double not */
1518 iff_stack_pop(&stack);
1519 } else {
1520 /* not has the highest priority, so do not pop from the stack
1521 * as in case of AND and OR */
1522 iff_stack_push(&stack, LYS_IFF_NOT);
1523 }
1524 } else if (!strncmp(&c[i], "and ", 4)) {
1525 /* as for OR - pop from the stack all operators with the same or higher
1526 * priority and store them to the result, then push the AND to the stack */
1527 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1528 op = iff_stack_pop(&stack);
1529 iff_setop(iffeat_expr->expr, op, expr_size--);
1530 }
1531 iff_stack_push(&stack, LYS_IFF_AND);
1532 } else if (!strncmp(&c[i], "or ", 3)) {
1533 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1534 op = iff_stack_pop(&stack);
1535 iff_setop(iffeat_expr->expr, op, expr_size--);
1536 }
1537 iff_stack_push(&stack, LYS_IFF_OR);
1538 } else {
1539 /* feature name, length is j - i */
1540
1541 /* add it to the result */
1542 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1543
1544 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001545 * forward referenced, we have to keep the feature name in auxiliary
1546 * structure passed into unres */
1547 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001548 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001549 iff_data->node = node;
1550 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001551 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001552 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1553 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001554 f_size--;
1555
1556 if (r == -1) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01001557 lydict_remove(node->module->ctx, iff_data->fname);
Pavol Vican4d084512016-09-29 16:38:12 +02001558 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001559 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001560 }
1561 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001562 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001563 while (stack.index) {
1564 op = iff_stack_pop(&stack);
1565 iff_setop(iffeat_expr->expr, op, expr_size--);
1566 }
1567
1568 if (++expr_size || ++f_size) {
1569 /* not all expected operators and operands found */
Michal Vasko53b7da02018-02-13 15:28:42 +01001570 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001571 rc = EXIT_FAILURE;
1572 } else {
1573 rc = EXIT_SUCCESS;
1574 }
1575
1576error:
1577 /* cleanup */
1578 iff_stack_clean(&stack);
1579
1580 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001581}
1582
1583/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001584 * @brief Resolve (find) a data node based on a schema-nodeid.
1585 *
1586 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1587 * module).
1588 *
1589 */
1590struct lyd_node *
1591resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1592{
1593 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001594 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001595 const struct lys_node *schema = NULL;
1596
1597 assert(nodeid && start);
1598
1599 if (nodeid[0] == '/') {
1600 return NULL;
1601 }
1602
1603 str = p = strdup(nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01001604 LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001605
Michal Vasko3edeaf72016-02-11 13:17:43 +01001606 while (p) {
1607 token = p;
1608 p = strchr(p, '/');
1609 if (p) {
1610 *p = '\0';
1611 p++;
1612 }
1613
Radek Krejci5da4eb62016-04-08 14:45:51 +02001614 if (p) {
1615 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001616 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001617 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001618 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001619 result = NULL;
1620 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001621 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001622
Radek Krejci5da4eb62016-04-08 14:45:51 +02001623 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1624 continue;
1625 }
1626 } else {
1627 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001628 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001629 || !schema) {
1630 result = NULL;
1631 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001632 }
1633 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001634 LY_TREE_FOR(result ? result->child : start, iter) {
1635 if (iter->schema == schema) {
1636 /* move in data tree according to returned schema */
1637 result = iter;
1638 break;
1639 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001640 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001641 if (!iter) {
1642 /* instance not found */
1643 result = NULL;
1644 break;
1645 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001646 }
1647 free(str);
1648
1649 return result;
1650}
1651
Radek Krejci1a9c3612017-04-24 14:49:43 +02001652int
Michal Vasko50576712017-07-28 12:28:33 +02001653schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name,
1654 int mod_name_len, const char *name, int nam_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001655{
1656 const struct lys_module *prefix_mod;
1657
Michal Vaskocdb3f062018-02-01 09:55:06 +01001658 /* handle special names */
1659 if (name[0] == '*') {
1660 return 2;
1661 } else if (name[0] == '.') {
1662 return 3;
1663 }
1664
Michal Vasko50576712017-07-28 12:28:33 +02001665 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001666 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001667 return 1;
1668 }
1669
Radek Krejcibdf92362016-04-08 14:43:34 +02001670 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001671 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001672 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001673 if (!prefix_mod) {
1674 return -1;
1675 }
1676 } else {
1677 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001678 }
1679 if (prefix_mod != lys_node_module(sibling)) {
1680 return 1;
1681 }
1682
Michal Vasko50576712017-07-28 12:28:33 +02001683 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001684 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001685}
1686
Michal Vasko50576712017-07-28 12:28:33 +02001687/* keys do not have to be ordered and do not have to be all of them */
1688static int
1689resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node,
1690 const struct lys_module *cur_module, int *nodeid_end)
1691{
1692 int mod_len, nam_len, has_predicate, r, i;
1693 const char *model, *name;
1694 struct lys_node_list *list;
1695
1696 if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1697 return 1;
1698 }
1699
1700 list = (struct lys_node_list *)node;
1701 do {
1702 r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate);
1703 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001704 LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001705 return -1;
1706 }
1707 nodeid += r;
1708
1709 if (node->nodetype == LYS_LEAFLIST) {
1710 /* just check syntax */
1711 if (model || !name || (name[0] != '.') || has_predicate) {
1712 return 1;
1713 }
1714 break;
1715 } else {
1716 /* check the key */
1717 for (i = 0; i < list->keys_size; ++i) {
1718 if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) {
1719 continue;
1720 }
1721 if (model) {
1722 if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len)
1723 || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) {
1724 continue;
1725 }
1726 } else {
1727 if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) {
1728 continue;
1729 }
1730 }
1731
1732 /* match */
1733 break;
1734 }
1735
1736 if (i == list->keys_size) {
1737 return 1;
1738 }
1739 }
1740 } while (has_predicate);
1741
1742 if (!nodeid[0]) {
1743 *nodeid_end = 1;
1744 }
1745 return 0;
1746}
1747
Michal Vasko97234262018-02-01 09:53:01 +01001748/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001749 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001750int
Michal Vasko97234262018-02-01 09:53:01 +01001751resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001752 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001753{
PavolVicanb28bbff2018-02-21 00:44:02 +01001754 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vasko97234262018-02-01 09:53:01 +01001755 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001756 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001757 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
PavolVicanb28bbff2018-02-21 00:44:02 +01001758 int yang_data_name_len, backup_mod_name_len = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001759 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001760 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001761 char *str;
Michal Vasko53b7da02018-02-13 15:28:42 +01001762 struct ly_ctx *ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001763
Michal Vasko97234262018-02-01 09:53:01 +01001764 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001765 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001766
Michal Vasko50576712017-07-28 12:28:33 +02001767 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001768 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001769 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001770 ctx = cur_module->ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001771 id = nodeid;
1772
PavolVican195cf392018-02-23 13:24:45 +01001773 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1);
PavolVicanb28bbff2018-02-21 00:44:02 +01001774 if (r < 1) {
1775 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
1776 return -1;
1777 }
1778
1779 if (name[0] == '#') {
1780 if (is_relative) {
1781 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
1782 return -1;
1783 }
1784 yang_data_name = name + 1;
1785 yang_data_name_len = nam_len - 1;
1786 backup_mod_name = mod_name;
1787 backup_mod_name_len = mod_name_len;
1788 id += r;
1789 } else {
1790 is_relative = -1;
1791 }
1792
Michal Vasko50576712017-07-28 12:28:33 +02001793 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1794 (extended ? &all_desc : NULL), extended);
1795 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001796 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001797 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001798 }
1799 id += r;
1800
PavolVicanb28bbff2018-02-21 00:44:02 +01001801 if (backup_mod_name) {
1802 mod_name = backup_mod_name;
1803 mod_name_len = backup_mod_name_len;
1804 }
1805
Michal Vasko97234262018-02-01 09:53:01 +01001806 if (is_relative && !start_parent) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001807 LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths.");
Michal Vasko3edeaf72016-02-11 13:17:43 +01001808 return -1;
1809 }
1810
1811 /* descendant-schema-nodeid */
1812 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001813 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001814
Michal Vasko3edeaf72016-02-11 13:17:43 +01001815 /* absolute-schema-nodeid */
1816 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001817 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001818 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001819 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001820 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001821 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001822 return -1;
1823 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001824 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01001825 if (yang_data_name) {
1826 start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len);
1827 if (!start_parent) {
1828 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
1829 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
1830 free(str);
1831 return -1;
1832 }
1833 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001834 }
1835
1836 while (1) {
1837 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001838 last_aug = NULL;
1839
1840 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001841 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1842 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001843 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001844 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001845 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001846 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001847 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001848 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001849 return -1;
1850 }
1851 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001852 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001853 * because this module may be not implemented and it augments something in another module and
1854 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001855 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001856 }
1857
1858 /* if the module is implemented, all the augments will be connected */
Michal Vasko50576712017-07-28 12:28:33 +02001859 if (!aux_mod->implemented && !extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001860get_next_augment:
1861 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1862 }
1863 }
1864
1865 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vaskocb45f472018-02-12 10:47:42 +01001866 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02001867 r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len);
1868
1869 /* resolve predicate */
1870 if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) {
1871 r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end);
1872 if (r == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001873 continue;
Michal Vasko50576712017-07-28 12:28:33 +02001874 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001875 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001876 }
Michal Vasko50576712017-07-28 12:28:33 +02001877 } else if (!id[0]) {
1878 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001879 }
Michal Vasko50576712017-07-28 12:28:33 +02001880
1881 if (r == 0) {
1882 /* one matching result */
1883 if (nodeid_end) {
1884 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001885 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001886 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1887 } else {
1888 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1889 return -1;
1890 }
1891 start_parent = sibling;
1892 }
1893 break;
1894 } else if (r == 1) {
1895 continue;
1896 } else if (r == 2) {
1897 /* "*" */
1898 if (!*ret) {
1899 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001900 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001901 }
1902 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1903 if (all_desc) {
1904 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1905 if (elem != sibling) {
1906 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1907 }
1908
1909 LY_TREE_DFS_END(sibling, next, elem);
1910 }
1911 }
1912 } else if (r == 3) {
1913 /* "." */
1914 if (!*ret) {
1915 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001916 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001917 ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST);
1918 }
1919 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1920 if (all_desc) {
1921 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1922 if (elem != sibling) {
1923 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1924 }
1925
1926 LY_TREE_DFS_END(sibling, next, elem);
1927 }
1928 }
1929 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01001930 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02001931 return -1;
1932 }
1933 }
1934
1935 /* skip predicate */
1936 if (extended && has_predicate) {
1937 while (id[0] == '[') {
1938 id = strchr(id, ']');
1939 if (!id) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001940 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02001941 return -1;
1942 }
1943 ++id;
1944 }
1945 }
1946
1947 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
1948 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001949 }
1950
1951 /* no match */
1952 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02001953 if (last_aug) {
1954 /* it still could be in another augment */
1955 goto get_next_augment;
1956 }
Michal Vasko50576712017-07-28 12:28:33 +02001957 if (no_node_error) {
1958 str = strndup(nodeid, (name - nodeid) + nam_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001959 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001960 free(str);
1961 return -1;
1962 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01001963 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001964 return EXIT_SUCCESS;
1965 }
1966
Michal Vasko50576712017-07-28 12:28:33 +02001967 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1968 (extended ? &all_desc : NULL), extended);
1969 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001970 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001971 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001972 }
1973 id += r;
1974 }
1975
1976 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01001977 LOGINT(ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001978 return -1;
1979}
1980
Radek Krejcif3c71de2016-04-11 12:45:46 +02001981/* unique, refine,
1982 * >0 - unexpected char on position (ret - 1),
1983 * 0 - ok (but ret can still be NULL),
1984 * -1 - error,
1985 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001986int
1987resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02001988 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001989{
1990 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001991 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001992 int r, nam_len, mod_name_len, is_relative = -1;
1993 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001994 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001995
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001996 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01001997 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01001998
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001999 if (!start) {
2000 /* leaf not found */
2001 return 0;
2002 }
2003
Michal Vasko3edeaf72016-02-11 13:17:43 +01002004 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02002005 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002006
Michal Vasko50576712017-07-28 12:28:33 +02002007 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 +01002008 return ((id - nodeid) - r) + 1;
2009 }
2010 id += r;
2011
2012 if (!is_relative) {
2013 return -1;
2014 }
2015
Michal Vasko24476fa2017-03-08 12:33:48 +01002016 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02002017 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01002018 start_parent = lys_parent(start_parent);
2019 }
2020
Michal Vasko3edeaf72016-02-11 13:17:43 +01002021 while (1) {
2022 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002023 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vaskocb45f472018-02-12 10:47:42 +01002024 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002025 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2026 if (r == 0) {
2027 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002028 if (!(sibling->nodetype & ret_nodetype)) {
2029 /* wrong node type, too bad */
2030 continue;
2031 }
2032 *ret = sibling;
2033 return EXIT_SUCCESS;
2034 }
Michal Vasko50576712017-07-28 12:28:33 +02002035 start_parent = sibling;
2036 break;
2037 } else if (r == 1) {
2038 continue;
2039 } else {
2040 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002041 }
2042 }
2043
2044 /* no match */
2045 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002046 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002047 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02002048 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
2049 *ret = NULL;
2050 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002051 }
2052
Michal Vasko50576712017-07-28 12:28:33 +02002053 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 +01002054 return ((id - nodeid) - r) + 1;
2055 }
2056 id += r;
2057 }
2058
2059 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002060 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002061 return -1;
2062}
2063
2064/* choice default */
2065int
2066resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
2067{
2068 /* cannot actually be a path */
2069 if (strchr(nodeid, '/')) {
2070 return -1;
2071 }
2072
Michal Vaskodc300b02017-04-07 14:09:20 +02002073 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002074}
2075
2076/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2077static int
2078resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
2079{
2080 const struct lys_module *module;
2081 const char *mod_prefix, *name;
2082 int i, mod_prefix_len, nam_len;
2083
2084 /* parse the identifier, it must be parsed on one call */
Michal Vasko50576712017-07-28 12:28:33 +02002085 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 +01002086 return -i + 1;
2087 }
2088
Michal Vasko921eb6b2017-10-13 10:01:39 +02002089 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002090 if (!module) {
2091 return -1;
2092 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002093 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002094 start = module->data;
2095 }
2096
2097 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2098
2099 return EXIT_SUCCESS;
2100}
2101
2102int
2103resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
2104 const struct lys_node **ret)
2105{
2106 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002107 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002108 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002109 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002110
2111 assert(nodeid && module && ret);
2112 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
2113
2114 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01002115 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002116
Michal Vasko50576712017-07-28 12:28:33 +02002117 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 +01002118 return ((id - nodeid) - r) + 1;
2119 }
2120 id += r;
2121
2122 if (is_relative) {
2123 return -1;
2124 }
2125
Michal Vasko921eb6b2017-10-13 10:01:39 +02002126 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002127 if (!abs_start_mod) {
2128 return -1;
2129 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002130
2131 while (1) {
2132 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002133 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vaskocb45f472018-02-12 10:47:42 +01002134 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002135 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2136 if (r == 0) {
2137 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002138 if (!(sibling->nodetype & ret_nodetype)) {
2139 /* wrong node type, too bad */
2140 continue;
2141 }
2142 *ret = sibling;
2143 return EXIT_SUCCESS;
2144 }
Michal Vasko50576712017-07-28 12:28:33 +02002145 start_parent = sibling;
2146 break;
2147 } else if (r == 1) {
2148 continue;
2149 } else {
2150 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002151 }
2152 }
2153
2154 /* no match */
2155 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002156 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002157 return EXIT_SUCCESS;
2158 }
2159
Michal Vasko50576712017-07-28 12:28:33 +02002160 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 +01002161 return ((id - nodeid) - r) + 1;
2162 }
2163 id += r;
2164 }
2165
2166 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002167 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002168 return -1;
2169}
2170
Michal Vaskoe733d682016-03-14 09:08:27 +01002171static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002172resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002173{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002174 const char *mod_name, *name;
2175 int mod_name_len, nam_len, has_predicate, i;
2176 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002177
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002178 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 +02002179 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002180 LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002181 return -1;
2182 }
2183
2184 predicate += i;
2185 *parsed += i;
2186
Michal Vasko58c2aab2017-01-05 10:02:05 +01002187 if (!isdigit(name[0])) {
2188 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002189 key = (struct lys_node *)list->keys[i];
2190 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002191 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002192 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002193 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002194
Michal Vasko58c2aab2017-01-05 10:02:05 +01002195 if (i == list->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002196 LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko58c2aab2017-01-05 10:02:05 +01002197 return -1;
2198 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002199 }
2200
2201 /* more predicates? */
2202 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002203 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002204 }
2205
2206 return 0;
2207}
2208
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002209/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002210const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002211resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002212{
Michal Vasko53b7da02018-02-13 15:28:42 +01002213 char *str;
PavolVicanb28bbff2018-02-21 00:44:02 +01002214 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vaskob3744402017-08-03 14:23:58 +02002215 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002216 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
PavolVicanb28bbff2018-02-21 00:44:02 +01002217 int yang_data_name_len, backup_mod_name_len;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002218 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002219 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002220
Michal Vasko3547c532016-03-14 09:40:50 +01002221 assert(nodeid && (ctx || start));
2222 if (!ctx) {
2223 ctx = start->module->ctx;
2224 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002225
2226 id = nodeid;
2227
PavolVican195cf392018-02-23 13:24:45 +01002228 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 +01002229 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2230 return NULL;
2231 }
2232
2233 if (name[0] == '#') {
2234 if (is_relative) {
2235 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
2236 return NULL;
2237 }
2238 yang_data_name = name + 1;
2239 yang_data_name_len = nam_len - 1;
2240 backup_mod_name = mod_name;
2241 backup_mod_name_len = mod_name_len;
2242 id += r;
2243 } else {
2244 is_relative = -1;
2245 }
2246
Michal Vasko50576712017-07-28 12:28:33 +02002247 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 +01002248 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002249 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002250 }
2251 id += r;
2252
PavolVicanb28bbff2018-02-21 00:44:02 +01002253 if (backup_mod_name) {
2254 mod_name = backup_mod_name;
2255 mod_name_len = backup_mod_name_len;
2256 }
2257
Michal Vasko3edeaf72016-02-11 13:17:43 +01002258 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002259 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002260 start_parent = start;
2261 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2262 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002263 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002264 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002265 } else {
2266 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002267 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002268 LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
Michal Vasko10728b52016-04-07 14:26:29 +02002269 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002270 return NULL;
2271 }
2272
Michal Vasko53b7da02018-02-13 15:28:42 +01002273 str = strndup(mod_name, mod_name_len);
2274 module = ly_ctx_get_module(ctx, str, NULL, 1);
2275 free(str);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002276
Michal Vaskof68a49e2017-08-14 13:23:37 +02002277 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002278 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002279 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002280 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002281 return NULL;
2282 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002283 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01002284 if (yang_data_name) {
2285 start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len);
2286 if (!start_parent) {
2287 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
2288 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
2289 free(str);
2290 return NULL;
2291 }
2292 }
Michal Vasko3547c532016-03-14 09:40:50 +01002293
2294 /* now it's as if there was no module name */
2295 mod_name = NULL;
2296 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002297 }
2298
Michal Vaskof68a49e2017-08-14 13:23:37 +02002299 prev_mod = module;
2300
Michal Vasko3edeaf72016-02-11 13:17:43 +01002301 while (1) {
2302 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002303 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002304 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002305 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002306 /* output check */
2307 for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2308 if (parent) {
2309 if (output && (parent->nodetype == LYS_INPUT)) {
2310 continue;
2311 } else if (!output && (parent->nodetype == LYS_OUTPUT)) {
2312 continue;
2313 }
2314 }
2315
Michal Vasko3edeaf72016-02-11 13:17:43 +01002316 /* module check */
2317 if (mod_name) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002318 /* will also find an augment module */
Michal Vasko53b7da02018-02-13 15:28:42 +01002319 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002320
Michal Vasko3edeaf72016-02-11 13:17:43 +01002321 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002322 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002323 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002324 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002325 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002326 }
2327 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002328 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002329 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002330 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002331 continue;
2332 }
2333
Michal Vaskoe733d682016-03-14 09:08:27 +01002334 /* do we have some predicates on it? */
2335 if (has_predicate) {
2336 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002337 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002338 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002339 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002340 return NULL;
2341 }
2342 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002343 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002344 return NULL;
2345 }
2346 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002347 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002348 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002349 }
2350 id += r;
2351 }
2352
Michal Vasko3edeaf72016-02-11 13:17:43 +01002353 /* the result node? */
2354 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002355 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002356 }
2357
Michal Vaskodc300b02017-04-07 14:09:20 +02002358 /* move down the tree, if possible */
2359 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002360 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskodc300b02017-04-07 14:09:20 +02002361 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002362 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002363 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002364
2365 /* update prev mod */
2366 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002367 break;
2368 }
2369 }
2370
2371 /* no match */
2372 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002373 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002374 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002375 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002376 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002377 }
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 }
2385
2386 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002387 LOGINT(ctx);
Michal Vaskoe733d682016-03-14 09:08:27 +01002388 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002389}
2390
Michal Vasko22448d32016-03-16 13:17:29 +01002391static int
Michal Vasko58c2aab2017-01-05 10:02:05 +01002392resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node,
Michal Vasko50576712017-07-28 12:28:33 +02002393 int position, int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002394{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002395 const char *mod_name, *name, *value, *key_val;
2396 int mod_name_len, nam_len, val_len, has_predicate = 1, r;
Michal Vasko22448d32016-03-16 13:17:29 +01002397 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002398 struct lyd_node_leaf_list *key;
Michal Vasko53b7da02018-02-13 15:28:42 +01002399 struct ly_ctx *ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002400
Radek Krejci61a86c62016-03-24 11:06:44 +01002401 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002402 assert(node->schema->nodetype == LYS_LIST);
Michal Vasko53b7da02018-02-13 15:28:42 +01002403 ctx = node->schema->module->ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002404
Michal Vasko53adfc72017-01-06 10:39:10 +01002405 /* is the predicate a number? */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002406 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko53adfc72017-01-06 10:39:10 +01002407 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002408 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vasko53adfc72017-01-06 10:39:10 +01002409 return -1;
2410 }
2411
2412 if (isdigit(name[0])) {
2413 if (position == atoi(name)) {
2414 /* match */
2415 *parsed += r;
2416 return 0;
2417 } else {
2418 /* not a match */
2419 return 1;
2420 }
2421 }
2422
2423 if (!((struct lys_node_list *)node->schema)->keys_size) {
2424 /* no keys in schema - causes an error later */
2425 return 0;
2426 }
2427
Michal Vaskof29903d2016-04-18 13:13:10 +02002428 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002429 if (!key) {
2430 /* it is not a position, so we need a key for it to be a match */
2431 return 1;
2432 }
2433
2434 /* go through all the keys */
2435 i = 0;
2436 goto check_parsed_values;
2437
2438 for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
Michal Vasko22448d32016-03-16 13:17:29 +01002439 if (!has_predicate) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002440 LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002441 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002442 }
2443
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002444 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002445 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002446 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002447 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002448 }
2449
Michal Vasko53adfc72017-01-06 10:39:10 +01002450check_parsed_values:
Michal Vasko22448d32016-03-16 13:17:29 +01002451 predicate += r;
2452 *parsed += r;
2453
Michal Vaskof29903d2016-04-18 13:13:10 +02002454 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002455 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002456 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002457 }
2458
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002459 if (mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002460 /* specific module, check that the found key is from that module */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002461 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len)
2462 || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002463 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002464 return -1;
2465 }
Michal Vasko50576712017-07-28 12:28:33 +02002466
2467 /* but if the module is the same as the parent, it should have been omitted */
2468 if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002469 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko50576712017-07-28 12:28:33 +02002470 return -1;
2471 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002472 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002473 /* no module, so it must be the same as the list (parent) */
2474 if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002475 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002476 return -1;
2477 }
2478 }
2479
Michal Vasko9ba34de2016-12-07 12:21:19 +01002480 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002481 if ((key->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002482 && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name))
2483 && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002484 key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1;
2485 } else {
2486 key_val = key->value_str;
2487 }
2488
Michal Vasko22448d32016-03-16 13:17:29 +01002489 /* value does not match */
Michal Vasko9ba34de2016-12-07 12:21:19 +01002490 if (strncmp(key_val, value, val_len) || key_val[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002491 return 1;
2492 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002493
2494 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002495 }
2496
2497 if (has_predicate) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002498 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002499 return -1;
2500 }
2501
2502 return 0;
2503}
2504
Radek Krejci45826012016-08-24 15:07:57 +02002505/**
2506 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2507 *
2508 * @param[in] nodeid Node data path to find
2509 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2510 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2511 * @param[out] parsed Number of characters processed in \p id
2512 * @return The closes parent (or the node itself) from the path
2513 */
Michal Vasko22448d32016-03-16 13:17:29 +01002514struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002515resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2516 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002517{
Michal Vasko53b7da02018-02-13 15:28:42 +01002518 char *str;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002519 const char *id, *mod_name, *name, *pred_name, *data_val;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002520 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
PavolVican195cf392018-02-23 13:24:45 +01002521 int has_predicate, last_parsed = 0, llval_len, pred_name_len, last_has_pred;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002522 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002523 struct lyd_node_leaf_list *llist;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002524 const struct lys_module *prefix_mod, *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002525 struct ly_ctx *ctx;
2526
2527 assert(nodeid && start && parsed);
2528
2529 ctx = start->schema->module->ctx;
2530 id = nodeid;
2531
PavolVican195cf392018-02-23 13:24:45 +01002532 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 +01002533 *parsed = -1;
2534 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2535 return NULL;
2536 }
2537
2538 if (name[0] == '#') {
2539 if (is_relative) {
2540 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
2541 *parsed = -1;
2542 return NULL;
2543 }
PavolVicanb28bbff2018-02-21 00:44:02 +01002544 id += r;
2545 last_parsed = r;
2546 } else {
2547 is_relative = -1;
2548 }
2549
Michal Vasko50576712017-07-28 12:28:33 +02002550 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 +01002551 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002552 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002553 return NULL;
2554 }
2555 id += r;
2556 /* add it to parsed only after the data node was actually found */
PavolVicanb28bbff2018-02-21 00:44:02 +01002557 last_parsed += r;
Michal Vasko22448d32016-03-16 13:17:29 +01002558
2559 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002560 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002561 start = start->child;
2562 } else {
2563 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002564 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002565 }
2566
2567 while (1) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002568 list_instance_position = 0;
2569
Michal Vasko22448d32016-03-16 13:17:29 +01002570 LY_TREE_FOR(start, sibling) {
Michal Vasko945b96b2016-10-18 11:49:12 +02002571 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
Michal Vasko2411b942016-03-23 13:50:03 +01002572 if (lys_parent(sibling->schema)) {
2573 if (options & LYD_PATH_OPT_OUTPUT) {
2574 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002575 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002576 *parsed = -1;
2577 return NULL;
2578 }
2579 } else {
2580 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002581 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002582 *parsed = -1;
2583 return NULL;
2584 }
2585 }
2586 }
2587
Michal Vasko22448d32016-03-16 13:17:29 +01002588 /* name match */
2589 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2590
2591 /* module check */
2592 if (mod_name) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002593 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002594
Michal Vasko22448d32016-03-16 13:17:29 +01002595 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002596 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002597 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002598 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002599 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002600 return NULL;
2601 }
2602 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002603 prefix_mod = prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002604 }
Michal Vasko1adc7242016-11-16 11:05:28 +01002605 if (prefix_mod != lyd_node_module(sibling)) {
Michal Vasko22448d32016-03-16 13:17:29 +01002606 continue;
2607 }
2608
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002609 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002610 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002611 llist = (struct lyd_node_leaf_list *)sibling;
2612
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002613 last_has_pred = 0;
Michal Vaskof0a50972016-10-19 11:33:55 +02002614 if (has_predicate) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002615 if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value,
2616 &llval_len, &last_has_pred)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002617 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskof0a50972016-10-19 11:33:55 +02002618 *parsed = -1;
2619 return NULL;
2620 }
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002621 if ((pred_name[0] != '.') || (pred_name_len != 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002622 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1);
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002623 *parsed = -1;
2624 return NULL;
2625 }
Michal Vaskof0a50972016-10-19 11:33:55 +02002626 } else {
2627 r = 0;
2628 if (llist_value) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002629 llval_len = strlen(llist_value);
Michal Vaskof0a50972016-10-19 11:33:55 +02002630 }
2631 }
2632
Michal Vasko21b90ce2017-09-19 09:38:27 +02002633 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vaskod6d51292017-09-22 14:30:48 +02002634 if (llist_value && !strchr(llist_value, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002635 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2636 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002637 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2638 } else {
2639 data_val = llist->value_str;
2640 }
2641
2642 if ((!llist_value && data_val && data_val[0])
2643 || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002644 continue;
2645 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002646
Michal Vaskof0a50972016-10-19 11:33:55 +02002647 id += r;
2648 last_parsed += r;
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002649 has_predicate = last_has_pred;
Michal Vaskof0a50972016-10-19 11:33:55 +02002650
Radek Krejci45826012016-08-24 15:07:57 +02002651 } else if (sibling->schema->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002652 /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */
Michal Vasko22448d32016-03-16 13:17:29 +01002653 if (!has_predicate) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002654 /* none match */
2655 return last_match;
Michal Vasko22448d32016-03-16 13:17:29 +01002656 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01002657
2658 ++list_instance_position;
2659 r = 0;
Michal Vasko50576712017-07-28 12:28:33 +02002660 ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r);
Michal Vasko22448d32016-03-16 13:17:29 +01002661 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002662 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002663 return NULL;
2664 } else if (ret == 1) {
2665 /* this list instance does not match */
2666 continue;
2667 }
2668 id += r;
2669 last_parsed += r;
2670 }
2671
2672 *parsed += last_parsed;
2673
2674 /* the result node? */
2675 if (!id[0]) {
2676 return sibling;
2677 }
2678
2679 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002680 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002681 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002682 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002683 return NULL;
2684 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002685 last_match = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002686 prev_mod = lyd_node_module(sibling);
Michal Vasko22448d32016-03-16 13:17:29 +01002687 start = sibling->child;
Michal Vasko22448d32016-03-16 13:17:29 +01002688 break;
2689 }
2690 }
2691
2692 /* no match, return last match */
2693 if (!sibling) {
2694 return last_match;
2695 }
2696
Michal Vasko50576712017-07-28 12:28:33 +02002697 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 +01002698 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002699 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002700 return NULL;
2701 }
2702 id += r;
2703 last_parsed = r;
2704 }
2705
2706 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002707 LOGINT(ctx);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002708 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002709 return NULL;
2710}
2711
Michal Vasko3edeaf72016-02-11 13:17:43 +01002712/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002713 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002714 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002715 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002716 * @param[in] ctx Context for errors.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002717 * @param[in] str_restr Restriction as a string.
2718 * @param[in] type Type of the restriction.
2719 * @param[out] ret Final interval structure that starts with
2720 * the interval of the initial type, continues with intervals
2721 * of any superior types derived from the initial one, and
2722 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002723 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002724 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002725 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002726int
Michal Vasko53b7da02018-02-13 15:28:42 +01002727resolve_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 +02002728{
2729 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002730 int kind;
Michal Vaskof75b2772018-03-14 09:55:33 +01002731 int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax;
2732 uint64_t local_umin, local_umax = 0;
2733 uint8_t local_fdig = 0;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002734 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002735 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002736
2737 switch (type->base) {
2738 case LY_TYPE_BINARY:
2739 kind = 0;
2740 local_umin = 0;
2741 local_umax = 18446744073709551615UL;
2742
2743 if (!str_restr && type->info.binary.length) {
2744 str_restr = type->info.binary.length->expr;
2745 }
2746 break;
2747 case LY_TYPE_DEC64:
2748 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002749 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2750 local_fmax = __INT64_C(9223372036854775807);
2751 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002752
2753 if (!str_restr && type->info.dec64.range) {
2754 str_restr = type->info.dec64.range->expr;
2755 }
2756 break;
2757 case LY_TYPE_INT8:
2758 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002759 local_smin = __INT64_C(-128);
2760 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002761
2762 if (!str_restr && type->info.num.range) {
2763 str_restr = type->info.num.range->expr;
2764 }
2765 break;
2766 case LY_TYPE_INT16:
2767 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002768 local_smin = __INT64_C(-32768);
2769 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002770
2771 if (!str_restr && type->info.num.range) {
2772 str_restr = type->info.num.range->expr;
2773 }
2774 break;
2775 case LY_TYPE_INT32:
2776 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002777 local_smin = __INT64_C(-2147483648);
2778 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002779
2780 if (!str_restr && type->info.num.range) {
2781 str_restr = type->info.num.range->expr;
2782 }
2783 break;
2784 case LY_TYPE_INT64:
2785 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002786 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2787 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002788
2789 if (!str_restr && type->info.num.range) {
2790 str_restr = type->info.num.range->expr;
2791 }
2792 break;
2793 case LY_TYPE_UINT8:
2794 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002795 local_umin = __UINT64_C(0);
2796 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002797
2798 if (!str_restr && type->info.num.range) {
2799 str_restr = type->info.num.range->expr;
2800 }
2801 break;
2802 case LY_TYPE_UINT16:
2803 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002804 local_umin = __UINT64_C(0);
2805 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002806
2807 if (!str_restr && type->info.num.range) {
2808 str_restr = type->info.num.range->expr;
2809 }
2810 break;
2811 case LY_TYPE_UINT32:
2812 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002813 local_umin = __UINT64_C(0);
2814 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002815
2816 if (!str_restr && type->info.num.range) {
2817 str_restr = type->info.num.range->expr;
2818 }
2819 break;
2820 case LY_TYPE_UINT64:
2821 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002822 local_umin = __UINT64_C(0);
2823 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002824
2825 if (!str_restr && type->info.num.range) {
2826 str_restr = type->info.num.range->expr;
2827 }
2828 break;
2829 case LY_TYPE_STRING:
2830 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002831 local_umin = __UINT64_C(0);
2832 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002833
2834 if (!str_restr && type->info.str.length) {
2835 str_restr = type->info.str.length->expr;
2836 }
2837 break;
2838 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002839 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002840 }
2841
2842 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002843 if (type->der) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002844 if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002845 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002846 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002847 assert(!intv || (intv->kind == kind));
2848 }
2849
2850 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002851 /* we do not have any restriction, return superior ones */
2852 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002853 return EXIT_SUCCESS;
2854 }
2855
2856 /* adjust local min and max */
2857 if (intv) {
2858 tmp_intv = intv;
2859
2860 if (kind == 0) {
2861 local_umin = tmp_intv->value.uval.min;
2862 } else if (kind == 1) {
2863 local_smin = tmp_intv->value.sval.min;
2864 } else if (kind == 2) {
2865 local_fmin = tmp_intv->value.fval.min;
2866 }
2867
2868 while (tmp_intv->next) {
2869 tmp_intv = tmp_intv->next;
2870 }
2871
2872 if (kind == 0) {
2873 local_umax = tmp_intv->value.uval.max;
2874 } else if (kind == 1) {
2875 local_smax = tmp_intv->value.sval.max;
2876 } else if (kind == 2) {
2877 local_fmax = tmp_intv->value.fval.max;
2878 }
2879 }
2880
2881 /* finally parse our restriction */
2882 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002883 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002884 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002885 if (!tmp_local_intv) {
2886 assert(!local_intv);
2887 local_intv = malloc(sizeof *local_intv);
2888 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002889 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002890 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002891 tmp_local_intv = tmp_local_intv->next;
2892 }
Michal Vasko53b7da02018-02-13 15:28:42 +01002893 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002894
2895 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002896 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002897 tmp_local_intv->next = NULL;
2898
2899 /* min */
2900 ptr = seg_ptr;
2901 while (isspace(ptr[0])) {
2902 ++ptr;
2903 }
2904 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2905 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002906 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002907 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002908 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002909 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002910 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002911 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02002912 goto error;
2913 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002914 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002915 } else if (!strncmp(ptr, "min", 3)) {
2916 if (kind == 0) {
2917 tmp_local_intv->value.uval.min = local_umin;
2918 } else if (kind == 1) {
2919 tmp_local_intv->value.sval.min = local_smin;
2920 } else if (kind == 2) {
2921 tmp_local_intv->value.fval.min = local_fmin;
2922 }
2923
2924 ptr += 3;
2925 } else if (!strncmp(ptr, "max", 3)) {
2926 if (kind == 0) {
2927 tmp_local_intv->value.uval.min = local_umax;
2928 } else if (kind == 1) {
2929 tmp_local_intv->value.sval.min = local_smax;
2930 } else if (kind == 2) {
2931 tmp_local_intv->value.fval.min = local_fmax;
2932 }
2933
2934 ptr += 3;
2935 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002936 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002937 }
2938
2939 while (isspace(ptr[0])) {
2940 ptr++;
2941 }
2942
2943 /* no interval or interval */
2944 if ((ptr[0] == '|') || !ptr[0]) {
2945 if (kind == 0) {
2946 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2947 } else if (kind == 1) {
2948 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2949 } else if (kind == 2) {
2950 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2951 }
2952 } else if (!strncmp(ptr, "..", 2)) {
2953 /* skip ".." */
2954 ptr += 2;
2955 while (isspace(ptr[0])) {
2956 ++ptr;
2957 }
2958
2959 /* max */
2960 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2961 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002962 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002963 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002964 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002965 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002966 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002967 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02002968 goto error;
2969 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002970 }
2971 } else if (!strncmp(ptr, "max", 3)) {
2972 if (kind == 0) {
2973 tmp_local_intv->value.uval.max = local_umax;
2974 } else if (kind == 1) {
2975 tmp_local_intv->value.sval.max = local_smax;
2976 } else if (kind == 2) {
2977 tmp_local_intv->value.fval.max = local_fmax;
2978 }
2979 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002980 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002981 }
2982 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002983 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002984 }
2985
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002986 /* check min and max in correct order*/
2987 if (kind == 0) {
2988 /* current segment */
2989 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2990 goto error;
2991 }
2992 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2993 goto error;
2994 }
2995 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02002996 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002997 goto error;
2998 }
2999 } else if (kind == 1) {
3000 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
3001 goto error;
3002 }
3003 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
3004 goto error;
3005 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003006 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003007 goto error;
3008 }
3009 } else if (kind == 2) {
3010 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
3011 goto error;
3012 }
3013 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
3014 goto error;
3015 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003016 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003017 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003018 goto error;
3019 }
3020 }
3021
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003022 /* next segment (next OR) */
3023 seg_ptr = strchr(seg_ptr, '|');
3024 if (!seg_ptr) {
3025 break;
3026 }
3027 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003028 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003029 }
3030
3031 /* check local restrictions against superior ones */
3032 if (intv) {
3033 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003034 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003035
3036 while (tmp_local_intv && tmp_intv) {
3037 /* reuse local variables */
3038 if (kind == 0) {
3039 local_umin = tmp_local_intv->value.uval.min;
3040 local_umax = tmp_local_intv->value.uval.max;
3041
3042 /* it must be in this interval */
3043 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
3044 /* this interval is covered, next one */
3045 if (local_umax <= tmp_intv->value.uval.max) {
3046 tmp_local_intv = tmp_local_intv->next;
3047 continue;
3048 /* ascending order of restrictions -> fail */
3049 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003050 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003051 }
3052 }
3053 } else if (kind == 1) {
3054 local_smin = tmp_local_intv->value.sval.min;
3055 local_smax = tmp_local_intv->value.sval.max;
3056
3057 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
3058 if (local_smax <= tmp_intv->value.sval.max) {
3059 tmp_local_intv = tmp_local_intv->next;
3060 continue;
3061 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003062 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003063 }
3064 }
3065 } else if (kind == 2) {
3066 local_fmin = tmp_local_intv->value.fval.min;
3067 local_fmax = tmp_local_intv->value.fval.max;
3068
Michal Vasko4d1f0482016-09-19 14:35:06 +02003069 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02003070 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003071 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003072 tmp_local_intv = tmp_local_intv->next;
3073 continue;
3074 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003075 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003076 }
3077 }
3078 }
3079
3080 tmp_intv = tmp_intv->next;
3081 }
3082
3083 /* some interval left uncovered -> fail */
3084 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003085 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003086 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003087 }
3088
Michal Vaskoaeb51802016-04-11 10:58:47 +02003089 /* append the local intervals to all the intervals of the superior types, return it all */
3090 if (intv) {
3091 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
3092 tmp_intv->next = local_intv;
3093 } else {
3094 intv = local_intv;
3095 }
3096 *ret = intv;
3097
3098 return EXIT_SUCCESS;
3099
3100error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003101 while (intv) {
3102 tmp_intv = intv->next;
3103 free(intv);
3104 intv = tmp_intv;
3105 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003106 while (local_intv) {
3107 tmp_local_intv = local_intv->next;
3108 free(local_intv);
3109 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003110 }
3111
Michal Vaskoaeb51802016-04-11 10:58:47 +02003112 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003113}
3114
Michal Vasko730dfdf2015-08-11 14:48:05 +02003115/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003116 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3117 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003118 *
3119 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003120 * @param[in] mod_name Typedef name module name.
3121 * @param[in] module Main module.
3122 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003123 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003124 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003125 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003126 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003127int
Michal Vasko1e62a092015-12-01 12:27:20 +01003128resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3129 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003130{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003131 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003132 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003133 int tpdf_size;
3134
Michal Vasko1dca6882015-10-22 14:29:42 +02003135 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003136 /* no prefix, try built-in types */
3137 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003138 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003139 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003140 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003141 }
3142 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003143 }
3144 }
3145 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003146 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003147 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003148 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003149 }
3150 }
3151
Michal Vasko1dca6882015-10-22 14:29:42 +02003152 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003153 /* search in local typedefs */
3154 while (parent) {
3155 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003156 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003157 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3158 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003159 break;
3160
Radek Krejci76512572015-08-04 09:47:08 +02003161 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003162 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3163 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003164 break;
3165
Radek Krejci76512572015-08-04 09:47:08 +02003166 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003167 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3168 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003169 break;
3170
Radek Krejci76512572015-08-04 09:47:08 +02003171 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003172 case LYS_ACTION:
3173 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3174 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003175 break;
3176
Radek Krejci76512572015-08-04 09:47:08 +02003177 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003178 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3179 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003180 break;
3181
Radek Krejci76512572015-08-04 09:47:08 +02003182 case LYS_INPUT:
3183 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003184 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3185 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003186 break;
3187
3188 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003189 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003190 continue;
3191 }
3192
3193 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003194 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003195 match = &tpdf[i];
3196 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003197 }
3198 }
3199
Michal Vaskodcf98e62016-05-05 17:53:53 +02003200 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003201 }
Radek Krejcic071c542016-01-27 14:57:51 +01003202 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003203 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003204 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003205 if (!module) {
3206 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003207 }
3208 }
3209
3210 /* search in top level typedefs */
3211 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003212 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003213 match = &module->tpdf[i];
3214 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003215 }
3216 }
3217
3218 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003219 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003220 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003221 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 +02003222 match = &module->inc[i].submodule->tpdf[j];
3223 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003224 }
3225 }
3226 }
3227
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003228 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003229
3230check_leafref:
3231 if (ret) {
3232 *ret = match;
3233 }
3234 if (match->type.base == LY_TYPE_LEAFREF) {
3235 while (!match->type.info.lref.path) {
3236 match = match->type.der;
3237 assert(match);
3238 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003239 }
3240 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003241}
3242
Michal Vasko1dca6882015-10-22 14:29:42 +02003243/**
3244 * @brief Check the default \p value of the \p type. Logs directly.
3245 *
3246 * @param[in] type Type definition to use.
3247 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003248 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003249 *
3250 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3251 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003252static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003253check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003254{
Radek Krejcibad2f172016-08-02 11:04:15 +02003255 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003256 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003257 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003258 char *s;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003259 int ret = EXIT_SUCCESS, r;
Michal Vasko53b7da02018-02-13 15:28:42 +01003260 struct ly_ctx *ctx = module->ctx;
Michal Vasko1dca6882015-10-22 14:29:42 +02003261
Radek Krejci51673202016-11-01 17:00:32 +01003262 assert(value);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003263 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003264
Radek Krejcic13db382016-08-16 10:52:42 +02003265 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003266 /* the type was not resolved yet, nothing to do for now */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003267 ret = EXIT_FAILURE;
3268 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003269 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003270 /* do not check defaults in not implemented module's data */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003271 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003272 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003273 /* identityrefs are checked when instantiated in data instead of typedef,
3274 * but in typedef the value has to be modified to include the prefix */
3275 if (*value) {
3276 if (strchr(*value, ':')) {
3277 dflt = transform_schema2json(module, *value);
3278 } else {
3279 /* default prefix of the module where the typedef is defined */
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003280 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
3281 LOGMEM(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003282 ret = -1;
3283 goto cleanup;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003284 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003285 dflt = lydict_insert_zc(ctx, s);
Radek Krejci9e6af732017-04-27 14:40:25 +02003286 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003287 lydict_remove(ctx, *value);
Radek Krejci9e6af732017-04-27 14:40:25 +02003288 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003289 dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003290 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003291 goto cleanup;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003292 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3293 /* leafref in typedef cannot be checked */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003294 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003295 }
3296
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003297 dflt = lydict_insert(ctx, *value, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003298 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003299 /* we do not have a new default value, so is there any to check even, in some base type? */
3300 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3301 if (base_tpdf->dflt) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003302 dflt = lydict_insert(ctx, base_tpdf->dflt, 0);
Michal Vasko478c4652016-07-21 12:55:01 +02003303 break;
3304 }
3305 }
3306
Radek Krejci51673202016-11-01 17:00:32 +01003307 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003308 /* no default value, nothing to check, all is well */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003309 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003310 }
3311
3312 /* 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)? */
3313 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003314 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003315 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003316 goto cleanup;
Radek Krejci9e6af732017-04-27 14:40:25 +02003317 } else {
3318 /* check the default value from typedef, but use also the typedef's module
3319 * due to possible searching in imported modules which is expected in
3320 * typedef's module instead of module where the typedef is used */
3321 module = base_tpdf->module;
3322 }
3323 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003324 case LY_TYPE_INST:
3325 case LY_TYPE_LEAFREF:
3326 case LY_TYPE_BOOL:
3327 case LY_TYPE_EMPTY:
3328 /* 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 +01003329 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003330 case LY_TYPE_BITS:
3331 /* the default value must match the restricted list of values, if the type was restricted */
3332 if (type->info.bits.count) {
3333 break;
3334 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003335 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003336 case LY_TYPE_ENUM:
3337 /* the default value must match the restricted list of values, if the type was restricted */
3338 if (type->info.enums.count) {
3339 break;
3340 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003341 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003342 case LY_TYPE_DEC64:
3343 if (type->info.dec64.range) {
3344 break;
3345 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003346 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003347 case LY_TYPE_BINARY:
3348 if (type->info.binary.length) {
3349 break;
3350 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003351 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003352 case LY_TYPE_INT8:
3353 case LY_TYPE_INT16:
3354 case LY_TYPE_INT32:
3355 case LY_TYPE_INT64:
3356 case LY_TYPE_UINT8:
3357 case LY_TYPE_UINT16:
3358 case LY_TYPE_UINT32:
3359 case LY_TYPE_UINT64:
3360 if (type->info.num.range) {
3361 break;
3362 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003363 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003364 case LY_TYPE_STRING:
3365 if (type->info.str.length || type->info.str.patterns) {
3366 break;
3367 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003368 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003369 case LY_TYPE_UNION:
3370 /* way too much trouble learning whether we need to check the default again, so just do it */
3371 break;
3372 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01003373 LOGINT(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003374 ret = -1;
3375 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003376 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003377 } else if (type->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003378 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3379 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003380 ret = -1;
3381 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003382 }
3383
Michal Vasko1dca6882015-10-22 14:29:42 +02003384 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003385 memset(&node, 0, sizeof node);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003386 node.value_str = lydict_insert(ctx, dflt, 0);
Michal Vasko1dca6882015-10-22 14:29:42 +02003387 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003388
3389 if (tpdf) {
3390 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003391 if (!node.schema) {
3392 LOGMEM(ctx);
3393 ret = -1;
3394 goto cleanup;
3395 }
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003396 r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003397 if (r == -1) {
3398 LOGMEM(ctx);
3399 ret = -1;
3400 goto cleanup;
3401 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003402 node.schema->module = module;
3403 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3404 } else {
3405 node.schema = (struct lys_node *)type->parent;
3406 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003407
Radek Krejci37b756f2016-01-18 10:15:03 +01003408 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003409 if (!type->info.lref.target) {
3410 ret = EXIT_FAILURE;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003411 goto cleanup;
Michal Vasko1dca6882015-10-22 14:29:42 +02003412 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003413 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003414 if (!ret) {
3415 /* adopt possibly changed default value to its canonical form */
3416 if (*value) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003417 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003418 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003419 dflt = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003420 }
3421 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003422 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01003423 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003424 /* possible forward reference */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003425 ret = EXIT_FAILURE;
Radek Krejcibad2f172016-08-02 11:04:15 +02003426 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003427 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003428 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3429 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3430 /* we have refined bits/enums */
Michal Vasko53b7da02018-02-13 15:28:42 +01003431 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
Radek Krejcibad2f172016-08-02 11:04:15 +02003432 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003433 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003434 }
3435 }
Radek Krejci51673202016-11-01 17:00:32 +01003436 } else {
3437 /* success - adopt canonical form from the node into the default value */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003438 if (!ly_strequal(dflt, node.value_str, 1)) {
Radek Krejci51673202016-11-01 17:00:32 +01003439 /* this can happen only if we have non-inherited default value,
3440 * inherited default values are already in canonical form */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003441 assert(ly_strequal(dflt, *value, 1));
3442
3443 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003444 *value = node.value_str;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003445 node.value_str = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003446 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003447 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003448 }
3449
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003450cleanup:
Michal Vasko70bf8e52018-03-26 11:32:33 +02003451 lyd_free_value(node.value, node.value_type, node.value_flags, type);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003452 lydict_remove(ctx, node.value_str);
3453 if (tpdf && node.schema) {
Michal Vasko31a2d322018-01-12 13:36:12 +01003454 free((char *)node.schema->name);
3455 free(node.schema);
3456 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003457 lydict_remove(ctx, dflt);
Michal Vasko1dca6882015-10-22 14:29:42 +02003458
3459 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003460}
3461
Michal Vasko730dfdf2015-08-11 14:48:05 +02003462/**
3463 * @brief Check a key for mandatory attributes. Logs directly.
3464 *
3465 * @param[in] key The key to check.
3466 * @param[in] flags What flags to check.
3467 * @param[in] list The list of all the keys.
3468 * @param[in] index Index of the key in the key list.
3469 * @param[in] name The name of the keys.
3470 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003471 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003472 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003473 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003474static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003475check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003476{
Radek Krejciadb57612016-02-16 13:34:34 +01003477 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003478 char *dup = NULL;
3479 int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01003480 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003481
3482 /* existence */
3483 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003484 if (name[len] != '\0') {
3485 dup = strdup(name);
Michal Vasko53b7da02018-02-13 15:28:42 +01003486 LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003487 dup[len] = '\0';
3488 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003489 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003490 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003491 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003492 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003493 }
3494
3495 /* uniqueness */
3496 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003497 if (key == list->keys[j]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003498 LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003499 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003500 }
3501 }
3502
3503 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003504 if (key->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003505 LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003506 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003507 }
3508
3509 /* type of the leaf is not built-in empty */
Radek Krejcic3738db2016-09-15 15:51:21 +02003510 if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003511 LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003512 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003513 }
3514
3515 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003516 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003517 LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003518 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003519 }
3520
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003521 /* key is not placed from augment */
3522 if (key->parent->nodetype == LYS_AUGMENT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003523 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3524 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003525 return -1;
3526 }
3527
Radek Krejci3f21ada2016-08-01 13:34:31 +02003528 /* key is not when/if-feature -conditional */
3529 j = 0;
3530 if (key->when || (key->iffeature_size && (j = 1))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003531 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3532 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003533 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003534 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003535 }
3536
Michal Vasko0b85aa82016-03-07 14:37:43 +01003537 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003538}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003539
3540/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003541 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003542 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003543 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003544 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003545 *
3546 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3547 */
3548int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003549resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003550{
Radek Krejci581ce772015-11-10 17:22:40 +01003551 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003552 const struct lys_node *leaf = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01003553 struct ly_ctx *ctx = parent->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003554
Michal Vaskodc300b02017-04-07 14:09:20 +02003555 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003556 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003557 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003558 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003559 if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003560 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 +02003561 } else if (rc == -2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003562 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003563 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003564 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003565 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003566 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3567 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003568 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003569 }
Radek Krejci581ce772015-11-10 17:22:40 +01003570 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003571 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003572 if (leaf->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003573 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3574 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003575 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003576 }
3577
Radek Krejcicf509982015-12-15 09:22:44 +01003578 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003579 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3580 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003581 return -1;
3582 }
3583
Radek Krejcid09d1a52016-08-11 14:05:45 +02003584 /* check that all unique's targets are of the same config type */
3585 if (*trg_type) {
3586 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003587 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3588 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003589 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3590 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3591 return -1;
3592 }
3593 } else {
3594 /* first unique */
3595 if (leaf->flags & LYS_CONFIG_W) {
3596 *trg_type = 1;
3597 } else {
3598 *trg_type = 2;
3599 }
3600 }
3601
Radek Krejcica7efb72016-01-18 13:06:01 +01003602 /* set leaf's unique flag */
3603 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3604
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003605 return EXIT_SUCCESS;
3606
3607error:
3608
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003609 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003610}
3611
Radek Krejci0c0086a2016-03-24 15:20:28 +01003612void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003613unres_data_del(struct unres_data *unres, uint32_t i)
3614{
3615 /* there are items after the one deleted */
3616 if (i+1 < unres->count) {
3617 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003618 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003619
3620 /* deleting the last item */
3621 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003622 free(unres->node);
3623 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003624 }
3625
3626 /* if there are no items after and it is not the last one, just move the counter */
3627 --unres->count;
3628}
3629
Michal Vasko0491ab32015-08-19 14:28:29 +02003630/**
3631 * @brief Resolve (find) a data node from a specific module. Does not log.
3632 *
3633 * @param[in] mod Module to search in.
3634 * @param[in] name Name of the data node.
3635 * @param[in] nam_len Length of the name.
3636 * @param[in] start Data node to start the search from.
3637 * @param[in,out] parents Resolved nodes. If there are some parents,
3638 * they are replaced (!!) with the resolvents.
3639 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003640 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003641 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003642static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003643resolve_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 +02003644{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003645 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003646 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003647 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003648
Michal Vasko23b61ec2015-08-19 11:19:50 +02003649 if (!parents->count) {
3650 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003651 parents->node = malloc(sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003652 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003653 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003654 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003655 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003656 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003657 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003658 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003659 continue;
3660 }
3661 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003662 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003663 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003664 && node->schema->name[nam_len] == '\0') {
3665 /* matching target */
3666 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003667 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003668 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003669 flag = 1;
3670 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003671 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003672 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003673 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003674 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003675 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003676 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003677 }
3678 }
3679 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003680
3681 if (!flag) {
3682 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003683 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003684 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003685 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003686 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003687 }
3688
Michal Vasko0491ab32015-08-19 14:28:29 +02003689 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003690}
3691
Michal Vaskoe27516a2016-10-10 17:55:31 +00003692static int
Michal Vasko1c007172017-03-10 10:20:44 +01003693resolve_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 +00003694{
3695 int dep1, dep2;
3696 const struct lys_node *node;
3697
3698 if (lys_parent(op_node)) {
3699 /* inner operation (notif/action) */
3700 if (abs_path) {
3701 return 1;
3702 } else {
3703 /* compare depth of both nodes */
3704 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3705 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3706 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3707 return 1;
3708 }
3709 }
3710 } else {
3711 /* top-level operation (notif/rpc) */
3712 if (op_node != first_node) {
3713 return 1;
3714 }
3715 }
3716
3717 return 0;
3718}
3719
Michal Vasko730dfdf2015-08-11 14:48:05 +02003720/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003721 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003722 *
Michal Vaskobb211122015-08-19 14:03:11 +02003723 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003724 * @param[in] context_node Predicate context node (where the predicate is placed).
3725 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003726 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003727 *
Michal Vasko184521f2015-09-24 13:14:26 +02003728 * @return 0 on forward reference, otherwise the number
3729 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003730 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003731 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003732static int
Michal Vasko1c007172017-03-10 10:20:44 +01003733resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3734 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003735{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003736 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003737 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003738 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003739 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3740 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003741 struct ly_ctx *ctx = context_node->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003742
3743 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003744 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003745 &pke_len, &has_predicate)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003746 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003747 return -parsed+i;
3748 }
3749 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003750 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003751
Michal Vasko58090902015-08-13 14:04:15 +02003752 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003753 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003754 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003755 } else {
3756 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003757 }
Michal Vaskobb520442017-05-23 10:55:18 +02003758 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003759 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003760 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003761 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003762 }
3763
3764 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003765 dest_parent_times = 0;
3766 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003767 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3768 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003769 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003770 return -parsed;
3771 }
3772 pke_parsed += i;
3773
Radek Krejciadb57612016-02-16 13:34:34 +01003774 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003775 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3776 && !((struct lys_node_augment *)dst_node->parent)->target) {
3777 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003778 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003779 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3780 return 0;
3781 }
3782
Michal Vaskofbaead72016-10-07 10:54:48 +02003783 /* path is supposed to be evaluated in data tree, so we have to skip
3784 * all schema nodes that cannot be instantiated in data tree */
3785 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003786 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003787 dst_node = lys_parent(dst_node));
3788
Michal Vasko1f76a282015-08-04 16:16:53 +02003789 if (!dst_node) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003790 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003791 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003792 }
3793 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003794 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003795 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003796 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003797 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003798 } else {
3799 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003800 }
Michal Vaskobb520442017-05-23 10:55:18 +02003801 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 +02003802 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003803 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003804 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003805 }
3806
Michal Vaskoe27516a2016-10-10 17:55:31 +00003807 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003808 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003809 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003810 }
3811 first_iter = 0;
3812 }
3813
Michal Vasko1f76a282015-08-04 16:16:53 +02003814 if (pke_len == pke_parsed) {
3815 break;
3816 }
3817
Michal Vaskobb520442017-05-23 10:55:18 +02003818 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 +02003819 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003820 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02003821 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003822 return -parsed;
3823 }
3824 pke_parsed += i;
3825 }
3826
3827 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003828 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003829 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed);
Michal Vasko53b7da02018-02-13 15:28:42 +01003830 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003831 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003832 return -parsed;
3833 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003834 } while (has_predicate);
3835
3836 return parsed;
3837}
3838
Michal Vasko730dfdf2015-08-11 14:48:05 +02003839/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003840 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003841 *
Michal Vaskobb211122015-08-19 14:03:11 +02003842 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003843 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003844 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003845 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003846 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003847 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003848static int
Michal Vasko1c007172017-03-10 10:20:44 +01003849resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003850{
Michal Vaskocb45f472018-02-12 10:47:42 +01003851 const struct lys_node *node, *op_node = NULL, *tmp_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02003852 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003853 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02003854 const char *id, *prefix, *name;
3855 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskocb45f472018-02-12 10:47:42 +01003856 int i, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003857 struct ly_ctx *ctx = parent->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003858
Michal Vasko184521f2015-09-24 13:14:26 +02003859 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003860 parent_times = 0;
3861 id = path;
3862
Michal Vasko1c007172017-03-10 10:20:44 +01003863 /* find operation schema we are in */
3864 for (op_node = lys_parent(parent);
3865 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
3866 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02003867
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003868 cur_module = lys_node_module(parent);
Michal Vasko1f76a282015-08-04 16:16:53 +02003869 do {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003870 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003871 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003872 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003873 }
3874 id += i;
3875
Michal Vaskobb520442017-05-23 10:55:18 +02003876 /* get the current module */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003877 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003878 if (!tmp_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003879 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vaskobb520442017-05-23 10:55:18 +02003880 return EXIT_FAILURE;
3881 }
3882 last_aug = NULL;
3883
Michal Vasko184521f2015-09-24 13:14:26 +02003884 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003885 if (parent_times == -1) {
Michal Vaskobb520442017-05-23 10:55:18 +02003886 /* use module data */
3887 node = NULL;
Radek Krejci990af1f2016-11-09 13:53:36 +01003888
Michal Vasko1f76a282015-08-04 16:16:53 +02003889 } else if (parent_times > 0) {
Michal Vaskobb520442017-05-23 10:55:18 +02003890 /* we are looking for the right parent */
3891 for (i = 0, node = parent; i < parent_times; i++) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003892 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
3893 && !((struct lys_node_augment *)node->parent)->target) {
3894 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003895 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003896 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path);
3897 return EXIT_FAILURE;
3898 }
3899
Radek Krejci3a5501d2016-07-18 22:03:34 +02003900 /* path is supposed to be evaluated in data tree, so we have to skip
3901 * all schema nodes that cannot be instantiated in data tree */
3902 for (node = lys_parent(node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003903 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Radek Krejci3a5501d2016-07-18 22:03:34 +02003904 node = lys_parent(node));
3905
Michal Vasko1f76a282015-08-04 16:16:53 +02003906 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003907 if (i == parent_times - 1) {
3908 /* top-level */
3909 break;
3910 }
3911
3912 /* higher than top-level */
Michal Vasko53b7da02018-02-13 15:28:42 +01003913 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003914 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003915 }
3916 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003917 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003918 LOGINT(ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003919 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003920 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003921 }
3922
Michal Vaskobb520442017-05-23 10:55:18 +02003923 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
3924 * - useless to search for that in augments) */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003925 if (!tmp_mod->implemented && node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003926get_next_augment:
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003927 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
Michal Vaskobb520442017-05-23 10:55:18 +02003928 }
3929
Michal Vaskocb45f472018-02-12 10:47:42 +01003930 tmp_parent = (last_aug ? (struct lys_node *)last_aug : node);
3931 node = NULL;
3932 while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) {
3933 if (lys_node_module(node) != lys_main_module(tmp_mod)) {
3934 continue;
3935 }
3936 if (strncmp(node->name, name, nam_len) || node->name[nam_len]) {
3937 continue;
3938 }
3939 /* match */
3940 break;
3941 }
3942 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003943 if (last_aug) {
Michal Vaskob6906872018-03-12 11:35:09 +01003944 /* restore the correct augment target */
3945 node = last_aug->target;
Michal Vaskobb520442017-05-23 10:55:18 +02003946 goto get_next_augment;
3947 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003948 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003949 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003950 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003951
Michal Vaskoe27516a2016-10-10 17:55:31 +00003952 if (first_iter) {
3953 /* set external dependency flag, we can decide based on the first found node */
Michal Vasko1c007172017-03-10 10:20:44 +01003954 if (op_node && parent_times &&
3955 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003956 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003957 }
3958 first_iter = 0;
3959 }
3960
Michal Vasko1f76a282015-08-04 16:16:53 +02003961 if (has_predicate) {
3962 /* we have predicate, so the current result must be list */
3963 if (node->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003964 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003965 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003966 }
3967
Michal Vasko1c007172017-03-10 10:20:44 +01003968 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
Michal Vaskobb520442017-05-23 10:55:18 +02003969 if (!i) {
3970 return EXIT_FAILURE;
3971 } else if (i < 0) {
3972 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003973 }
3974 id += i;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003975 has_predicate = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003976 }
3977 } while (id[0]);
3978
Michal Vaskoca917682016-07-25 11:00:37 +02003979 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
Radek Krejci2a5a9602016-11-04 10:21:13 +01003980 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003981 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3982 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003983 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003984 }
3985
Radek Krejcicf509982015-12-15 09:22:44 +01003986 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003987 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003988 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003989 return -1;
3990 }
3991
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003992 if (ret) {
3993 *ret = node;
3994 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003995
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003996 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003997}
3998
Michal Vasko730dfdf2015-08-11 14:48:05 +02003999/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02004000 * @brief Compare 2 data node values.
4001 *
4002 * Comparison performed on canonical forms, the first value
4003 * is first transformed into canonical form.
4004 *
4005 * @param[in] node Leaf/leaf-list with these values.
4006 * @param[in] noncan_val Non-canonical value.
4007 * @param[in] noncan_val_len Length of \p noncal_val.
4008 * @param[in] can_val Canonical value.
4009 * @return 1 if equal, 0 if not, -1 on error (logged).
4010 */
4011static int
4012valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
4013{
4014 int ret;
4015 struct lyd_node_leaf_list leaf;
4016 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
4017
4018 /* dummy leaf */
4019 memset(&leaf, 0, sizeof leaf);
4020 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
4021
4022repeat:
4023 leaf.value_type = sleaf->type.base;
4024 leaf.schema = node;
4025
4026 if (leaf.value_type == LY_TYPE_LEAFREF) {
4027 if (!sleaf->type.info.lref.target) {
4028 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01004029 LOGINT(node->module->ctx);
Michal Vasko718ecdd2017-10-03 14:12:39 +02004030 ret = -1;
4031 goto finish;
4032 }
4033 sleaf = sleaf->type.info.lref.target;
4034 goto repeat;
4035 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01004036 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02004037 ret = -1;
4038 goto finish;
4039 }
4040 }
4041
4042 if (!strcmp(leaf.value_str, can_val)) {
4043 ret = 1;
4044 } else {
4045 ret = 0;
4046 }
4047
4048finish:
4049 lydict_remove(node->module->ctx, leaf.value_str);
4050 return ret;
4051}
4052
4053/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004054 * @brief Resolve instance-identifier predicate in JSON data format.
4055 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004056 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02004057 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02004058 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004059 * @param[in,out] node Node matching the restriction without
4060 * the predicate. If it does not satisfy the predicate,
4061 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004062 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004063 * @return Number of characters successfully parsed,
4064 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004065 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004066static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004067resolve_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 +02004068{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004069 /* ... /node[key=value] ... */
4070 struct lyd_node_leaf_list *key;
4071 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02004072 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004073 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004074 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vasko53b7da02018-02-13 15:28:42 +01004075 struct ly_ctx *ctx = prev_mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004076
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004077 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02004078
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004079 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004080 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004081 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
4082 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004083 }
4084 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004085
Michal Vasko88850b72017-10-02 13:13:21 +02004086 if (!(*node)) {
4087 /* just parse it all */
4088 continue;
4089 }
4090
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004091 /* target */
4092 if (name[0] == '.') {
4093 /* leaf-list value */
4094 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004095 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004096 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4097 parsed = -1;
4098 goto cleanup;
4099 }
4100
4101 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004102 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004103 *node = NULL;
4104 goto cleanup;
4105 }
4106
4107 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004108 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004109
4110 /* keyless list position */
4111 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004112 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004113 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4114 parsed = -1;
4115 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004116 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004117
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004118 if (((struct lys_node_list *)(*node)->schema)->keys) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004119 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 +02004120 (*node)->schema->name);
4121 parsed = -1;
4122 goto cleanup;
4123 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004124
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004125 /* check the index */
4126 if (atoi(name) != cur_idx) {
4127 *node = NULL;
4128 goto cleanup;
4129 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004130
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004131 } else {
4132 /* list key value */
4133 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004134 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004135 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4136 parsed = -1;
4137 goto cleanup;
4138 }
4139 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004140
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004141 /* prepare key array */
4142 if (!list_keys) {
4143 list_keys = malloc(slist->keys_size * sizeof *list_keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01004144 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004145 for (i = 0; i < slist->keys_size; ++i) {
4146 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004147 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004148 }
4149
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004150 /* find the schema key leaf */
4151 for (i = 0; i < slist->keys_size; ++i) {
4152 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4153 break;
4154 }
4155 }
4156 if (i == slist->keys_size) {
4157 /* this list has no such key */
Michal Vasko53b7da02018-02-13 15:28:42 +01004158 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004159 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4160 parsed = -1;
4161 goto cleanup;
4162 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004163
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004164 /* check module */
4165 if (model) {
4166 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004167 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 +02004168 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4169 parsed = -1;
4170 goto cleanup;
4171 }
4172 } else {
4173 if (list_keys[i]->module != prev_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004174 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 +02004175 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4176 parsed = -1;
4177 goto cleanup;
4178 }
4179 }
4180
4181 /* find the actual data key */
4182 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4183 if (key->schema == (struct lys_node *)list_keys[i]) {
4184 break;
4185 }
4186 }
4187 if (!key) {
4188 /* list instance is missing a key? definitely should not happen */
Michal Vasko53b7da02018-02-13 15:28:42 +01004189 LOGINT(ctx);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004190 parsed = -1;
4191 goto cleanup;
4192 }
4193
4194 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004195 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004196 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004197 /* we still want to parse the whole predicate */
4198 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004199 }
4200
4201 /* everything is fine, mark this key as resolved */
4202 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004203 }
4204 } while (has_predicate);
4205
Michal Vaskob2f40be2016-09-08 16:03:48 +02004206 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004207 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004208 for (i = 0; i < slist->keys_size; ++i) {
4209 if (list_keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004210 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 +02004211 parsed = -1;
4212 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004213 }
4214 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004215 }
4216
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004217cleanup:
4218 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004219 return parsed;
4220}
4221
Michal Vasko895c11f2018-03-12 11:35:58 +01004222static int
4223check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004224{
Michal Vasko0b963112017-08-11 12:45:36 +02004225 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004226 struct lyxp_set set;
Michal Vasko895c11f2018-03-12 11:35:58 +01004227 enum int_log_opts prev_ilo;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004228
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004229 if (check_place) {
4230 parent = node;
4231 while (parent) {
4232 if (parent->nodetype == LYS_GROUPING) {
4233 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004234 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004235 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004236 if (parent->nodetype == LYS_AUGMENT) {
4237 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004238 /* unresolved augment, skip for now (will be checked later) */
4239 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004240 } else {
4241 parent = ((struct lys_node_augment *)parent)->target;
4242 continue;
4243 }
4244 }
4245 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004246 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004247 }
4248
Michal Vasko895c11f2018-03-12 11:35:58 +01004249 memset(&set, 0, sizeof set);
Michal Vasko9e635ac2016-10-17 11:44:09 +02004250
Michal Vasko895c11f2018-03-12 11:35:58 +01004251 /* produce just warnings */
4252 ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL);
4253 lyxp_node_atomize(node, &set, 1);
4254 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
4255
4256 if (set.val.snodes) {
4257 free(set.val.snodes);
4258 }
4259 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004260}
4261
Radek Krejcif71f48f2016-10-25 16:37:24 +02004262static int
4263check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4264{
Radek Krejcidce5f972017-09-12 15:47:49 +02004265 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004266
4267 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004268 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4269 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004270 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 +02004271 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4272 return -1;
4273 }
4274 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4275 * of leafref resolving (lys_leaf_add_leafref_target()) */
4276 } else if (type->base == LY_TYPE_UNION) {
4277 for (i = 0; i < type->info.uni.count; i++) {
4278 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4279 return -1;
4280 }
4281 }
4282 }
4283 return 0;
4284}
4285
Michal Vasko9e635ac2016-10-17 11:44:09 +02004286/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004287 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004288 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004289 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004290 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004291 * @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 +02004292 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004293 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004294 *
4295 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004296 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004297int
4298inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004299{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004300 struct lys_node_leaf *leaf;
Michal Vasko53b7da02018-02-13 15:28:42 +01004301 struct ly_ctx *ctx;
4302
4303 if (!node) {
4304 return 0;
4305 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004306
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004307 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Michal Vasko53b7da02018-02-13 15:28:42 +01004308 ctx = node->module->ctx;
4309
Radek Krejci1d82ef62015-08-07 14:44:40 +02004310 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004311 if (clear) {
4312 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004313 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004314 } else {
4315 if (node->flags & LYS_CONFIG_SET) {
4316 /* skip nodes with an explicit config value */
4317 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004318 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config");
4319 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004320 return -1;
4321 }
4322 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004323 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004324
4325 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4326 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4327 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004328 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4329 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004330 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
Michal Vaskoe022a562016-09-27 14:24:15 +02004331 return -1;
4332 }
4333 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004334 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004335 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004336 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004337 return -1;
4338 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004339 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4340 leaf = (struct lys_node_leaf *)node;
4341 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004342 return -1;
4343 }
4344 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004345 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004346
4347 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004348}
4349
Michal Vasko730dfdf2015-08-11 14:48:05 +02004350/**
Michal Vasko7178e692016-02-12 15:58:05 +01004351 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004352 *
Michal Vaskobb211122015-08-19 14:03:11 +02004353 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004354 * @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 +01004355 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004356 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004357 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004358 */
Michal Vasko7178e692016-02-12 15:58:05 +01004359static int
Michal Vasko97234262018-02-01 09:53:01 +01004360resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004361{
Michal Vasko44ab1462017-05-18 13:18:36 +02004362 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004363 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004364 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004365 struct ly_set *set;
Michal Vasko53b7da02018-02-13 15:28:42 +01004366 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004367
Michal Vasko2ef7db62017-06-12 09:24:02 +02004368 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004369 mod = lys_main_module(aug->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01004370 ctx = mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004371
Michal Vaskobb520442017-05-23 10:55:18 +02004372 /* set it as not applied for now */
4373 aug->flags |= LYS_NOTAPPLIED;
4374
Michal Vasko2ef7db62017-06-12 09:24:02 +02004375 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004376 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004377 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004378 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 +02004379 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004380 LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004381 return -1;
4382 }
Michal Vasko50576712017-07-28 12:28:33 +02004383 if (!set) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004384 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004385 return EXIT_FAILURE;
4386 }
Michal Vasko50576712017-07-28 12:28:33 +02004387 aug->target = set->set.s[0];
4388 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004389 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004390
Michal Vaskod58d5962016-03-02 14:29:41 +01004391 /* check for mandatory nodes - if the target node is in another module
4392 * the added nodes cannot be mandatory
4393 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004394 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4395 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004396 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004397 }
4398
Michal Vasko07e89ef2016-03-03 13:28:57 +01004399 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004400 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004401 LY_TREE_FOR(aug->child, sub) {
4402 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4403 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004404 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4405 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004406 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004407 return -1;
4408 }
4409 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004410 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004411 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004412 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004413 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4414 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004415 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004416 return -1;
4417 }
4418 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004419 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004420 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004421 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004422 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4423 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004424 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004425 return -1;
4426 }
4427 }
4428 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004429 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
4430 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 +01004431 return -1;
4432 }
4433
Radek Krejcic071c542016-01-27 14:57:51 +01004434 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004435 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004436 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004437 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004438 }
4439 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004440
Michal Vasko44ab1462017-05-18 13:18:36 +02004441 if (!aug->child) {
4442 /* empty augment, nothing to connect, but it is techincally applied */
Michal Vasko53b7da02018-02-13 15:28:42 +01004443 LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name);
Michal Vasko44ab1462017-05-18 13:18:36 +02004444 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004445 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4446 /* we try to connect the augment only in case the module is implemented or
4447 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004448 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004449 }
4450
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004451 return EXIT_SUCCESS;
4452}
4453
Radek Krejcie534c132016-11-23 13:32:31 +01004454static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004455resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004456{
4457 enum LY_VLOG_ELEM vlog_type;
4458 void *vlog_node;
4459 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004460 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004461 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004462 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004463 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004464 struct lys_ext_instance *tmp_ext;
Michal Vasko53b7da02018-02-13 15:28:42 +01004465 struct ly_ctx *ctx = NULL;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004466 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004467
4468 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004469 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004470 vlog_node = info->parent;
4471 vlog_type = LY_VLOG_LYS;
4472 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004473 case LYEXT_PAR_MODULE:
4474 case LYEXT_PAR_IMPORT:
4475 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004476 vlog_node = NULL;
4477 vlog_type = LY_VLOG_LYS;
4478 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004479 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004480 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004481 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004482 break;
4483 }
4484
4485 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004486 /* YIN */
4487
Radek Krejcie534c132016-11-23 13:32:31 +01004488 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004489 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004490 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004491 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004492 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004493 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004494 ctx = mod->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01004495
4496 /* find the extension definition */
4497 e = NULL;
4498 for (i = 0; i < mod->extensions_size; i++) {
4499 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4500 e = &mod->extensions[i];
4501 break;
4502 }
4503 }
4504 /* try submodules */
4505 for (j = 0; !e && j < mod->inc_size; j++) {
4506 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4507 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4508 e = &mod->inc[j].submodule->extensions[i];
4509 break;
4510 }
4511 }
4512 }
4513 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004514 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004515 return EXIT_FAILURE;
4516 }
4517
4518 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004519
Radek Krejci72b35992017-01-04 16:27:44 +01004520 if (e->plugin && e->plugin->check_position) {
4521 /* common part - we have plugin with position checking function, use it first */
4522 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4523 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004524 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004525 return -1;
4526 }
4527 }
4528
Radek Krejci8d6b7422017-02-03 14:42:13 +01004529 /* extension type-specific part - allocation */
4530 if (e->plugin) {
4531 etype = e->plugin->type;
4532 } else {
4533 /* default type */
4534 etype = LYEXT_FLAG;
4535 }
4536 switch (etype) {
4537 case LYEXT_FLAG:
4538 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4539 break;
4540 case LYEXT_COMPLEX:
4541 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4542 break;
4543 case LYEXT_ERR:
4544 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004545 LOGINT(ctx);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004546 return -1;
4547 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004548 LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004549
4550 /* common part for all extension types */
4551 (*ext)->def = e;
4552 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004553 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004554 (*ext)->insubstmt = info->substmt;
4555 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004556 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004557 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004558
PavolVicand86b0d62017-09-01 11:01:39 +02004559 if (e->argument) {
4560 if (!(e->flags & LYS_YINELEM)) {
4561 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4562 if (!(*ext)->arg_value) {
PavolVicane7a1fc62018-02-19 16:50:27 +01004563 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
PavolVicand86b0d62017-09-01 11:01:39 +02004564 return -1;
4565 }
4566
4567 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4568 } else {
4569 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4570 if (ly_strequal(yin->name, e->argument, 1)) {
4571 (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0);
4572 lyxml_free(mod->ctx, yin);
4573 break;
4574 }
4575 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004576 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004577 }
4578
PavolVican92f23622017-12-12 13:35:56 +01004579 if ((*ext)->flags & LYEXT_OPT_VALID &&
4580 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01004581 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004582 }
4583
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004584 (*ext)->nodetype = LYS_EXT;
4585 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004586
Radek Krejci8d6b7422017-02-03 14:42:13 +01004587 /* extension type-specific part - parsing content */
4588 switch (etype) {
4589 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004590 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4591 if (!yin->ns) {
4592 /* garbage */
4593 lyxml_free(mod->ctx, yin);
4594 continue;
4595 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4596 /* standard YANG statements are not expected here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004597 LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004598 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004599 } else if (yin->ns == info->data.yin->ns &&
4600 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004601 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004602 if ((*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004603 LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004604 return -1;
4605 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004606 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004607 yin->content = NULL;
4608 lyxml_free(mod->ctx, yin);
4609 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004610 /* extension instance */
4611 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4612 LYEXT_SUBSTMT_SELF, 0, unres)) {
4613 return -1;
4614 }
Radek Krejci72b35992017-01-04 16:27:44 +01004615
Radek Krejci72b35992017-01-04 16:27:44 +01004616 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004617 }
Radek Krejci72b35992017-01-04 16:27:44 +01004618 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004619 break;
4620 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004621 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004622 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4623 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004624 return -1;
4625 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004626 break;
4627 default:
4628 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004629 }
Radek Krejci72b35992017-01-04 16:27:44 +01004630
4631 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4632
Radek Krejcie534c132016-11-23 13:32:31 +01004633 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004634 /* YANG */
4635
PavolVicanc1807262017-01-31 18:00:27 +01004636 ext_prefix = (char *)(*ext)->def;
4637 tmp = strchr(ext_prefix, ':');
4638 if (!tmp) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004639 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004640 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004641 }
4642 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004643
PavolVicanc1807262017-01-31 18:00:27 +01004644 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004645 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004646 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004647 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004648 return EXIT_FAILURE;
4649 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004650 ctx = mod->ctx;
PavolVicanc1807262017-01-31 18:00:27 +01004651
4652 /* find the extension definition */
4653 e = NULL;
4654 for (i = 0; i < mod->extensions_size; i++) {
4655 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4656 e = &mod->extensions[i];
4657 break;
4658 }
4659 }
4660 /* try submodules */
4661 for (j = 0; !e && j < mod->inc_size; j++) {
4662 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4663 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4664 e = &mod->inc[j].submodule->extensions[i];
4665 break;
4666 }
4667 }
4668 }
4669 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004670 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004671 return EXIT_FAILURE;
4672 }
4673
PavolVicanfcc98762017-09-01 15:51:39 +02004674 (*ext)->flags &= ~LYEXT_OPT_YANG;
4675 (*ext)->def = NULL;
4676
PavolVicanc1807262017-01-31 18:00:27 +01004677 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4678
4679 if (e->plugin && e->plugin->check_position) {
4680 /* common part - we have plugin with position checking function, use it first */
4681 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4682 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004683 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01004684 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004685 }
4686 }
4687
PavolVican22e88682017-02-14 22:38:18 +01004688 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01004689 (*ext)->def = e;
4690 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01004691 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004692 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
PavolVican22e88682017-02-14 22:38:18 +01004693
PavolVicanb0d84102017-02-15 16:32:42 +01004694 if (e->argument && !(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004695 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
PavolVicanb0d84102017-02-15 16:32:42 +01004696 goto error;
4697 }
4698
PavolVican92f23622017-12-12 13:35:56 +01004699 if ((*ext)->flags & LYEXT_OPT_VALID &&
4700 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01004701 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
PavolVican92f23622017-12-12 13:35:56 +01004702 }
4703
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004704 (*ext)->module = info->mod;
4705 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004706
PavolVican22e88682017-02-14 22:38:18 +01004707 /* extension type-specific part */
4708 if (e->plugin) {
4709 etype = e->plugin->type;
4710 } else {
4711 /* default type */
4712 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01004713 }
PavolVican22e88682017-02-14 22:38:18 +01004714 switch (etype) {
4715 case LYEXT_FLAG:
4716 /* nothing change */
4717 break;
4718 case LYEXT_COMPLEX:
4719 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Michal Vasko53b7da02018-02-13 15:28:42 +01004720 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error);
PavolVican22e88682017-02-14 22:38:18 +01004721 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
4722 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01004723 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01004724 if (info->data.yang) {
4725 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01004726 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
4727 (struct lys_ext_instance_complex*)(*ext))) {
4728 goto error;
4729 }
4730 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
4731 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01004732 goto error;
4733 }
PavolVicana3876672017-02-21 15:49:51 +01004734 }
4735 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
4736 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01004737 }
PavolVican22e88682017-02-14 22:38:18 +01004738 break;
4739 case LYEXT_ERR:
4740 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004741 LOGINT(ctx);
PavolVican22e88682017-02-14 22:38:18 +01004742 goto error;
4743 }
4744
PavolVican22e88682017-02-14 22:38:18 +01004745 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
4746 goto error;
4747 }
4748 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01004749 }
4750
4751 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01004752error:
4753 free(ext_prefix);
4754 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01004755}
4756
Michal Vasko730dfdf2015-08-11 14:48:05 +02004757/**
Pavol Vican855ca622016-09-05 13:07:54 +02004758 * @brief Resolve (find) choice default case. Does not log.
4759 *
4760 * @param[in] choic Choice to use.
4761 * @param[in] dflt Name of the default case.
4762 *
4763 * @return Pointer to the default node or NULL.
4764 */
4765static struct lys_node *
4766resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4767{
4768 struct lys_node *child, *ret;
4769
4770 LY_TREE_FOR(choic->child, child) {
4771 if (child->nodetype == LYS_USES) {
4772 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4773 if (ret) {
4774 return ret;
4775 }
4776 }
4777
4778 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02004779 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02004780 return child;
4781 }
4782 }
4783
4784 return NULL;
4785}
4786
4787/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02004788 * @brief Resolve uses, apply augments, refines. Logs directly.
4789 *
Michal Vaskobb211122015-08-19 14:03:11 +02004790 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004791 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004792 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004793 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004794 */
Michal Vasko184521f2015-09-24 13:14:26 +02004795static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004796resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004797{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004798 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02004799 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02004800 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02004801 struct lys_node_leaflist *llist;
4802 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02004803 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004804 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004805 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004806 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004807 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004808 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004809
Michal Vasko71e1aa82015-08-12 12:17:51 +02004810 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01004811
Radek Krejci93def382017-05-24 15:33:48 +02004812 /* check that the grouping is resolved (no unresolved uses inside) */
4813 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02004814
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004815 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004816 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01004817 if (node_aux->nodetype & LYS_GROUPING) {
4818 /* do not instantiate groupings from groupings */
4819 continue;
4820 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01004821 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004822 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004823 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
4824 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004825 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004826 }
Pavol Vican55abd332016-07-12 15:54:49 +02004827 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01004828 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 +02004829 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004830 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004831 }
4832 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004833 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004834
Michal Vaskodef0db12015-10-07 13:22:48 +02004835 /* we managed to copy the grouping, the rest must be possible to resolve */
4836
Pavol Vican855ca622016-09-05 13:07:54 +02004837 if (uses->refine_size) {
4838 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01004839 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004840 }
4841
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004842 /* apply refines */
4843 for (i = 0; i < uses->refine_size; i++) {
4844 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01004845 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
4846 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02004847 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004848 if (rc || !node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004849 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004850 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004851 }
4852
Radek Krejci1d82ef62015-08-07 14:44:40 +02004853 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004854 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
4855 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004856 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004857 }
Pavol Vican855ca622016-09-05 13:07:54 +02004858 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004859
4860 /* description on any nodetype */
4861 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004862 lydict_remove(ctx, node->dsc);
4863 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004864 }
4865
4866 /* reference on any nodetype */
4867 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004868 lydict_remove(ctx, node->ref);
4869 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004870 }
4871
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004872 /* config on any nodetype,
4873 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
4874 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004875 node->flags &= ~LYS_CONFIG_MASK;
4876 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004877 }
4878
4879 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02004880 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004881 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004882 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02004883 leaf = (struct lys_node_leaf *)node;
4884
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004885 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02004886 lydict_remove(ctx, leaf->dflt);
4887 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
4888
4889 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01004890 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
4891 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004892 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004893 }
Radek Krejci200bf712016-08-16 17:11:04 +02004894 } else if (node->nodetype == LYS_LEAFLIST) {
4895 /* leaf-list */
4896 llist = (struct lys_node_leaflist *)node;
4897
4898 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01004899 for (j = 0; j < llist->dflt_size; j++) {
4900 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02004901 }
4902 free(llist->dflt);
4903
4904 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02004905 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01004906 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail);
Radek Krejci200bf712016-08-16 17:11:04 +02004907 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01004908 for (j = 0; j < llist->dflt_size; j++) {
4909 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02004910 }
4911
4912 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01004913 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01004914 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01004915 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02004916 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02004917 }
4918 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004919 }
4920 }
4921
4922 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004923 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01004924 /* remove current value */
4925 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004926
Radek Krejcibf285832017-01-26 16:05:41 +01004927 /* set new value */
4928 node->flags |= (rfn->flags & LYS_MAND_MASK);
4929
Pavol Vican855ca622016-09-05 13:07:54 +02004930 if (rfn->flags & LYS_MAND_TRUE) {
4931 /* check if node has default value */
4932 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004933 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02004934 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004935 goto fail;
4936 }
4937 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004938 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02004939 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004940 goto fail;
4941 }
4942 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004943 }
4944
4945 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004946 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4947 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4948 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004949 }
4950
4951 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004952 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004953 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004954 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004955 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004956 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004957 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004958 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004959 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004960 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004961 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004962 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004963 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004964 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004965 }
4966 }
4967
4968 /* must in leaf, leaf-list, list, container or anyxml */
4969 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004970 switch (node->nodetype) {
4971 case LYS_LEAF:
4972 old_size = &((struct lys_node_leaf *)node)->must_size;
4973 old_must = &((struct lys_node_leaf *)node)->must;
4974 break;
4975 case LYS_LEAFLIST:
4976 old_size = &((struct lys_node_leaflist *)node)->must_size;
4977 old_must = &((struct lys_node_leaflist *)node)->must;
4978 break;
4979 case LYS_LIST:
4980 old_size = &((struct lys_node_list *)node)->must_size;
4981 old_must = &((struct lys_node_list *)node)->must;
4982 break;
4983 case LYS_CONTAINER:
4984 old_size = &((struct lys_node_container *)node)->must_size;
4985 old_must = &((struct lys_node_container *)node)->must;
4986 break;
4987 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004988 case LYS_ANYDATA:
4989 old_size = &((struct lys_node_anydata *)node)->must_size;
4990 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004991 break;
4992 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01004993 LOGINT(ctx);
Michal Vaskoa86508c2016-08-26 14:30:19 +02004994 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004995 }
4996
4997 size = *old_size + rfn->must_size;
4998 must = realloc(*old_must, size * sizeof *rfn->must);
Michal Vasko53b7da02018-02-13 15:28:42 +01004999 LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005000 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01005001 must[j].ext_size = rfn->must[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005002 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 +02005003 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02005004 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
5005 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
5006 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
5007 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
5008 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02005009 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005010 }
5011
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005012 *old_must = must;
5013 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02005014
5015 /* check XPath dependencies again */
5016 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
5017 goto fail;
5018 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005019 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02005020
5021 /* if-feature in leaf, leaf-list, list, container or anyxml */
5022 if (rfn->iffeature_size) {
5023 old_size = &node->iffeature_size;
5024 old_iff = &node->iffeature;
5025
5026 size = *old_size + rfn->iffeature_size;
5027 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01005028 LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02005029 *old_iff = iff;
5030
Pavol Vican855ca622016-09-05 13:07:54 +02005031 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
5032 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005033 if (usize1) {
5034 /* there is something to duplicate */
5035 /* duplicate compiled expression */
5036 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
5037 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01005038 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005039 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005040
5041 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02005042 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01005043 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005044 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005045
Radek Krejci3a3b2002017-09-13 16:39:02 +02005046 /* duplicate extensions */
5047 iff[j].ext_size = rfn->iffeature[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005048 lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
Radek Krejci3a3b2002017-09-13 16:39:02 +02005049 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
5050 }
5051 (*old_size)++;
5052 }
5053 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005054 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005055 }
5056
5057 /* apply augments */
5058 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01005059 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005060 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005061 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005062 }
5063 }
5064
Pavol Vican855ca622016-09-05 13:07:54 +02005065 /* check refines */
5066 for (i = 0; i < uses->refine_size; i++) {
5067 node = refine_nodes[i];
5068 rfn = &uses->refine[i];
5069
5070 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005071 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02005072 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01005073 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02005074 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
5075 (rfn->flags & LYS_CONFIG_W)) {
5076 /* setting config true under config false is prohibited */
Michal Vasko53b7da02018-02-13 15:28:42 +01005077 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5078 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005079 "changing config from 'false' to 'true' is prohibited while "
5080 "the target's parent is still config 'false'.");
5081 goto fail;
5082 }
5083
5084 /* inherit config change to the target children */
5085 LY_TREE_DFS_BEGIN(node->child, next, iter) {
5086 if (rfn->flags & LYS_CONFIG_W) {
5087 if (iter->flags & LYS_CONFIG_SET) {
5088 /* config is set explicitely, go to next sibling */
5089 next = NULL;
5090 goto nextsibling;
5091 }
5092 } else { /* LYS_CONFIG_R */
5093 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
5094 /* error - we would have config data under status data */
Michal Vasko53b7da02018-02-13 15:28:42 +01005095 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5096 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005097 "changing config from 'true' to 'false' is prohibited while the target "
5098 "has still a children with explicit config 'true'.");
5099 goto fail;
5100 }
5101 }
5102 /* change config */
5103 iter->flags &= ~LYS_CONFIG_MASK;
5104 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
5105
5106 /* select next iter - modified LY_TREE_DFS_END */
5107 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5108 next = NULL;
5109 } else {
5110 next = iter->child;
5111 }
5112nextsibling:
5113 if (!next) {
5114 /* try siblings */
5115 next = iter->next;
5116 }
5117 while (!next) {
5118 /* parent is already processed, go to its sibling */
5119 iter = lys_parent(iter);
5120
5121 /* no siblings, go back through parents */
5122 if (iter == node) {
5123 /* we are done, no next element to process */
5124 break;
5125 }
5126 next = iter->next;
5127 }
5128 }
5129 }
5130
5131 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005132 if (rfn->dflt_size) {
5133 if (node->nodetype == LYS_CHOICE) {
5134 /* choice */
5135 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5136 rfn->dflt[0]);
5137 if (!((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005138 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005139 goto fail;
5140 }
5141 if (lyp_check_mandatory_choice(node)) {
5142 goto fail;
5143 }
Pavol Vican855ca622016-09-05 13:07:54 +02005144 }
5145 }
5146
5147 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02005148 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005149 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005150 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5151 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005152 goto fail;
5153 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005154 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005155 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005156 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5157 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005158 goto fail;
5159 }
5160 }
5161
5162 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005163 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005164 if (node->nodetype == LYS_LEAFLIST) {
5165 llist = (struct lys_node_leaflist *)node;
5166 if (llist->dflt_size && llist->min) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005167 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5168 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005169 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5170 goto fail;
5171 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005172 } else if (node->nodetype == LYS_LEAF) {
5173 leaf = (struct lys_node_leaf *)node;
5174 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005175 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5176 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005177 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5178 goto fail;
5179 }
Pavol Vican855ca622016-09-05 13:07:54 +02005180 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005181
Pavol Vican855ca622016-09-05 13:07:54 +02005182 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005183 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005184 for (parent = node->parent;
5185 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5186 parent = parent->parent) {
5187 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5188 /* stop also on presence containers */
5189 break;
5190 }
5191 }
5192 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5193 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5194 if (lyp_check_mandatory_choice(parent)) {
5195 goto fail;
5196 }
5197 }
5198 }
5199 }
5200 free(refine_nodes);
5201
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005202 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005203
5204fail:
5205 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5206 lys_node_free(iter, NULL, 0);
5207 }
Pavol Vican855ca622016-09-05 13:07:54 +02005208 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005209 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005210}
5211
Radek Krejci83a4bac2017-02-07 15:53:04 +01005212void
5213resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005214{
5215 int i;
5216
5217 assert(der && base);
5218
Radek Krejci018f1f52016-08-03 16:01:20 +02005219 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005220 /* create a set for backlinks if it does not exist */
5221 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005222 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005223 /* store backlink */
5224 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005225
Radek Krejci85a54be2016-10-20 12:39:56 +02005226 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005227 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005228 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005229 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005230}
5231
Michal Vasko730dfdf2015-08-11 14:48:05 +02005232/**
5233 * @brief Resolve base identity recursively. Does not log.
5234 *
5235 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005236 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005237 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005238 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005239 *
Radek Krejci219fa612016-08-15 10:36:51 +02005240 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005241 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005242static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005243resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005244 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005245{
Michal Vaskof02e3742015-08-05 16:27:02 +02005246 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005247 struct lys_ident *base = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005248 struct ly_ctx *ctx = module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005249
Radek Krejcicf509982015-12-15 09:22:44 +01005250 assert(ret);
5251
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005252 /* search module */
5253 for (i = 0; i < module->ident_size; i++) {
5254 if (!strcmp(basename, module->ident[i].name)) {
5255
5256 if (!ident) {
5257 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005258 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005259 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005260 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005261 }
5262
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005263 base = &module->ident[i];
5264 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005265 }
5266 }
5267
5268 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005269 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5270 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5271 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005272
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005273 if (!ident) {
5274 *ret = &module->inc[j].submodule->ident[i];
5275 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005276 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005277
5278 base = &module->inc[j].submodule->ident[i];
5279 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005280 }
5281 }
5282 }
5283
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005284matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005285 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005286 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005287 /* is it already completely resolved? */
5288 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005289 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005290 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5291
5292 /* simple check for circular reference,
5293 * the complete check is done as a side effect of using only completely
5294 * resolved identities (previous check of unres content) */
5295 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005296 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5297 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005298 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005299 }
5300
Radek Krejci06f64ed2016-08-15 11:07:44 +02005301 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005302 }
5303 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005304
Radek Krejcibabbff82016-02-19 13:31:37 +01005305 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005306 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005307 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005308 }
5309
Radek Krejci219fa612016-08-15 10:36:51 +02005310 /* base not found (maybe a forward reference) */
5311 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005312}
5313
Michal Vasko730dfdf2015-08-11 14:48:05 +02005314/**
5315 * @brief Resolve base identity. Logs directly.
5316 *
5317 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005318 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005319 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005320 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005321 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005322 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005323 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005324 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005325static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005326resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005327 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005328{
5329 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005330 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005331 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005332 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005333 struct lys_module *mod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005334 struct ly_ctx *ctx = module->ctx;
Radek Krejcicf509982015-12-15 09:22:44 +01005335
5336 assert((ident && !type) || (!ident && type));
5337
5338 if (!type) {
5339 /* have ident to resolve */
5340 ret = &target;
5341 flags = ident->flags;
5342 mod = ident->module;
5343 } else {
5344 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005345 ++type->info.ident.count;
5346 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 +01005347 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005348
5349 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005350 flags = type->parent->flags;
5351 mod = type->parent->module;
5352 }
Michal Vaskof2006002016-04-21 16:28:15 +02005353 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005354
5355 /* search for the base identity */
5356 name = strchr(basename, ':');
5357 if (name) {
5358 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005359 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005360 name++;
5361
Michal Vasko2d851a92015-10-20 16:16:36 +02005362 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005363 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005364 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005365 }
5366 } else {
5367 name = basename;
5368 }
5369
Radek Krejcic071c542016-01-27 14:57:51 +01005370 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005371 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005372 if (!module) {
5373 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01005374 LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005375 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005376 }
5377
Radek Krejcic071c542016-01-27 14:57:51 +01005378 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005379 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5380 if (!rc) {
5381 assert(*ret);
5382
5383 /* check status */
5384 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5385 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5386 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005387 } else if (ident) {
5388 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005389 if (lys_main_module(mod)->implemented) {
5390 /* in case of the implemented identity, maintain backlinks to it
5391 * from the base identities to make it available when resolving
5392 * data with the identity values (not implemented identity is not
5393 * allowed as an identityref value). */
5394 resolve_identity_backlink_update(ident, *ret);
5395 }
Radek Krejci219fa612016-08-15 10:36:51 +02005396 }
5397 } else if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005398 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005399 if (type) {
5400 --type->info.ident.count;
5401 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005402 }
5403
Radek Krejci219fa612016-08-15 10:36:51 +02005404 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005405}
5406
Radek Krejci9e6af732017-04-27 14:40:25 +02005407/*
5408 * 1 - true (der is derived from base)
5409 * 0 - false (der is not derived from base)
5410 */
5411static int
5412search_base_identity(struct lys_ident *der, struct lys_ident *base)
5413{
5414 int i;
5415
5416 if (der == base) {
5417 return 1;
5418 } else {
5419 for(i = 0; i < der->base_size; i++) {
5420 if (search_base_identity(der->base[i], base) == 1) {
5421 return 1;
5422 }
5423 }
5424 }
5425
5426 return 0;
5427}
5428
Michal Vasko730dfdf2015-08-11 14:48:05 +02005429/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005430 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005431 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005432 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005433 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005434 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005435 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005436 *
5437 * @return Pointer to the identity resolvent, NULL on error.
5438 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005439struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005440resolve_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 +02005441{
Radek Krejci9e6af732017-04-27 14:40:25 +02005442 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005443 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005444 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005445 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005446 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005447 struct lys_ident *der, *cur;
Radek Krejci9e6af732017-04-27 14:40:25 +02005448 struct lys_module *imod = NULL, *m;
Michal Vasko53b7da02018-02-13 15:28:42 +01005449 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005450
Radek Krejci9e6af732017-04-27 14:40:25 +02005451 assert(type && ident_name && node && mod);
Michal Vasko53b7da02018-02-13 15:28:42 +01005452 ctx = mod->ctx;
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005453
Michal Vaskof2d43962016-09-02 11:10:16 +02005454 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005455 return NULL;
5456 }
5457
Michal Vasko50576712017-07-28 12:28:33 +02005458 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005459 if (rc < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005460 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005461 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005462 } else if (rc < (signed)strlen(ident_name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005463 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005464 return NULL;
5465 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005466
5467 m = lys_main_module(mod); /* shortcut */
5468 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5469 /* identity is defined in the same module as node */
5470 imod = m;
5471 } else if (dflt) {
5472 /* solving identityref in default definition in schema -
5473 * find the identity's module in the imported modules list to have a correct revision */
5474 for (i = 0; i < mod->imp_size; i++) {
5475 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5476 imod = mod->imp[i].module;
5477 break;
5478 }
5479 }
5480 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005481 /* solving identityref in data - get the module from the context */
5482 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5483 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005484 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005485 break;
5486 }
Michal Vasko08767f72017-10-06 14:38:08 +02005487 imod = NULL;
5488 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005489 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5490 /* we are currently parsing some module and checking XPath or a default value,
5491 * so take this module into account */
5492 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5493 imod = mod->ctx->models.parsing_sub_modules[i];
5494 if (imod->type) {
5495 /* skip submodules */
5496 continue;
5497 }
5498 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5499 break;
5500 }
5501 imod = NULL;
5502 }
5503 }
Michal Vasko08767f72017-10-06 14:38:08 +02005504 }
5505
Michal Vasko53b7da02018-02-13 15:28:42 +01005506 if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) {
Michal Vasko08767f72017-10-06 14:38:08 +02005507 /* the needed module was not found, but it may have been expected so call the data callback */
5508 if (imod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005509 ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005510 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005511 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01005512 imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
Michal Vasko08767f72017-10-06 14:38:08 +02005513 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005514 }
5515 }
5516 if (!imod) {
5517 goto fail;
5518 }
5519
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005520 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005521 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005522 * THEN, we may need to make the module with the identity implemented, but only if it really
5523 * contains the identity */
5524 if (!imod->implemented) {
5525 cur = NULL;
5526 /* get the identity in the module */
5527 for (i = 0; i < imod->ident_size; i++) {
5528 if (!strcmp(name, imod->ident[i].name)) {
5529 cur = &imod->ident[i];
5530 break;
5531 }
5532 }
5533 if (!cur) {
5534 /* go through includes */
5535 for (j = 0; j < imod->inc_size; j++) {
5536 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5537 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5538 cur = &imod->inc[j].submodule->ident[i];
5539 break;
5540 }
5541 }
5542 }
5543 if (!cur) {
5544 goto fail;
5545 }
5546 }
5547
5548 /* check that identity is derived from one of the type's base */
5549 while (type->der) {
5550 for (i = 0; i < type->info.ident.count; i++) {
5551 if (search_base_identity(cur, type->info.ident.ref[i])) {
5552 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005553 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005554 goto match;
5555 }
5556 }
5557 type = &type->der->type;
5558 }
5559 /* matching base not found */
Michal Vasko53b7da02018-02-13 15:28:42 +01005560 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
Radek Krejci9e6af732017-04-27 14:40:25 +02005561 goto fail;
5562 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005563 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005564
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005565 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005566 while (type->der) {
5567 for (i = 0; i < type->info.ident.count; ++i) {
5568 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005569
Radek Krejci85a54be2016-10-20 12:39:56 +02005570 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005571 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005572 for (j = 0; j < cur->der->number; j++) {
5573 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005574 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005575 /* we have match */
5576 cur = der;
5577 goto match;
5578 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005579 }
5580 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005581 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005582 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005583 }
5584
Radek Krejci9e6af732017-04-27 14:40:25 +02005585fail:
Michal Vasko53b7da02018-02-13 15:28:42 +01005586 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005587 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005588
5589match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005590 for (i = 0; i < cur->iffeature_size; i++) {
5591 if (!resolve_iffeature(&cur->iffeature[i])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005592 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
5593 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 +02005594 return NULL;
5595 }
5596 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005597 if (need_implemented) {
5598 if (dflt) {
5599 /* try to make the module implemented */
5600 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5601 imod->name, cur->name, mod->name);
5602 if (lys_set_implemented(imod)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005603 LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005604 imod->name, cur->name);
Michal Vasko53b7da02018-02-13 15:28:42 +01005605 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005606 goto fail;
5607 }
5608 } else {
5609 /* just say that it was found, but in a non-implemented module */
Michal Vasko53b7da02018-02-13 15:28:42 +01005610 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005611 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005612 goto fail;
5613 }
5614 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005615 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005616}
5617
Michal Vasko730dfdf2015-08-11 14:48:05 +02005618/**
Michal Vaskobb211122015-08-19 14:03:11 +02005619 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005620 *
Michal Vaskobb211122015-08-19 14:03:11 +02005621 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005622 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005623 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005624 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005625 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005626static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005627resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005628{
Radek Krejci93def382017-05-24 15:33:48 +02005629 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005630 struct lys_node *par_grp;
Michal Vasko53b7da02018-02-13 15:28:42 +01005631 struct ly_ctx *ctx = uses->module->ctx;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005632
Radek Krejci6ff885d2017-01-03 14:06:22 +01005633 /* 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 +02005634 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5635 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5636 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005637 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 +02005638
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005639 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005640 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5641 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005642 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005643 return -1;
5644 } else if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005645 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005646 return -1;
5647 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005648 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005649 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005650 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005651 return -1;
5652 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005653 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005654 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005655 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005656 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005657 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005658 }
5659
Radek Krejci93def382017-05-24 15:33:48 +02005660 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005661 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005662 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005663 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005664 return -1;
5665 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005666 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005667 } else {
5668 /* instantiate grouping only when it is completely resolved */
5669 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005670 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005671 return EXIT_FAILURE;
5672 }
5673
Radek Krejci48464ed2016-03-17 15:44:09 +01005674 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005675 if (!rc) {
5676 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005677 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005678 assert(((struct lys_node_grp *)par_grp)->unres_count);
5679 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01005680 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005681 }
Radek Krejcicf509982015-12-15 09:22:44 +01005682
5683 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005684 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005685 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005686 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01005687 return -1;
5688 }
5689
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005690 return EXIT_SUCCESS;
5691 }
5692
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005693 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005694}
5695
Michal Vasko730dfdf2015-08-11 14:48:05 +02005696/**
Michal Vasko9957e592015-08-17 15:04:09 +02005697 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005698 *
Michal Vaskobb211122015-08-19 14:03:11 +02005699 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005700 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005701 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005702 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005703 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005704static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005705resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005706{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005707 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01005708 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02005709 char *s = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005710 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005711
5712 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02005713 assert(keys_str);
5714
Radek Krejci5c08a992016-11-02 13:30:04 +01005715 if (!list->child) {
5716 /* no child, possible forward reference */
Michal Vasko53b7da02018-02-13 15:28:42 +01005717 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Radek Krejci5c08a992016-11-02 13:30:04 +01005718 return EXIT_FAILURE;
5719 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005720 /* get the key name */
5721 if ((value = strpbrk(keys_str, " \t\n"))) {
5722 len = value - keys_str;
5723 while (isspace(value[0])) {
5724 value++;
5725 }
5726 } else {
5727 len = strlen(keys_str);
5728 }
5729
Radek Krejcia98048c2017-05-24 16:35:48 +02005730 if (list->keys[i]) {
5731 /* skip already resolved keys */
5732 goto next;
5733 }
5734
Michal Vaskobb520442017-05-23 10:55:18 +02005735 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
5736 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005737 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005738 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02005739 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005740 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005741
Radek Krejci48464ed2016-03-17 15:44:09 +01005742 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005743 /* check_key logs */
5744 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005745 }
5746
Radek Krejcicf509982015-12-15 09:22:44 +01005747 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005748 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005749 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
5750 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01005751 return -1;
5752 }
5753
Radek Krejcia98048c2017-05-24 16:35:48 +02005754 /* default value - is ignored, keep it but print a warning */
5755 if (list->keys[i]->dflt) {
5756 /* log is not hidden only in case this resolving fails and in such a case
5757 * we cannot get here
5758 */
Michal Vasko53b7da02018-02-13 15:28:42 +01005759 assert(log_opt == ILO_STORE);
5760 log_opt = ILO_LOG;
5761 LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01005762 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Michal Vasko53b7da02018-02-13 15:28:42 +01005763 log_opt = ILO_STORE;
Radek Krejcia98048c2017-05-24 16:35:48 +02005764 free(s);
5765 }
5766
5767next:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005768 /* prepare for next iteration */
5769 while (value && isspace(value[0])) {
5770 value++;
5771 }
5772 keys_str = value;
5773 }
5774
Michal Vaskof02e3742015-08-05 16:27:02 +02005775 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005776}
5777
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005778/**
Michal Vaskobf19d252015-10-08 15:39:17 +02005779 * @brief Resolve (check) all must conditions of \p node.
5780 * Logs directly.
5781 *
5782 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005783 * @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 +02005784 *
5785 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
5786 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005787static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005788resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02005789{
Michal Vaskobf19d252015-10-08 15:39:17 +02005790 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005791 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02005792 struct lys_restr *must;
5793 struct lyxp_set set;
Michal Vasko53b7da02018-02-13 15:28:42 +01005794 struct ly_ctx *ctx = node->schema->module->ctx;
Michal Vaskobf19d252015-10-08 15:39:17 +02005795
5796 assert(node);
5797 memset(&set, 0, sizeof set);
5798
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005799 if (inout_parent) {
5800 for (schema = lys_parent(node->schema);
5801 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
5802 schema = lys_parent(schema));
5803 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005804 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005805 return -1;
5806 }
5807 must_size = ((struct lys_node_inout *)schema)->must_size;
5808 must = ((struct lys_node_inout *)schema)->must;
5809
5810 /* context node is the RPC/action */
5811 node = node->parent;
5812 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005813 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005814 return -1;
5815 }
5816 } else {
5817 switch (node->schema->nodetype) {
5818 case LYS_CONTAINER:
5819 must_size = ((struct lys_node_container *)node->schema)->must_size;
5820 must = ((struct lys_node_container *)node->schema)->must;
5821 break;
5822 case LYS_LEAF:
5823 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
5824 must = ((struct lys_node_leaf *)node->schema)->must;
5825 break;
5826 case LYS_LEAFLIST:
5827 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
5828 must = ((struct lys_node_leaflist *)node->schema)->must;
5829 break;
5830 case LYS_LIST:
5831 must_size = ((struct lys_node_list *)node->schema)->must_size;
5832 must = ((struct lys_node_list *)node->schema)->must;
5833 break;
5834 case LYS_ANYXML:
5835 case LYS_ANYDATA:
5836 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
5837 must = ((struct lys_node_anydata *)node->schema)->must;
5838 break;
5839 case LYS_NOTIF:
5840 must_size = ((struct lys_node_notif *)node->schema)->must_size;
5841 must = ((struct lys_node_notif *)node->schema)->must;
5842 break;
5843 default:
5844 must_size = 0;
5845 break;
5846 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005847 }
5848
5849 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005850 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005851 return -1;
5852 }
5853
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005854 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02005855
Michal Vasko8146d4c2016-05-09 15:50:29 +02005856 if (!set.val.bool) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01005857 if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005858 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
5859 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01005860 LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005861 if (must[i].emsg) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005862 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005863 }
5864 if (must[i].eapptag) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005865 ly_err_last_set_apptag(ctx, must[i].eapptag);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005866 }
5867 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02005868 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005869 }
5870 }
5871
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005872 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02005873}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005874
Michal Vaskobf19d252015-10-08 15:39:17 +02005875/**
Michal Vasko508a50d2016-09-07 14:50:33 +02005876 * @brief Resolve (find) when condition schema context node. Does not log.
5877 *
5878 * @param[in] schema Schema node with the when condition.
5879 * @param[out] ctx_snode When schema context node.
5880 * @param[out] ctx_snode_type Schema context node type.
5881 */
5882void
5883resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
5884{
5885 const struct lys_node *sparent;
5886
5887 /* find a not schema-only node */
5888 *ctx_snode_type = LYXP_NODE_ELEM;
5889 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
5890 if (schema->nodetype == LYS_AUGMENT) {
5891 sparent = ((struct lys_node_augment *)schema)->target;
5892 } else {
5893 sparent = schema->parent;
5894 }
5895 if (!sparent) {
5896 /* context node is the document root (fake root in our case) */
5897 if (schema->flags & LYS_CONFIG_W) {
5898 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
5899 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02005900 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02005901 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02005902 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskocb45f472018-02-12 10:47:42 +01005903 schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko508a50d2016-09-07 14:50:33 +02005904 break;
5905 }
5906 schema = sparent;
5907 }
5908
5909 *ctx_snode = (struct lys_node *)schema;
5910}
5911
5912/**
Michal Vaskocf024702015-10-08 15:01:42 +02005913 * @brief Resolve (find) when condition context node. Does not log.
5914 *
5915 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02005916 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02005917 * @param[out] ctx_node Context node.
5918 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02005919 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02005920 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02005921 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02005922static int
5923resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
5924 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005925{
Michal Vaskocf024702015-10-08 15:01:42 +02005926 struct lyd_node *parent;
5927 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005928 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02005929 uint16_t i, data_depth, schema_depth;
5930
Michal Vasko508a50d2016-09-07 14:50:33 +02005931 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02005932
Michal Vaskofe989752016-09-08 08:47:26 +02005933 if (node_type == LYXP_NODE_ELEM) {
5934 /* standard element context node */
5935 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
5936 for (sparent = schema, schema_depth = 0;
5937 sparent;
5938 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
5939 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
5940 ++schema_depth;
5941 }
Michal Vaskocf024702015-10-08 15:01:42 +02005942 }
Michal Vaskofe989752016-09-08 08:47:26 +02005943 if (data_depth < schema_depth) {
5944 return -1;
5945 }
Michal Vaskocf024702015-10-08 15:01:42 +02005946
Michal Vasko956e8542016-08-26 09:43:35 +02005947 /* find the corresponding data node */
5948 for (i = 0; i < data_depth - schema_depth; ++i) {
5949 node = node->parent;
5950 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02005951 if (node->schema != schema) {
5952 return -1;
5953 }
Michal Vaskofe989752016-09-08 08:47:26 +02005954 } else {
5955 /* root context node */
5956 while (node->parent) {
5957 node = node->parent;
5958 }
5959 while (node->prev->next) {
5960 node = node->prev;
5961 }
Michal Vaskocf024702015-10-08 15:01:42 +02005962 }
5963
Michal Vaskoa59495d2016-08-22 09:18:58 +02005964 *ctx_node = node;
5965 *ctx_node_type = node_type;
5966 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02005967}
5968
Michal Vasko76c3bd32016-08-24 16:02:52 +02005969/**
5970 * @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 +01005971 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02005972 *
5973 * @param[in] snode Schema node, whose children instances need to be unlinked.
5974 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
5975 * it is moved to point to another sibling still in the original tree.
5976 * @param[in,out] ctx_node When context node, adjusted if needed.
5977 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
5978 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
5979 * Ordering may change, but there will be no semantic change.
5980 *
5981 * @return EXIT_SUCCESS on success, -1 on error.
5982 */
5983static int
5984resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
5985 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
5986{
5987 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005988 const struct lys_node *slast;
Michal Vasko53b7da02018-02-13 15:28:42 +01005989 struct ly_ctx *ctx = snode->module->ctx;
Michal Vasko76c3bd32016-08-24 16:02:52 +02005990
5991 switch (snode->nodetype) {
5992 case LYS_AUGMENT:
5993 case LYS_USES:
5994 case LYS_CHOICE:
5995 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005996 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01005997 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005998 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
5999 continue;
6000 }
6001
6002 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006003 return -1;
6004 }
6005 }
6006 break;
6007 case LYS_CONTAINER:
6008 case LYS_LIST:
6009 case LYS_LEAF:
6010 case LYS_LEAFLIST:
6011 case LYS_ANYXML:
6012 case LYS_ANYDATA:
6013 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
6014 if (elem->schema == snode) {
6015
6016 if (elem == *ctx_node) {
6017 /* We are going to unlink our context node! This normally cannot happen,
6018 * but we use normal top-level data nodes for faking a document root node,
6019 * so if this is the context node, we just use the next top-level node.
6020 * Additionally, it can even happen that there are no top-level data nodes left,
6021 * all were unlinked, so in this case we pass NULL as the context node/data tree,
6022 * lyxp_eval() can handle this special situation.
6023 */
6024 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006025 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006026 return -1;
6027 }
6028
6029 if (elem->prev == elem) {
6030 /* unlinking last top-level element, use an empty data tree */
6031 *ctx_node = NULL;
6032 } else {
6033 /* in this case just use the previous/last top-level data node */
6034 *ctx_node = elem->prev;
6035 }
6036 } else if (elem == *node) {
6037 /* We are going to unlink the currently processed node. This does not matter that
6038 * much, but we would lose access to the original data tree, so just move our
6039 * pointer somewhere still inside it.
6040 */
6041 if ((*node)->prev != *node) {
6042 *node = (*node)->prev;
6043 } else {
6044 /* the processed node with sibings were all unlinked, oh well */
6045 *node = NULL;
6046 }
6047 }
6048
6049 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01006050 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006051 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006052 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006053 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006054 return -1;
6055 }
6056 } else {
6057 *unlinked_nodes = elem;
6058 }
6059
6060 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
6061 /* there can be only one instance */
6062 break;
6063 }
6064 }
6065 }
6066 break;
6067 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006068 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006069 return -1;
6070 }
6071
6072 return EXIT_SUCCESS;
6073}
6074
6075/**
6076 * @brief Relink the unlinked nodes back.
6077 *
6078 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
6079 * we simply need a sibling from the original data tree.
6080 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
6081 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
6082 * or the sibling of \p unlinked_nodes.
6083 *
6084 * @return EXIT_SUCCESS on success, -1 on error.
6085 */
6086static int
6087resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
6088{
6089 struct lyd_node *elem;
6090
6091 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006092 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006093 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006094 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006095 return -1;
6096 }
6097 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006098 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006099 return -1;
6100 }
6101 }
6102 }
6103
6104 return EXIT_SUCCESS;
6105}
6106
Radek Krejci03b71f72016-03-16 11:10:09 +01006107int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006108resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01006109{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006110 int ret = 0;
6111 uint8_t must_size;
6112 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02006113
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006114 assert(node);
6115
6116 schema = node->schema;
6117
6118 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02006119 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01006120 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006121 must_size = ((struct lys_node_container *)schema)->must_size;
6122 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006123 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006124 must_size = ((struct lys_node_leaf *)schema)->must_size;
6125 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006126 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006127 must_size = ((struct lys_node_leaflist *)schema)->must_size;
6128 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006129 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006130 must_size = ((struct lys_node_list *)schema)->must_size;
6131 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006132 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02006133 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006134 must_size = ((struct lys_node_anydata *)schema)->must_size;
6135 break;
6136 case LYS_NOTIF:
6137 must_size = ((struct lys_node_notif *)schema)->must_size;
6138 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006139 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006140 must_size = 0;
6141 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006142 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006143
6144 if (must_size) {
6145 ++ret;
6146 }
6147
6148 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6149 if (!node->prev->next) {
6150 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6151 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6152 ret += 0x2;
6153 }
6154 }
6155
6156 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006157}
6158
6159int
Radek Krejci46165822016-08-26 14:06:27 +02006160resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006161{
Radek Krejci46165822016-08-26 14:06:27 +02006162 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006163
Radek Krejci46165822016-08-26 14:06:27 +02006164 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006165
Radek Krejci46165822016-08-26 14:06:27 +02006166 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006167 return 1;
6168 }
6169
Radek Krejci46165822016-08-26 14:06:27 +02006170 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006171 goto check_augment;
6172
Radek Krejci46165822016-08-26 14:06:27 +02006173 while (parent) {
6174 /* stop conditions */
6175 if (!mode) {
6176 /* stop on node that can be instantiated in data tree */
6177 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6178 break;
6179 }
6180 } else {
6181 /* stop on the specified node */
6182 if (parent == stop) {
6183 break;
6184 }
6185 }
6186
6187 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006188 return 1;
6189 }
6190check_augment:
6191
6192 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02006193 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006194 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006195 }
6196 parent = lys_parent(parent);
6197 }
6198
6199 return 0;
6200}
6201
Michal Vaskocf024702015-10-08 15:01:42 +02006202/**
6203 * @brief Resolve (check) all when conditions relevant for \p node.
6204 * Logs directly.
6205 *
6206 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006207 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6208 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006209 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006210 * @return
6211 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006212 * 0 - all "when" statements true
6213 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006214 * 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 +02006215 */
Radek Krejci46165822016-08-26 14:06:27 +02006216int
Michal Vasko0b963112017-08-11 12:45:36 +02006217resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006218{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006219 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006220 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006221 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006222 enum lyxp_node_type ctx_node_type;
Michal Vasko53b7da02018-02-13 15:28:42 +01006223 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci51093642016-03-29 10:14:59 +02006224 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006225
6226 assert(node);
6227 memset(&set, 0, sizeof set);
6228
Michal Vasko78d97e22017-02-21 09:54:38 +01006229 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006230 /* make the node dummy for the evaluation */
6231 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006232 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
6233 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006234 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006235 if (rc) {
6236 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006237 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006238 }
Radek Krejci51093642016-03-29 10:14:59 +02006239 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006240 }
6241
Radek Krejci03b71f72016-03-16 11:10:09 +01006242 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006243 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006244 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006245 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006246 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006247 || ((((struct lys_node_container *)node->schema)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6248 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006249 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6250 ((struct lys_node_container *)node->schema)->when->cond);
6251 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006252 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006253 if (failed_when) {
6254 *failed_when = ((struct lys_node_container *)node->schema)->when;
6255 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006256 goto cleanup;
6257 }
Michal Vaskocf024702015-10-08 15:01:42 +02006258 }
Radek Krejci51093642016-03-29 10:14:59 +02006259
6260 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006261 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006262 }
6263
Michal Vasko90fc2a32016-08-24 15:58:58 +02006264 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006265 goto check_augment;
6266
6267 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006268 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6269 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006270 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006271 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006272 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006273 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006274 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006275 }
6276 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006277
6278 unlinked_nodes = NULL;
6279 /* we do not want our node pointer to change */
6280 tmp_node = node;
6281 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6282 if (rc) {
6283 goto cleanup;
6284 }
6285
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006286 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6287 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006288
6289 if (unlinked_nodes && ctx_node) {
6290 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6291 rc = -1;
6292 goto cleanup;
6293 }
6294 }
6295
Radek Krejci03b71f72016-03-16 11:10:09 +01006296 if (rc) {
6297 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006298 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006299 }
Radek Krejci51093642016-03-29 10:14:59 +02006300 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006301 }
6302
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006303 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006304 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02006305 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006306 || ((((struct lys_node_uses *)sparent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6307 || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006308 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6309 ((struct lys_node_uses *)sparent)->when->cond);
6310 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006311 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko53b7da02018-02-13 15:28:42 +01006312 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006313 if (failed_when) {
6314 *failed_when = ((struct lys_node_uses *)sparent)->when;
6315 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006316 goto cleanup;
6317 }
Michal Vaskocf024702015-10-08 15:01:42 +02006318 }
Radek Krejci51093642016-03-29 10:14:59 +02006319
6320 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006321 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006322 }
6323
6324check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006325 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006326 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006327 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006328 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006329 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006330 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006331 }
6332 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006333
6334 unlinked_nodes = NULL;
6335 tmp_node = node;
6336 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6337 if (rc) {
6338 goto cleanup;
6339 }
6340
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006341 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6342 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006343
6344 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6345 * so the tree did not actually change and there is nothing for us to do
6346 */
6347 if (unlinked_nodes && ctx_node) {
6348 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6349 rc = -1;
6350 goto cleanup;
6351 }
6352 }
6353
Radek Krejci03b71f72016-03-16 11:10:09 +01006354 if (rc) {
6355 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006356 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006357 }
Radek Krejci51093642016-03-29 10:14:59 +02006358 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006359 }
6360
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006361 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006362 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006363 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006364 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006365 || ((((struct lys_node_augment *)sparent->parent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6366 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006367 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006368 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006369 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006370 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006371 if (failed_when) {
6372 *failed_when = ((struct lys_node_augment *)sparent->parent)->when;
6373 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006374 goto cleanup;
6375 }
Michal Vaskocf024702015-10-08 15:01:42 +02006376 }
Radek Krejci51093642016-03-29 10:14:59 +02006377
6378 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006379 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006380 }
6381
Michal Vasko90fc2a32016-08-24 15:58:58 +02006382 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006383 }
6384
Radek Krejci0b7704f2016-03-18 12:16:14 +01006385 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006386
Radek Krejci51093642016-03-29 10:14:59 +02006387cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006388 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006389 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006390 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006391}
6392
Radek Krejcicbb473e2016-09-16 14:48:32 +02006393static int
6394check_leafref_features(struct lys_type *type)
6395{
6396 struct lys_node *iter;
6397 struct ly_set *src_parents, *trg_parents, *features;
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006398 struct lys_node_augment *aug;
Michal Vasko53b7da02018-02-13 15:28:42 +01006399 struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006400 unsigned int i, j, size, x;
6401 int ret = EXIT_SUCCESS;
6402
6403 assert(type->parent);
6404
6405 src_parents = ly_set_new();
6406 trg_parents = ly_set_new();
6407 features = ly_set_new();
6408
6409 /* get parents chain of source (leafref) */
Radek Krejciecda01a2017-04-05 15:44:27 +02006410 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006411 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6412 continue;
6413 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006414 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006415 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006416 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006417 /* unresolved augment, wait until it's resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01006418 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
Michal Vaskobd026102017-11-21 13:43:01 +01006419 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006420 ret = EXIT_FAILURE;
6421 goto cleanup;
6422 }
6423 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006424 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
6425 }
6426 /* get parents chain of target */
Radek Krejciecda01a2017-04-05 15:44:27 +02006427 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006428 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6429 continue;
6430 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006431 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006432 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006433 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006434 /* unresolved augment, wait until it's resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01006435 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
Michal Vaskobd026102017-11-21 13:43:01 +01006436 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006437 ret = EXIT_FAILURE;
6438 goto cleanup;
6439 }
6440 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006441 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
6442 }
6443
6444 /* compare the features used in if-feature statements in the rest of both
6445 * chains of parents. The set of features used for target must be a subset
6446 * of features used for the leafref. This is not a perfect, we should compare
6447 * the truth tables but it could require too much resources, so we simplify that */
6448 for (i = 0; i < src_parents->number; i++) {
6449 iter = src_parents->set.s[i]; /* shortcut */
6450 if (!iter->iffeature_size) {
6451 continue;
6452 }
6453 for (j = 0; j < iter->iffeature_size; j++) {
6454 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6455 for (; size; size--) {
6456 if (!iter->iffeature[j].features[size - 1]) {
6457 /* not yet resolved feature, postpone this check */
6458 ret = EXIT_FAILURE;
6459 goto cleanup;
6460 }
6461 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
6462 }
6463 }
6464 }
6465 x = features->number;
6466 for (i = 0; i < trg_parents->number; i++) {
6467 iter = trg_parents->set.s[i]; /* shortcut */
6468 if (!iter->iffeature_size) {
6469 continue;
6470 }
6471 for (j = 0; j < iter->iffeature_size; j++) {
6472 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6473 for (; size; size--) {
6474 if (!iter->iffeature[j].features[size - 1]) {
6475 /* not yet resolved feature, postpone this check */
6476 ret = EXIT_FAILURE;
6477 goto cleanup;
6478 }
Michal Vasko53b7da02018-02-13 15:28:42 +01006479 if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006480 /* the feature is not present in features set of target's parents chain */
Michal Vasko53b7da02018-02-13 15:28:42 +01006481 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
6482 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcicbb473e2016-09-16 14:48:32 +02006483 "Leafref is not conditional based on \"%s\" feature as its target.",
6484 iter->iffeature[j].features[size - 1]->name);
6485 ret = -1;
6486 goto cleanup;
6487 }
6488 }
6489 }
6490 }
6491
6492cleanup:
6493 ly_set_free(features);
6494 ly_set_free(src_parents);
6495 ly_set_free(trg_parents);
6496
6497 return ret;
6498}
6499
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006500static int
6501check_type_union_leafref(struct lys_type *type)
6502{
6503 uint8_t i;
6504
6505 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6506 /* go through unions and look for leafref */
6507 for (i = 0; i < type->info.uni.count; ++i) {
6508 switch (type->info.uni.types[i].base) {
6509 case LY_TYPE_LEAFREF:
6510 return 1;
6511 case LY_TYPE_UNION:
6512 if (check_type_union_leafref(&type->info.uni.types[i])) {
6513 return 1;
6514 }
6515 break;
6516 default:
6517 break;
6518 }
6519 }
6520
6521 return 0;
6522 }
6523
6524 /* just inherit the flag value */
6525 return type->der->has_union_leafref;
6526}
6527
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006528/**
Michal Vaskobb211122015-08-19 14:03:11 +02006529 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006530 *
6531 * @param[in] mod Main module.
6532 * @param[in] item Item to resolve. Type determined by \p type.
6533 * @param[in] type Type of the unresolved item.
6534 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006535 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006536 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006537 *
6538 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6539 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006540static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006541resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006542 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006543{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006544 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vasko53b7da02018-02-13 15:28:42 +01006545 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006546 unsigned int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01006547 struct ly_ctx * ctx = mod->ctx;
Radek Krejci80056d52017-01-05 13:13:33 +01006548 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006549 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006550 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006551
Radek Krejcic79c6b12016-07-26 15:11:49 +02006552 struct ly_set *refs, *procs;
6553 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006554 struct lys_ident *ident;
6555 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006556 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006557 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006558 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006559 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006560 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006561 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006562 struct lys_ext_instance *ext, **extlist;
6563 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006564
6565 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006566 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006567 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006568 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006569 ident = item;
6570
Radek Krejci018f1f52016-08-03 16:01:20 +02006571 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006572 break;
6573 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006574 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006575 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006576 stype = item;
6577
Radek Krejci018f1f52016-08-03 16:01:20 +02006578 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006579 break;
6580 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006581 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006582 stype = item;
6583
Michal Vasko1c007172017-03-10 10:20:44 +01006584 rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target);
6585 if (!rc) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02006586 assert(stype->info.lref.target);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006587
Michal Vaskobb520442017-05-23 10:55:18 +02006588 if (lys_node_module(node)->implemented) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006589 /* make all the modules in the path implemented */
Michal Vaskobb520442017-05-23 10:55:18 +02006590 for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) {
6591 if (!lys_node_module(next)->implemented) {
6592 if (lys_set_implemented(lys_node_module(next))) {
6593 rc = -1;
6594 break;
6595 }
6596 }
6597 }
6598 if (next) {
6599 break;
6600 }
6601
6602 /* store the backlink from leafref target */
6603 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
6604 rc = -1;
Michal Vaskobe136f62017-09-21 12:08:39 +02006605 break;
6606 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006607 }
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006608
6609 /* check if leafref and its target are under common if-features */
6610 rc = check_leafref_features(stype);
Radek Krejci46c4cd72016-01-21 15:13:52 +01006611 }
6612
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006613 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006614 case UNRES_TYPE_DER_EXT:
6615 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006616 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006617 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006618 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006619 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006620 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006621 /* parent */
6622 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006623 stype = item;
6624
Michal Vasko88c29542015-11-27 14:57:53 +01006625 /* HACK type->der is temporarily unparsed type statement */
6626 yin = (struct lyxml_elem *)stype->der;
6627 stype->der = NULL;
6628
Pavol Vicana0e4e672016-02-24 12:20:04 +01006629 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6630 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006631 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006632
6633 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006634 /* may try again later */
6635 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006636 } else {
6637 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006638 lydict_remove(ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006639 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006640 }
6641
Michal Vasko88c29542015-11-27 14:57:53 +01006642 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006643 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006644 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006645 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006646 lyxml_free(ctx, yin);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006647 } else {
6648 /* may try again later, put all back how it was */
6649 stype->der = (struct lys_tpdf *)yin;
6650 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006651 }
Radek Krejcic13db382016-08-16 10:52:42 +02006652 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006653 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006654 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006655 LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
Radek Krejci2c2fce82016-08-01 13:52:26 +02006656 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006657
6658 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6659 /* fill typedef union leafref flag */
6660 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6661 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6662 /* copy the type in case it has union leafref flag */
6663 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006664 LOGERR(ctx, LY_EINT, "Failed to duplicate type.");
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006665 return -1;
6666 }
6667 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02006668 } else if (rc == EXIT_FAILURE && !(stype->flags & LYTYPE_GRP)) {
Radek Krejcic13db382016-08-16 10:52:42 +02006669 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6670 * 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 +02006671 * grouping. The grouping cannot be used unless the unres counter is 0.
Michal Vasko70bf8e52018-03-26 11:32:33 +02006672 * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006673 * of the type's base member. */
6674 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6675 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006676 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006677 LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006678 return -1;
6679 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02006680 stype->flags |= LYTYPE_GRP;
Radek Krejcic13db382016-08-16 10:52:42 +02006681 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006682 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006683 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006684 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006685 iff_data = str_snode;
6686 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006687 if (!rc) {
6688 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006689 if (iff_data->infeature) {
6690 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6691 feat = *((struct lys_feature **)item);
6692 if (!feat->depfeatures) {
6693 feat->depfeatures = ly_set_new();
6694 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006695 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006696 }
6697 /* cleanup temporary data */
Michal Vasko53b7da02018-02-13 15:28:42 +01006698 lydict_remove(ctx, iff_data->fname);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006699 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006700 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006701 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006702 case UNRES_FEATURE:
6703 feat = (struct lys_feature *)item;
6704
6705 if (feat->iffeature_size) {
6706 refs = ly_set_new();
6707 procs = ly_set_new();
6708 ly_set_add(procs, feat, 0);
6709
6710 while (procs->number) {
6711 ref = procs->set.g[procs->number - 1];
6712 ly_set_rm_index(procs, procs->number - 1);
6713
6714 for (i = 0; i < ref->iffeature_size; i++) {
6715 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6716 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006717 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006718 if (ref->iffeature[i].features[j - 1] == feat) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006719 LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006720 goto featurecheckdone;
6721 }
6722
6723 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6724 k = refs->number;
6725 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6726 /* not yet seen feature, add it for processing */
6727 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6728 }
6729 }
6730 } else {
6731 /* forward reference */
6732 rc = EXIT_FAILURE;
6733 goto featurecheckdone;
6734 }
6735 }
6736
6737 }
6738 }
6739 rc = EXIT_SUCCESS;
6740
6741featurecheckdone:
6742 ly_set_free(refs);
6743 ly_set_free(procs);
6744 }
6745
6746 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006747 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006748 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006749 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006750 case UNRES_TYPEDEF_DFLT:
6751 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006752 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006753 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006754 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006755 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006756 break;
6757 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006758 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006759 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006760 choic = item;
6761
Radek Krejcie00d2312016-08-12 15:27:49 +02006762 if (!choic->dflt) {
6763 choic->dflt = resolve_choice_dflt(choic, expr);
6764 }
Michal Vasko7955b362015-09-04 14:18:15 +02006765 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006766 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006767 } else {
6768 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006769 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006770 break;
6771 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006772 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006773 break;
6774 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006775 unique_info = (struct unres_list_uniq *)item;
6776 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006777 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006778 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006779 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006780 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006781 case UNRES_XPATH:
6782 node = (struct lys_node *)item;
Michal Vasko895c11f2018-03-12 11:35:58 +01006783 rc = check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006784 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006785 case UNRES_EXT:
6786 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006787 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006788 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006789 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006790 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006791 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006792 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006793 if (eplugin) {
6794 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006795 u = malloc(sizeof *u);
Michal Vasko53b7da02018-02-13 15:28:42 +01006796 LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006797 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006798 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6799 /* something really bad happend since the extension finalization is not actually
6800 * being resolved while adding into unres, so something more serious with the unres
6801 * list itself must happened */
6802 return -1;
6803 }
Radek Krejci80056d52017-01-05 13:13:33 +01006804 }
6805 }
Radek Krejci79685c92017-02-17 10:49:43 +01006806 }
6807 if (!rc || rc == -1) {
6808 /* cleanup on success or fatal error */
6809 if (ext_data->datatype == LYS_IN_YIN) {
6810 /* YIN */
Michal Vasko53b7da02018-02-13 15:28:42 +01006811 lyxml_free(ctx, ext_data->data.yin);
Radek Krejci79685c92017-02-17 10:49:43 +01006812 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01006813 /* YANG */
6814 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01006815 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006816 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006817 }
6818 break;
Radek Krejci80056d52017-01-05 13:13:33 +01006819 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01006820 u = (uint8_t *)str_snode;
6821 ext = (*(struct lys_ext_instance ***)item)[*u];
6822 free(u);
6823
Radek Krejci80056d52017-01-05 13:13:33 +01006824 eplugin = ext->def->plugin;
6825
6826 /* inherit */
6827 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
6828 root = (struct lys_node *)ext->parent;
6829 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6830 LY_TREE_DFS_BEGIN(root->child, next, node) {
6831 /* first, check if the node already contain instance of the same extension,
6832 * in such a case we won't inherit. In case the node was actually defined as
6833 * augment data, we are supposed to check the same way also the augment node itself */
6834 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
6835 goto inherit_dfs_sibling;
6836 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
6837 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
6838 goto inherit_dfs_sibling;
6839 }
6840
6841 if (eplugin->check_inherit) {
6842 /* we have a callback to check the inheritance, use it */
6843 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
6844 case 0:
6845 /* yes - continue with the inheriting code */
6846 break;
6847 case 1:
6848 /* no - continue with the node's sibling */
6849 goto inherit_dfs_sibling;
6850 case 2:
6851 /* no, but continue with the children, just skip the inheriting code for this node */
6852 goto inherit_dfs_child;
6853 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006854 LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
Radek Krejci80056d52017-01-05 13:13:33 +01006855 ext->def->module->name, ext->def->name, rc);
6856 }
6857 }
6858
6859 /* inherit the extension */
6860 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01006861 LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006862 extlist[node->ext_size] = malloc(sizeof **extlist);
Michal Vasko53b7da02018-02-13 15:28:42 +01006863 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006864 memcpy(extlist[node->ext_size], ext, sizeof *ext);
6865 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
6866
6867 node->ext = extlist;
6868 node->ext_size++;
6869
6870inherit_dfs_child:
6871 /* modification of - select element for the next run - children first */
6872 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
6873 next = NULL;
6874 } else {
6875 next = node->child;
6876 }
6877 if (!next) {
6878inherit_dfs_sibling:
6879 /* no children, try siblings */
6880 next = node->next;
6881 }
6882 while (!next) {
6883 /* go to the parent */
6884 node = lys_parent(node);
6885
6886 /* we are done if we are back in the root (the starter's parent */
6887 if (node == root) {
6888 break;
6889 }
6890
6891 /* parent is already processed, go to its sibling */
6892 next = node->next;
6893 }
6894 }
6895 }
6896 }
6897
6898 /* final check */
6899 if (eplugin->check_result) {
6900 if ((*eplugin->check_result)(ext)) {
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01006901 LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed.");
Radek Krejci80056d52017-01-05 13:13:33 +01006902 return -1;
6903 }
6904 }
6905
6906 rc = 0;
6907 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006908 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006909 LOGINT(ctx);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006910 break;
6911 }
6912
Radek Krejci54081ce2016-08-12 15:21:47 +02006913 if (has_str && !rc) {
6914 /* the string is no more needed in case of success.
6915 * In case of forward reference, we will try to resolve the string later */
Michal Vasko53b7da02018-02-13 15:28:42 +01006916 lydict_remove(ctx, str_snode);
Radek Krejci4f78b532016-02-17 13:43:00 +01006917 }
6918
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006919 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006920}
6921
Michal Vaskof02e3742015-08-05 16:27:02 +02006922/* logs directly */
6923static void
Radek Krejci48464ed2016-03-17 15:44:09 +01006924print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006925{
Michal Vaskocb34dc62016-05-20 14:38:37 +02006926 struct lyxml_elem *xml;
6927 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006928 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006929 const char *name = NULL;
6930 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006931
Michal Vaskof02e3742015-08-05 16:27:02 +02006932 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02006933 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006934 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006935 break;
6936 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006937 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006938 break;
6939 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006940 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
6941 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02006942 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006943 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02006944 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02006945 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02006946 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
6947 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01006948 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006949 } else {
6950 LY_TREE_FOR(xml->attr, attr) {
6951 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006952 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006953 break;
6954 }
6955 }
6956 assert(attr);
6957 }
Radek Krejcie534c132016-11-23 13:32:31 +01006958 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006959 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006960 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006961 iff_data = str_node;
6962 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02006963 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006964 case UNRES_FEATURE:
6965 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
6966 ((struct lys_feature *)item)->name);
6967 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006968 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006969 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006970 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006971 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02006972 case UNRES_TYPE_DFLT:
Michal Vaskoba835cd2018-02-23 09:25:48 +01006973 if (*(char **)str_node) {
6974 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node);
Radek Krejci2e2de832016-10-13 16:12:26 +02006975 } /* else no default value in the type itself, but we are checking some restrictions against
6976 * possible default value of some base type. The failure is caused by not resolved base type,
6977 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02006978 break;
6979 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006980 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006981 break;
6982 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01006983 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006984 break;
6985 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01006986 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006987 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006988 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006989 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
6990 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01006991 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006992 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01006993 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
6994 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02006995 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006996 case UNRES_EXT:
6997 extinfo = (struct unres_ext *)str_node;
6998 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
6999 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
7000 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007001 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007002 LOGINT(NULL);
Michal Vaskof02e3742015-08-05 16:27:02 +02007003 break;
7004 }
7005}
7006
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007007/**
Michal Vaskobb211122015-08-19 14:03:11 +02007008 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007009 *
7010 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007011 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007012 *
Michal Vasko92b8a382015-08-19 14:03:49 +02007013 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007014 */
Michal Vaskof02e3742015-08-05 16:27:02 +02007015int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007016resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02007017{
Radek Krejci010e54b2016-03-15 09:40:34 +01007018 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko53b7da02018-02-13 15:28:42 +01007019 struct ly_err_item *prev_eitem;
7020 enum int_log_opts prev_ilo;
7021 LY_ERR prev_ly_errno;
7022 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007023
7024 assert(unres);
7025
Michal Vaskoe8734262016-09-29 14:12:06 +02007026 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko53b7da02018-02-13 15:28:42 +01007027 prev_ly_errno = ly_errno;
7028 ly_ilo_change(mod->ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vasko51054ca2015-08-12 12:20:00 +02007029
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007030 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02007031 do {
Michal Vasko88c29542015-11-27 14:57:53 +01007032 unres_count = 0;
7033 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02007034
7035 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02007036 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02007037 * if-features are resolved here to make sure that we will have all if-features for
7038 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02007039 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02007040 continue;
7041 }
Radek Krejci018f1f52016-08-03 16:01:20 +02007042 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejci818b0c52016-11-09 15:10:51 +01007043 * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02007044
Michal Vasko88c29542015-11-27 14:57:53 +01007045 ++unres_count;
Michal Vaskof96dfb62017-08-17 12:23:49 +02007046 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007047 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02007048 unres->type[i] = UNRES_RESOLVED;
7049 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01007050 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02007051 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007052 goto error;
Radek Krejcic2a180f2016-06-22 13:28:16 +02007053 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007054 /* forward reference, erase errors */
7055 ly_err_free_next(mod->ctx, prev_eitem);
Michal Vasko51054ca2015-08-12 12:20:00 +02007056 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007057 }
Michal Vasko88c29542015-11-27 14:57:53 +01007058 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02007059
Michal Vasko88c29542015-11-27 14:57:53 +01007060 if (res_count < unres_count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007061 /* just print the errors (but we must free the ones we have and get them again :-/ ) */
7062 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02007063
7064 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02007065 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02007066 continue;
7067 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02007068 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko22af5ca2016-05-20 11:44:02 +02007069 }
Michal Vasko92b8a382015-08-19 14:03:49 +02007070 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007071 }
7072
Michal Vaskof96dfb62017-08-17 12:23:49 +02007073 /* the rest except finalizing extensions and xpath */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007074 for (i = 0; i < unres->count; ++i) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02007075 if ((unres->type[i] == UNRES_RESOLVED) || (unres->type[i] == UNRES_EXT_FINALIZE) || (unres->type[i] == UNRES_XPATH)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007076 continue;
7077 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02007078
Michal Vaskof96dfb62017-08-17 12:23:49 +02007079 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01007080 if (rc == 0) {
Pavol Vican88e16c92016-09-07 15:41:50 +02007081 if (unres->type[i] == UNRES_LIST_UNIQ) {
7082 /* free the allocated structure */
7083 free(unres->item[i]);
7084 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007085 unres->type[i] = UNRES_RESOLVED;
7086 ++resolved;
7087 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007088 goto error;
Radek Krejci791f6c72017-02-22 15:23:39 +01007089 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007090 /* forward reference, erase errors */
7091 ly_err_free_next(mod->ctx, prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007092 }
7093 }
7094
Michal Vasko53b7da02018-02-13 15:28:42 +01007095 /* log normally now */
7096 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 0);
7097 ly_errno = prev_ly_errno;
Radek Krejci010e54b2016-03-15 09:40:34 +01007098
Radek Krejci80056d52017-01-05 13:13:33 +01007099 /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
7100 for (i = 0; i < unres->count; ++i) {
7101 if (unres->type[i] != UNRES_EXT_FINALIZE) {
7102 continue;
7103 }
7104
Michal Vaskof96dfb62017-08-17 12:23:49 +02007105 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci791f6c72017-02-22 15:23:39 +01007106 unres->type[i] = UNRES_RESOLVED;
Radek Krejci80056d52017-01-05 13:13:33 +01007107 if (rc == 0) {
Radek Krejci80056d52017-01-05 13:13:33 +01007108 ++resolved;
7109 }
Radek Krejci791f6c72017-02-22 15:23:39 +01007110 /* else error - it was already printed, but resolved was not increased,
7111 so this unres item will not be resolved again in the following code,
7112 but it will cause returning -1 at the end, this way we are able to
7113 print all the issues with unres */
Radek Krejci80056d52017-01-05 13:13:33 +01007114 }
7115
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007116 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007117 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
Michal Vaskof96dfb62017-08-17 12:23:49 +02007118 * all the validation errors, xpath is resolved only here to properly print all the messages
Radek Krejci010e54b2016-03-15 09:40:34 +01007119 */
7120 for (i = 0; i < unres->count; ++i) {
7121 if (unres->type[i] == UNRES_RESOLVED) {
7122 continue;
7123 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02007124 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejcib3142312016-11-09 11:04:12 +01007125 if (unres->type[i] == UNRES_XPATH) {
Michal Vasko769f8032017-01-24 13:11:55 +01007126 /* XPath referencing an unknown node is actually supposed to be just a warning */
Radek Krejcib3142312016-11-09 11:04:12 +01007127 unres->type[i] = UNRES_RESOLVED;
7128 resolved++;
Radek Krejcib3142312016-11-09 11:04:12 +01007129 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007130 }
Radek Krejcib3142312016-11-09 11:04:12 +01007131 if (resolved < unres->count) {
7132 return -1;
7133 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007134 }
7135
Michal Vaskoe8734262016-09-29 14:12:06 +02007136 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01007137 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007138 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01007139
7140error:
7141 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 1);
7142 /* ly_errno will be set accordingly, we do not want to keep the previous value */
7143 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007144}
7145
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007146/**
Michal Vaskobb211122015-08-19 14:03:11 +02007147 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007148 *
7149 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007150 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007151 * @param[in] item Item to resolve. Type determined by \p type.
7152 * @param[in] type Type of the unresolved item.
7153 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007154 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007155 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007156 */
7157int
Radek Krejci48464ed2016-03-17 15:44:09 +01007158unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7159 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007160{
Radek Krejci54081ce2016-08-12 15:21:47 +02007161 int rc;
7162 const char *dictstr;
7163
7164 dictstr = lydict_insert(mod->ctx, str, 0);
7165 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7166
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007167 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007168 lydict_remove(mod->ctx, dictstr);
7169 }
7170 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007171}
7172
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007173/**
Michal Vaskobb211122015-08-19 14:03:11 +02007174 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007175 *
7176 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007177 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007178 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007179 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007180 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007181 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007182 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007183 */
7184int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007185unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007186 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007187{
Michal Vasko53b7da02018-02-13 15:28:42 +01007188 int rc;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007189 uint32_t u;
Michal Vasko53b7da02018-02-13 15:28:42 +01007190 enum int_log_opts prev_ilo;
7191 struct ly_err_item *prev_eitem;
7192 LY_ERR prev_ly_errno;
Michal Vasko88c29542015-11-27 14:57:53 +01007193 struct lyxml_elem *yin;
Michal Vasko53b7da02018-02-13 15:28:42 +01007194 struct ly_ctx *ctx = mod->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007195
Michal Vasko9bf425b2015-10-22 11:42:03 +02007196 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
7197 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007198
Radek Krejci850a5de2016-11-08 14:06:40 +01007199 /* check for duplicities in unres */
7200 for (u = 0; u < unres->count; u++) {
7201 if (unres->type[u] == type && unres->item[u] == item &&
7202 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007203 /* duplication can happen when the node contains multiple statements of the same type to check,
7204 * this can happen for example when refinement is being applied, so we just postpone the processing
7205 * and do not duplicate the information */
7206 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007207 }
7208 }
7209
Michal Vaskof96dfb62017-08-17 12:23:49 +02007210 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH)) {
7211 /* extension finalization is not even tried when adding the item into the inres list,
7212 * xpath is not tried because it would hide some potential warnings */
Radek Krejcic293bac2017-02-27 11:25:28 +01007213 rc = EXIT_FAILURE;
7214 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007215 prev_ly_errno = ly_errno;
7216 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007217
Michal Vasko53b7da02018-02-13 15:28:42 +01007218 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007219 if (rc != EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007220 ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0);
7221 if (rc != -1) {
7222 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007223 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007224
Radek Krejci80056d52017-01-05 13:13:33 +01007225 if (type == UNRES_LIST_UNIQ) {
7226 /* free the allocated structure */
7227 free(item);
7228 } else if (rc == -1 && type == UNRES_IFFEAT) {
7229 /* free the allocated resources */
7230 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007231 }
Radek Krejci80056d52017-01-05 13:13:33 +01007232 return rc;
7233 } else {
7234 /* erase info about validation errors */
Michal Vasko53b7da02018-02-13 15:28:42 +01007235 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7236 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007237 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007238
Radek Krejci80056d52017-01-05 13:13:33 +01007239 print_unres_schema_item_fail(item, type, snode);
7240
7241 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7242 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7243 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7244 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7245 lyxml_unlink_elem(mod->ctx, yin, 1);
7246 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7247 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007248 }
Michal Vasko88c29542015-11-27 14:57:53 +01007249 }
7250
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007251 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007252 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Michal Vasko53b7da02018-02-13 15:28:42 +01007253 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007254 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007255 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007256 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007257 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007258 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Michal Vasko53b7da02018-02-13 15:28:42 +01007259 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007260 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007261 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01007262 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007263 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007264
Michal Vasko3767fb22016-07-21 12:10:57 +02007265 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007266}
7267
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007268/**
Michal Vaskobb211122015-08-19 14:03:11 +02007269 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007270 *
7271 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007272 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007273 * @param[in] item Old item to be resolved.
7274 * @param[in] type Type of the old unresolved item.
7275 * @param[in] new_item New item to use in the duplicate.
7276 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007277 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007278 */
Michal Vaskodad19402015-08-06 09:51:53 +02007279int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007280unres_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 +02007281{
7282 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007283 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007284 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007285
Michal Vaskocf024702015-10-08 15:01:42 +02007286 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007287
Radek Krejcid09d1a52016-08-11 14:05:45 +02007288 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7289 if (type == UNRES_LIST_UNIQ) {
7290 aux_uniq.list = item;
7291 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7292 item = &aux_uniq;
7293 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007294 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007295
7296 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007297 if (type == UNRES_LIST_UNIQ) {
7298 free(new_item);
7299 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007300 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007301 }
7302
Radek Krejcic79c6b12016-07-26 15:11:49 +02007303 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007304 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007305 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007306 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007307 return -1;
7308 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007309 } else if (type == UNRES_IFFEAT) {
7310 /* duplicate unres_iffeature_data */
7311 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01007312 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007313 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7314 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7315 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007316 LOGINT(mod->ctx);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007317 return -1;
7318 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007319 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007320 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007321 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007322 return -1;
7323 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007324 }
Michal Vaskodad19402015-08-06 09:51:53 +02007325
7326 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007327}
7328
Michal Vaskof02e3742015-08-05 16:27:02 +02007329/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007330int
Michal Vasko878e38d2016-09-05 12:17:53 +02007331unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007332{
Michal Vasko878e38d2016-09-05 12:17:53 +02007333 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007334 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007335
Radek Krejciddddd0d2017-01-20 15:20:46 +01007336 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007337 i = start_on_backwards;
7338 } else {
7339 i = unres->count - 1;
7340 }
7341 for (; i > -1; i--) {
7342 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007343 continue;
7344 }
7345 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007346 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007347 break;
7348 }
7349 } else {
7350 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7351 aux_uniq2 = (struct unres_list_uniq *)item;
7352 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007353 break;
7354 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007355 }
7356 }
7357
Michal Vasko878e38d2016-09-05 12:17:53 +02007358 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007359}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007360
Michal Vaskoede9c472016-06-07 09:38:15 +02007361static void
7362unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7363{
7364 struct lyxml_elem *yin;
7365 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007366 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007367
7368 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007369 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007370 case UNRES_TYPE_DER:
7371 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7372 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7373 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007374 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007375 lydict_remove(ctx, yang->name);
7376 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007377 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7378 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7379 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007380 } else {
7381 lyxml_free(ctx, yin);
7382 }
7383 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007384 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007385 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7386 lydict_remove(ctx, iff_data->fname);
7387 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007388 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007389 case UNRES_IDENT:
7390 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007391 case UNRES_CHOICE_DFLT:
7392 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007393 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7394 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007395 case UNRES_LIST_UNIQ:
7396 free(unres->item[i]);
7397 break;
PavolVicanc1807262017-01-31 18:00:27 +01007398 case UNRES_EXT:
7399 free(unres->str_snode[i]);
7400 break;
PavolVicanfcc98762017-09-01 15:51:39 +02007401 case UNRES_EXT_FINALIZE:
7402 free(unres->str_snode[i]);
Michal Vaskoede9c472016-06-07 09:38:15 +02007403 default:
7404 break;
7405 }
7406 unres->type[i] = UNRES_RESOLVED;
7407}
7408
Michal Vasko88c29542015-11-27 14:57:53 +01007409void
Michal Vasko44ab1462017-05-18 13:18:36 +02007410unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007411{
7412 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007413 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007414
Radek Krejcic071c542016-01-27 14:57:51 +01007415 if (!unres || !(*unres)) {
7416 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007417 }
7418
Michal Vasko44ab1462017-05-18 13:18:36 +02007419 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007420
7421 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007422 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007423 if ((*unres)->type[i] != UNRES_RESOLVED) {
7424 unresolved++;
7425 }
7426 continue;
7427 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007428
7429 /* free heap memory for the specific item */
7430 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007431 }
7432
Michal Vaskoede9c472016-06-07 09:38:15 +02007433 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007434 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007435 free((*unres)->item);
7436 free((*unres)->type);
7437 free((*unres)->str_snode);
7438 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007439 free((*unres));
7440 (*unres) = NULL;
7441 }
Michal Vasko88c29542015-11-27 14:57:53 +01007442}
7443
Michal Vaskoff690e72017-08-03 14:25:07 +02007444/* check whether instance-identifier points outside its data subtree (for operation it is any node
7445 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007446static int
7447check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7448{
Michal Vaskoff690e72017-08-03 14:25:07 +02007449 const struct lys_node *op_node, *first_node;
Michal Vasko53b7da02018-02-13 15:28:42 +01007450 enum int_log_opts prev_ilo;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007451 char *buf;
Michal Vasko53b7da02018-02-13 15:28:42 +01007452 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007453
Radek Krejci034cb102017-08-01 15:45:13 +02007454 if (!json_instid || !json_instid[0]) {
7455 /* no/empty value */
7456 return 0;
7457 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007458
7459 for (op_node = lys_parent(sleaf);
7460 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7461 op_node = lys_parent(op_node));
7462
7463 if (op_node && lys_parent(op_node)) {
7464 /* nested operation - any absolute path is external */
7465 return 1;
7466 }
7467
7468 /* get the first node from the instid */
7469 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7470 if (!buf) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007471 /* so that we do not have to bother with logging, say it is not external */
7472 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007473 }
7474
Michal Vaskoff690e72017-08-03 14:25:07 +02007475 /* find the first schema node, do not log */
Michal Vasko53b7da02018-02-13 15:28:42 +01007476 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoff690e72017-08-03 14:25:07 +02007477 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
Michal Vasko53b7da02018-02-13 15:28:42 +01007478 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007479
Michal Vasko53b7da02018-02-13 15:28:42 +01007480 free(buf);
Michal Vaskoff690e72017-08-03 14:25:07 +02007481 if (!first_node) {
7482 /* unknown path, say it is not external */
Michal Vaskoff690e72017-08-03 14:25:07 +02007483 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007484 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007485
7486 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7487
Michal Vaskoff690e72017-08-03 14:25:07 +02007488 if (op_node && (op_node != first_node)) {
7489 /* it is a top-level operation, so we're good if it points somewhere inside it */
7490 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007491 }
7492
7493 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7494 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007495 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007496}
7497
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007498/**
7499 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7500 *
7501 * @param[in] data Data node where the path is used
7502 * @param[in] path Instance-identifier node value.
7503 * @param[in,out] ret Resolved instance or NULL.
7504 *
7505 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7506 */
7507static int
7508resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7509{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007510 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007511 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007512 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007513 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007514 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007515 char *str;
7516 int mod_len, name_len, has_predicate;
7517 struct unres_data node_match;
7518
7519 memset(&node_match, 0, sizeof node_match);
7520 *ret = NULL;
7521
7522 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007523 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007524 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007525 if (root->prev) {
7526 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007527 }
7528
7529 /* search for the instance node */
7530 while (path[i]) {
7531 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7532 if (j <= 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007533 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007534 goto error;
7535 }
7536 i += j;
7537
Michal Vasko1b6ca962017-08-03 14:23:09 +02007538 if (model) {
7539 str = strndup(model, mod_len);
7540 if (!str) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007541 LOGMEM(ctx);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007542 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007543 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007544 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007545 if (ctx->data_clb) {
7546 if (!mod) {
7547 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7548 } else if (!mod->implemented) {
7549 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7550 }
7551 }
7552 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007553
Michal Vasko1b6ca962017-08-03 14:23:09 +02007554 if (!mod || !mod->implemented || mod->disabled) {
7555 break;
7556 }
7557 } else if (!prev_mod) {
7558 /* first iteration and we are missing module name */
Michal Vaskoaf8ec362018-03-28 09:08:09 +02007559 LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name);
7560 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node.");
Michal Vasko1b6ca962017-08-03 14:23:09 +02007561 goto error;
7562 } else {
7563 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007564 }
7565
Radek Krejci2c822ed2017-08-03 14:23:36 +02007566 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007567 /* no instance exists */
7568 break;
7569 }
7570
7571 if (has_predicate) {
7572 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007573 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007574 /* index of the current node (for lists with position predicates) */
7575 cur_idx = 1;
7576 while (j < (signed)node_match.count) {
7577 node = node_match.node[j];
7578 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7579 if (parsed < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007580 LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007581 goto error;
7582 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007583
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007584 if (!node) {
7585 /* current node does not satisfy the predicate */
7586 unres_data_del(&node_match, j);
7587 } else {
7588 ++j;
7589 }
7590 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007591 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007592
7593 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007594 } else if (node_match.count) {
7595 /* check that we are not addressing lists */
7596 for (j = 0; (unsigned)j < node_match.count; ++j) {
7597 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7598 unres_data_del(&node_match, j--);
7599 }
7600 }
7601 if (!node_match.count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007602 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007603 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007604 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007605
7606 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007607 }
7608
7609 if (!node_match.count) {
7610 /* no instance exists */
7611 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007612 LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007613 return EXIT_FAILURE;
7614 }
7615 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7616 return EXIT_SUCCESS;
7617 } else if (node_match.count > 1) {
7618 /* instance identifier must resolve to a single node */
Michal Vasko53b7da02018-02-13 15:28:42 +01007619 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007620 goto error;
7621 } else {
7622 /* we have required result, remember it and cleanup */
7623 *ret = node_match.node[0];
7624 free(node_match.node);
7625 return EXIT_SUCCESS;
7626 }
7627
7628error:
7629 /* cleanup */
7630 free(node_match.node);
7631 return -1;
7632}
7633
7634static int
7635resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007636{
Michal Vaskoca16cb32017-07-10 11:50:33 +02007637 struct ly_set *set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007638 uint32_t i;
7639
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007640 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007641
Michal Vaskoca16cb32017-07-10 11:50:33 +02007642 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vasko50576712017-07-28 12:28:33 +02007643 set = lyd_find_path((struct lyd_node *)leaf, path);
Michal Vaskoca16cb32017-07-10 11:50:33 +02007644 if (!set) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007645 return -1;
7646 }
7647
Michal Vaskoca16cb32017-07-10 11:50:33 +02007648 for (i = 0; i < set->number; ++i) {
7649 if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7650 continue;
7651 }
7652
Radek Krejci1899d6a2016-11-03 13:48:07 +01007653 /* not that the value is already in canonical form since the parsers does the conversion,
7654 * so we can simply compare just the values */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007655 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 +01007656 /* we have the match */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007657 *ret = set->set.d[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007658 break;
7659 }
7660 }
7661
Michal Vaskoca16cb32017-07-10 11:50:33 +02007662 ly_set_free(set);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007663
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007664 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007665 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007666 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007667 LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007668 return EXIT_FAILURE;
7669 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007670 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 +02007671 }
7672 }
7673
7674 return EXIT_SUCCESS;
7675}
7676
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007677/* 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 +01007678int
7679resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7680 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007681{
Michal Vasko53b7da02018-02-13 15:28:42 +01007682 struct ly_ctx *ctx = leaf->schema->module->ctx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007683 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007684 struct lyd_node *ret;
Michal Vasko53b7da02018-02-13 15:28:42 +01007685 enum int_log_opts prev_ilo;
7686 int found, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007687 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007688
7689 assert(type->base == LY_TYPE_UNION);
7690
Michal Vasko70bf8e52018-03-26 11:32:33 +02007691 if ((leaf->value_type == LY_TYPE_UNION) || ((leaf->value_type == LY_TYPE_INST) && (leaf->value_flags & LYTYPE_UNRES))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007692 /* either NULL or instid previously converted to JSON */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007693 json_val = lydict_insert(ctx, leaf->value.string, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007694 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007695
Michal Vaskofd6c6502017-01-06 12:15:41 +01007696 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007697 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 +01007698 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007699 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007700
7701 /* 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 +01007702 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007703
7704 t = NULL;
7705 found = 0;
7706 while ((t = lyp_get_next_union_type(type, t, &found))) {
7707 found = 0;
7708
7709 switch (t->base) {
7710 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007711 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7712 req_inst = -1;
7713 } else {
7714 req_inst = t->info.lref.req;
7715 }
7716
7717 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007718 if (store) {
7719 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7720 /* valid resolved */
7721 leaf->value.leafref = ret;
7722 leaf->value_type = LY_TYPE_LEAFREF;
7723 } else {
7724 /* valid unresolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01007725 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko31a2d322018-01-12 13:36:12 +01007726 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007727 return -1;
7728 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007729 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007730 }
7731 }
7732
7733 success = 1;
7734 }
7735 break;
7736 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007737 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7738 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7739 req_inst = -1;
7740 } else {
7741 req_inst = t->info.inst.req;
7742 }
7743
Michal Vaskod3a03112017-01-23 09:56:02 +01007744 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007745 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007746 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007747 /* valid resolved */
7748 leaf->value.instance = ret;
7749 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007750
Michal Vaskofd6c6502017-01-06 12:15:41 +01007751 if (json_val) {
7752 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7753 leaf->value_str = json_val;
7754 json_val = NULL;
7755 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007756 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007757 /* valid unresolved */
7758 if (json_val) {
7759 /* put the JSON val back */
7760 leaf->value.string = json_val;
7761 json_val = NULL;
7762 } else {
7763 leaf->value.instance = NULL;
7764 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02007765 leaf->value_type = LY_TYPE_INST;
7766 leaf->value_flags |= LYTYPE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007767 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007768 }
7769
7770 success = 1;
7771 }
7772 break;
7773 default:
Michal Vasko31a2d322018-01-12 13:36:12 +01007774 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007775 success = 1;
7776 }
7777 break;
7778 }
7779
7780 if (success) {
7781 break;
7782 }
7783
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007784 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007785 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007786 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007787 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007788 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007789 }
7790
7791 /* turn logging back on */
Michal Vasko53b7da02018-02-13 15:28:42 +01007792 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007793
7794 if (json_val) {
7795 if (!success) {
7796 /* put the value back for now */
7797 assert(leaf->value_type == LY_TYPE_UNION);
7798 leaf->value.string = json_val;
7799 } else {
7800 /* value was ultimately useless, but we could not have known */
7801 lydict_remove(leaf->schema->module->ctx, json_val);
7802 }
7803 }
7804
Michal Vaskofd6c6502017-01-06 12:15:41 +01007805 if (success) {
7806 if (resolved_type) {
7807 *resolved_type = t;
7808 }
7809 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007810 /* not found and it is required */
Michal Vasko53b7da02018-02-13 15:28:42 +01007811 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007812 return EXIT_FAILURE;
7813 }
7814
7815 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007816
Radek Krejci9b6aad22016-09-20 15:55:51 +02007817}
7818
Michal Vasko8bcdf292015-08-19 14:04:43 +02007819/**
7820 * @brief Resolve a single unres data item. Logs directly.
7821 *
Michal Vaskocf024702015-10-08 15:01:42 +02007822 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007823 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007824 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007825 *
7826 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7827 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007828int
Michal Vasko0b963112017-08-11 12:45:36 +02007829resolve_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 +02007830{
Michal Vasko3cfa3182017-01-17 10:00:58 +01007831 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02007832 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007833 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007834 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007835
Michal Vasko83a6c462015-10-08 16:43:53 +02007836 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02007837 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007838
Michal Vaskocf024702015-10-08 15:01:42 +02007839 switch (type) {
7840 case UNRES_LEAFREF:
7841 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007842 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007843 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7844 req_inst = -1;
7845 } else {
7846 req_inst = sleaf->type.info.lref.req;
7847 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007848 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
7849 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01007850 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007851 /* valid resolved */
Michal Vasko70bf8e52018-03-26 11:32:33 +02007852 if (leaf->value_type == LY_TYPE_BITS) {
Michal Vasko1c8567a2017-01-05 13:42:27 +01007853 free(leaf->value.bit);
7854 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007855 leaf->value.leafref = ret;
7856 leaf->value_type = LY_TYPE_LEAFREF;
Michal Vasko70bf8e52018-03-26 11:32:33 +02007857 leaf->value_flags &= ~LYTYPE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007858 } else {
7859 /* valid unresolved */
Michal Vasko70bf8e52018-03-26 11:32:33 +02007860 if (!(leaf->value_flags & LYTYPE_UNRES)) {
Michal Vasko31a2d322018-01-12 13:36:12 +01007861 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007862 return -1;
7863 }
7864 }
7865 }
7866 leaf->validity &= ~LYD_VAL_LEAFREF;
7867 } else {
7868 return rc;
7869 }
7870 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007871
Michal Vaskocf024702015-10-08 15:01:42 +02007872 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02007873 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007874 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
7875 if (ext_dep == -1) {
7876 return -1;
7877 }
7878
7879 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7880 req_inst = -1;
7881 } else {
7882 req_inst = sleaf->type.info.inst.req;
7883 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007884 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
7885 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007886 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007887 /* valid resolved */
7888 leaf->value.instance = ret;
7889 leaf->value_type = LY_TYPE_INST;
Michal Vasko70bf8e52018-03-26 11:32:33 +02007890 leaf->value_flags &= ~LYTYPE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007891 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007892 /* valid unresolved */
7893 leaf->value.instance = NULL;
Michal Vasko70bf8e52018-03-26 11:32:33 +02007894 leaf->value_type = LY_TYPE_INST;
7895 leaf->value_flags |= LYTYPE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007896 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007897 } else {
7898 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007899 }
Michal Vaskocf024702015-10-08 15:01:42 +02007900 break;
7901
Radek Krejci7de36cf2016-09-12 16:18:50 +02007902 case UNRES_UNION:
7903 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007904 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007905
Michal Vaskocf024702015-10-08 15:01:42 +02007906 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02007907 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02007908 return rc;
7909 }
7910 break;
7911
Michal Vaskobf19d252015-10-08 15:39:17 +02007912 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007913 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02007914 return rc;
7915 }
7916 break;
7917
7918 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007919 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02007920 return rc;
7921 }
7922 break;
7923
Michal Vaskocf024702015-10-08 15:01:42 +02007924 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007925 LOGINT(NULL);
Michal Vasko8bcdf292015-08-19 14:04:43 +02007926 return -1;
7927 }
7928
7929 return EXIT_SUCCESS;
7930}
7931
7932/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01007933 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02007934 *
7935 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02007936 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007937 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01007938 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007939 */
7940int
Radek Krejci0b7704f2016-03-18 12:16:14 +01007941unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007942{
Radek Krejci03b71f72016-03-16 11:10:09 +01007943 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02007944 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02007945 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02007946
Radek Krejci03b71f72016-03-16 11:10:09 +01007947 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007948 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01007949 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007950 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01007951 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007952 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007953 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007954
Radek Krejci0b7704f2016-03-18 12:16:14 +01007955 if (type == UNRES_WHEN) {
7956 /* remove previous result */
7957 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007958 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007959
Michal Vasko53b7da02018-02-13 15:28:42 +01007960 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007961}
7962
7963/**
7964 * @brief Resolve every unres data item in the structure. Logs directly.
7965 *
Michal Vasko660582a2018-03-19 10:10:08 +01007966 * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected,
7967 * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit
7968 * non-presence containers may need to be deleted).
Radek Krejci082c84f2016-10-17 16:33:06 +02007969 *
7970 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
7971 *
Michal Vasko53b7da02018-02-13 15:28:42 +01007972 * @param[in] ctx Context used.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007973 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02007974 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
7975 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007976 *
7977 * @return EXIT_SUCCESS on success, -1 on error.
7978 */
7979int
Michal Vasko53b7da02018-02-13 15:28:42 +01007980resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007981{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007982 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007983 int rc, progress, ignore_fail;
Michal Vasko53b7da02018-02-13 15:28:42 +01007984 enum int_log_opts prev_ilo;
7985 struct ly_err_item *prev_eitem;
mohitarora2489837dc2018-05-01 15:09:36 +05307986 LY_ERR prev_ly_errno = ly_errno;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007987 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02007988 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007989
Radek Krejci082c84f2016-10-17 16:33:06 +02007990 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01007991 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01007992
7993 if (!unres->count) {
7994 return EXIT_SUCCESS;
7995 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007996
Michal Vasko7fa93142018-03-19 09:59:10 +01007997 if (options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007998 ignore_fail = 1;
7999 } else if (options & LYD_OPT_NOEXTDEPS) {
8000 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008001 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008002 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008003 }
8004
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008005 LOGVRB("Resolving unresolved data nodes and their constraints...");
Michal Vasko7fa93142018-03-19 09:59:10 +01008006 if (!ignore_fail) {
8007 /* remember logging state only if errors are generated and valid */
Michal Vasko7fa93142018-03-19 09:59:10 +01008008 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
8009 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008010
Michal Vasko7fa93142018-03-19 09:59:10 +01008011 /*
8012 * when-stmt first
8013 */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008014 first = 1;
8015 stmt_count = 0;
8016 resolved = 0;
8017 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01008018 do {
Michal Vasko7fa93142018-03-19 09:59:10 +01008019 if (!ignore_fail) {
8020 ly_err_free_next(ctx, prev_eitem);
8021 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008022 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02008023 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008024 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008025 continue;
8026 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008027 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008028 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008029 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008030 }
8031
8032 /* resolve when condition only when all parent when conditions are already resolved */
8033 for (parent = unres->node[i]->parent;
8034 parent && LYD_WHEN_DONE(parent->when_status);
8035 parent = parent->parent) {
8036 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
8037 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02008038 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01008039 */
8040 unres->node[i]->when_status |= LYD_WHEN_FALSE;
8041 unres->type[i] = UNRES_RESOLVED;
8042 resolved++;
8043 break;
8044 }
8045 }
8046 if (parent) {
8047 continue;
8048 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008049
Michal Vasko0b963112017-08-11 12:45:36 +02008050 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01008051 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02008052 /* finish with error/delete the node only if when was false, an external dependency was not required,
8053 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02008054 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vaskoc04173b2018-03-09 10:43:22 +01008055 && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) {
Radek Krejci082c84f2016-10-17 16:33:06 +02008056 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01008057 /* false when condition */
Michal Vasko53b7da02018-02-13 15:28:42 +01008058 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008059 } /* follows else */
8060
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008061 /* auto-delete */
Michal Vasko53b7da02018-02-13 15:28:42 +01008062 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond);
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008063
Radek Krejci0c0086a2016-03-24 15:20:28 +01008064 /* only unlink now, the subtree can contain another nodes stored in the unres list */
8065 /* if it has parent non-presence containers that would be empty, we should actually
8066 * remove the container
8067 */
Radek Krejci2537fd32016-09-07 16:22:41 +02008068 for (parent = unres->node[i];
8069 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
8070 parent = parent->parent) {
8071 if (((struct lys_node_container *)parent->parent->schema)->presence) {
8072 /* presence container */
8073 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008074 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008075 if (parent->next || parent->prev != parent) {
8076 /* non empty (the child we are in and we are going to remove is not the only child) */
8077 break;
8078 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008079 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008080 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008081
Radek Krejci0c0086a2016-03-24 15:20:28 +01008082 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008083 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01008084 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008085
Radek Krejci0b7704f2016-03-18 12:16:14 +01008086 lyd_unlink(unres->node[i]);
8087 unres->type[i] = UNRES_DELETE;
8088 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01008089
8090 /* update the rest of unres items */
8091 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01008092 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01008093 continue;
8094 }
8095
8096 /* test if the node is in subtree to be deleted */
8097 for (parent = unres->node[j]; parent; parent = parent->parent) {
8098 if (parent == unres->node[i]) {
8099 /* yes, it is */
8100 unres->type[j] = UNRES_RESOLVED;
8101 resolved++;
8102 break;
8103 }
8104 }
8105 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008106 } else {
8107 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01008108 }
Michal Vasko7fa93142018-03-19 09:59:10 +01008109 if (!ignore_fail) {
8110 ly_err_free_next(ctx, prev_eitem);
8111 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008112 resolved++;
8113 progress = 1;
8114 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008115 goto error;
Radek Krejci2467a492016-10-24 15:16:59 +02008116 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01008117 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008118 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008119 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01008120
Radek Krejci0b7704f2016-03-18 12:16:14 +01008121 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008122 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008123 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008124 }
8125
8126 for (i = 0; del_items && i < unres->count; i++) {
8127 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
8128 if (unres->type[i] != UNRES_DELETE) {
8129 continue;
8130 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008131 if (!unres->node[i]) {
8132 unres->type[i] = UNRES_RESOLVED;
8133 del_items--;
8134 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008135 }
8136
8137 /* really remove the complete subtree */
8138 lyd_free(unres->node[i]);
8139 unres->type[i] = UNRES_RESOLVED;
8140 del_items--;
8141 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008142
Michal Vasko7fa93142018-03-19 09:59:10 +01008143 /*
8144 * now leafrefs
8145 */
8146 if (options & LYD_OPT_TRUSTED) {
8147 /* we want to attempt to resolve leafrefs */
8148 assert(!ignore_fail);
8149 ignore_fail = 1;
8150
8151 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8152 ly_errno = prev_ly_errno;
8153 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008154 first = 1;
8155 stmt_count = 0;
8156 resolved = 0;
8157 do {
8158 progress = 0;
8159 for (i = 0; i < unres->count; i++) {
8160 if (unres->type[i] != UNRES_LEAFREF) {
8161 continue;
8162 }
8163 if (first) {
8164 /* count leafref nodes in unres list */
8165 stmt_count++;
8166 }
8167
Michal Vasko0b963112017-08-11 12:45:36 +02008168 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008169 if (!rc) {
8170 unres->type[i] = UNRES_RESOLVED;
Michal Vasko7fa93142018-03-19 09:59:10 +01008171 if (!ignore_fail) {
8172 ly_err_free_next(ctx, prev_eitem);
8173 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008174 resolved++;
8175 progress = 1;
8176 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008177 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008178 } /* else forward reference */
8179 }
8180 first = 0;
8181 } while (progress && resolved < stmt_count);
8182
8183 /* do we have some unresolved leafrefs? */
8184 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008185 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008186 }
8187
Michal Vasko7fa93142018-03-19 09:59:10 +01008188 if (!ignore_fail) {
8189 /* log normally now, throw away irrelevant errors */
8190 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8191 ly_errno = prev_ly_errno;
8192 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008193
Michal Vasko7fa93142018-03-19 09:59:10 +01008194 /*
8195 * rest
8196 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008197 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008198 if (unres->type[i] == UNRES_RESOLVED) {
8199 continue;
8200 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008201 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008202
Michal Vasko0b963112017-08-11 12:45:36 +02008203 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008204 if (rc) {
8205 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008206 return -1;
8207 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008208
8209 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008210 }
8211
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008212 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008213 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008214 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01008215
8216error:
Michal Vasko7fa93142018-03-19 09:59:10 +01008217 if (!ignore_fail) {
8218 /* print all the new errors */
8219 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
8220 /* do not restore ly_errno, it was udpated properly */
8221 }
Michal Vasko53b7da02018-02-13 15:28:42 +01008222 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008223}