blob: 6a4c611cc04b804d3ddbde1bc9da6515f5011aa4 [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
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"
Radek Krejci41912fe2015-10-22 10:22:12 +020030#include "dict_private.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.
172 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:].).
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{
181 int parsed = 0, ret;
182
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));
186 assert(!extended || all_desc);
187
Michal Vasko723e50c2015-10-20 15:20:29 +0200188 if (mod_name) {
189 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200190 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200191 }
192 if (name) {
193 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200194 *nam_len = 0;
195 }
196
Michal Vasko50576712017-07-28 12:28:33 +0200197 if (extended) {
198 /* try to parse only the extended expressions */
199 if (id[parsed] == '/') {
200 *all_desc = 1;
201 } else {
202 *all_desc = 0;
203 }
204
205 /* is there a prefix? */
206 ret = parse_identifier(id + *all_desc);
207 if (ret > 0) {
208 if (id[*all_desc + ret] != ':') {
209 /* this is not a prefix, so not an extended id */
210 goto standard_id;
211 }
212
213 if (mod_name) {
214 *mod_name = id + *all_desc;
215 *mod_name_len = ret;
216 }
217
218 /* "/" and ":" */
219 ret += *all_desc + 1;
220 } else {
221 ret = *all_desc;
222 }
223
224 /* parse either "*" or "." */
225 if (!strcmp(id + ret, "*")) {
226 if (name) {
227 *name = id + ret;
228 *nam_len = 1;
229 }
230 ++ret;
231
232 return ret;
233 } else if (!strcmp(id + ret, ".")) {
234 if (!*all_desc) {
235 /* /. is redundant expression, we do not accept it */
236 return -ret;
237 }
238
239 if (name) {
240 *name = id + ret;
241 *nam_len = 1;
242 }
243 ++ret;
244
245 return ret;
246 }
247 /* else a standard id, parse it all again */
248 }
249
250standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200251 if ((ret = parse_identifier(id)) < 1) {
252 return ret;
253 }
254
Michal Vasko723e50c2015-10-20 15:20:29 +0200255 if (mod_name) {
256 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200257 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200258 }
259
260 parsed += ret;
261 id += ret;
262
263 /* there is prefix */
264 if (id[0] == ':') {
265 ++parsed;
266 ++id;
267
268 /* there isn't */
269 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200270 if (name && mod_name) {
271 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200272 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200273 if (mod_name) {
274 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200275 }
276
Michal Vasko723e50c2015-10-20 15:20:29 +0200277 if (nam_len && mod_name_len) {
278 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200279 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200280 if (mod_name_len) {
281 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200282 }
283
284 return parsed;
285 }
286
287 /* identifier (node name) */
288 if ((ret = parse_identifier(id)) < 1) {
289 return -parsed+ret;
290 }
291
292 if (name) {
293 *name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200294 *nam_len = ret;
295 }
296
297 return parsed+ret;
298}
299
300/**
301 * @brief Parse a path-predicate (leafref).
302 *
303 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
304 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
305 *
Michal Vaskobb211122015-08-19 14:03:11 +0200306 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200307 * @param[out] prefix Points to the prefix, NULL if there is not any.
308 * @param[out] pref_len Length of the prefix, 0 if there is not any.
309 * @param[out] name Points to the node name.
310 * @param[out] nam_len Length of the node name.
311 * @param[out] path_key_expr Points to the path-key-expr.
312 * @param[out] pke_len Length of the path-key-expr.
313 * @param[out] has_predicate Flag to mark whether there is another predicate following.
314 *
315 * @return Number of characters successfully parsed,
316 * positive on success, negative on failure.
317 */
318static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200319parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
320 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200321{
322 const char *ptr;
323 int parsed = 0, ret;
324
325 assert(id);
326 if (prefix) {
327 *prefix = NULL;
328 }
329 if (pref_len) {
330 *pref_len = 0;
331 }
332 if (name) {
333 *name = NULL;
334 }
335 if (nam_len) {
336 *nam_len = 0;
337 }
338 if (path_key_expr) {
339 *path_key_expr = NULL;
340 }
341 if (pke_len) {
342 *pke_len = 0;
343 }
344 if (has_predicate) {
345 *has_predicate = 0;
346 }
347
348 if (id[0] != '[') {
349 return -parsed;
350 }
351
352 ++parsed;
353 ++id;
354
355 while (isspace(id[0])) {
356 ++parsed;
357 ++id;
358 }
359
Michal Vasko50576712017-07-28 12:28:33 +0200360 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200361 return -parsed+ret;
362 }
363
364 parsed += ret;
365 id += ret;
366
367 while (isspace(id[0])) {
368 ++parsed;
369 ++id;
370 }
371
372 if (id[0] != '=') {
373 return -parsed;
374 }
375
376 ++parsed;
377 ++id;
378
379 while (isspace(id[0])) {
380 ++parsed;
381 ++id;
382 }
383
384 if ((ptr = strchr(id, ']')) == NULL) {
385 return -parsed;
386 }
387
388 --ptr;
389 while (isspace(ptr[0])) {
390 --ptr;
391 }
392 ++ptr;
393
394 ret = ptr-id;
395 if (path_key_expr) {
396 *path_key_expr = id;
397 }
398 if (pke_len) {
399 *pke_len = ret;
400 }
401
402 parsed += ret;
403 id += ret;
404
405 while (isspace(id[0])) {
406 ++parsed;
407 ++id;
408 }
409
410 assert(id[0] == ']');
411
412 if (id[1] == '[') {
413 *has_predicate = 1;
414 }
415
416 return parsed+1;
417}
418
419/**
420 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
421 * the ".." and the first node-identifier, other calls parse a single
422 * node-identifier each.
423 *
424 * path-key-expr = current-function-invocation *WSP "/" *WSP
425 * rel-path-keyexpr
426 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
427 * *(node-identifier *WSP "/" *WSP)
428 * node-identifier
429 *
Michal Vaskobb211122015-08-19 14:03:11 +0200430 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200431 * @param[out] prefix Points to the prefix, NULL if there is not any.
432 * @param[out] pref_len Length of the prefix, 0 if there is not any.
433 * @param[out] name Points to the node name.
434 * @param[out] nam_len Length of the node name.
435 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
436 * must not be changed between consecutive calls.
437 * @return Number of characters successfully parsed,
438 * positive on success, negative on failure.
439 */
440static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200441parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
442 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200443{
444 int parsed = 0, ret, par_times = 0;
445
446 assert(id);
447 assert(parent_times);
448 if (prefix) {
449 *prefix = NULL;
450 }
451 if (pref_len) {
452 *pref_len = 0;
453 }
454 if (name) {
455 *name = NULL;
456 }
457 if (nam_len) {
458 *nam_len = 0;
459 }
460
461 if (!*parent_times) {
462 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
463 if (strncmp(id, "current()", 9)) {
464 return -parsed;
465 }
466
467 parsed += 9;
468 id += 9;
469
470 while (isspace(id[0])) {
471 ++parsed;
472 ++id;
473 }
474
475 if (id[0] != '/') {
476 return -parsed;
477 }
478
479 ++parsed;
480 ++id;
481
482 while (isspace(id[0])) {
483 ++parsed;
484 ++id;
485 }
486
487 /* rel-path-keyexpr */
488 if (strncmp(id, "..", 2)) {
489 return -parsed;
490 }
491 ++par_times;
492
493 parsed += 2;
494 id += 2;
495
496 while (isspace(id[0])) {
497 ++parsed;
498 ++id;
499 }
500 }
501
502 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
503 *
504 * first parent reference with whitespaces already parsed
505 */
506 if (id[0] != '/') {
507 return -parsed;
508 }
509
510 ++parsed;
511 ++id;
512
513 while (isspace(id[0])) {
514 ++parsed;
515 ++id;
516 }
517
518 while (!strncmp(id, "..", 2) && !*parent_times) {
519 ++par_times;
520
521 parsed += 2;
522 id += 2;
523
524 while (isspace(id[0])) {
525 ++parsed;
526 ++id;
527 }
528
529 if (id[0] != '/') {
530 return -parsed;
531 }
532
533 ++parsed;
534 ++id;
535
536 while (isspace(id[0])) {
537 ++parsed;
538 ++id;
539 }
540 }
541
542 if (!*parent_times) {
543 *parent_times = par_times;
544 }
545
546 /* all parent references must be parsed at this point */
Michal Vasko50576712017-07-28 12:28:33 +0200547 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
548 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200549 }
550
551 parsed += ret;
552 id += ret;
553
554 return parsed;
555}
556
557/**
558 * @brief Parse path-arg (leafref).
559 *
560 * path-arg = absolute-path / relative-path
561 * absolute-path = 1*("/" (node-identifier *path-predicate))
562 * relative-path = 1*(".." "/") descendant-path
563 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200564 * @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 +0200565 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200566 * @param[out] prefix Points to the prefix, NULL if there is not any.
567 * @param[out] pref_len Length of the prefix, 0 if there is not any.
568 * @param[out] name Points to the node name.
569 * @param[out] nam_len Length of the node name.
570 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
571 * must not be changed between consecutive calls. -1 if the
572 * path is relative.
573 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
574 *
575 * @return Number of characters successfully parsed,
576 * positive on success, negative on failure.
577 */
578static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200579parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200580 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200581{
582 int parsed = 0, ret, par_times = 0;
583
584 assert(id);
585 assert(parent_times);
586 if (prefix) {
587 *prefix = NULL;
588 }
589 if (pref_len) {
590 *pref_len = 0;
591 }
592 if (name) {
593 *name = NULL;
594 }
595 if (nam_len) {
596 *nam_len = 0;
597 }
598 if (has_predicate) {
599 *has_predicate = 0;
600 }
601
602 if (!*parent_times && !strncmp(id, "..", 2)) {
603 ++par_times;
604
605 parsed += 2;
606 id += 2;
607
608 while (!strncmp(id, "/..", 3)) {
609 ++par_times;
610
611 parsed += 3;
612 id += 3;
613 }
614 }
615
616 if (!*parent_times) {
617 if (par_times) {
618 *parent_times = par_times;
619 } else {
620 *parent_times = -1;
621 }
622 }
623
624 if (id[0] != '/') {
625 return -parsed;
626 }
627
628 /* skip '/' */
629 ++parsed;
630 ++id;
631
632 /* node-identifier ([prefix:]identifier) */
Michal Vasko50576712017-07-28 12:28:33 +0200633 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
634 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200635 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200636 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200637 /* actually we always need prefix even it is not specified */
638 *prefix = lys_main_module(mod)->name;
639 *pref_len = strlen(*prefix);
640 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200641
642 parsed += ret;
643 id += ret;
644
645 /* there is no predicate */
646 if ((id[0] == '/') || !id[0]) {
647 return parsed;
648 } else if (id[0] != '[') {
649 return -parsed;
650 }
651
652 if (has_predicate) {
653 *has_predicate = 1;
654 }
655
656 return parsed;
657}
658
659/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200660 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200661 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200662 *
663 * instance-identifier = 1*("/" (node-identifier *predicate))
664 *
Michal Vaskobb211122015-08-19 14:03:11 +0200665 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200666 * @param[out] model Points to the model name.
667 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200668 * @param[out] name Points to the node name.
669 * @param[out] nam_len Length of the node name.
670 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
671 *
672 * @return Number of characters successfully parsed,
673 * positive on success, negative on failure.
674 */
675static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200676parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
677 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200678{
679 int parsed = 0, ret;
680
Michal Vasko1b6ca962017-08-03 14:23:09 +0200681 assert(id && model && mod_len && name && nam_len);
682
Radek Krejci6dc53a22015-08-17 13:27:59 +0200683 if (has_predicate) {
684 *has_predicate = 0;
685 }
686
687 if (id[0] != '/') {
688 return -parsed;
689 }
690
691 ++parsed;
692 ++id;
693
Michal Vaskob2f40be2016-09-08 16:03:48 +0200694 if ((ret = parse_identifier(id)) < 1) {
695 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200696 }
697
Michal Vaskob2f40be2016-09-08 16:03:48 +0200698 *name = id;
699 *nam_len = ret;
700
701 parsed += ret;
702 id += ret;
703
Michal Vasko1b6ca962017-08-03 14:23:09 +0200704 if (id[0] == ':') {
705 /* we have prefix */
706 *model = *name;
707 *mod_len = *nam_len;
708
709 ++parsed;
710 ++id;
711
712 if ((ret = parse_identifier(id)) < 1) {
713 return ret;
714 }
715
716 *name = id;
717 *nam_len = ret;
718
719 parsed += ret;
720 id += ret;
721 }
722
Radek Krejci4967cb62016-09-14 16:40:28 +0200723 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200724 *has_predicate = 1;
725 }
726
727 return parsed;
728}
729
730/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200731 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200732 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200733 *
734 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
735 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
736 * ((DQUOTE string DQUOTE) /
737 * (SQUOTE string SQUOTE))
738 * pos = non-negative-integer-value
739 *
Michal Vaskobb211122015-08-19 14:03:11 +0200740 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200741 * @param[out] model Points to the model name.
742 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200743 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
744 * @param[out] nam_len Length of the node name.
745 * @param[out] value Value the node-identifier must have (string from the grammar),
746 * NULL if there is not any.
747 * @param[out] val_len Length of the value, 0 if there is not any.
748 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
749 *
750 * @return Number of characters successfully parsed,
751 * positive on success, negative on failure.
752 */
753static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200754parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
755 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200756{
757 const char *ptr;
758 int parsed = 0, ret;
759 char quote;
760
761 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200762 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200763 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200764 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200765 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200766 }
767 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200768 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200769 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200770 *nam_len = 0;
771 }
772 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200773 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200774 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200775 *val_len = 0;
776 }
777 if (has_predicate) {
778 *has_predicate = 0;
779 }
780
781 if (id[0] != '[') {
782 return -parsed;
783 }
784
785 ++parsed;
786 ++id;
787
788 while (isspace(id[0])) {
789 ++parsed;
790 ++id;
791 }
792
793 /* pos */
794 if (isdigit(id[0])) {
795 if (name) {
796 *name = id;
797 }
798
799 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200800 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200801 }
802
803 while (isdigit(id[0])) {
804 ++parsed;
805 ++id;
806 }
807
808 if (nam_len) {
809 *nam_len = id-(*name);
810 }
811
Michal Vaskof2f28a12016-09-09 12:43:06 +0200812 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200813 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200814 if (id[0] == '.') {
815 if (name) {
816 *name = id;
817 }
818 if (nam_len) {
819 *nam_len = 1;
820 }
821
822 ++parsed;
823 ++id;
824
825 } else {
Michal Vasko50576712017-07-28 12:28:33 +0200826 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200827 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200828 }
829
830 parsed += ret;
831 id += ret;
832 }
833
834 while (isspace(id[0])) {
835 ++parsed;
836 ++id;
837 }
838
839 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200840 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200841 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200842
Radek Krejci6dc53a22015-08-17 13:27:59 +0200843 ++parsed;
844 ++id;
845
Michal Vaskof2f28a12016-09-09 12:43:06 +0200846 while (isspace(id[0])) {
847 ++parsed;
848 ++id;
849 }
850
851 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
852 if ((id[0] == '\"') || (id[0] == '\'')) {
853 quote = id[0];
854
855 ++parsed;
856 ++id;
857
858 if ((ptr = strchr(id, quote)) == NULL) {
859 return -parsed;
860 }
Michal Vasko1b6ca962017-08-03 14:23:09 +0200861 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200862
863 if (value) {
864 *value = id;
865 }
866 if (val_len) {
867 *val_len = ret;
868 }
869
Michal Vasko1b6ca962017-08-03 14:23:09 +0200870 parsed += ret + 1;
871 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200872 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200873 return -parsed;
874 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200875 }
876
877 while (isspace(id[0])) {
878 ++parsed;
879 ++id;
880 }
881
882 if (id[0] != ']') {
883 return -parsed;
884 }
885
886 ++parsed;
887 ++id;
888
889 if ((id[0] == '[') && has_predicate) {
890 *has_predicate = 1;
891 }
892
893 return parsed;
894}
895
896/**
897 * @brief Parse schema-nodeid.
898 *
899 * schema-nodeid = absolute-schema-nodeid /
900 * descendant-schema-nodeid
901 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200902 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200903 * node-identifier
904 * absolute-schema-nodeid
905 *
Michal Vaskobb211122015-08-19 14:03:11 +0200906 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200907 * @param[out] mod_name Points to the module name, NULL if there is not any.
908 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200909 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200910 * @param[out] nam_len Length of the node name.
911 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
912 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100913 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
914 * based on the grammar, in those cases use NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200915 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200916 *
917 * @return Number of characters successfully parsed,
918 * positive on success, negative on failure.
919 */
Michal Vasko22448d32016-03-16 13:17:29 +0100920int
Michal Vasko723e50c2015-10-20 15:20:29 +0200921parse_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 +0200922 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200923{
924 int parsed = 0, ret;
925
926 assert(id);
927 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200928
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100929 if (has_predicate) {
930 *has_predicate = 0;
931 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200932
933 if (id[0] != '/') {
934 if (*is_relative != -1) {
935 return -parsed;
936 } else {
937 *is_relative = 1;
938 }
Michal Vasko48935352016-03-29 11:52:36 +0200939 if (!strncmp(id, "./", 2)) {
940 parsed += 2;
941 id += 2;
942 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200943 } else {
944 if (*is_relative == -1) {
945 *is_relative = 0;
946 }
947 ++parsed;
948 ++id;
949 }
950
Michal Vasko50576712017-07-28 12:28:33 +0200951 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
952 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200953 }
954
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100955 parsed += ret;
956 id += ret;
957
958 if ((id[0] == '[') && has_predicate) {
959 *has_predicate = 1;
960 }
961
962 return parsed;
963}
964
965/**
966 * @brief Parse schema predicate (special format internally used).
967 *
968 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200969 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100970 * key-with-value = identifier *WSP "=" *WSP
971 * ((DQUOTE string DQUOTE) /
972 * (SQUOTE string SQUOTE))
973 *
974 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200975 * @param[out] mod_name Points to the list key module name.
976 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100977 * @param[out] name Points to the list key name.
978 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100979 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100980 * @param[out] val_len Length of \p value.
981 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
982 */
Michal Vasko22448d32016-03-16 13:17:29 +0100983int
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200984parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
985 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100986{
987 const char *ptr;
988 int parsed = 0, ret;
989 char quote;
990
991 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200992 if (mod_name) {
993 *mod_name = NULL;
994 }
995 if (mod_name_len) {
996 *mod_name_len = 0;
997 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100998 if (name) {
999 *name = NULL;
1000 }
1001 if (nam_len) {
1002 *nam_len = 0;
1003 }
1004 if (value) {
1005 *value = NULL;
1006 }
1007 if (val_len) {
1008 *val_len = 0;
1009 }
1010 if (has_predicate) {
1011 *has_predicate = 0;
1012 }
1013
1014 if (id[0] != '[') {
1015 return -parsed;
1016 }
1017
1018 ++parsed;
1019 ++id;
1020
1021 while (isspace(id[0])) {
1022 ++parsed;
1023 ++id;
1024 }
1025
Michal Vasko22448d32016-03-16 13:17:29 +01001026 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001027 if (id[0] == '.') {
1028 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001029
1030 if (name) {
1031 *name = id;
1032 }
1033 if (nam_len) {
1034 *nam_len = ret;
1035 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001036 } else if (isdigit(id[0])) {
1037 if (id[0] == '0') {
1038 return -parsed;
1039 }
1040 ret = 1;
1041 while (isdigit(id[ret])) {
1042 ++ret;
1043 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001044
1045 if (name) {
1046 *name = id;
1047 }
1048 if (nam_len) {
1049 *nam_len = ret;
1050 }
Michal Vasko50576712017-07-28 12:28:33 +02001051 } 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 +01001052 return -parsed + ret;
1053 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001054
1055 parsed += ret;
1056 id += ret;
1057
1058 while (isspace(id[0])) {
1059 ++parsed;
1060 ++id;
1061 }
1062
1063 /* there is value as well */
1064 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001065 if (name && isdigit(**name)) {
1066 return -parsed;
1067 }
1068
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001069 ++parsed;
1070 ++id;
1071
1072 while (isspace(id[0])) {
1073 ++parsed;
1074 ++id;
1075 }
1076
1077 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1078 if ((id[0] == '\"') || (id[0] == '\'')) {
1079 quote = id[0];
1080
1081 ++parsed;
1082 ++id;
1083
1084 if ((ptr = strchr(id, quote)) == NULL) {
1085 return -parsed;
1086 }
1087 ret = ptr - id;
1088
1089 if (value) {
1090 *value = id;
1091 }
1092 if (val_len) {
1093 *val_len = ret;
1094 }
1095
1096 parsed += ret + 1;
1097 id += ret + 1;
1098 } else {
1099 return -parsed;
1100 }
1101
1102 while (isspace(id[0])) {
1103 ++parsed;
1104 ++id;
1105 }
1106 }
1107
1108 if (id[0] != ']') {
1109 return -parsed;
1110 }
1111
1112 ++parsed;
1113 ++id;
1114
1115 if ((id[0] == '[') && has_predicate) {
1116 *has_predicate = 1;
1117 }
1118
1119 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001120}
1121
1122/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001123 * @brief Resolve (find) a feature definition. Logs directly.
1124 *
1125 * @param[in] feat_name Feature name to resolve.
1126 * @param[in] len Length of \p feat_name.
1127 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001128 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1129 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001130 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001131 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001132 */
1133static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001134resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001135{
1136 char *str;
1137 const char *mod_name, *name;
1138 int mod_name_len, nam_len, i, j;
1139 const struct lys_module *module;
1140
Radek Krejci9ff0a922016-07-14 13:08:05 +02001141 assert(feature);
1142
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001143 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001144 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001145 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
1146 return -1;
1147 }
1148
Michal Vasko921eb6b2017-10-13 10:01:39 +02001149 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001150 if (!module) {
1151 /* identity refers unknown data model */
1152 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
1153 return -1;
1154 }
1155
Radek Krejci9ff0a922016-07-14 13:08:05 +02001156 if (module != node->module && module == lys_node_module(node)) {
1157 /* first, try to search directly in submodule where the feature was mentioned */
1158 for (j = 0; j < node->module->features_size; j++) {
1159 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1160 /* check status */
1161 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001162 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001163 return -1;
1164 }
1165 *feature = &node->module->features[j];
1166 return 0;
1167 }
1168 }
1169 }
1170
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001171 /* search in the identified module ... */
1172 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001173 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001174 /* check status */
1175 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001176 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001177 return -1;
1178 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001179 *feature = &module->features[j];
1180 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001181 }
1182 }
1183 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001184 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001185 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001186 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1187 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001188 /* check status */
1189 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1190 module->inc[i].submodule->features[j].flags,
1191 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001192 module->inc[i].submodule->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->inc[i].submodule->features[j];
1196 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001197 }
1198 }
1199 }
1200
1201 /* not found */
1202 str = strndup(feat_name, len);
1203 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
1204 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001205 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001206}
1207
Radek Krejci9ff0a922016-07-14 13:08:05 +02001208/*
1209 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001210 * - 1 if enabled
1211 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001212 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001213static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001214resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001215{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001216 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001217
Radek Krejci9ff0a922016-07-14 13:08:05 +02001218 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001219 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001220 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001221 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001222 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001223
Radek Krejci69b8d922016-07-27 13:13:41 +02001224 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001225}
1226
1227static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001228resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001229{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001230 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001231 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001232
Radek Krejci9ff0a922016-07-14 13:08:05 +02001233 op = iff_getop(expr->expr, *index_e);
1234 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001235
Radek Krejci9ff0a922016-07-14 13:08:05 +02001236 switch (op) {
1237 case LYS_IFF_F:
1238 /* resolve feature */
1239 return resolve_feature_value(expr->features[(*index_f)++]);
1240 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001241 /* invert result */
1242 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001243 case LYS_IFF_AND:
1244 case LYS_IFF_OR:
1245 a = resolve_iffeature_recursive(expr, index_e, index_f);
1246 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001247 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001248 return a && b;
1249 } else { /* LYS_IFF_OR */
1250 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001251 }
1252 }
1253
Radek Krejciaf566332017-02-07 15:56:59 +01001254 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001255}
1256
1257int
1258resolve_iffeature(struct lys_iffeature *expr)
1259{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001260 int index_e = 0, index_f = 0;
1261
1262 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001263 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001264 }
Radek Krejciaf566332017-02-07 15:56:59 +01001265 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001266}
1267
1268struct iff_stack {
1269 int size;
1270 int index; /* first empty item */
1271 uint8_t *stack;
1272};
1273
1274static int
1275iff_stack_push(struct iff_stack *stack, uint8_t value)
1276{
1277 if (stack->index == stack->size) {
1278 stack->size += 4;
1279 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001280 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001281 }
1282
1283 stack->stack[stack->index++] = value;
1284 return EXIT_SUCCESS;
1285}
1286
1287static uint8_t
1288iff_stack_pop(struct iff_stack *stack)
1289{
1290 stack->index--;
1291 return stack->stack[stack->index];
1292}
1293
1294static void
1295iff_stack_clean(struct iff_stack *stack)
1296{
1297 stack->size = 0;
1298 free(stack->stack);
1299}
1300
1301static void
1302iff_setop(uint8_t *list, uint8_t op, int pos)
1303{
1304 uint8_t *item;
1305 uint8_t mask = 3;
1306
1307 assert(pos >= 0);
1308 assert(op <= 3); /* max 2 bits */
1309
1310 item = &list[pos / 4];
1311 mask = mask << 2 * (pos % 4);
1312 *item = (*item) & ~mask;
1313 *item = (*item) | (op << 2 * (pos % 4));
1314}
1315
1316uint8_t
1317iff_getop(uint8_t *list, int pos)
1318{
1319 uint8_t *item;
1320 uint8_t mask = 3, result;
1321
1322 assert(pos >= 0);
1323
1324 item = &list[pos / 4];
1325 result = (*item) & (mask << 2 * (pos % 4));
1326 return result >> 2 * (pos % 4);
1327}
1328
1329#define LYS_IFF_LP 0x04 /* ( */
1330#define LYS_IFF_RP 0x08 /* ) */
1331
Radek Krejcicbb473e2016-09-16 14:48:32 +02001332/* internal structure for passing data for UNRES_IFFEAT */
1333struct unres_iffeat_data {
1334 struct lys_node *node;
1335 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001336 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001337};
1338
Radek Krejci9ff0a922016-07-14 13:08:05 +02001339void
1340resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1341{
1342 unsigned int e = 0, f = 0, r = 0;
1343 uint8_t op;
1344
1345 assert(iffeat);
1346
1347 if (!iffeat->expr) {
1348 goto result;
1349 }
1350
1351 do {
1352 op = iff_getop(iffeat->expr, e++);
1353 switch (op) {
1354 case LYS_IFF_NOT:
1355 if (!r) {
1356 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001357 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001358 break;
1359 case LYS_IFF_AND:
1360 case LYS_IFF_OR:
1361 if (!r) {
1362 r += 2;
1363 } else {
1364 r += 1;
1365 }
1366 break;
1367 case LYS_IFF_F:
1368 f++;
1369 if (r) {
1370 r--;
1371 }
1372 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001373 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001374 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001375
Radek Krejci9ff0a922016-07-14 13:08:05 +02001376result:
1377 if (expr_size) {
1378 *expr_size = e;
1379 }
1380 if (feat_size) {
1381 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001382 }
1383}
1384
1385int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001386resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001387 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001388{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001389 const char *c = value;
1390 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001391 int i, j, last_not, checkversion = 0;
1392 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001393 uint8_t op;
1394 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001395 struct unres_iffeat_data *iff_data;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001396
Radek Krejci9ff0a922016-07-14 13:08:05 +02001397 assert(c);
1398
1399 if (isspace(c[0])) {
1400 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
1401 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001402 }
1403
Radek Krejci9ff0a922016-07-14 13:08:05 +02001404 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1405 for (i = j = last_not = 0; c[i]; i++) {
1406 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001407 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001408 j++;
1409 continue;
1410 } else if (c[i] == ')') {
1411 j--;
1412 continue;
1413 } else if (isspace(c[i])) {
1414 continue;
1415 }
1416
1417 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1418 if (c[i + r] == '\0') {
Radek Krejcia98da3f2016-07-27 14:05:22 +02001419 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001420 return EXIT_FAILURE;
1421 } else if (!isspace(c[i + r])) {
1422 /* feature name starting with the not/and/or */
1423 last_not = 0;
1424 f_size++;
1425 } else if (c[i] == 'n') { /* not operation */
1426 if (last_not) {
1427 /* double not */
1428 expr_size = expr_size - 2;
1429 last_not = 0;
1430 } else {
1431 last_not = 1;
1432 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001433 } else { /* and, or */
1434 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001435 /* not a not operation */
1436 last_not = 0;
1437 }
1438 i += r;
1439 } else {
1440 f_size++;
1441 last_not = 0;
1442 }
1443 expr_size++;
1444
1445 while (!isspace(c[i])) {
1446 if (!c[i] || c[i] == ')') {
1447 i--;
1448 break;
1449 }
1450 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001451 }
1452 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001453 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001454 /* not matching count of ( and ) */
1455 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1456 return EXIT_FAILURE;
1457 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001458
Radek Krejci69b8d922016-07-27 13:13:41 +02001459 if (checkversion || expr_size > 1) {
1460 /* check that we have 1.1 module */
1461 if (node->module->version != 2) {
1462 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1463 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
1464 return EXIT_FAILURE;
1465 }
1466 }
1467
Radek Krejci9ff0a922016-07-14 13:08:05 +02001468 /* allocate the memory */
1469 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001470 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001471 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001472 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error);
1473 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001474 f_size--; expr_size--; /* used as indexes from now */
1475
1476 for (i--; i >= 0; i--) {
1477 if (c[i] == ')') {
1478 /* push it on stack */
1479 iff_stack_push(&stack, LYS_IFF_RP);
1480 continue;
1481 } else if (c[i] == '(') {
1482 /* pop from the stack into result all operators until ) */
1483 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1484 iff_setop(iffeat_expr->expr, op, expr_size--);
1485 }
1486 continue;
1487 } else if (isspace(c[i])) {
1488 continue;
1489 }
1490
1491 /* end operator or operand -> find beginning and get what is it */
1492 j = i + 1;
1493 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1494 i--;
1495 }
1496 i++; /* get back by one step */
1497
1498 if (!strncmp(&c[i], "not ", 4)) {
1499 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1500 /* double not */
1501 iff_stack_pop(&stack);
1502 } else {
1503 /* not has the highest priority, so do not pop from the stack
1504 * as in case of AND and OR */
1505 iff_stack_push(&stack, LYS_IFF_NOT);
1506 }
1507 } else if (!strncmp(&c[i], "and ", 4)) {
1508 /* as for OR - pop from the stack all operators with the same or higher
1509 * priority and store them to the result, then push the AND to the stack */
1510 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1511 op = iff_stack_pop(&stack);
1512 iff_setop(iffeat_expr->expr, op, expr_size--);
1513 }
1514 iff_stack_push(&stack, LYS_IFF_AND);
1515 } else if (!strncmp(&c[i], "or ", 3)) {
1516 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1517 op = iff_stack_pop(&stack);
1518 iff_setop(iffeat_expr->expr, op, expr_size--);
1519 }
1520 iff_stack_push(&stack, LYS_IFF_OR);
1521 } else {
1522 /* feature name, length is j - i */
1523
1524 /* add it to the result */
1525 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1526
1527 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001528 * forward referenced, we have to keep the feature name in auxiliary
1529 * structure passed into unres */
1530 iff_data = malloc(sizeof *iff_data);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001531 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001532 iff_data->node = node;
1533 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001534 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001535 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1536 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001537 f_size--;
1538
1539 if (r == -1) {
Pavol Vican4d084512016-09-29 16:38:12 +02001540 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001541 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001542 }
1543 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001544 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001545 while (stack.index) {
1546 op = iff_stack_pop(&stack);
1547 iff_setop(iffeat_expr->expr, op, expr_size--);
1548 }
1549
1550 if (++expr_size || ++f_size) {
1551 /* not all expected operators and operands found */
1552 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1553 rc = EXIT_FAILURE;
1554 } else {
1555 rc = EXIT_SUCCESS;
1556 }
1557
1558error:
1559 /* cleanup */
1560 iff_stack_clean(&stack);
1561
1562 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001563}
1564
1565/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001566 * @brief Resolve (find) a data node based on a schema-nodeid.
1567 *
1568 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1569 * module).
1570 *
1571 */
1572struct lyd_node *
1573resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1574{
1575 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001576 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001577 const struct lys_node *schema = NULL;
1578
1579 assert(nodeid && start);
1580
1581 if (nodeid[0] == '/') {
1582 return NULL;
1583 }
1584
1585 str = p = strdup(nodeid);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001586 LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001587
Michal Vasko3edeaf72016-02-11 13:17:43 +01001588 while (p) {
1589 token = p;
1590 p = strchr(p, '/');
1591 if (p) {
1592 *p = '\0';
1593 p++;
1594 }
1595
Radek Krejci5da4eb62016-04-08 14:45:51 +02001596 if (p) {
1597 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001598 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001599 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001600 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001601 result = NULL;
1602 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001603 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001604
Radek Krejci5da4eb62016-04-08 14:45:51 +02001605 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1606 continue;
1607 }
1608 } else {
1609 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001610 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001611 || !schema) {
1612 result = NULL;
1613 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001614 }
1615 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001616 LY_TREE_FOR(result ? result->child : start, iter) {
1617 if (iter->schema == schema) {
1618 /* move in data tree according to returned schema */
1619 result = iter;
1620 break;
1621 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001622 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001623 if (!iter) {
1624 /* instance not found */
1625 result = NULL;
1626 break;
1627 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001628 }
1629 free(str);
1630
1631 return result;
1632}
1633
Radek Krejci1a9c3612017-04-24 14:49:43 +02001634int
Michal Vasko50576712017-07-28 12:28:33 +02001635schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name,
1636 int mod_name_len, const char *name, int nam_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001637{
1638 const struct lys_module *prefix_mod;
1639
Michal Vaskocdb3f062018-02-01 09:55:06 +01001640 /* handle special names */
1641 if (name[0] == '*') {
1642 return 2;
1643 } else if (name[0] == '.') {
1644 return 3;
1645 }
1646
Michal Vasko50576712017-07-28 12:28:33 +02001647 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001648 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001649 return 1;
1650 }
1651
Radek Krejcibdf92362016-04-08 14:43:34 +02001652 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001653 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001654 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001655 if (!prefix_mod) {
1656 return -1;
1657 }
1658 } else {
1659 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001660 }
1661 if (prefix_mod != lys_node_module(sibling)) {
1662 return 1;
1663 }
1664
Michal Vasko50576712017-07-28 12:28:33 +02001665 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001666 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001667}
1668
Michal Vasko50576712017-07-28 12:28:33 +02001669/* keys do not have to be ordered and do not have to be all of them */
1670static int
1671resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node,
1672 const struct lys_module *cur_module, int *nodeid_end)
1673{
1674 int mod_len, nam_len, has_predicate, r, i;
1675 const char *model, *name;
1676 struct lys_node_list *list;
1677
1678 if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1679 return 1;
1680 }
1681
1682 list = (struct lys_node_list *)node;
1683 do {
1684 r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate);
1685 if (r < 1) {
1686 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
1687 return -1;
1688 }
1689 nodeid += r;
1690
1691 if (node->nodetype == LYS_LEAFLIST) {
1692 /* just check syntax */
1693 if (model || !name || (name[0] != '.') || has_predicate) {
1694 return 1;
1695 }
1696 break;
1697 } else {
1698 /* check the key */
1699 for (i = 0; i < list->keys_size; ++i) {
1700 if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) {
1701 continue;
1702 }
1703 if (model) {
1704 if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len)
1705 || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) {
1706 continue;
1707 }
1708 } else {
1709 if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) {
1710 continue;
1711 }
1712 }
1713
1714 /* match */
1715 break;
1716 }
1717
1718 if (i == list->keys_size) {
1719 return 1;
1720 }
1721 }
1722 } while (has_predicate);
1723
1724 if (!nodeid[0]) {
1725 *nodeid_end = 1;
1726 }
1727 return 0;
1728}
1729
Michal Vasko97234262018-02-01 09:53:01 +01001730/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001731 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001732int
Michal Vasko97234262018-02-01 09:53:01 +01001733resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001734 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001735{
Michal Vaskobb520442017-05-23 10:55:18 +02001736 const char *name, *mod_name, *id;
Michal Vasko97234262018-02-01 09:53:01 +01001737 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001738 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001739 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001740 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001741 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001742 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001743
Michal Vasko97234262018-02-01 09:53:01 +01001744 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001745 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001746
Michal Vasko50576712017-07-28 12:28:33 +02001747 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001748 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001749 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001750 id = nodeid;
1751
Michal Vasko50576712017-07-28 12:28:33 +02001752 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1753 (extended ? &all_desc : NULL), extended);
1754 if (r < 1) {
1755 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
1756 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001757 }
1758 id += r;
1759
Michal Vasko97234262018-02-01 09:53:01 +01001760 if (is_relative && !start_parent) {
Michal Vasko50576712017-07-28 12:28:33 +02001761 LOGINT;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001762 return -1;
1763 }
1764
1765 /* descendant-schema-nodeid */
1766 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001767 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001768
Michal Vasko3edeaf72016-02-11 13:17:43 +01001769 /* absolute-schema-nodeid */
1770 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001771 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001772 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001773 str = strndup(mod_name, mod_name_len);
1774 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1775 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001776 return -1;
1777 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001778 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001779 }
1780
1781 while (1) {
1782 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001783 last_aug = NULL;
1784
1785 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001786 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1787 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001788 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001789 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001790 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001791 str = strndup(mod_name, mod_name_len);
1792 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1793 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001794 return -1;
1795 }
1796 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001797 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001798 * because this module may be not implemented and it augments something in another module and
1799 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001800 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001801 }
1802
1803 /* if the module is implemented, all the augments will be connected */
Michal Vasko50576712017-07-28 12:28:33 +02001804 if (!aux_mod->implemented && !extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001805get_next_augment:
1806 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1807 }
1808 }
1809
1810 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vasko5b997902017-04-03 14:16:22 +02001811 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) {
Michal Vasko50576712017-07-28 12:28:33 +02001812 r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len);
1813
1814 /* resolve predicate */
1815 if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) {
1816 r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end);
1817 if (r == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001818 continue;
Michal Vasko50576712017-07-28 12:28:33 +02001819 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001820 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001821 }
Michal Vasko50576712017-07-28 12:28:33 +02001822 } else if (!id[0]) {
1823 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001824 }
Michal Vasko50576712017-07-28 12:28:33 +02001825
1826 if (r == 0) {
1827 /* one matching result */
1828 if (nodeid_end) {
1829 *ret = ly_set_new();
1830 LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1);
1831 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1832 } else {
1833 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1834 return -1;
1835 }
1836 start_parent = sibling;
1837 }
1838 break;
1839 } else if (r == 1) {
1840 continue;
1841 } else if (r == 2) {
1842 /* "*" */
1843 if (!*ret) {
1844 *ret = ly_set_new();
1845 LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1);
1846 }
1847 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1848 if (all_desc) {
1849 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1850 if (elem != sibling) {
1851 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1852 }
1853
1854 LY_TREE_DFS_END(sibling, next, elem);
1855 }
1856 }
1857 } else if (r == 3) {
1858 /* "." */
1859 if (!*ret) {
1860 *ret = ly_set_new();
1861 LY_CHECK_ERR_RETURN(!*ret, LOGMEM, -1);
1862 ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST);
1863 }
1864 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1865 if (all_desc) {
1866 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1867 if (elem != sibling) {
1868 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1869 }
1870
1871 LY_TREE_DFS_END(sibling, next, elem);
1872 }
1873 }
1874 } else {
1875 LOGINT;
1876 return -1;
1877 }
1878 }
1879
1880 /* skip predicate */
1881 if (extended && has_predicate) {
1882 while (id[0] == '[') {
1883 id = strchr(id, ']');
1884 if (!id) {
1885 LOGINT;
1886 return -1;
1887 }
1888 ++id;
1889 }
1890 }
1891
1892 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
1893 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001894 }
1895
1896 /* no match */
1897 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02001898 if (last_aug) {
1899 /* it still could be in another augment */
1900 goto get_next_augment;
1901 }
Michal Vasko50576712017-07-28 12:28:33 +02001902 if (no_node_error) {
1903 str = strndup(nodeid, (name - nodeid) + nam_len);
1904 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1905 free(str);
1906 return -1;
1907 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01001908 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001909 return EXIT_SUCCESS;
1910 }
1911
Michal Vasko50576712017-07-28 12:28:33 +02001912 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1913 (extended ? &all_desc : NULL), extended);
1914 if (r < 1) {
1915 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
1916 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001917 }
1918 id += r;
1919 }
1920
1921 /* cannot get here */
1922 LOGINT;
1923 return -1;
1924}
1925
Radek Krejcif3c71de2016-04-11 12:45:46 +02001926/* unique, refine,
1927 * >0 - unexpected char on position (ret - 1),
1928 * 0 - ok (but ret can still be NULL),
1929 * -1 - error,
1930 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001931int
1932resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02001933 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001934{
1935 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001936 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001937 int r, nam_len, mod_name_len, is_relative = -1;
1938 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001939 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001940
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001941 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01001942 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01001943
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001944 if (!start) {
1945 /* leaf not found */
1946 return 0;
1947 }
1948
Michal Vasko3edeaf72016-02-11 13:17:43 +01001949 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02001950 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001951
Michal Vasko50576712017-07-28 12:28:33 +02001952 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 +01001953 return ((id - nodeid) - r) + 1;
1954 }
1955 id += r;
1956
1957 if (!is_relative) {
1958 return -1;
1959 }
1960
Michal Vasko24476fa2017-03-08 12:33:48 +01001961 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02001962 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01001963 start_parent = lys_parent(start_parent);
1964 }
1965
Michal Vasko3edeaf72016-02-11 13:17:43 +01001966 while (1) {
1967 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01001968 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vasko74a991b2017-03-31 09:17:22 +02001969 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) {
Michal Vasko50576712017-07-28 12:28:33 +02001970 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
1971 if (r == 0) {
1972 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001973 if (!(sibling->nodetype & ret_nodetype)) {
1974 /* wrong node type, too bad */
1975 continue;
1976 }
1977 *ret = sibling;
1978 return EXIT_SUCCESS;
1979 }
Michal Vasko50576712017-07-28 12:28:33 +02001980 start_parent = sibling;
1981 break;
1982 } else if (r == 1) {
1983 continue;
1984 } else {
1985 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001986 }
1987 }
1988
1989 /* no match */
1990 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001991 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001992 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001993 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1994 *ret = NULL;
1995 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001996 }
1997
Michal Vasko50576712017-07-28 12:28:33 +02001998 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 +01001999 return ((id - nodeid) - r) + 1;
2000 }
2001 id += r;
2002 }
2003
2004 /* cannot get here */
2005 LOGINT;
2006 return -1;
2007}
2008
2009/* choice default */
2010int
2011resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
2012{
2013 /* cannot actually be a path */
2014 if (strchr(nodeid, '/')) {
2015 return -1;
2016 }
2017
Michal Vaskodc300b02017-04-07 14:09:20 +02002018 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002019}
2020
2021/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2022static int
2023resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
2024{
2025 const struct lys_module *module;
2026 const char *mod_prefix, *name;
2027 int i, mod_prefix_len, nam_len;
2028
2029 /* parse the identifier, it must be parsed on one call */
Michal Vasko50576712017-07-28 12:28:33 +02002030 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 +01002031 return -i + 1;
2032 }
2033
Michal Vasko921eb6b2017-10-13 10:01:39 +02002034 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002035 if (!module) {
2036 return -1;
2037 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002038 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002039 start = module->data;
2040 }
2041
2042 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2043
2044 return EXIT_SUCCESS;
2045}
2046
2047int
2048resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
2049 const struct lys_node **ret)
2050{
2051 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002052 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002053 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002054 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002055
2056 assert(nodeid && module && ret);
2057 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
2058
2059 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01002060 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002061
Michal Vasko50576712017-07-28 12:28:33 +02002062 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 +01002063 return ((id - nodeid) - r) + 1;
2064 }
2065 id += r;
2066
2067 if (is_relative) {
2068 return -1;
2069 }
2070
Michal Vasko921eb6b2017-10-13 10:01:39 +02002071 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002072 if (!abs_start_mod) {
2073 return -1;
2074 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002075
2076 while (1) {
2077 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002078 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vasko3edeaf72016-02-11 13:17:43 +01002079 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
Michal Vasko50576712017-07-28 12:28:33 +02002080 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2081 if (r == 0) {
2082 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002083 if (!(sibling->nodetype & ret_nodetype)) {
2084 /* wrong node type, too bad */
2085 continue;
2086 }
2087 *ret = sibling;
2088 return EXIT_SUCCESS;
2089 }
Michal Vasko50576712017-07-28 12:28:33 +02002090 start_parent = sibling;
2091 break;
2092 } else if (r == 1) {
2093 continue;
2094 } else {
2095 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002096 }
2097 }
2098
2099 /* no match */
2100 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002101 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002102 return EXIT_SUCCESS;
2103 }
2104
Michal Vasko50576712017-07-28 12:28:33 +02002105 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 +01002106 return ((id - nodeid) - r) + 1;
2107 }
2108 id += r;
2109 }
2110
2111 /* cannot get here */
2112 LOGINT;
2113 return -1;
2114}
2115
Michal Vaskoe733d682016-03-14 09:08:27 +01002116static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002117resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002118{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002119 const char *mod_name, *name;
2120 int mod_name_len, nam_len, has_predicate, i;
2121 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002122
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002123 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 +02002124 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002125 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002126 return -1;
2127 }
2128
2129 predicate += i;
2130 *parsed += i;
2131
Michal Vasko58c2aab2017-01-05 10:02:05 +01002132 if (!isdigit(name[0])) {
2133 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002134 key = (struct lys_node *)list->keys[i];
2135 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002136 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002137 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002138 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002139
Michal Vasko58c2aab2017-01-05 10:02:05 +01002140 if (i == list->keys_size) {
2141 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2142 return -1;
2143 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002144 }
2145
2146 /* more predicates? */
2147 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002148 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002149 }
2150
2151 return 0;
2152}
2153
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002154/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002155const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002156resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002157{
Michal Vasko10728b52016-04-07 14:26:29 +02002158 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002159 const char *name, *mod_name, *id;
Michal Vaskob3744402017-08-03 14:23:58 +02002160 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002161 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002162 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002163 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002164
Michal Vasko3547c532016-03-14 09:40:50 +01002165 assert(nodeid && (ctx || start));
2166 if (!ctx) {
2167 ctx = start->module->ctx;
2168 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002169
2170 id = nodeid;
2171
Michal Vasko50576712017-07-28 12:28:33 +02002172 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002173 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002174 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002175 }
2176 id += r;
2177
2178 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002179 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002180 start_parent = start;
2181 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2182 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002183 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002184 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002185 } else {
2186 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002187 str = strndup(nodeid, (name + nam_len) - nodeid);
2188 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
2189 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002190 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02002191 } else if (mod_name_len > LY_BUF_SIZE - 1) {
2192 LOGINT;
2193 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002194 }
2195
Michal Vasko971a3ca2016-04-01 13:09:29 +02002196 if (ly_buf_used && module_name[0]) {
2197 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2198 }
2199 ly_buf_used++;
2200
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002201 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002202 module_name[mod_name_len] = '\0';
Radek Krejcidfb00d62017-09-06 09:39:35 +02002203 module = ly_ctx_get_module(ctx, module_name, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002204
2205 if (buf_backup) {
2206 /* return previous internal buffer content */
2207 strcpy(module_name, buf_backup);
2208 free(buf_backup);
2209 buf_backup = NULL;
2210 }
2211 ly_buf_used--;
2212
Michal Vaskof68a49e2017-08-14 13:23:37 +02002213 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002214 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2215 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2216 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002217 return NULL;
2218 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002219 start_parent = NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002220
2221 /* now it's as if there was no module name */
2222 mod_name = NULL;
2223 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002224 }
2225
Michal Vaskof68a49e2017-08-14 13:23:37 +02002226 prev_mod = module;
2227
Michal Vasko3edeaf72016-02-11 13:17:43 +01002228 while (1) {
2229 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002230 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002231 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002232 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002233 /* output check */
2234 for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2235 if (parent) {
2236 if (output && (parent->nodetype == LYS_INPUT)) {
2237 continue;
2238 } else if (!output && (parent->nodetype == LYS_OUTPUT)) {
2239 continue;
2240 }
2241 }
2242
Michal Vasko3edeaf72016-02-11 13:17:43 +01002243 /* module check */
2244 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002245 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002246 LOGINT;
2247 return NULL;
2248 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002249
2250 if (ly_buf_used && module_name[0]) {
2251 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2252 }
2253 ly_buf_used++;
2254
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002255 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01002256 module_name[mod_name_len] = '\0';
2257 /* will also find an augment module */
Radek Krejcidfb00d62017-09-06 09:39:35 +02002258 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002259
2260 if (buf_backup) {
2261 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002262 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002263 free(buf_backup);
2264 buf_backup = NULL;
2265 }
2266 ly_buf_used--;
2267
Michal Vasko3edeaf72016-02-11 13:17:43 +01002268 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002269 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2270 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2271 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002272 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002273 }
2274 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002275 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002276 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002277 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002278 continue;
2279 }
2280
Michal Vaskoe733d682016-03-14 09:08:27 +01002281 /* do we have some predicates on it? */
2282 if (has_predicate) {
2283 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002284 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002285 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002286 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2287 return NULL;
2288 }
2289 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002290 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002291 return NULL;
2292 }
2293 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01002294 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002295 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002296 }
2297 id += r;
2298 }
2299
Michal Vasko3edeaf72016-02-11 13:17:43 +01002300 /* the result node? */
2301 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002302 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002303 }
2304
Michal Vaskodc300b02017-04-07 14:09:20 +02002305 /* move down the tree, if possible */
2306 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
2307 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2308 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002309 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002310 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002311
2312 /* update prev mod */
2313 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002314 break;
2315 }
2316 }
2317
2318 /* no match */
2319 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002320 str = strndup(nodeid, (name + nam_len) - nodeid);
2321 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2322 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002323 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002324 }
2325
Michal Vasko50576712017-07-28 12:28:33 +02002326 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002327 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002328 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002329 }
2330 id += r;
2331 }
2332
2333 /* cannot get here */
2334 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01002335 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002336}
2337
Michal Vasko22448d32016-03-16 13:17:29 +01002338static int
Michal Vasko58c2aab2017-01-05 10:02:05 +01002339resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node,
Michal Vasko50576712017-07-28 12:28:33 +02002340 int position, int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002341{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002342 const char *mod_name, *name, *value, *key_val;
2343 int mod_name_len, nam_len, val_len, has_predicate = 1, r;
Michal Vasko22448d32016-03-16 13:17:29 +01002344 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002345 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01002346
Radek Krejci61a86c62016-03-24 11:06:44 +01002347 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002348 assert(node->schema->nodetype == LYS_LIST);
2349
Michal Vasko53adfc72017-01-06 10:39:10 +01002350 /* is the predicate a number? */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002351 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 +01002352 || !strncmp(name, ".", nam_len)) {
2353 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
2354 return -1;
2355 }
2356
2357 if (isdigit(name[0])) {
2358 if (position == atoi(name)) {
2359 /* match */
2360 *parsed += r;
2361 return 0;
2362 } else {
2363 /* not a match */
2364 return 1;
2365 }
2366 }
2367
2368 if (!((struct lys_node_list *)node->schema)->keys_size) {
2369 /* no keys in schema - causes an error later */
2370 return 0;
2371 }
2372
Michal Vaskof29903d2016-04-18 13:13:10 +02002373 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002374 if (!key) {
2375 /* it is not a position, so we need a key for it to be a match */
2376 return 1;
2377 }
2378
2379 /* go through all the keys */
2380 i = 0;
2381 goto check_parsed_values;
2382
2383 for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
Michal Vasko22448d32016-03-16 13:17:29 +01002384 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002385 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002386 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002387 }
2388
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002389 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 +02002390 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002391 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002392 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002393 }
2394
Michal Vasko53adfc72017-01-06 10:39:10 +01002395check_parsed_values:
Michal Vasko22448d32016-03-16 13:17:29 +01002396 predicate += r;
2397 *parsed += r;
2398
Michal Vaskof29903d2016-04-18 13:13:10 +02002399 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002400 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002401 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002402 }
2403
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002404 if (mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002405 /* specific module, check that the found key is from that module */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002406 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len)
2407 || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) {
2408 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2409 return -1;
2410 }
Michal Vasko50576712017-07-28 12:28:33 +02002411
2412 /* but if the module is the same as the parent, it should have been omitted */
2413 if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) {
2414 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2415 return -1;
2416 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002417 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002418 /* no module, so it must be the same as the list (parent) */
2419 if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002420 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2421 return -1;
2422 }
2423 }
2424
Michal Vasko9ba34de2016-12-07 12:21:19 +01002425 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002426 if ((key->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002427 && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name))
2428 && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002429 key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1;
2430 } else {
2431 key_val = key->value_str;
2432 }
2433
Michal Vasko22448d32016-03-16 13:17:29 +01002434 /* value does not match */
Michal Vasko9ba34de2016-12-07 12:21:19 +01002435 if (strncmp(key_val, value, val_len) || key_val[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002436 return 1;
2437 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002438
2439 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002440 }
2441
2442 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002443 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002444 return -1;
2445 }
2446
2447 return 0;
2448}
2449
Radek Krejci45826012016-08-24 15:07:57 +02002450/**
2451 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2452 *
2453 * @param[in] nodeid Node data path to find
2454 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2455 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2456 * @param[out] parsed Number of characters processed in \p id
2457 * @return The closes parent (or the node itself) from the path
2458 */
Michal Vasko22448d32016-03-16 13:17:29 +01002459struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002460resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2461 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002462{
Michal Vasko10728b52016-04-07 14:26:29 +02002463 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002464 const char *id, *mod_name, *name, *pred_name, *data_val;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002465 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002466 int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002467 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002468 struct lyd_node_leaf_list *llist;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002469 const struct lys_module *prefix_mod, *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002470 struct ly_ctx *ctx;
2471
2472 assert(nodeid && start && parsed);
2473
2474 ctx = start->schema->module->ctx;
2475 id = nodeid;
2476
Michal Vasko50576712017-07-28 12:28:33 +02002477 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002478 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002479 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002480 return NULL;
2481 }
2482 id += r;
2483 /* add it to parsed only after the data node was actually found */
2484 last_parsed = r;
2485
2486 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002487 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002488 start = start->child;
2489 } else {
2490 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002491 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002492 }
2493
2494 while (1) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002495 list_instance_position = 0;
2496
Michal Vasko22448d32016-03-16 13:17:29 +01002497 LY_TREE_FOR(start, sibling) {
Michal Vasko945b96b2016-10-18 11:49:12 +02002498 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
Michal Vasko2411b942016-03-23 13:50:03 +01002499 if (lys_parent(sibling->schema)) {
2500 if (options & LYD_PATH_OPT_OUTPUT) {
2501 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002502 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002503 *parsed = -1;
2504 return NULL;
2505 }
2506 } else {
2507 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002508 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002509 *parsed = -1;
2510 return NULL;
2511 }
2512 }
2513 }
2514
Michal Vasko22448d32016-03-16 13:17:29 +01002515 /* name match */
2516 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2517
2518 /* module check */
2519 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002520 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01002521 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002522 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002523 return NULL;
2524 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002525
2526 if (ly_buf_used && module_name[0]) {
2527 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2528 }
2529 ly_buf_used++;
2530
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002531 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01002532 module_name[mod_name_len] = '\0';
2533 /* will also find an augment module */
Radek Krejcidfb00d62017-09-06 09:39:35 +02002534 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002535
2536 if (buf_backup) {
2537 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002538 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002539 free(buf_backup);
2540 buf_backup = NULL;
2541 }
2542 ly_buf_used--;
2543
Michal Vasko22448d32016-03-16 13:17:29 +01002544 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002545 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2546 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2547 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002548 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002549 return NULL;
2550 }
2551 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002552 prefix_mod = prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002553 }
Michal Vasko1adc7242016-11-16 11:05:28 +01002554 if (prefix_mod != lyd_node_module(sibling)) {
Michal Vasko22448d32016-03-16 13:17:29 +01002555 continue;
2556 }
2557
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002558 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002559 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002560 llist = (struct lyd_node_leaf_list *)sibling;
2561
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002562 last_has_pred = 0;
Michal Vaskof0a50972016-10-19 11:33:55 +02002563 if (has_predicate) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002564 if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value,
2565 &llval_len, &last_has_pred)) < 1) {
Michal Vaskof0a50972016-10-19 11:33:55 +02002566 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2567 *parsed = -1;
2568 return NULL;
2569 }
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002570 if ((pred_name[0] != '.') || (pred_name_len != 1)) {
2571 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1);
2572 *parsed = -1;
2573 return NULL;
2574 }
Michal Vaskof0a50972016-10-19 11:33:55 +02002575 } else {
2576 r = 0;
2577 if (llist_value) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002578 llval_len = strlen(llist_value);
Michal Vaskof0a50972016-10-19 11:33:55 +02002579 }
2580 }
2581
Michal Vasko21b90ce2017-09-19 09:38:27 +02002582 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vaskod6d51292017-09-22 14:30:48 +02002583 if (llist_value && !strchr(llist_value, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002584 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2585 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002586 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2587 } else {
2588 data_val = llist->value_str;
2589 }
2590
2591 if ((!llist_value && data_val && data_val[0])
2592 || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002593 continue;
2594 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002595
Michal Vaskof0a50972016-10-19 11:33:55 +02002596 id += r;
2597 last_parsed += r;
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002598 has_predicate = last_has_pred;
Michal Vaskof0a50972016-10-19 11:33:55 +02002599
Radek Krejci45826012016-08-24 15:07:57 +02002600 } else if (sibling->schema->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002601 /* 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 +01002602 if (!has_predicate) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002603 /* none match */
2604 return last_match;
Michal Vasko22448d32016-03-16 13:17:29 +01002605 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01002606
2607 ++list_instance_position;
2608 r = 0;
Michal Vasko50576712017-07-28 12:28:33 +02002609 ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r);
Michal Vasko22448d32016-03-16 13:17:29 +01002610 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002611 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002612 return NULL;
2613 } else if (ret == 1) {
2614 /* this list instance does not match */
2615 continue;
2616 }
2617 id += r;
2618 last_parsed += r;
2619 }
2620
2621 *parsed += last_parsed;
2622
2623 /* the result node? */
2624 if (!id[0]) {
2625 return sibling;
2626 }
2627
2628 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002629 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002630 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002631 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002632 return NULL;
2633 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002634 last_match = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002635 prev_mod = lyd_node_module(sibling);
Michal Vasko22448d32016-03-16 13:17:29 +01002636 start = sibling->child;
Michal Vasko22448d32016-03-16 13:17:29 +01002637 break;
2638 }
2639 }
2640
2641 /* no match, return last match */
2642 if (!sibling) {
2643 return last_match;
2644 }
2645
Michal Vasko50576712017-07-28 12:28:33 +02002646 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002647 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002648 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002649 return NULL;
2650 }
2651 id += r;
2652 last_parsed = r;
2653 }
2654
2655 /* cannot get here */
2656 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002657 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002658 return NULL;
2659}
2660
Michal Vasko3edeaf72016-02-11 13:17:43 +01002661/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002662 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002663 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002664 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02002665 * @param[in] str_restr Restriction as a string.
2666 * @param[in] type Type of the restriction.
2667 * @param[out] ret Final interval structure that starts with
2668 * the interval of the initial type, continues with intervals
2669 * of any superior types derived from the initial one, and
2670 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002671 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002672 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002673 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002674int
Michal Vaskoaeb51802016-04-11 10:58:47 +02002675resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002676{
2677 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002678 int kind;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002679 int64_t local_smin, local_smax, local_fmin, local_fmax;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002680 uint64_t local_umin, local_umax;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002681 uint8_t local_fdig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002682 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002683 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002684
2685 switch (type->base) {
2686 case LY_TYPE_BINARY:
2687 kind = 0;
2688 local_umin = 0;
2689 local_umax = 18446744073709551615UL;
2690
2691 if (!str_restr && type->info.binary.length) {
2692 str_restr = type->info.binary.length->expr;
2693 }
2694 break;
2695 case LY_TYPE_DEC64:
2696 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002697 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2698 local_fmax = __INT64_C(9223372036854775807);
2699 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002700
2701 if (!str_restr && type->info.dec64.range) {
2702 str_restr = type->info.dec64.range->expr;
2703 }
2704 break;
2705 case LY_TYPE_INT8:
2706 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002707 local_smin = __INT64_C(-128);
2708 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002709
2710 if (!str_restr && type->info.num.range) {
2711 str_restr = type->info.num.range->expr;
2712 }
2713 break;
2714 case LY_TYPE_INT16:
2715 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002716 local_smin = __INT64_C(-32768);
2717 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002718
2719 if (!str_restr && type->info.num.range) {
2720 str_restr = type->info.num.range->expr;
2721 }
2722 break;
2723 case LY_TYPE_INT32:
2724 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002725 local_smin = __INT64_C(-2147483648);
2726 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002727
2728 if (!str_restr && type->info.num.range) {
2729 str_restr = type->info.num.range->expr;
2730 }
2731 break;
2732 case LY_TYPE_INT64:
2733 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002734 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2735 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002736
2737 if (!str_restr && type->info.num.range) {
2738 str_restr = type->info.num.range->expr;
2739 }
2740 break;
2741 case LY_TYPE_UINT8:
2742 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002743 local_umin = __UINT64_C(0);
2744 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002745
2746 if (!str_restr && type->info.num.range) {
2747 str_restr = type->info.num.range->expr;
2748 }
2749 break;
2750 case LY_TYPE_UINT16:
2751 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002752 local_umin = __UINT64_C(0);
2753 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002754
2755 if (!str_restr && type->info.num.range) {
2756 str_restr = type->info.num.range->expr;
2757 }
2758 break;
2759 case LY_TYPE_UINT32:
2760 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002761 local_umin = __UINT64_C(0);
2762 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002763
2764 if (!str_restr && type->info.num.range) {
2765 str_restr = type->info.num.range->expr;
2766 }
2767 break;
2768 case LY_TYPE_UINT64:
2769 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002770 local_umin = __UINT64_C(0);
2771 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002772
2773 if (!str_restr && type->info.num.range) {
2774 str_restr = type->info.num.range->expr;
2775 }
2776 break;
2777 case LY_TYPE_STRING:
2778 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002779 local_umin = __UINT64_C(0);
2780 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002781
2782 if (!str_restr && type->info.str.length) {
2783 str_restr = type->info.str.length->expr;
2784 }
2785 break;
2786 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002787 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002788 }
2789
2790 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002791 if (type->der) {
2792 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002793 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002794 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002795 assert(!intv || (intv->kind == kind));
2796 }
2797
2798 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002799 /* we do not have any restriction, return superior ones */
2800 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002801 return EXIT_SUCCESS;
2802 }
2803
2804 /* adjust local min and max */
2805 if (intv) {
2806 tmp_intv = intv;
2807
2808 if (kind == 0) {
2809 local_umin = tmp_intv->value.uval.min;
2810 } else if (kind == 1) {
2811 local_smin = tmp_intv->value.sval.min;
2812 } else if (kind == 2) {
2813 local_fmin = tmp_intv->value.fval.min;
2814 }
2815
2816 while (tmp_intv->next) {
2817 tmp_intv = tmp_intv->next;
2818 }
2819
2820 if (kind == 0) {
2821 local_umax = tmp_intv->value.uval.max;
2822 } else if (kind == 1) {
2823 local_smax = tmp_intv->value.sval.max;
2824 } else if (kind == 2) {
2825 local_fmax = tmp_intv->value.fval.max;
2826 }
2827 }
2828
2829 /* finally parse our restriction */
2830 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002831 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002832 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002833 if (!tmp_local_intv) {
2834 assert(!local_intv);
2835 local_intv = malloc(sizeof *local_intv);
2836 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002837 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002838 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002839 tmp_local_intv = tmp_local_intv->next;
2840 }
Radek Krejciaa1303c2017-05-31 13:57:37 +02002841 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002842
2843 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002844 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002845 tmp_local_intv->next = NULL;
2846
2847 /* min */
2848 ptr = seg_ptr;
2849 while (isspace(ptr[0])) {
2850 ++ptr;
2851 }
2852 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2853 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002854 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002855 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002856 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002857 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002858 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
2859 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2860 goto error;
2861 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002862 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002863 } else if (!strncmp(ptr, "min", 3)) {
2864 if (kind == 0) {
2865 tmp_local_intv->value.uval.min = local_umin;
2866 } else if (kind == 1) {
2867 tmp_local_intv->value.sval.min = local_smin;
2868 } else if (kind == 2) {
2869 tmp_local_intv->value.fval.min = local_fmin;
2870 }
2871
2872 ptr += 3;
2873 } else if (!strncmp(ptr, "max", 3)) {
2874 if (kind == 0) {
2875 tmp_local_intv->value.uval.min = local_umax;
2876 } else if (kind == 1) {
2877 tmp_local_intv->value.sval.min = local_smax;
2878 } else if (kind == 2) {
2879 tmp_local_intv->value.fval.min = local_fmax;
2880 }
2881
2882 ptr += 3;
2883 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002884 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002885 }
2886
2887 while (isspace(ptr[0])) {
2888 ptr++;
2889 }
2890
2891 /* no interval or interval */
2892 if ((ptr[0] == '|') || !ptr[0]) {
2893 if (kind == 0) {
2894 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2895 } else if (kind == 1) {
2896 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2897 } else if (kind == 2) {
2898 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2899 }
2900 } else if (!strncmp(ptr, "..", 2)) {
2901 /* skip ".." */
2902 ptr += 2;
2903 while (isspace(ptr[0])) {
2904 ++ptr;
2905 }
2906
2907 /* max */
2908 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2909 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002910 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002911 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002912 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002913 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002914 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
2915 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2916 goto error;
2917 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002918 }
2919 } else if (!strncmp(ptr, "max", 3)) {
2920 if (kind == 0) {
2921 tmp_local_intv->value.uval.max = local_umax;
2922 } else if (kind == 1) {
2923 tmp_local_intv->value.sval.max = local_smax;
2924 } else if (kind == 2) {
2925 tmp_local_intv->value.fval.max = local_fmax;
2926 }
2927 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002928 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002929 }
2930 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002931 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002932 }
2933
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002934 /* check min and max in correct order*/
2935 if (kind == 0) {
2936 /* current segment */
2937 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2938 goto error;
2939 }
2940 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2941 goto error;
2942 }
2943 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02002944 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002945 goto error;
2946 }
2947 } else if (kind == 1) {
2948 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
2949 goto error;
2950 }
2951 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
2952 goto error;
2953 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002954 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002955 goto error;
2956 }
2957 } else if (kind == 2) {
2958 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
2959 goto error;
2960 }
2961 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
2962 goto error;
2963 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002964 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002965 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002966 goto error;
2967 }
2968 }
2969
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002970 /* next segment (next OR) */
2971 seg_ptr = strchr(seg_ptr, '|');
2972 if (!seg_ptr) {
2973 break;
2974 }
2975 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002976 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002977 }
2978
2979 /* check local restrictions against superior ones */
2980 if (intv) {
2981 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002982 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002983
2984 while (tmp_local_intv && tmp_intv) {
2985 /* reuse local variables */
2986 if (kind == 0) {
2987 local_umin = tmp_local_intv->value.uval.min;
2988 local_umax = tmp_local_intv->value.uval.max;
2989
2990 /* it must be in this interval */
2991 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2992 /* this interval is covered, next one */
2993 if (local_umax <= tmp_intv->value.uval.max) {
2994 tmp_local_intv = tmp_local_intv->next;
2995 continue;
2996 /* ascending order of restrictions -> fail */
2997 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002998 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002999 }
3000 }
3001 } else if (kind == 1) {
3002 local_smin = tmp_local_intv->value.sval.min;
3003 local_smax = tmp_local_intv->value.sval.max;
3004
3005 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
3006 if (local_smax <= tmp_intv->value.sval.max) {
3007 tmp_local_intv = tmp_local_intv->next;
3008 continue;
3009 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003010 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003011 }
3012 }
3013 } else if (kind == 2) {
3014 local_fmin = tmp_local_intv->value.fval.min;
3015 local_fmax = tmp_local_intv->value.fval.max;
3016
Michal Vasko4d1f0482016-09-19 14:35:06 +02003017 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02003018 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003019 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003020 tmp_local_intv = tmp_local_intv->next;
3021 continue;
3022 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003023 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003024 }
3025 }
3026 }
3027
3028 tmp_intv = tmp_intv->next;
3029 }
3030
3031 /* some interval left uncovered -> fail */
3032 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003033 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003034 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003035 }
3036
Michal Vaskoaeb51802016-04-11 10:58:47 +02003037 /* append the local intervals to all the intervals of the superior types, return it all */
3038 if (intv) {
3039 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
3040 tmp_intv->next = local_intv;
3041 } else {
3042 intv = local_intv;
3043 }
3044 *ret = intv;
3045
3046 return EXIT_SUCCESS;
3047
3048error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003049 while (intv) {
3050 tmp_intv = intv->next;
3051 free(intv);
3052 intv = tmp_intv;
3053 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003054 while (local_intv) {
3055 tmp_local_intv = local_intv->next;
3056 free(local_intv);
3057 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003058 }
3059
Michal Vaskoaeb51802016-04-11 10:58:47 +02003060 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003061}
3062
Michal Vasko730dfdf2015-08-11 14:48:05 +02003063/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003064 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3065 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003066 *
3067 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003068 * @param[in] mod_name Typedef name module name.
3069 * @param[in] module Main module.
3070 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003071 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003072 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003073 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003074 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003075int
Michal Vasko1e62a092015-12-01 12:27:20 +01003076resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3077 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003078{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003079 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003080 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003081 int tpdf_size;
3082
Michal Vasko1dca6882015-10-22 14:29:42 +02003083 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003084 /* no prefix, try built-in types */
3085 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003086 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003087 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003088 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003089 }
3090 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003091 }
3092 }
3093 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003094 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003095 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003096 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003097 }
3098 }
3099
Michal Vasko1dca6882015-10-22 14:29:42 +02003100 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003101 /* search in local typedefs */
3102 while (parent) {
3103 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003104 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003105 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3106 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003107 break;
3108
Radek Krejci76512572015-08-04 09:47:08 +02003109 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003110 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3111 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003112 break;
3113
Radek Krejci76512572015-08-04 09:47:08 +02003114 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003115 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3116 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003117 break;
3118
Radek Krejci76512572015-08-04 09:47:08 +02003119 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003120 case LYS_ACTION:
3121 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3122 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003123 break;
3124
Radek Krejci76512572015-08-04 09:47:08 +02003125 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003126 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3127 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003128 break;
3129
Radek Krejci76512572015-08-04 09:47:08 +02003130 case LYS_INPUT:
3131 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003132 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3133 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003134 break;
3135
3136 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003137 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003138 continue;
3139 }
3140
3141 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003142 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003143 match = &tpdf[i];
3144 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003145 }
3146 }
3147
Michal Vaskodcf98e62016-05-05 17:53:53 +02003148 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003149 }
Radek Krejcic071c542016-01-27 14:57:51 +01003150 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003151 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003152 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003153 if (!module) {
3154 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003155 }
3156 }
3157
3158 /* search in top level typedefs */
3159 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003160 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003161 match = &module->tpdf[i];
3162 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003163 }
3164 }
3165
3166 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003167 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003168 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003169 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 +02003170 match = &module->inc[i].submodule->tpdf[j];
3171 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003172 }
3173 }
3174 }
3175
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003176 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003177
3178check_leafref:
3179 if (ret) {
3180 *ret = match;
3181 }
3182 if (match->type.base == LY_TYPE_LEAFREF) {
3183 while (!match->type.info.lref.path) {
3184 match = match->type.der;
3185 assert(match);
3186 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003187 }
3188 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003189}
3190
Michal Vasko1dca6882015-10-22 14:29:42 +02003191/**
3192 * @brief Check the default \p value of the \p type. Logs directly.
3193 *
3194 * @param[in] type Type definition to use.
3195 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003196 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003197 *
3198 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3199 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003200static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003201check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003202{
Radek Krejcibad2f172016-08-02 11:04:15 +02003203 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003204 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003205 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003206 char *s;
Radek Krejci37b756f2016-01-18 10:15:03 +01003207 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02003208
Radek Krejci51673202016-11-01 17:00:32 +01003209 assert(value);
3210
Radek Krejcic13db382016-08-16 10:52:42 +02003211 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003212 /* the type was not resolved yet, nothing to do for now */
3213 return EXIT_FAILURE;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003214 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003215 /* do not check defaults in not implemented module's data */
3216 return EXIT_SUCCESS;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003217 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003218 /* identityrefs are checked when instantiated in data instead of typedef,
3219 * but in typedef the value has to be modified to include the prefix */
3220 if (*value) {
3221 if (strchr(*value, ':')) {
3222 dflt = transform_schema2json(module, *value);
3223 } else {
3224 /* default prefix of the module where the typedef is defined */
3225 asprintf(&s, "%s:%s", lys_main_module(module)->name, *value);
3226 dflt = lydict_insert_zc(module->ctx, s);
3227 }
3228 lydict_remove(module->ctx, *value);
3229 *value = dflt;
3230 }
3231 return EXIT_SUCCESS;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003232 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3233 /* leafref in typedef cannot be checked */
3234 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003235 }
3236
Radek Krejci51673202016-11-01 17:00:32 +01003237 dflt = *value;
3238 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003239 /* we do not have a new default value, so is there any to check even, in some base type? */
3240 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3241 if (base_tpdf->dflt) {
Radek Krejci51673202016-11-01 17:00:32 +01003242 dflt = base_tpdf->dflt;
Michal Vasko478c4652016-07-21 12:55:01 +02003243 break;
3244 }
3245 }
3246
Radek Krejci51673202016-11-01 17:00:32 +01003247 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003248 /* no default value, nothing to check, all is well */
3249 return EXIT_SUCCESS;
3250 }
3251
3252 /* 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)? */
3253 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003254 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003255 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
3256 return EXIT_SUCCESS;
3257 } else {
3258 /* check the default value from typedef, but use also the typedef's module
3259 * due to possible searching in imported modules which is expected in
3260 * typedef's module instead of module where the typedef is used */
3261 module = base_tpdf->module;
3262 }
3263 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003264 case LY_TYPE_INST:
3265 case LY_TYPE_LEAFREF:
3266 case LY_TYPE_BOOL:
3267 case LY_TYPE_EMPTY:
3268 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
3269 return EXIT_SUCCESS;
Radek Krejcibad2f172016-08-02 11:04:15 +02003270 case LY_TYPE_BITS:
3271 /* the default value must match the restricted list of values, if the type was restricted */
3272 if (type->info.bits.count) {
3273 break;
3274 }
3275 return EXIT_SUCCESS;
3276 case LY_TYPE_ENUM:
3277 /* the default value must match the restricted list of values, if the type was restricted */
3278 if (type->info.enums.count) {
3279 break;
3280 }
3281 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003282 case LY_TYPE_DEC64:
3283 if (type->info.dec64.range) {
3284 break;
3285 }
3286 return EXIT_SUCCESS;
3287 case LY_TYPE_BINARY:
3288 if (type->info.binary.length) {
3289 break;
3290 }
3291 return EXIT_SUCCESS;
3292 case LY_TYPE_INT8:
3293 case LY_TYPE_INT16:
3294 case LY_TYPE_INT32:
3295 case LY_TYPE_INT64:
3296 case LY_TYPE_UINT8:
3297 case LY_TYPE_UINT16:
3298 case LY_TYPE_UINT32:
3299 case LY_TYPE_UINT64:
3300 if (type->info.num.range) {
3301 break;
3302 }
3303 return EXIT_SUCCESS;
3304 case LY_TYPE_STRING:
3305 if (type->info.str.length || type->info.str.patterns) {
3306 break;
3307 }
3308 return EXIT_SUCCESS;
3309 case LY_TYPE_UNION:
3310 /* way too much trouble learning whether we need to check the default again, so just do it */
3311 break;
3312 default:
3313 LOGINT;
3314 return -1;
3315 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003316 } else if (type->base == LY_TYPE_EMPTY) {
3317 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3318 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
3319 return -1;
Michal Vasko478c4652016-07-21 12:55:01 +02003320 }
3321
Michal Vasko1dca6882015-10-22 14:29:42 +02003322 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003323 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003324 node.value_str = dflt;
Michal Vasko1dca6882015-10-22 14:29:42 +02003325 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003326
3327 if (tpdf) {
3328 node.schema = calloc(1, sizeof (struct lys_node_leaf));
3329 LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1);
3330 asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
3331 LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1);
3332 node.schema->module = module;
3333 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3334 } else {
3335 node.schema = (struct lys_node *)type->parent;
3336 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003337
Radek Krejci37b756f2016-01-18 10:15:03 +01003338 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003339 if (!type->info.lref.target) {
3340 ret = EXIT_FAILURE;
3341 goto finish;
3342 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003343 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003344 if (!ret) {
3345 /* adopt possibly changed default value to its canonical form */
3346 if (*value) {
3347 *value = dflt;
3348 }
3349 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003350 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01003351 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003352 /* possible forward reference */
3353 ret = 1;
Radek Krejcibad2f172016-08-02 11:04:15 +02003354 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003355 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003356 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3357 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3358 /* we have refined bits/enums */
3359 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
3360 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003361 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003362 }
3363 }
Radek Krejci51673202016-11-01 17:00:32 +01003364 } else {
3365 /* success - adopt canonical form from the node into the default value */
3366 if (dflt != node.value_str) {
3367 /* this can happen only if we have non-inherited default value,
3368 * inherited default values are already in canonical form */
3369 assert(dflt == *value);
3370 *value = node.value_str;
3371 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003372 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003373 }
3374
3375finish:
3376 if (node.value_type == LY_TYPE_BITS) {
3377 free(node.value.bit);
3378 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003379 if (tpdf) {
3380 free((char *)node.schema->name);
3381 free(node.schema);
3382 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003383
3384 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003385}
3386
Michal Vasko730dfdf2015-08-11 14:48:05 +02003387/**
3388 * @brief Check a key for mandatory attributes. Logs directly.
3389 *
3390 * @param[in] key The key to check.
3391 * @param[in] flags What flags to check.
3392 * @param[in] list The list of all the keys.
3393 * @param[in] index Index of the key in the key list.
3394 * @param[in] name The name of the keys.
3395 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003396 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003397 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003398 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003399static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003400check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003401{
Radek Krejciadb57612016-02-16 13:34:34 +01003402 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003403 char *dup = NULL;
3404 int j;
3405
3406 /* existence */
3407 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003408 if (name[len] != '\0') {
3409 dup = strdup(name);
Radek Krejciaa1303c2017-05-31 13:57:37 +02003410 LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003411 dup[len] = '\0';
3412 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003413 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003414 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003415 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003416 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003417 }
3418
3419 /* uniqueness */
3420 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003421 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003422 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003423 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003424 }
3425 }
3426
3427 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003428 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003429 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003430 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003431 }
3432
3433 /* type of the leaf is not built-in empty */
Radek Krejcic3738db2016-09-15 15:51:21 +02003434 if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003435 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003436 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003437 }
3438
3439 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003440 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003441 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003442 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003443 }
3444
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003445 /* key is not placed from augment */
3446 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003447 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01003448 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003449 return -1;
3450 }
3451
Radek Krejci3f21ada2016-08-01 13:34:31 +02003452 /* key is not when/if-feature -conditional */
3453 j = 0;
3454 if (key->when || (key->iffeature_size && (j = 1))) {
3455 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
Michal Vasko51e5c582017-01-19 14:16:39 +01003456 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003457 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003458 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003459 }
3460
Michal Vasko0b85aa82016-03-07 14:37:43 +01003461 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003462}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003463
3464/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003465 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003466 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003467 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003468 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003469 *
3470 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3471 */
3472int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003473resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003474{
Radek Krejci581ce772015-11-10 17:22:40 +01003475 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003476 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003477
Michal Vaskodc300b02017-04-07 14:09:20 +02003478 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003479 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003480 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003481 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003482 if (rc > 0) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003483 LOGVAL(LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003484 } else if (rc == -2) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003485 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003486 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003487 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003488 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003489 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003490 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003491 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003492 }
Radek Krejci581ce772015-11-10 17:22:40 +01003493 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003494 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003495 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003496 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003497 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003498 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003499 }
3500
Radek Krejcicf509982015-12-15 09:22:44 +01003501 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003502 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3503 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003504 return -1;
3505 }
3506
Radek Krejcid09d1a52016-08-11 14:05:45 +02003507 /* check that all unique's targets are of the same config type */
3508 if (*trg_type) {
3509 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
3510 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003511 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003512 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3513 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3514 return -1;
3515 }
3516 } else {
3517 /* first unique */
3518 if (leaf->flags & LYS_CONFIG_W) {
3519 *trg_type = 1;
3520 } else {
3521 *trg_type = 2;
3522 }
3523 }
3524
Radek Krejcica7efb72016-01-18 13:06:01 +01003525 /* set leaf's unique flag */
3526 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3527
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003528 return EXIT_SUCCESS;
3529
3530error:
3531
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003532 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003533}
3534
Radek Krejci0c0086a2016-03-24 15:20:28 +01003535void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003536unres_data_del(struct unres_data *unres, uint32_t i)
3537{
3538 /* there are items after the one deleted */
3539 if (i+1 < unres->count) {
3540 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003541 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003542
3543 /* deleting the last item */
3544 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003545 free(unres->node);
3546 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003547 }
3548
3549 /* if there are no items after and it is not the last one, just move the counter */
3550 --unres->count;
3551}
3552
Michal Vasko0491ab32015-08-19 14:28:29 +02003553/**
3554 * @brief Resolve (find) a data node from a specific module. Does not log.
3555 *
3556 * @param[in] mod Module to search in.
3557 * @param[in] name Name of the data node.
3558 * @param[in] nam_len Length of the name.
3559 * @param[in] start Data node to start the search from.
3560 * @param[in,out] parents Resolved nodes. If there are some parents,
3561 * they are replaced (!!) with the resolvents.
3562 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003563 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003564 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003565static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003566resolve_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 +02003567{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003568 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003569 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003570 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003571
Michal Vasko23b61ec2015-08-19 11:19:50 +02003572 if (!parents->count) {
3573 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003574 parents->node = malloc(sizeof *parents->node);
Radek Krejciaa1303c2017-05-31 13:57:37 +02003575 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003576 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003577 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003578 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003579 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003580 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003581 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003582 continue;
3583 }
3584 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003585 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003586 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003587 && node->schema->name[nam_len] == '\0') {
3588 /* matching target */
3589 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003590 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003591 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003592 flag = 1;
3593 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003594 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003595 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003596 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Radek Krejciaa1303c2017-05-31 13:57:37 +02003597 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003598 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003599 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003600 }
3601 }
3602 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003603
3604 if (!flag) {
3605 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003606 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003607 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003608 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003609 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003610 }
3611
Michal Vasko0491ab32015-08-19 14:28:29 +02003612 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003613}
3614
Michal Vaskoe27516a2016-10-10 17:55:31 +00003615static int
Michal Vasko1c007172017-03-10 10:20:44 +01003616resolve_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 +00003617{
3618 int dep1, dep2;
3619 const struct lys_node *node;
3620
3621 if (lys_parent(op_node)) {
3622 /* inner operation (notif/action) */
3623 if (abs_path) {
3624 return 1;
3625 } else {
3626 /* compare depth of both nodes */
3627 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3628 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3629 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3630 return 1;
3631 }
3632 }
3633 } else {
3634 /* top-level operation (notif/rpc) */
3635 if (op_node != first_node) {
3636 return 1;
3637 }
3638 }
3639
3640 return 0;
3641}
3642
Michal Vasko730dfdf2015-08-11 14:48:05 +02003643/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003644 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003645 *
Michal Vaskobb211122015-08-19 14:03:11 +02003646 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003647 * @param[in] context_node Predicate context node (where the predicate is placed).
3648 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003649 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003650 *
Michal Vasko184521f2015-09-24 13:14:26 +02003651 * @return 0 on forward reference, otherwise the number
3652 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003653 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003654 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003655static int
Michal Vasko1c007172017-03-10 10:20:44 +01003656resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3657 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003658{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003659 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003660 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003661 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003662 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3663 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko1f76a282015-08-04 16:16:53 +02003664
3665 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003666 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003667 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003668 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003669 return -parsed+i;
3670 }
3671 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003672 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003673
Michal Vasko58090902015-08-13 14:04:15 +02003674 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003675 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003676 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003677 } else {
3678 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003679 }
Michal Vaskobb520442017-05-23 10:55:18 +02003680 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003681 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003682 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003683 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003684 }
3685
3686 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003687 dest_parent_times = 0;
3688 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003689 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3690 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003691 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr[-i], path_key_expr-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003692 return -parsed;
3693 }
3694 pke_parsed += i;
3695
Radek Krejciadb57612016-02-16 13:34:34 +01003696 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003697 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3698 && !((struct lys_node_augment *)dst_node->parent)->target) {
3699 /* we are in an unresolved augment, cannot evaluate */
3700 LOGVAL(LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
3701 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3702 return 0;
3703 }
3704
Michal Vaskofbaead72016-10-07 10:54:48 +02003705 /* path is supposed to be evaluated in data tree, so we have to skip
3706 * all schema nodes that cannot be instantiated in data tree */
3707 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003708 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003709 dst_node = lys_parent(dst_node));
3710
Michal Vasko1f76a282015-08-04 16:16:53 +02003711 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003712 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003713 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003714 }
3715 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003716 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003717 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003718 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003719 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003720 } else {
3721 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003722 }
Michal Vaskobb520442017-05-23 10:55:18 +02003723 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 +02003724 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003725 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003726 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003727 }
3728
Michal Vaskoe27516a2016-10-10 17:55:31 +00003729 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003730 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003731 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003732 }
3733 first_iter = 0;
3734 }
3735
Michal Vasko1f76a282015-08-04 16:16:53 +02003736 if (pke_len == pke_parsed) {
3737 break;
3738 }
3739
Michal Vaskobb520442017-05-23 10:55:18 +02003740 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 +02003741 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003742 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02003743 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003744 return -parsed;
3745 }
3746 pke_parsed += i;
3747 }
3748
3749 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003750 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskobb520442017-05-23 10:55:18 +02003751 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path - parsed);
Michal Vasko51e5c582017-01-19 14:16:39 +01003752 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003753 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003754 return -parsed;
3755 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003756 } while (has_predicate);
3757
3758 return parsed;
3759}
3760
Michal Vasko730dfdf2015-08-11 14:48:05 +02003761/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003762 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003763 *
Michal Vaskobb211122015-08-19 14:03:11 +02003764 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003765 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003766 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003767 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003768 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003769 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003770static int
Michal Vasko1c007172017-03-10 10:20:44 +01003771resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003772{
Michal Vaskoe27516a2016-10-10 17:55:31 +00003773 const struct lys_node *node, *op_node = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02003774 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003775 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02003776 const char *id, *prefix, *name;
3777 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003778 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003779
Michal Vasko184521f2015-09-24 13:14:26 +02003780 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003781 parent_times = 0;
3782 id = path;
3783
Michal Vasko1c007172017-03-10 10:20:44 +01003784 /* find operation schema we are in */
3785 for (op_node = lys_parent(parent);
3786 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
3787 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02003788
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003789 cur_module = lys_node_module(parent);
Michal Vasko1f76a282015-08-04 16:16:53 +02003790 do {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003791 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Michal Vasko1c007172017-03-10 10:20:44 +01003792 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003793 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003794 }
3795 id += i;
3796
Michal Vaskobb520442017-05-23 10:55:18 +02003797 /* get the current module */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003798 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003799 if (!tmp_mod) {
Michal Vaskobb520442017-05-23 10:55:18 +02003800 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3801 return EXIT_FAILURE;
3802 }
3803 last_aug = NULL;
3804
Michal Vasko184521f2015-09-24 13:14:26 +02003805 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003806 if (parent_times == -1) {
Michal Vaskobb520442017-05-23 10:55:18 +02003807 /* use module data */
3808 node = NULL;
Radek Krejci990af1f2016-11-09 13:53:36 +01003809
Michal Vasko1f76a282015-08-04 16:16:53 +02003810 } else if (parent_times > 0) {
Michal Vaskobb520442017-05-23 10:55:18 +02003811 /* we are looking for the right parent */
3812 for (i = 0, node = parent; i < parent_times; i++) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003813 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
3814 && !((struct lys_node_augment *)node->parent)->target) {
3815 /* we are in an unresolved augment, cannot evaluate */
3816 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node->parent,
3817 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path);
3818 return EXIT_FAILURE;
3819 }
3820
Radek Krejci3a5501d2016-07-18 22:03:34 +02003821 /* path is supposed to be evaluated in data tree, so we have to skip
3822 * all schema nodes that cannot be instantiated in data tree */
3823 for (node = lys_parent(node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003824 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Radek Krejci3a5501d2016-07-18 22:03:34 +02003825 node = lys_parent(node));
3826
Michal Vasko1f76a282015-08-04 16:16:53 +02003827 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003828 if (i == parent_times - 1) {
3829 /* top-level */
3830 break;
3831 }
3832
3833 /* higher than top-level */
Michal Vaskoe9914d12016-10-07 14:32:37 +02003834 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003835 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003836 }
3837 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003838 } else {
3839 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003840 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003841 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003842 }
3843
Michal Vaskobb520442017-05-23 10:55:18 +02003844 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
3845 * - useless to search for that in augments) */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003846 if (!tmp_mod->implemented && node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003847get_next_augment:
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003848 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
Michal Vaskobb520442017-05-23 10:55:18 +02003849 }
3850
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003851 rc = lys_getnext_data(tmp_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST
Michal Vaskobb520442017-05-23 10:55:18 +02003852 | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003853 if (rc) {
Michal Vaskobb520442017-05-23 10:55:18 +02003854 if (last_aug) {
3855 goto get_next_augment;
3856 }
Michal Vasko1c007172017-03-10 10:20:44 +01003857 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003858 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003859 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003860
Michal Vaskoe27516a2016-10-10 17:55:31 +00003861 if (first_iter) {
3862 /* set external dependency flag, we can decide based on the first found node */
Michal Vasko1c007172017-03-10 10:20:44 +01003863 if (op_node && parent_times &&
3864 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003865 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003866 }
3867 first_iter = 0;
3868 }
3869
Michal Vasko1f76a282015-08-04 16:16:53 +02003870 if (has_predicate) {
3871 /* we have predicate, so the current result must be list */
3872 if (node->nodetype != LYS_LIST) {
Michal Vasko1c007172017-03-10 10:20:44 +01003873 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003874 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003875 }
3876
Michal Vasko1c007172017-03-10 10:20:44 +01003877 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
Michal Vaskobb520442017-05-23 10:55:18 +02003878 if (!i) {
3879 return EXIT_FAILURE;
3880 } else if (i < 0) {
3881 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003882 }
3883 id += i;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003884 has_predicate = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003885 }
3886 } while (id[0]);
3887
Michal Vaskoca917682016-07-25 11:00:37 +02003888 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
Radek Krejci2a5a9602016-11-04 10:21:13 +01003889 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
Michal Vasko1c007172017-03-10 10:20:44 +01003890 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko51e5c582017-01-19 14:16:39 +01003891 LOGVAL(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 +02003892 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003893 }
3894
Radek Krejcicf509982015-12-15 09:22:44 +01003895 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003896 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003897 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003898 return -1;
3899 }
3900
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003901 if (ret) {
3902 *ret = node;
3903 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003904
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003905 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003906}
3907
Michal Vasko730dfdf2015-08-11 14:48:05 +02003908/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02003909 * @brief Compare 2 data node values.
3910 *
3911 * Comparison performed on canonical forms, the first value
3912 * is first transformed into canonical form.
3913 *
3914 * @param[in] node Leaf/leaf-list with these values.
3915 * @param[in] noncan_val Non-canonical value.
3916 * @param[in] noncan_val_len Length of \p noncal_val.
3917 * @param[in] can_val Canonical value.
3918 * @return 1 if equal, 0 if not, -1 on error (logged).
3919 */
3920static int
3921valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
3922{
3923 int ret;
3924 struct lyd_node_leaf_list leaf;
3925 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
3926
3927 /* dummy leaf */
3928 memset(&leaf, 0, sizeof leaf);
3929 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
3930
3931repeat:
3932 leaf.value_type = sleaf->type.base;
3933 leaf.schema = node;
3934
3935 if (leaf.value_type == LY_TYPE_LEAFREF) {
3936 if (!sleaf->type.info.lref.target) {
3937 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
3938 LOGINT;
3939 ret = -1;
3940 goto finish;
3941 }
3942 sleaf = sleaf->type.info.lref.target;
3943 goto repeat;
3944 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01003945 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02003946 ret = -1;
3947 goto finish;
3948 }
3949 }
3950
3951 if (!strcmp(leaf.value_str, can_val)) {
3952 ret = 1;
3953 } else {
3954 ret = 0;
3955 }
3956
3957finish:
3958 lydict_remove(node->module->ctx, leaf.value_str);
3959 return ret;
3960}
3961
3962/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003963 * @brief Resolve instance-identifier predicate in JSON data format.
3964 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003965 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02003966 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02003967 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003968 * @param[in,out] node Node matching the restriction without
3969 * the predicate. If it does not satisfy the predicate,
3970 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003971 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003972 * @return Number of characters successfully parsed,
3973 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003974 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003975static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003976resolve_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 +02003977{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003978 /* ... /node[key=value] ... */
3979 struct lyd_node_leaf_list *key;
3980 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02003981 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003982 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003983 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003984
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003985 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02003986
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003987 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003988 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003989 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
3990 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003991 }
3992 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003993
Michal Vasko88850b72017-10-02 13:13:21 +02003994 if (!(*node)) {
3995 /* just parse it all */
3996 continue;
3997 }
3998
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003999 /* target */
4000 if (name[0] == '.') {
4001 /* leaf-list value */
4002 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
4003 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
4004 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4005 parsed = -1;
4006 goto cleanup;
4007 }
4008
4009 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004010 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004011 *node = NULL;
4012 goto cleanup;
4013 }
4014
4015 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004016 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004017
4018 /* keyless list position */
4019 if ((*node)->schema->nodetype != LYS_LIST) {
4020 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
4021 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4022 parsed = -1;
4023 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004024 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004025
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004026 if (((struct lys_node_list *)(*node)->schema)->keys) {
4027 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".",
4028 (*node)->schema->name);
4029 parsed = -1;
4030 goto cleanup;
4031 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004032
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004033 /* check the index */
4034 if (atoi(name) != cur_idx) {
4035 *node = NULL;
4036 goto cleanup;
4037 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004038
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004039 } else {
4040 /* list key value */
4041 if ((*node)->schema->nodetype != LYS_LIST) {
4042 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
4043 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4044 parsed = -1;
4045 goto cleanup;
4046 }
4047 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004048
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004049 /* prepare key array */
4050 if (!list_keys) {
4051 list_keys = malloc(slist->keys_size * sizeof *list_keys);
4052 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM, -1);
4053 for (i = 0; i < slist->keys_size; ++i) {
4054 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004055 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004056 }
4057
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004058 /* find the schema key leaf */
4059 for (i = 0; i < slist->keys_size; ++i) {
4060 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4061 break;
4062 }
4063 }
4064 if (i == slist->keys_size) {
4065 /* this list has no such key */
4066 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
4067 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4068 parsed = -1;
4069 goto cleanup;
4070 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004071
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004072 /* check module */
4073 if (model) {
4074 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
4075 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".",
4076 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4077 parsed = -1;
4078 goto cleanup;
4079 }
4080 } else {
4081 if (list_keys[i]->module != prev_mod) {
4082 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".",
4083 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4084 parsed = -1;
4085 goto cleanup;
4086 }
4087 }
4088
4089 /* find the actual data key */
4090 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4091 if (key->schema == (struct lys_node *)list_keys[i]) {
4092 break;
4093 }
4094 }
4095 if (!key) {
4096 /* list instance is missing a key? definitely should not happen */
4097 LOGINT;
4098 parsed = -1;
4099 goto cleanup;
4100 }
4101
4102 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004103 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004104 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004105 /* we still want to parse the whole predicate */
4106 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004107 }
4108
4109 /* everything is fine, mark this key as resolved */
4110 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004111 }
4112 } while (has_predicate);
4113
Michal Vaskob2f40be2016-09-08 16:03:48 +02004114 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004115 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004116 for (i = 0; i < slist->keys_size; ++i) {
4117 if (list_keys[i]) {
4118 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name);
4119 parsed = -1;
4120 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004121 }
4122 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004123 }
4124
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004125cleanup:
4126 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004127 return parsed;
4128}
4129
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004130int
Michal Vaskof96dfb62017-08-17 12:23:49 +02004131lys_check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004132{
Michal Vasko0b963112017-08-11 12:45:36 +02004133 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004134 struct lyxp_set set;
Michal Vasko769f8032017-01-24 13:11:55 +01004135 int ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004136
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004137 if (check_place) {
4138 parent = node;
4139 while (parent) {
4140 if (parent->nodetype == LYS_GROUPING) {
4141 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004142 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004143 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004144 if (parent->nodetype == LYS_AUGMENT) {
4145 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004146 /* unresolved augment, skip for now (will be checked later) */
4147 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004148 } else {
4149 parent = ((struct lys_node_augment *)parent)->target;
4150 continue;
4151 }
4152 }
4153 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004154 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004155 }
4156
Michal Vaskof96dfb62017-08-17 12:23:49 +02004157 ret = lyxp_node_atomize(node, &set, 1);
Michal Vasko769f8032017-01-24 13:11:55 +01004158 if (ret == -1) {
4159 return -1;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004160 }
4161
Michal Vasko9e635ac2016-10-17 11:44:09 +02004162 free(set.val.snodes);
Michal Vasko769f8032017-01-24 13:11:55 +01004163 return ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004164}
4165
Radek Krejcif71f48f2016-10-25 16:37:24 +02004166static int
4167check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4168{
Radek Krejcidce5f972017-09-12 15:47:49 +02004169 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004170
4171 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004172 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4173 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Radek Krejcid831dd42017-03-16 12:59:30 +01004174 LOGVAL(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 +02004175 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4176 return -1;
4177 }
4178 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4179 * of leafref resolving (lys_leaf_add_leafref_target()) */
4180 } else if (type->base == LY_TYPE_UNION) {
4181 for (i = 0; i < type->info.uni.count; i++) {
4182 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4183 return -1;
4184 }
4185 }
4186 }
4187 return 0;
4188}
4189
Michal Vasko9e635ac2016-10-17 11:44:09 +02004190/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004191 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004192 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004193 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004194 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004195 * @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 +02004196 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004197 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004198 *
4199 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004200 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004201int
4202inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004203{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004204 struct lys_node_leaf *leaf;
4205
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004206 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02004207 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004208 if (clear) {
4209 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004210 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004211 } else {
4212 if (node->flags & LYS_CONFIG_SET) {
4213 /* skip nodes with an explicit config value */
4214 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4215 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01004216 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004217 return -1;
4218 }
4219 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004220 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004221
4222 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4223 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4224 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004225 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4226 && !((struct lys_node_list *)node)->keys_size) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004227 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
4228 return -1;
4229 }
4230 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004231 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004232 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004233 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004234 return -1;
4235 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004236 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4237 leaf = (struct lys_node_leaf *)node;
4238 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004239 return -1;
4240 }
4241 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004242 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004243
4244 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004245}
4246
Michal Vasko730dfdf2015-08-11 14:48:05 +02004247/**
Michal Vasko7178e692016-02-12 15:58:05 +01004248 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004249 *
Michal Vaskobb211122015-08-19 14:03:11 +02004250 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004251 * @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 +01004252 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004253 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004254 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004255 */
Michal Vasko7178e692016-02-12 15:58:05 +01004256static int
Michal Vasko97234262018-02-01 09:53:01 +01004257resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004258{
Michal Vasko44ab1462017-05-18 13:18:36 +02004259 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004260 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004261 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004262 struct ly_set *set;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004263
Michal Vasko2ef7db62017-06-12 09:24:02 +02004264 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004265 mod = lys_main_module(aug->module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004266
Michal Vaskobb520442017-05-23 10:55:18 +02004267 /* set it as not applied for now */
4268 aug->flags |= LYS_NOTAPPLIED;
4269
Michal Vasko2ef7db62017-06-12 09:24:02 +02004270 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004271 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004272 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004273 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 +02004274 if (rc == -1) {
Michal Vasko50576712017-07-28 12:28:33 +02004275 LOGVAL(LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004276 return -1;
4277 }
Michal Vasko50576712017-07-28 12:28:33 +02004278 if (!set) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004279 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
4280 return EXIT_FAILURE;
4281 }
Michal Vasko50576712017-07-28 12:28:33 +02004282 aug->target = set->set.s[0];
4283 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004284 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004285
Michal Vaskod58d5962016-03-02 14:29:41 +01004286 /* check for mandatory nodes - if the target node is in another module
4287 * the added nodes cannot be mandatory
4288 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004289 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4290 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004291 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004292 }
4293
Michal Vasko07e89ef2016-03-03 13:28:57 +01004294 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004295 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004296 LY_TREE_FOR(aug->child, sub) {
4297 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4298 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
4299 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4300 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004301 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004302 return -1;
4303 }
4304 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004305 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004306 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004307 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004308 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004309 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004310 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004311 return -1;
4312 }
4313 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004314 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004315 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004316 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004317 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004318 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004319 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004320 return -1;
4321 }
4322 }
4323 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004324 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
Michal Vasko44ab1462017-05-18 13:18:36 +02004325 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004326 return -1;
4327 }
4328
Radek Krejcic071c542016-01-27 14:57:51 +01004329 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004330 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004331 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004332 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004333 }
4334 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004335
Michal Vasko44ab1462017-05-18 13:18:36 +02004336 if (!aug->child) {
4337 /* empty augment, nothing to connect, but it is techincally applied */
4338 LOGWRN("Augment \"%s\" without children.", aug->target_name);
4339 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004340 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4341 /* we try to connect the augment only in case the module is implemented or
4342 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004343 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004344 }
4345
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004346 return EXIT_SUCCESS;
4347}
4348
Radek Krejcie534c132016-11-23 13:32:31 +01004349static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004350resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004351{
4352 enum LY_VLOG_ELEM vlog_type;
4353 void *vlog_node;
4354 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004355 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004356 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004357 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004358 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004359 struct lys_ext_instance *tmp_ext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004360 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004361
4362 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004363 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004364 vlog_node = info->parent;
4365 vlog_type = LY_VLOG_LYS;
4366 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004367 case LYEXT_PAR_MODULE:
4368 case LYEXT_PAR_IMPORT:
4369 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004370 vlog_node = NULL;
4371 vlog_type = LY_VLOG_LYS;
4372 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004373 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004374 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004375 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004376 break;
4377 }
4378
4379 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004380 /* YIN */
4381
Radek Krejcie534c132016-11-23 13:32:31 +01004382 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004383 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004384 if (!mod) {
4385 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004386 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004387 }
4388
4389 /* find the extension definition */
4390 e = NULL;
4391 for (i = 0; i < mod->extensions_size; i++) {
4392 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4393 e = &mod->extensions[i];
4394 break;
4395 }
4396 }
4397 /* try submodules */
4398 for (j = 0; !e && j < mod->inc_size; j++) {
4399 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4400 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4401 e = &mod->inc[j].submodule->extensions[i];
4402 break;
4403 }
4404 }
4405 }
4406 if (!e) {
4407 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
4408 return EXIT_FAILURE;
4409 }
4410
4411 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004412
Radek Krejci72b35992017-01-04 16:27:44 +01004413 if (e->plugin && e->plugin->check_position) {
4414 /* common part - we have plugin with position checking function, use it first */
4415 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4416 /* extension is not allowed here */
4417 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name);
4418 return -1;
4419 }
4420 }
4421
Radek Krejci8d6b7422017-02-03 14:42:13 +01004422 /* extension type-specific part - allocation */
4423 if (e->plugin) {
4424 etype = e->plugin->type;
4425 } else {
4426 /* default type */
4427 etype = LYEXT_FLAG;
4428 }
4429 switch (etype) {
4430 case LYEXT_FLAG:
4431 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4432 break;
4433 case LYEXT_COMPLEX:
4434 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4435 break;
4436 case LYEXT_ERR:
4437 /* we never should be here */
4438 LOGINT;
4439 return -1;
4440 }
Radek Krejciaa1303c2017-05-31 13:57:37 +02004441 LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004442
4443 /* common part for all extension types */
4444 (*ext)->def = e;
4445 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004446 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004447 (*ext)->insubstmt = info->substmt;
4448 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004449 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004450
PavolVicand86b0d62017-09-01 11:01:39 +02004451 if (e->argument) {
4452 if (!(e->flags & LYS_YINELEM)) {
4453 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4454 if (!(*ext)->arg_value) {
4455 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
4456 return -1;
4457 }
4458
4459 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4460 } else {
4461 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4462 if (ly_strequal(yin->name, e->argument, 1)) {
4463 (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0);
4464 lyxml_free(mod->ctx, yin);
4465 break;
4466 }
4467 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004468 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004469 }
4470
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004471 (*ext)->nodetype = LYS_EXT;
4472 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004473
Radek Krejci8d6b7422017-02-03 14:42:13 +01004474 /* extension type-specific part - parsing content */
4475 switch (etype) {
4476 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004477 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4478 if (!yin->ns) {
4479 /* garbage */
4480 lyxml_free(mod->ctx, yin);
4481 continue;
4482 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4483 /* standard YANG statements are not expected here */
4484 LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
4485 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004486 } else if (yin->ns == info->data.yin->ns &&
4487 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004488 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004489 if ((*ext)->arg_value) {
Radek Krejci72b35992017-01-04 16:27:44 +01004490 LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004491 return -1;
4492 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004493 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004494 yin->content = NULL;
4495 lyxml_free(mod->ctx, yin);
4496 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004497 /* extension instance */
4498 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4499 LYEXT_SUBSTMT_SELF, 0, unres)) {
4500 return -1;
4501 }
Radek Krejci72b35992017-01-04 16:27:44 +01004502
Radek Krejci72b35992017-01-04 16:27:44 +01004503 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004504 }
Radek Krejci72b35992017-01-04 16:27:44 +01004505 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004506 break;
4507 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004508 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004509 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4510 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004511 return -1;
4512 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004513 break;
4514 default:
4515 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004516 }
Radek Krejci72b35992017-01-04 16:27:44 +01004517
4518 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4519
Radek Krejcie534c132016-11-23 13:32:31 +01004520 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004521 /* YANG */
4522
PavolVicanc1807262017-01-31 18:00:27 +01004523 ext_prefix = (char *)(*ext)->def;
4524 tmp = strchr(ext_prefix, ':');
4525 if (!tmp) {
4526 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004527 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004528 }
4529 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004530
PavolVicanc1807262017-01-31 18:00:27 +01004531 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004532 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004533 if (!mod) {
4534 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
4535 return EXIT_FAILURE;
4536 }
4537
4538 /* find the extension definition */
4539 e = NULL;
4540 for (i = 0; i < mod->extensions_size; i++) {
4541 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4542 e = &mod->extensions[i];
4543 break;
4544 }
4545 }
4546 /* try submodules */
4547 for (j = 0; !e && j < mod->inc_size; j++) {
4548 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4549 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4550 e = &mod->inc[j].submodule->extensions[i];
4551 break;
4552 }
4553 }
4554 }
4555 if (!e) {
4556 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
4557 return EXIT_FAILURE;
4558 }
4559
PavolVicanfcc98762017-09-01 15:51:39 +02004560 (*ext)->flags &= ~LYEXT_OPT_YANG;
4561 (*ext)->def = NULL;
4562
PavolVicanc1807262017-01-31 18:00:27 +01004563 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4564
4565 if (e->plugin && e->plugin->check_position) {
4566 /* common part - we have plugin with position checking function, use it first */
4567 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4568 /* extension is not allowed here */
4569 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01004570 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004571 }
4572 }
4573
PavolVican22e88682017-02-14 22:38:18 +01004574 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01004575 (*ext)->def = e;
4576 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01004577 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican22e88682017-02-14 22:38:18 +01004578
PavolVicanb0d84102017-02-15 16:32:42 +01004579 if (e->argument && !(*ext)->arg_value) {
4580 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
4581 goto error;
4582 }
4583
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004584 (*ext)->module = info->mod;
4585 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004586
PavolVican22e88682017-02-14 22:38:18 +01004587 /* extension type-specific part */
4588 if (e->plugin) {
4589 etype = e->plugin->type;
4590 } else {
4591 /* default type */
4592 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01004593 }
PavolVican22e88682017-02-14 22:38:18 +01004594 switch (etype) {
4595 case LYEXT_FLAG:
4596 /* nothing change */
4597 break;
4598 case LYEXT_COMPLEX:
4599 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004600 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error);
PavolVican22e88682017-02-14 22:38:18 +01004601 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
4602 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01004603 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01004604 if (info->data.yang) {
4605 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01004606 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
4607 (struct lys_ext_instance_complex*)(*ext))) {
4608 goto error;
4609 }
4610 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
4611 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01004612 goto error;
4613 }
PavolVicana3876672017-02-21 15:49:51 +01004614 }
4615 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
4616 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01004617 }
PavolVican22e88682017-02-14 22:38:18 +01004618 break;
4619 case LYEXT_ERR:
4620 /* we never should be here */
4621 LOGINT;
4622 goto error;
4623 }
4624
PavolVican22e88682017-02-14 22:38:18 +01004625 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
4626 goto error;
4627 }
4628 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01004629 }
4630
4631 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01004632error:
4633 free(ext_prefix);
4634 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01004635}
4636
Michal Vasko730dfdf2015-08-11 14:48:05 +02004637/**
Pavol Vican855ca622016-09-05 13:07:54 +02004638 * @brief Resolve (find) choice default case. Does not log.
4639 *
4640 * @param[in] choic Choice to use.
4641 * @param[in] dflt Name of the default case.
4642 *
4643 * @return Pointer to the default node or NULL.
4644 */
4645static struct lys_node *
4646resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4647{
4648 struct lys_node *child, *ret;
4649
4650 LY_TREE_FOR(choic->child, child) {
4651 if (child->nodetype == LYS_USES) {
4652 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4653 if (ret) {
4654 return ret;
4655 }
4656 }
4657
4658 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02004659 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02004660 return child;
4661 }
4662 }
4663
4664 return NULL;
4665}
4666
4667/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02004668 * @brief Resolve uses, apply augments, refines. Logs directly.
4669 *
Michal Vaskobb211122015-08-19 14:03:11 +02004670 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004671 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004672 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004673 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004674 */
Michal Vasko184521f2015-09-24 13:14:26 +02004675static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004676resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004677{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004678 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02004679 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02004680 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02004681 struct lys_node_leaflist *llist;
4682 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02004683 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004684 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004685 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004686 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004687 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004688 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004689
Michal Vasko71e1aa82015-08-12 12:17:51 +02004690 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01004691
Radek Krejci93def382017-05-24 15:33:48 +02004692 /* check that the grouping is resolved (no unresolved uses inside) */
4693 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02004694
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004695 if (!uses->grp->child) {
4696 /* grouping without children, warning was already displayed */
4697 return EXIT_SUCCESS;
4698 }
4699
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004700 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004701 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01004702 if (node_aux->nodetype & LYS_GROUPING) {
4703 /* do not instantiate groupings from groupings */
4704 continue;
4705 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01004706 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004707 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004708 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
Michal Vasko51e5c582017-01-19 14:16:39 +01004709 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004710 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004711 }
Pavol Vican55abd332016-07-12 15:54:49 +02004712 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01004713 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 +02004714 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004715 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004716 }
4717 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004718 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004719
Michal Vaskodef0db12015-10-07 13:22:48 +02004720 /* we managed to copy the grouping, the rest must be possible to resolve */
4721
Pavol Vican855ca622016-09-05 13:07:54 +02004722 if (uses->refine_size) {
4723 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004724 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004725 }
4726
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004727 /* apply refines */
4728 for (i = 0; i < uses->refine_size; i++) {
4729 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01004730 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
4731 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02004732 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004733 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004734 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004735 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004736 }
4737
Radek Krejci1d82ef62015-08-07 14:44:40 +02004738 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004739 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004740 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004741 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004742 }
Pavol Vican855ca622016-09-05 13:07:54 +02004743 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004744
4745 /* description on any nodetype */
4746 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004747 lydict_remove(ctx, node->dsc);
4748 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004749 }
4750
4751 /* reference on any nodetype */
4752 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004753 lydict_remove(ctx, node->ref);
4754 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004755 }
4756
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004757 /* config on any nodetype,
4758 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
4759 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004760 node->flags &= ~LYS_CONFIG_MASK;
4761 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004762 }
4763
4764 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02004765 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004766 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004767 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02004768 leaf = (struct lys_node_leaf *)node;
4769
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004770 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02004771 lydict_remove(ctx, leaf->dflt);
4772 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
4773
4774 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01004775 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
4776 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004777 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004778 }
Radek Krejci200bf712016-08-16 17:11:04 +02004779 } else if (node->nodetype == LYS_LEAFLIST) {
4780 /* leaf-list */
4781 llist = (struct lys_node_leaflist *)node;
4782
4783 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01004784 for (j = 0; j < llist->dflt_size; j++) {
4785 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02004786 }
4787 free(llist->dflt);
4788
4789 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02004790 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
4791 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail);
Radek Krejci200bf712016-08-16 17:11:04 +02004792 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01004793 for (j = 0; j < llist->dflt_size; j++) {
4794 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02004795 }
4796
4797 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01004798 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01004799 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01004800 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02004801 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02004802 }
4803 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004804 }
4805 }
4806
4807 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004808 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01004809 /* remove current value */
4810 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004811
Radek Krejcibf285832017-01-26 16:05:41 +01004812 /* set new value */
4813 node->flags |= (rfn->flags & LYS_MAND_MASK);
4814
Pavol Vican855ca622016-09-05 13:07:54 +02004815 if (rfn->flags & LYS_MAND_TRUE) {
4816 /* check if node has default value */
4817 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004818 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4819 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004820 goto fail;
4821 }
4822 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004823 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4824 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004825 goto fail;
4826 }
4827 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004828 }
4829
4830 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004831 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4832 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4833 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004834 }
4835
4836 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004837 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004838 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004839 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004840 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004841 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004842 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004843 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004844 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004845 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004846 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004847 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004848 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004849 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004850 }
4851 }
4852
4853 /* must in leaf, leaf-list, list, container or anyxml */
4854 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004855 switch (node->nodetype) {
4856 case LYS_LEAF:
4857 old_size = &((struct lys_node_leaf *)node)->must_size;
4858 old_must = &((struct lys_node_leaf *)node)->must;
4859 break;
4860 case LYS_LEAFLIST:
4861 old_size = &((struct lys_node_leaflist *)node)->must_size;
4862 old_must = &((struct lys_node_leaflist *)node)->must;
4863 break;
4864 case LYS_LIST:
4865 old_size = &((struct lys_node_list *)node)->must_size;
4866 old_must = &((struct lys_node_list *)node)->must;
4867 break;
4868 case LYS_CONTAINER:
4869 old_size = &((struct lys_node_container *)node)->must_size;
4870 old_must = &((struct lys_node_container *)node)->must;
4871 break;
4872 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004873 case LYS_ANYDATA:
4874 old_size = &((struct lys_node_anydata *)node)->must_size;
4875 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004876 break;
4877 default:
4878 LOGINT;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004879 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004880 }
4881
4882 size = *old_size + rfn->must_size;
4883 must = realloc(*old_must, size * sizeof *rfn->must);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004884 LY_CHECK_ERR_GOTO(!must, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004885 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01004886 must[j].ext_size = rfn->must[k].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004887 lys_ext_dup(rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004888 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02004889 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
4890 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
4891 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
4892 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
4893 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02004894 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004895 }
4896
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004897 *old_must = must;
4898 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02004899
4900 /* check XPath dependencies again */
4901 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
4902 goto fail;
4903 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004904 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02004905
4906 /* if-feature in leaf, leaf-list, list, container or anyxml */
4907 if (rfn->iffeature_size) {
4908 old_size = &node->iffeature_size;
4909 old_iff = &node->iffeature;
4910
4911 size = *old_size + rfn->iffeature_size;
4912 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004913 LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02004914 *old_iff = iff;
4915
Pavol Vican855ca622016-09-05 13:07:54 +02004916 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
4917 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004918 if (usize1) {
4919 /* there is something to duplicate */
4920 /* duplicate compiled expression */
4921 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
4922 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004923 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004924 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004925
4926 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02004927 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Radek Krejciaa1303c2017-05-31 13:57:37 +02004928 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004929 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004930
Radek Krejci3a3b2002017-09-13 16:39:02 +02004931 /* duplicate extensions */
4932 iff[j].ext_size = rfn->iffeature[k].ext_size;
4933 lys_ext_dup(rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
4934 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
4935 }
4936 (*old_size)++;
4937 }
4938 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004939 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004940 }
4941
4942 /* apply augments */
4943 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01004944 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004945 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004946 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004947 }
4948 }
4949
Pavol Vican855ca622016-09-05 13:07:54 +02004950 /* check refines */
4951 for (i = 0; i < uses->refine_size; i++) {
4952 node = refine_nodes[i];
4953 rfn = &uses->refine[i];
4954
4955 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004956 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02004957 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01004958 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02004959 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
4960 (rfn->flags & LYS_CONFIG_W)) {
4961 /* setting config true under config false is prohibited */
4962 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004963 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004964 "changing config from 'false' to 'true' is prohibited while "
4965 "the target's parent is still config 'false'.");
4966 goto fail;
4967 }
4968
4969 /* inherit config change to the target children */
4970 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4971 if (rfn->flags & LYS_CONFIG_W) {
4972 if (iter->flags & LYS_CONFIG_SET) {
4973 /* config is set explicitely, go to next sibling */
4974 next = NULL;
4975 goto nextsibling;
4976 }
4977 } else { /* LYS_CONFIG_R */
4978 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4979 /* error - we would have config data under status data */
4980 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004981 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004982 "changing config from 'true' to 'false' is prohibited while the target "
4983 "has still a children with explicit config 'true'.");
4984 goto fail;
4985 }
4986 }
4987 /* change config */
4988 iter->flags &= ~LYS_CONFIG_MASK;
4989 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4990
4991 /* select next iter - modified LY_TREE_DFS_END */
4992 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4993 next = NULL;
4994 } else {
4995 next = iter->child;
4996 }
4997nextsibling:
4998 if (!next) {
4999 /* try siblings */
5000 next = iter->next;
5001 }
5002 while (!next) {
5003 /* parent is already processed, go to its sibling */
5004 iter = lys_parent(iter);
5005
5006 /* no siblings, go back through parents */
5007 if (iter == node) {
5008 /* we are done, no next element to process */
5009 break;
5010 }
5011 next = iter->next;
5012 }
5013 }
5014 }
5015
5016 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005017 if (rfn->dflt_size) {
5018 if (node->nodetype == LYS_CHOICE) {
5019 /* choice */
5020 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5021 rfn->dflt[0]);
5022 if (!((struct lys_node_choice *)node)->dflt) {
5023 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
5024 goto fail;
5025 }
5026 if (lyp_check_mandatory_choice(node)) {
5027 goto fail;
5028 }
Pavol Vican855ca622016-09-05 13:07:54 +02005029 }
5030 }
5031
5032 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02005033 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005034 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02005035 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5036 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005037 goto fail;
5038 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005039 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005040 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02005041 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5042 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005043 goto fail;
5044 }
5045 }
5046
5047 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005048 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005049 if (node->nodetype == LYS_LEAFLIST) {
5050 llist = (struct lys_node_leaflist *)node;
5051 if (llist->dflt_size && llist->min) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02005052 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5053 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005054 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5055 goto fail;
5056 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005057 } else if (node->nodetype == LYS_LEAF) {
5058 leaf = (struct lys_node_leaf *)node;
5059 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02005060 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5061 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005062 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5063 goto fail;
5064 }
Pavol Vican855ca622016-09-05 13:07:54 +02005065 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005066
Pavol Vican855ca622016-09-05 13:07:54 +02005067 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005068 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005069 for (parent = node->parent;
5070 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5071 parent = parent->parent) {
5072 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5073 /* stop also on presence containers */
5074 break;
5075 }
5076 }
5077 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5078 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5079 if (lyp_check_mandatory_choice(parent)) {
5080 goto fail;
5081 }
5082 }
5083 }
5084 }
5085 free(refine_nodes);
5086
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005087 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005088
5089fail:
5090 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5091 lys_node_free(iter, NULL, 0);
5092 }
Pavol Vican855ca622016-09-05 13:07:54 +02005093 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005094 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005095}
5096
Radek Krejci83a4bac2017-02-07 15:53:04 +01005097void
5098resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005099{
5100 int i;
5101
5102 assert(der && base);
5103
Radek Krejci018f1f52016-08-03 16:01:20 +02005104 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005105 /* create a set for backlinks if it does not exist */
5106 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005107 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005108 /* store backlink */
5109 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005110
Radek Krejci85a54be2016-10-20 12:39:56 +02005111 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005112 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005113 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005114 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005115}
5116
Michal Vasko730dfdf2015-08-11 14:48:05 +02005117/**
5118 * @brief Resolve base identity recursively. Does not log.
5119 *
5120 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005121 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005122 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005123 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005124 *
Radek Krejci219fa612016-08-15 10:36:51 +02005125 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005126 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005127static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005128resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005129 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005130{
Michal Vaskof02e3742015-08-05 16:27:02 +02005131 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005132 struct lys_ident *base = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005133
Radek Krejcicf509982015-12-15 09:22:44 +01005134 assert(ret);
5135
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005136 /* search module */
5137 for (i = 0; i < module->ident_size; i++) {
5138 if (!strcmp(basename, module->ident[i].name)) {
5139
5140 if (!ident) {
5141 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005142 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005143 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005144 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005145 }
5146
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005147 base = &module->ident[i];
5148 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005149 }
5150 }
5151
5152 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005153 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5154 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5155 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005156
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005157 if (!ident) {
5158 *ret = &module->inc[j].submodule->ident[i];
5159 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005160 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005161
5162 base = &module->inc[j].submodule->ident[i];
5163 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005164 }
5165 }
5166 }
5167
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005168matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005169 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005170 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005171 /* is it already completely resolved? */
5172 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005173 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005174 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5175
5176 /* simple check for circular reference,
5177 * the complete check is done as a side effect of using only completely
5178 * resolved identities (previous check of unres content) */
5179 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
5180 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5181 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005182 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005183 }
5184
Radek Krejci06f64ed2016-08-15 11:07:44 +02005185 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005186 }
5187 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005188
Radek Krejcibabbff82016-02-19 13:31:37 +01005189 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005190 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005191 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005192 }
5193
Radek Krejci219fa612016-08-15 10:36:51 +02005194 /* base not found (maybe a forward reference) */
5195 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005196}
5197
Michal Vasko730dfdf2015-08-11 14:48:05 +02005198/**
5199 * @brief Resolve base identity. Logs directly.
5200 *
5201 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005202 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005203 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005204 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005205 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005206 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005207 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005208 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005209static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005210resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005211 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005212{
5213 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005214 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005215 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005216 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005217 struct lys_module *mod;
5218
5219 assert((ident && !type) || (!ident && type));
5220
5221 if (!type) {
5222 /* have ident to resolve */
5223 ret = &target;
5224 flags = ident->flags;
5225 mod = ident->module;
5226 } else {
5227 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005228 ++type->info.ident.count;
5229 type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref);
Radek Krejciaa1303c2017-05-31 13:57:37 +02005230 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005231
5232 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005233 flags = type->parent->flags;
5234 mod = type->parent->module;
5235 }
Michal Vaskof2006002016-04-21 16:28:15 +02005236 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005237
5238 /* search for the base identity */
5239 name = strchr(basename, ':');
5240 if (name) {
5241 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005242 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005243 name++;
5244
Michal Vasko2d851a92015-10-20 16:16:36 +02005245 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005246 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005247 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005248 }
5249 } else {
5250 name = basename;
5251 }
5252
Radek Krejcic071c542016-01-27 14:57:51 +01005253 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005254 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005255 if (!module) {
5256 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01005257 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005258 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005259 }
5260
Radek Krejcic071c542016-01-27 14:57:51 +01005261 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005262 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5263 if (!rc) {
5264 assert(*ret);
5265
5266 /* check status */
5267 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5268 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5269 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005270 } else if (ident) {
5271 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005272 if (lys_main_module(mod)->implemented) {
5273 /* in case of the implemented identity, maintain backlinks to it
5274 * from the base identities to make it available when resolving
5275 * data with the identity values (not implemented identity is not
5276 * allowed as an identityref value). */
5277 resolve_identity_backlink_update(ident, *ret);
5278 }
Radek Krejci219fa612016-08-15 10:36:51 +02005279 }
5280 } else if (rc == EXIT_FAILURE) {
5281 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005282 if (type) {
5283 --type->info.ident.count;
5284 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005285 }
5286
Radek Krejci219fa612016-08-15 10:36:51 +02005287 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005288}
5289
Radek Krejci9e6af732017-04-27 14:40:25 +02005290/*
5291 * 1 - true (der is derived from base)
5292 * 0 - false (der is not derived from base)
5293 */
5294static int
5295search_base_identity(struct lys_ident *der, struct lys_ident *base)
5296{
5297 int i;
5298
5299 if (der == base) {
5300 return 1;
5301 } else {
5302 for(i = 0; i < der->base_size; i++) {
5303 if (search_base_identity(der->base[i], base) == 1) {
5304 return 1;
5305 }
5306 }
5307 }
5308
5309 return 0;
5310}
5311
Michal Vasko730dfdf2015-08-11 14:48:05 +02005312/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005313 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005314 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005315 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005316 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005317 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005318 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005319 *
5320 * @return Pointer to the identity resolvent, NULL on error.
5321 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005322struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005323resolve_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 +02005324{
Radek Krejci9e6af732017-04-27 14:40:25 +02005325 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005326 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005327 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005328 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005329 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005330 struct lys_ident *der, *cur;
Radek Krejci9e6af732017-04-27 14:40:25 +02005331 struct lys_module *imod = NULL, *m;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005332
Radek Krejci9e6af732017-04-27 14:40:25 +02005333 assert(type && ident_name && node && mod);
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005334
Michal Vaskof2d43962016-09-02 11:10:16 +02005335 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005336 return NULL;
5337 }
5338
Michal Vasko50576712017-07-28 12:28:33 +02005339 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005340 if (rc < 1) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005341 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005342 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005343 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005344 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005345 return NULL;
5346 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005347
5348 m = lys_main_module(mod); /* shortcut */
5349 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5350 /* identity is defined in the same module as node */
5351 imod = m;
5352 } else if (dflt) {
5353 /* solving identityref in default definition in schema -
5354 * find the identity's module in the imported modules list to have a correct revision */
5355 for (i = 0; i < mod->imp_size; i++) {
5356 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5357 imod = mod->imp[i].module;
5358 break;
5359 }
5360 }
5361 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005362 /* solving identityref in data - get the module from the context */
5363 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5364 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005365 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005366 break;
5367 }
Michal Vasko08767f72017-10-06 14:38:08 +02005368 imod = NULL;
5369 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005370 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5371 /* we are currently parsing some module and checking XPath or a default value,
5372 * so take this module into account */
5373 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5374 imod = mod->ctx->models.parsing_sub_modules[i];
5375 if (imod->type) {
5376 /* skip submodules */
5377 continue;
5378 }
5379 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5380 break;
5381 }
5382 imod = NULL;
5383 }
5384 }
Michal Vasko08767f72017-10-06 14:38:08 +02005385 }
5386
5387 if (!dflt && (!imod || !imod->implemented) && mod->ctx->data_clb) {
5388 /* the needed module was not found, but it may have been expected so call the data callback */
5389 if (imod) {
5390 mod->ctx->data_clb(mod->ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, mod->ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005391 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005392 str = strndup(mod_name, mod_name_len);
5393 imod = (struct lys_module *)mod->ctx->data_clb(mod->ctx, str, NULL, 0, mod->ctx->data_clb_data);
5394 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005395 }
5396 }
5397 if (!imod) {
5398 goto fail;
5399 }
5400
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005401 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005402 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005403 * THEN, we may need to make the module with the identity implemented, but only if it really
5404 * contains the identity */
5405 if (!imod->implemented) {
5406 cur = NULL;
5407 /* get the identity in the module */
5408 for (i = 0; i < imod->ident_size; i++) {
5409 if (!strcmp(name, imod->ident[i].name)) {
5410 cur = &imod->ident[i];
5411 break;
5412 }
5413 }
5414 if (!cur) {
5415 /* go through includes */
5416 for (j = 0; j < imod->inc_size; j++) {
5417 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5418 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5419 cur = &imod->inc[j].submodule->ident[i];
5420 break;
5421 }
5422 }
5423 }
5424 if (!cur) {
5425 goto fail;
5426 }
5427 }
5428
5429 /* check that identity is derived from one of the type's base */
5430 while (type->der) {
5431 for (i = 0; i < type->info.ident.count; i++) {
5432 if (search_base_identity(cur, type->info.ident.ref[i])) {
5433 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005434 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005435 goto match;
5436 }
5437 }
5438 type = &type->der->type;
5439 }
5440 /* matching base not found */
5441 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
5442 goto fail;
5443 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005444 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005445
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005446 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005447 while (type->der) {
5448 for (i = 0; i < type->info.ident.count; ++i) {
5449 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005450
Radek Krejci85a54be2016-10-20 12:39:56 +02005451 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005452 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005453 for (j = 0; j < cur->der->number; j++) {
5454 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005455 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005456 /* we have match */
5457 cur = der;
5458 goto match;
5459 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005460 }
5461 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005462 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005463 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005464 }
5465
Radek Krejci9e6af732017-04-27 14:40:25 +02005466fail:
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005467 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005468 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005469
5470match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005471 for (i = 0; i < cur->iffeature_size; i++) {
5472 if (!resolve_iffeature(&cur->iffeature[i])) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005473 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01005474 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005475 return NULL;
5476 }
5477 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005478 if (need_implemented) {
5479 if (dflt) {
5480 /* try to make the module implemented */
5481 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5482 imod->name, cur->name, mod->name);
5483 if (lys_set_implemented(imod)) {
5484 LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
5485 imod->name, cur->name);
5486 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
5487 goto fail;
5488 }
5489 } else {
5490 /* just say that it was found, but in a non-implemented module */
5491 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
5492 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005493 goto fail;
5494 }
5495 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005496 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005497}
5498
Michal Vasko730dfdf2015-08-11 14:48:05 +02005499/**
Michal Vaskobb211122015-08-19 14:03:11 +02005500 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005501 *
Michal Vaskobb211122015-08-19 14:03:11 +02005502 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005503 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005504 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005505 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005506 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005507static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005508resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005509{
Radek Krejci93def382017-05-24 15:33:48 +02005510 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005511 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005512
Radek Krejci6ff885d2017-01-03 14:06:22 +01005513 /* 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 +02005514 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5515 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5516 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005517 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 +02005518
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005519 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005520 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5521 if (rc == -1) {
Michal Vasko92981a62016-10-14 10:25:16 +02005522 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005523 return -1;
5524 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005525 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005526 return -1;
5527 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005528 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005529 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
5530 LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping.");
5531 return -1;
5532 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005533 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005534 }
Michal Vasko92981a62016-10-14 10:25:16 +02005535 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005536 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005537 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005538 }
5539
Radek Krejci93def382017-05-24 15:33:48 +02005540 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005541 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005542 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
5543 LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping.");
5544 return -1;
5545 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005546 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005547 } else {
5548 /* instantiate grouping only when it is completely resolved */
5549 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005550 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005551 return EXIT_FAILURE;
5552 }
5553
Radek Krejci48464ed2016-03-17 15:44:09 +01005554 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005555 if (!rc) {
5556 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005557 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005558 assert(((struct lys_node_grp *)par_grp)->unres_count);
5559 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01005560 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005561 }
Radek Krejcicf509982015-12-15 09:22:44 +01005562
5563 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005564 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005565 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005566 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01005567 return -1;
5568 }
5569
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005570 return EXIT_SUCCESS;
5571 }
5572
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005573 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005574}
5575
Michal Vasko730dfdf2015-08-11 14:48:05 +02005576/**
Michal Vasko9957e592015-08-17 15:04:09 +02005577 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005578 *
Michal Vaskobb211122015-08-19 14:03:11 +02005579 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005580 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005581 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005582 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005583 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005584static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005585resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005586{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005587 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01005588 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02005589 char *s = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005590
5591 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02005592 assert(keys_str);
5593
Radek Krejci5c08a992016-11-02 13:30:04 +01005594 if (!list->child) {
5595 /* no child, possible forward reference */
5596 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
5597 return EXIT_FAILURE;
5598 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005599 /* get the key name */
5600 if ((value = strpbrk(keys_str, " \t\n"))) {
5601 len = value - keys_str;
5602 while (isspace(value[0])) {
5603 value++;
5604 }
5605 } else {
5606 len = strlen(keys_str);
5607 }
5608
Radek Krejcia98048c2017-05-24 16:35:48 +02005609 if (list->keys[i]) {
5610 /* skip already resolved keys */
5611 goto next;
5612 }
5613
Michal Vaskobb520442017-05-23 10:55:18 +02005614 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
5615 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005616 if (rc) {
Radek Krejcia98048c2017-05-24 16:35:48 +02005617 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02005618 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005619 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005620
Radek Krejci48464ed2016-03-17 15:44:09 +01005621 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005622 /* check_key logs */
5623 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005624 }
5625
Radek Krejcicf509982015-12-15 09:22:44 +01005626 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005627 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005628 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
5629 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01005630 return -1;
5631 }
5632
Radek Krejcia98048c2017-05-24 16:35:48 +02005633 /* default value - is ignored, keep it but print a warning */
5634 if (list->keys[i]->dflt) {
5635 /* log is not hidden only in case this resolving fails and in such a case
5636 * we cannot get here
5637 */
Michal Vasko4814fb02017-08-17 14:49:38 +02005638 assert(ly_err_main.vlog_hide);
Radek Krejcia98048c2017-05-24 16:35:48 +02005639 ly_vlog_hide(0);
5640 LOGWRN("Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01005641 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Radek Krejcia98048c2017-05-24 16:35:48 +02005642 ly_vlog_hide(1);
5643 free(s);
5644 }
5645
5646next:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005647 /* prepare for next iteration */
5648 while (value && isspace(value[0])) {
5649 value++;
5650 }
5651 keys_str = value;
5652 }
5653
Michal Vaskof02e3742015-08-05 16:27:02 +02005654 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005655}
5656
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005657/**
Michal Vaskobf19d252015-10-08 15:39:17 +02005658 * @brief Resolve (check) all must conditions of \p node.
5659 * Logs directly.
5660 *
5661 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005662 * @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 +02005663 *
5664 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
5665 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005666static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005667resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02005668{
Michal Vaskobf19d252015-10-08 15:39:17 +02005669 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005670 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02005671 struct lys_restr *must;
5672 struct lyxp_set set;
5673
5674 assert(node);
5675 memset(&set, 0, sizeof set);
5676
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005677 if (inout_parent) {
5678 for (schema = lys_parent(node->schema);
5679 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
5680 schema = lys_parent(schema));
5681 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
5682 LOGINT;
5683 return -1;
5684 }
5685 must_size = ((struct lys_node_inout *)schema)->must_size;
5686 must = ((struct lys_node_inout *)schema)->must;
5687
5688 /* context node is the RPC/action */
5689 node = node->parent;
5690 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
5691 LOGINT;
5692 return -1;
5693 }
5694 } else {
5695 switch (node->schema->nodetype) {
5696 case LYS_CONTAINER:
5697 must_size = ((struct lys_node_container *)node->schema)->must_size;
5698 must = ((struct lys_node_container *)node->schema)->must;
5699 break;
5700 case LYS_LEAF:
5701 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
5702 must = ((struct lys_node_leaf *)node->schema)->must;
5703 break;
5704 case LYS_LEAFLIST:
5705 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
5706 must = ((struct lys_node_leaflist *)node->schema)->must;
5707 break;
5708 case LYS_LIST:
5709 must_size = ((struct lys_node_list *)node->schema)->must_size;
5710 must = ((struct lys_node_list *)node->schema)->must;
5711 break;
5712 case LYS_ANYXML:
5713 case LYS_ANYDATA:
5714 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
5715 must = ((struct lys_node_anydata *)node->schema)->must;
5716 break;
5717 case LYS_NOTIF:
5718 must_size = ((struct lys_node_notif *)node->schema)->must_size;
5719 must = ((struct lys_node_notif *)node->schema)->must;
5720 break;
5721 default:
5722 must_size = 0;
5723 break;
5724 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005725 }
5726
5727 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005728 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005729 return -1;
5730 }
5731
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005732 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02005733
Michal Vasko8146d4c2016-05-09 15:50:29 +02005734 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02005735 if ((ignore_fail == 1) || ((must[i].flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005736 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
5737 } else {
5738 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
5739 if (must[i].emsg) {
Michal Vasko51e5c582017-01-19 14:16:39 +01005740 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005741 }
5742 if (must[i].eapptag) {
5743 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
5744 }
5745 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02005746 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005747 }
5748 }
5749
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005750 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02005751}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005752
Michal Vaskobf19d252015-10-08 15:39:17 +02005753/**
Michal Vasko508a50d2016-09-07 14:50:33 +02005754 * @brief Resolve (find) when condition schema context node. Does not log.
5755 *
5756 * @param[in] schema Schema node with the when condition.
5757 * @param[out] ctx_snode When schema context node.
5758 * @param[out] ctx_snode_type Schema context node type.
5759 */
5760void
5761resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
5762{
5763 const struct lys_node *sparent;
5764
5765 /* find a not schema-only node */
5766 *ctx_snode_type = LYXP_NODE_ELEM;
5767 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
5768 if (schema->nodetype == LYS_AUGMENT) {
5769 sparent = ((struct lys_node_augment *)schema)->target;
5770 } else {
5771 sparent = schema->parent;
5772 }
5773 if (!sparent) {
5774 /* context node is the document root (fake root in our case) */
5775 if (schema->flags & LYS_CONFIG_W) {
5776 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
5777 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02005778 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02005779 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02005780 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskob94a5e42016-09-08 14:01:56 +02005781 schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0);
Michal Vasko508a50d2016-09-07 14:50:33 +02005782 break;
5783 }
5784 schema = sparent;
5785 }
5786
5787 *ctx_snode = (struct lys_node *)schema;
5788}
5789
5790/**
Michal Vaskocf024702015-10-08 15:01:42 +02005791 * @brief Resolve (find) when condition context node. Does not log.
5792 *
5793 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02005794 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02005795 * @param[out] ctx_node Context node.
5796 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02005797 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02005798 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02005799 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02005800static int
5801resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
5802 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005803{
Michal Vaskocf024702015-10-08 15:01:42 +02005804 struct lyd_node *parent;
5805 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005806 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02005807 uint16_t i, data_depth, schema_depth;
5808
Michal Vasko508a50d2016-09-07 14:50:33 +02005809 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02005810
Michal Vaskofe989752016-09-08 08:47:26 +02005811 if (node_type == LYXP_NODE_ELEM) {
5812 /* standard element context node */
5813 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
5814 for (sparent = schema, schema_depth = 0;
5815 sparent;
5816 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
5817 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
5818 ++schema_depth;
5819 }
Michal Vaskocf024702015-10-08 15:01:42 +02005820 }
Michal Vaskofe989752016-09-08 08:47:26 +02005821 if (data_depth < schema_depth) {
5822 return -1;
5823 }
Michal Vaskocf024702015-10-08 15:01:42 +02005824
Michal Vasko956e8542016-08-26 09:43:35 +02005825 /* find the corresponding data node */
5826 for (i = 0; i < data_depth - schema_depth; ++i) {
5827 node = node->parent;
5828 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02005829 if (node->schema != schema) {
5830 return -1;
5831 }
Michal Vaskofe989752016-09-08 08:47:26 +02005832 } else {
5833 /* root context node */
5834 while (node->parent) {
5835 node = node->parent;
5836 }
5837 while (node->prev->next) {
5838 node = node->prev;
5839 }
Michal Vaskocf024702015-10-08 15:01:42 +02005840 }
5841
Michal Vaskoa59495d2016-08-22 09:18:58 +02005842 *ctx_node = node;
5843 *ctx_node_type = node_type;
5844 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02005845}
5846
Michal Vasko76c3bd32016-08-24 16:02:52 +02005847/**
5848 * @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 +01005849 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02005850 *
5851 * @param[in] snode Schema node, whose children instances need to be unlinked.
5852 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
5853 * it is moved to point to another sibling still in the original tree.
5854 * @param[in,out] ctx_node When context node, adjusted if needed.
5855 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
5856 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
5857 * Ordering may change, but there will be no semantic change.
5858 *
5859 * @return EXIT_SUCCESS on success, -1 on error.
5860 */
5861static int
5862resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
5863 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
5864{
5865 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005866 const struct lys_node *slast;
Michal Vasko76c3bd32016-08-24 16:02:52 +02005867
5868 switch (snode->nodetype) {
5869 case LYS_AUGMENT:
5870 case LYS_USES:
5871 case LYS_CHOICE:
5872 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005873 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01005874 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005875 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
5876 continue;
5877 }
5878
5879 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005880 return -1;
5881 }
5882 }
5883 break;
5884 case LYS_CONTAINER:
5885 case LYS_LIST:
5886 case LYS_LEAF:
5887 case LYS_LEAFLIST:
5888 case LYS_ANYXML:
5889 case LYS_ANYDATA:
5890 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
5891 if (elem->schema == snode) {
5892
5893 if (elem == *ctx_node) {
5894 /* We are going to unlink our context node! This normally cannot happen,
5895 * but we use normal top-level data nodes for faking a document root node,
5896 * so if this is the context node, we just use the next top-level node.
5897 * Additionally, it can even happen that there are no top-level data nodes left,
5898 * all were unlinked, so in this case we pass NULL as the context node/data tree,
5899 * lyxp_eval() can handle this special situation.
5900 */
5901 if (ctx_node_type == LYXP_NODE_ELEM) {
5902 LOGINT;
5903 return -1;
5904 }
5905
5906 if (elem->prev == elem) {
5907 /* unlinking last top-level element, use an empty data tree */
5908 *ctx_node = NULL;
5909 } else {
5910 /* in this case just use the previous/last top-level data node */
5911 *ctx_node = elem->prev;
5912 }
5913 } else if (elem == *node) {
5914 /* We are going to unlink the currently processed node. This does not matter that
5915 * much, but we would lose access to the original data tree, so just move our
5916 * pointer somewhere still inside it.
5917 */
5918 if ((*node)->prev != *node) {
5919 *node = (*node)->prev;
5920 } else {
5921 /* the processed node with sibings were all unlinked, oh well */
5922 *node = NULL;
5923 }
5924 }
5925
5926 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01005927 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005928 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005929 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005930 LOGINT;
5931 return -1;
5932 }
5933 } else {
5934 *unlinked_nodes = elem;
5935 }
5936
5937 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
5938 /* there can be only one instance */
5939 break;
5940 }
5941 }
5942 }
5943 break;
5944 default:
5945 LOGINT;
5946 return -1;
5947 }
5948
5949 return EXIT_SUCCESS;
5950}
5951
5952/**
5953 * @brief Relink the unlinked nodes back.
5954 *
5955 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
5956 * we simply need a sibling from the original data tree.
5957 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
5958 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
5959 * or the sibling of \p unlinked_nodes.
5960 *
5961 * @return EXIT_SUCCESS on success, -1 on error.
5962 */
5963static int
5964resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
5965{
5966 struct lyd_node *elem;
5967
5968 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005969 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005970 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005971 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005972 return -1;
5973 }
5974 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005975 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005976 return -1;
5977 }
5978 }
5979 }
5980
5981 return EXIT_SUCCESS;
5982}
5983
Radek Krejci03b71f72016-03-16 11:10:09 +01005984int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005985resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01005986{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005987 int ret = 0;
5988 uint8_t must_size;
5989 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02005990
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005991 assert(node);
5992
5993 schema = node->schema;
5994
5995 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02005996 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01005997 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005998 must_size = ((struct lys_node_container *)schema)->must_size;
5999 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006000 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006001 must_size = ((struct lys_node_leaf *)schema)->must_size;
6002 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006003 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006004 must_size = ((struct lys_node_leaflist *)schema)->must_size;
6005 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006006 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006007 must_size = ((struct lys_node_list *)schema)->must_size;
6008 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006009 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02006010 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006011 must_size = ((struct lys_node_anydata *)schema)->must_size;
6012 break;
6013 case LYS_NOTIF:
6014 must_size = ((struct lys_node_notif *)schema)->must_size;
6015 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006016 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006017 must_size = 0;
6018 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006019 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006020
6021 if (must_size) {
6022 ++ret;
6023 }
6024
6025 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6026 if (!node->prev->next) {
6027 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6028 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6029 ret += 0x2;
6030 }
6031 }
6032
6033 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006034}
6035
6036int
Radek Krejci46165822016-08-26 14:06:27 +02006037resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006038{
Radek Krejci46165822016-08-26 14:06:27 +02006039 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006040
Radek Krejci46165822016-08-26 14:06:27 +02006041 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006042
Radek Krejci46165822016-08-26 14:06:27 +02006043 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006044 return 1;
6045 }
6046
Radek Krejci46165822016-08-26 14:06:27 +02006047 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006048 goto check_augment;
6049
Radek Krejci46165822016-08-26 14:06:27 +02006050 while (parent) {
6051 /* stop conditions */
6052 if (!mode) {
6053 /* stop on node that can be instantiated in data tree */
6054 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6055 break;
6056 }
6057 } else {
6058 /* stop on the specified node */
6059 if (parent == stop) {
6060 break;
6061 }
6062 }
6063
6064 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006065 return 1;
6066 }
6067check_augment:
6068
6069 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02006070 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006071 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006072 }
6073 parent = lys_parent(parent);
6074 }
6075
6076 return 0;
6077}
6078
Michal Vaskocf024702015-10-08 15:01:42 +02006079/**
6080 * @brief Resolve (check) all when conditions relevant for \p node.
6081 * Logs directly.
6082 *
6083 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006084 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6085 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006086 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006087 * @return
6088 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006089 * 0 - all "when" statements true
6090 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006091 * 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 +02006092 */
Radek Krejci46165822016-08-26 14:06:27 +02006093int
Michal Vasko0b963112017-08-11 12:45:36 +02006094resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006095{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006096 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006097 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006098 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006099 enum lyxp_node_type ctx_node_type;
Radek Krejci51093642016-03-29 10:14:59 +02006100 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006101
6102 assert(node);
6103 memset(&set, 0, sizeof set);
6104
Michal Vasko78d97e22017-02-21 09:54:38 +01006105 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006106 /* make the node dummy for the evaluation */
6107 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006108 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
6109 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006110 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006111 if (rc) {
6112 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006113 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006114 }
Radek Krejci51093642016-03-29 10:14:59 +02006115 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006116 }
6117
Radek Krejci03b71f72016-03-16 11:10:09 +01006118 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006119 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006120 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006121 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006122 if ((ignore_fail == 1)
6123 || ((((struct lys_node_container *)node->schema)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006124 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6125 ((struct lys_node_container *)node->schema)->when->cond);
6126 } else {
6127 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006128 if (failed_when) {
6129 *failed_when = ((struct lys_node_container *)node->schema)->when;
6130 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006131 goto cleanup;
6132 }
Michal Vaskocf024702015-10-08 15:01:42 +02006133 }
Radek Krejci51093642016-03-29 10:14:59 +02006134
6135 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006136 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006137 }
6138
Michal Vasko90fc2a32016-08-24 15:58:58 +02006139 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006140 goto check_augment;
6141
6142 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006143 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6144 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006145 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006146 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006147 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02006148 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02006149 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006150 }
6151 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006152
6153 unlinked_nodes = NULL;
6154 /* we do not want our node pointer to change */
6155 tmp_node = node;
6156 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6157 if (rc) {
6158 goto cleanup;
6159 }
6160
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006161 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6162 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006163
6164 if (unlinked_nodes && ctx_node) {
6165 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6166 rc = -1;
6167 goto cleanup;
6168 }
6169 }
6170
Radek Krejci03b71f72016-03-16 11:10:09 +01006171 if (rc) {
6172 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006173 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006174 }
Radek Krejci51093642016-03-29 10:14:59 +02006175 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006176 }
6177
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006178 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006179 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02006180 if ((ignore_fail == 1)
6181 || ((((struct lys_node_uses *)sparent)->when->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006182 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6183 ((struct lys_node_uses *)sparent)->when->cond);
6184 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006185 node->when_status |= LYD_WHEN_FALSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006186 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006187 if (failed_when) {
6188 *failed_when = ((struct lys_node_uses *)sparent)->when;
6189 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006190 goto cleanup;
6191 }
Michal Vaskocf024702015-10-08 15:01:42 +02006192 }
Radek Krejci51093642016-03-29 10:14:59 +02006193
6194 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006195 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006196 }
6197
6198check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006199 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006200 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006201 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006202 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02006203 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02006204 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006205 }
6206 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006207
6208 unlinked_nodes = NULL;
6209 tmp_node = node;
6210 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6211 if (rc) {
6212 goto cleanup;
6213 }
6214
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006215 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6216 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006217
6218 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6219 * so the tree did not actually change and there is nothing for us to do
6220 */
6221 if (unlinked_nodes && ctx_node) {
6222 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6223 rc = -1;
6224 goto cleanup;
6225 }
6226 }
6227
Radek Krejci03b71f72016-03-16 11:10:09 +01006228 if (rc) {
6229 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006230 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006231 }
Radek Krejci51093642016-03-29 10:14:59 +02006232 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006233 }
6234
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006235 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006236 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006237 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006238 if ((ignore_fail == 1)
6239 || ((((struct lys_node_augment *)sparent->parent)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006240 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006241 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006242 } else {
6243 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006244 if (failed_when) {
6245 *failed_when = ((struct lys_node_augment *)sparent->parent)->when;
6246 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006247 goto cleanup;
6248 }
Michal Vaskocf024702015-10-08 15:01:42 +02006249 }
Radek Krejci51093642016-03-29 10:14:59 +02006250
6251 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006252 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006253 }
6254
Michal Vasko90fc2a32016-08-24 15:58:58 +02006255 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006256 }
6257
Radek Krejci0b7704f2016-03-18 12:16:14 +01006258 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006259
Radek Krejci51093642016-03-29 10:14:59 +02006260cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006261 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006262 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006263 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006264}
6265
Radek Krejcicbb473e2016-09-16 14:48:32 +02006266static int
6267check_leafref_features(struct lys_type *type)
6268{
6269 struct lys_node *iter;
6270 struct ly_set *src_parents, *trg_parents, *features;
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006271 struct lys_node_augment *aug;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006272 unsigned int i, j, size, x;
6273 int ret = EXIT_SUCCESS;
6274
6275 assert(type->parent);
6276
6277 src_parents = ly_set_new();
6278 trg_parents = ly_set_new();
6279 features = ly_set_new();
6280
6281 /* get parents chain of source (leafref) */
Radek Krejciecda01a2017-04-05 15:44:27 +02006282 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006283 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6284 continue;
6285 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006286 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006287 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006288 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006289 /* unresolved augment, wait until it's resolved */
Michal Vaskobd026102017-11-21 13:43:01 +01006290 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug,
6291 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006292 ret = EXIT_FAILURE;
6293 goto cleanup;
6294 }
6295 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006296 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
6297 }
6298 /* get parents chain of target */
Radek Krejciecda01a2017-04-05 15:44:27 +02006299 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006300 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6301 continue;
6302 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006303 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006304 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006305 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006306 /* unresolved augment, wait until it's resolved */
Michal Vaskobd026102017-11-21 13:43:01 +01006307 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug,
6308 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006309 ret = EXIT_FAILURE;
6310 goto cleanup;
6311 }
6312 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006313 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
6314 }
6315
6316 /* compare the features used in if-feature statements in the rest of both
6317 * chains of parents. The set of features used for target must be a subset
6318 * of features used for the leafref. This is not a perfect, we should compare
6319 * the truth tables but it could require too much resources, so we simplify that */
6320 for (i = 0; i < src_parents->number; i++) {
6321 iter = src_parents->set.s[i]; /* shortcut */
6322 if (!iter->iffeature_size) {
6323 continue;
6324 }
6325 for (j = 0; j < iter->iffeature_size; j++) {
6326 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6327 for (; size; size--) {
6328 if (!iter->iffeature[j].features[size - 1]) {
6329 /* not yet resolved feature, postpone this check */
6330 ret = EXIT_FAILURE;
6331 goto cleanup;
6332 }
6333 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
6334 }
6335 }
6336 }
6337 x = features->number;
6338 for (i = 0; i < trg_parents->number; i++) {
6339 iter = trg_parents->set.s[i]; /* shortcut */
6340 if (!iter->iffeature_size) {
6341 continue;
6342 }
6343 for (j = 0; j < iter->iffeature_size; j++) {
6344 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6345 for (; size; size--) {
6346 if (!iter->iffeature[j].features[size - 1]) {
6347 /* not yet resolved feature, postpone this check */
6348 ret = EXIT_FAILURE;
6349 goto cleanup;
6350 }
6351 if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
6352 /* the feature is not present in features set of target's parents chain */
6353 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
Michal Vasko51e5c582017-01-19 14:16:39 +01006354 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcicbb473e2016-09-16 14:48:32 +02006355 "Leafref is not conditional based on \"%s\" feature as its target.",
6356 iter->iffeature[j].features[size - 1]->name);
6357 ret = -1;
6358 goto cleanup;
6359 }
6360 }
6361 }
6362 }
6363
6364cleanup:
6365 ly_set_free(features);
6366 ly_set_free(src_parents);
6367 ly_set_free(trg_parents);
6368
6369 return ret;
6370}
6371
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006372static int
6373check_type_union_leafref(struct lys_type *type)
6374{
6375 uint8_t i;
6376
6377 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6378 /* go through unions and look for leafref */
6379 for (i = 0; i < type->info.uni.count; ++i) {
6380 switch (type->info.uni.types[i].base) {
6381 case LY_TYPE_LEAFREF:
6382 return 1;
6383 case LY_TYPE_UNION:
6384 if (check_type_union_leafref(&type->info.uni.types[i])) {
6385 return 1;
6386 }
6387 break;
6388 default:
6389 break;
6390 }
6391 }
6392
6393 return 0;
6394 }
6395
6396 /* just inherit the flag value */
6397 return type->der->has_union_leafref;
6398}
6399
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006400/**
Michal Vaskobb211122015-08-19 14:03:11 +02006401 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006402 *
6403 * @param[in] mod Main module.
6404 * @param[in] item Item to resolve. Type determined by \p type.
6405 * @param[in] type Type of the unresolved item.
6406 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006407 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006408 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006409 *
6410 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6411 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006412static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006413resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006414 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006415{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006416 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vaskobe136f62017-09-21 12:08:39 +02006417 int rc = -1, has_str = 0, parent_type = 0, i, k, hidden;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006418 unsigned int j;
Radek Krejci80056d52017-01-05 13:13:33 +01006419 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006420 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006421 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006422
Radek Krejcic79c6b12016-07-26 15:11:49 +02006423 struct ly_set *refs, *procs;
6424 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006425 struct lys_ident *ident;
6426 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006427 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006428 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006429 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006430 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006431 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006432 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006433 struct lys_ext_instance *ext, **extlist;
6434 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006435
6436 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006437 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006438 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006439 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006440 ident = item;
6441
Radek Krejci018f1f52016-08-03 16:01:20 +02006442 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006443 break;
6444 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006445 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006446 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006447 stype = item;
6448
Radek Krejci018f1f52016-08-03 16:01:20 +02006449 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006450 break;
6451 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006452 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006453 stype = item;
6454
Michal Vasko1c007172017-03-10 10:20:44 +01006455 rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target);
6456 if (!rc) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02006457 assert(stype->info.lref.target);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006458
Michal Vaskobb520442017-05-23 10:55:18 +02006459 if (lys_node_module(node)->implemented) {
Michal Vaskobe136f62017-09-21 12:08:39 +02006460 /* make all the modules on the path implemented, print verbose messages */
6461 hidden = ly_vlog_hidden;
6462 if (hidden) {
6463 ly_vlog_hide(0);
6464 }
6465
Michal Vaskobb520442017-05-23 10:55:18 +02006466 for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) {
6467 if (!lys_node_module(next)->implemented) {
6468 if (lys_set_implemented(lys_node_module(next))) {
6469 rc = -1;
6470 break;
6471 }
6472 }
6473 }
6474 if (next) {
6475 break;
6476 }
6477
6478 /* store the backlink from leafref target */
6479 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
6480 rc = -1;
6481 }
Michal Vaskobe136f62017-09-21 12:08:39 +02006482
6483 if (hidden) {
6484 ly_vlog_hide(1);
6485 }
6486
6487 if (rc) {
6488 break;
6489 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006490 }
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006491
6492 /* check if leafref and its target are under common if-features */
6493 rc = check_leafref_features(stype);
Radek Krejci46c4cd72016-01-21 15:13:52 +01006494 }
6495
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006496 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006497 case UNRES_TYPE_DER_EXT:
6498 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006499 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006500 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006501 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006502 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006503 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006504 /* parent */
6505 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006506 stype = item;
6507
Michal Vasko88c29542015-11-27 14:57:53 +01006508 /* HACK type->der is temporarily unparsed type statement */
6509 yin = (struct lyxml_elem *)stype->der;
6510 stype->der = NULL;
6511
Pavol Vicana0e4e672016-02-24 12:20:04 +01006512 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6513 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006514 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006515
6516 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006517 /* may try again later */
6518 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006519 } else {
6520 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02006521 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006522 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006523 }
6524
Michal Vasko88c29542015-11-27 14:57:53 +01006525 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006526 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006527 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006528 /* we need to always be able to free this, it's safe only in this case */
6529 lyxml_free(mod->ctx, yin);
6530 } else {
6531 /* may try again later, put all back how it was */
6532 stype->der = (struct lys_tpdf *)yin;
6533 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006534 }
Radek Krejcic13db382016-08-16 10:52:42 +02006535 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006536 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006537 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006538 LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
6539 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006540
6541 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6542 /* fill typedef union leafref flag */
6543 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6544 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6545 /* copy the type in case it has union leafref flag */
6546 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
6547 LOGERR(LY_EINT, "Failed to duplicate type.");
6548 return -1;
6549 }
6550 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006551 } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +02006552 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6553 * 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 +02006554 * grouping. The grouping cannot be used unless the unres counter is 0.
6555 * To remember that the grouping already increased the counter, the LY_TYPE_ERR is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006556 * of the type's base member. */
6557 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6558 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006559 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
6560 LOGERR(LY_EINT, "Too many unresolved items (type) inside a grouping.");
6561 return -1;
6562 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006563 stype->base = LY_TYPE_ERR;
Radek Krejcic13db382016-08-16 10:52:42 +02006564 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006565 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006566 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006567 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006568 iff_data = str_snode;
6569 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006570 if (!rc) {
6571 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006572 if (iff_data->infeature) {
6573 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6574 feat = *((struct lys_feature **)item);
6575 if (!feat->depfeatures) {
6576 feat->depfeatures = ly_set_new();
6577 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006578 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006579 }
6580 /* cleanup temporary data */
Radek Krejcicbb473e2016-09-16 14:48:32 +02006581 lydict_remove(mod->ctx, iff_data->fname);
6582 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006583 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006584 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006585 case UNRES_FEATURE:
6586 feat = (struct lys_feature *)item;
6587
6588 if (feat->iffeature_size) {
6589 refs = ly_set_new();
6590 procs = ly_set_new();
6591 ly_set_add(procs, feat, 0);
6592
6593 while (procs->number) {
6594 ref = procs->set.g[procs->number - 1];
6595 ly_set_rm_index(procs, procs->number - 1);
6596
6597 for (i = 0; i < ref->iffeature_size; i++) {
6598 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6599 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006600 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006601 if (ref->iffeature[i].features[j - 1] == feat) {
6602 LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
6603 goto featurecheckdone;
6604 }
6605
6606 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6607 k = refs->number;
6608 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6609 /* not yet seen feature, add it for processing */
6610 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6611 }
6612 }
6613 } else {
6614 /* forward reference */
6615 rc = EXIT_FAILURE;
6616 goto featurecheckdone;
6617 }
6618 }
6619
6620 }
6621 }
6622 rc = EXIT_SUCCESS;
6623
6624featurecheckdone:
6625 ly_set_free(refs);
6626 ly_set_free(procs);
6627 }
6628
6629 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006630 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006631 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006632 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006633 case UNRES_TYPEDEF_DFLT:
6634 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006635 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006636 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006637 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006638 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006639 break;
6640 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006641 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006642 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006643 choic = item;
6644
Radek Krejcie00d2312016-08-12 15:27:49 +02006645 if (!choic->dflt) {
6646 choic->dflt = resolve_choice_dflt(choic, expr);
6647 }
Michal Vasko7955b362015-09-04 14:18:15 +02006648 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006649 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006650 } else {
6651 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006652 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006653 break;
6654 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006655 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006656 break;
6657 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006658 unique_info = (struct unres_list_uniq *)item;
6659 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006660 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006661 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006662 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006663 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006664 case UNRES_XPATH:
6665 node = (struct lys_node *)item;
Michal Vaskof96dfb62017-08-17 12:23:49 +02006666 rc = lys_check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006667 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006668 case UNRES_EXT:
6669 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006670 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006671 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006672 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006673 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006674 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006675 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006676 if (eplugin) {
6677 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006678 u = malloc(sizeof *u);
Radek Krejciaa1303c2017-05-31 13:57:37 +02006679 LY_CHECK_ERR_RETURN(!u, LOGMEM, -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006680 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006681 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6682 /* something really bad happend since the extension finalization is not actually
6683 * being resolved while adding into unres, so something more serious with the unres
6684 * list itself must happened */
6685 return -1;
6686 }
Radek Krejci80056d52017-01-05 13:13:33 +01006687 }
6688 }
Radek Krejci79685c92017-02-17 10:49:43 +01006689 }
6690 if (!rc || rc == -1) {
6691 /* cleanup on success or fatal error */
6692 if (ext_data->datatype == LYS_IN_YIN) {
6693 /* YIN */
6694 lyxml_free(mod->ctx, ext_data->data.yin);
6695 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01006696 /* YANG */
6697 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01006698 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006699 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006700 }
6701 break;
Radek Krejci80056d52017-01-05 13:13:33 +01006702 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01006703 u = (uint8_t *)str_snode;
6704 ext = (*(struct lys_ext_instance ***)item)[*u];
6705 free(u);
6706
Radek Krejci80056d52017-01-05 13:13:33 +01006707 eplugin = ext->def->plugin;
6708
6709 /* inherit */
6710 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
6711 root = (struct lys_node *)ext->parent;
6712 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6713 LY_TREE_DFS_BEGIN(root->child, next, node) {
6714 /* first, check if the node already contain instance of the same extension,
6715 * in such a case we won't inherit. In case the node was actually defined as
6716 * augment data, we are supposed to check the same way also the augment node itself */
6717 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
6718 goto inherit_dfs_sibling;
6719 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
6720 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
6721 goto inherit_dfs_sibling;
6722 }
6723
6724 if (eplugin->check_inherit) {
6725 /* we have a callback to check the inheritance, use it */
6726 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
6727 case 0:
6728 /* yes - continue with the inheriting code */
6729 break;
6730 case 1:
6731 /* no - continue with the node's sibling */
6732 goto inherit_dfs_sibling;
6733 case 2:
6734 /* no, but continue with the children, just skip the inheriting code for this node */
6735 goto inherit_dfs_child;
6736 default:
6737 LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
6738 ext->def->module->name, ext->def->name, rc);
6739 }
6740 }
6741
6742 /* inherit the extension */
6743 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Radek Krejciaa1303c2017-05-31 13:57:37 +02006744 LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006745 extlist[node->ext_size] = malloc(sizeof **extlist);
Radek Krejciaa1303c2017-05-31 13:57:37 +02006746 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM; node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006747 memcpy(extlist[node->ext_size], ext, sizeof *ext);
6748 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
6749
6750 node->ext = extlist;
6751 node->ext_size++;
6752
6753inherit_dfs_child:
6754 /* modification of - select element for the next run - children first */
6755 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
6756 next = NULL;
6757 } else {
6758 next = node->child;
6759 }
6760 if (!next) {
6761inherit_dfs_sibling:
6762 /* no children, try siblings */
6763 next = node->next;
6764 }
6765 while (!next) {
6766 /* go to the parent */
6767 node = lys_parent(node);
6768
6769 /* we are done if we are back in the root (the starter's parent */
6770 if (node == root) {
6771 break;
6772 }
6773
6774 /* parent is already processed, go to its sibling */
6775 next = node->next;
6776 }
6777 }
6778 }
6779 }
6780
6781 /* final check */
6782 if (eplugin->check_result) {
6783 if ((*eplugin->check_result)(ext)) {
Radek Krejci2c121b32017-02-24 10:03:16 +01006784 ly_errno = LY_EEXT;
Radek Krejci80056d52017-01-05 13:13:33 +01006785 return -1;
6786 }
6787 }
6788
6789 rc = 0;
6790 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006791 default:
6792 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006793 break;
6794 }
6795
Radek Krejci54081ce2016-08-12 15:21:47 +02006796 if (has_str && !rc) {
6797 /* the string is no more needed in case of success.
6798 * In case of forward reference, we will try to resolve the string later */
Radek Krejci4f78b532016-02-17 13:43:00 +01006799 lydict_remove(mod->ctx, str_snode);
6800 }
6801
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006802 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006803}
6804
Michal Vaskof02e3742015-08-05 16:27:02 +02006805/* logs directly */
6806static void
Radek Krejci48464ed2016-03-17 15:44:09 +01006807print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006808{
Michal Vaskocb34dc62016-05-20 14:38:37 +02006809 struct lyxml_elem *xml;
6810 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006811 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006812 const char *name = NULL;
6813 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006814
Michal Vaskof02e3742015-08-05 16:27:02 +02006815 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02006816 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006817 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006818 break;
6819 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006820 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006821 break;
6822 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006823 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
6824 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02006825 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006826 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02006827 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02006828 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02006829 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
6830 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01006831 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006832 } else {
6833 LY_TREE_FOR(xml->attr, attr) {
6834 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006835 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006836 break;
6837 }
6838 }
6839 assert(attr);
6840 }
Radek Krejcie534c132016-11-23 13:32:31 +01006841 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006842 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006843 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006844 iff_data = str_node;
6845 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02006846 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006847 case UNRES_FEATURE:
6848 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
6849 ((struct lys_feature *)item)->name);
6850 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006851 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006852 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006853 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006854 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02006855 case UNRES_TYPE_DFLT:
Radek Krejci2e2de832016-10-13 16:12:26 +02006856 if (str_node) {
6857 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
6858 } /* else no default value in the type itself, but we are checking some restrictions against
6859 * possible default value of some base type. The failure is caused by not resolved base type,
6860 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02006861 break;
6862 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006863 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006864 break;
6865 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01006866 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006867 break;
6868 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01006869 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006870 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006871 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006872 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
6873 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01006874 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006875 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01006876 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
6877 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02006878 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006879 case UNRES_EXT:
6880 extinfo = (struct unres_ext *)str_node;
6881 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
6882 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
6883 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006884 default:
6885 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02006886 break;
6887 }
6888}
6889
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006890/**
Michal Vaskobb211122015-08-19 14:03:11 +02006891 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006892 *
6893 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006894 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006895 *
Michal Vasko92b8a382015-08-19 14:03:49 +02006896 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006897 */
Michal Vaskof02e3742015-08-05 16:27:02 +02006898int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006899resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02006900{
Radek Krejci010e54b2016-03-15 09:40:34 +01006901 uint32_t i, resolved = 0, unres_count, res_count;
PavolVicana0fdbf32017-02-15 17:59:02 +01006902 struct lyxml_elem *yin;
6903 struct yang_type *yang;
Michal Vasko74a60c02017-03-08 10:19:48 +01006904 int rc, log_hidden;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006905
6906 assert(unres);
6907
Michal Vaskoe8734262016-09-29 14:12:06 +02006908 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko4814fb02017-08-17 14:49:38 +02006909 if (ly_vlog_hidden) {
Michal Vasko74a60c02017-03-08 10:19:48 +01006910 log_hidden = 1;
6911 } else {
6912 log_hidden = 0;
6913 ly_vlog_hide(1);
6914 }
Michal Vasko51054ca2015-08-12 12:20:00 +02006915
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006916 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02006917 do {
Michal Vasko88c29542015-11-27 14:57:53 +01006918 unres_count = 0;
6919 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02006920
6921 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006922 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006923 * if-features are resolved here to make sure that we will have all if-features for
6924 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02006925 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006926 continue;
6927 }
Radek Krejci018f1f52016-08-03 16:01:20 +02006928 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejci818b0c52016-11-09 15:10:51 +01006929 * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02006930
Michal Vasko88c29542015-11-27 14:57:53 +01006931 ++unres_count;
Michal Vaskof96dfb62017-08-17 12:23:49 +02006932 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 +02006933 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006934 unres->type[i] = UNRES_RESOLVED;
6935 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01006936 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02006937 } else if (rc == -1) {
Michal Vasko74a60c02017-03-08 10:19:48 +01006938 if (!log_hidden) {
6939 ly_vlog_hide(0);
6940 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006941 /* print the error */
Radek Krejcicf748252017-09-04 11:11:14 +02006942 ly_err_repeat(ly_parser_data.ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006943 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02006944 } else {
6945 /* forward reference, erase ly_errno */
Radek Krejcicf748252017-09-04 11:11:14 +02006946 ly_err_clean(ly_parser_data.ctx, 1);
Michal Vasko51054ca2015-08-12 12:20:00 +02006947 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006948 }
Michal Vasko88c29542015-11-27 14:57:53 +01006949 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02006950
Michal Vasko88c29542015-11-27 14:57:53 +01006951 if (res_count < unres_count) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006952 /* just print the errors */
Michal Vasko74a60c02017-03-08 10:19:48 +01006953 if (!log_hidden) {
6954 ly_vlog_hide(0);
6955 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006956
6957 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006958 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006959 continue;
6960 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02006961 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006962 if (unres->type[i] == UNRES_TYPE_DER_EXT) {
PavolVicana0fdbf32017-02-15 17:59:02 +01006963 yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der;
6964 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6965 yang =(struct yang_type *)yin;
6966 ((struct lys_type *)unres->item[i])->base = yang->base;
6967 if (yang->base == LY_TYPE_UNION) {
6968 yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]);
6969 }
6970 lydict_remove(mod->ctx, yang->name);
6971 free(yang);
6972 } else {
6973 lyxml_free(mod->ctx, yin);
6974 }
Radek Krejci63fc0962017-02-15 13:20:18 +01006975 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006976 }
Michal Vasko92b8a382015-08-19 14:03:49 +02006977 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006978 }
6979
Michal Vaskof96dfb62017-08-17 12:23:49 +02006980 /* the rest except finalizing extensions and xpath */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006981 for (i = 0; i < unres->count; ++i) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02006982 if ((unres->type[i] == UNRES_RESOLVED) || (unres->type[i] == UNRES_EXT_FINALIZE) || (unres->type[i] == UNRES_XPATH)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006983 continue;
6984 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02006985
Michal Vaskof96dfb62017-08-17 12:23:49 +02006986 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 +01006987 if (rc == 0) {
Pavol Vican88e16c92016-09-07 15:41:50 +02006988 if (unres->type[i] == UNRES_LIST_UNIQ) {
6989 /* free the allocated structure */
6990 free(unres->item[i]);
6991 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006992 unres->type[i] = UNRES_RESOLVED;
6993 ++resolved;
6994 } else if (rc == -1) {
Michal Vasko74a60c02017-03-08 10:19:48 +01006995 if (!log_hidden) {
6996 ly_vlog_hide(0);
6997 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006998 /* print the error */
Radek Krejcicf748252017-09-04 11:11:14 +02006999 ly_err_repeat(ly_parser_data.ctx);
Michal Vasko22af5ca2016-05-20 11:44:02 +02007000 return -1;
Radek Krejci791f6c72017-02-22 15:23:39 +01007001 } else {
7002 /* forward reference, erase ly_errno */
Radek Krejcicf748252017-09-04 11:11:14 +02007003 ly_err_clean(ly_parser_data.ctx, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007004 }
7005 }
7006
Michal Vasko74a60c02017-03-08 10:19:48 +01007007 if (!log_hidden) {
7008 ly_vlog_hide(0);
7009 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007010
Radek Krejci80056d52017-01-05 13:13:33 +01007011 /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
7012 for (i = 0; i < unres->count; ++i) {
7013 if (unres->type[i] != UNRES_EXT_FINALIZE) {
7014 continue;
7015 }
7016
Michal Vaskof96dfb62017-08-17 12:23:49 +02007017 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 +01007018 unres->type[i] = UNRES_RESOLVED;
Radek Krejci80056d52017-01-05 13:13:33 +01007019 if (rc == 0) {
Radek Krejci80056d52017-01-05 13:13:33 +01007020 ++resolved;
7021 }
Radek Krejci791f6c72017-02-22 15:23:39 +01007022 /* else error - it was already printed, but resolved was not increased,
7023 so this unres item will not be resolved again in the following code,
7024 but it will cause returning -1 at the end, this way we are able to
7025 print all the issues with unres */
Radek Krejci80056d52017-01-05 13:13:33 +01007026 }
7027
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007028 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007029 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
Michal Vaskof96dfb62017-08-17 12:23:49 +02007030 * all the validation errors, xpath is resolved only here to properly print all the messages
Radek Krejci010e54b2016-03-15 09:40:34 +01007031 */
7032 for (i = 0; i < unres->count; ++i) {
7033 if (unres->type[i] == UNRES_RESOLVED) {
7034 continue;
7035 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02007036 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 +01007037 if (unres->type[i] == UNRES_XPATH) {
Michal Vasko769f8032017-01-24 13:11:55 +01007038 /* XPath referencing an unknown node is actually supposed to be just a warning */
Radek Krejcib3142312016-11-09 11:04:12 +01007039 unres->type[i] = UNRES_RESOLVED;
7040 resolved++;
Radek Krejcib3142312016-11-09 11:04:12 +01007041 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007042 }
Radek Krejcib3142312016-11-09 11:04:12 +01007043 if (resolved < unres->count) {
7044 return -1;
7045 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007046 }
7047
Michal Vaskoe8734262016-09-29 14:12:06 +02007048 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01007049 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007050 return EXIT_SUCCESS;
7051}
7052
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007053/**
Michal Vaskobb211122015-08-19 14:03:11 +02007054 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007055 *
7056 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007057 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007058 * @param[in] item Item to resolve. Type determined by \p type.
7059 * @param[in] type Type of the unresolved item.
7060 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007061 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007062 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007063 */
7064int
Radek Krejci48464ed2016-03-17 15:44:09 +01007065unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7066 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007067{
Radek Krejci54081ce2016-08-12 15:21:47 +02007068 int rc;
7069 const char *dictstr;
7070
7071 dictstr = lydict_insert(mod->ctx, str, 0);
7072 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7073
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007074 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007075 lydict_remove(mod->ctx, dictstr);
7076 }
7077 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007078}
7079
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007080/**
Michal Vaskobb211122015-08-19 14:03:11 +02007081 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007082 *
7083 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007084 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007085 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007086 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007087 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007088 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007089 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007090 */
7091int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007092unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007093 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007094{
Michal Vaskoef486d72016-09-27 12:10:44 +02007095 int rc, log_hidden;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007096 uint32_t u;
Michal Vasko88c29542015-11-27 14:57:53 +01007097 struct lyxml_elem *yin;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007098
Michal Vasko9bf425b2015-10-22 11:42:03 +02007099 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
7100 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007101
Radek Krejci850a5de2016-11-08 14:06:40 +01007102 /* check for duplicities in unres */
7103 for (u = 0; u < unres->count; u++) {
7104 if (unres->type[u] == type && unres->item[u] == item &&
7105 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007106 /* duplication can happen when the node contains multiple statements of the same type to check,
7107 * this can happen for example when refinement is being applied, so we just postpone the processing
7108 * and do not duplicate the information */
7109 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007110 }
7111 }
7112
Michal Vaskof96dfb62017-08-17 12:23:49 +02007113 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH)) {
7114 /* extension finalization is not even tried when adding the item into the inres list,
7115 * xpath is not tried because it would hide some potential warnings */
Radek Krejcic293bac2017-02-27 11:25:28 +01007116 rc = EXIT_FAILURE;
7117 } else {
Michal Vasko4814fb02017-08-17 14:49:38 +02007118 if (ly_vlog_hidden) {
Radek Krejci80056d52017-01-05 13:13:33 +01007119 log_hidden = 1;
7120 } else {
7121 log_hidden = 0;
7122 ly_vlog_hide(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007123 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02007124 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007125 if (!log_hidden) {
7126 ly_vlog_hide(0);
7127 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007128
Radek Krejci80056d52017-01-05 13:13:33 +01007129 if (rc != EXIT_FAILURE) {
Michal Vaskobb520442017-05-23 10:55:18 +02007130 if (rc == -1) {
Radek Krejcicf748252017-09-04 11:11:14 +02007131 ly_err_repeat(ly_parser_data.ctx);
Radek Krejci80056d52017-01-05 13:13:33 +01007132 }
7133 if (type == UNRES_LIST_UNIQ) {
7134 /* free the allocated structure */
7135 free(item);
7136 } else if (rc == -1 && type == UNRES_IFFEAT) {
7137 /* free the allocated resources */
7138 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007139 }
Radek Krejci80056d52017-01-05 13:13:33 +01007140 return rc;
7141 } else {
7142 /* erase info about validation errors */
Radek Krejcicf748252017-09-04 11:11:14 +02007143 ly_err_clean(ly_parser_data.ctx, 1);
Radek Krejci80056d52017-01-05 13:13:33 +01007144 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007145
Radek Krejci80056d52017-01-05 13:13:33 +01007146 print_unres_schema_item_fail(item, type, snode);
7147
7148 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7149 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7150 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7151 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7152 lyxml_unlink_elem(mod->ctx, yin, 1);
7153 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7154 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007155 }
Michal Vasko88c29542015-11-27 14:57:53 +01007156 }
7157
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007158 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007159 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007160 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007161 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007162 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007163 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007164 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007165 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007166 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007167 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007168 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007169 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007170 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007171
Michal Vasko3767fb22016-07-21 12:10:57 +02007172 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007173}
7174
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007175/**
Michal Vaskobb211122015-08-19 14:03:11 +02007176 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007177 *
7178 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007179 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007180 * @param[in] item Old item to be resolved.
7181 * @param[in] type Type of the old unresolved item.
7182 * @param[in] new_item New item to use in the duplicate.
7183 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007184 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007185 */
Michal Vaskodad19402015-08-06 09:51:53 +02007186int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007187unres_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 +02007188{
7189 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007190 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007191 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007192
Michal Vaskocf024702015-10-08 15:01:42 +02007193 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007194
Radek Krejcid09d1a52016-08-11 14:05:45 +02007195 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7196 if (type == UNRES_LIST_UNIQ) {
7197 aux_uniq.list = item;
7198 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7199 item = &aux_uniq;
7200 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007201 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007202
7203 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007204 if (type == UNRES_LIST_UNIQ) {
7205 free(new_item);
7206 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007207 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007208 }
7209
Radek Krejcic79c6b12016-07-26 15:11:49 +02007210 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007211 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007212 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007213 LOGINT;
7214 return -1;
7215 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007216 } else if (type == UNRES_IFFEAT) {
7217 /* duplicate unres_iffeature_data */
7218 iff_data = malloc(sizeof *iff_data);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007219 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007220 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7221 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7222 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
7223 LOGINT;
7224 return -1;
7225 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007226 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007227 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007228 LOGINT;
7229 return -1;
7230 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007231 }
Michal Vaskodad19402015-08-06 09:51:53 +02007232
7233 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007234}
7235
Michal Vaskof02e3742015-08-05 16:27:02 +02007236/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007237int
Michal Vasko878e38d2016-09-05 12:17:53 +02007238unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007239{
Michal Vasko878e38d2016-09-05 12:17:53 +02007240 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007241 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007242
Radek Krejciddddd0d2017-01-20 15:20:46 +01007243 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007244 i = start_on_backwards;
7245 } else {
7246 i = unres->count - 1;
7247 }
7248 for (; i > -1; i--) {
7249 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007250 continue;
7251 }
7252 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007253 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007254 break;
7255 }
7256 } else {
7257 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7258 aux_uniq2 = (struct unres_list_uniq *)item;
7259 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007260 break;
7261 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007262 }
7263 }
7264
Michal Vasko878e38d2016-09-05 12:17:53 +02007265 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007266}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007267
Michal Vaskoede9c472016-06-07 09:38:15 +02007268static void
7269unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7270{
7271 struct lyxml_elem *yin;
7272 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007273 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007274
7275 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007276 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007277 case UNRES_TYPE_DER:
7278 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7279 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7280 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007281 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007282 lydict_remove(ctx, yang->name);
7283 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007284 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7285 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7286 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007287 } else {
7288 lyxml_free(ctx, yin);
7289 }
7290 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007291 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007292 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7293 lydict_remove(ctx, iff_data->fname);
7294 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007295 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007296 case UNRES_IDENT:
7297 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007298 case UNRES_CHOICE_DFLT:
7299 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007300 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7301 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007302 case UNRES_LIST_UNIQ:
7303 free(unres->item[i]);
7304 break;
PavolVicanc1807262017-01-31 18:00:27 +01007305 case UNRES_EXT:
7306 free(unres->str_snode[i]);
7307 break;
PavolVicanfcc98762017-09-01 15:51:39 +02007308 case UNRES_EXT_FINALIZE:
7309 free(unres->str_snode[i]);
Michal Vaskoede9c472016-06-07 09:38:15 +02007310 default:
7311 break;
7312 }
7313 unres->type[i] = UNRES_RESOLVED;
7314}
7315
Michal Vasko88c29542015-11-27 14:57:53 +01007316void
Michal Vasko44ab1462017-05-18 13:18:36 +02007317unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007318{
7319 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007320 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007321
Radek Krejcic071c542016-01-27 14:57:51 +01007322 if (!unres || !(*unres)) {
7323 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007324 }
7325
Michal Vasko44ab1462017-05-18 13:18:36 +02007326 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007327
7328 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007329 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007330 if ((*unres)->type[i] != UNRES_RESOLVED) {
7331 unresolved++;
7332 }
7333 continue;
7334 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007335
7336 /* free heap memory for the specific item */
7337 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007338 }
7339
Michal Vaskoede9c472016-06-07 09:38:15 +02007340 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007341 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007342 free((*unres)->item);
7343 free((*unres)->type);
7344 free((*unres)->str_snode);
7345 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007346 free((*unres));
7347 (*unres) = NULL;
7348 }
Michal Vasko88c29542015-11-27 14:57:53 +01007349}
7350
Michal Vaskoff690e72017-08-03 14:25:07 +02007351/* check whether instance-identifier points outside its data subtree (for operation it is any node
7352 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007353static int
7354check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7355{
Michal Vaskoff690e72017-08-03 14:25:07 +02007356 const struct lys_node *op_node, *first_node;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007357 char *buf;
Michal Vaskoff690e72017-08-03 14:25:07 +02007358 int ret = 0, hidden;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007359
Radek Krejci034cb102017-08-01 15:45:13 +02007360 if (!json_instid || !json_instid[0]) {
7361 /* no/empty value */
7362 return 0;
7363 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007364
7365 for (op_node = lys_parent(sleaf);
7366 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7367 op_node = lys_parent(op_node));
7368
7369 if (op_node && lys_parent(op_node)) {
7370 /* nested operation - any absolute path is external */
7371 return 1;
7372 }
7373
7374 /* get the first node from the instid */
7375 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7376 if (!buf) {
7377 LOGMEM;
7378 return -1;
7379 }
7380
Michal Vaskoff690e72017-08-03 14:25:07 +02007381 /* find the first schema node, do not log */
Michal Vasko4814fb02017-08-17 14:49:38 +02007382 hidden = ly_vlog_hidden;
Michal Vaskoff690e72017-08-03 14:25:07 +02007383 if (!hidden) {
7384 ly_vlog_hide(1);
7385 }
7386 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
7387 if (!hidden) {
7388 ly_vlog_hide(0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007389 }
7390
Michal Vaskoff690e72017-08-03 14:25:07 +02007391 if (!first_node) {
7392 /* unknown path, say it is not external */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007393 free(buf);
Radek Krejci98caa582017-09-07 14:32:56 +02007394 ly_errno = LY_SUCCESS;
Michal Vaskoff690e72017-08-03 14:25:07 +02007395 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007396 }
7397 free(buf);
7398
7399 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7400
Michal Vaskoff690e72017-08-03 14:25:07 +02007401 if (op_node && (op_node != first_node)) {
7402 /* it is a top-level operation, so we're good if it points somewhere inside it */
7403 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007404 }
7405
7406 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7407 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007408 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007409}
7410
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007411/**
7412 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7413 *
7414 * @param[in] data Data node where the path is used
7415 * @param[in] path Instance-identifier node value.
7416 * @param[in,out] ret Resolved instance or NULL.
7417 *
7418 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7419 */
7420static int
7421resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7422{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007423 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007424 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007425 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007426 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007427 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007428 char *str;
7429 int mod_len, name_len, has_predicate;
7430 struct unres_data node_match;
7431
7432 memset(&node_match, 0, sizeof node_match);
7433 *ret = NULL;
7434
7435 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007436 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007437 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007438 if (root->prev) {
7439 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007440 }
7441
7442 /* search for the instance node */
7443 while (path[i]) {
7444 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7445 if (j <= 0) {
7446 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
7447 goto error;
7448 }
7449 i += j;
7450
Michal Vasko1b6ca962017-08-03 14:23:09 +02007451 if (model) {
7452 str = strndup(model, mod_len);
7453 if (!str) {
7454 LOGMEM;
7455 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007456 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007457 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007458 if (ctx->data_clb) {
7459 if (!mod) {
7460 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7461 } else if (!mod->implemented) {
7462 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7463 }
7464 }
7465 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007466
Michal Vasko1b6ca962017-08-03 14:23:09 +02007467 if (!mod || !mod->implemented || mod->disabled) {
7468 break;
7469 }
7470 } else if (!prev_mod) {
7471 /* first iteration and we are missing module name */
7472 LOGVAL(LYE_INELEM_LEN, LY_VLOG_NONE, NULL, name_len, name);
7473 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Instane-identifier is missing prefix in the first node.");
7474 goto error;
7475 } else {
7476 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007477 }
7478
Radek Krejci2c822ed2017-08-03 14:23:36 +02007479 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007480 /* no instance exists */
7481 break;
7482 }
7483
7484 if (has_predicate) {
7485 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007486 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007487 /* index of the current node (for lists with position predicates) */
7488 cur_idx = 1;
7489 while (j < (signed)node_match.count) {
7490 node = node_match.node[j];
7491 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7492 if (parsed < 1) {
7493 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
7494 goto error;
7495 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007496
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007497 if (!node) {
7498 /* current node does not satisfy the predicate */
7499 unres_data_del(&node_match, j);
7500 } else {
7501 ++j;
7502 }
7503 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007504 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007505
7506 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007507 } else if (node_match.count) {
7508 /* check that we are not addressing lists */
7509 for (j = 0; (unsigned)j < node_match.count; ++j) {
7510 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7511 unres_data_del(&node_match, j--);
7512 }
7513 }
7514 if (!node_match.count) {
Radek Krejci2c822ed2017-08-03 14:23:36 +02007515 LOGVAL(LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007516 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007517 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007518
7519 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007520 }
7521
7522 if (!node_match.count) {
7523 /* no instance exists */
7524 if (req_inst > -1) {
Radek Krejci2c822ed2017-08-03 14:23:36 +02007525 LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007526 return EXIT_FAILURE;
7527 }
7528 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7529 return EXIT_SUCCESS;
7530 } else if (node_match.count > 1) {
7531 /* instance identifier must resolve to a single node */
7532 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
7533 goto error;
7534 } else {
7535 /* we have required result, remember it and cleanup */
7536 *ret = node_match.node[0];
7537 free(node_match.node);
7538 return EXIT_SUCCESS;
7539 }
7540
7541error:
7542 /* cleanup */
7543 free(node_match.node);
7544 return -1;
7545}
7546
7547static int
7548resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007549{
Michal Vaskoca16cb32017-07-10 11:50:33 +02007550 struct ly_set *set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007551 uint32_t i;
7552
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007553 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007554
Michal Vaskoca16cb32017-07-10 11:50:33 +02007555 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vasko50576712017-07-28 12:28:33 +02007556 set = lyd_find_path((struct lyd_node *)leaf, path);
Michal Vaskoca16cb32017-07-10 11:50:33 +02007557 if (!set) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007558 return -1;
7559 }
7560
Michal Vaskoca16cb32017-07-10 11:50:33 +02007561 for (i = 0; i < set->number; ++i) {
7562 if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7563 continue;
7564 }
7565
Radek Krejci1899d6a2016-11-03 13:48:07 +01007566 /* not that the value is already in canonical form since the parsers does the conversion,
7567 * so we can simply compare just the values */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007568 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 +01007569 /* we have the match */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007570 *ret = set->set.d[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007571 break;
7572 }
7573 }
7574
Michal Vaskoca16cb32017-07-10 11:50:33 +02007575 ly_set_free(set);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007576
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007577 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007578 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007579 if (req_inst > -1) {
7580 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007581 return EXIT_FAILURE;
7582 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007583 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 +02007584 }
7585 }
7586
7587 return EXIT_SUCCESS;
7588}
7589
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007590/* 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 +01007591int
7592resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7593 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007594{
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007595 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007596 struct lyd_node *ret;
7597 int found, hidden, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007598 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007599
7600 assert(type->base == LY_TYPE_UNION);
7601
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007602 if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) {
7603 /* either NULL or instid previously converted to JSON */
7604 json_val = leaf->value.string;
7605 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007606
Michal Vaskofd6c6502017-01-06 12:15:41 +01007607 if (store) {
7608 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7609 free(leaf->value.bit);
7610 }
7611 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007612 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007613
7614 /* turn logging off, we are going to try to validate the value with all the types in order */
Michal Vasko4814fb02017-08-17 14:49:38 +02007615 hidden = ly_vlog_hidden;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007616 ly_vlog_hide(1);
7617
7618 t = NULL;
7619 found = 0;
7620 while ((t = lyp_get_next_union_type(type, t, &found))) {
7621 found = 0;
7622
7623 switch (t->base) {
7624 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007625 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7626 req_inst = -1;
7627 } else {
7628 req_inst = t->info.lref.req;
7629 }
7630
7631 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007632 if (store) {
7633 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7634 /* valid resolved */
7635 leaf->value.leafref = ret;
7636 leaf->value_type = LY_TYPE_LEAFREF;
7637 } else {
7638 /* valid unresolved */
Michal Vasko31a2d322018-01-12 13:36:12 +01007639 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007640 return -1;
7641 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007642 }
7643 }
7644
7645 success = 1;
7646 }
7647 break;
7648 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007649 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
Radek Krejci034cb102017-08-01 15:45:13 +02007650 if (ext_dep == -1) {
7651 return -1;
7652 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007653 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7654 req_inst = -1;
7655 } else {
7656 req_inst = t->info.inst.req;
7657 }
7658
Michal Vaskod3a03112017-01-23 09:56:02 +01007659 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007660 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007661 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007662 /* valid resolved */
7663 leaf->value.instance = ret;
7664 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007665
Michal Vaskofd6c6502017-01-06 12:15:41 +01007666 if (json_val) {
7667 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7668 leaf->value_str = json_val;
7669 json_val = NULL;
7670 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007671 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007672 /* valid unresolved */
7673 if (json_val) {
7674 /* put the JSON val back */
7675 leaf->value.string = json_val;
7676 json_val = NULL;
7677 } else {
7678 leaf->value.instance = NULL;
7679 }
7680 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007681 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007682 }
7683
7684 success = 1;
7685 }
7686 break;
7687 default:
Michal Vasko31a2d322018-01-12 13:36:12 +01007688 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007689 success = 1;
7690 }
7691 break;
7692 }
7693
7694 if (success) {
7695 break;
7696 }
7697
7698 /* erase information about errors - they are false or irrelevant
7699 * and will be replaced by a single error messages */
Radek Krejcicf748252017-09-04 11:11:14 +02007700 ly_err_clean(ly_parser_data.ctx, 1);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007701
7702 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007703 if (store) {
7704 if (t->base == LY_TYPE_BITS) {
7705 free(leaf->value.bit);
7706 }
7707 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007708 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007709 }
7710
7711 /* turn logging back on */
7712 if (!hidden) {
7713 ly_vlog_hide(0);
7714 }
7715
7716 if (json_val) {
7717 if (!success) {
7718 /* put the value back for now */
7719 assert(leaf->value_type == LY_TYPE_UNION);
7720 leaf->value.string = json_val;
7721 } else {
7722 /* value was ultimately useless, but we could not have known */
7723 lydict_remove(leaf->schema->module->ctx, json_val);
7724 }
7725 }
7726
Michal Vaskofd6c6502017-01-06 12:15:41 +01007727 if (success) {
7728 if (resolved_type) {
7729 *resolved_type = t;
7730 }
7731 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007732 /* not found and it is required */
7733 LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007734 return EXIT_FAILURE;
7735 }
7736
7737 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007738
Radek Krejci9b6aad22016-09-20 15:55:51 +02007739}
7740
Michal Vasko8bcdf292015-08-19 14:04:43 +02007741/**
7742 * @brief Resolve a single unres data item. Logs directly.
7743 *
Michal Vaskocf024702015-10-08 15:01:42 +02007744 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007745 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007746 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007747 *
7748 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7749 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007750int
Michal Vasko0b963112017-08-11 12:45:36 +02007751resolve_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 +02007752{
Michal Vasko3cfa3182017-01-17 10:00:58 +01007753 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02007754 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007755 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007756 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007757
Michal Vasko83a6c462015-10-08 16:43:53 +02007758 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02007759 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007760
Michal Vaskocf024702015-10-08 15:01:42 +02007761 switch (type) {
7762 case UNRES_LEAFREF:
7763 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007764 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007765 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7766 req_inst = -1;
7767 } else {
7768 req_inst = sleaf->type.info.lref.req;
7769 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007770 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
7771 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01007772 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007773 /* valid resolved */
Michal Vasko1c8567a2017-01-05 13:42:27 +01007774 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7775 free(leaf->value.bit);
7776 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007777 leaf->value.leafref = ret;
7778 leaf->value_type = LY_TYPE_LEAFREF;
7779 } else {
7780 /* valid unresolved */
7781 if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) {
Michal Vasko31a2d322018-01-12 13:36:12 +01007782 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007783 return -1;
7784 }
7785 }
7786 }
7787 leaf->validity &= ~LYD_VAL_LEAFREF;
7788 } else {
7789 return rc;
7790 }
7791 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007792
Michal Vaskocf024702015-10-08 15:01:42 +02007793 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02007794 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007795 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
7796 if (ext_dep == -1) {
7797 return -1;
7798 }
7799
7800 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7801 req_inst = -1;
7802 } else {
7803 req_inst = sleaf->type.info.inst.req;
7804 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007805 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
7806 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007807 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007808 /* valid resolved */
7809 leaf->value.instance = ret;
7810 leaf->value_type = LY_TYPE_INST;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007811 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007812 /* valid unresolved */
7813 leaf->value.instance = NULL;
7814 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007815 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007816 } else {
7817 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007818 }
Michal Vaskocf024702015-10-08 15:01:42 +02007819 break;
7820
Radek Krejci7de36cf2016-09-12 16:18:50 +02007821 case UNRES_UNION:
7822 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007823 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007824
Michal Vaskocf024702015-10-08 15:01:42 +02007825 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02007826 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02007827 return rc;
7828 }
7829 break;
7830
Michal Vaskobf19d252015-10-08 15:39:17 +02007831 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007832 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02007833 return rc;
7834 }
7835 break;
7836
7837 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007838 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02007839 return rc;
7840 }
7841 break;
7842
Michal Vaskocf024702015-10-08 15:01:42 +02007843 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02007844 LOGINT;
7845 return -1;
7846 }
7847
7848 return EXIT_SUCCESS;
7849}
7850
7851/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01007852 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02007853 *
7854 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02007855 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007856 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01007857 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007858 */
7859int
Radek Krejci0b7704f2016-03-18 12:16:14 +01007860unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007861{
Radek Krejci03b71f72016-03-16 11:10:09 +01007862 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02007863 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02007864 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02007865
Radek Krejci03b71f72016-03-16 11:10:09 +01007866 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007867 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007868 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007869 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01007870 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Radek Krejciaa1303c2017-05-31 13:57:37 +02007871 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007872 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007873
Radek Krejci0b7704f2016-03-18 12:16:14 +01007874 if (type == UNRES_WHEN) {
7875 /* remove previous result */
7876 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007877 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007878
7879 return EXIT_SUCCESS;
7880}
7881
7882/**
7883 * @brief Resolve every unres data item in the structure. Logs directly.
7884 *
Radek Krejci082c84f2016-10-17 16:33:06 +02007885 * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected,
7886 * unresolved leafrefs/instids are accepted).
7887 *
7888 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
7889 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007890 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02007891 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
7892 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007893 *
7894 * @return EXIT_SUCCESS on success, -1 on error.
7895 */
7896int
Radek Krejci082c84f2016-10-17 16:33:06 +02007897resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007898{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007899 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007900 int rc, progress, ignore_fail;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007901 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02007902 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007903
Radek Krejci082c84f2016-10-17 16:33:06 +02007904 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01007905 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01007906
7907 if (!unres->count) {
7908 return EXIT_SUCCESS;
7909 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007910
Michal Vaskoad2e44a2017-01-03 10:31:35 +01007911 if (options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007912 ignore_fail = 1;
7913 } else if (options & LYD_OPT_NOEXTDEPS) {
7914 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007915 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007916 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007917 }
7918
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007919 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01007920 ly_vlog_hide(1);
7921
Radek Krejci0b7704f2016-03-18 12:16:14 +01007922 /* when-stmt first */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007923 first = 1;
7924 stmt_count = 0;
7925 resolved = 0;
7926 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01007927 do {
Radek Krejcicf748252017-09-04 11:11:14 +02007928 ly_err_clean(ly_parser_data.ctx, 1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007929 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02007930 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007931 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007932 continue;
7933 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007934 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007935 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007936 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007937 }
7938
7939 /* resolve when condition only when all parent when conditions are already resolved */
7940 for (parent = unres->node[i]->parent;
7941 parent && LYD_WHEN_DONE(parent->when_status);
7942 parent = parent->parent) {
7943 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
7944 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02007945 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01007946 */
7947 unres->node[i]->when_status |= LYD_WHEN_FALSE;
7948 unres->type[i] = UNRES_RESOLVED;
7949 resolved++;
7950 break;
7951 }
7952 }
7953 if (parent) {
7954 continue;
7955 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007956
Michal Vasko0b963112017-08-11 12:45:36 +02007957 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01007958 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02007959 /* finish with error/delete the node only if when was false, an external dependency was not required,
7960 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02007961 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vasko0b963112017-08-11 12:45:36 +02007962 && (!(when->flags & LYS_XPATH_DEP) || !(options & LYD_OPT_NOEXTDEPS))) {
Radek Krejci082c84f2016-10-17 16:33:06 +02007963 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01007964 /* false when condition */
7965 ly_vlog_hide(0);
Radek Krejcicf748252017-09-04 11:11:14 +02007966 ly_err_repeat(ly_parser_data.ctx);
Radek Krejci03b71f72016-03-16 11:10:09 +01007967 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007968 } /* follows else */
7969
Michal Vaskoe31d34a2017-03-28 14:50:38 +02007970 /* auto-delete */
7971 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
7972 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
7973
Radek Krejci0c0086a2016-03-24 15:20:28 +01007974 /* only unlink now, the subtree can contain another nodes stored in the unres list */
7975 /* if it has parent non-presence containers that would be empty, we should actually
7976 * remove the container
7977 */
Radek Krejci2537fd32016-09-07 16:22:41 +02007978 for (parent = unres->node[i];
7979 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
7980 parent = parent->parent) {
7981 if (((struct lys_node_container *)parent->parent->schema)->presence) {
7982 /* presence container */
7983 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007984 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007985 if (parent->next || parent->prev != parent) {
7986 /* non empty (the child we are in and we are going to remove is not the only child) */
7987 break;
7988 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007989 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007990 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007991
Radek Krejci0c0086a2016-03-24 15:20:28 +01007992 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007993 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01007994 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007995
Radek Krejci0b7704f2016-03-18 12:16:14 +01007996 lyd_unlink(unres->node[i]);
7997 unres->type[i] = UNRES_DELETE;
7998 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01007999
8000 /* update the rest of unres items */
8001 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01008002 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01008003 continue;
8004 }
8005
8006 /* test if the node is in subtree to be deleted */
8007 for (parent = unres->node[j]; parent; parent = parent->parent) {
8008 if (parent == unres->node[i]) {
8009 /* yes, it is */
8010 unres->type[j] = UNRES_RESOLVED;
8011 resolved++;
8012 break;
8013 }
8014 }
8015 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008016 } else {
8017 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01008018 }
Radek Krejcicf748252017-09-04 11:11:14 +02008019 ly_err_clean(ly_parser_data.ctx, 1);
Radek Krejci010e54b2016-03-15 09:40:34 +01008020 resolved++;
8021 progress = 1;
8022 } else if (rc == -1) {
8023 ly_vlog_hide(0);
Michal Vasko76e73402016-08-24 16:00:13 +02008024 /* print only this last error */
Michal Vasko0b963112017-08-11 12:45:36 +02008025 resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Radek Krejci010e54b2016-03-15 09:40:34 +01008026 return -1;
Radek Krejci2467a492016-10-24 15:16:59 +02008027 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01008028 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008029 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008030 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01008031
Radek Krejci0b7704f2016-03-18 12:16:14 +01008032 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008033 if (stmt_count > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008034 ly_vlog_hide(0);
Radek Krejcicf748252017-09-04 11:11:14 +02008035 ly_err_repeat(ly_parser_data.ctx);
Radek Krejci0b7704f2016-03-18 12:16:14 +01008036 return -1;
8037 }
8038
8039 for (i = 0; del_items && i < unres->count; i++) {
8040 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
8041 if (unres->type[i] != UNRES_DELETE) {
8042 continue;
8043 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008044 if (!unres->node[i]) {
8045 unres->type[i] = UNRES_RESOLVED;
8046 del_items--;
8047 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008048 }
8049
8050 /* really remove the complete subtree */
8051 lyd_free(unres->node[i]);
8052 unres->type[i] = UNRES_RESOLVED;
8053 del_items--;
8054 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008055
8056 /* now leafrefs */
8057 first = 1;
8058 stmt_count = 0;
8059 resolved = 0;
8060 do {
8061 progress = 0;
8062 for (i = 0; i < unres->count; i++) {
8063 if (unres->type[i] != UNRES_LEAFREF) {
8064 continue;
8065 }
8066 if (first) {
8067 /* count leafref nodes in unres list */
8068 stmt_count++;
8069 }
8070
Michal Vasko0b963112017-08-11 12:45:36 +02008071 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008072 if (!rc) {
8073 unres->type[i] = UNRES_RESOLVED;
Radek Krejcicf748252017-09-04 11:11:14 +02008074 ly_err_clean(ly_parser_data.ctx, 1);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008075 resolved++;
8076 progress = 1;
8077 } else if (rc == -1) {
8078 ly_vlog_hide(0);
8079 /* print only this last error */
Michal Vasko0b963112017-08-11 12:45:36 +02008080 resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008081 return -1;
8082 } /* else forward reference */
8083 }
8084 first = 0;
8085 } while (progress && resolved < stmt_count);
8086
8087 /* do we have some unresolved leafrefs? */
8088 if (stmt_count > resolved) {
8089 ly_vlog_hide(0);
Radek Krejcicf748252017-09-04 11:11:14 +02008090 ly_err_repeat(ly_parser_data.ctx);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008091 return -1;
8092 }
8093
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008094 ly_vlog_hide(0);
Radek Krejci010e54b2016-03-15 09:40:34 +01008095
8096 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008097 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008098 if (unres->type[i] == UNRES_RESOLVED) {
8099 continue;
8100 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008101 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008102
Michal Vasko0b963112017-08-11 12:45:36 +02008103 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008104 if (rc) {
8105 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008106 return -1;
8107 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008108
8109 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008110 }
8111
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008112 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008113 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008114 return EXIT_SUCCESS;
8115}