blob: 0957b78c26c3f682b2338c560076d880b32047f9 [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"
32
Michal Vasko730dfdf2015-08-11 14:48:05 +020033/**
Radek Krejci6dc53a22015-08-17 13:27:59 +020034 * @brief Parse an identifier.
35 *
36 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
37 * identifier = (ALPHA / "_")
38 * *(ALPHA / DIGIT / "_" / "-" / ".")
39 *
Michal Vaskobb211122015-08-19 14:03:11 +020040 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +020041 *
42 * @return Number of characters successfully parsed.
43 */
Michal Vasko249e6b52015-08-19 11:08:52 +020044int
Radek Krejci6dc53a22015-08-17 13:27:59 +020045parse_identifier(const char *id)
46{
47 int parsed = 0;
48
Michal Vasko1ab90bc2016-03-15 10:40:22 +010049 assert(id);
50
Radek Krejci6dc53a22015-08-17 13:27:59 +020051 if (((id[0] == 'x') || (id[0] == 'X'))
Michal Vasko1ab90bc2016-03-15 10:40:22 +010052 && (id[0] && ((id[1] == 'm') || (id[0] == 'M')))
53 && (id[1] && ((id[2] == 'l') || (id[2] == 'L')))) {
Radek Krejci6dc53a22015-08-17 13:27:59 +020054 return -parsed;
55 }
56
57 if (!isalpha(id[0]) && (id[0] != '_')) {
58 return -parsed;
59 }
60
61 ++parsed;
62 ++id;
63
64 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
65 ++parsed;
66 ++id;
67 }
68
69 return parsed;
70}
71
72/**
73 * @brief Parse a node-identifier.
74 *
Michal Vasko723e50c2015-10-20 15:20:29 +020075 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +020076 *
Michal Vaskobb211122015-08-19 14:03:11 +020077 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +020078 * @param[out] mod_name Points to the module name, NULL if there is not any.
79 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +020080 * @param[out] name Points to the node name.
81 * @param[out] nam_len Length of the node name.
82 *
83 * @return Number of characters successfully parsed,
84 * positive on success, negative on failure.
85 */
86static int
Michal Vasko723e50c2015-10-20 15:20:29 +020087parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len)
Radek Krejci6dc53a22015-08-17 13:27:59 +020088{
89 int parsed = 0, ret;
90
91 assert(id);
Michal Vasko723e50c2015-10-20 15:20:29 +020092 if (mod_name) {
93 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +020094 }
Michal Vasko723e50c2015-10-20 15:20:29 +020095 if (mod_name_len) {
96 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +020097 }
98 if (name) {
99 *name = NULL;
100 }
101 if (nam_len) {
102 *nam_len = 0;
103 }
104
105 if ((ret = parse_identifier(id)) < 1) {
106 return ret;
107 }
108
Michal Vasko723e50c2015-10-20 15:20:29 +0200109 if (mod_name) {
110 *mod_name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200111 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200112 if (mod_name_len) {
113 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200114 }
115
116 parsed += ret;
117 id += ret;
118
119 /* there is prefix */
120 if (id[0] == ':') {
121 ++parsed;
122 ++id;
123
124 /* there isn't */
125 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200126 if (name && mod_name) {
127 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200128 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200129 if (mod_name) {
130 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200131 }
132
Michal Vasko723e50c2015-10-20 15:20:29 +0200133 if (nam_len && mod_name_len) {
134 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200135 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200136 if (mod_name_len) {
137 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200138 }
139
140 return parsed;
141 }
142
143 /* identifier (node name) */
144 if ((ret = parse_identifier(id)) < 1) {
145 return -parsed+ret;
146 }
147
148 if (name) {
149 *name = id;
150 }
151 if (nam_len) {
152 *nam_len = ret;
153 }
154
155 return parsed+ret;
156}
157
158/**
159 * @brief Parse a path-predicate (leafref).
160 *
161 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
162 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
163 *
Michal Vaskobb211122015-08-19 14:03:11 +0200164 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200165 * @param[out] prefix Points to the prefix, NULL if there is not any.
166 * @param[out] pref_len Length of the prefix, 0 if there is not any.
167 * @param[out] name Points to the node name.
168 * @param[out] nam_len Length of the node name.
169 * @param[out] path_key_expr Points to the path-key-expr.
170 * @param[out] pke_len Length of the path-key-expr.
171 * @param[out] has_predicate Flag to mark whether there is another predicate following.
172 *
173 * @return Number of characters successfully parsed,
174 * positive on success, negative on failure.
175 */
176static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200177parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
178 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200179{
180 const char *ptr;
181 int parsed = 0, ret;
182
183 assert(id);
184 if (prefix) {
185 *prefix = NULL;
186 }
187 if (pref_len) {
188 *pref_len = 0;
189 }
190 if (name) {
191 *name = NULL;
192 }
193 if (nam_len) {
194 *nam_len = 0;
195 }
196 if (path_key_expr) {
197 *path_key_expr = NULL;
198 }
199 if (pke_len) {
200 *pke_len = 0;
201 }
202 if (has_predicate) {
203 *has_predicate = 0;
204 }
205
206 if (id[0] != '[') {
207 return -parsed;
208 }
209
210 ++parsed;
211 ++id;
212
213 while (isspace(id[0])) {
214 ++parsed;
215 ++id;
216 }
217
218 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
219 return -parsed+ret;
220 }
221
222 parsed += ret;
223 id += ret;
224
225 while (isspace(id[0])) {
226 ++parsed;
227 ++id;
228 }
229
230 if (id[0] != '=') {
231 return -parsed;
232 }
233
234 ++parsed;
235 ++id;
236
237 while (isspace(id[0])) {
238 ++parsed;
239 ++id;
240 }
241
242 if ((ptr = strchr(id, ']')) == NULL) {
243 return -parsed;
244 }
245
246 --ptr;
247 while (isspace(ptr[0])) {
248 --ptr;
249 }
250 ++ptr;
251
252 ret = ptr-id;
253 if (path_key_expr) {
254 *path_key_expr = id;
255 }
256 if (pke_len) {
257 *pke_len = ret;
258 }
259
260 parsed += ret;
261 id += ret;
262
263 while (isspace(id[0])) {
264 ++parsed;
265 ++id;
266 }
267
268 assert(id[0] == ']');
269
270 if (id[1] == '[') {
271 *has_predicate = 1;
272 }
273
274 return parsed+1;
275}
276
277/**
278 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
279 * the ".." and the first node-identifier, other calls parse a single
280 * node-identifier each.
281 *
282 * path-key-expr = current-function-invocation *WSP "/" *WSP
283 * rel-path-keyexpr
284 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
285 * *(node-identifier *WSP "/" *WSP)
286 * node-identifier
287 *
Michal Vaskobb211122015-08-19 14:03:11 +0200288 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200289 * @param[out] prefix Points to the prefix, NULL if there is not any.
290 * @param[out] pref_len Length of the prefix, 0 if there is not any.
291 * @param[out] name Points to the node name.
292 * @param[out] nam_len Length of the node name.
293 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
294 * must not be changed between consecutive calls.
295 * @return Number of characters successfully parsed,
296 * positive on success, negative on failure.
297 */
298static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200299parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
300 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200301{
302 int parsed = 0, ret, par_times = 0;
303
304 assert(id);
305 assert(parent_times);
306 if (prefix) {
307 *prefix = NULL;
308 }
309 if (pref_len) {
310 *pref_len = 0;
311 }
312 if (name) {
313 *name = NULL;
314 }
315 if (nam_len) {
316 *nam_len = 0;
317 }
318
319 if (!*parent_times) {
320 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
321 if (strncmp(id, "current()", 9)) {
322 return -parsed;
323 }
324
325 parsed += 9;
326 id += 9;
327
328 while (isspace(id[0])) {
329 ++parsed;
330 ++id;
331 }
332
333 if (id[0] != '/') {
334 return -parsed;
335 }
336
337 ++parsed;
338 ++id;
339
340 while (isspace(id[0])) {
341 ++parsed;
342 ++id;
343 }
344
345 /* rel-path-keyexpr */
346 if (strncmp(id, "..", 2)) {
347 return -parsed;
348 }
349 ++par_times;
350
351 parsed += 2;
352 id += 2;
353
354 while (isspace(id[0])) {
355 ++parsed;
356 ++id;
357 }
358 }
359
360 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
361 *
362 * first parent reference with whitespaces already parsed
363 */
364 if (id[0] != '/') {
365 return -parsed;
366 }
367
368 ++parsed;
369 ++id;
370
371 while (isspace(id[0])) {
372 ++parsed;
373 ++id;
374 }
375
376 while (!strncmp(id, "..", 2) && !*parent_times) {
377 ++par_times;
378
379 parsed += 2;
380 id += 2;
381
382 while (isspace(id[0])) {
383 ++parsed;
384 ++id;
385 }
386
387 if (id[0] != '/') {
388 return -parsed;
389 }
390
391 ++parsed;
392 ++id;
393
394 while (isspace(id[0])) {
395 ++parsed;
396 ++id;
397 }
398 }
399
400 if (!*parent_times) {
401 *parent_times = par_times;
402 }
403
404 /* all parent references must be parsed at this point */
405 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
406 return -parsed+ret;
407 }
408
409 parsed += ret;
410 id += ret;
411
412 return parsed;
413}
414
415/**
416 * @brief Parse path-arg (leafref).
417 *
418 * path-arg = absolute-path / relative-path
419 * absolute-path = 1*("/" (node-identifier *path-predicate))
420 * relative-path = 1*(".." "/") descendant-path
421 *
Michal Vaskobb211122015-08-19 14:03:11 +0200422 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200423 * @param[out] prefix Points to the prefix, NULL if there is not any.
424 * @param[out] pref_len Length of the prefix, 0 if there is not any.
425 * @param[out] name Points to the node name.
426 * @param[out] nam_len Length of the node name.
427 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
428 * must not be changed between consecutive calls. -1 if the
429 * path is relative.
430 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
431 *
432 * @return Number of characters successfully parsed,
433 * positive on success, negative on failure.
434 */
435static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200436parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times,
437 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200438{
439 int parsed = 0, ret, par_times = 0;
440
441 assert(id);
442 assert(parent_times);
443 if (prefix) {
444 *prefix = NULL;
445 }
446 if (pref_len) {
447 *pref_len = 0;
448 }
449 if (name) {
450 *name = NULL;
451 }
452 if (nam_len) {
453 *nam_len = 0;
454 }
455 if (has_predicate) {
456 *has_predicate = 0;
457 }
458
459 if (!*parent_times && !strncmp(id, "..", 2)) {
460 ++par_times;
461
462 parsed += 2;
463 id += 2;
464
465 while (!strncmp(id, "/..", 3)) {
466 ++par_times;
467
468 parsed += 3;
469 id += 3;
470 }
471 }
472
473 if (!*parent_times) {
474 if (par_times) {
475 *parent_times = par_times;
476 } else {
477 *parent_times = -1;
478 }
479 }
480
481 if (id[0] != '/') {
482 return -parsed;
483 }
484
485 /* skip '/' */
486 ++parsed;
487 ++id;
488
489 /* node-identifier ([prefix:]identifier) */
490 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
491 return -parsed-ret;
492 }
493
494 parsed += ret;
495 id += ret;
496
497 /* there is no predicate */
498 if ((id[0] == '/') || !id[0]) {
499 return parsed;
500 } else if (id[0] != '[') {
501 return -parsed;
502 }
503
504 if (has_predicate) {
505 *has_predicate = 1;
506 }
507
508 return parsed;
509}
510
511/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200512 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200513 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200514 *
515 * instance-identifier = 1*("/" (node-identifier *predicate))
516 *
Michal Vaskobb211122015-08-19 14:03:11 +0200517 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200518 * @param[out] model Points to the model name.
519 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200520 * @param[out] name Points to the node name.
521 * @param[out] nam_len Length of the node name.
522 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
523 *
524 * @return Number of characters successfully parsed,
525 * positive on success, negative on failure.
526 */
527static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200528parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
529 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200530{
531 int parsed = 0, ret;
532
533 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200534 if (model) {
535 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200536 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200537 if (mod_len) {
538 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200539 }
540 if (name) {
541 *name = NULL;
542 }
543 if (nam_len) {
544 *nam_len = 0;
545 }
546 if (has_predicate) {
547 *has_predicate = 0;
548 }
549
550 if (id[0] != '/') {
551 return -parsed;
552 }
553
554 ++parsed;
555 ++id;
556
Michal Vasko1f2cc332015-08-19 11:18:32 +0200557 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
Michal Vaskobea08e92016-05-18 13:28:08 +0200558 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200559 }
560
561 parsed += ret;
562 id += ret;
563
564 if ((id[0] == '[') && has_predicate) {
565 *has_predicate = 1;
566 }
567
568 return parsed;
569}
570
571/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200572 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200573 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200574 *
575 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
576 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
577 * ((DQUOTE string DQUOTE) /
578 * (SQUOTE string SQUOTE))
579 * pos = non-negative-integer-value
580 *
Michal Vaskobb211122015-08-19 14:03:11 +0200581 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200582 * @param[out] model Points to the model name.
583 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200584 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
585 * @param[out] nam_len Length of the node name.
586 * @param[out] value Value the node-identifier must have (string from the grammar),
587 * NULL if there is not any.
588 * @param[out] val_len Length of the value, 0 if there is not any.
589 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
590 *
591 * @return Number of characters successfully parsed,
592 * positive on success, negative on failure.
593 */
594static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200595parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
596 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200597{
598 const char *ptr;
599 int parsed = 0, ret;
600 char quote;
601
602 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200603 if (model) {
604 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200605 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200606 if (mod_len) {
607 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200608 }
609 if (name) {
610 *name = NULL;
611 }
612 if (nam_len) {
613 *nam_len = 0;
614 }
615 if (value) {
616 *value = NULL;
617 }
618 if (val_len) {
619 *val_len = 0;
620 }
621 if (has_predicate) {
622 *has_predicate = 0;
623 }
624
625 if (id[0] != '[') {
626 return -parsed;
627 }
628
629 ++parsed;
630 ++id;
631
632 while (isspace(id[0])) {
633 ++parsed;
634 ++id;
635 }
636
637 /* pos */
638 if (isdigit(id[0])) {
639 if (name) {
640 *name = id;
641 }
642
643 if (id[0] == '0') {
644 ++parsed;
645 ++id;
646
647 if (isdigit(id[0])) {
648 return -parsed;
649 }
650 }
651
652 while (isdigit(id[0])) {
653 ++parsed;
654 ++id;
655 }
656
657 if (nam_len) {
658 *nam_len = id-(*name);
659 }
660
661 /* "." */
662 } else if (id[0] == '.') {
663 if (name) {
664 *name = id;
665 }
666 if (nam_len) {
667 *nam_len = 1;
668 }
669
670 ++parsed;
671 ++id;
672
673 /* node-identifier */
674 } else {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200675 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200676 return -parsed+ret;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200677 } else if (model && !*model) {
678 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200679 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200680
681 parsed += ret;
682 id += ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200683 }
684
685 while (isspace(id[0])) {
686 ++parsed;
687 ++id;
688 }
689
690 if (id[0] != '=') {
691 return -parsed;
692 }
693
694 ++parsed;
695 ++id;
696
697 while (isspace(id[0])) {
698 ++parsed;
699 ++id;
700 }
701
702 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
703 if ((id[0] == '\"') || (id[0] == '\'')) {
704 quote = id[0];
705
706 ++parsed;
707 ++id;
708
709 if ((ptr = strchr(id, quote)) == NULL) {
710 return -parsed;
711 }
712 ret = ptr-id;
713
714 if (value) {
715 *value = id;
716 }
717 if (val_len) {
718 *val_len = ret;
719 }
720
721 parsed += ret+1;
722 id += ret+1;
723 } else {
724 return -parsed;
725 }
726
727 while (isspace(id[0])) {
728 ++parsed;
729 ++id;
730 }
731
732 if (id[0] != ']') {
733 return -parsed;
734 }
735
736 ++parsed;
737 ++id;
738
739 if ((id[0] == '[') && has_predicate) {
740 *has_predicate = 1;
741 }
742
743 return parsed;
744}
745
746/**
747 * @brief Parse schema-nodeid.
748 *
749 * schema-nodeid = absolute-schema-nodeid /
750 * descendant-schema-nodeid
751 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200752 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200753 * node-identifier
754 * absolute-schema-nodeid
755 *
Michal Vaskobb211122015-08-19 14:03:11 +0200756 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200757 * @param[out] mod_name Points to the module name, NULL if there is not any.
758 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200759 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200760 * @param[out] nam_len Length of the node name.
761 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
762 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100763 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
764 * based on the grammar, in those cases use NULL.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200765 *
766 * @return Number of characters successfully parsed,
767 * positive on success, negative on failure.
768 */
Michal Vasko22448d32016-03-16 13:17:29 +0100769int
Michal Vasko723e50c2015-10-20 15:20:29 +0200770parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100771 int *is_relative, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200772{
773 int parsed = 0, ret;
774
775 assert(id);
776 assert(is_relative);
Michal Vasko723e50c2015-10-20 15:20:29 +0200777 if (mod_name) {
778 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200779 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200780 if (mod_name_len) {
781 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200782 }
783 if (name) {
784 *name = NULL;
785 }
786 if (nam_len) {
787 *nam_len = 0;
788 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100789 if (has_predicate) {
790 *has_predicate = 0;
791 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200792
793 if (id[0] != '/') {
794 if (*is_relative != -1) {
795 return -parsed;
796 } else {
797 *is_relative = 1;
798 }
Michal Vasko48935352016-03-29 11:52:36 +0200799 if (!strncmp(id, "./", 2)) {
800 parsed += 2;
801 id += 2;
802 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200803 } else {
804 if (*is_relative == -1) {
805 *is_relative = 0;
806 }
807 ++parsed;
808 ++id;
809 }
810
Michal Vasko723e50c2015-10-20 15:20:29 +0200811 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200812 return -parsed+ret;
813 }
814
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100815 parsed += ret;
816 id += ret;
817
818 if ((id[0] == '[') && has_predicate) {
819 *has_predicate = 1;
820 }
821
822 return parsed;
823}
824
825/**
826 * @brief Parse schema predicate (special format internally used).
827 *
828 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200829 * predicate-expr = "." / identifier / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100830 * key-with-value = identifier *WSP "=" *WSP
831 * ((DQUOTE string DQUOTE) /
832 * (SQUOTE string SQUOTE))
833 *
834 * @param[in] id Identifier to use.
835 * @param[out] name Points to the list key name.
836 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100837 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100838 * @param[out] val_len Length of \p value.
839 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
840 */
Michal Vasko22448d32016-03-16 13:17:29 +0100841int
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200842parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len,
Michal Vasko3547c532016-03-14 09:40:50 +0100843 int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100844{
845 const char *ptr;
846 int parsed = 0, ret;
847 char quote;
848
849 assert(id);
850 if (name) {
851 *name = NULL;
852 }
853 if (nam_len) {
854 *nam_len = 0;
855 }
856 if (value) {
857 *value = NULL;
858 }
859 if (val_len) {
860 *val_len = 0;
861 }
862 if (has_predicate) {
863 *has_predicate = 0;
864 }
865
866 if (id[0] != '[') {
867 return -parsed;
868 }
869
870 ++parsed;
871 ++id;
872
873 while (isspace(id[0])) {
874 ++parsed;
875 ++id;
876 }
877
Michal Vasko22448d32016-03-16 13:17:29 +0100878 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200879 if (id[0] == '.') {
880 ret = 1;
881 } else if ((ret = parse_identifier(id)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100882 return -parsed + ret;
883 }
884 if (name) {
885 *name = id;
886 }
887 if (nam_len) {
888 *nam_len = ret;
889 }
890
891 parsed += ret;
892 id += ret;
893
894 while (isspace(id[0])) {
895 ++parsed;
896 ++id;
897 }
898
899 /* there is value as well */
900 if (id[0] == '=') {
901 ++parsed;
902 ++id;
903
904 while (isspace(id[0])) {
905 ++parsed;
906 ++id;
907 }
908
909 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
910 if ((id[0] == '\"') || (id[0] == '\'')) {
911 quote = id[0];
912
913 ++parsed;
914 ++id;
915
916 if ((ptr = strchr(id, quote)) == NULL) {
917 return -parsed;
918 }
919 ret = ptr - id;
920
921 if (value) {
922 *value = id;
923 }
924 if (val_len) {
925 *val_len = ret;
926 }
927
928 parsed += ret + 1;
929 id += ret + 1;
930 } else {
931 return -parsed;
932 }
933
934 while (isspace(id[0])) {
935 ++parsed;
936 ++id;
937 }
Michal Vasko22448d32016-03-16 13:17:29 +0100938 } else if (value) {
939 /* if value was expected, it's mandatory */
940 return -parsed;
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100941 }
942
943 if (id[0] != ']') {
944 return -parsed;
945 }
946
947 ++parsed;
948 ++id;
949
950 if ((id[0] == '[') && has_predicate) {
951 *has_predicate = 1;
952 }
953
954 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200955}
956
957/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200958 * @brief Resolve (find) a feature definition. Logs directly.
959 *
960 * @param[in] feat_name Feature name to resolve.
961 * @param[in] len Length of \p feat_name.
962 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +0200963 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
964 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200965 *
Radek Krejci9ff0a922016-07-14 13:08:05 +0200966 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200967 */
968static int
Radek Krejci9ff0a922016-07-14 13:08:05 +0200969resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200970{
971 char *str;
972 const char *mod_name, *name;
973 int mod_name_len, nam_len, i, j;
974 const struct lys_module *module;
975
Radek Krejci9ff0a922016-07-14 13:08:05 +0200976 assert(feature);
977
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200978 /* check prefix */
979 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
980 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
981 return -1;
982 }
983
984 module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len);
985 if (!module) {
986 /* identity refers unknown data model */
987 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
988 return -1;
989 }
990
Radek Krejci9ff0a922016-07-14 13:08:05 +0200991 if (module != node->module && module == lys_node_module(node)) {
992 /* first, try to search directly in submodule where the feature was mentioned */
993 for (j = 0; j < node->module->features_size; j++) {
994 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
995 /* check status */
996 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
997 node->module->features[j].module, node->module->features[j].name, node)) {
998 return -1;
999 }
1000 *feature = &node->module->features[j];
1001 return 0;
1002 }
1003 }
1004 }
1005
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001006 /* search in the identified module ... */
1007 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001008 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001009 /* check status */
1010 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
1011 module->features[j].module, module->features[j].name, node)) {
1012 return -1;
1013 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001014 *feature = &module->features[j];
1015 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001016 }
1017 }
1018 /* ... and all its submodules */
1019 for (i = 0; i < module->inc_size; i++) {
1020 if (!module->inc[i].submodule) {
1021 /* not yet resolved */
1022 continue;
1023 }
1024 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001025 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1026 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001027 /* check status */
1028 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1029 module->inc[i].submodule->features[j].flags,
1030 module->inc[i].submodule->features[j].module,
1031 module->inc[i].submodule->features[j].name, node)) {
1032 return -1;
1033 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001034 *feature = &module->inc[i].submodule->features[j];
1035 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001036 }
1037 }
1038 }
1039
1040 /* not found */
1041 str = strndup(feat_name, len);
1042 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
1043 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001044 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001045}
1046
Radek Krejci9ff0a922016-07-14 13:08:05 +02001047/*
1048 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001049 * - 1 if enabled
1050 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001051 * - -1 if not usable by its if-feature expression
1052 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001053static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001054resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001055{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001056 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001057
Radek Krejci9ff0a922016-07-14 13:08:05 +02001058 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001059 if (!resolve_iffeature(&feat->iffeature[i])) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001060 return -1;
1061 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001062 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001063
Radek Krejci69b8d922016-07-27 13:13:41 +02001064 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001065}
1066
1067static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001068resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001069{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001070 uint8_t op;
1071 int rc, a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001072
Radek Krejci9ff0a922016-07-14 13:08:05 +02001073 op = iff_getop(expr->expr, *index_e);
1074 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001075
Radek Krejci9ff0a922016-07-14 13:08:05 +02001076 switch (op) {
1077 case LYS_IFF_F:
1078 /* resolve feature */
1079 return resolve_feature_value(expr->features[(*index_f)++]);
1080 case LYS_IFF_NOT:
1081 rc = resolve_iffeature_recursive(expr, index_e, index_f);
1082 if (rc == -1) {
1083 /* one of the referenced feature is hidden by its if-feature,
1084 * so this if-feature expression is always false */
1085 return -1;
1086 } else {
1087 /* invert result */
1088 return rc ? 0 : 1;
1089 }
1090 case LYS_IFF_AND:
1091 case LYS_IFF_OR:
1092 a = resolve_iffeature_recursive(expr, index_e, index_f);
1093 b = resolve_iffeature_recursive(expr, index_e, index_f);
1094 if (a == -1 || b == -1) {
1095 /* one of the referenced feature is hidden by its if-feature,
1096 * so this if-feature expression is always false */
1097 return -1;
1098 } else if (op == LYS_IFF_AND) {
1099 return a && b;
1100 } else { /* LYS_IFF_OR */
1101 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001102 }
1103 }
1104
Radek Krejci9ff0a922016-07-14 13:08:05 +02001105 return -1;
1106}
1107
1108int
1109resolve_iffeature(struct lys_iffeature *expr)
1110{
1111 int rc = -1;
1112 int index_e = 0, index_f = 0;
1113
1114 if (expr->expr) {
1115 rc = resolve_iffeature_recursive(expr, &index_e, &index_f);
1116 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001117 return (rc == 1) ? 1 : 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001118}
1119
1120struct iff_stack {
1121 int size;
1122 int index; /* first empty item */
1123 uint8_t *stack;
1124};
1125
1126static int
1127iff_stack_push(struct iff_stack *stack, uint8_t value)
1128{
1129 if (stack->index == stack->size) {
1130 stack->size += 4;
1131 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
1132 if (!stack->stack) {
1133 LOGMEM;
1134 stack->size = 0;
1135 return EXIT_FAILURE;
1136 }
1137 }
1138
1139 stack->stack[stack->index++] = value;
1140 return EXIT_SUCCESS;
1141}
1142
1143static uint8_t
1144iff_stack_pop(struct iff_stack *stack)
1145{
1146 stack->index--;
1147 return stack->stack[stack->index];
1148}
1149
1150static void
1151iff_stack_clean(struct iff_stack *stack)
1152{
1153 stack->size = 0;
1154 free(stack->stack);
1155}
1156
1157static void
1158iff_setop(uint8_t *list, uint8_t op, int pos)
1159{
1160 uint8_t *item;
1161 uint8_t mask = 3;
1162
1163 assert(pos >= 0);
1164 assert(op <= 3); /* max 2 bits */
1165
1166 item = &list[pos / 4];
1167 mask = mask << 2 * (pos % 4);
1168 *item = (*item) & ~mask;
1169 *item = (*item) | (op << 2 * (pos % 4));
1170}
1171
1172uint8_t
1173iff_getop(uint8_t *list, int pos)
1174{
1175 uint8_t *item;
1176 uint8_t mask = 3, result;
1177
1178 assert(pos >= 0);
1179
1180 item = &list[pos / 4];
1181 result = (*item) & (mask << 2 * (pos % 4));
1182 return result >> 2 * (pos % 4);
1183}
1184
1185#define LYS_IFF_LP 0x04 /* ( */
1186#define LYS_IFF_RP 0x08 /* ) */
1187
1188void
1189resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1190{
1191 unsigned int e = 0, f = 0, r = 0;
1192 uint8_t op;
1193
1194 assert(iffeat);
1195
1196 if (!iffeat->expr) {
1197 goto result;
1198 }
1199
1200 do {
1201 op = iff_getop(iffeat->expr, e++);
1202 switch (op) {
1203 case LYS_IFF_NOT:
1204 if (!r) {
1205 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001206 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001207 break;
1208 case LYS_IFF_AND:
1209 case LYS_IFF_OR:
1210 if (!r) {
1211 r += 2;
1212 } else {
1213 r += 1;
1214 }
1215 break;
1216 case LYS_IFF_F:
1217 f++;
1218 if (r) {
1219 r--;
1220 }
1221 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001222 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001223 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001224
Radek Krejci9ff0a922016-07-14 13:08:05 +02001225result:
1226 if (expr_size) {
1227 *expr_size = e;
1228 }
1229 if (feat_size) {
1230 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001231 }
1232}
1233
1234int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001235resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
1236 struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001237{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001238 const char *c = value;
1239 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001240 int i, j, last_not, checkversion = 0;
1241 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001242 uint8_t op;
1243 struct iff_stack stack = {0, 0, NULL};
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001244
Radek Krejci9ff0a922016-07-14 13:08:05 +02001245 assert(c);
1246
1247 if (isspace(c[0])) {
1248 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
1249 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001250 }
1251
Radek Krejci9ff0a922016-07-14 13:08:05 +02001252 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1253 for (i = j = last_not = 0; c[i]; i++) {
1254 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001255 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001256 j++;
1257 continue;
1258 } else if (c[i] == ')') {
1259 j--;
1260 continue;
1261 } else if (isspace(c[i])) {
1262 continue;
1263 }
1264
1265 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1266 if (c[i + r] == '\0') {
Radek Krejcia98da3f2016-07-27 14:05:22 +02001267 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001268 return EXIT_FAILURE;
1269 } else if (!isspace(c[i + r])) {
1270 /* feature name starting with the not/and/or */
1271 last_not = 0;
1272 f_size++;
1273 } else if (c[i] == 'n') { /* not operation */
1274 if (last_not) {
1275 /* double not */
1276 expr_size = expr_size - 2;
1277 last_not = 0;
1278 } else {
1279 last_not = 1;
1280 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001281 } else { /* and, or */
1282 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001283 /* not a not operation */
1284 last_not = 0;
1285 }
1286 i += r;
1287 } else {
1288 f_size++;
1289 last_not = 0;
1290 }
1291 expr_size++;
1292
1293 while (!isspace(c[i])) {
1294 if (!c[i] || c[i] == ')') {
1295 i--;
1296 break;
1297 }
1298 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001299 }
1300 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001301 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001302 /* not matching count of ( and ) */
1303 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1304 return EXIT_FAILURE;
1305 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001306
Radek Krejci69b8d922016-07-27 13:13:41 +02001307 if (checkversion || expr_size > 1) {
1308 /* check that we have 1.1 module */
1309 if (node->module->version != 2) {
1310 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1311 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
1312 return EXIT_FAILURE;
1313 }
1314 }
1315
Radek Krejci9ff0a922016-07-14 13:08:05 +02001316 /* allocate the memory */
1317 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
1318 iffeat_expr->features = malloc(f_size * sizeof *iffeat_expr->features);
1319 stack.size = expr_size;
1320 stack.stack = malloc(expr_size * sizeof *stack.stack);
1321 if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) {
1322 LOGMEM;
1323 goto error;
1324 }
1325 f_size--; expr_size--; /* used as indexes from now */
1326
1327 for (i--; i >= 0; i--) {
1328 if (c[i] == ')') {
1329 /* push it on stack */
1330 iff_stack_push(&stack, LYS_IFF_RP);
1331 continue;
1332 } else if (c[i] == '(') {
1333 /* pop from the stack into result all operators until ) */
1334 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1335 iff_setop(iffeat_expr->expr, op, expr_size--);
1336 }
1337 continue;
1338 } else if (isspace(c[i])) {
1339 continue;
1340 }
1341
1342 /* end operator or operand -> find beginning and get what is it */
1343 j = i + 1;
1344 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1345 i--;
1346 }
1347 i++; /* get back by one step */
1348
1349 if (!strncmp(&c[i], "not ", 4)) {
1350 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1351 /* double not */
1352 iff_stack_pop(&stack);
1353 } else {
1354 /* not has the highest priority, so do not pop from the stack
1355 * as in case of AND and OR */
1356 iff_stack_push(&stack, LYS_IFF_NOT);
1357 }
1358 } else if (!strncmp(&c[i], "and ", 4)) {
1359 /* as for OR - pop from the stack all operators with the same or higher
1360 * priority and store them to the result, then push the AND to the stack */
1361 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1362 op = iff_stack_pop(&stack);
1363 iff_setop(iffeat_expr->expr, op, expr_size--);
1364 }
1365 iff_stack_push(&stack, LYS_IFF_AND);
1366 } else if (!strncmp(&c[i], "or ", 3)) {
1367 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1368 op = iff_stack_pop(&stack);
1369 iff_setop(iffeat_expr->expr, op, expr_size--);
1370 }
1371 iff_stack_push(&stack, LYS_IFF_OR);
1372 } else {
1373 /* feature name, length is j - i */
1374
1375 /* add it to the result */
1376 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1377
1378 /* now get the link to the feature definition. Since it can be
1379 * forward referenced, we have hack for unres - until resolved,
1380 * the feature name is stored instead of link to the lys_feature */
1381 iffeat_expr->features[f_size] = (void*)strndup(&c[i], j - i);
1382 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, node);
1383 f_size--;
1384
1385 if (r == -1) {
1386 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001387 }
1388 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001389 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001390 while (stack.index) {
1391 op = iff_stack_pop(&stack);
1392 iff_setop(iffeat_expr->expr, op, expr_size--);
1393 }
1394
1395 if (++expr_size || ++f_size) {
1396 /* not all expected operators and operands found */
1397 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1398 rc = EXIT_FAILURE;
1399 } else {
1400 rc = EXIT_SUCCESS;
1401 }
1402
1403error:
1404 /* cleanup */
1405 iff_stack_clean(&stack);
1406
1407 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001408}
1409
1410/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001411 * @brief Resolve (find) a data node based on a schema-nodeid.
1412 *
1413 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1414 * module).
1415 *
1416 */
1417struct lyd_node *
1418resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1419{
1420 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001421 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001422 const struct lys_node *schema = NULL;
Radek Krejcicc217a62016-04-08 16:58:11 +02001423 int shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001424
1425 assert(nodeid && start);
1426
1427 if (nodeid[0] == '/') {
1428 return NULL;
1429 }
1430
1431 str = p = strdup(nodeid);
1432 if (!str) {
1433 LOGMEM;
1434 return NULL;
1435 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001436
Michal Vasko3edeaf72016-02-11 13:17:43 +01001437 while (p) {
1438 token = p;
1439 p = strchr(p, '/');
1440 if (p) {
1441 *p = '\0';
1442 p++;
1443 }
1444
Radek Krejci5da4eb62016-04-08 14:45:51 +02001445 if (p) {
1446 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001447 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001448 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001449 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001450 result = NULL;
1451 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001452 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001453
Radek Krejci5da4eb62016-04-08 14:45:51 +02001454 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1455 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001456 } else if (lys_parent(schema)->nodetype == LYS_CHOICE) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001457 /* shorthand case */
1458 if (!shorthand) {
1459 shorthand = 1;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001460 schema = lys_parent(schema);
Radek Krejcicc217a62016-04-08 16:58:11 +02001461 continue;
1462 } else {
1463 shorthand = 0;
1464 if (schema->nodetype == LYS_LEAF) {
1465 /* should not be here, since we have leaf, which is not a shorthand nor final node */
1466 result = NULL;
1467 break;
1468 }
1469 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001470 }
1471 } else {
1472 /* final node */
Radek Krejcif3c71de2016-04-11 12:45:46 +02001473 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF,
1474 shorthand ? 0 : 1, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001475 || !schema) {
1476 result = NULL;
1477 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001478 }
1479 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001480 LY_TREE_FOR(result ? result->child : start, iter) {
1481 if (iter->schema == schema) {
1482 /* move in data tree according to returned schema */
1483 result = iter;
1484 break;
1485 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001486 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001487 if (!iter) {
1488 /* instance not found */
1489 result = NULL;
1490 break;
1491 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001492 }
1493 free(str);
1494
1495 return result;
1496}
1497
Radek Krejcibdf92362016-04-08 14:43:34 +02001498/*
1499 * 0 - ok (done)
1500 * 1 - continue
1501 * 2 - break
1502 * -1 - error
1503 */
1504static int
Radek Krejcicc217a62016-04-08 16:58:11 +02001505schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id,
Radek Krejcibdf92362016-04-08 14:43:34 +02001506 const struct lys_module *module, const char *mod_name, int mod_name_len,
1507 const struct lys_node **start)
1508{
1509 const struct lys_module *prefix_mod;
Radek Krejcicc217a62016-04-08 16:58:11 +02001510 int sh = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001511
1512 /* module check */
1513 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1514 if (!prefix_mod) {
1515 return -1;
1516 }
1517 if (prefix_mod != lys_node_module(sibling)) {
1518 return 1;
1519 }
1520
1521 /* check for shorthand cases - then 'start' does not change */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001522 if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001523 if (*shorthand != -1) {
1524 *shorthand = *shorthand ? 0 : 1;
1525 }
1526 sh = 1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001527 }
1528
1529 /* the result node? */
1530 if (!id[0]) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001531 if (*shorthand == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001532 return 1;
1533 }
1534 return 0;
1535 }
1536
Radek Krejcicc217a62016-04-08 16:58:11 +02001537 if (!sh) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001538 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02001539 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001540 return -1;
1541 }
1542 *start = sibling->child;
1543 }
1544
1545 return 2;
1546}
1547
Michal Vasko3edeaf72016-02-11 13:17:43 +01001548/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1549int
1550resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module,
1551 const struct lys_node **ret)
1552{
1553 const char *name, *mod_name, *id;
1554 const struct lys_node *sibling;
1555 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001556 int8_t shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001557 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001558 const struct lys_module *start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001559
1560 assert(nodeid && (start || module) && !(start && module) && ret);
1561
1562 id = nodeid;
1563
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001564 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001565 return ((id - nodeid) - r) + 1;
1566 }
1567 id += r;
1568
1569 if ((is_relative && !start) || (!is_relative && !module)) {
1570 return -1;
1571 }
1572
1573 /* descendant-schema-nodeid */
1574 if (is_relative) {
Michal Vasko4c06fd82016-02-17 13:43:38 +01001575 module = start_mod = start->module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001576
1577 /* absolute-schema-nodeid */
1578 } else {
1579 start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001580 if (!start_mod) {
1581 return -1;
1582 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001583 start = start_mod->data;
1584 }
1585
1586 while (1) {
1587 sibling = NULL;
1588 while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod,
1589 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
1590 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001591 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001592 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1593 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001594 *ret = sibling;
1595 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001596 } else if (r == 1) {
1597 continue;
1598 } else if (r == 2) {
1599 break;
1600 } else {
1601 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001602 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001603 }
1604 }
1605
1606 /* no match */
1607 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001608 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001609 return EXIT_SUCCESS;
1610 }
1611
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001612 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001613 return ((id - nodeid) - r) + 1;
1614 }
1615 id += r;
1616 }
1617
1618 /* cannot get here */
1619 LOGINT;
1620 return -1;
1621}
1622
Radek Krejcif3c71de2016-04-11 12:45:46 +02001623/* unique, refine,
1624 * >0 - unexpected char on position (ret - 1),
1625 * 0 - ok (but ret can still be NULL),
1626 * -1 - error,
1627 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001628int
1629resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001630 int check_shorthand, int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001631{
1632 const char *name, *mod_name, *id;
1633 const struct lys_node *sibling;
1634 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001635 int8_t shorthand = check_shorthand ? 0 : -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001636 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001637 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001638
1639 assert(nodeid && start && ret);
1640 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1641
1642 id = nodeid;
1643 module = start->module;
1644
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001645 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001646 return ((id - nodeid) - r) + 1;
1647 }
1648 id += r;
1649
1650 if (!is_relative) {
1651 return -1;
1652 }
1653
1654 while (1) {
1655 sibling = NULL;
1656 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
1657 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
1658 /* name match */
1659 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001660 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1661 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001662 if (!(sibling->nodetype & ret_nodetype)) {
1663 /* wrong node type, too bad */
1664 continue;
1665 }
1666 *ret = sibling;
1667 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001668 } else if (r == 1) {
1669 continue;
1670 } else if (r == 2) {
1671 break;
1672 } else {
1673 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001674 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001675 }
1676 }
1677
1678 /* no match */
1679 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001680 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001681 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001682 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1683 *ret = NULL;
1684 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001685 }
1686
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001687 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001688 return ((id - nodeid) - r) + 1;
1689 }
1690 id += r;
1691 }
1692
1693 /* cannot get here */
1694 LOGINT;
1695 return -1;
1696}
1697
1698/* choice default */
1699int
1700resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1701{
1702 /* cannot actually be a path */
1703 if (strchr(nodeid, '/')) {
1704 return -1;
1705 }
1706
Radek Krejcif3c71de2016-04-11 12:45:46 +02001707 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001708}
1709
1710/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1711static int
1712resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1713{
1714 const struct lys_module *module;
1715 const char *mod_prefix, *name;
1716 int i, mod_prefix_len, nam_len;
1717
1718 /* parse the identifier, it must be parsed on one call */
1719 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1720 return -i + 1;
1721 }
1722
1723 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1724 if (!module) {
1725 return -1;
1726 }
1727 if (module != start->module) {
1728 start = module->data;
1729 }
1730
1731 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1732
1733 return EXIT_SUCCESS;
1734}
1735
1736int
1737resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1738 const struct lys_node **ret)
1739{
1740 const char *name, *mod_name, *id;
1741 const struct lys_node *sibling, *start;
1742 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001743 int8_t shorthand = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001744 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001745
1746 assert(nodeid && module && ret);
1747 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1748
1749 id = nodeid;
1750 start = module->data;
1751
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001752 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001753 return ((id - nodeid) - r) + 1;
1754 }
1755 id += r;
1756
1757 if (is_relative) {
1758 return -1;
1759 }
1760
1761 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001762 if (!abs_start_mod) {
1763 return -1;
1764 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001765
1766 while (1) {
1767 sibling = NULL;
1768 while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE
1769 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1770 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001771 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001772 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1773 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001774 if (!(sibling->nodetype & ret_nodetype)) {
1775 /* wrong node type, too bad */
1776 continue;
1777 }
1778 *ret = sibling;
1779 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001780 } else if (r == 1) {
1781 continue;
1782 } else if (r == 2) {
1783 break;
1784 } else {
1785 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001786 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001787 }
1788 }
1789
1790 /* no match */
1791 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001792 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001793 return EXIT_SUCCESS;
1794 }
1795
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001796 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001797 return ((id - nodeid) - r) + 1;
1798 }
1799 id += r;
1800 }
1801
1802 /* cannot get here */
1803 LOGINT;
1804 return -1;
1805}
1806
Michal Vaskoe733d682016-03-14 09:08:27 +01001807static int
Michal Vasko3547c532016-03-14 09:40:50 +01001808resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001809{
1810 const char *name;
1811 int nam_len, has_predicate, i;
1812
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001813 if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
1814 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001815 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001816 return -1;
1817 }
1818
1819 predicate += i;
1820 *parsed += i;
1821
1822 for (i = 0; i < list->keys_size; ++i) {
1823 if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) {
1824 break;
1825 }
1826 }
1827
1828 if (i == list->keys_size) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001829 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskoe733d682016-03-14 09:08:27 +01001830 return -1;
1831 }
1832
1833 /* more predicates? */
1834 if (has_predicate) {
Michal Vasko3547c532016-03-14 09:40:50 +01001835 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001836 }
1837
1838 return 0;
1839}
1840
Michal Vasko9fd98e22016-04-07 15:44:19 +02001841/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly
1842 * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */
Michal Vaskoe733d682016-03-14 09:08:27 +01001843const struct lys_node *
Michal Vaskoa9c8e992016-07-26 16:27:05 +02001844resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001845{
Michal Vasko10728b52016-04-07 14:26:29 +02001846 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001847 const char *name, *mod_name, *id;
Michal Vasko3547c532016-03-14 09:40:50 +01001848 const struct lys_node *sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001849 int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001850 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskoe733d682016-03-14 09:08:27 +01001851 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001852
Michal Vasko3547c532016-03-14 09:40:50 +01001853 assert(nodeid && (ctx || start));
1854 if (!ctx) {
1855 ctx = start->module->ctx;
1856 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001857
1858 id = nodeid;
1859
Michal Vaskoe733d682016-03-14 09:08:27 +01001860 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001861 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001862 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001863 }
1864 id += r;
1865
1866 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01001867 assert(start);
1868 start = start->child;
1869 if (!start) {
1870 /* no descendants, fail for sure */
Michal Vasko10728b52016-04-07 14:26:29 +02001871 str = strndup(nodeid, (name + nam_len) - nodeid);
1872 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1873 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001874 return NULL;
1875 }
1876 module = start->module;
1877 } else {
1878 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02001879 str = strndup(nodeid, (name + nam_len) - nodeid);
1880 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
1881 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001882 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02001883 } else if (mod_name_len > LY_BUF_SIZE - 1) {
1884 LOGINT;
1885 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01001886 }
1887
Michal Vasko971a3ca2016-04-01 13:09:29 +02001888 if (ly_buf_used && module_name[0]) {
1889 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1890 }
1891 ly_buf_used++;
1892
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001893 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001894 module_name[mod_name_len] = '\0';
1895 module = ly_ctx_get_module(ctx, module_name, NULL);
1896
1897 if (buf_backup) {
1898 /* return previous internal buffer content */
1899 strcpy(module_name, buf_backup);
1900 free(buf_backup);
1901 buf_backup = NULL;
1902 }
1903 ly_buf_used--;
1904
Michal Vasko3547c532016-03-14 09:40:50 +01001905 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02001906 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1907 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1908 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001909 return NULL;
1910 }
1911 start = module->data;
1912
1913 /* now it's as if there was no module name */
1914 mod_name = NULL;
1915 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01001916 }
1917
Michal Vaskoe733d682016-03-14 09:08:27 +01001918 prev_mod = module;
1919
Michal Vasko3edeaf72016-02-11 13:17:43 +01001920 while (1) {
1921 sibling = NULL;
Michal Vasko9fd98e22016-04-07 15:44:19 +02001922 while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ?
1923 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001924 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001925 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001926
1927 /* data RPC input/output check */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001928 if ((data_nodeid == 1) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_OUTPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001929 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001930 } else if ((data_nodeid == 2) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_INPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001931 continue;
1932 }
1933
Michal Vasko3edeaf72016-02-11 13:17:43 +01001934 /* module check */
1935 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001936 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01001937 LOGINT;
1938 return NULL;
1939 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001940
1941 if (ly_buf_used && module_name[0]) {
1942 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1943 }
1944 ly_buf_used++;
1945
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001946 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01001947 module_name[mod_name_len] = '\0';
1948 /* will also find an augment module */
1949 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001950
1951 if (buf_backup) {
1952 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001953 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001954 free(buf_backup);
1955 buf_backup = NULL;
1956 }
1957 ly_buf_used--;
1958
Michal Vasko3edeaf72016-02-11 13:17:43 +01001959 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001960 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1961 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1962 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001963 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001964 }
1965 } else {
1966 prefix_mod = prev_mod;
1967 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01001968 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001969 continue;
1970 }
1971
Michal Vaskoe733d682016-03-14 09:08:27 +01001972 /* do we have some predicates on it? */
1973 if (has_predicate) {
1974 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001975 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1976 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
1977 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
1978 return NULL;
1979 }
1980 } else if (sibling->nodetype == LYS_LIST) {
1981 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
1982 return NULL;
1983 }
1984 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01001985 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01001986 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01001987 }
1988 id += r;
1989 }
1990
Radek Krejcibdf92362016-04-08 14:43:34 +02001991 /* check for shorthand cases - then 'start' does not change */
Michal Vasko3c0f9f52016-05-17 16:38:10 +02001992 if (!data_nodeid && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001993 shorthand = ~shorthand;
1994 }
1995
Michal Vasko3edeaf72016-02-11 13:17:43 +01001996 /* the result node? */
1997 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001998 if (shorthand) {
1999 /* wrong path for shorthand */
Michal Vasko025e0452016-05-17 16:14:20 +02002000 str = strndup(nodeid, (name + nam_len) - nodeid);
2001 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko3c0f9f52016-05-17 16:38:10 +02002002 LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement.");
Radek Krejci9a5fccc2016-05-18 15:44:58 +02002003 free(str);
Michal Vasko025e0452016-05-17 16:14:20 +02002004 return NULL;
Radek Krejcibdf92362016-04-08 14:43:34 +02002005 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002006 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002007 }
2008
Michal Vasko3c0f9f52016-05-17 16:38:10 +02002009 if (data_nodeid || !shorthand) {
Michal Vasko7dc71d02016-03-15 10:42:28 +01002010 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002011 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002012 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko7dc71d02016-03-15 10:42:28 +01002013 return NULL;
2014 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002015 start = sibling->child;
2016 }
2017
2018 /* update prev mod */
2019 prev_mod = start->module;
2020 break;
2021 }
2022 }
2023
2024 /* no match */
2025 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002026 str = strndup(nodeid, (name + nam_len) - nodeid);
2027 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2028 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002029 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002030 }
2031
Michal Vaskoe733d682016-03-14 09:08:27 +01002032 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002033 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002034 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002035 }
2036 id += r;
2037 }
2038
2039 /* cannot get here */
2040 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01002041 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002042}
2043
Michal Vasko22448d32016-03-16 13:17:29 +01002044static int
2045resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed)
2046{
2047 const char *name, *value;
2048 int nam_len, val_len, has_predicate = 1, r;
2049 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002050 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01002051
Radek Krejci61a86c62016-03-24 11:06:44 +01002052 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002053 assert(node->schema->nodetype == LYS_LIST);
2054
Michal Vaskof29903d2016-04-18 13:13:10 +02002055 key = (struct lyd_node_leaf_list *)node->child;
2056 for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
2057 if (!key) {
2058 /* invalid data */
2059 LOGINT;
2060 return -1;
2061 }
Michal Vasko22448d32016-03-16 13:17:29 +01002062
Michal Vasko22448d32016-03-16 13:17:29 +01002063 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002064 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002065 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002066 }
2067
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002068 if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
2069 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002070 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002071 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002072 }
2073
2074 predicate += r;
2075 *parsed += r;
2076
Michal Vaskof29903d2016-04-18 13:13:10 +02002077 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002078 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002079 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002080 }
2081
2082 /* value does not match */
Michal Vaskof29903d2016-04-18 13:13:10 +02002083 if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002084 return 1;
2085 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002086
2087 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002088 }
2089
2090 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002091 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002092 return -1;
2093 }
2094
2095 return 0;
2096}
2097
Radek Krejci45826012016-08-24 15:07:57 +02002098/**
2099 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2100 *
2101 * @param[in] nodeid Node data path to find
2102 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2103 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2104 * @param[out] parsed Number of characters processed in \p id
2105 * @return The closes parent (or the node itself) from the path
2106 */
Michal Vasko22448d32016-03-16 13:17:29 +01002107struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002108resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2109 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002110{
Michal Vasko10728b52016-04-07 14:26:29 +02002111 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko22448d32016-03-16 13:17:29 +01002112 const char *id, *mod_name, *name;
2113 int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002114 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002115 struct lyd_node_leaf_list *llist;
Michal Vasko22448d32016-03-16 13:17:29 +01002116 const struct lys_module *prefix_mod, *prev_mod;
2117 struct ly_ctx *ctx;
2118
2119 assert(nodeid && start && parsed);
2120
2121 ctx = start->schema->module->ctx;
2122 id = nodeid;
2123
2124 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002125 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002126 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002127 return NULL;
2128 }
2129 id += r;
2130 /* add it to parsed only after the data node was actually found */
2131 last_parsed = r;
2132
2133 if (is_relative) {
2134 prev_mod = start->schema->module;
Michal Vasko22448d32016-03-16 13:17:29 +01002135 start = start->child;
2136 } else {
2137 for (; start->parent; start = start->parent);
Michal Vasko22448d32016-03-16 13:17:29 +01002138 prev_mod = start->schema->module;
2139 }
2140
2141 while (1) {
2142 LY_TREE_FOR(start, sibling) {
Michal Vasko2411b942016-03-23 13:50:03 +01002143 /* RPC data check, return simply invalid argument, because the data tree is invalid */
2144 if (lys_parent(sibling->schema)) {
2145 if (options & LYD_PATH_OPT_OUTPUT) {
2146 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002147 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002148 *parsed = -1;
2149 return NULL;
2150 }
2151 } else {
2152 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002153 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002154 *parsed = -1;
2155 return NULL;
2156 }
2157 }
2158 }
2159
Michal Vasko22448d32016-03-16 13:17:29 +01002160 /* name match */
2161 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2162
2163 /* module check */
2164 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002165 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01002166 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002167 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002168 return NULL;
2169 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002170
2171 if (ly_buf_used && module_name[0]) {
2172 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2173 }
2174 ly_buf_used++;
2175
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002176 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01002177 module_name[mod_name_len] = '\0';
2178 /* will also find an augment module */
2179 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002180
2181 if (buf_backup) {
2182 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002183 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002184 free(buf_backup);
2185 buf_backup = NULL;
2186 }
2187 ly_buf_used--;
2188
Michal Vasko22448d32016-03-16 13:17:29 +01002189 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002190 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2191 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2192 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002193 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002194 return NULL;
2195 }
2196 } else {
2197 prefix_mod = prev_mod;
2198 }
2199 if (prefix_mod != lys_node_module(sibling->schema)) {
2200 continue;
2201 }
2202
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002203 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002204 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002205 llist = (struct lyd_node_leaf_list *)sibling;
2206 if ((!llist_value && llist->value_str && llist->value_str[0])
2207 || (llist_value && strcmp(llist_value, llist->value_str))) {
2208 continue;
2209 }
Radek Krejci45826012016-08-24 15:07:57 +02002210 } else if (sibling->schema->nodetype == LYS_LIST) {
2211 /* list, we need predicates'n'stuff then */
Michal Vasko22448d32016-03-16 13:17:29 +01002212 r = 0;
2213 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002214 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002215 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002216 return NULL;
2217 }
2218 ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r);
2219 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002220 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002221 return NULL;
2222 } else if (ret == 1) {
2223 /* this list instance does not match */
2224 continue;
2225 }
2226 id += r;
2227 last_parsed += r;
2228 }
2229
2230 *parsed += last_parsed;
2231
2232 /* the result node? */
2233 if (!id[0]) {
2234 return sibling;
2235 }
2236
2237 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002238 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002239 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002240 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002241 return NULL;
2242 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002243 last_match = sibling;
Michal Vasko22448d32016-03-16 13:17:29 +01002244 start = sibling->child;
2245 if (start) {
2246 prev_mod = start->schema->module;
2247 }
2248 break;
2249 }
2250 }
2251
2252 /* no match, return last match */
2253 if (!sibling) {
2254 return last_match;
2255 }
2256
2257 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002258 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002259 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002260 return NULL;
2261 }
2262 id += r;
2263 last_parsed = r;
2264 }
2265
2266 /* cannot get here */
2267 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002268 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002269 return NULL;
2270}
2271
Michal Vasko3edeaf72016-02-11 13:17:43 +01002272/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002273 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002274 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002275 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02002276 * @param[in] str_restr Restriction as a string.
2277 * @param[in] type Type of the restriction.
2278 * @param[out] ret Final interval structure that starts with
2279 * the interval of the initial type, continues with intervals
2280 * of any superior types derived from the initial one, and
2281 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002282 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002283 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002284 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002285int
Michal Vaskoaeb51802016-04-11 10:58:47 +02002286resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002287{
2288 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002289 int kind;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002290 int64_t local_smin, local_smax;
2291 uint64_t local_umin, local_umax;
2292 long double local_fmin, local_fmax;
2293 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002294 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002295
2296 switch (type->base) {
2297 case LY_TYPE_BINARY:
2298 kind = 0;
2299 local_umin = 0;
2300 local_umax = 18446744073709551615UL;
2301
2302 if (!str_restr && type->info.binary.length) {
2303 str_restr = type->info.binary.length->expr;
2304 }
2305 break;
2306 case LY_TYPE_DEC64:
2307 kind = 2;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02002308 local_fmin = ((long double)-9223372036854775808.0) / type->info.dec64.div;
2309 local_fmax = ((long double)9223372036854775807.0) / type->info.dec64.div;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002310
2311 if (!str_restr && type->info.dec64.range) {
2312 str_restr = type->info.dec64.range->expr;
2313 }
2314 break;
2315 case LY_TYPE_INT8:
2316 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002317 local_smin = __INT64_C(-128);
2318 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002319
2320 if (!str_restr && type->info.num.range) {
2321 str_restr = type->info.num.range->expr;
2322 }
2323 break;
2324 case LY_TYPE_INT16:
2325 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002326 local_smin = __INT64_C(-32768);
2327 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002328
2329 if (!str_restr && type->info.num.range) {
2330 str_restr = type->info.num.range->expr;
2331 }
2332 break;
2333 case LY_TYPE_INT32:
2334 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002335 local_smin = __INT64_C(-2147483648);
2336 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002337
2338 if (!str_restr && type->info.num.range) {
2339 str_restr = type->info.num.range->expr;
2340 }
2341 break;
2342 case LY_TYPE_INT64:
2343 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002344 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2345 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002346
2347 if (!str_restr && type->info.num.range) {
2348 str_restr = type->info.num.range->expr;
2349 }
2350 break;
2351 case LY_TYPE_UINT8:
2352 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002353 local_umin = __UINT64_C(0);
2354 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002355
2356 if (!str_restr && type->info.num.range) {
2357 str_restr = type->info.num.range->expr;
2358 }
2359 break;
2360 case LY_TYPE_UINT16:
2361 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002362 local_umin = __UINT64_C(0);
2363 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002364
2365 if (!str_restr && type->info.num.range) {
2366 str_restr = type->info.num.range->expr;
2367 }
2368 break;
2369 case LY_TYPE_UINT32:
2370 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002371 local_umin = __UINT64_C(0);
2372 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002373
2374 if (!str_restr && type->info.num.range) {
2375 str_restr = type->info.num.range->expr;
2376 }
2377 break;
2378 case LY_TYPE_UINT64:
2379 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002380 local_umin = __UINT64_C(0);
2381 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002382
2383 if (!str_restr && type->info.num.range) {
2384 str_restr = type->info.num.range->expr;
2385 }
2386 break;
2387 case LY_TYPE_STRING:
2388 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002389 local_umin = __UINT64_C(0);
2390 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002391
2392 if (!str_restr && type->info.str.length) {
2393 str_restr = type->info.str.length->expr;
2394 }
2395 break;
2396 default:
2397 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002398 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002399 }
2400
2401 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002402 if (type->der) {
2403 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002404 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002405 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002406 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002407 assert(!intv || (intv->kind == kind));
2408 }
2409
2410 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002411 /* we do not have any restriction, return superior ones */
2412 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002413 return EXIT_SUCCESS;
2414 }
2415
2416 /* adjust local min and max */
2417 if (intv) {
2418 tmp_intv = intv;
2419
2420 if (kind == 0) {
2421 local_umin = tmp_intv->value.uval.min;
2422 } else if (kind == 1) {
2423 local_smin = tmp_intv->value.sval.min;
2424 } else if (kind == 2) {
2425 local_fmin = tmp_intv->value.fval.min;
2426 }
2427
2428 while (tmp_intv->next) {
2429 tmp_intv = tmp_intv->next;
2430 }
2431
2432 if (kind == 0) {
2433 local_umax = tmp_intv->value.uval.max;
2434 } else if (kind == 1) {
2435 local_smax = tmp_intv->value.sval.max;
2436 } else if (kind == 2) {
2437 local_fmax = tmp_intv->value.fval.max;
2438 }
2439 }
2440
2441 /* finally parse our restriction */
2442 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002443 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002444 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002445 if (!tmp_local_intv) {
2446 assert(!local_intv);
2447 local_intv = malloc(sizeof *local_intv);
2448 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002449 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002450 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002451 tmp_local_intv = tmp_local_intv->next;
2452 }
Michal Vasko253035f2015-12-17 16:58:13 +01002453 if (!tmp_local_intv) {
2454 LOGMEM;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002455 goto error;
Michal Vasko253035f2015-12-17 16:58:13 +01002456 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002457
2458 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002459 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002460 tmp_local_intv->next = NULL;
2461
2462 /* min */
2463 ptr = seg_ptr;
2464 while (isspace(ptr[0])) {
2465 ++ptr;
2466 }
2467 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2468 if (kind == 0) {
2469 tmp_local_intv->value.uval.min = atoll(ptr);
2470 } else if (kind == 1) {
2471 tmp_local_intv->value.sval.min = atoll(ptr);
2472 } else if (kind == 2) {
2473 tmp_local_intv->value.fval.min = atoll(ptr);
2474 }
2475
2476 if ((ptr[0] == '+') || (ptr[0] == '-')) {
2477 ++ptr;
2478 }
2479 while (isdigit(ptr[0])) {
2480 ++ptr;
2481 }
2482 } else if (!strncmp(ptr, "min", 3)) {
2483 if (kind == 0) {
2484 tmp_local_intv->value.uval.min = local_umin;
2485 } else if (kind == 1) {
2486 tmp_local_intv->value.sval.min = local_smin;
2487 } else if (kind == 2) {
2488 tmp_local_intv->value.fval.min = local_fmin;
2489 }
2490
2491 ptr += 3;
2492 } else if (!strncmp(ptr, "max", 3)) {
2493 if (kind == 0) {
2494 tmp_local_intv->value.uval.min = local_umax;
2495 } else if (kind == 1) {
2496 tmp_local_intv->value.sval.min = local_smax;
2497 } else if (kind == 2) {
2498 tmp_local_intv->value.fval.min = local_fmax;
2499 }
2500
2501 ptr += 3;
2502 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002503 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002504 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002505 }
2506
2507 while (isspace(ptr[0])) {
2508 ptr++;
2509 }
2510
2511 /* no interval or interval */
2512 if ((ptr[0] == '|') || !ptr[0]) {
2513 if (kind == 0) {
2514 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2515 } else if (kind == 1) {
2516 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2517 } else if (kind == 2) {
2518 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2519 }
2520 } else if (!strncmp(ptr, "..", 2)) {
2521 /* skip ".." */
2522 ptr += 2;
2523 while (isspace(ptr[0])) {
2524 ++ptr;
2525 }
2526
2527 /* max */
2528 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2529 if (kind == 0) {
2530 tmp_local_intv->value.uval.max = atoll(ptr);
2531 } else if (kind == 1) {
2532 tmp_local_intv->value.sval.max = atoll(ptr);
2533 } else if (kind == 2) {
2534 tmp_local_intv->value.fval.max = atoll(ptr);
2535 }
2536 } else if (!strncmp(ptr, "max", 3)) {
2537 if (kind == 0) {
2538 tmp_local_intv->value.uval.max = local_umax;
2539 } else if (kind == 1) {
2540 tmp_local_intv->value.sval.max = local_smax;
2541 } else if (kind == 2) {
2542 tmp_local_intv->value.fval.max = local_fmax;
2543 }
2544 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002545 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002546 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002547 }
2548 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002549 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002550 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002551 }
2552
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002553 /* check min and max in correct order*/
2554 if (kind == 0) {
2555 /* current segment */
2556 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2557 goto error;
2558 }
2559 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2560 goto error;
2561 }
2562 /* segments sholud be ascending order */
2563 if (tmp_intv && (tmp_intv->value.uval.max > tmp_local_intv->value.uval.min)) {
2564 goto error;
2565 }
2566 } else if (kind == 1) {
2567 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
2568 goto error;
2569 }
2570 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
2571 goto error;
2572 }
2573 if (tmp_intv && (tmp_intv->value.sval.max > tmp_local_intv->value.sval.min)) {
2574 goto error;
2575 }
2576 } else if (kind == 2) {
2577 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
2578 goto error;
2579 }
2580 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
2581 goto error;
2582 }
2583 if (tmp_intv && (tmp_intv->value.fval.max > tmp_local_intv->value.fval.min)) {
2584 goto error;
2585 }
2586 }
2587
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002588 /* next segment (next OR) */
2589 seg_ptr = strchr(seg_ptr, '|');
2590 if (!seg_ptr) {
2591 break;
2592 }
2593 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002594 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002595 }
2596
2597 /* check local restrictions against superior ones */
2598 if (intv) {
2599 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002600 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002601
2602 while (tmp_local_intv && tmp_intv) {
2603 /* reuse local variables */
2604 if (kind == 0) {
2605 local_umin = tmp_local_intv->value.uval.min;
2606 local_umax = tmp_local_intv->value.uval.max;
2607
2608 /* it must be in this interval */
2609 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2610 /* this interval is covered, next one */
2611 if (local_umax <= tmp_intv->value.uval.max) {
2612 tmp_local_intv = tmp_local_intv->next;
2613 continue;
2614 /* ascending order of restrictions -> fail */
2615 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002616 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002617 }
2618 }
2619 } else if (kind == 1) {
2620 local_smin = tmp_local_intv->value.sval.min;
2621 local_smax = tmp_local_intv->value.sval.max;
2622
2623 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2624 if (local_smax <= tmp_intv->value.sval.max) {
2625 tmp_local_intv = tmp_local_intv->next;
2626 continue;
2627 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002628 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002629 }
2630 }
2631 } else if (kind == 2) {
2632 local_fmin = tmp_local_intv->value.fval.min;
2633 local_fmax = tmp_local_intv->value.fval.max;
2634
2635 if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) {
2636 if (local_fmax <= tmp_intv->value.fval.max) {
2637 tmp_local_intv = tmp_local_intv->next;
2638 continue;
2639 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002640 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002641 }
2642 }
2643 }
2644
2645 tmp_intv = tmp_intv->next;
2646 }
2647
2648 /* some interval left uncovered -> fail */
2649 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002650 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002651 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002652 }
2653
Michal Vaskoaeb51802016-04-11 10:58:47 +02002654 /* append the local intervals to all the intervals of the superior types, return it all */
2655 if (intv) {
2656 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2657 tmp_intv->next = local_intv;
2658 } else {
2659 intv = local_intv;
2660 }
2661 *ret = intv;
2662
2663 return EXIT_SUCCESS;
2664
2665error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002666 while (intv) {
2667 tmp_intv = intv->next;
2668 free(intv);
2669 intv = tmp_intv;
2670 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02002671 while (local_intv) {
2672 tmp_local_intv = local_intv->next;
2673 free(local_intv);
2674 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002675 }
2676
Michal Vaskoaeb51802016-04-11 10:58:47 +02002677 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002678}
2679
Michal Vasko730dfdf2015-08-11 14:48:05 +02002680/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02002681 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
2682 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002683 *
2684 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002685 * @param[in] mod_name Typedef name module name.
2686 * @param[in] module Main module.
2687 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002688 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002689 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002690 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002691 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002692int
Michal Vasko1e62a092015-12-01 12:27:20 +01002693resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2694 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002695{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002696 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002697 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002698 int tpdf_size;
2699
Michal Vasko1dca6882015-10-22 14:29:42 +02002700 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002701 /* no prefix, try built-in types */
2702 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
2703 if (!strcmp(ly_types[i].def->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002704 if (ret) {
2705 *ret = ly_types[i].def;
2706 }
2707 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002708 }
2709 }
2710 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002711 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002712 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002713 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002714 }
2715 }
2716
Michal Vasko1dca6882015-10-22 14:29:42 +02002717 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002718 /* search in local typedefs */
2719 while (parent) {
2720 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002721 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002722 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2723 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002724 break;
2725
Radek Krejci76512572015-08-04 09:47:08 +02002726 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002727 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2728 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002729 break;
2730
Radek Krejci76512572015-08-04 09:47:08 +02002731 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002732 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2733 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002734 break;
2735
Radek Krejci76512572015-08-04 09:47:08 +02002736 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002737 case LYS_ACTION:
2738 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
2739 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002740 break;
2741
Radek Krejci76512572015-08-04 09:47:08 +02002742 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002743 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2744 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002745 break;
2746
Radek Krejci76512572015-08-04 09:47:08 +02002747 case LYS_INPUT:
2748 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02002749 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
2750 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002751 break;
2752
2753 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02002754 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002755 continue;
2756 }
2757
2758 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002759 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002760 match = &tpdf[i];
2761 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002762 }
2763 }
2764
Michal Vaskodcf98e62016-05-05 17:53:53 +02002765 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002766 }
Radek Krejcic071c542016-01-27 14:57:51 +01002767 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002768 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002769 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002770 if (!module) {
2771 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002772 }
2773 }
2774
2775 /* search in top level typedefs */
2776 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002777 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002778 match = &module->tpdf[i];
2779 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002780 }
2781 }
2782
2783 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01002784 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002785 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002786 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 +02002787 match = &module->inc[i].submodule->tpdf[j];
2788 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002789 }
2790 }
2791 }
2792
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002793 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002794
2795check_leafref:
2796 if (ret) {
2797 *ret = match;
2798 }
2799 if (match->type.base == LY_TYPE_LEAFREF) {
2800 while (!match->type.info.lref.path) {
2801 match = match->type.der;
2802 assert(match);
2803 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02002804 }
2805 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002806}
2807
Michal Vasko1dca6882015-10-22 14:29:42 +02002808/**
2809 * @brief Check the default \p value of the \p type. Logs directly.
2810 *
2811 * @param[in] type Type definition to use.
2812 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01002813 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02002814 *
2815 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
2816 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002817static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002818check_default(struct lys_type *type, const char *value, struct lys_module *module)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002819{
Radek Krejcibad2f172016-08-02 11:04:15 +02002820 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02002821 struct lyd_node_leaf_list node;
Radek Krejci37b756f2016-01-18 10:15:03 +01002822 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02002823
Radek Krejcic13db382016-08-16 10:52:42 +02002824 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02002825 /* the type was not resolved yet, nothing to do for now */
2826 return EXIT_FAILURE;
2827 }
2828
2829 if (!value) {
2830 /* we do not have a new default value, so is there any to check even, in some base type? */
2831 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
2832 if (base_tpdf->dflt) {
2833 value = base_tpdf->dflt;
2834 break;
2835 }
2836 }
2837
2838 if (!value) {
2839 /* no default value, nothing to check, all is well */
2840 return EXIT_SUCCESS;
2841 }
2842
2843 /* 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)? */
2844 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02002845 case LY_TYPE_IDENT:
2846 case LY_TYPE_INST:
2847 case LY_TYPE_LEAFREF:
2848 case LY_TYPE_BOOL:
2849 case LY_TYPE_EMPTY:
2850 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
2851 return EXIT_SUCCESS;
Radek Krejcibad2f172016-08-02 11:04:15 +02002852 case LY_TYPE_BITS:
2853 /* the default value must match the restricted list of values, if the type was restricted */
2854 if (type->info.bits.count) {
2855 break;
2856 }
2857 return EXIT_SUCCESS;
2858 case LY_TYPE_ENUM:
2859 /* the default value must match the restricted list of values, if the type was restricted */
2860 if (type->info.enums.count) {
2861 break;
2862 }
2863 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02002864 case LY_TYPE_DEC64:
2865 if (type->info.dec64.range) {
2866 break;
2867 }
2868 return EXIT_SUCCESS;
2869 case LY_TYPE_BINARY:
2870 if (type->info.binary.length) {
2871 break;
2872 }
2873 return EXIT_SUCCESS;
2874 case LY_TYPE_INT8:
2875 case LY_TYPE_INT16:
2876 case LY_TYPE_INT32:
2877 case LY_TYPE_INT64:
2878 case LY_TYPE_UINT8:
2879 case LY_TYPE_UINT16:
2880 case LY_TYPE_UINT32:
2881 case LY_TYPE_UINT64:
2882 if (type->info.num.range) {
2883 break;
2884 }
2885 return EXIT_SUCCESS;
2886 case LY_TYPE_STRING:
2887 if (type->info.str.length || type->info.str.patterns) {
2888 break;
2889 }
2890 return EXIT_SUCCESS;
2891 case LY_TYPE_UNION:
2892 /* way too much trouble learning whether we need to check the default again, so just do it */
2893 break;
2894 default:
2895 LOGINT;
2896 return -1;
2897 }
2898 }
2899
Michal Vasko1dca6882015-10-22 14:29:42 +02002900 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01002901 memset(&node, 0, sizeof node);
Michal Vasko1dca6882015-10-22 14:29:42 +02002902 node.value_str = value;
2903 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01002904 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko253035f2015-12-17 16:58:13 +01002905 if (!node.schema) {
2906 LOGMEM;
2907 return -1;
2908 }
Radek Krejcibad2f172016-08-02 11:04:15 +02002909 node.schema->name = strdup("fake-default");
Michal Vasko253035f2015-12-17 16:58:13 +01002910 if (!node.schema->name) {
2911 LOGMEM;
Michal Vaskof49eda02016-07-21 12:17:01 +02002912 free(node.schema);
Michal Vasko253035f2015-12-17 16:58:13 +01002913 return -1;
2914 }
Michal Vasko56826402016-03-02 11:11:37 +01002915 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01002916 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02002917
Radek Krejci37b756f2016-01-18 10:15:03 +01002918 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02002919 if (!type->info.lref.target) {
2920 ret = EXIT_FAILURE;
2921 goto finish;
2922 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002923 ret = check_default(&type->info.lref.target->type, value, module);
Michal Vasko1dca6882015-10-22 14:29:42 +02002924
2925 } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) {
2926 /* it was converted to JSON format before, nothing else sensible we can do */
2927
2928 } else {
Michal Vasko3767fb22016-07-21 12:10:57 +02002929 if (lyp_parse_value(&node, NULL, 1)) {
2930 ret = -1;
Radek Krejcibad2f172016-08-02 11:04:15 +02002931 if (base_tpdf) {
2932 /* default value was is defined in some base typedef */
2933 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
2934 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
2935 /* we have refined bits/enums */
2936 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2937 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
2938 value, type->parent->name, base_tpdf->name);
2939 }
2940 }
Michal Vasko3767fb22016-07-21 12:10:57 +02002941 }
Michal Vasko1dca6882015-10-22 14:29:42 +02002942 }
2943
2944finish:
2945 if (node.value_type == LY_TYPE_BITS) {
2946 free(node.value.bit);
2947 }
2948 free((char *)node.schema->name);
2949 free(node.schema);
2950
2951 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002952}
2953
Michal Vasko730dfdf2015-08-11 14:48:05 +02002954/**
2955 * @brief Check a key for mandatory attributes. Logs directly.
2956 *
2957 * @param[in] key The key to check.
2958 * @param[in] flags What flags to check.
2959 * @param[in] list The list of all the keys.
2960 * @param[in] index Index of the key in the key list.
2961 * @param[in] name The name of the keys.
2962 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002963 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002964 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002965 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002966static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002967check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002968{
Radek Krejciadb57612016-02-16 13:34:34 +01002969 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002970 char *dup = NULL;
2971 int j;
2972
2973 /* existence */
2974 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02002975 if (name[len] != '\0') {
2976 dup = strdup(name);
Michal Vasko253035f2015-12-17 16:58:13 +01002977 if (!dup) {
2978 LOGMEM;
2979 return -1;
2980 }
Michal Vaskof02e3742015-08-05 16:27:02 +02002981 dup[len] = '\0';
2982 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002983 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002984 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002985 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002986 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002987 }
2988
2989 /* uniqueness */
2990 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01002991 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002992 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002993 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002994 }
2995 }
2996
2997 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02002998 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002999 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003000 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003001 }
3002
3003 /* type of the leaf is not built-in empty */
3004 if (key->type.base == LY_TYPE_EMPTY) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003005 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003006 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003007 }
3008
3009 /* config attribute is the same as of the list */
Radek Krejciadb57612016-02-16 13:34:34 +01003010 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003011 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003012 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003013 }
3014
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003015 /* key is not placed from augment */
3016 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003017 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3018 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003019 return -1;
3020 }
3021
Radek Krejci3f21ada2016-08-01 13:34:31 +02003022 /* key is not when/if-feature -conditional */
3023 j = 0;
3024 if (key->when || (key->iffeature_size && (j = 1))) {
3025 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3026 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"%s\" condition.",
3027 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003028 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003029 }
3030
Michal Vasko0b85aa82016-03-07 14:37:43 +01003031 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003032}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003033
3034/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003035 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003036 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003037 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003038 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003039 *
3040 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3041 */
3042int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003043resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003044{
Radek Krejci581ce772015-11-10 17:22:40 +01003045 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003046 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003047
Radek Krejcif3c71de2016-04-11 12:45:46 +02003048 rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003049 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003050 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003051 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003052 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003053 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003054 } else if (rc == -2) {
Michal Vaskoc66c6d82016-04-12 11:37:31 +02003055 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003056 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003057 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003058 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003059 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3060 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003061 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003062 }
Radek Krejci581ce772015-11-10 17:22:40 +01003063 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003064 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003065 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003066 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3067 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003068 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003069 }
3070
Radek Krejcicf509982015-12-15 09:22:44 +01003071 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01003072 if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003073 return -1;
3074 }
3075
Radek Krejcid09d1a52016-08-11 14:05:45 +02003076 /* check that all unique's targets are of the same config type */
3077 if (*trg_type) {
3078 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
3079 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3080 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent,
3081 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3082 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3083 return -1;
3084 }
3085 } else {
3086 /* first unique */
3087 if (leaf->flags & LYS_CONFIG_W) {
3088 *trg_type = 1;
3089 } else {
3090 *trg_type = 2;
3091 }
3092 }
3093
Radek Krejcica7efb72016-01-18 13:06:01 +01003094 /* set leaf's unique flag */
3095 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3096
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003097 return EXIT_SUCCESS;
3098
3099error:
3100
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003101 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003102}
3103
Radek Krejci0c0086a2016-03-24 15:20:28 +01003104void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003105unres_data_del(struct unres_data *unres, uint32_t i)
3106{
3107 /* there are items after the one deleted */
3108 if (i+1 < unres->count) {
3109 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003110 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003111
3112 /* deleting the last item */
3113 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003114 free(unres->node);
3115 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003116 }
3117
3118 /* if there are no items after and it is not the last one, just move the counter */
3119 --unres->count;
3120}
3121
Michal Vasko0491ab32015-08-19 14:28:29 +02003122/**
3123 * @brief Resolve (find) a data node from a specific module. Does not log.
3124 *
3125 * @param[in] mod Module to search in.
3126 * @param[in] name Name of the data node.
3127 * @param[in] nam_len Length of the name.
3128 * @param[in] start Data node to start the search from.
3129 * @param[in,out] parents Resolved nodes. If there are some parents,
3130 * they are replaced (!!) with the resolvents.
3131 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003132 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003133 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003134static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003135resolve_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 +02003136{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003137 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003138 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003139 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003140
Michal Vasko23b61ec2015-08-19 11:19:50 +02003141 if (!parents->count) {
3142 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003143 parents->node = malloc(sizeof *parents->node);
Michal Vasko253035f2015-12-17 16:58:13 +01003144 if (!parents->node) {
3145 LOGMEM;
Michal Vasko2471e7f2016-04-11 11:00:15 +02003146 return -1;
Michal Vasko253035f2015-12-17 16:58:13 +01003147 }
Michal Vaskocf024702015-10-08 15:01:42 +02003148 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003149 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003150 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003151 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003152 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003153 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003154 continue;
3155 }
3156 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003157 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003158 if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len)
3159 && node->schema->name[nam_len] == '\0') {
3160 /* matching target */
3161 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003162 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003163 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003164 flag = 1;
3165 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003166 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003167 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003168 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
3169 if (!parents->node) {
3170 return EXIT_FAILURE;
3171 }
Michal Vaskocf024702015-10-08 15:01:42 +02003172 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003173 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003174 }
3175 }
3176 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003177
3178 if (!flag) {
3179 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003180 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003181 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003182 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003183 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003184 }
3185
Michal Vasko0491ab32015-08-19 14:28:29 +02003186 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003187}
3188
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003189/**
3190 * @brief Resolve (find) a data node. Does not log.
3191 *
Radek Krejci581ce772015-11-10 17:22:40 +01003192 * @param[in] mod_name Module name of the data node.
3193 * @param[in] mod_name_len Length of the module name.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003194 * @param[in] name Name of the data node.
3195 * @param[in] nam_len Length of the name.
3196 * @param[in] start Data node to start the search from.
3197 * @param[in,out] parents Resolved nodes. If there are some parents,
3198 * they are replaced (!!) with the resolvents.
3199 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003200 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003201 */
Radek Krejcic5090c32015-08-12 09:46:19 +02003202static int
Radek Krejci581ce772015-11-10 17:22:40 +01003203resolve_data_node(const char *mod_name, int mod_name_len, const char *name, int name_len, struct lyd_node *start,
Michal Vasko23b61ec2015-08-19 11:19:50 +02003204 struct unres_data *parents)
Radek Krejcic5090c32015-08-12 09:46:19 +02003205{
Michal Vasko1e62a092015-12-01 12:27:20 +01003206 const struct lys_module *mod;
Michal Vasko31fc3672015-10-21 12:08:13 +02003207 char *str;
Radek Krejcic5090c32015-08-12 09:46:19 +02003208
Michal Vasko23b61ec2015-08-19 11:19:50 +02003209 assert(start);
3210
Michal Vasko31fc3672015-10-21 12:08:13 +02003211 if (mod_name) {
3212 /* we have mod_name, find appropriate module */
3213 str = strndup(mod_name, mod_name_len);
Michal Vasko253035f2015-12-17 16:58:13 +01003214 if (!str) {
3215 LOGMEM;
3216 return -1;
3217 }
Michal Vasko31fc3672015-10-21 12:08:13 +02003218 mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL);
3219 free(str);
Radek Krejcic5090c32015-08-12 09:46:19 +02003220 if (!mod) {
3221 /* invalid prefix */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003222 return -1;
Radek Krejcic5090c32015-08-12 09:46:19 +02003223 }
3224 } else {
3225 /* no prefix, module is the same as of current node */
3226 mod = start->schema->module;
3227 }
3228
3229 return resolve_data(mod, name, name_len, start, parents);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003230}
3231
Michal Vasko730dfdf2015-08-11 14:48:05 +02003232/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003233 * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly
Radek Krejci48464ed2016-03-17 15:44:09 +01003234 * only specific errors, general no-resolvent error is left to the caller.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003235 *
Michal Vaskobb211122015-08-19 14:03:11 +02003236 * @param[in] pred Predicate to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003237 * @param[in] node Node from which the predicate is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02003238 * @param[in,out] node_match Nodes satisfying the restriction
3239 * without the predicate. Nodes not
3240 * satisfying the predicate are removed.
Michal Vasko0491ab32015-08-19 14:28:29 +02003241 * @param[out] parsed Number of characters parsed, negative on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003242 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003243 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003244 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003245static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003246resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match,
Radek Krejci010e54b2016-03-15 09:40:34 +01003247 int *parsed)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003248{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003249 /* ... /node[source = destination] ... */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003250 struct unres_data source_match, dest_match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003251 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vasko0491ab32015-08-19 14:28:29 +02003252 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0;
3253 int has_predicate, dest_parent_times, i, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003254 uint32_t j;
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003255 struct lyd_node_leaf_list *leaf_dst, *leaf_src;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003256
3257 source_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003258 source_match.node = malloc(sizeof *source_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003259 if (!source_match.node) {
3260 LOGMEM;
3261 return -1;
3262 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003263 dest_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003264 dest_match.node = malloc(sizeof *dest_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003265 if (!dest_match.node) {
3266 LOGMEM;
3267 return -1;
3268 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003269
3270 do {
3271 if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
3272 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003273 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003274 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003275 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003276 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003277 parsed_loc += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003278 pred += i;
3279
Michal Vasko23b61ec2015-08-19 11:19:50 +02003280 for (j = 0; j < node_match->count;) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003281 /* source */
Michal Vaskocf024702015-10-08 15:01:42 +02003282 source_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02003283
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003284 /* must be leaf (key of a list) */
Radek Krejci581ce772015-11-10 17:22:40 +01003285 if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j],
Michal Vaskocf024702015-10-08 15:01:42 +02003286 &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003287 i = 0;
3288 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003289 }
3290
3291 /* destination */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003292 dest_match.node[0] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003293 dest_parent_times = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003294 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3295 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003296 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003297 rc = -1;
3298 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003299 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003300 pke_parsed = i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003301 for (i = 0; i < dest_parent_times; ++i) {
Michal Vaskocf024702015-10-08 15:01:42 +02003302 dest_match.node[0] = dest_match.node[0]->parent;
3303 if (!dest_match.node[0]) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003304 i = 0;
Michal Vasko0491ab32015-08-19 14:28:29 +02003305 rc = EXIT_FAILURE;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003306 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003307 }
3308 }
3309 while (1) {
Radek Krejci581ce772015-11-10 17:22:40 +01003310 if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0],
Michal Vasko0491ab32015-08-19 14:28:29 +02003311 &dest_match)) || (dest_match.count != 1)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003312 i = 0;
3313 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003314 }
3315
3316 if (pke_len == pke_parsed) {
3317 break;
3318 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003319 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003320 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003321 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003322 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003323 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003324 }
3325 pke_parsed += i;
3326 }
3327
3328 /* check match between source and destination nodes */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003329 leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0];
3330 while (leaf_dst->value_type == LY_TYPE_LEAFREF) {
3331 leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref;
3332 }
3333 leaf_src = (struct lyd_node_leaf_list *)source_match.node[0];
3334 while (leaf_src->value_type == LY_TYPE_LEAFREF) {
3335 leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref;
3336 }
3337 if (leaf_src->value_type != leaf_dst->value_type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003338 goto remove_leafref;
3339 }
3340
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003341 if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003342 goto remove_leafref;
3343 }
3344
3345 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003346 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003347 continue;
3348
3349remove_leafref:
3350 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003351 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003352 }
3353 } while (has_predicate);
3354
Michal Vaskocf024702015-10-08 15:01:42 +02003355 free(source_match.node);
3356 free(dest_match.node);
Michal Vasko0491ab32015-08-19 14:28:29 +02003357 if (parsed) {
3358 *parsed = parsed_loc;
3359 }
3360 return EXIT_SUCCESS;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003361
3362error:
3363
3364 if (source_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003365 free(source_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003366 }
3367 if (dest_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003368 free(dest_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003369 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003370 if (parsed) {
3371 *parsed = -parsed_loc+i;
3372 }
3373 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003374}
3375
Michal Vasko730dfdf2015-08-11 14:48:05 +02003376/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003377 * @brief Resolve a path (leafref) in JSON data context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003378 *
Michal Vaskocf024702015-10-08 15:01:42 +02003379 * @param[in] node Leafref data node.
Michal Vasko23b61ec2015-08-19 11:19:50 +02003380 * @param[in] path Path of the leafref.
Radek Krejci48464ed2016-03-17 15:44:09 +01003381 * @param[out] ret Matching nodes. Expects an empty, but allocated structure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003382 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003383 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003384 */
Michal Vasko184521f2015-09-24 13:14:26 +02003385static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003386resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003387{
Radek Krejci71b795b2015-08-10 16:20:39 +02003388 struct lyd_node *data = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003389 const char *prefix, *name;
Michal Vasko0491ab32015-08-19 14:28:29 +02003390 int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003391 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003392
Michal Vaskocf024702015-10-08 15:01:42 +02003393 assert(node && path && ret && !ret->count);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003394
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003395 parent_times = 0;
Michal Vaskod9173342015-08-17 14:35:36 +02003396 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003397
3398 /* searching for nodeset */
3399 do {
3400 if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003401 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003402 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003403 goto error;
3404 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003405 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003406 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003407
Michal Vasko23b61ec2015-08-19 11:19:50 +02003408 if (!ret->count) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02003409 if (parent_times != -1) {
3410 ret->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003411 ret->node = calloc(1, sizeof *ret->node);
Michal Vasko253035f2015-12-17 16:58:13 +01003412 if (!ret->node) {
3413 LOGMEM;
Radek Krejci50501732016-01-07 13:06:39 +01003414 rc = -1;
Michal Vasko253035f2015-12-17 16:58:13 +01003415 goto error;
3416 }
Michal Vasko8bcdf292015-08-19 14:04:43 +02003417 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003418 for (i = 0; i < parent_times; ++i) {
3419 /* relative path */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003420 if (!ret->count) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003421 /* error, too many .. */
Radek Krejci48464ed2016-03-17 15:44:09 +01003422 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name);
Michal Vasko0491ab32015-08-19 14:28:29 +02003423 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003424 goto error;
Michal Vaskocf024702015-10-08 15:01:42 +02003425 } else if (!ret->node[0]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003426 /* first .. */
Michal Vaskocf024702015-10-08 15:01:42 +02003427 data = ret->node[0] = node->parent;
3428 } else if (!ret->node[0]->parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003429 /* we are in root */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003430 ret->count = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003431 free(ret->node);
3432 ret->node = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003433 } else {
3434 /* multiple .. */
Michal Vaskocf024702015-10-08 15:01:42 +02003435 data = ret->node[0] = ret->node[0]->parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003436 }
3437 }
3438
3439 /* absolute path */
3440 if (parent_times == -1) {
Michal Vaskocf024702015-10-08 15:01:42 +02003441 for (data = node; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003442 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko8bcdf292015-08-19 14:04:43 +02003443 if (data->prev) {
3444 for (; data->prev->next; data = data->prev);
3445 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003446 }
3447 }
3448
3449 /* node identifier */
Radek Krejci581ce772015-11-10 17:22:40 +01003450 if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003451 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003452 LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name);
Michal Vasko184521f2015-09-24 13:14:26 +02003453 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003454 goto error;
3455 }
3456
3457 if (has_predicate) {
3458 /* we have predicate, so the current results must be lists */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003459 for (j = 0; j < ret->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003460 if (ret->node[j]->schema->nodetype == LYS_LIST &&
3461 ((struct lys_node_list *)ret->node[0]->schema)->keys) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003462 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003463 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003464 continue;
3465 }
3466
3467 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003468 unres_data_del(ret, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003469 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003470 if ((rc = resolve_path_predicate_data(path, node, ret, &i))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003471 if (rc == -1) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003472 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path);
Michal Vasko184521f2015-09-24 13:14:26 +02003473 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003474 goto error;
3475 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003476 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003477 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003478
Michal Vasko23b61ec2015-08-19 11:19:50 +02003479 if (!ret->count) {
Michal Vasko0491ab32015-08-19 14:28:29 +02003480 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003481 goto error;
3482 }
3483 }
3484 } while (path[0] != '\0');
3485
Michal Vaskof02e3742015-08-05 16:27:02 +02003486 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003487
3488error:
3489
Michal Vaskocf024702015-10-08 15:01:42 +02003490 free(ret->node);
3491 ret->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003492 ret->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003493
Michal Vasko0491ab32015-08-19 14:28:29 +02003494 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003495}
3496
Michal Vasko730dfdf2015-08-11 14:48:05 +02003497/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003498 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003499 *
Michal Vaskobb211122015-08-19 14:03:11 +02003500 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003501 * @param[in] context_node Predicate context node (where the predicate is placed).
3502 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003503 *
Michal Vasko184521f2015-09-24 13:14:26 +02003504 * @return 0 on forward reference, otherwise the number
3505 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003506 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003507 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003508static int
Radek Krejciadb57612016-02-16 13:34:34 +01003509resolve_path_predicate_schema(const char *path, const struct lys_node *context_node,
Radek Krejci48464ed2016-03-17 15:44:09 +01003510 struct lys_node *parent)
Michal Vasko1f76a282015-08-04 16:16:53 +02003511{
Michal Vasko1e62a092015-12-01 12:27:20 +01003512 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003513 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
3514 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003515 int has_predicate, dest_parent_times = 0, i, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003516
3517 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003518 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003519 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003520 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003521 return -parsed+i;
3522 }
3523 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003524 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003525
Michal Vasko58090902015-08-13 14:04:15 +02003526 /* source (must be leaf) */
Michal Vasko36cbaa42015-12-14 13:15:48 +01003527 if (!sour_pref) {
Radek Krejciadb57612016-02-16 13:34:34 +01003528 sour_pref = context_node->module->name;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003529 }
Radek Krejciadb57612016-02-16 13:34:34 +01003530 rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003531 LYS_LEAF | LYS_AUGMENT, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003532 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003533 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003534 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003535 }
3536
3537 /* destination */
3538 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3539 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003540 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 +02003541 return -parsed;
3542 }
3543 pke_parsed += i;
3544
Radek Krejciadb57612016-02-16 13:34:34 +01003545 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003546 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003547 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003548 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003549 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02003550 /* path is supposed to be evaluated in data tree, so we have to skip
3551 * all schema nodes that cannot be instantiated in data tree */
3552 for (dst_node = lys_parent(dst_node);
3553 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST));
3554 dst_node = lys_parent(dst_node));
Michal Vasko1f76a282015-08-04 16:16:53 +02003555 }
3556 while (1) {
Michal Vasko36cbaa42015-12-14 13:15:48 +01003557 if (!dest_pref) {
3558 dest_pref = dst_node->module->name;
3559 }
3560 rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003561 LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003562 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003563 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003564 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003565 }
3566
3567 if (pke_len == pke_parsed) {
3568 break;
3569 }
3570
3571 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
3572 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003573 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Radek Krejciadb57612016-02-16 13:34:34 +01003574 (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003575 return -parsed;
3576 }
3577 pke_parsed += i;
3578 }
3579
3580 /* check source - dest match */
Michal Vasko184521f2015-09-24 13:14:26 +02003581 if (dst_node->nodetype != LYS_LEAF) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003582 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Radek Krejci48464ed2016-03-17 15:44:09 +01003583 LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
3584 "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003585 return -parsed;
3586 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003587 } while (has_predicate);
3588
3589 return parsed;
3590}
3591
Michal Vasko730dfdf2015-08-11 14:48:05 +02003592/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003593 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003594 *
Michal Vaskobb211122015-08-19 14:03:11 +02003595 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003596 * @param[in] parent_node Parent of the leafref.
Radek Krejci2f12f852016-01-08 12:59:57 +01003597 * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path
3598 * has to contain absolute path
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003599 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003600 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003601 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003602 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003603static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003604resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf,
Michal Vasko36cbaa42015-12-14 13:15:48 +01003605 const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003606{
Michal Vasko1e62a092015-12-01 12:27:20 +01003607 const struct lys_node *node;
Radek Krejcic071c542016-01-27 14:57:51 +01003608 const struct lys_module *mod;
Michal Vasko1f76a282015-08-04 16:16:53 +02003609 const char *id, *prefix, *name;
3610 int pref_len, nam_len, parent_times, has_predicate;
Michal Vasko184521f2015-09-24 13:14:26 +02003611 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003612
Michal Vasko184521f2015-09-24 13:14:26 +02003613 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003614 parent_times = 0;
3615 id = path;
3616
3617 do {
3618 if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003619 LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003620 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003621 }
3622 id += i;
3623
Michal Vasko184521f2015-09-24 13:14:26 +02003624 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003625 if (parent_times == -1) {
Radek Krejcic071c542016-01-27 14:57:51 +01003626 /* resolve prefix of the module */
Radek Krejciadb57612016-02-16 13:34:34 +01003627 mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len);
Radek Krejcic071c542016-01-27 14:57:51 +01003628 /* get start node */
3629 node = mod ? mod->data : NULL;
Michal Vasko58090902015-08-13 14:04:15 +02003630 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003631 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3632 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003633 return EXIT_FAILURE;
Michal Vasko58090902015-08-13 14:04:15 +02003634 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003635 } else if (parent_times > 0) {
Radek Krejci2f12f852016-01-08 12:59:57 +01003636 if (parent_tpdf) {
3637 /* the path is not allowed to contain relative path since we are in top level typedef */
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003638 LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path);
Radek Krejci2f12f852016-01-08 12:59:57 +01003639 return -1;
3640 }
3641
Michal Vaskodf053d12016-07-21 13:33:31 +02003642 /* node is the parent already, skip one ".." */
3643 for (i = 1, node = parent; i < parent_times; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02003644 /* path is supposed to be evaluated in data tree, so we have to skip
3645 * all schema nodes that cannot be instantiated in data tree */
3646 for (node = lys_parent(node);
3647 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST));
3648 node = lys_parent(node));
3649
Michal Vasko1f76a282015-08-04 16:16:53 +02003650 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003651 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3652 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003653 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003654 }
Michal Vasko58090902015-08-13 14:04:15 +02003655
Michal Vasko1f76a282015-08-04 16:16:53 +02003656 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003657 } else {
3658 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003659 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003660 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003661
Michal Vasko184521f2015-09-24 13:14:26 +02003662 first_iter = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003663 } else {
Michal Vasko7dc71d02016-03-15 10:42:28 +01003664 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02003665 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko2f5aceb2016-03-22 10:24:14 +01003666 LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, name[0], name);
Michal Vasko7dc71d02016-03-15 10:42:28 +01003667 return -1;
3668 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003669 node = node->child;
3670 }
3671
Michal Vasko4f0dad02016-02-15 14:08:23 +01003672 if (!prefix) {
Radek Krejciadb57612016-02-16 13:34:34 +01003673 prefix = lys_node_module(parent)->name;
Michal Vasko4f0dad02016-02-15 14:08:23 +01003674 }
3675
Michal Vasko36cbaa42015-12-14 13:15:48 +01003676 rc = lys_get_sibling(node, prefix, pref_len, name, nam_len, LYS_ANY & ~(LYS_USES | LYS_GROUPING), &node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003677 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003678 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003679 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003680 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003681
3682 if (has_predicate) {
3683 /* we have predicate, so the current result must be list */
3684 if (node->nodetype != LYS_LIST) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003685 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003686 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003687 }
3688
Radek Krejci48464ed2016-03-17 15:44:09 +01003689 i = resolve_path_predicate_schema(id, node, parent);
Michal Vasko184521f2015-09-24 13:14:26 +02003690 if (!i) {
Michal Vaskof9664da2015-08-24 15:03:30 +02003691 return EXIT_FAILURE;
Michal Vasko184521f2015-09-24 13:14:26 +02003692 } else if (i < 0) {
3693 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003694 }
3695 id += i;
3696 }
3697 } while (id[0]);
3698
Michal Vaskoca917682016-07-25 11:00:37 +02003699 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
3700 if ((node->nodetype != LYS_LEAF) && ((lys_node_module(parent)->version != 2) || (node->nodetype != LYS_LEAFLIST))) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003701 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Radek Krejcid47daf62016-08-22 16:23:38 +02003702 LOGVAL(LYE_SPEC, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3703 "Leafref target \"%s\" is not a leaf%s.", path,
3704 lys_node_module(parent)->version != 2 ? "" : " nor a leaf-list");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003705 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003706 }
3707
Radek Krejcicf509982015-12-15 09:22:44 +01003708 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003709 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003710 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003711 return -1;
3712 }
3713
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003714 if (ret) {
3715 *ret = node;
3716 }
3717 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003718}
3719
Michal Vasko730dfdf2015-08-11 14:48:05 +02003720/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003721 * @brief Resolve instance-identifier predicate in JSON data format.
3722 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003723 *
Michal Vaskobb211122015-08-19 14:03:11 +02003724 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003725 * @param[in,out] node_match Nodes matching the restriction without
3726 * the predicate. Nodes not satisfying
3727 * the predicate are removed.
3728 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003729 * @return Number of characters successfully parsed,
3730 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003731 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003732static int
Michal Vaskof39142b2015-10-21 11:40:05 +02003733resolve_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003734{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003735 /* ... /node[target = value] ... */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003736 struct unres_data target_match;
3737 struct ly_ctx *ctx;
Michal Vasko1e62a092015-12-01 12:27:20 +01003738 const struct lys_module *mod;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003739 const char *model, *name, *value;
3740 char *str;
3741 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed;
3742 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003743
Michal Vasko1f2cc332015-08-19 11:18:32 +02003744 assert(pred && node_match->count);
3745
Michal Vaskocf024702015-10-08 15:01:42 +02003746 ctx = node_match->node[0]->schema->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003747 idx = -1;
3748 parsed = 0;
3749
3750 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02003751 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003752 return -parsed+i;
3753 }
3754 parsed += i;
3755 pred += i;
3756
Michal Vasko1f2cc332015-08-19 11:18:32 +02003757 /* pos */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003758 if (isdigit(name[0])) {
3759 idx = atoi(name);
3760 }
3761
Michal Vasko1f2cc332015-08-19 11:18:32 +02003762 for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003763 /* target */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003764 memset(&target_match, 0, sizeof target_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003765 if ((name[0] == '.') || !value) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003766 target_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003767 target_match.node = malloc(sizeof *target_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003768 if (!target_match.node) {
3769 LOGMEM;
3770 return -1;
3771 }
Michal Vaskocf024702015-10-08 15:01:42 +02003772 target_match.node[0] = node_match->node[j];
Michal Vasko1f2cc332015-08-19 11:18:32 +02003773 } else {
3774 str = strndup(model, mod_len);
3775 mod = ly_ctx_get_module(ctx, str, NULL);
3776 free(str);
3777
Radek Krejci804836a2016-02-03 10:39:55 +01003778 if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003779 goto remove_instid;
3780 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003781 }
3782
3783 /* check that we have the correct type */
3784 if (name[0] == '.') {
Michal Vaskocf024702015-10-08 15:01:42 +02003785 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003786 goto remove_instid;
3787 }
3788 } else if (value) {
Michal Vaskocf024702015-10-08 15:01:42 +02003789 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003790 goto remove_instid;
3791 }
3792 }
3793
Michal Vasko83a6c462015-10-08 16:43:53 +02003794 if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len)
3795 || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len]))
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003796 || (!value && (idx != cur_idx))) {
3797 goto remove_instid;
3798 }
3799
Michal Vaskocf024702015-10-08 15:01:42 +02003800 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003801
3802 /* leafref is ok, continue check with next leafref */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003803 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003804 continue;
3805
3806remove_instid:
Michal Vaskocf024702015-10-08 15:01:42 +02003807 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003808
3809 /* does not fulfill conditions, remove leafref record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003810 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003811 }
3812 } while (has_predicate);
3813
3814 return parsed;
3815}
3816
Michal Vasko730dfdf2015-08-11 14:48:05 +02003817/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003818 * @brief Resolve instance-identifier in JSON data format. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003819 *
Radek Krejciadb57612016-02-16 13:34:34 +01003820 * @param[in] data Data node where the path is used
Michal Vasko730dfdf2015-08-11 14:48:05 +02003821 * @param[in] path Instance-identifier node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003822 *
Radek Krejcic5090c32015-08-12 09:46:19 +02003823 * @return Matching node or NULL if no such a node exists. If error occurs, NULL is returned and ly_errno is set.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003824 */
Michal Vasko184521f2015-09-24 13:14:26 +02003825static struct lyd_node *
Radek Krejci48464ed2016-03-17 15:44:09 +01003826resolve_instid(struct lyd_node *data, const char *path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003827{
Radek Krejcic5090c32015-08-12 09:46:19 +02003828 int i = 0, j;
3829 struct lyd_node *result = NULL;
Michal Vaskobea08e92016-05-18 13:28:08 +02003830 const struct lys_module *mod, *prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003831 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003832 const char *model, *name;
Radek Krejcic5090c32015-08-12 09:46:19 +02003833 char *str;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003834 int mod_len, name_len, has_predicate;
3835 struct unres_data node_match;
3836 uint32_t k;
3837
3838 memset(&node_match, 0, sizeof node_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003839
Radek Krejcic5090c32015-08-12 09:46:19 +02003840 /* we need root to resolve absolute path */
3841 for (; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003842 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko0491ab32015-08-19 14:28:29 +02003843 if (data->prev) {
3844 for (; data->prev->next; data = data->prev);
3845 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003846
Michal Vaskobea08e92016-05-18 13:28:08 +02003847 prev_mod = lyd_node_module(data);
3848
Radek Krejcic5090c32015-08-12 09:46:19 +02003849 /* search for the instance node */
3850 while (path[i]) {
Michal Vaskof39142b2015-10-21 11:40:05 +02003851 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
Radek Krejcic5090c32015-08-12 09:46:19 +02003852 if (j <= 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003853 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003854 goto error;
3855 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003856 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003857
Michal Vaskobea08e92016-05-18 13:28:08 +02003858 if (model) {
3859 str = strndup(model, mod_len);
3860 if (!str) {
3861 LOGMEM;
3862 goto error;
3863 }
3864 mod = ly_ctx_get_module(ctx, str, NULL);
3865 free(str);
3866 } else {
3867 mod = prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003868 }
3869
Michal Vasko1f2cc332015-08-19 11:18:32 +02003870 if (resolve_data(mod, name, name_len, data, &node_match)) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003871 /* no instance exists */
3872 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003873 }
3874
3875 if (has_predicate) {
3876 /* we have predicate, so the current results must be list or leaf-list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003877 for (k = 0; k < node_match.count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003878 if ((node_match.node[k]->schema->nodetype == LYS_LIST &&
3879 ((struct lys_node_list *)node_match.node[k]->schema)->keys)
3880 || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003881 /* instid is ok, continue check with next instid */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003882 ++k;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003883 continue;
3884 }
3885
3886 /* does not fulfill conditions, remove inst record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003887 unres_data_del(&node_match, k);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003888 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003889
Michal Vaskof39142b2015-10-21 11:40:05 +02003890 j = resolve_predicate(&path[i], &node_match);
Radek Krejcic5090c32015-08-12 09:46:19 +02003891 if (j < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003892 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003893 goto error;
3894 }
Michal Vasko1f2cc332015-08-19 11:18:32 +02003895 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003896
Michal Vasko1f2cc332015-08-19 11:18:32 +02003897 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003898 /* no instance exists */
3899 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003900 }
3901 }
Michal Vaskobea08e92016-05-18 13:28:08 +02003902
3903 prev_mod = mod;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003904 }
3905
Michal Vasko1f2cc332015-08-19 11:18:32 +02003906 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003907 /* no instance exists */
3908 return NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003909 } else if (node_match.count > 1) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003910 /* instance identifier must resolve to a single node */
Radek Krejci48464ed2016-03-17 15:44:09 +01003911 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Radek Krejcic5090c32015-08-12 09:46:19 +02003912
Michal Vaskod6adbaa2016-04-11 11:01:09 +02003913 goto error;
Radek Krejcic5090c32015-08-12 09:46:19 +02003914 } else {
3915 /* we have required result, remember it and cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003916 result = node_match.node[0];
3917 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003918
3919 return result;
3920 }
3921
3922error:
3923
3924 /* cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003925 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003926
3927 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003928}
3929
Michal Vasko730dfdf2015-08-11 14:48:05 +02003930/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003931 * @brief Passes config flag down to children, skips nodes without config flags.
3932 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003933 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003934 * @param[in] node Siblings and their children to have flags changed.
Pavol Vican47319932016-08-29 09:14:47 +02003935 * @param[in] ignore Flag to ignore check if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC.
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003936 * @param[in] flags Flags to assign to all the nodes.
Michal Vaskoa86508c2016-08-26 14:30:19 +02003937 *
3938 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003939 */
Michal Vaskoa86508c2016-08-26 14:30:19 +02003940static int
Pavol Vican47319932016-08-29 09:14:47 +02003941inherit_config_flag(struct lys_node *node, int ignore, int flags)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003942{
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003943 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02003944 LY_TREE_FOR(node, node) {
Pavol Vican47319932016-08-29 09:14:47 +02003945 if (!ignore && (node->flags & LYS_CONFIG_SET)) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003946 /* skip nodes with an explicit config value */
Michal Vaskoa86508c2016-08-26 14:30:19 +02003947 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3948 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
3949 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children.");
3950 return -1;
3951 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003952 continue;
3953 }
3954 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
Radek Krejci43f548d2016-06-16 11:06:38 +02003955 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003956 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02003957 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Pavol Vican47319932016-08-29 09:14:47 +02003958 if (inherit_config_flag(node->child, ignore, flags)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02003959 return -1;
3960 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02003961 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003962 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02003963
3964 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003965}
3966
Michal Vasko730dfdf2015-08-11 14:48:05 +02003967/**
Michal Vasko7178e692016-02-12 15:58:05 +01003968 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003969 *
Michal Vaskobb211122015-08-19 14:03:11 +02003970 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01003971 * @param[in] siblings Nodes where to start the search in. If set, uses augment, if not, standalone augment.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003972 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003973 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003974 */
Michal Vasko7178e692016-02-12 15:58:05 +01003975static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003976resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003977{
Pavol Vican47319932016-08-29 09:14:47 +02003978 int rc, ignore_config;
Michal Vasko1d87a922015-08-21 12:57:16 +02003979 struct lys_node *sub;
Pavol Vican47319932016-08-29 09:14:47 +02003980 const struct lys_node *aug_target, *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003981
Michal Vasko15b36692016-08-26 15:29:54 +02003982 assert(aug && !aug->target);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003983
Michal Vasko15b36692016-08-26 15:29:54 +02003984 /* resolve target node */
3985 rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), &aug_target);
3986 if (rc == -1) {
3987 return -1;
3988 }
3989 if (rc > 0) {
3990 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
3991 return -1;
3992 }
3993 if (!aug_target) {
3994 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
3995 return EXIT_FAILURE;
3996 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003997
Michal Vasko15b36692016-08-26 15:29:54 +02003998 if (!aug->child) {
3999 /* nothing to do */
4000 LOGWRN("Augment \"%s\" without children.", aug->target_name);
4001 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004002 }
4003
Michal Vaskod58d5962016-03-02 14:29:41 +01004004 /* check for mandatory nodes - if the target node is in another module
4005 * the added nodes cannot be mandatory
4006 */
Michal Vasko15b36692016-08-26 15:29:54 +02004007 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target))
Radek Krejcie00d2312016-08-12 15:27:49 +02004008 && (rc = lyp_check_mandatory_augment(aug))) {
4009 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004010 }
4011
Michal Vasko07e89ef2016-03-03 13:28:57 +01004012 /* check augment target type and then augment nodes type */
Michal Vasko15b36692016-08-26 15:29:54 +02004013 if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004014 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004015 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004016 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4017 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko15b36692016-08-26 15:29:54 +02004018 strnodetype(aug_target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004019 return -1;
4020 }
4021 }
Michal Vasko15b36692016-08-26 15:29:54 +02004022 } else if (aug_target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004023 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004024 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004025 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4026 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko15b36692016-08-26 15:29:54 +02004027 strnodetype(aug_target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004028 return -1;
4029 }
4030 }
4031 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004032 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
Michal Vasko15b36692016-08-26 15:29:54 +02004033 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug_target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004034 return -1;
4035 }
4036
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004037 /* inherit config information from actual parent */
Pavol Vican47319932016-08-29 09:14:47 +02004038 for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = parent->parent);
4039 ignore_config = (parent) ? 1 : 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004040 LY_TREE_FOR(aug->child, sub) {
Pavol Vican47319932016-08-29 09:14:47 +02004041 if (inherit_config_flag(sub, ignore_config, aug_target->flags & LYS_CONFIG_MASK)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004042 return -1;
4043 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004044 }
4045
Radek Krejcic071c542016-01-27 14:57:51 +01004046 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004047 LY_TREE_FOR(aug->child, sub) {
Michal Vasko15b36692016-08-26 15:29:54 +02004048 if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004049 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004050 }
4051 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004052
Michal Vasko15b36692016-08-26 15:29:54 +02004053 /* finally reconnect augmenting data into the target - add them to the target child list,
4054 * by setting aug->target we know the augment is fully resolved now */
4055 aug->target = (struct lys_node *)aug_target;
4056 if (aug->target->child) {
4057 sub = aug->target->child->prev; /* remember current target's last node */
4058 sub->next = aug->child; /* connect augmenting data after target's last node */
4059 aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */
4060 aug->child->prev = sub; /* finish connecting of both child lists */
4061 } else {
4062 aug->target->child = aug->child;
4063 }
4064
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004065 return EXIT_SUCCESS;
4066}
4067
Michal Vasko730dfdf2015-08-11 14:48:05 +02004068/**
4069 * @brief Resolve uses, apply augments, refines. Logs directly.
4070 *
Michal Vaskobb211122015-08-19 14:03:11 +02004071 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004072 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004073 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004074 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004075 */
Michal Vasko184521f2015-09-24 13:14:26 +02004076static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004077resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004078{
4079 struct ly_ctx *ctx;
Radek Krejci989790c2016-04-14 17:49:41 +02004080 struct lys_node *node = NULL, *next, *iter;
Pavol Vican55abd332016-07-12 15:54:49 +02004081 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci76512572015-08-04 09:47:08 +02004082 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004083 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004084 struct lys_iffeature *iff, **old_iff;
Pavol Vican47319932016-08-29 09:14:47 +02004085 int i, j, rc, parent_config, ignore_config;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004086 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004087 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004088
Michal Vasko71e1aa82015-08-12 12:17:51 +02004089 assert(uses->grp);
Michal Vaskoe7708512016-03-11 10:26:55 +01004090 /* HACK just check that the grouping is resolved */
Michal Vaskodef0db12015-10-07 13:22:48 +02004091 assert(!uses->grp->nacm);
Michal Vasko71e1aa82015-08-12 12:17:51 +02004092
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004093 if (!uses->grp->child) {
4094 /* grouping without children, warning was already displayed */
4095 return EXIT_SUCCESS;
4096 }
4097
4098 /* get proper parent (config) flags */
4099 for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux));
4100 if (node_aux) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004101 parent_config = node_aux->flags & LYS_CONFIG_MASK;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004102 } else {
4103 /* default */
Michal Vaskoa86508c2016-08-26 14:30:19 +02004104 parent_config = LYS_CONFIG_W;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004105 }
4106
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004107 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004108 LY_TREE_FOR(uses->grp->child, node_aux) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004109 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, 0, uses->nacm, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004110 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004111 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
4112 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004113 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004114 }
Pavol Vican55abd332016-07-12 15:54:49 +02004115 /* test the name of siblings */
4116 LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02004117 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004118 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004119 }
4120 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004121 }
4122 ctx = uses->module->ctx;
4123
Michal Vaskoa86508c2016-08-26 14:30:19 +02004124 if (parent_config) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004125 assert(uses->child);
Pavol Vican47319932016-08-29 09:14:47 +02004126 for(parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = parent->parent);
4127 ignore_config = (parent) ? 1 : 0;
4128 if (inherit_config_flag(uses->child, ignore_config, parent_config)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004129 goto fail;
4130 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004131 }
4132
Michal Vaskodef0db12015-10-07 13:22:48 +02004133 /* we managed to copy the grouping, the rest must be possible to resolve */
4134
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004135 /* apply refines */
4136 for (i = 0; i < uses->refine_size; i++) {
4137 rfn = &uses->refine[i];
Michal Vasko3edeaf72016-02-11 13:17:43 +01004138 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE,
Radek Krejcif3c71de2016-04-11 12:45:46 +02004139 1, 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004140 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004141 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004142 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004143 }
4144
Radek Krejci1d82ef62015-08-07 14:44:40 +02004145 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004146 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
4147 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004148 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004149 }
4150
4151 /* description on any nodetype */
4152 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004153 lydict_remove(ctx, node->dsc);
4154 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004155 }
4156
4157 /* reference on any nodetype */
4158 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004159 lydict_remove(ctx, node->ref);
4160 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004161 }
4162
4163 /* config on any nodetype */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004164 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejcia881e3e2016-06-23 17:02:39 +02004165 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
4166 if (parent && parent->nodetype != LYS_GROUPING &&
4167 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
Radek Krejci989790c2016-04-14 17:49:41 +02004168 (rfn->flags & LYS_CONFIG_W)) {
4169 /* setting config true under config false is prohibited */
4170 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4171 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4172 "changing config from 'false' to 'true' is prohibited while "
4173 "the target's parent is still config 'false'.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004174 goto fail;
Radek Krejci989790c2016-04-14 17:49:41 +02004175 }
4176
Radek Krejci1d82ef62015-08-07 14:44:40 +02004177 node->flags &= ~LYS_CONFIG_MASK;
4178 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Radek Krejci989790c2016-04-14 17:49:41 +02004179
4180 /* inherit config change to the target children */
4181 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4182 if (rfn->flags & LYS_CONFIG_W) {
4183 if (iter->flags & LYS_CONFIG_SET) {
4184 /* config is set explicitely, go to next sibling */
4185 next = NULL;
4186 goto nextsibling;
4187 }
4188 } else { /* LYS_CONFIG_R */
4189 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4190 /* error - we would have config data under status data */
4191 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4192 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4193 "changing config from 'true' to 'false' is prohibited while the target "
4194 "has still a children with explicit config 'true'.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004195 goto fail;
Radek Krejci989790c2016-04-14 17:49:41 +02004196 }
4197 }
4198 /* change config */
4199 iter->flags &= ~LYS_CONFIG_MASK;
4200 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4201
4202 /* select next iter - modified LY_TREE_DFS_END */
Radek Krejcibf2abff2016-08-23 15:51:52 +02004203 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejci989790c2016-04-14 17:49:41 +02004204 next = NULL;
4205 } else {
4206 next = iter->child;
4207 }
4208nextsibling:
4209 if (!next) {
4210 /* no children */
4211 if (iter == node->child) {
4212 /* we are done, (START) has no children */
4213 break;
4214 }
4215 /* try siblings */
4216 next = iter->next;
4217 }
4218 while (!next) {
4219 /* parent is already processed, go to its sibling */
4220 iter = lys_parent(iter);
4221
4222 /* no siblings, go back through parents */
4223 if (iter == node) {
4224 /* we are done, no next element to process */
4225 break;
4226 }
4227 next = iter->next;
4228 }
4229 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004230 }
4231
4232 /* default value ... */
4233 if (rfn->mod.dflt) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004234 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004235 /* leaf */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004236 lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt);
4237 ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0);
4238 } else if (node->nodetype == LYS_CHOICE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004239 /* choice */
Michal Vasko3edeaf72016-02-11 13:17:43 +01004240 rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child,
4241 (const struct lys_node **)&((struct lys_node_choice *)node)->dflt);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004242 if (rc || !((struct lys_node_choice *)node)->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004243 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004244 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004245 }
4246 }
4247 }
4248
4249 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004250 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004251 if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004252 /* remove current value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004253 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004254
4255 /* set new value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004256 node->flags |= (rfn->flags & LYS_MAND_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004257 }
4258 }
4259
4260 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004261 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4262 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4263 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004264 }
4265
4266 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004267 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004268 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004269 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004270 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004271 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004272 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004273 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004274 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004275 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004276 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004277 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004278 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004279 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004280 }
4281 }
4282
4283 /* must in leaf, leaf-list, list, container or anyxml */
4284 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004285 switch (node->nodetype) {
4286 case LYS_LEAF:
4287 old_size = &((struct lys_node_leaf *)node)->must_size;
4288 old_must = &((struct lys_node_leaf *)node)->must;
4289 break;
4290 case LYS_LEAFLIST:
4291 old_size = &((struct lys_node_leaflist *)node)->must_size;
4292 old_must = &((struct lys_node_leaflist *)node)->must;
4293 break;
4294 case LYS_LIST:
4295 old_size = &((struct lys_node_list *)node)->must_size;
4296 old_must = &((struct lys_node_list *)node)->must;
4297 break;
4298 case LYS_CONTAINER:
4299 old_size = &((struct lys_node_container *)node)->must_size;
4300 old_must = &((struct lys_node_container *)node)->must;
4301 break;
4302 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004303 case LYS_ANYDATA:
4304 old_size = &((struct lys_node_anydata *)node)->must_size;
4305 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004306 break;
4307 default:
4308 LOGINT;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004309 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004310 }
4311
4312 size = *old_size + rfn->must_size;
4313 must = realloc(*old_must, size * sizeof *rfn->must);
4314 if (!must) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004315 LOGMEM;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004316 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004317 }
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004318 for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) {
4319 must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0);
4320 must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0);
4321 must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0);
4322 must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0);
4323 must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004324 }
4325
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004326 *old_must = must;
4327 *old_size = size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004328 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02004329
4330 /* if-feature in leaf, leaf-list, list, container or anyxml */
4331 if (rfn->iffeature_size) {
4332 old_size = &node->iffeature_size;
4333 old_iff = &node->iffeature;
4334
4335 size = *old_size + rfn->iffeature_size;
4336 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
4337 if (!iff) {
4338 LOGMEM;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004339 goto fail;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004340 }
4341 for (i = 0, j = *old_size; i < rfn->iffeature_size; i++, j++) {
4342 resolve_iffeature_getsizes(&rfn->iffeature[i], &usize1, &usize2);
4343 if (usize1) {
4344 /* there is something to duplicate */
4345 /* duplicate compiled expression */
4346 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
4347 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
4348 memcpy(iff[j].expr, rfn->iffeature[i].expr, usize * sizeof *iff[j].expr);
4349
4350 /* duplicate list of feature pointers */
4351 iff[j].features = malloc(usize2 * sizeof *iff[i].features);
4352 memcpy(iff[j].features, rfn->iffeature[i].features, usize2 * sizeof *iff[j].features);
4353 }
4354 }
4355
4356 *old_iff = iff;
4357 *old_size = size;
4358 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004359 }
4360
4361 /* apply augments */
4362 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004363 rc = resolve_augment(&uses->augment[i], uses->child);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004364 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004365 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004366 }
4367 }
4368
4369 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004370
4371fail:
4372 LY_TREE_FOR_SAFE(uses->child, next, iter) {
4373 lys_node_free(iter, NULL, 0);
4374 }
4375 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004376}
4377
Radek Krejci018f1f52016-08-03 16:01:20 +02004378static int
4379identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
4380{
4381 int i;
4382
4383 assert(der && base);
4384
4385 base->der = ly_realloc(base->der, (base->der_size + 1) * sizeof *(base->der));
4386 if (!base->der) {
4387 LOGMEM;
4388 return EXIT_FAILURE;
4389 }
4390 base->der[base->der_size++] = der;
4391
4392 for (i = 0; i < base->base_size; i++) {
4393 if (identity_backlink_update(der, base->base[i])) {
4394 return EXIT_FAILURE;
4395 }
4396 }
4397
4398 return EXIT_SUCCESS;
4399}
4400
Michal Vasko730dfdf2015-08-11 14:48:05 +02004401/**
4402 * @brief Resolve base identity recursively. Does not log.
4403 *
4404 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004405 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004406 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004407 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004408 *
Radek Krejci219fa612016-08-15 10:36:51 +02004409 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004410 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004411static int
Michal Vasko1e62a092015-12-01 12:27:20 +01004412resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02004413 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004414{
Michal Vaskof02e3742015-08-05 16:27:02 +02004415 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02004416 struct lys_ident *base = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004417
Radek Krejcicf509982015-12-15 09:22:44 +01004418 assert(ret);
4419
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004420 /* search module */
4421 for (i = 0; i < module->ident_size; i++) {
4422 if (!strcmp(basename, module->ident[i].name)) {
4423
4424 if (!ident) {
4425 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004426 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01004427 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004428 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004429 }
4430
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004431 base = &module->ident[i];
4432 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004433 }
4434 }
4435
4436 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004437 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
4438 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
4439 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004440
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004441 if (!ident) {
4442 *ret = &module->inc[j].submodule->ident[i];
4443 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004444 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004445
4446 base = &module->inc[j].submodule->ident[i];
4447 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004448 }
4449 }
4450 }
4451
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004452matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004453 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01004454 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02004455 /* is it already completely resolved? */
4456 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02004457 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02004458 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
4459
4460 /* simple check for circular reference,
4461 * the complete check is done as a side effect of using only completely
4462 * resolved identities (previous check of unres content) */
4463 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
4464 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
4465 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02004466 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02004467 }
4468
Radek Krejci06f64ed2016-08-15 11:07:44 +02004469 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01004470 }
4471 }
Radek Krejci018f1f52016-08-03 16:01:20 +02004472
Radek Krejcibabbff82016-02-19 13:31:37 +01004473 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02004474 ident->base[ident->base_size++] = base;
4475 *ret = base;
Radek Krejcibabbff82016-02-19 13:31:37 +01004476
Radek Krejci018f1f52016-08-03 16:01:20 +02004477 /* maintain backlinks to the derived identities */
Radek Krejci219fa612016-08-15 10:36:51 +02004478 return identity_backlink_update(ident, base) ? -1 : EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004479
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004480 }
4481
Radek Krejci219fa612016-08-15 10:36:51 +02004482 /* base not found (maybe a forward reference) */
4483 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004484}
4485
Michal Vasko730dfdf2015-08-11 14:48:05 +02004486/**
4487 * @brief Resolve base identity. Logs directly.
4488 *
4489 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004490 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004491 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01004492 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01004493 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004494 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004495 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004496 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004497static int
Michal Vasko1e62a092015-12-01 12:27:20 +01004498resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02004499 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004500{
4501 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02004502 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01004503 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02004504 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01004505 struct lys_module *mod;
4506
4507 assert((ident && !type) || (!ident && type));
4508
4509 if (!type) {
4510 /* have ident to resolve */
4511 ret = &target;
4512 flags = ident->flags;
4513 mod = ident->module;
4514 } else {
4515 /* have type to fill */
4516 ret = &type->info.ident.ref;
4517 flags = type->parent->flags;
4518 mod = type->parent->module;
4519 }
Michal Vaskof2006002016-04-21 16:28:15 +02004520 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004521
4522 /* search for the base identity */
4523 name = strchr(basename, ':');
4524 if (name) {
4525 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02004526 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004527 name++;
4528
Michal Vasko2d851a92015-10-20 16:16:36 +02004529 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004530 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02004531 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004532 }
4533 } else {
4534 name = basename;
4535 }
4536
Radek Krejcic071c542016-01-27 14:57:51 +01004537 /* get module where to search */
4538 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
4539 if (!module) {
4540 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01004541 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01004542 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004543 }
4544
Radek Krejcic071c542016-01-27 14:57:51 +01004545 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02004546 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
4547 if (!rc) {
4548 assert(*ret);
4549
4550 /* check status */
4551 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
4552 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
4553 rc = -1;
4554 }
4555 } else if (rc == EXIT_FAILURE) {
4556 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004557 }
4558
Radek Krejci219fa612016-08-15 10:36:51 +02004559 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004560}
4561
Michal Vasko730dfdf2015-08-11 14:48:05 +02004562/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004563 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004564 *
4565 * @param[in] base Base identity.
Michal Vaskofb0873c2015-08-21 09:00:07 +02004566 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01004567 * @param[in] node Node where the identityref is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02004568 *
4569 * @return Pointer to the identity resolvent, NULL on error.
4570 */
Radek Krejcia52656e2015-08-05 13:41:50 +02004571struct lys_ident *
Radek Krejci48464ed2016-03-17 15:44:09 +01004572resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004573{
Michal Vaskoc633ca02015-08-21 14:03:51 +02004574 const char *mod_name, *name;
4575 int mod_name_len, rc;
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004576 int i;
4577 struct lys_ident *der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004578
Michal Vaskofb0873c2015-08-21 09:00:07 +02004579 if (!base || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004580 return NULL;
4581 }
4582
Michal Vaskoc633ca02015-08-21 14:03:51 +02004583 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01004584 if (rc < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004585 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02004586 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01004587 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejci02a04992016-03-17 16:06:37 +01004588 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02004589 return NULL;
4590 }
4591
Michal Vaskoc633ca02015-08-21 14:03:51 +02004592 if (!strcmp(base->name, name) && (!mod_name
4593 || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) {
Radek Krejcif1ee2e22016-08-02 16:36:48 +02004594 der = base;
4595 goto match;
Michal Vaskofb0873c2015-08-21 09:00:07 +02004596 }
4597
Radek Krejci018f1f52016-08-03 16:01:20 +02004598 for (i = 0; i < base->der_size; i++) {
4599 der = base->der[i]; /* shortcut */
4600 if (!strcmp(der->name, name) &&
4601 (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) {
4602 /* we have match */
4603 goto match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004604 }
Radek Krejci018f1f52016-08-03 16:01:20 +02004605
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004606 }
4607
Radek Krejci48464ed2016-03-17 15:44:09 +01004608 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004609 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02004610
4611match:
4612 for (i = 0; i < der->iffeature_size; i++) {
4613 if (!resolve_iffeature(&der->iffeature[i])) {
4614 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, der->name, node->schema->name);
4615 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity \"%s\" is disabled by its if-feature condition.",
4616 der->name);
4617 return NULL;
4618 }
4619 }
4620 return der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004621}
4622
Michal Vasko730dfdf2015-08-11 14:48:05 +02004623/**
Michal Vasko7955b362015-09-04 14:18:15 +02004624 * @brief Resolve (find) choice default case. Does not log.
4625 *
4626 * @param[in] choic Choice to use.
4627 * @param[in] dflt Name of the default case.
4628 *
4629 * @return Pointer to the default node or NULL.
4630 */
4631static struct lys_node *
4632resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4633{
4634 struct lys_node *child, *ret;
4635
4636 LY_TREE_FOR(choic->child, child) {
4637 if (child->nodetype == LYS_USES) {
4638 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4639 if (ret) {
4640 return ret;
4641 }
4642 }
4643
Radek Krejcibf2abff2016-08-23 15:51:52 +02004644 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Michal Vasko7955b362015-09-04 14:18:15 +02004645 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
4646 return child;
4647 }
4648 }
4649
4650 return NULL;
4651}
4652
4653/**
Michal Vaskobb211122015-08-19 14:03:11 +02004654 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004655 *
Michal Vaskobb211122015-08-19 14:03:11 +02004656 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004657 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004658 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004659 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004660 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004661static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004662resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004663{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004664 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01004665 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02004666
Radek Krejci010e54b2016-03-15 09:40:34 +01004667 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself
4668 * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping)
4669 * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm
4670 * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the
4671 * LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02004672 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 +02004673
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004674 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01004675 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
4676 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004677 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004678 return -1;
4679 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004680 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004681 return -1;
4682 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004683 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004684 /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field
4685 * (and smaller flags - it uses only a limited set of flags)
4686 */
4687 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004688 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004689 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01004690 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02004691 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004692 }
4693
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004694 if (uses->grp->nacm) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004695 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004696 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004697 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02004698 } else {
4699 /* instantiate grouping only when it is completely resolved */
4700 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004701 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004702 return EXIT_FAILURE;
4703 }
4704
Radek Krejci48464ed2016-03-17 15:44:09 +01004705 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004706 if (!rc) {
4707 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01004708 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004709 if (!((struct lys_node_grp *)par_grp)->nacm) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004710 LOGINT;
4711 return -1;
4712 }
Radek Krejci4372b4e2016-04-14 17:42:16 +02004713 ((struct lys_node_grp *)par_grp)->nacm--;
Radek Krejci010e54b2016-03-15 09:40:34 +01004714 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004715 }
Radek Krejcicf509982015-12-15 09:22:44 +01004716
4717 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004718 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01004719 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004720 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01004721 return -1;
4722 }
4723
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004724 return EXIT_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01004725 } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004726 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004727 uses->flags |= LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004728 }
4729
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004730 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004731}
4732
Michal Vasko730dfdf2015-08-11 14:48:05 +02004733/**
Michal Vasko9957e592015-08-17 15:04:09 +02004734 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004735 *
Michal Vaskobb211122015-08-19 14:03:11 +02004736 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004737 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004738 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004739 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004740 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004741static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004742resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004743{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004744 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01004745 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004746
4747 for (i = 0; i < list->keys_size; ++i) {
4748 /* get the key name */
4749 if ((value = strpbrk(keys_str, " \t\n"))) {
4750 len = value - keys_str;
4751 while (isspace(value[0])) {
4752 value++;
4753 }
4754 } else {
4755 len = strlen(keys_str);
4756 }
4757
Radek Krejcic4283442016-04-22 09:19:27 +02004758 rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004759 if (rc) {
Michal Vasko7a55bea2016-05-02 14:51:20 +02004760 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
4761 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004762 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004763
Radek Krejci48464ed2016-03-17 15:44:09 +01004764 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004765 /* check_key logs */
4766 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004767 }
4768
Radek Krejcicf509982015-12-15 09:22:44 +01004769 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004770 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004771 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
4772 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01004773 return -1;
4774 }
4775
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004776 /* prepare for next iteration */
4777 while (value && isspace(value[0])) {
4778 value++;
4779 }
4780 keys_str = value;
4781 }
4782
Michal Vaskof02e3742015-08-05 16:27:02 +02004783 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004784}
4785
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004786/**
Michal Vaskobf19d252015-10-08 15:39:17 +02004787 * @brief Resolve (check) all must conditions of \p node.
4788 * Logs directly.
4789 *
4790 * @param[in] node Data node with optional must statements.
Michal Vaskobf19d252015-10-08 15:39:17 +02004791 *
4792 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
4793 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004794static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004795resolve_must(struct lyd_node *node)
Michal Vaskof02e3742015-08-05 16:27:02 +02004796{
Michal Vaskobf19d252015-10-08 15:39:17 +02004797 uint8_t i, must_size;
4798 struct lys_restr *must;
4799 struct lyxp_set set;
4800
4801 assert(node);
4802 memset(&set, 0, sizeof set);
4803
4804 switch (node->schema->nodetype) {
4805 case LYS_CONTAINER:
4806 must_size = ((struct lys_node_container *)node->schema)->must_size;
4807 must = ((struct lys_node_container *)node->schema)->must;
4808 break;
4809 case LYS_LEAF:
4810 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
4811 must = ((struct lys_node_leaf *)node->schema)->must;
4812 break;
4813 case LYS_LEAFLIST:
4814 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
4815 must = ((struct lys_node_leaflist *)node->schema)->must;
4816 break;
4817 case LYS_LIST:
4818 must_size = ((struct lys_node_list *)node->schema)->must_size;
4819 must = ((struct lys_node_list *)node->schema)->must;
4820 break;
4821 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004822 case LYS_ANYDATA:
4823 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
4824 must = ((struct lys_node_anydata *)node->schema)->must;
Michal Vaskobf19d252015-10-08 15:39:17 +02004825 break;
4826 default:
4827 must_size = 0;
4828 break;
4829 }
4830
4831 for (i = 0; i < must_size; ++i) {
Michal Vaskoa59495d2016-08-22 09:18:58 +02004832 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004833 return -1;
4834 }
4835
Michal Vasko944a5642016-03-21 11:48:58 +01004836 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02004837
Michal Vasko8146d4c2016-05-09 15:50:29 +02004838 if (!set.val.bool) {
Michal Vasko6ac68282016-04-11 10:56:47 +02004839 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
4840 if (must[i].emsg) {
4841 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg);
4842 }
4843 if (must[i].eapptag) {
4844 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
4845 }
Michal Vaskobf19d252015-10-08 15:39:17 +02004846 return 1;
4847 }
4848 }
4849
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004850 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02004851}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004852
Michal Vaskobf19d252015-10-08 15:39:17 +02004853/**
Michal Vaskocf024702015-10-08 15:01:42 +02004854 * @brief Resolve (find) when condition context node. Does not log.
4855 *
4856 * @param[in] node Data node, whose conditional definition is being decided.
4857 * @param[in] schema Schema node with a when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02004858 * @param[out] ctx_node Context node.
4859 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02004860 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02004861 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02004862 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02004863static int
4864resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
4865 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004866{
Michal Vaskocf024702015-10-08 15:01:42 +02004867 struct lyd_node *parent;
4868 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02004869 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02004870 uint16_t i, data_depth, schema_depth;
4871
4872 /* find a not schema-only node */
Michal Vaskoa59495d2016-08-22 09:18:58 +02004873 node_type = LYXP_NODE_ELEM;
Michal Vaskocf024702015-10-08 15:01:42 +02004874 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
Michal Vaskoe3655562016-08-24 15:56:17 +02004875 if (schema->nodetype == LYS_AUGMENT) {
4876 sparent = ((struct lys_node_augment *)schema)->target;
4877 } else {
4878 sparent = schema->parent;
4879 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02004880 if (!sparent) {
4881 /* context node is the document root (fake root in our case) */
4882 if (schema->flags & LYS_CONFIG_W) {
4883 node_type = LYXP_NODE_ROOT_CONFIG;
4884 } else {
4885 node_type = LYXP_NODE_ROOT_STATE;
4886 }
4887 break;
Michal Vaskocf024702015-10-08 15:01:42 +02004888 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02004889 schema = sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02004890 }
4891
4892 /* get node depths */
4893 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
Michal Vaskoe3655562016-08-24 15:56:17 +02004894 for (sparent = schema, schema_depth = 0;
4895 sparent;
4896 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004897 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
Michal Vaskocf024702015-10-08 15:01:42 +02004898 ++schema_depth;
4899 }
4900 }
4901 if (data_depth < schema_depth) {
Michal Vaskoa59495d2016-08-22 09:18:58 +02004902 return -1;
Michal Vaskocf024702015-10-08 15:01:42 +02004903 }
4904
Michal Vasko956e8542016-08-26 09:43:35 +02004905 if (schema_depth) {
4906 /* find the corresponding data node */
4907 for (i = 0; i < data_depth - schema_depth; ++i) {
4908 node = node->parent;
4909 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02004910 if (node->schema != schema) {
4911 return -1;
4912 }
4913 } else {
Michal Vasko956e8542016-08-26 09:43:35 +02004914 /* special fake document root, move it to the beginning of the list, if any */
Michal Vasko61db7572016-08-24 15:57:35 +02004915 while (node && node->prev->next) {
Michal Vaskoa59495d2016-08-22 09:18:58 +02004916 node = node->prev;
4917 }
Michal Vaskocf024702015-10-08 15:01:42 +02004918 }
4919
Michal Vaskoa59495d2016-08-22 09:18:58 +02004920 *ctx_node = node;
4921 *ctx_node_type = node_type;
4922 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02004923}
4924
Michal Vasko76c3bd32016-08-24 16:02:52 +02004925/**
4926 * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation.
4927 * The context nodes is adjusted if needed.
4928 *
4929 * @param[in] snode Schema node, whose children instances need to be unlinked.
4930 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
4931 * it is moved to point to another sibling still in the original tree.
4932 * @param[in,out] ctx_node When context node, adjusted if needed.
4933 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
4934 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
4935 * Ordering may change, but there will be no semantic change.
4936 *
4937 * @return EXIT_SUCCESS on success, -1 on error.
4938 */
4939static int
4940resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
4941 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
4942{
4943 struct lyd_node *next, *elem;
4944
4945 switch (snode->nodetype) {
4946 case LYS_AUGMENT:
4947 case LYS_USES:
4948 case LYS_CHOICE:
4949 case LYS_CASE:
4950 LY_TREE_FOR(snode->child, snode) {
4951 if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) {
4952 return -1;
4953 }
4954 }
4955 break;
4956 case LYS_CONTAINER:
4957 case LYS_LIST:
4958 case LYS_LEAF:
4959 case LYS_LEAFLIST:
4960 case LYS_ANYXML:
4961 case LYS_ANYDATA:
4962 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
4963 if (elem->schema == snode) {
4964
4965 if (elem == *ctx_node) {
4966 /* We are going to unlink our context node! This normally cannot happen,
4967 * but we use normal top-level data nodes for faking a document root node,
4968 * so if this is the context node, we just use the next top-level node.
4969 * Additionally, it can even happen that there are no top-level data nodes left,
4970 * all were unlinked, so in this case we pass NULL as the context node/data tree,
4971 * lyxp_eval() can handle this special situation.
4972 */
4973 if (ctx_node_type == LYXP_NODE_ELEM) {
4974 LOGINT;
4975 return -1;
4976 }
4977
4978 if (elem->prev == elem) {
4979 /* unlinking last top-level element, use an empty data tree */
4980 *ctx_node = NULL;
4981 } else {
4982 /* in this case just use the previous/last top-level data node */
4983 *ctx_node = elem->prev;
4984 }
4985 } else if (elem == *node) {
4986 /* We are going to unlink the currently processed node. This does not matter that
4987 * much, but we would lose access to the original data tree, so just move our
4988 * pointer somewhere still inside it.
4989 */
4990 if ((*node)->prev != *node) {
4991 *node = (*node)->prev;
4992 } else {
4993 /* the processed node with sibings were all unlinked, oh well */
4994 *node = NULL;
4995 }
4996 }
4997
4998 /* temporarily unlink the node */
4999 lyd_unlink(elem);
5000 if (*unlinked_nodes) {
5001 if (lyd_insert_after(*unlinked_nodes, elem)) {
5002 LOGINT;
5003 return -1;
5004 }
5005 } else {
5006 *unlinked_nodes = elem;
5007 }
5008
5009 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
5010 /* there can be only one instance */
5011 break;
5012 }
5013 }
5014 }
5015 break;
5016 default:
5017 LOGINT;
5018 return -1;
5019 }
5020
5021 return EXIT_SUCCESS;
5022}
5023
5024/**
5025 * @brief Relink the unlinked nodes back.
5026 *
5027 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
5028 * we simply need a sibling from the original data tree.
5029 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
5030 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
5031 * or the sibling of \p unlinked_nodes.
5032 *
5033 * @return EXIT_SUCCESS on success, -1 on error.
5034 */
5035static int
5036resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
5037{
5038 struct lyd_node *elem;
5039
5040 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
5041 if (ctx_node_type == LYXP_NODE_ELEM) {
5042 if (lyd_insert(node, elem)) {
5043 return -1;
5044 }
5045 } else {
5046 if (lyd_insert_after(node, elem)) {
5047 return -1;
5048 }
5049 }
5050 }
5051
5052 return EXIT_SUCCESS;
5053}
5054
Radek Krejci03b71f72016-03-16 11:10:09 +01005055int
Radek Krejci46165822016-08-26 14:06:27 +02005056resolve_applies_must(const struct lys_node *schema)
Radek Krejci01696bf2016-03-18 13:19:36 +01005057{
Radek Krejci46165822016-08-26 14:06:27 +02005058 assert(schema);
5059
5060 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01005061 case LYS_CONTAINER:
Radek Krejci46165822016-08-26 14:06:27 +02005062 return ((struct lys_node_container *)schema)->must_size;
Radek Krejci01696bf2016-03-18 13:19:36 +01005063 case LYS_LEAF:
Radek Krejci46165822016-08-26 14:06:27 +02005064 return ((struct lys_node_leaf *)schema)->must_size;
Radek Krejci01696bf2016-03-18 13:19:36 +01005065 case LYS_LEAFLIST:
Radek Krejci46165822016-08-26 14:06:27 +02005066 return ((struct lys_node_leaflist *)schema)->must_size;
Radek Krejci01696bf2016-03-18 13:19:36 +01005067 case LYS_LIST:
Radek Krejci46165822016-08-26 14:06:27 +02005068 return ((struct lys_node_list *)schema)->must_size;
Radek Krejci01696bf2016-03-18 13:19:36 +01005069 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005070 case LYS_ANYDATA:
Radek Krejci46165822016-08-26 14:06:27 +02005071 return ((struct lys_node_anydata *)schema)->must_size;
Radek Krejci01696bf2016-03-18 13:19:36 +01005072 default:
5073 return 0;
5074 }
5075}
5076
5077int
Radek Krejci46165822016-08-26 14:06:27 +02005078resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01005079{
Radek Krejci46165822016-08-26 14:06:27 +02005080 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01005081
Radek Krejci46165822016-08-26 14:06:27 +02005082 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01005083
Radek Krejci46165822016-08-26 14:06:27 +02005084 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005085 return 1;
5086 }
5087
Radek Krejci46165822016-08-26 14:06:27 +02005088 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01005089 goto check_augment;
5090
Radek Krejci46165822016-08-26 14:06:27 +02005091 while (parent) {
5092 /* stop conditions */
5093 if (!mode) {
5094 /* stop on node that can be instantiated in data tree */
5095 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
5096 break;
5097 }
5098 } else {
5099 /* stop on the specified node */
5100 if (parent == stop) {
5101 break;
5102 }
5103 }
5104
5105 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005106 return 1;
5107 }
5108check_augment:
5109
5110 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02005111 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02005112 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01005113 }
5114 parent = lys_parent(parent);
5115 }
5116
5117 return 0;
5118}
5119
Michal Vaskocf024702015-10-08 15:01:42 +02005120/**
5121 * @brief Resolve (check) all when conditions relevant for \p node.
5122 * Logs directly.
5123 *
5124 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02005125 *
Radek Krejci03b71f72016-03-16 11:10:09 +01005126 * @return
5127 * -1 - error, ly_errno is set
5128 * 0 - true "when" statement
Radek Krejci46165822016-08-26 14:06:27 +02005129 * 0, ly_vecode = LYVE_NOWHEN - false "when" statement
Radek Krejci03b71f72016-03-16 11:10:09 +01005130 * 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 +02005131 */
Radek Krejci46165822016-08-26 14:06:27 +02005132int
5133resolve_when(struct lyd_node *node, int *result)
Michal Vaskocf024702015-10-08 15:01:42 +02005134{
Michal Vasko76c3bd32016-08-24 16:02:52 +02005135 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02005136 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02005137 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005138 enum lyxp_node_type ctx_node_type;
Radek Krejci51093642016-03-29 10:14:59 +02005139 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02005140
5141 assert(node);
5142 memset(&set, 0, sizeof set);
5143
5144 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005145 /* make the node dummy for the evaluation */
5146 node->validity |= LYD_VAL_INUSE;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005147 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005148 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01005149 if (rc) {
5150 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005151 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005152 }
Radek Krejci51093642016-03-29 10:14:59 +02005153 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005154 }
5155
Radek Krejci03b71f72016-03-16 11:10:09 +01005156 /* set boolean result of the condition */
Michal Vasko944a5642016-03-21 11:48:58 +01005157 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005158 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005159 ly_vlog_hide(0);
Michal Vasko90fc2a32016-08-24 15:58:58 +02005160 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
5161 ly_vlog_hide(1);
Radek Krejci0b7704f2016-03-18 12:16:14 +01005162 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02005163 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005164 }
Radek Krejci51093642016-03-29 10:14:59 +02005165
5166 /* free xpath set content */
5167 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005168 }
5169
Michal Vasko90fc2a32016-08-24 15:58:58 +02005170 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02005171 goto check_augment;
5172
5173 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02005174 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
5175 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02005176 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005177 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02005178 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02005179 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02005180 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005181 }
5182 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02005183
5184 unlinked_nodes = NULL;
5185 /* we do not want our node pointer to change */
5186 tmp_node = node;
5187 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
5188 if (rc) {
5189 goto cleanup;
5190 }
5191
5192 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN);
5193
5194 if (unlinked_nodes && ctx_node) {
5195 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
5196 rc = -1;
5197 goto cleanup;
5198 }
5199 }
5200
Radek Krejci03b71f72016-03-16 11:10:09 +01005201 if (rc) {
5202 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005203 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005204 }
Radek Krejci51093642016-03-29 10:14:59 +02005205 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005206 }
5207
Michal Vasko944a5642016-03-21 11:48:58 +01005208 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005209 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005210 ly_vlog_hide(0);
Michal Vasko90fc2a32016-08-24 15:58:58 +02005211 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
5212 ly_vlog_hide(1);
Radek Krejci0b7704f2016-03-18 12:16:14 +01005213 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02005214 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005215 }
Radek Krejci51093642016-03-29 10:14:59 +02005216
5217 /* free xpath set content */
5218 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005219 }
5220
5221check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02005222 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02005223 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005224 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02005225 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02005226 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02005227 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005228 }
5229 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02005230
5231 unlinked_nodes = NULL;
5232 tmp_node = node;
5233 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
5234 if (rc) {
5235 goto cleanup;
5236 }
5237
5238 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type, &set, LYXP_WHEN);
5239
5240 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
5241 * so the tree did not actually change and there is nothing for us to do
5242 */
5243 if (unlinked_nodes && ctx_node) {
5244 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
5245 rc = -1;
5246 goto cleanup;
5247 }
5248 }
5249
Radek Krejci03b71f72016-03-16 11:10:09 +01005250 if (rc) {
5251 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005252 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005253 }
Radek Krejci51093642016-03-29 10:14:59 +02005254 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005255 }
5256
Michal Vasko944a5642016-03-21 11:48:58 +01005257 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02005258
Michal Vasko8146d4c2016-05-09 15:50:29 +02005259 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005260 ly_vlog_hide(0);
Michal Vasko90fc2a32016-08-24 15:58:58 +02005261 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
5262 ly_vlog_hide(1);
Radek Krejci0b7704f2016-03-18 12:16:14 +01005263 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02005264 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005265 }
Radek Krejci51093642016-03-29 10:14:59 +02005266
5267 /* free xpath set content */
5268 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005269 }
5270
Michal Vasko90fc2a32016-08-24 15:58:58 +02005271 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02005272 }
5273
Radek Krejci0b7704f2016-03-18 12:16:14 +01005274 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01005275
Radek Krejci51093642016-03-29 10:14:59 +02005276cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02005277 /* free xpath set content */
5278 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0);
5279
Radek Krejci46165822016-08-26 14:06:27 +02005280 if (result) {
5281 if (node->when_status & LYD_WHEN_TRUE) {
5282 *result = 1;
5283 } else {
5284 *result = 0;
5285 }
5286 }
5287
Radek Krejci51093642016-03-29 10:14:59 +02005288 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005289}
5290
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005291/**
Michal Vaskobb211122015-08-19 14:03:11 +02005292 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005293 *
5294 * @param[in] mod Main module.
5295 * @param[in] item Item to resolve. Type determined by \p type.
5296 * @param[in] type Type of the unresolved item.
5297 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02005298 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005299 *
5300 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
5301 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005302static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005303resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Radek Krejci48464ed2016-03-17 15:44:09 +01005304 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005305{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005306 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Radek Krejcic79c6b12016-07-26 15:11:49 +02005307 int rc = -1, has_str = 0, tpdf_flag = 0, i, k;
5308 unsigned int j;
Radek Krejcic13db382016-08-16 10:52:42 +02005309 struct lys_node *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005310 const char *expr;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005311
Radek Krejcic79c6b12016-07-26 15:11:49 +02005312 struct ly_set *refs, *procs;
5313 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005314 struct lys_ident *ident;
5315 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005316 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01005317 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01005318 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02005319 struct unres_list_uniq *unique_info;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005320
5321 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005322 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005323 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005324 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005325 ident = item;
5326
Radek Krejci018f1f52016-08-03 16:01:20 +02005327 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005328 break;
5329 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005330 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005331 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005332 stype = item;
5333
Radek Krejci018f1f52016-08-03 16:01:20 +02005334 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005335 break;
5336 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02005337 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005338 stype = item;
5339
Radek Krejci2f12f852016-01-08 12:59:57 +01005340 /* HACK - when there is no parent, we are in top level typedef and in that
5341 * case, the path has to contain absolute path, so we let the resolve_path_arg_schema()
5342 * know it via tpdf_flag */
5343 if (!node) {
Radek Krejci4f78b532016-02-17 13:43:00 +01005344 tpdf_flag = 1;
Radek Krejci2f12f852016-01-08 12:59:57 +01005345 node = (struct lys_node *)stype->parent;
5346 }
5347
Radek Krejci48464ed2016-03-17 15:44:09 +01005348 rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag,
Michal Vasko1e62a092015-12-01 12:27:20 +01005349 (const struct lys_node **)&stype->info.lref.target);
Michal Vasko01c6fd22016-05-20 11:43:05 +02005350 if (!tpdf_flag && !rc) {
5351 assert(stype->info.lref.target);
Radek Krejci46c4cd72016-01-21 15:13:52 +01005352 /* store the backlink from leafref target */
Michal Vasko01c6fd22016-05-20 11:43:05 +02005353 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
5354 rc = -1;
Radek Krejci46c4cd72016-01-21 15:13:52 +01005355 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01005356 }
5357
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005358 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02005359 case UNRES_TYPE_DER_TPDF:
5360 tpdf_flag = 1;
5361 /* no break */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005362 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01005363 /* parent */
5364 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005365 stype = item;
5366
Michal Vasko88c29542015-11-27 14:57:53 +01005367 /* HACK type->der is temporarily unparsed type statement */
5368 yin = (struct lyxml_elem *)stype->der;
5369 stype->der = NULL;
5370
Pavol Vicana0e4e672016-02-24 12:20:04 +01005371 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
5372 yang = (struct yang_type *)yin;
Radek Krejci3a5501d2016-07-18 22:03:34 +02005373 rc = yang_check_type(mod, node, yang, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01005374
5375 if (rc) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02005376 if (rc == -1) {
5377 yang->type->base = yang->base;
5378 lydict_remove(mod->ctx, yang->name);
5379 free(yang);
5380 stype->der = NULL;
5381 } else {
5382 /* may try again later */
5383 stype->der = (struct lys_tpdf *)yang;
5384 }
Pavol Vicand01d8ae2016-03-01 10:45:59 +01005385 } else {
5386 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02005387 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01005388 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01005389 }
5390
Michal Vasko88c29542015-11-27 14:57:53 +01005391 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02005392 rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01005393 if (!rc) {
5394 /* we need to always be able to free this, it's safe only in this case */
5395 lyxml_free(mod->ctx, yin);
5396 } else {
5397 /* may try again later, put all back how it was */
5398 stype->der = (struct lys_tpdf *)yin;
5399 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005400 }
Radek Krejcic13db382016-08-16 10:52:42 +02005401 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02005402 /* it does not make sense to have leaf-list of empty type */
5403 if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
5404 LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
5405 }
Radek Krejcic13db382016-08-16 10:52:42 +02005406 } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_INGRP) {
5407 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
5408 * by uses statement until the type is resolved. We do that the same way as uses statements inside
5409 * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of
5410 * so far unresolved items (uses and types). The grouping cannot be used unless the nacm value is 0.
5411 * To remember that the grouping already increased grouping's nacm, the LY_TYPE_INGRP is used as value
5412 * of the type's base member. */
5413 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
5414 if (par_grp) {
5415 ((struct lys_node_grp *)par_grp)->nacm++;
5416 stype->base = LY_TYPE_INGRP;
5417 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02005418 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005419 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005420 case UNRES_IFFEAT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005421 node = str_snode;
Radek Krejci9ff0a922016-07-14 13:08:05 +02005422 expr = *((const char **)item);
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005423
Radek Krejci9ff0a922016-07-14 13:08:05 +02005424 rc = resolve_feature(expr, strlen(expr), node, item);
5425 if (!rc) {
5426 /* success */
5427 free((char *)expr);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005428 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005429 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02005430 case UNRES_FEATURE:
5431 feat = (struct lys_feature *)item;
5432
5433 if (feat->iffeature_size) {
5434 refs = ly_set_new();
5435 procs = ly_set_new();
5436 ly_set_add(procs, feat, 0);
5437
5438 while (procs->number) {
5439 ref = procs->set.g[procs->number - 1];
5440 ly_set_rm_index(procs, procs->number - 1);
5441
5442 for (i = 0; i < ref->iffeature_size; i++) {
5443 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
5444 for (; j > 0 ; j--) {
5445 if (unres_schema_find(unres, &ref->iffeature[i].features[j - 1], UNRES_IFFEAT) == -1) {
5446 if (ref->iffeature[i].features[j - 1] == feat) {
5447 LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
5448 goto featurecheckdone;
5449 }
5450
5451 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
5452 k = refs->number;
5453 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
5454 /* not yet seen feature, add it for processing */
5455 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
5456 }
5457 }
5458 } else {
5459 /* forward reference */
5460 rc = EXIT_FAILURE;
5461 goto featurecheckdone;
5462 }
5463 }
5464
5465 }
5466 }
5467 rc = EXIT_SUCCESS;
5468
5469featurecheckdone:
5470 ly_set_free(refs);
5471 ly_set_free(procs);
5472 }
5473
5474 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005475 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01005476 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005477 break;
5478 case UNRES_TYPE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005479 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005480 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005481 stype = item;
5482
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005483 rc = check_default(stype, expr, mod);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005484 break;
5485 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005486 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005487 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005488 choic = item;
5489
Radek Krejcie00d2312016-08-12 15:27:49 +02005490 if (!choic->dflt) {
5491 choic->dflt = resolve_choice_dflt(choic, expr);
5492 }
Michal Vasko7955b362015-09-04 14:18:15 +02005493 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02005494 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02005495 } else {
5496 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005497 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005498 break;
5499 case UNRES_LIST_KEYS:
Radek Krejci4f78b532016-02-17 13:43:00 +01005500 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01005501 rc = resolve_list_keys(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005502 break;
5503 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02005504 unique_info = (struct unres_list_uniq *)item;
5505 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005506 break;
Michal Vasko7178e692016-02-12 15:58:05 +01005507 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005508 rc = resolve_augment(item, NULL);
Michal Vasko7178e692016-02-12 15:58:05 +01005509 break;
Michal Vaskocf024702015-10-08 15:01:42 +02005510 default:
5511 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005512 break;
5513 }
5514
Radek Krejci54081ce2016-08-12 15:21:47 +02005515 if (has_str && !rc) {
5516 /* the string is no more needed in case of success.
5517 * In case of forward reference, we will try to resolve the string later */
Radek Krejci4f78b532016-02-17 13:43:00 +01005518 lydict_remove(mod->ctx, str_snode);
5519 }
5520
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005521 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005522}
5523
Michal Vaskof02e3742015-08-05 16:27:02 +02005524/* logs directly */
5525static void
Radek Krejci48464ed2016-03-17 15:44:09 +01005526print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005527{
Michal Vaskocb34dc62016-05-20 14:38:37 +02005528 struct lyxml_elem *xml;
5529 struct lyxml_attr *attr;
Radek Krejci76e15e12016-06-22 11:02:24 +02005530 const char *type_name = NULL;
Michal Vaskocb34dc62016-05-20 14:38:37 +02005531
Michal Vaskof02e3742015-08-05 16:27:02 +02005532 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02005533 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005534 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005535 break;
5536 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01005537 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005538 break;
5539 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01005540 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
5541 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02005542 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02005543 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02005544 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02005545 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
5546 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
5547 type_name = ((struct yang_type *)xml)->name;
5548 } else {
5549 LY_TREE_FOR(xml->attr, attr) {
5550 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
5551 type_name = attr->value;
5552 break;
5553 }
5554 }
5555 assert(attr);
5556 }
5557 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", type_name);
Michal Vaskof02e3742015-08-05 16:27:02 +02005558 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02005559 case UNRES_IFFEAT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005560 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)item);
Michal Vaskof02e3742015-08-05 16:27:02 +02005561 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02005562 case UNRES_FEATURE:
5563 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
5564 ((struct lys_feature *)item)->name);
5565 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02005566 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01005567 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02005568 break;
5569 case UNRES_TYPE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005570 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005571 break;
5572 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005573 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005574 break;
5575 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01005576 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005577 break;
5578 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01005579 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005580 break;
Michal Vasko7178e692016-02-12 15:58:05 +01005581 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005582 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
5583 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01005584 break;
Michal Vaskocf024702015-10-08 15:01:42 +02005585 default:
5586 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02005587 break;
5588 }
5589}
5590
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005591/**
Michal Vaskobb211122015-08-19 14:03:11 +02005592 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005593 *
5594 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005595 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005596 *
Michal Vasko92b8a382015-08-19 14:03:49 +02005597 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005598 */
Michal Vaskof02e3742015-08-05 16:27:02 +02005599int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005600resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02005601{
Radek Krejci010e54b2016-03-15 09:40:34 +01005602 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005603 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005604
5605 assert(unres);
5606
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005607 LOGVRB("Resolving unresolved schema nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01005608 ly_vlog_hide(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02005609
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005610 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02005611 do {
Michal Vasko88c29542015-11-27 14:57:53 +01005612 unres_count = 0;
5613 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02005614
5615 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005616 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02005617 * if-features are resolved here to make sure that we will have all if-features for
5618 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02005619 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02005620 continue;
5621 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005622 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejcie00d2312016-08-12 15:27:49 +02005623 * UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02005624
Michal Vasko88c29542015-11-27 14:57:53 +01005625 ++unres_count;
Radek Krejci48464ed2016-03-17 15:44:09 +01005626 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005627 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02005628 unres->type[i] = UNRES_RESOLVED;
5629 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01005630 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02005631 } else if (rc == -1) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005632 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02005633 /* print the error */
5634 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005635 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02005636 } else {
5637 /* forward reference, erase ly_errno */
5638 ly_errno = LY_SUCCESS;
5639 ly_vecode = LYVE_SUCCESS;
Michal Vasko51054ca2015-08-12 12:20:00 +02005640 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005641 }
Michal Vasko88c29542015-11-27 14:57:53 +01005642 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02005643
Michal Vasko88c29542015-11-27 14:57:53 +01005644 if (res_count < unres_count) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02005645 /* just print the errors */
5646 ly_vlog_hide(0);
5647
5648 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005649 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02005650 continue;
5651 }
5652 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci8a8d5ff2016-07-27 14:08:38 +02005653
5654 /* free the allocated resources */
5655 if (unres->type[i] == UNRES_IFFEAT) {
5656 free(*((char **)unres->item[i]));
5657 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02005658 }
Michal Vasko92b8a382015-08-19 14:03:49 +02005659 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005660 }
5661
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005662 /* the rest */
5663 for (i = 0; i < unres->count; ++i) {
5664 if (unres->type[i] == UNRES_RESOLVED) {
5665 continue;
5666 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02005667
Radek Krejci48464ed2016-03-17 15:44:09 +01005668 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005669 if (rc == 0) {
5670 unres->type[i] = UNRES_RESOLVED;
5671 ++resolved;
5672 } else if (rc == -1) {
5673 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02005674 /* print the error */
5675 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
5676 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005677 }
5678 }
5679
Radek Krejci010e54b2016-03-15 09:40:34 +01005680 ly_vlog_hide(0);
5681
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005682 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005683 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
5684 * all the validation errors
5685 */
5686 for (i = 0; i < unres->count; ++i) {
5687 if (unres->type[i] == UNRES_RESOLVED) {
5688 continue;
5689 }
Radek Krejci48464ed2016-03-17 15:44:09 +01005690 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005691 }
Michal Vasko92b8a382015-08-19 14:03:49 +02005692 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005693 }
5694
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005695 LOGVRB("All schema nodes and constraints resolved.");
Radek Krejcic071c542016-01-27 14:57:51 +01005696 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005697 return EXIT_SUCCESS;
5698}
5699
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005700/**
Michal Vaskobb211122015-08-19 14:03:11 +02005701 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005702 *
5703 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005704 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005705 * @param[in] item Item to resolve. Type determined by \p type.
5706 * @param[in] type Type of the unresolved item.
5707 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005708 *
Michal Vasko3767fb22016-07-21 12:10:57 +02005709 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005710 */
5711int
Radek Krejci48464ed2016-03-17 15:44:09 +01005712unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
5713 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005714{
Radek Krejci54081ce2016-08-12 15:21:47 +02005715 int rc;
5716 const char *dictstr;
5717
5718 dictstr = lydict_insert(mod->ctx, str, 0);
5719 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
5720
5721 if (rc == -1) {
5722 lydict_remove(mod->ctx, dictstr);
5723 }
5724 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005725}
5726
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005727/**
Michal Vaskobb211122015-08-19 14:03:11 +02005728 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005729 *
5730 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005731 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005732 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01005733 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005734 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005735 *
Michal Vasko3767fb22016-07-21 12:10:57 +02005736 * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005737 */
5738int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005739unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01005740 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005741{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005742 int rc;
Michal Vasko88c29542015-11-27 14:57:53 +01005743 struct lyxml_elem *yin;
Radek Krejci010e54b2016-03-15 09:40:34 +01005744 char *path, *msg;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005745
Michal Vasko9bf425b2015-10-22 11:42:03 +02005746 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
5747 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005748
Radek Krejci010e54b2016-03-15 09:40:34 +01005749 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01005750 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005751 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005752 if (rc != EXIT_FAILURE) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005753 if (rc == -1 && ly_errno == LY_EVALID) {
Radek Krejci76e15e12016-06-22 11:02:24 +02005754 if (ly_log_level >= LY_LLERR) {
5755 path = strdup(ly_errpath());
5756 msg = strdup(ly_errmsg());
5757 LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
5758 free(path);
5759 free(msg);
5760 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005761 }
Radek Krejcid09d1a52016-08-11 14:05:45 +02005762 if (type == UNRES_LIST_UNIQ) {
5763 /* free the allocated structure */
5764 free(item);
5765 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005766 return rc;
Radek Krejcif347abc2016-06-22 10:18:47 +02005767 } else {
5768 /* erase info about validation errors */
5769 ly_errno = LY_SUCCESS;
5770 ly_vecode = LYVE_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005771 }
5772
Radek Krejci48464ed2016-03-17 15:44:09 +01005773 print_unres_schema_item_fail(item, type, snode);
Michal Vaskof02e3742015-08-05 16:27:02 +02005774
Michal Vasko88c29542015-11-27 14:57:53 +01005775 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
Radek Krejci3a5501d2016-07-18 22:03:34 +02005776 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
Michal Vasko88c29542015-11-27 14:57:53 +01005777 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
Pavol Vicana0e4e672016-02-24 12:20:04 +01005778 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
5779 lyxml_unlink_elem(mod->ctx, yin, 1);
5780 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
5781 }
Michal Vasko88c29542015-11-27 14:57:53 +01005782 }
5783
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005784 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01005785 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
5786 if (!unres->item) {
5787 LOGMEM;
5788 return -1;
5789 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005790 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01005791 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
5792 if (!unres->type) {
5793 LOGMEM;
5794 return -1;
5795 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005796 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01005797 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
5798 if (!unres->str_snode) {
5799 LOGMEM;
5800 return -1;
5801 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005802 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01005803 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
5804 if (!unres->module) {
5805 LOGMEM;
5806 return -1;
5807 }
5808 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005809
Michal Vasko3767fb22016-07-21 12:10:57 +02005810 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005811}
5812
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005813/**
Michal Vaskobb211122015-08-19 14:03:11 +02005814 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005815 *
5816 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005817 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005818 * @param[in] item Old item to be resolved.
5819 * @param[in] type Type of the old unresolved item.
5820 * @param[in] new_item New item to use in the duplicate.
5821 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02005822 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005823 */
Michal Vaskodad19402015-08-06 09:51:53 +02005824int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005825unres_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 +02005826{
5827 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02005828 struct unres_list_uniq aux_uniq;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005829
Michal Vaskocf024702015-10-08 15:01:42 +02005830 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005831
Radek Krejcid09d1a52016-08-11 14:05:45 +02005832 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
5833 if (type == UNRES_LIST_UNIQ) {
5834 aux_uniq.list = item;
5835 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
5836 item = &aux_uniq;
5837 }
Michal Vasko0bd29d12015-08-19 11:45:49 +02005838 i = unres_schema_find(unres, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005839
5840 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02005841 if (type == UNRES_LIST_UNIQ) {
5842 free(new_item);
5843 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02005844 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005845 }
5846
Radek Krejcic79c6b12016-07-26 15:11:49 +02005847 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcid09d1a52016-08-11 14:05:45 +02005848 (type == UNRES_IFFEAT) || (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005849 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005850 LOGINT;
5851 return -1;
5852 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005853 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005854 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005855 LOGINT;
5856 return -1;
5857 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005858 }
Michal Vaskodad19402015-08-06 09:51:53 +02005859
5860 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005861}
5862
Michal Vaskof02e3742015-08-05 16:27:02 +02005863/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005864int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005865unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005866{
5867 uint32_t ret = -1, i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02005868 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005869
Radek Krejcic79c6b12016-07-26 15:11:49 +02005870 for (i = unres->count; i > 0; i--) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02005871 if (unres->type[i - 1] != type) {
5872 continue;
5873 }
5874 if (type != UNRES_LIST_UNIQ) {
5875 if (unres->item[i - 1] == item) {
5876 ret = i - 1;
5877 break;
5878 }
5879 } else {
5880 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
5881 aux_uniq2 = (struct unres_list_uniq *)item;
5882 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
5883 ret = i - 1;
5884 break;
5885 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005886 }
5887 }
5888
5889 return ret;
5890}
Michal Vasko8bcdf292015-08-19 14:04:43 +02005891
Michal Vaskoede9c472016-06-07 09:38:15 +02005892static void
5893unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
5894{
5895 struct lyxml_elem *yin;
5896 struct yang_type *yang;
5897
5898 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02005899 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02005900 case UNRES_TYPE_DER:
5901 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
5902 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
5903 yang =(struct yang_type *)yin;
5904 yang->type->base = yang->base;
5905 lydict_remove(ctx, yang->name);
5906 free(yang);
5907 } else {
5908 lyxml_free(ctx, yin);
5909 }
5910 break;
5911 case UNRES_IDENT:
5912 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02005913 case UNRES_TYPE_DFLT:
5914 case UNRES_CHOICE_DFLT:
5915 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02005916 lydict_remove(ctx, (const char *)unres->str_snode[i]);
5917 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02005918 case UNRES_LIST_UNIQ:
5919 free(unres->item[i]);
5920 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02005921 default:
5922 break;
5923 }
5924 unres->type[i] = UNRES_RESOLVED;
5925}
5926
Michal Vasko88c29542015-11-27 14:57:53 +01005927void
Radek Krejcic071c542016-01-27 14:57:51 +01005928unres_schema_free(struct lys_module *module, struct unres_schema **unres)
Michal Vasko88c29542015-11-27 14:57:53 +01005929{
5930 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01005931 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01005932
Radek Krejcic071c542016-01-27 14:57:51 +01005933 if (!unres || !(*unres)) {
5934 return;
Michal Vasko88c29542015-11-27 14:57:53 +01005935 }
5936
Radek Krejcic071c542016-01-27 14:57:51 +01005937 assert(module || (*unres)->count == 0);
5938
5939 for (i = 0; i < (*unres)->count; ++i) {
5940 if ((*unres)->module[i] != module) {
5941 if ((*unres)->type[i] != UNRES_RESOLVED) {
5942 unresolved++;
5943 }
5944 continue;
5945 }
Michal Vaskoede9c472016-06-07 09:38:15 +02005946
5947 /* free heap memory for the specific item */
5948 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01005949 }
5950
Michal Vaskoede9c472016-06-07 09:38:15 +02005951 /* free it all */
Radek Krejcic071c542016-01-27 14:57:51 +01005952 if (!module || (!unresolved && !module->type)) {
5953 free((*unres)->item);
5954 free((*unres)->type);
5955 free((*unres)->str_snode);
5956 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01005957 free((*unres));
5958 (*unres) = NULL;
5959 }
Michal Vasko88c29542015-11-27 14:57:53 +01005960}
5961
Michal Vasko8bcdf292015-08-19 14:04:43 +02005962/**
5963 * @brief Resolve a single unres data item. Logs directly.
5964 *
Michal Vaskocf024702015-10-08 15:01:42 +02005965 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02005966 * @param[in] type Type of the unresolved item.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005967 *
5968 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
5969 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02005970int
Radek Krejci48464ed2016-03-17 15:44:09 +01005971resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02005972{
5973 uint32_t i;
Michal Vasko0491ab32015-08-19 14:28:29 +02005974 int rc;
Michal Vasko83a6c462015-10-08 16:43:53 +02005975 struct lyd_node_leaf_list *leaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005976 struct lys_node_leaf *sleaf;
Michal Vaskoc4280842016-04-19 16:10:42 +02005977 struct lyd_node *parent;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005978 struct unres_data matches;
5979
5980 memset(&matches, 0, sizeof matches);
Michal Vasko83a6c462015-10-08 16:43:53 +02005981 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02005982 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005983
Michal Vaskocf024702015-10-08 15:01:42 +02005984 switch (type) {
5985 case UNRES_LEAFREF:
5986 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vasko2471e7f2016-04-11 11:00:15 +02005987 /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */
5988 if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) {
5989 return -1;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005990 }
5991
5992 /* check that value matches */
5993 for (i = 0; i < matches.count; ++i) {
Radek Krejci749190d2016-02-18 16:26:25 +01005994 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) {
Michal Vaskocf024702015-10-08 15:01:42 +02005995 leaf->value.leafref = matches.node[i];
Michal Vasko8bcdf292015-08-19 14:04:43 +02005996 break;
5997 }
5998 }
5999
Michal Vaskocf024702015-10-08 15:01:42 +02006000 free(matches.node);
Michal Vasko8bcdf292015-08-19 14:04:43 +02006001
Michal Vaskocf024702015-10-08 15:01:42 +02006002 if (!leaf->value.leafref) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02006003 /* reference not found */
Michal Vasko6ac68282016-04-11 10:56:47 +02006004 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02006005 return EXIT_FAILURE;
6006 }
Michal Vaskocf024702015-10-08 15:01:42 +02006007 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02006008
Michal Vaskocf024702015-10-08 15:01:42 +02006009 case UNRES_INSTID:
Michal Vasko976a5f22016-05-18 13:28:42 +02006010 assert((sleaf->type.base == LY_TYPE_INST) || (sleaf->type.base == LY_TYPE_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02006011 ly_errno = 0;
Radek Krejci48464ed2016-03-17 15:44:09 +01006012 leaf->value.instance = resolve_instid(node, leaf->value_str);
Radek Krejci40f17b92016-02-03 14:30:43 +01006013 if (!leaf->value.instance) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02006014 if (ly_errno) {
6015 return -1;
6016 } else if (sleaf->type.info.inst.req > -1) {
Michal Vasko6ac68282016-04-11 10:56:47 +02006017 LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02006018 return EXIT_FAILURE;
6019 } else {
Radek Krejci4ce42be2016-02-03 13:04:41 +01006020 LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02006021 }
6022 }
Michal Vaskocf024702015-10-08 15:01:42 +02006023 break;
6024
6025 case UNRES_WHEN:
Radek Krejci46165822016-08-26 14:06:27 +02006026 if ((rc = resolve_when(node, NULL))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006027 return rc;
6028 }
6029 break;
6030
Michal Vaskobf19d252015-10-08 15:39:17 +02006031 case UNRES_MUST:
Radek Krejci48464ed2016-03-17 15:44:09 +01006032 if ((rc = resolve_must(node))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02006033 return rc;
6034 }
6035 break;
6036
Michal Vaskoc4280842016-04-19 16:10:42 +02006037 case UNRES_EMPTYCONT:
6038 do {
6039 parent = node->parent;
6040 lyd_free(node);
6041 node = parent;
6042 } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child
6043 && !((struct lys_node_container *)node->schema)->presence);
6044 break;
6045
Michal Vaskocf024702015-10-08 15:01:42 +02006046 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02006047 LOGINT;
6048 return -1;
6049 }
6050
6051 return EXIT_SUCCESS;
6052}
6053
6054/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01006055 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02006056 *
6057 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02006058 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02006059 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01006060 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02006061 */
6062int
Radek Krejci0b7704f2016-03-18 12:16:14 +01006063unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02006064{
Radek Krejci03b71f72016-03-16 11:10:09 +01006065 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02006066 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
6067 || (type == UNRES_EMPTYCONT));
Michal Vasko8bcdf292015-08-19 14:04:43 +02006068
Radek Krejci03b71f72016-03-16 11:10:09 +01006069 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01006070 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
6071 if (!unres->node) {
6072 LOGMEM;
6073 return -1;
6074 }
Michal Vaskocf024702015-10-08 15:01:42 +02006075 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01006076 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
6077 if (!unres->type) {
6078 LOGMEM;
6079 return -1;
6080 }
Michal Vaskocf024702015-10-08 15:01:42 +02006081 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02006082
Radek Krejci0b7704f2016-03-18 12:16:14 +01006083 if (type == UNRES_WHEN) {
6084 /* remove previous result */
6085 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006086 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006087
6088 return EXIT_SUCCESS;
6089}
6090
6091/**
6092 * @brief Resolve every unres data item in the structure. Logs directly.
6093 *
6094 * @param[in] unres Unres data structure to use.
Radek Krejci03b71f72016-03-16 11:10:09 +01006095 * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If
6096 * NULL and when condition is false the error is raised.
Radek Krejci0c0086a2016-03-24 15:20:28 +01006097 * @param[in] options Parer options
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006098 *
6099 * @return EXIT_SUCCESS on success, -1 on error.
6100 */
6101int
Radek Krejci0c0086a2016-03-24 15:20:28 +01006102resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006103{
Radek Krejci0c0086a2016-03-24 15:20:28 +01006104 uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01006105 int rc, progress;
Radek Krejci03b71f72016-03-16 11:10:09 +01006106 char *msg, *path;
Radek Krejci0b7704f2016-03-18 12:16:14 +01006107 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006108
Radek Krejci03b71f72016-03-16 11:10:09 +01006109 assert(unres);
Radek Krejci8ee1b562016-03-31 10:58:31 +02006110 assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL));
Radek Krejci03b71f72016-03-16 11:10:09 +01006111
6112 if (!unres->count) {
6113 return EXIT_SUCCESS;
6114 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006115
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02006116 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01006117 ly_vlog_hide(1);
6118
Radek Krejci0b7704f2016-03-18 12:16:14 +01006119 /* when-stmt first */
Radek Krejci03b71f72016-03-16 11:10:09 +01006120 ly_errno = LY_SUCCESS;
6121 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01006122 do {
6123 progress = 0;
6124 for(i = 0; i < unres->count; i++) {
6125 if (unres->type[i] != UNRES_WHEN) {
6126 continue;
6127 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01006128 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006129 /* count when-stmt nodes in unres list */
6130 when_stmt++;
6131 }
6132
6133 /* resolve when condition only when all parent when conditions are already resolved */
6134 for (parent = unres->node[i]->parent;
6135 parent && LYD_WHEN_DONE(parent->when_status);
6136 parent = parent->parent) {
6137 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
6138 /* the parent node was already unlinked, do not resolve this node,
6139 * it will be removed anyway, so just mark it as resolved
6140 */
6141 unres->node[i]->when_status |= LYD_WHEN_FALSE;
6142 unres->type[i] = UNRES_RESOLVED;
6143 resolved++;
6144 break;
6145 }
6146 }
6147 if (parent) {
6148 continue;
6149 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006150
Radek Krejci48464ed2016-03-17 15:44:09 +01006151 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01006152 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006153 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
6154 if (!root) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006155 /* false when condition */
6156 ly_vlog_hide(0);
Radek Krejci76e15e12016-06-22 11:02:24 +02006157 if (ly_log_level >= LY_LLERR) {
6158 path = strdup(ly_errpath());
6159 msg = strdup(ly_errmsg());
6160 LOGERR(LY_EVALID, "%s%s%s%s", msg,
6161 path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
6162 free(path);
6163 free(msg);
6164 }
Radek Krejci03b71f72016-03-16 11:10:09 +01006165 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01006166 } /* follows else */
6167
Radek Krejci0c0086a2016-03-24 15:20:28 +01006168 /* only unlink now, the subtree can contain another nodes stored in the unres list */
6169 /* if it has parent non-presence containers that would be empty, we should actually
6170 * remove the container
6171 */
6172 if (!(options & LYD_OPT_KEEPEMPTYCONT)) {
6173 for (parent = unres->node[i];
6174 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
6175 parent = parent->parent) {
6176 if (((struct lys_node_container *)parent->parent->schema)->presence) {
6177 /* presence container */
6178 break;
6179 }
6180 if (parent->next || parent->prev != parent) {
6181 /* non empty (the child we are in and we are going to remove is not the only child) */
6182 break;
6183 }
6184 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01006185 unres->node[i] = parent;
6186 }
6187
Radek Krejci0b7704f2016-03-18 12:16:14 +01006188 /* auto-delete */
6189 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
6190 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
Radek Krejci0c0086a2016-03-24 15:20:28 +01006191 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006192 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01006193 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01006194
Radek Krejci0b7704f2016-03-18 12:16:14 +01006195 lyd_unlink(unres->node[i]);
6196 unres->type[i] = UNRES_DELETE;
6197 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01006198
6199 /* update the rest of unres items */
6200 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01006201 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01006202 continue;
6203 }
6204
6205 /* test if the node is in subtree to be deleted */
6206 for (parent = unres->node[j]; parent; parent = parent->parent) {
6207 if (parent == unres->node[i]) {
6208 /* yes, it is */
6209 unres->type[j] = UNRES_RESOLVED;
6210 resolved++;
6211 break;
6212 }
6213 }
6214 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01006215 } else {
6216 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01006217 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01006218 ly_errno = LY_SUCCESS;
6219 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01006220 resolved++;
6221 progress = 1;
6222 } else if (rc == -1) {
6223 ly_vlog_hide(0);
Michal Vasko76e73402016-08-24 16:00:13 +02006224 /* print only this last error */
6225 resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01006226 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02006227 } else {
6228 /* forward reference, erase ly_errno */
6229 ly_errno = LY_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01006230 }
6231 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01006232 first = 0;
Radek Krejci0b7704f2016-03-18 12:16:14 +01006233 } while (progress && resolved < when_stmt);
Radek Krejci010e54b2016-03-15 09:40:34 +01006234
Radek Krejci0b7704f2016-03-18 12:16:14 +01006235 /* do we have some unresolved when-stmt? */
Radek Krejcid940d732016-03-24 16:02:28 +01006236 if (when_stmt > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006237 ly_vlog_hide(0);
Radek Krejci76e15e12016-06-22 11:02:24 +02006238 if (ly_log_level >= LY_LLERR) {
6239 path = strdup(ly_errpath());
6240 msg = strdup(ly_errmsg());
6241 LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
6242 free(path);
6243 free(msg);
6244 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01006245 return -1;
6246 }
6247
6248 for (i = 0; del_items && i < unres->count; i++) {
6249 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
6250 if (unres->type[i] != UNRES_DELETE) {
6251 continue;
6252 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01006253 if (!unres->node[i]) {
6254 unres->type[i] = UNRES_RESOLVED;
6255 del_items--;
6256 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01006257 }
6258
6259 /* really remove the complete subtree */
6260 lyd_free(unres->node[i]);
6261 unres->type[i] = UNRES_RESOLVED;
6262 del_items--;
6263 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006264
6265 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006266 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01006267 if (unres->type[i] == UNRES_RESOLVED) {
6268 continue;
6269 }
6270
Radek Krejci48464ed2016-03-17 15:44:09 +01006271 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01006272 if (rc == 0) {
6273 unres->type[i] = UNRES_RESOLVED;
6274 resolved++;
6275 } else if (rc == -1) {
6276 ly_vlog_hide(0);
Michal Vasko96b846c2016-05-18 13:28:58 +02006277 /* print only this last error */
6278 resolve_unres_data_item(unres->node[i], unres->type[i]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006279 return -1;
6280 }
6281 }
6282
Radek Krejci010e54b2016-03-15 09:40:34 +01006283 ly_vlog_hide(0);
6284 if (resolved < unres->count) {
6285 /* try to resolve the unresolved data again, it will not resolve anything, but it will print
6286 * all the validation errors
6287 */
6288 for (i = 0; i < unres->count; ++i) {
6289 if (unres->type[i] == UNRES_RESOLVED) {
6290 continue;
6291 }
Radek Krejci48464ed2016-03-17 15:44:09 +01006292 resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01006293 }
6294 return -1;
6295 }
6296
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02006297 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01006298 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006299 return EXIT_SUCCESS;
6300}