blob: 786ec24ec0d0bdbd12bc44eeb121fc2fa3122bb5 [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) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200558 return -parsed+ret;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200559 } else if (model && !*model) {
560 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200561 }
562
563 parsed += ret;
564 id += ret;
565
566 if ((id[0] == '[') && has_predicate) {
567 *has_predicate = 1;
568 }
569
570 return parsed;
571}
572
573/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200574 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200575 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200576 *
577 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
578 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
579 * ((DQUOTE string DQUOTE) /
580 * (SQUOTE string SQUOTE))
581 * pos = non-negative-integer-value
582 *
Michal Vaskobb211122015-08-19 14:03:11 +0200583 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200584 * @param[out] model Points to the model name.
585 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200586 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
587 * @param[out] nam_len Length of the node name.
588 * @param[out] value Value the node-identifier must have (string from the grammar),
589 * NULL if there is not any.
590 * @param[out] val_len Length of the value, 0 if there is not any.
591 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
592 *
593 * @return Number of characters successfully parsed,
594 * positive on success, negative on failure.
595 */
596static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200597parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
598 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200599{
600 const char *ptr;
601 int parsed = 0, ret;
602 char quote;
603
604 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200605 if (model) {
606 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200607 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200608 if (mod_len) {
609 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200610 }
611 if (name) {
612 *name = NULL;
613 }
614 if (nam_len) {
615 *nam_len = 0;
616 }
617 if (value) {
618 *value = NULL;
619 }
620 if (val_len) {
621 *val_len = 0;
622 }
623 if (has_predicate) {
624 *has_predicate = 0;
625 }
626
627 if (id[0] != '[') {
628 return -parsed;
629 }
630
631 ++parsed;
632 ++id;
633
634 while (isspace(id[0])) {
635 ++parsed;
636 ++id;
637 }
638
639 /* pos */
640 if (isdigit(id[0])) {
641 if (name) {
642 *name = id;
643 }
644
645 if (id[0] == '0') {
646 ++parsed;
647 ++id;
648
649 if (isdigit(id[0])) {
650 return -parsed;
651 }
652 }
653
654 while (isdigit(id[0])) {
655 ++parsed;
656 ++id;
657 }
658
659 if (nam_len) {
660 *nam_len = id-(*name);
661 }
662
663 /* "." */
664 } else if (id[0] == '.') {
665 if (name) {
666 *name = id;
667 }
668 if (nam_len) {
669 *nam_len = 1;
670 }
671
672 ++parsed;
673 ++id;
674
675 /* node-identifier */
676 } else {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200677 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200678 return -parsed+ret;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200679 } else if (model && !*model) {
680 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200681 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200682
683 parsed += ret;
684 id += ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200685 }
686
687 while (isspace(id[0])) {
688 ++parsed;
689 ++id;
690 }
691
692 if (id[0] != '=') {
693 return -parsed;
694 }
695
696 ++parsed;
697 ++id;
698
699 while (isspace(id[0])) {
700 ++parsed;
701 ++id;
702 }
703
704 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
705 if ((id[0] == '\"') || (id[0] == '\'')) {
706 quote = id[0];
707
708 ++parsed;
709 ++id;
710
711 if ((ptr = strchr(id, quote)) == NULL) {
712 return -parsed;
713 }
714 ret = ptr-id;
715
716 if (value) {
717 *value = id;
718 }
719 if (val_len) {
720 *val_len = ret;
721 }
722
723 parsed += ret+1;
724 id += ret+1;
725 } else {
726 return -parsed;
727 }
728
729 while (isspace(id[0])) {
730 ++parsed;
731 ++id;
732 }
733
734 if (id[0] != ']') {
735 return -parsed;
736 }
737
738 ++parsed;
739 ++id;
740
741 if ((id[0] == '[') && has_predicate) {
742 *has_predicate = 1;
743 }
744
745 return parsed;
746}
747
748/**
749 * @brief Parse schema-nodeid.
750 *
751 * schema-nodeid = absolute-schema-nodeid /
752 * descendant-schema-nodeid
753 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200754 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200755 * node-identifier
756 * absolute-schema-nodeid
757 *
Michal Vaskobb211122015-08-19 14:03:11 +0200758 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200759 * @param[out] mod_name Points to the module name, NULL if there is not any.
760 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200761 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200762 * @param[out] nam_len Length of the node name.
763 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
764 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100765 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
766 * based on the grammar, in those cases use NULL.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200767 *
768 * @return Number of characters successfully parsed,
769 * positive on success, negative on failure.
770 */
Michal Vasko22448d32016-03-16 13:17:29 +0100771int
Michal Vasko723e50c2015-10-20 15:20:29 +0200772parse_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 +0100773 int *is_relative, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200774{
775 int parsed = 0, ret;
776
777 assert(id);
778 assert(is_relative);
Michal Vasko723e50c2015-10-20 15:20:29 +0200779 if (mod_name) {
780 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200781 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200782 if (mod_name_len) {
783 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200784 }
785 if (name) {
786 *name = NULL;
787 }
788 if (nam_len) {
789 *nam_len = 0;
790 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100791 if (has_predicate) {
792 *has_predicate = 0;
793 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200794
795 if (id[0] != '/') {
796 if (*is_relative != -1) {
797 return -parsed;
798 } else {
799 *is_relative = 1;
800 }
Michal Vasko48935352016-03-29 11:52:36 +0200801 if (!strncmp(id, "./", 2)) {
802 parsed += 2;
803 id += 2;
804 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200805 } else {
806 if (*is_relative == -1) {
807 *is_relative = 0;
808 }
809 ++parsed;
810 ++id;
811 }
812
Michal Vasko723e50c2015-10-20 15:20:29 +0200813 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200814 return -parsed+ret;
815 }
816
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100817 parsed += ret;
818 id += ret;
819
820 if ((id[0] == '[') && has_predicate) {
821 *has_predicate = 1;
822 }
823
824 return parsed;
825}
826
827/**
828 * @brief Parse schema predicate (special format internally used).
829 *
830 * predicate = "[" *WSP predicate-expr *WSP "]"
831 * predicate-expr = identifier / key-with-value
832 * key-with-value = identifier *WSP "=" *WSP
833 * ((DQUOTE string DQUOTE) /
834 * (SQUOTE string SQUOTE))
835 *
836 * @param[in] id Identifier to use.
837 * @param[out] name Points to the list key name.
838 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100839 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100840 * @param[out] val_len Length of \p value.
841 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
842 */
Michal Vasko22448d32016-03-16 13:17:29 +0100843int
Michal Vasko3547c532016-03-14 09:40:50 +0100844parse_schema_list_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len,
845 int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100846{
847 const char *ptr;
848 int parsed = 0, ret;
849 char quote;
850
851 assert(id);
852 if (name) {
853 *name = NULL;
854 }
855 if (nam_len) {
856 *nam_len = 0;
857 }
858 if (value) {
859 *value = NULL;
860 }
861 if (val_len) {
862 *val_len = 0;
863 }
864 if (has_predicate) {
865 *has_predicate = 0;
866 }
867
868 if (id[0] != '[') {
869 return -parsed;
870 }
871
872 ++parsed;
873 ++id;
874
875 while (isspace(id[0])) {
876 ++parsed;
877 ++id;
878 }
879
Michal Vasko22448d32016-03-16 13:17:29 +0100880 /* identifier */
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100881 if ((ret = parse_identifier(id)) < 1) {
882 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 Vasko3edeaf72016-02-11 13:17:43 +0100958 * @brief Resolve (find) a data node based on a schema-nodeid.
959 *
960 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
961 * module).
962 *
963 */
964struct lyd_node *
965resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
966{
967 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +0200968 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +0100969 const struct lys_node *schema = NULL;
Radek Krejcicc217a62016-04-08 16:58:11 +0200970 int shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +0100971
972 assert(nodeid && start);
973
974 if (nodeid[0] == '/') {
975 return NULL;
976 }
977
978 str = p = strdup(nodeid);
979 if (!str) {
980 LOGMEM;
981 return NULL;
982 }
Radek Krejci5da4eb62016-04-08 14:45:51 +0200983
Michal Vasko3edeaf72016-02-11 13:17:43 +0100984 while (p) {
985 token = p;
986 p = strchr(p, '/');
987 if (p) {
988 *p = '\0';
989 p++;
990 }
991
Radek Krejci5da4eb62016-04-08 14:45:51 +0200992 if (p) {
993 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +0200994 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
995 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_LEAF, &schema, 0)
Radek Krejci5da4eb62016-04-08 14:45:51 +0200996 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +0200997 result = NULL;
998 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +0200999 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001000
Radek Krejci5da4eb62016-04-08 14:45:51 +02001001 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1002 continue;
Radek Krejcicc217a62016-04-08 16:58:11 +02001003 } else if (schema->parent->nodetype == LYS_CHOICE) {
1004 /* shorthand case */
1005 if (!shorthand) {
1006 shorthand = 1;
1007 schema = schema->parent;
1008 continue;
1009 } else {
1010 shorthand = 0;
1011 if (schema->nodetype == LYS_LEAF) {
1012 /* should not be here, since we have leaf, which is not a shorthand nor final node */
1013 result = NULL;
1014 break;
1015 }
1016 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001017 }
1018 } else {
1019 /* final node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001020 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, &schema, shorthand ? 0 : 1)
1021 || !schema) {
1022 result = NULL;
1023 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001024 }
1025 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001026 LY_TREE_FOR(result ? result->child : start, iter) {
1027 if (iter->schema == schema) {
1028 /* move in data tree according to returned schema */
1029 result = iter;
1030 break;
1031 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001032 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001033 if (!iter) {
1034 /* instance not found */
1035 result = NULL;
1036 break;
1037 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001038 }
1039 free(str);
1040
1041 return result;
1042}
1043
Radek Krejcibdf92362016-04-08 14:43:34 +02001044/*
1045 * 0 - ok (done)
1046 * 1 - continue
1047 * 2 - break
1048 * -1 - error
1049 */
1050static int
Radek Krejcicc217a62016-04-08 16:58:11 +02001051schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id,
Radek Krejcibdf92362016-04-08 14:43:34 +02001052 const struct lys_module *module, const char *mod_name, int mod_name_len,
1053 const struct lys_node **start)
1054{
1055 const struct lys_module *prefix_mod;
Radek Krejcicc217a62016-04-08 16:58:11 +02001056 int sh = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001057
1058 /* module check */
1059 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1060 if (!prefix_mod) {
1061 return -1;
1062 }
1063 if (prefix_mod != lys_node_module(sibling)) {
1064 return 1;
1065 }
1066
1067 /* check for shorthand cases - then 'start' does not change */
1068 if (sibling->parent && sibling->parent->nodetype == LYS_CHOICE && sibling->nodetype != LYS_CASE) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001069 if (*shorthand != -1) {
1070 *shorthand = *shorthand ? 0 : 1;
1071 }
1072 sh = 1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001073 }
1074
1075 /* the result node? */
1076 if (!id[0]) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001077 if (*shorthand == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001078 return 1;
1079 }
1080 return 0;
1081 }
1082
Radek Krejcicc217a62016-04-08 16:58:11 +02001083 if (!sh) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001084 /* move down the tree, if possible */
1085 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
1086 return -1;
1087 }
1088 *start = sibling->child;
1089 }
1090
1091 return 2;
1092}
1093
Michal Vasko3edeaf72016-02-11 13:17:43 +01001094/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1095int
1096resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module,
1097 const struct lys_node **ret)
1098{
1099 const char *name, *mod_name, *id;
1100 const struct lys_node *sibling;
1101 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001102 int8_t shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001103 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001104 const struct lys_module *start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001105
1106 assert(nodeid && (start || module) && !(start && module) && ret);
1107
1108 id = nodeid;
1109
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001110 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 +01001111 return ((id - nodeid) - r) + 1;
1112 }
1113 id += r;
1114
1115 if ((is_relative && !start) || (!is_relative && !module)) {
1116 return -1;
1117 }
1118
1119 /* descendant-schema-nodeid */
1120 if (is_relative) {
Michal Vasko4c06fd82016-02-17 13:43:38 +01001121 module = start_mod = start->module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001122
1123 /* absolute-schema-nodeid */
1124 } else {
1125 start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001126 if (!start_mod) {
1127 return -1;
1128 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001129 start = start_mod->data;
1130 }
1131
1132 while (1) {
1133 sibling = NULL;
1134 while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod,
1135 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
1136 /* name match */
1137 if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len])
Michal Vasko044697a2016-03-24 13:21:12 +01001138 || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5))
1139 || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001140
Radek Krejcibdf92362016-04-08 14:43:34 +02001141 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1142 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001143 *ret = sibling;
1144 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001145 } else if (r == 1) {
1146 continue;
1147 } else if (r == 2) {
1148 break;
1149 } else {
1150 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001151 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001152 }
1153 }
1154
1155 /* no match */
1156 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001157 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001158 return EXIT_SUCCESS;
1159 }
1160
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001161 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 +01001162 return ((id - nodeid) - r) + 1;
1163 }
1164 id += r;
1165 }
1166
1167 /* cannot get here */
1168 LOGINT;
1169 return -1;
1170}
1171
1172/* unique, refine, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1173int
1174resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Radek Krejcicc217a62016-04-08 16:58:11 +02001175 const struct lys_node **ret, int check_shorthand)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001176{
1177 const char *name, *mod_name, *id;
1178 const struct lys_node *sibling;
1179 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001180 int8_t shorthand = check_shorthand ? 0 : -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001181 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001182 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001183
1184 assert(nodeid && start && ret);
1185 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1186
1187 id = nodeid;
1188 module = start->module;
1189
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001190 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 +01001191 return ((id - nodeid) - r) + 1;
1192 }
1193 id += r;
1194
1195 if (!is_relative) {
1196 return -1;
1197 }
1198
1199 while (1) {
1200 sibling = NULL;
1201 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
1202 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
1203 /* name match */
1204 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
1205
Radek Krejcibdf92362016-04-08 14:43:34 +02001206 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1207 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001208 if (!(sibling->nodetype & ret_nodetype)) {
1209 /* wrong node type, too bad */
1210 continue;
1211 }
1212 *ret = sibling;
1213 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001214 } else if (r == 1) {
1215 continue;
1216 } else if (r == 2) {
1217 break;
1218 } else {
1219 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001220 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001221 }
1222 }
1223
1224 /* no match */
1225 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001226 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001227 return EXIT_SUCCESS;
1228 }
1229
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001230 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 +01001231 return ((id - nodeid) - r) + 1;
1232 }
1233 id += r;
1234 }
1235
1236 /* cannot get here */
1237 LOGINT;
1238 return -1;
1239}
1240
1241/* choice default */
1242int
1243resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1244{
1245 /* cannot actually be a path */
1246 if (strchr(nodeid, '/')) {
1247 return -1;
1248 }
1249
Radek Krejcicc217a62016-04-08 16:58:11 +02001250 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, ret, 1);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001251}
1252
1253/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1254static int
1255resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1256{
1257 const struct lys_module *module;
1258 const char *mod_prefix, *name;
1259 int i, mod_prefix_len, nam_len;
1260
1261 /* parse the identifier, it must be parsed on one call */
1262 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1263 return -i + 1;
1264 }
1265
1266 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1267 if (!module) {
1268 return -1;
1269 }
1270 if (module != start->module) {
1271 start = module->data;
1272 }
1273
1274 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1275
1276 return EXIT_SUCCESS;
1277}
1278
1279int
1280resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1281 const struct lys_node **ret)
1282{
1283 const char *name, *mod_name, *id;
1284 const struct lys_node *sibling, *start;
1285 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001286 int8_t shorthand = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001287 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001288
1289 assert(nodeid && module && ret);
1290 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1291
1292 id = nodeid;
1293 start = module->data;
1294
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001295 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 +01001296 return ((id - nodeid) - r) + 1;
1297 }
1298 id += r;
1299
1300 if (is_relative) {
1301 return -1;
1302 }
1303
1304 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001305 if (!abs_start_mod) {
1306 return -1;
1307 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001308
1309 while (1) {
1310 sibling = NULL;
1311 while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE
1312 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1313 /* name match */
Michal Vasko044697a2016-03-24 13:21:12 +01001314 if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len])
1315 || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5))
1316 || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001317
Radek Krejcibdf92362016-04-08 14:43:34 +02001318 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1319 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001320 if (!(sibling->nodetype & ret_nodetype)) {
1321 /* wrong node type, too bad */
1322 continue;
1323 }
1324 *ret = sibling;
1325 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001326 } else if (r == 1) {
1327 continue;
1328 } else if (r == 2) {
1329 break;
1330 } else {
1331 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001332 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001333 }
1334 }
1335
1336 /* no match */
1337 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001338 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001339 return EXIT_SUCCESS;
1340 }
1341
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001342 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 +01001343 return ((id - nodeid) - r) + 1;
1344 }
1345 id += r;
1346 }
1347
1348 /* cannot get here */
1349 LOGINT;
1350 return -1;
1351}
1352
Michal Vaskoe733d682016-03-14 09:08:27 +01001353static int
Michal Vasko3547c532016-03-14 09:40:50 +01001354resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001355{
1356 const char *name;
1357 int nam_len, has_predicate, i;
1358
Michal Vasko3547c532016-03-14 09:40:50 +01001359 if ((i = parse_schema_list_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001360 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001361 return -1;
1362 }
1363
1364 predicate += i;
1365 *parsed += i;
1366
1367 for (i = 0; i < list->keys_size; ++i) {
1368 if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) {
1369 break;
1370 }
1371 }
1372
1373 if (i == list->keys_size) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001374 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskoe733d682016-03-14 09:08:27 +01001375 return -1;
1376 }
1377
1378 /* more predicates? */
1379 if (has_predicate) {
Michal Vasko3547c532016-03-14 09:40:50 +01001380 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001381 }
1382
1383 return 0;
1384}
1385
Michal Vasko9fd98e22016-04-07 15:44:19 +02001386/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly
1387 * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */
Michal Vaskoe733d682016-03-14 09:08:27 +01001388const struct lys_node *
Michal Vasko9fd98e22016-04-07 15:44:19 +02001389resolve_json_schema_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001390{
Michal Vasko10728b52016-04-07 14:26:29 +02001391 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001392 const char *name, *mod_name, *id;
Michal Vasko3547c532016-03-14 09:40:50 +01001393 const struct lys_node *sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001394 int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001395 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskoe733d682016-03-14 09:08:27 +01001396 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001397
Michal Vasko3547c532016-03-14 09:40:50 +01001398 assert(nodeid && (ctx || start));
1399 if (!ctx) {
1400 ctx = start->module->ctx;
1401 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001402
1403 id = nodeid;
1404
Michal Vaskoe733d682016-03-14 09:08:27 +01001405 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 +01001406 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001407 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001408 }
1409 id += r;
1410
1411 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01001412 assert(start);
1413 start = start->child;
1414 if (!start) {
1415 /* no descendants, fail for sure */
Michal Vasko10728b52016-04-07 14:26:29 +02001416 str = strndup(nodeid, (name + nam_len) - nodeid);
1417 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1418 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001419 return NULL;
1420 }
1421 module = start->module;
1422 } else {
1423 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02001424 str = strndup(nodeid, (name + nam_len) - nodeid);
1425 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
1426 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001427 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02001428 } else if (mod_name_len > LY_BUF_SIZE - 1) {
1429 LOGINT;
1430 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01001431 }
1432
Michal Vasko971a3ca2016-04-01 13:09:29 +02001433 if (ly_buf_used && module_name[0]) {
1434 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1435 }
1436 ly_buf_used++;
1437
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001438 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001439 module_name[mod_name_len] = '\0';
1440 module = ly_ctx_get_module(ctx, module_name, NULL);
1441
1442 if (buf_backup) {
1443 /* return previous internal buffer content */
1444 strcpy(module_name, buf_backup);
1445 free(buf_backup);
1446 buf_backup = NULL;
1447 }
1448 ly_buf_used--;
1449
Michal Vasko3547c532016-03-14 09:40:50 +01001450 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02001451 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1452 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1453 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001454 return NULL;
1455 }
1456 start = module->data;
1457
1458 /* now it's as if there was no module name */
1459 mod_name = NULL;
1460 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01001461 }
1462
Michal Vaskoe733d682016-03-14 09:08:27 +01001463 prev_mod = module;
1464
Michal Vasko3edeaf72016-02-11 13:17:43 +01001465 while (1) {
1466 sibling = NULL;
Michal Vasko9fd98e22016-04-07 15:44:19 +02001467 while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ?
1468 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001469 /* name match */
Michal Vasko044697a2016-03-24 13:21:12 +01001470 if ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len])
1471 || ((sibling->nodetype == LYS_INPUT) && !strncmp(name, "input", nam_len) && (nam_len == 5))
1472 || ((sibling->nodetype == LYS_OUTPUT) && !strncmp(name, "output", nam_len) && (nam_len == 6))) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001473
1474 /* data RPC input/output check */
1475 if ((data_nodeid == 1) && sibling->parent && (sibling->parent->nodetype == LYS_OUTPUT)) {
1476 continue;
1477 } else if ((data_nodeid == 2) && sibling->parent && (sibling->parent->nodetype == LYS_INPUT)) {
1478 continue;
1479 }
1480
Michal Vasko3edeaf72016-02-11 13:17:43 +01001481 /* module check */
1482 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001483 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01001484 LOGINT;
1485 return NULL;
1486 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001487
1488 if (ly_buf_used && module_name[0]) {
1489 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1490 }
1491 ly_buf_used++;
1492
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001493 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01001494 module_name[mod_name_len] = '\0';
1495 /* will also find an augment module */
1496 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001497
1498 if (buf_backup) {
1499 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001500 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001501 free(buf_backup);
1502 buf_backup = NULL;
1503 }
1504 ly_buf_used--;
1505
Michal Vasko3edeaf72016-02-11 13:17:43 +01001506 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001507 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1508 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1509 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001510 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001511 }
1512 } else {
1513 prefix_mod = prev_mod;
1514 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01001515 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001516 continue;
1517 }
1518
Michal Vaskoe733d682016-03-14 09:08:27 +01001519 /* do we have some predicates on it? */
1520 if (has_predicate) {
1521 r = 0;
1522 if (sibling->nodetype != LYS_LIST) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001523 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01001524 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01001525 } else if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vaskoe733d682016-03-14 09:08:27 +01001526 return NULL;
1527 }
1528 id += r;
1529 }
1530
Radek Krejcibdf92362016-04-08 14:43:34 +02001531 /* check for shorthand cases - then 'start' does not change */
1532 if (sibling->parent && sibling->parent->nodetype == LYS_CHOICE && sibling->nodetype != LYS_CASE) {
1533 shorthand = ~shorthand;
1534 }
1535
Michal Vasko3edeaf72016-02-11 13:17:43 +01001536 /* the result node? */
1537 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001538 if (shorthand) {
1539 /* wrong path for shorthand */
1540 sibling = NULL;
1541 break;
1542 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001543 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001544 }
1545
Radek Krejcibdf92362016-04-08 14:43:34 +02001546 if (!shorthand) {
Michal Vasko7dc71d02016-03-15 10:42:28 +01001547 /* move down the tree, if possible */
1548 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001549 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko7dc71d02016-03-15 10:42:28 +01001550 return NULL;
1551 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001552 start = sibling->child;
1553 }
1554
1555 /* update prev mod */
1556 prev_mod = start->module;
1557 break;
1558 }
1559 }
1560
1561 /* no match */
1562 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02001563 str = strndup(nodeid, (name + nam_len) - nodeid);
1564 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1565 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001566 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001567 }
1568
Michal Vaskoe733d682016-03-14 09:08:27 +01001569 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 +01001570 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001571 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001572 }
1573 id += r;
1574 }
1575
1576 /* cannot get here */
1577 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01001578 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001579}
1580
Michal Vasko22448d32016-03-16 13:17:29 +01001581static int
1582resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed)
1583{
1584 const char *name, *value;
1585 int nam_len, val_len, has_predicate = 1, r;
1586 uint16_t i;
1587 struct ly_set *keys;
1588
Radek Krejci61a86c62016-03-24 11:06:44 +01001589 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01001590 assert(node->schema->nodetype == LYS_LIST);
1591
1592 keys = lyd_get_list_keys(node);
1593
1594 for (i = 0; i < keys->number; ++i) {
1595 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001596 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Radek Krejciba5cec62016-03-24 10:40:29 +01001597 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01001598 }
1599
1600 if ((r = parse_schema_list_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001601 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Radek Krejciba5cec62016-03-24 10:40:29 +01001602 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01001603 }
1604
1605 predicate += r;
1606 *parsed += r;
1607
Radek Krejci8f08df12016-03-21 11:11:30 +01001608 if (strncmp(keys->set.d[i]->schema->name, name, nam_len) || keys->set.d[i]->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001609 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Radek Krejciba5cec62016-03-24 10:40:29 +01001610 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01001611 }
1612
1613 /* value does not match */
Radek Krejci8f08df12016-03-21 11:11:30 +01001614 if (strncmp(((struct lyd_node_leaf_list *)keys->set.d[i])->value_str, value, val_len)
1615 || ((struct lyd_node_leaf_list *)keys->set.d[i])->value_str[val_len]) {
Radek Krejciba5cec62016-03-24 10:40:29 +01001616 ly_set_free(keys);
Michal Vasko22448d32016-03-16 13:17:29 +01001617 return 1;
1618 }
1619 }
1620
Radek Krejciba5cec62016-03-24 10:40:29 +01001621 ly_set_free(keys);
Michal Vasko22448d32016-03-16 13:17:29 +01001622 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001623 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01001624 return -1;
1625 }
1626
1627 return 0;
Radek Krejciba5cec62016-03-24 10:40:29 +01001628
1629error:
1630 ly_set_free(keys);
1631 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001632}
1633
1634struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001635resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
1636 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01001637{
Michal Vasko10728b52016-04-07 14:26:29 +02001638 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko22448d32016-03-16 13:17:29 +01001639 const char *id, *mod_name, *name;
1640 int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001641 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001642 struct lyd_node_leaf_list *llist;
Michal Vasko22448d32016-03-16 13:17:29 +01001643 const struct lys_module *prefix_mod, *prev_mod;
1644 struct ly_ctx *ctx;
1645
1646 assert(nodeid && start && parsed);
1647
1648 ctx = start->schema->module->ctx;
1649 id = nodeid;
1650
1651 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 +01001652 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001653 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001654 return NULL;
1655 }
1656 id += r;
1657 /* add it to parsed only after the data node was actually found */
1658 last_parsed = r;
1659
1660 if (is_relative) {
1661 prev_mod = start->schema->module;
Michal Vasko22448d32016-03-16 13:17:29 +01001662 start = start->child;
1663 } else {
1664 for (; start->parent; start = start->parent);
Michal Vasko22448d32016-03-16 13:17:29 +01001665 prev_mod = start->schema->module;
1666 }
1667
1668 while (1) {
1669 LY_TREE_FOR(start, sibling) {
Michal Vasko2411b942016-03-23 13:50:03 +01001670 /* RPC data check, return simply invalid argument, because the data tree is invalid */
1671 if (lys_parent(sibling->schema)) {
1672 if (options & LYD_PATH_OPT_OUTPUT) {
1673 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vasko9ffd8992016-03-24 13:42:29 +01001674 LOGERR(LY_EINVAL, "%s: provided data tree includes some RPC input nodes.");
Michal Vasko2411b942016-03-23 13:50:03 +01001675 *parsed = -1;
1676 return NULL;
1677 }
1678 } else {
1679 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vasko9ffd8992016-03-24 13:42:29 +01001680 LOGERR(LY_EINVAL, "%s: provided data tree includes some RPC output nodes.");
Michal Vasko2411b942016-03-23 13:50:03 +01001681 *parsed = -1;
1682 return NULL;
1683 }
1684 }
1685 }
1686
Michal Vasko22448d32016-03-16 13:17:29 +01001687 /* name match */
1688 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
1689
1690 /* module check */
1691 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001692 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01001693 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001694 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001695 return NULL;
1696 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001697
1698 if (ly_buf_used && module_name[0]) {
1699 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1700 }
1701 ly_buf_used++;
1702
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001703 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01001704 module_name[mod_name_len] = '\0';
1705 /* will also find an augment module */
1706 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001707
1708 if (buf_backup) {
1709 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001710 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001711 free(buf_backup);
1712 buf_backup = NULL;
1713 }
1714 ly_buf_used--;
1715
Michal Vasko22448d32016-03-16 13:17:29 +01001716 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001717 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1718 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1719 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001720 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001721 return NULL;
1722 }
1723 } else {
1724 prefix_mod = prev_mod;
1725 }
1726 if (prefix_mod != lys_node_module(sibling->schema)) {
1727 continue;
1728 }
1729
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001730 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01001731 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001732 llist = (struct lyd_node_leaf_list *)sibling;
1733 if ((!llist_value && llist->value_str && llist->value_str[0])
1734 || (llist_value && strcmp(llist_value, llist->value_str))) {
1735 continue;
1736 }
Michal Vasko22448d32016-03-16 13:17:29 +01001737 }
1738
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001739 /* list, we need predicates'n'stuff then */
Michal Vasko22448d32016-03-16 13:17:29 +01001740 if (sibling->schema->nodetype == LYS_LIST) {
1741 r = 0;
1742 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001743 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001744 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001745 return NULL;
1746 }
1747 ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r);
1748 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01001749 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001750 return NULL;
1751 } else if (ret == 1) {
1752 /* this list instance does not match */
1753 continue;
1754 }
1755 id += r;
1756 last_parsed += r;
1757 }
1758
1759 *parsed += last_parsed;
1760
1761 /* the result node? */
1762 if (!id[0]) {
1763 return sibling;
1764 }
1765
1766 /* move down the tree, if possible */
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02001767 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001768 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001769 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001770 return NULL;
1771 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02001772 last_match = sibling;
Michal Vasko22448d32016-03-16 13:17:29 +01001773 start = sibling->child;
1774 if (start) {
1775 prev_mod = start->schema->module;
1776 }
1777 break;
1778 }
1779 }
1780
1781 /* no match, return last match */
1782 if (!sibling) {
1783 return last_match;
1784 }
1785
1786 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 +01001787 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001788 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001789 return NULL;
1790 }
1791 id += r;
1792 last_parsed = r;
1793 }
1794
1795 /* cannot get here */
1796 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001797 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001798 return NULL;
1799}
1800
Michal Vasko3edeaf72016-02-11 13:17:43 +01001801/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02001802 * @brief Resolves length or range intervals. Does not log.
1803 * Syntax is assumed to be correct, *local_intv MUST be NULL.
1804 *
1805 * @param[in] str_restr The restriction as a string.
1806 * @param[in] type The type of the restriction.
1807 * @param[in] superior_restr Flag whether to check superior
1808 * types.
1809 * @param[out] local_intv The final interval structure.
1810 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001811 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02001812 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001813int
Michal Vasko23b61ec2015-08-19 11:19:50 +02001814resolve_len_ran_interval(const char *str_restr, struct lys_type *type, int superior_restr,
1815 struct len_ran_intv** local_intv)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001816{
1817 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001818 int kind, rc = EXIT_SUCCESS;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001819 int64_t local_smin, local_smax;
1820 uint64_t local_umin, local_umax;
1821 long double local_fmin, local_fmax;
1822 const char *seg_ptr, *ptr;
Michal Vaskoe01eca52015-08-13 14:42:02 +02001823 struct len_ran_intv *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001824
1825 switch (type->base) {
1826 case LY_TYPE_BINARY:
1827 kind = 0;
1828 local_umin = 0;
1829 local_umax = 18446744073709551615UL;
1830
1831 if (!str_restr && type->info.binary.length) {
1832 str_restr = type->info.binary.length->expr;
1833 }
1834 break;
1835 case LY_TYPE_DEC64:
1836 kind = 2;
1837 local_fmin = -9223372036854775808.0;
1838 local_fmin /= 1 << type->info.dec64.dig;
1839 local_fmax = 9223372036854775807.0;
1840 local_fmax /= 1 << type->info.dec64.dig;
1841
1842 if (!str_restr && type->info.dec64.range) {
1843 str_restr = type->info.dec64.range->expr;
1844 }
1845 break;
1846 case LY_TYPE_INT8:
1847 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001848 local_smin = __INT64_C(-128);
1849 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001850
1851 if (!str_restr && type->info.num.range) {
1852 str_restr = type->info.num.range->expr;
1853 }
1854 break;
1855 case LY_TYPE_INT16:
1856 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001857 local_smin = __INT64_C(-32768);
1858 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001859
1860 if (!str_restr && type->info.num.range) {
1861 str_restr = type->info.num.range->expr;
1862 }
1863 break;
1864 case LY_TYPE_INT32:
1865 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001866 local_smin = __INT64_C(-2147483648);
1867 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001868
1869 if (!str_restr && type->info.num.range) {
1870 str_restr = type->info.num.range->expr;
1871 }
1872 break;
1873 case LY_TYPE_INT64:
1874 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001875 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
1876 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001877
1878 if (!str_restr && type->info.num.range) {
1879 str_restr = type->info.num.range->expr;
1880 }
1881 break;
1882 case LY_TYPE_UINT8:
1883 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001884 local_umin = __UINT64_C(0);
1885 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001886
1887 if (!str_restr && type->info.num.range) {
1888 str_restr = type->info.num.range->expr;
1889 }
1890 break;
1891 case LY_TYPE_UINT16:
1892 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001893 local_umin = __UINT64_C(0);
1894 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001895
1896 if (!str_restr && type->info.num.range) {
1897 str_restr = type->info.num.range->expr;
1898 }
1899 break;
1900 case LY_TYPE_UINT32:
1901 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001902 local_umin = __UINT64_C(0);
1903 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001904
1905 if (!str_restr && type->info.num.range) {
1906 str_restr = type->info.num.range->expr;
1907 }
1908 break;
1909 case LY_TYPE_UINT64:
1910 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001911 local_umin = __UINT64_C(0);
1912 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001913
1914 if (!str_restr && type->info.num.range) {
1915 str_restr = type->info.num.range->expr;
1916 }
1917 break;
1918 case LY_TYPE_STRING:
1919 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001920 local_umin = __UINT64_C(0);
1921 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001922
1923 if (!str_restr && type->info.str.length) {
1924 str_restr = type->info.str.length->expr;
1925 }
1926 break;
1927 default:
1928 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001929 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001930 }
1931
1932 /* process superior types */
1933 if (type->der && superior_restr) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02001934 if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) {
1935 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001936 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02001937 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001938 assert(!intv || (intv->kind == kind));
1939 }
1940
1941 if (!str_restr) {
1942 /* we are validating data and not have any restriction, but a superior type might have */
1943 if (type->der && !superior_restr && !intv) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02001944 if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) {
1945 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001946 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02001947 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001948 assert(!intv || (intv->kind == kind));
1949 }
1950 *local_intv = intv;
1951 return EXIT_SUCCESS;
1952 }
1953
1954 /* adjust local min and max */
1955 if (intv) {
1956 tmp_intv = intv;
1957
1958 if (kind == 0) {
1959 local_umin = tmp_intv->value.uval.min;
1960 } else if (kind == 1) {
1961 local_smin = tmp_intv->value.sval.min;
1962 } else if (kind == 2) {
1963 local_fmin = tmp_intv->value.fval.min;
1964 }
1965
1966 while (tmp_intv->next) {
1967 tmp_intv = tmp_intv->next;
1968 }
1969
1970 if (kind == 0) {
1971 local_umax = tmp_intv->value.uval.max;
1972 } else if (kind == 1) {
1973 local_smax = tmp_intv->value.sval.max;
1974 } else if (kind == 2) {
1975 local_fmax = tmp_intv->value.fval.max;
1976 }
1977 }
1978
1979 /* finally parse our restriction */
1980 seg_ptr = str_restr;
1981 while (1) {
Michal Vaskoe01eca52015-08-13 14:42:02 +02001982 if (!*local_intv && !tmp_local_intv) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001983 *local_intv = malloc(sizeof **local_intv);
1984 tmp_local_intv = *local_intv;
1985 } else {
1986 tmp_local_intv->next = malloc(sizeof **local_intv);
1987 tmp_local_intv = tmp_local_intv->next;
1988 }
Michal Vasko253035f2015-12-17 16:58:13 +01001989 if (!tmp_local_intv) {
1990 LOGMEM;
1991 return -1;
1992 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001993
1994 tmp_local_intv->kind = kind;
1995 tmp_local_intv->next = NULL;
1996
1997 /* min */
1998 ptr = seg_ptr;
1999 while (isspace(ptr[0])) {
2000 ++ptr;
2001 }
2002 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2003 if (kind == 0) {
2004 tmp_local_intv->value.uval.min = atoll(ptr);
2005 } else if (kind == 1) {
2006 tmp_local_intv->value.sval.min = atoll(ptr);
2007 } else if (kind == 2) {
2008 tmp_local_intv->value.fval.min = atoll(ptr);
2009 }
2010
2011 if ((ptr[0] == '+') || (ptr[0] == '-')) {
2012 ++ptr;
2013 }
2014 while (isdigit(ptr[0])) {
2015 ++ptr;
2016 }
2017 } else if (!strncmp(ptr, "min", 3)) {
2018 if (kind == 0) {
2019 tmp_local_intv->value.uval.min = local_umin;
2020 } else if (kind == 1) {
2021 tmp_local_intv->value.sval.min = local_smin;
2022 } else if (kind == 2) {
2023 tmp_local_intv->value.fval.min = local_fmin;
2024 }
2025
2026 ptr += 3;
2027 } else if (!strncmp(ptr, "max", 3)) {
2028 if (kind == 0) {
2029 tmp_local_intv->value.uval.min = local_umax;
2030 } else if (kind == 1) {
2031 tmp_local_intv->value.sval.min = local_smax;
2032 } else if (kind == 2) {
2033 tmp_local_intv->value.fval.min = local_fmax;
2034 }
2035
2036 ptr += 3;
2037 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002038 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002039 rc = -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002040 goto cleanup;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002041 }
2042
2043 while (isspace(ptr[0])) {
2044 ptr++;
2045 }
2046
2047 /* no interval or interval */
2048 if ((ptr[0] == '|') || !ptr[0]) {
2049 if (kind == 0) {
2050 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2051 } else if (kind == 1) {
2052 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2053 } else if (kind == 2) {
2054 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2055 }
2056 } else if (!strncmp(ptr, "..", 2)) {
2057 /* skip ".." */
2058 ptr += 2;
2059 while (isspace(ptr[0])) {
2060 ++ptr;
2061 }
2062
2063 /* max */
2064 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2065 if (kind == 0) {
2066 tmp_local_intv->value.uval.max = atoll(ptr);
2067 } else if (kind == 1) {
2068 tmp_local_intv->value.sval.max = atoll(ptr);
2069 } else if (kind == 2) {
2070 tmp_local_intv->value.fval.max = atoll(ptr);
2071 }
2072 } else if (!strncmp(ptr, "max", 3)) {
2073 if (kind == 0) {
2074 tmp_local_intv->value.uval.max = local_umax;
2075 } else if (kind == 1) {
2076 tmp_local_intv->value.sval.max = local_smax;
2077 } else if (kind == 2) {
2078 tmp_local_intv->value.fval.max = local_fmax;
2079 }
2080 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002081 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002082 rc = -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002083 goto cleanup;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002084 }
2085 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002086 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002087 rc = -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002088 goto cleanup;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002089 }
2090
2091 /* next segment (next OR) */
2092 seg_ptr = strchr(seg_ptr, '|');
2093 if (!seg_ptr) {
2094 break;
2095 }
2096 seg_ptr++;
2097 }
2098
2099 /* check local restrictions against superior ones */
2100 if (intv) {
2101 tmp_intv = intv;
2102 tmp_local_intv = *local_intv;
2103
2104 while (tmp_local_intv && tmp_intv) {
2105 /* reuse local variables */
2106 if (kind == 0) {
2107 local_umin = tmp_local_intv->value.uval.min;
2108 local_umax = tmp_local_intv->value.uval.max;
2109
2110 /* it must be in this interval */
2111 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2112 /* this interval is covered, next one */
2113 if (local_umax <= tmp_intv->value.uval.max) {
2114 tmp_local_intv = tmp_local_intv->next;
2115 continue;
2116 /* ascending order of restrictions -> fail */
2117 } else {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002118 rc = -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002119 goto cleanup;
2120 }
2121 }
2122 } else if (kind == 1) {
2123 local_smin = tmp_local_intv->value.sval.min;
2124 local_smax = tmp_local_intv->value.sval.max;
2125
2126 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2127 if (local_smax <= tmp_intv->value.sval.max) {
2128 tmp_local_intv = tmp_local_intv->next;
2129 continue;
2130 } else {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002131 rc = -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002132 goto cleanup;
2133 }
2134 }
2135 } else if (kind == 2) {
2136 local_fmin = tmp_local_intv->value.fval.min;
2137 local_fmax = tmp_local_intv->value.fval.max;
2138
2139 if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) {
2140 if (local_fmax <= tmp_intv->value.fval.max) {
2141 tmp_local_intv = tmp_local_intv->next;
2142 continue;
2143 } else {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002144 rc = -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002145 goto cleanup;
2146 }
2147 }
2148 }
2149
2150 tmp_intv = tmp_intv->next;
2151 }
2152
2153 /* some interval left uncovered -> fail */
2154 if (tmp_local_intv) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002155 rc = -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002156 }
2157
2158 }
2159
2160cleanup:
2161 while (intv) {
2162 tmp_intv = intv->next;
2163 free(intv);
2164 intv = tmp_intv;
2165 }
2166
2167 /* fail */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002168 if (rc) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002169 while (*local_intv) {
2170 tmp_local_intv = (*local_intv)->next;
2171 free(*local_intv);
2172 *local_intv = tmp_local_intv;
2173 }
2174 }
2175
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002176 return rc;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002177}
2178
Michal Vasko730dfdf2015-08-11 14:48:05 +02002179/**
Michal Vasko88c29542015-11-27 14:57:53 +01002180 * @brief Resolve a typedef, return only resolved typedefs if derived. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002181 *
2182 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002183 * @param[in] mod_name Typedef name module name.
2184 * @param[in] module Main module.
2185 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002186 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002187 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002188 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002189 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002190int
Michal Vasko1e62a092015-12-01 12:27:20 +01002191resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2192 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002193{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002194 int i, j;
Radek Krejci1574a8d2015-08-03 14:16:52 +02002195 struct lys_tpdf *tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002196 int tpdf_size;
2197
Michal Vasko1dca6882015-10-22 14:29:42 +02002198 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002199 /* no prefix, try built-in types */
2200 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
2201 if (!strcmp(ly_types[i].def->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002202 if (ret) {
2203 *ret = ly_types[i].def;
2204 }
2205 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002206 }
2207 }
2208 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002209 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002210 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002211 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002212 }
2213 }
2214
Michal Vasko1dca6882015-10-22 14:29:42 +02002215 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002216 /* search in local typedefs */
2217 while (parent) {
2218 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002219 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002220 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2221 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002222 break;
2223
Radek Krejci76512572015-08-04 09:47:08 +02002224 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002225 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2226 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002227 break;
2228
Radek Krejci76512572015-08-04 09:47:08 +02002229 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002230 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2231 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002232 break;
2233
Radek Krejci76512572015-08-04 09:47:08 +02002234 case LYS_RPC:
Radek Krejcib8048692015-08-05 13:36:34 +02002235 tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size;
2236 tpdf = ((struct lys_node_rpc *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002237 break;
2238
Radek Krejci76512572015-08-04 09:47:08 +02002239 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002240 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2241 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002242 break;
2243
Radek Krejci76512572015-08-04 09:47:08 +02002244 case LYS_INPUT:
2245 case LYS_OUTPUT:
Radek Krejci4608ada2015-08-05 16:04:37 +02002246 tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size;
2247 tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002248 break;
2249
2250 default:
2251 parent = parent->parent;
2252 continue;
2253 }
2254
2255 for (i = 0; i < tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002256 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002257 if (ret) {
2258 *ret = &tpdf[i];
2259 }
2260 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002261 }
2262 }
2263
2264 parent = parent->parent;
2265 }
Radek Krejcic071c542016-01-27 14:57:51 +01002266 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002267 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002268 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002269 if (!module) {
2270 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002271 }
2272 }
2273
2274 /* search in top level typedefs */
2275 for (i = 0; i < module->tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002276 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002277 if (ret) {
2278 *ret = &module->tpdf[i];
2279 }
2280 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002281 }
2282 }
2283
2284 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01002285 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002286 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002287 if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002288 if (ret) {
2289 *ret = &module->inc[i].submodule->tpdf[j];
2290 }
2291 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002292 }
2293 }
2294 }
2295
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002296 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002297}
2298
Michal Vasko1dca6882015-10-22 14:29:42 +02002299/**
2300 * @brief Check the default \p value of the \p type. Logs directly.
2301 *
2302 * @param[in] type Type definition to use.
2303 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01002304 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02002305 *
2306 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
2307 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002308static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002309check_default(struct lys_type *type, const char *value, struct lys_module *module)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002310{
Michal Vasko1dca6882015-10-22 14:29:42 +02002311 struct lyd_node_leaf_list node;
Radek Krejci37b756f2016-01-18 10:15:03 +01002312 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02002313
2314 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01002315 memset(&node, 0, sizeof node);
Michal Vasko1dca6882015-10-22 14:29:42 +02002316 node.value_str = value;
2317 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01002318 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko253035f2015-12-17 16:58:13 +01002319 if (!node.schema) {
2320 LOGMEM;
2321 return -1;
2322 }
Michal Vasko1dca6882015-10-22 14:29:42 +02002323 node.schema->name = strdup("default");
Michal Vasko253035f2015-12-17 16:58:13 +01002324 if (!node.schema->name) {
2325 LOGMEM;
2326 return -1;
2327 }
Michal Vasko56826402016-03-02 11:11:37 +01002328 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01002329 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02002330
Radek Krejci37b756f2016-01-18 10:15:03 +01002331 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02002332 if (!type->info.lref.target) {
2333 ret = EXIT_FAILURE;
2334 goto finish;
2335 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002336 ret = check_default(&type->info.lref.target->type, value, module);
Michal Vasko1dca6882015-10-22 14:29:42 +02002337
2338 } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) {
2339 /* it was converted to JSON format before, nothing else sensible we can do */
2340
2341 } else {
Radek Krejci0b7704f2016-03-18 12:16:14 +01002342 ret = lyp_parse_value(&node, NULL, 1);
Michal Vasko1dca6882015-10-22 14:29:42 +02002343 }
2344
2345finish:
2346 if (node.value_type == LY_TYPE_BITS) {
2347 free(node.value.bit);
2348 }
2349 free((char *)node.schema->name);
2350 free(node.schema);
2351
2352 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002353}
2354
Michal Vasko730dfdf2015-08-11 14:48:05 +02002355/**
2356 * @brief Check a key for mandatory attributes. Logs directly.
2357 *
2358 * @param[in] key The key to check.
2359 * @param[in] flags What flags to check.
2360 * @param[in] list The list of all the keys.
2361 * @param[in] index Index of the key in the key list.
2362 * @param[in] name The name of the keys.
2363 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002364 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002365 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002366 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002367static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002368check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002369{
Radek Krejciadb57612016-02-16 13:34:34 +01002370 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002371 char *dup = NULL;
2372 int j;
2373
2374 /* existence */
2375 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02002376 if (name[len] != '\0') {
2377 dup = strdup(name);
Michal Vasko253035f2015-12-17 16:58:13 +01002378 if (!dup) {
2379 LOGMEM;
2380 return -1;
2381 }
Michal Vaskof02e3742015-08-05 16:27:02 +02002382 dup[len] = '\0';
2383 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002384 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002385 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002386 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002387 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002388 }
2389
2390 /* uniqueness */
2391 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01002392 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002393 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002394 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002395 }
2396 }
2397
2398 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02002399 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002400 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002401 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002402 }
2403
2404 /* type of the leaf is not built-in empty */
2405 if (key->type.base == LY_TYPE_EMPTY) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002406 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002407 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002408 }
2409
2410 /* config attribute is the same as of the list */
Radek Krejciadb57612016-02-16 13:34:34 +01002411 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002412 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002413 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002414 }
2415
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002416 /* key is not placed from augment */
2417 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002418 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
2419 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002420 return -1;
2421 }
2422
Michal Vaskocca47842016-03-17 10:31:07 +01002423 /* key is not when-conditional */
2424 if (key->when) {
Radek Krejci02a04992016-03-17 16:06:37 +01002425 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, "when", "leaf");
2426 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"when\" condition.");
Radek Krejci581ce772015-11-10 17:22:40 +01002427 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02002428 }
2429
Michal Vasko0b85aa82016-03-07 14:37:43 +01002430 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02002431}
Michal Vasko730dfdf2015-08-11 14:48:05 +02002432
2433/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002434 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002435 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002436 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01002437 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002438 *
2439 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
2440 */
2441int
Radek Krejci48464ed2016-03-17 15:44:09 +01002442resolve_unique(struct lys_node *parent, const char *uniq_str_path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002443{
Radek Krejci581ce772015-11-10 17:22:40 +01002444 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01002445 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002446
Radek Krejcicc217a62016-04-08 16:58:11 +02002447 rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, &leaf, 1);
Michal Vasko9bb061b2016-02-12 11:00:19 +01002448 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01002449 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002450 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002451 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002452 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejci581ce772015-11-10 17:22:40 +01002453 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01002454 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01002455 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002456 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
2457 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002458 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002459 }
Radek Krejci581ce772015-11-10 17:22:40 +01002460 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002461 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01002462 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002463 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
2464 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf.");
Radek Krejci581ce772015-11-10 17:22:40 +01002465 rc = -1;
2466 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002467 }
2468
Radek Krejcicf509982015-12-15 09:22:44 +01002469 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01002470 if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01002471 return -1;
2472 }
2473
Radek Krejcica7efb72016-01-18 13:06:01 +01002474 /* set leaf's unique flag */
2475 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
2476
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002477 return EXIT_SUCCESS;
2478
2479error:
2480
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002481 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002482}
2483
Michal Vasko730dfdf2015-08-11 14:48:05 +02002484/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002485 * @brief Resolve (find) a feature definition. Logs directly.
2486 *
2487 * @param[in] name Feature name.
2488 * @param[in] module Module to search in.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002489 * @param[out] ret Pointer to the resolved feature. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002490 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002491 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002492 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002493static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002494resolve_feature(const char *id, const struct lys_module *module, struct lys_feature **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002495{
Michal Vasko2d851a92015-10-20 16:16:36 +02002496 const char *mod_name, *name;
Michal Vasko2d851a92015-10-20 16:16:36 +02002497 int mod_name_len, nam_len, i, j;
Radek Krejcicf509982015-12-15 09:22:44 +01002498 struct lys_node *node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002499
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002500 assert(id);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002501 assert(module);
2502
2503 /* check prefix */
Michal Vasko2d851a92015-10-20 16:16:36 +02002504 if ((i = parse_node_identifier(id, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002505 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002506 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002507 }
2508
Radek Krejcic071c542016-01-27 14:57:51 +01002509 module = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
2510 if (!module) {
2511 /* identity refers unknown data model */
Radek Krejci48464ed2016-03-17 15:44:09 +01002512 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Radek Krejcic071c542016-01-27 14:57:51 +01002513 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002514 }
2515
Radek Krejcic071c542016-01-27 14:57:51 +01002516 /* search in the identified module ... */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002517 for (j = 0; j < module->features_size; j++) {
2518 if (!strcmp(name, module->features[j].name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002519 if (ret) {
Radek Krejcicf509982015-12-15 09:22:44 +01002520 /* check status */
2521 node = (struct lys_node *)*ret;
Radek Krejcic6556022016-01-27 15:16:45 +01002522 if (lyp_check_status(node->flags, node->module, node->name, module->features[j].flags,
Radek Krejci48464ed2016-03-17 15:44:09 +01002523 module->features[j].module, module->features[j].name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01002524 return -1;
2525 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002526 *ret = &module->features[j];
2527 }
2528 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002529 }
2530 }
Radek Krejcic071c542016-01-27 14:57:51 +01002531 /* ... and all its submodules */
Michal Vasko27ab8222016-02-12 09:33:52 +01002532 for (i = 0; i < module->inc_size; i++) {
2533 if (!module->inc[i].submodule) {
2534 /* not yet resolved */
2535 continue;
2536 }
Radek Krejcic071c542016-01-27 14:57:51 +01002537 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
2538 if (!strcmp(name, module->inc[i].submodule->features[j].name)) {
2539 if (ret) {
2540 /* check status */
2541 node = (struct lys_node *)*ret;
Radek Krejcic6556022016-01-27 15:16:45 +01002542 if (lyp_check_status(node->flags, node->module, node->name,
Radek Krejcic071c542016-01-27 14:57:51 +01002543 module->inc[i].submodule->features[j].flags,
2544 module->inc[i].submodule->features[j].module,
Radek Krejci48464ed2016-03-17 15:44:09 +01002545 module->inc[i].submodule->features[j].name, node)) {
Radek Krejcic071c542016-01-27 14:57:51 +01002546 return -1;
2547 }
2548 *ret = &(module->inc[i].submodule->features[j]);
2549 }
2550 return EXIT_SUCCESS;
2551 }
2552 }
2553 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002554
2555 /* not found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002556 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", id);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002557 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002558}
2559
Radek Krejci0c0086a2016-03-24 15:20:28 +01002560void
Michal Vasko23b61ec2015-08-19 11:19:50 +02002561unres_data_del(struct unres_data *unres, uint32_t i)
2562{
2563 /* there are items after the one deleted */
2564 if (i+1 < unres->count) {
2565 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02002566 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002567
2568 /* deleting the last item */
2569 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02002570 free(unres->node);
2571 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002572 }
2573
2574 /* if there are no items after and it is not the last one, just move the counter */
2575 --unres->count;
2576}
2577
Michal Vasko0491ab32015-08-19 14:28:29 +02002578/**
2579 * @brief Resolve (find) a data node from a specific module. Does not log.
2580 *
2581 * @param[in] mod Module to search in.
2582 * @param[in] name Name of the data node.
2583 * @param[in] nam_len Length of the name.
2584 * @param[in] start Data node to start the search from.
2585 * @param[in,out] parents Resolved nodes. If there are some parents,
2586 * they are replaced (!!) with the resolvents.
2587 *
2588 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference.
2589 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002590static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002591resolve_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 +02002592{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002593 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02002594 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002595 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002596
Michal Vasko23b61ec2015-08-19 11:19:50 +02002597 if (!parents->count) {
2598 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002599 parents->node = malloc(sizeof *parents->node);
Michal Vasko253035f2015-12-17 16:58:13 +01002600 if (!parents->node) {
2601 LOGMEM;
2602 return EXIT_FAILURE;
2603 }
Michal Vaskocf024702015-10-08 15:01:42 +02002604 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002605 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002606 for (i = 0; i < parents->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02002607 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002608 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002609 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002610 continue;
2611 }
2612 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02002613 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002614 if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len)
2615 && node->schema->name[nam_len] == '\0') {
2616 /* matching target */
2617 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02002618 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02002619 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002620 flag = 1;
2621 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02002622 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002623 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01002624 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
2625 if (!parents->node) {
2626 return EXIT_FAILURE;
2627 }
Michal Vaskocf024702015-10-08 15:01:42 +02002628 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002629 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002630 }
2631 }
2632 }
Radek Krejcic5090c32015-08-12 09:46:19 +02002633
2634 if (!flag) {
2635 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002636 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02002637 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002638 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02002639 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002640 }
2641
Michal Vasko0491ab32015-08-19 14:28:29 +02002642 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02002643}
2644
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002645/**
2646 * @brief Resolve (find) a data node. Does not log.
2647 *
Radek Krejci581ce772015-11-10 17:22:40 +01002648 * @param[in] mod_name Module name of the data node.
2649 * @param[in] mod_name_len Length of the module name.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002650 * @param[in] name Name of the data node.
2651 * @param[in] nam_len Length of the name.
2652 * @param[in] start Data node to start the search from.
2653 * @param[in,out] parents Resolved nodes. If there are some parents,
2654 * they are replaced (!!) with the resolvents.
2655 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002656 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002657 */
Radek Krejcic5090c32015-08-12 09:46:19 +02002658static int
Radek Krejci581ce772015-11-10 17:22:40 +01002659resolve_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 +02002660 struct unres_data *parents)
Radek Krejcic5090c32015-08-12 09:46:19 +02002661{
Michal Vasko1e62a092015-12-01 12:27:20 +01002662 const struct lys_module *mod;
Michal Vasko31fc3672015-10-21 12:08:13 +02002663 char *str;
Radek Krejcic5090c32015-08-12 09:46:19 +02002664
Michal Vasko23b61ec2015-08-19 11:19:50 +02002665 assert(start);
2666
Michal Vasko31fc3672015-10-21 12:08:13 +02002667 if (mod_name) {
2668 /* we have mod_name, find appropriate module */
2669 str = strndup(mod_name, mod_name_len);
Michal Vasko253035f2015-12-17 16:58:13 +01002670 if (!str) {
2671 LOGMEM;
2672 return -1;
2673 }
Michal Vasko31fc3672015-10-21 12:08:13 +02002674 mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL);
2675 free(str);
Radek Krejcic5090c32015-08-12 09:46:19 +02002676 if (!mod) {
2677 /* invalid prefix */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002678 return -1;
Radek Krejcic5090c32015-08-12 09:46:19 +02002679 }
2680 } else {
2681 /* no prefix, module is the same as of current node */
2682 mod = start->schema->module;
2683 }
2684
2685 return resolve_data(mod, name, name_len, start, parents);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002686}
2687
Michal Vasko730dfdf2015-08-11 14:48:05 +02002688/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002689 * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly
Radek Krejci48464ed2016-03-17 15:44:09 +01002690 * only specific errors, general no-resolvent error is left to the caller.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002691 *
Michal Vaskobb211122015-08-19 14:03:11 +02002692 * @param[in] pred Predicate to use.
Radek Krejciadb57612016-02-16 13:34:34 +01002693 * @param[in] node Node from which the predicate is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02002694 * @param[in,out] node_match Nodes satisfying the restriction
2695 * without the predicate. Nodes not
2696 * satisfying the predicate are removed.
Michal Vasko0491ab32015-08-19 14:28:29 +02002697 * @param[out] parsed Number of characters parsed, negative on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002698 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002699 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002700 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002701static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002702resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match,
Radek Krejci010e54b2016-03-15 09:40:34 +01002703 int *parsed)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002704{
Michal Vasko730dfdf2015-08-11 14:48:05 +02002705 /* ... /node[source = destination] ... */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002706 struct unres_data source_match, dest_match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002707 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vasko0491ab32015-08-19 14:28:29 +02002708 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0;
2709 int has_predicate, dest_parent_times, i, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002710 uint32_t j;
2711
2712 source_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002713 source_match.node = malloc(sizeof *source_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01002714 if (!source_match.node) {
2715 LOGMEM;
2716 return -1;
2717 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002718 dest_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002719 dest_match.node = malloc(sizeof *dest_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01002720 if (!dest_match.node) {
2721 LOGMEM;
2722 return -1;
2723 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002724
2725 do {
2726 if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
2727 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002728 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002729 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002730 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002731 }
Michal Vasko0491ab32015-08-19 14:28:29 +02002732 parsed_loc += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002733 pred += i;
2734
Michal Vasko23b61ec2015-08-19 11:19:50 +02002735 for (j = 0; j < node_match->count;) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002736 /* source */
Michal Vaskocf024702015-10-08 15:01:42 +02002737 source_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02002738
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002739 /* must be leaf (key of a list) */
Radek Krejci581ce772015-11-10 17:22:40 +01002740 if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j],
Michal Vaskocf024702015-10-08 15:01:42 +02002741 &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002742 i = 0;
2743 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002744 }
2745
2746 /* destination */
Michal Vaskocf024702015-10-08 15:01:42 +02002747 dest_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02002748 dest_parent_times = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002749 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
2750 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002751 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002752 rc = -1;
2753 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002754 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002755 pke_parsed = i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002756 for (i = 0; i < dest_parent_times; ++i) {
Michal Vaskocf024702015-10-08 15:01:42 +02002757 dest_match.node[0] = dest_match.node[0]->parent;
2758 if (!dest_match.node[0]) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002759 i = 0;
Michal Vasko0491ab32015-08-19 14:28:29 +02002760 rc = EXIT_FAILURE;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002761 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002762 }
2763 }
2764 while (1) {
Radek Krejci581ce772015-11-10 17:22:40 +01002765 if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0],
Michal Vasko0491ab32015-08-19 14:28:29 +02002766 &dest_match)) || (dest_match.count != 1)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002767 i = 0;
2768 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002769 }
2770
2771 if (pke_len == pke_parsed) {
2772 break;
2773 }
Michal Vasko1f76a282015-08-04 16:16:53 +02002774 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 +02002775 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002776 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002777 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002778 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002779 }
2780 pke_parsed += i;
2781 }
2782
2783 /* check match between source and destination nodes */
Michal Vaskocf024702015-10-08 15:01:42 +02002784 if (((struct lys_node_leaf *)source_match.node[0]->schema)->type.base
2785 != ((struct lys_node_leaf *)dest_match.node[0]->schema)->type.base) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002786 goto remove_leafref;
2787 }
2788
Radek Krejcic1ffa4d2016-02-17 13:11:11 +01002789 if (!ly_strequal(((struct lyd_node_leaf_list *)source_match.node[0])->value_str,
Radek Krejci749190d2016-02-18 16:26:25 +01002790 ((struct lyd_node_leaf_list *)dest_match.node[0])->value_str, 1)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002791 goto remove_leafref;
2792 }
2793
2794 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002795 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002796 continue;
2797
2798remove_leafref:
2799 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002800 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002801 }
2802 } while (has_predicate);
2803
Michal Vaskocf024702015-10-08 15:01:42 +02002804 free(source_match.node);
2805 free(dest_match.node);
Michal Vasko0491ab32015-08-19 14:28:29 +02002806 if (parsed) {
2807 *parsed = parsed_loc;
2808 }
2809 return EXIT_SUCCESS;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002810
2811error:
2812
2813 if (source_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02002814 free(source_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002815 }
2816 if (dest_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02002817 free(dest_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002818 }
Michal Vasko0491ab32015-08-19 14:28:29 +02002819 if (parsed) {
2820 *parsed = -parsed_loc+i;
2821 }
2822 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002823}
2824
Michal Vasko730dfdf2015-08-11 14:48:05 +02002825/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002826 * @brief Resolve a path (leafref) in JSON data context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002827 *
Michal Vaskocf024702015-10-08 15:01:42 +02002828 * @param[in] node Leafref data node.
Michal Vasko23b61ec2015-08-19 11:19:50 +02002829 * @param[in] path Path of the leafref.
Radek Krejci48464ed2016-03-17 15:44:09 +01002830 * @param[out] ret Matching nodes. Expects an empty, but allocated structure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002831 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002832 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002833 */
Michal Vasko184521f2015-09-24 13:14:26 +02002834static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002835resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002836{
Radek Krejci71b795b2015-08-10 16:20:39 +02002837 struct lyd_node *data = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002838 const char *prefix, *name;
Michal Vasko0491ab32015-08-19 14:28:29 +02002839 int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002840 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002841
Michal Vaskocf024702015-10-08 15:01:42 +02002842 assert(node && path && ret && !ret->count);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002843
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002844 parent_times = 0;
Michal Vaskod9173342015-08-17 14:35:36 +02002845 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002846
2847 /* searching for nodeset */
2848 do {
2849 if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002850 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002851 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002852 goto error;
2853 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002854 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02002855 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002856
Michal Vasko23b61ec2015-08-19 11:19:50 +02002857 if (!ret->count) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02002858 if (parent_times != -1) {
2859 ret->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002860 ret->node = calloc(1, sizeof *ret->node);
Michal Vasko253035f2015-12-17 16:58:13 +01002861 if (!ret->node) {
2862 LOGMEM;
Radek Krejci50501732016-01-07 13:06:39 +01002863 rc = -1;
Michal Vasko253035f2015-12-17 16:58:13 +01002864 goto error;
2865 }
Michal Vasko8bcdf292015-08-19 14:04:43 +02002866 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002867 for (i = 0; i < parent_times; ++i) {
2868 /* relative path */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002869 if (!ret->count) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002870 /* error, too many .. */
Radek Krejci48464ed2016-03-17 15:44:09 +01002871 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name);
Michal Vasko0491ab32015-08-19 14:28:29 +02002872 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002873 goto error;
Michal Vaskocf024702015-10-08 15:01:42 +02002874 } else if (!ret->node[0]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002875 /* first .. */
Michal Vaskocf024702015-10-08 15:01:42 +02002876 data = ret->node[0] = node->parent;
2877 } else if (!ret->node[0]->parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002878 /* we are in root */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002879 ret->count = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02002880 free(ret->node);
2881 ret->node = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002882 } else {
2883 /* multiple .. */
Michal Vaskocf024702015-10-08 15:01:42 +02002884 data = ret->node[0] = ret->node[0]->parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002885 }
2886 }
2887
2888 /* absolute path */
2889 if (parent_times == -1) {
Michal Vaskocf024702015-10-08 15:01:42 +02002890 for (data = node; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02002891 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko8bcdf292015-08-19 14:04:43 +02002892 if (data->prev) {
2893 for (; data->prev->next; data = data->prev);
2894 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002895 }
2896 }
2897
2898 /* node identifier */
Radek Krejci581ce772015-11-10 17:22:40 +01002899 if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01002900 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002901 LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name);
Michal Vasko184521f2015-09-24 13:14:26 +02002902 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002903 goto error;
2904 }
2905
2906 if (has_predicate) {
2907 /* we have predicate, so the current results must be lists */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002908 for (j = 0; j < ret->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02002909 if (ret->node[j]->schema->nodetype == LYS_LIST &&
2910 ((struct lys_node_list *)ret->node[0]->schema)->keys) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002911 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002912 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002913 continue;
2914 }
2915
2916 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002917 unres_data_del(ret, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002918 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002919 if ((rc = resolve_path_predicate_data(path, node, ret, &i))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01002920 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002921 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, path);
Michal Vasko184521f2015-09-24 13:14:26 +02002922 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002923 goto error;
2924 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002925 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02002926 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002927
Michal Vasko23b61ec2015-08-19 11:19:50 +02002928 if (!ret->count) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002929 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, path-parsed);
Michal Vasko0491ab32015-08-19 14:28:29 +02002930 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002931 goto error;
2932 }
2933 }
2934 } while (path[0] != '\0');
2935
Michal Vaskof02e3742015-08-05 16:27:02 +02002936 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002937
2938error:
2939
Michal Vaskocf024702015-10-08 15:01:42 +02002940 free(ret->node);
2941 ret->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002942 ret->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002943
Michal Vasko0491ab32015-08-19 14:28:29 +02002944 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002945}
2946
Michal Vasko730dfdf2015-08-11 14:48:05 +02002947/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002948 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002949 *
Michal Vaskobb211122015-08-19 14:03:11 +02002950 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01002951 * @param[in] context_node Predicate context node (where the predicate is placed).
2952 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vasko730dfdf2015-08-11 14:48:05 +02002953 *
Michal Vasko184521f2015-09-24 13:14:26 +02002954 * @return 0 on forward reference, otherwise the number
2955 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002956 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002957 */
Michal Vasko1f76a282015-08-04 16:16:53 +02002958static int
Radek Krejciadb57612016-02-16 13:34:34 +01002959resolve_path_predicate_schema(const char *path, const struct lys_node *context_node,
Radek Krejci48464ed2016-03-17 15:44:09 +01002960 struct lys_node *parent)
Michal Vasko1f76a282015-08-04 16:16:53 +02002961{
Michal Vasko1e62a092015-12-01 12:27:20 +01002962 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02002963 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
2964 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 +02002965 int has_predicate, dest_parent_times = 0, i, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02002966
2967 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02002968 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02002969 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002970 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02002971 return -parsed+i;
2972 }
2973 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02002974 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02002975
Michal Vasko58090902015-08-13 14:04:15 +02002976 /* source (must be leaf) */
Michal Vasko36cbaa42015-12-14 13:15:48 +01002977 if (!sour_pref) {
Radek Krejciadb57612016-02-16 13:34:34 +01002978 sour_pref = context_node->module->name;
Michal Vasko36cbaa42015-12-14 13:15:48 +01002979 }
Radek Krejciadb57612016-02-16 13:34:34 +01002980 rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02002981 LYS_LEAF | LYS_AUGMENT, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002982 if (rc) {
Radek Krejci010e54b2016-03-15 09:40:34 +01002983 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002984 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02002985 }
2986 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02002987 }
2988
2989 /* destination */
2990 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
2991 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002992 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 +02002993 return -parsed;
2994 }
2995 pke_parsed += i;
2996
Radek Krejciadb57612016-02-16 13:34:34 +01002997 /* parent is actually the parent of this leaf, so skip the first ".." */
2998 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko1f76a282015-08-04 16:16:53 +02002999 if (!dst_node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003000 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003001 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003002 }
Radek Krejciadb57612016-02-16 13:34:34 +01003003 dst_node = dst_node->parent;
Michal Vasko1f76a282015-08-04 16:16:53 +02003004 }
3005 while (1) {
Michal Vasko36cbaa42015-12-14 13:15:48 +01003006 if (!dest_pref) {
3007 dest_pref = dst_node->module->name;
3008 }
3009 rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003010 LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003011 if (rc) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003012 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003013 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003014 }
3015 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003016 }
3017
3018 if (pke_len == pke_parsed) {
3019 break;
3020 }
3021
3022 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
3023 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003024 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Radek Krejciadb57612016-02-16 13:34:34 +01003025 (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003026 return -parsed;
3027 }
3028 pke_parsed += i;
3029 }
3030
3031 /* check source - dest match */
Michal Vasko184521f2015-09-24 13:14:26 +02003032 if (dst_node->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003033 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path-parsed);
3034 LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
3035 "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003036 return -parsed;
3037 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003038 } while (has_predicate);
3039
3040 return parsed;
3041}
3042
Michal Vasko730dfdf2015-08-11 14:48:05 +02003043/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003044 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003045 *
Michal Vaskobb211122015-08-19 14:03:11 +02003046 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003047 * @param[in] parent_node Parent of the leafref.
Radek Krejci2f12f852016-01-08 12:59:57 +01003048 * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path
3049 * has to contain absolute path
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003050 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003051 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003052 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003053 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003054static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003055resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf,
Michal Vasko36cbaa42015-12-14 13:15:48 +01003056 const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003057{
Michal Vasko1e62a092015-12-01 12:27:20 +01003058 const struct lys_node *node;
Radek Krejcic071c542016-01-27 14:57:51 +01003059 const struct lys_module *mod;
Michal Vasko1f76a282015-08-04 16:16:53 +02003060 const char *id, *prefix, *name;
3061 int pref_len, nam_len, parent_times, has_predicate;
Michal Vasko184521f2015-09-24 13:14:26 +02003062 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003063
Michal Vasko184521f2015-09-24 13:14:26 +02003064 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003065 parent_times = 0;
3066 id = path;
3067
3068 do {
3069 if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003070 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 +02003071 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003072 }
3073 id += i;
3074
Michal Vasko184521f2015-09-24 13:14:26 +02003075 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003076 if (parent_times == -1) {
Radek Krejcic071c542016-01-27 14:57:51 +01003077 /* resolve prefix of the module */
Radek Krejciadb57612016-02-16 13:34:34 +01003078 mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len);
Radek Krejcic071c542016-01-27 14:57:51 +01003079 /* get start node */
3080 node = mod ? mod->data : NULL;
Michal Vasko58090902015-08-13 14:04:15 +02003081 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003082 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003083 return EXIT_FAILURE;
Michal Vasko58090902015-08-13 14:04:15 +02003084 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003085 } else if (parent_times > 0) {
Michal Vasko73ae2562015-08-06 11:58:13 +02003086 /* node is the parent already, skip one ".." */
Radek Krejci2f12f852016-01-08 12:59:57 +01003087 if (parent_tpdf) {
3088 /* the path is not allowed to contain relative path since we are in top level typedef */
Radek Krejci48464ed2016-03-17 15:44:09 +01003089 LOGVAL(LYE_NORESOLV, 0, NULL, path);
Radek Krejci2f12f852016-01-08 12:59:57 +01003090 return -1;
3091 }
3092
Radek Krejciadb57612016-02-16 13:34:34 +01003093 node = parent;
Michal Vasko58090902015-08-13 14:04:15 +02003094 i = 0;
3095 while (1) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003096 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003097 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003098 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003099 }
Michal Vasko58090902015-08-13 14:04:15 +02003100
3101 /* this node is a wrong node, we actually need the augment target */
3102 if (node->nodetype == LYS_AUGMENT) {
3103 node = ((struct lys_node_augment *)node)->target;
3104 if (!node) {
3105 continue;
3106 }
3107 }
3108
3109 ++i;
3110 if (i == parent_times) {
3111 break;
3112 }
3113 node = node->parent;
Michal Vasko1f76a282015-08-04 16:16:53 +02003114 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003115
Michal Vasko1f76a282015-08-04 16:16:53 +02003116 node = node->child;
Michal Vaskoe01eca52015-08-13 14:42:02 +02003117 } else {
3118 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003119 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003120 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003121
Michal Vasko184521f2015-09-24 13:14:26 +02003122 first_iter = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003123 } else {
Michal Vasko7dc71d02016-03-15 10:42:28 +01003124 /* move down the tree, if possible */
3125 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko2f5aceb2016-03-22 10:24:14 +01003126 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 +01003127 return -1;
3128 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003129 node = node->child;
3130 }
3131
Michal Vasko4f0dad02016-02-15 14:08:23 +01003132 if (!prefix) {
Radek Krejciadb57612016-02-16 13:34:34 +01003133 prefix = lys_node_module(parent)->name;
Michal Vasko4f0dad02016-02-15 14:08:23 +01003134 }
3135
Michal Vasko36cbaa42015-12-14 13:15:48 +01003136 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 +02003137 if (rc) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003138 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003139 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path);
Michal Vasko184521f2015-09-24 13:14:26 +02003140 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003141 return rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003142 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003143
3144 if (has_predicate) {
3145 /* we have predicate, so the current result must be list */
3146 if (node->nodetype != LYS_LIST) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003147 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003148 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003149 }
3150
Radek Krejci48464ed2016-03-17 15:44:09 +01003151 i = resolve_path_predicate_schema(id, node, parent);
Michal Vasko184521f2015-09-24 13:14:26 +02003152 if (!i) {
Michal Vaskof9664da2015-08-24 15:03:30 +02003153 return EXIT_FAILURE;
Michal Vasko184521f2015-09-24 13:14:26 +02003154 } else if (i < 0) {
3155 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003156 }
3157 id += i;
3158 }
3159 } while (id[0]);
3160
Radek Krejcib1c12512015-08-11 11:22:04 +02003161 /* the target must be leaf or leaf-list */
3162 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003163 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003164 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003165 }
3166
Radek Krejcicf509982015-12-15 09:22:44 +01003167 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003168 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003169 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003170 return -1;
3171 }
3172
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003173 if (ret) {
3174 *ret = node;
3175 }
3176 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003177}
3178
Michal Vasko730dfdf2015-08-11 14:48:05 +02003179/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003180 * @brief Resolve instance-identifier predicate in JSON data format.
3181 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003182 *
Michal Vaskobb211122015-08-19 14:03:11 +02003183 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003184 * @param[in,out] node_match Nodes matching the restriction without
3185 * the predicate. Nodes not satisfying
3186 * the predicate are removed.
3187 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003188 * @return Number of characters successfully parsed,
3189 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003190 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003191static int
Michal Vaskof39142b2015-10-21 11:40:05 +02003192resolve_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003193{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003194 /* ... /node[target = value] ... */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003195 struct unres_data target_match;
3196 struct ly_ctx *ctx;
Michal Vasko1e62a092015-12-01 12:27:20 +01003197 const struct lys_module *mod;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003198 const char *model, *name, *value;
3199 char *str;
3200 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed;
3201 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003202
Michal Vasko1f2cc332015-08-19 11:18:32 +02003203 assert(pred && node_match->count);
3204
Michal Vaskocf024702015-10-08 15:01:42 +02003205 ctx = node_match->node[0]->schema->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003206 idx = -1;
3207 parsed = 0;
3208
3209 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02003210 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003211 return -parsed+i;
3212 }
3213 parsed += i;
3214 pred += i;
3215
Michal Vasko1f2cc332015-08-19 11:18:32 +02003216 /* pos */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003217 if (isdigit(name[0])) {
3218 idx = atoi(name);
3219 }
3220
Michal Vasko1f2cc332015-08-19 11:18:32 +02003221 for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003222 /* target */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003223 memset(&target_match, 0, sizeof target_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003224 if ((name[0] == '.') || !value) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003225 target_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003226 target_match.node = malloc(sizeof *target_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003227 if (!target_match.node) {
3228 LOGMEM;
3229 return -1;
3230 }
Michal Vaskocf024702015-10-08 15:01:42 +02003231 target_match.node[0] = node_match->node[j];
Michal Vasko1f2cc332015-08-19 11:18:32 +02003232 } else {
3233 str = strndup(model, mod_len);
3234 mod = ly_ctx_get_module(ctx, str, NULL);
3235 free(str);
3236
Radek Krejci804836a2016-02-03 10:39:55 +01003237 if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003238 goto remove_instid;
3239 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003240 }
3241
3242 /* check that we have the correct type */
3243 if (name[0] == '.') {
Michal Vaskocf024702015-10-08 15:01:42 +02003244 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003245 goto remove_instid;
3246 }
3247 } else if (value) {
Michal Vaskocf024702015-10-08 15:01:42 +02003248 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003249 goto remove_instid;
3250 }
3251 }
3252
Michal Vasko83a6c462015-10-08 16:43:53 +02003253 if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len)
3254 || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len]))
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003255 || (!value && (idx != cur_idx))) {
3256 goto remove_instid;
3257 }
3258
Michal Vaskocf024702015-10-08 15:01:42 +02003259 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003260
3261 /* leafref is ok, continue check with next leafref */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003262 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003263 continue;
3264
3265remove_instid:
Michal Vaskocf024702015-10-08 15:01:42 +02003266 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003267
3268 /* does not fulfill conditions, remove leafref record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003269 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003270 }
3271 } while (has_predicate);
3272
3273 return parsed;
3274}
3275
Michal Vasko730dfdf2015-08-11 14:48:05 +02003276/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003277 * @brief Resolve instance-identifier in JSON data format. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003278 *
Radek Krejciadb57612016-02-16 13:34:34 +01003279 * @param[in] data Data node where the path is used
Michal Vasko730dfdf2015-08-11 14:48:05 +02003280 * @param[in] path Instance-identifier node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003281 *
Radek Krejcic5090c32015-08-12 09:46:19 +02003282 * @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 +02003283 */
Michal Vasko184521f2015-09-24 13:14:26 +02003284static struct lyd_node *
Radek Krejci48464ed2016-03-17 15:44:09 +01003285resolve_instid(struct lyd_node *data, const char *path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003286{
Radek Krejcic5090c32015-08-12 09:46:19 +02003287 int i = 0, j;
3288 struct lyd_node *result = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +01003289 const struct lys_module *mod = NULL;
Radek Krejcic5090c32015-08-12 09:46:19 +02003290 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003291 const char *model, *name;
Radek Krejcic5090c32015-08-12 09:46:19 +02003292 char *str;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003293 int mod_len, name_len, has_predicate;
3294 struct unres_data node_match;
3295 uint32_t k;
3296
3297 memset(&node_match, 0, sizeof node_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003298
Radek Krejcic5090c32015-08-12 09:46:19 +02003299 /* we need root to resolve absolute path */
3300 for (; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003301 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko0491ab32015-08-19 14:28:29 +02003302 if (data->prev) {
3303 for (; data->prev->next; data = data->prev);
3304 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003305
Radek Krejcic5090c32015-08-12 09:46:19 +02003306 /* search for the instance node */
3307 while (path[i]) {
Michal Vaskof39142b2015-10-21 11:40:05 +02003308 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
Radek Krejcic5090c32015-08-12 09:46:19 +02003309 if (j <= 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003310 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003311 goto error;
3312 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003313 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003314
Michal Vasko1f2cc332015-08-19 11:18:32 +02003315 str = strndup(model, mod_len);
Michal Vasko253035f2015-12-17 16:58:13 +01003316 if (!str) {
3317 LOGMEM;
3318 goto error;
3319 }
Michal Vasko1f2cc332015-08-19 11:18:32 +02003320 mod = ly_ctx_get_module(ctx, str, NULL);
3321 free(str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003322
Radek Krejcic5090c32015-08-12 09:46:19 +02003323 if (!mod) {
3324 /* no instance exists */
3325 return NULL;
3326 }
3327
Michal Vasko1f2cc332015-08-19 11:18:32 +02003328 if (resolve_data(mod, name, name_len, data, &node_match)) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003329 /* no instance exists */
3330 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003331 }
3332
3333 if (has_predicate) {
3334 /* we have predicate, so the current results must be list or leaf-list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003335 for (k = 0; k < node_match.count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003336 if ((node_match.node[k]->schema->nodetype == LYS_LIST &&
3337 ((struct lys_node_list *)node_match.node[k]->schema)->keys)
3338 || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003339 /* instid is ok, continue check with next instid */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003340 ++k;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003341 continue;
3342 }
3343
3344 /* does not fulfill conditions, remove inst record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003345 unres_data_del(&node_match, k);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003346 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003347
Michal Vaskof39142b2015-10-21 11:40:05 +02003348 j = resolve_predicate(&path[i], &node_match);
Radek Krejcic5090c32015-08-12 09:46:19 +02003349 if (j < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003350 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003351 goto error;
3352 }
Michal Vasko1f2cc332015-08-19 11:18:32 +02003353 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003354
Michal Vasko1f2cc332015-08-19 11:18:32 +02003355 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003356 /* no instance exists */
3357 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003358 }
3359 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003360 }
3361
Michal Vasko1f2cc332015-08-19 11:18:32 +02003362 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003363 /* no instance exists */
3364 return NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003365 } else if (node_match.count > 1) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003366 /* instance identifier must resolve to a single node */
Radek Krejci48464ed2016-03-17 15:44:09 +01003367 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Radek Krejcic5090c32015-08-12 09:46:19 +02003368
3369 /* cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003370 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003371
3372 return NULL;
3373 } else {
3374 /* we have required result, remember it and cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003375 result = node_match.node[0];
3376 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003377
3378 return result;
3379 }
3380
3381error:
3382
3383 /* cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003384 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003385
3386 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003387}
3388
Michal Vasko730dfdf2015-08-11 14:48:05 +02003389/**
3390 * @brief Passes config flag down to children. Does not log.
3391 *
3392 * @param[in] node Parent node.
3393 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003394static void
Radek Krejci1d82ef62015-08-07 14:44:40 +02003395inherit_config_flag(struct lys_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003396{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003397 LY_TREE_FOR(node, node) {
3398 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
3399 inherit_config_flag(node->child);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003400 }
3401}
3402
Michal Vasko730dfdf2015-08-11 14:48:05 +02003403/**
Michal Vasko7178e692016-02-12 15:58:05 +01003404 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003405 *
Michal Vaskobb211122015-08-19 14:03:11 +02003406 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01003407 * @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 +02003408 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003409 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003410 */
Michal Vasko7178e692016-02-12 15:58:05 +01003411static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003412resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003413{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003414 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02003415 struct lys_node *sub;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003416
Michal Vasko1d87a922015-08-21 12:57:16 +02003417 assert(aug);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003418
3419 /* resolve target node */
Michal Vasko3edeaf72016-02-11 13:17:43 +01003420 rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), (const struct lys_node **)&aug->target);
Michal Vasko7178e692016-02-12 15:58:05 +01003421 if (rc == -1) {
3422 return -1;
3423 }
3424 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003425 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003426 return -1;
3427 }
3428 if (!aug->target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003429 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003430 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003431 }
3432
3433 if (!aug->child) {
3434 /* nothing to do */
Michal Vasko1d87a922015-08-21 12:57:16 +02003435 LOGWRN("Augment \"%s\" without children.", aug->target_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003436 return EXIT_SUCCESS;
3437 }
3438
Michal Vaskod58d5962016-03-02 14:29:41 +01003439 /* check for mandatory nodes - if the target node is in another module
3440 * the added nodes cannot be mandatory
3441 */
3442 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
3443 && lyp_check_mandatory((struct lys_node *)aug)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003444 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, "mandatory", "augment node");
3445 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "When augmenting data in another module, mandatory nodes are not allowed.");
Michal Vaskod58d5962016-03-02 14:29:41 +01003446 return -1;
3447 }
3448
Michal Vasko07e89ef2016-03-03 13:28:57 +01003449 /* check augment target type and then augment nodes type */
3450 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
3451 LY_TREE_FOR(aug->child, sub) {
3452 if (!(sub->nodetype & (LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003453 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3454 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003455 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3456 return -1;
3457 }
3458 }
3459 } else if (aug->target->nodetype == LYS_CHOICE) {
3460 LY_TREE_FOR(aug->child, sub) {
3461 if (!(sub->nodetype & (LYS_CASE | LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003462 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3463 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003464 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3465 return -1;
3466 }
3467 }
3468 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003469 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
3470 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01003471 return -1;
3472 }
3473
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003474 /* inherit config information from parent, augment does not have
3475 * config property, but we need to keep the information for subelements
3476 */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003477 aug->flags |= aug->target->flags & LYS_CONFIG_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003478 LY_TREE_FOR(aug->child, sub) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003479 inherit_config_flag(sub);
3480 }
3481
Radek Krejcic071c542016-01-27 14:57:51 +01003482 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02003483 LY_TREE_FOR(aug->child, sub) {
Michal Vasko4f0dad02016-02-15 14:08:23 +01003484 if (lys_check_id(sub, aug->parent, lys_module(aug->module))) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02003485 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02003486 }
3487 }
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003488 /* reconnect augmenting data into the target - add them to the target child list */
3489 if (aug->target->child) {
Michal Vasko1d87a922015-08-21 12:57:16 +02003490 sub = aug->target->child->prev; /* remember current target's last node */
3491 sub->next = aug->child; /* connect augmenting data after target's last node */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003492 aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */
Michal Vasko1d87a922015-08-21 12:57:16 +02003493 aug->child->prev = sub; /* finish connecting of both child lists */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003494 } else {
3495 aug->target->child = aug->child;
3496 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003497
3498 return EXIT_SUCCESS;
3499}
3500
Michal Vasko730dfdf2015-08-11 14:48:05 +02003501/**
3502 * @brief Resolve uses, apply augments, refines. Logs directly.
3503 *
Michal Vaskobb211122015-08-19 14:03:11 +02003504 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003505 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003506 *
Michal Vaskodef0db12015-10-07 13:22:48 +02003507 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003508 */
Michal Vasko184521f2015-09-24 13:14:26 +02003509static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003510resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003511{
3512 struct ly_ctx *ctx;
Michal Vasko1e62a092015-12-01 12:27:20 +01003513 struct lys_node *node = NULL;
3514 const struct lys_node *node_aux;
Radek Krejci76512572015-08-04 09:47:08 +02003515 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003516 struct lys_restr *must, **old_must;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003517 int i, j, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003518 uint8_t size, *old_size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003519
Michal Vasko71e1aa82015-08-12 12:17:51 +02003520 assert(uses->grp);
Michal Vaskoe7708512016-03-11 10:26:55 +01003521 /* HACK just check that the grouping is resolved */
Michal Vaskodef0db12015-10-07 13:22:48 +02003522 assert(!uses->grp->nacm);
Michal Vasko71e1aa82015-08-12 12:17:51 +02003523
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003524 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01003525 LY_TREE_FOR(uses->grp->child, node_aux) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003526 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, uses->flags, uses->nacm, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01003527 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003528 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
3529 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003530 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003531 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003532 }
3533 ctx = uses->module->ctx;
3534
Michal Vaskodef0db12015-10-07 13:22:48 +02003535 /* we managed to copy the grouping, the rest must be possible to resolve */
3536
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003537 /* apply refines */
3538 for (i = 0; i < uses->refine_size; i++) {
3539 rfn = &uses->refine[i];
Michal Vasko3edeaf72016-02-11 13:17:43 +01003540 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE,
Radek Krejcicc217a62016-04-08 16:58:11 +02003541 (const struct lys_node **)&node, 1);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003542 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003543 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskodef0db12015-10-07 13:22:48 +02003544 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003545 }
3546
Radek Krejci1d82ef62015-08-07 14:44:40 +02003547 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003548 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
3549 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003550 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003551 }
3552
3553 /* description on any nodetype */
3554 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003555 lydict_remove(ctx, node->dsc);
3556 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003557 }
3558
3559 /* reference on any nodetype */
3560 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003561 lydict_remove(ctx, node->ref);
3562 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003563 }
3564
3565 /* config on any nodetype */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003566 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003567 node->flags &= ~LYS_CONFIG_MASK;
3568 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003569 }
3570
3571 /* default value ... */
3572 if (rfn->mod.dflt) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003573 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003574 /* leaf */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003575 lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt);
3576 ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0);
3577 } else if (node->nodetype == LYS_CHOICE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003578 /* choice */
Michal Vasko3edeaf72016-02-11 13:17:43 +01003579 rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child,
3580 (const struct lys_node **)&((struct lys_node_choice *)node)->dflt);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003581 if (rc || !((struct lys_node_choice *)node)->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003582 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default");
Michal Vaskodef0db12015-10-07 13:22:48 +02003583 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003584 }
3585 }
3586 }
3587
3588 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003589 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003590 if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003591 /* remove current value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003592 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003593
3594 /* set new value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003595 node->flags |= (rfn->flags & LYS_MAND_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003596 }
3597 }
3598
3599 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003600 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
3601 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
3602 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003603 }
3604
3605 /* min/max-elements on list or leaf-list */
3606 /* magic - bit 3 in flags means min set, bit 4 says max set */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003607 if (node->nodetype == LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003608 if (rfn->flags & 0x04) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003609 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003610 }
3611 if (rfn->flags & 0x08) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003612 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003613 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003614 } else if (node->nodetype == LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003615 if (rfn->flags & 0x04) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003616 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003617 }
3618 if (rfn->flags & 0x08) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003619 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003620 }
3621 }
3622
3623 /* must in leaf, leaf-list, list, container or anyxml */
3624 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003625 switch (node->nodetype) {
3626 case LYS_LEAF:
3627 old_size = &((struct lys_node_leaf *)node)->must_size;
3628 old_must = &((struct lys_node_leaf *)node)->must;
3629 break;
3630 case LYS_LEAFLIST:
3631 old_size = &((struct lys_node_leaflist *)node)->must_size;
3632 old_must = &((struct lys_node_leaflist *)node)->must;
3633 break;
3634 case LYS_LIST:
3635 old_size = &((struct lys_node_list *)node)->must_size;
3636 old_must = &((struct lys_node_list *)node)->must;
3637 break;
3638 case LYS_CONTAINER:
3639 old_size = &((struct lys_node_container *)node)->must_size;
3640 old_must = &((struct lys_node_container *)node)->must;
3641 break;
3642 case LYS_ANYXML:
3643 old_size = &((struct lys_node_anyxml *)node)->must_size;
3644 old_must = &((struct lys_node_anyxml *)node)->must;
3645 break;
3646 default:
3647 LOGINT;
Radek Krejcie4e4d722015-10-05 16:53:50 +02003648 return -1;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003649 }
3650
3651 size = *old_size + rfn->must_size;
3652 must = realloc(*old_must, size * sizeof *rfn->must);
3653 if (!must) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003654 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003655 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003656 }
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003657 for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) {
3658 must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0);
3659 must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0);
3660 must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0);
3661 must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0);
3662 must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003663 }
3664
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003665 *old_must = must;
3666 *old_size = size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003667 }
3668 }
3669
3670 /* apply augments */
3671 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003672 rc = resolve_augment(&uses->augment[i], uses->child);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003673 if (rc) {
Michal Vaskodef0db12015-10-07 13:22:48 +02003674 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003675 }
3676 }
3677
3678 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003679}
3680
Michal Vasko730dfdf2015-08-11 14:48:05 +02003681/**
3682 * @brief Resolve base identity recursively. Does not log.
3683 *
3684 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02003685 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003686 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003687 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003688 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003689 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003690 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003691static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003692resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003693 struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003694{
Michal Vaskof02e3742015-08-05 16:27:02 +02003695 uint32_t i, j;
Radek Krejcibabbff82016-02-19 13:31:37 +01003696 struct lys_ident *base = NULL, *base_iter;
Radek Krejcia52656e2015-08-05 13:41:50 +02003697 struct lys_ident_der *der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003698
Radek Krejcicf509982015-12-15 09:22:44 +01003699 assert(ret);
3700
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003701 /* search module */
3702 for (i = 0; i < module->ident_size; i++) {
3703 if (!strcmp(basename, module->ident[i].name)) {
3704
3705 if (!ident) {
3706 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003707 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01003708 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003709 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003710 }
3711
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003712 base = &module->ident[i];
3713 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003714 }
3715 }
3716
3717 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003718 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
3719 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
3720 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003721
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003722 if (!ident) {
3723 *ret = &module->inc[j].submodule->ident[i];
3724 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003725 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003726
3727 base = &module->inc[j].submodule->ident[i];
3728 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003729 }
3730 }
3731 }
3732
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003733matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003734 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01003735 if (base) {
3736 /* check for circular reference */
3737 for (base_iter = base; base_iter; base_iter = base_iter->base) {
3738 if (ident == base_iter) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003739 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, base_iter->name, "base");
3740 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejcibabbff82016-02-19 13:31:37 +01003741 return EXIT_FAILURE;
3742 }
3743 }
3744 /* checks done, store the result */
3745 ident->base = base;
3746
3747 /* maintain backlinks to the derived identitise */
3748 while (base) {
3749 for (der = base->der; der && der->next; der = der->next);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003750 if (der) {
3751 der->next = malloc(sizeof *der);
3752 der = der->next;
3753 } else {
3754 ident->base->der = der = malloc(sizeof *der);
3755 }
Michal Vasko253035f2015-12-17 16:58:13 +01003756 if (!der) {
3757 LOGMEM;
3758 return EXIT_FAILURE;
3759 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003760 der->next = NULL;
3761 der->ident = ident;
3762
Radek Krejcibabbff82016-02-19 13:31:37 +01003763 base = base->base;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003764 }
Radek Krejcicf509982015-12-15 09:22:44 +01003765 *ret = ident->base;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003766 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003767 }
3768
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003769 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003770}
3771
Michal Vasko730dfdf2015-08-11 14:48:05 +02003772/**
3773 * @brief Resolve base identity. Logs directly.
3774 *
3775 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02003776 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003777 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01003778 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01003779 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003780 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003781 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003782 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003783static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003784resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent,
Radek Krejci48464ed2016-03-17 15:44:09 +01003785 struct lys_type *type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003786{
3787 const char *name;
Michal Vasko2d851a92015-10-20 16:16:36 +02003788 int i, mod_name_len = 0;
Radek Krejcicf509982015-12-15 09:22:44 +01003789 struct lys_ident *target, **ret;
3790 uint8_t flags;
3791 struct lys_module *mod;
3792
3793 assert((ident && !type) || (!ident && type));
3794
3795 if (!type) {
3796 /* have ident to resolve */
3797 ret = &target;
3798 flags = ident->flags;
3799 mod = ident->module;
3800 } else {
3801 /* have type to fill */
3802 ret = &type->info.ident.ref;
3803 flags = type->parent->flags;
3804 mod = type->parent->module;
3805 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003806
3807 /* search for the base identity */
3808 name = strchr(basename, ':');
3809 if (name) {
3810 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02003811 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003812 name++;
3813
Michal Vasko2d851a92015-10-20 16:16:36 +02003814 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003815 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02003816 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003817 }
3818 } else {
3819 name = basename;
3820 }
3821
Radek Krejcic071c542016-01-27 14:57:51 +01003822 /* get module where to search */
3823 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
3824 if (!module) {
3825 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01003826 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01003827 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003828 }
3829
Radek Krejcic071c542016-01-27 14:57:51 +01003830 /* search in the identified module ... */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003831 if (!resolve_base_ident_sub(module, ident, name, ret)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003832 goto success;
Radek Krejcibabbff82016-02-19 13:31:37 +01003833 } else if (ly_errno) {
Radek Krejcibabbff82016-02-19 13:31:37 +01003834 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003835 }
Radek Krejcic071c542016-01-27 14:57:51 +01003836 /* and all its submodules */
3837 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
3838 if (!resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) {
3839 goto success;
Radek Krejcibabbff82016-02-19 13:31:37 +01003840 } else if (ly_errno) {
Radek Krejcibabbff82016-02-19 13:31:37 +01003841 return EXIT_FAILURE;
Radek Krejcic071c542016-01-27 14:57:51 +01003842 }
3843 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003844
Radek Krejci02a04992016-03-17 16:06:37 +01003845 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, parent);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003846 return EXIT_FAILURE;
Radek Krejcicf509982015-12-15 09:22:44 +01003847
3848success:
3849 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01003850 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
3851 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003852 return -1;
3853 }
3854
3855 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003856}
3857
Michal Vasko730dfdf2015-08-11 14:48:05 +02003858/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003859 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003860 *
3861 * @param[in] base Base identity.
Michal Vaskofb0873c2015-08-21 09:00:07 +02003862 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01003863 * @param[in] node Node where the identityref is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02003864 *
3865 * @return Pointer to the identity resolvent, NULL on error.
3866 */
Radek Krejcia52656e2015-08-05 13:41:50 +02003867struct lys_ident *
Radek Krejci48464ed2016-03-17 15:44:09 +01003868resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003869{
Michal Vaskoc633ca02015-08-21 14:03:51 +02003870 const char *mod_name, *name;
3871 int mod_name_len, rc;
Radek Krejcia52656e2015-08-05 13:41:50 +02003872 struct lys_ident_der *der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003873
Michal Vaskofb0873c2015-08-21 09:00:07 +02003874 if (!base || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003875 return NULL;
3876 }
3877
Michal Vaskoc633ca02015-08-21 14:03:51 +02003878 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01003879 if (rc < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003880 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02003881 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01003882 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejci02a04992016-03-17 16:06:37 +01003883 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02003884 return NULL;
3885 }
3886
Michal Vaskoc633ca02015-08-21 14:03:51 +02003887 if (!strcmp(base->name, name) && (!mod_name
3888 || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) {
Michal Vaskofb0873c2015-08-21 09:00:07 +02003889 return base;
3890 }
3891
3892 for (der = base->der; der; der = der->next) {
Michal Vaskoc633ca02015-08-21 14:03:51 +02003893 if (!strcmp(der->ident->name, name) && (!mod_name
3894 || (!strncmp(der->ident->module->name, mod_name, mod_name_len)
3895 && !der->ident->module->name[mod_name_len]))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003896 /* we have match */
3897 return der->ident;
3898 }
3899 }
3900
Radek Krejci48464ed2016-03-17 15:44:09 +01003901 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003902 return NULL;
3903}
3904
Michal Vasko730dfdf2015-08-11 14:48:05 +02003905/**
Michal Vasko7955b362015-09-04 14:18:15 +02003906 * @brief Resolve (find) choice default case. Does not log.
3907 *
3908 * @param[in] choic Choice to use.
3909 * @param[in] dflt Name of the default case.
3910 *
3911 * @return Pointer to the default node or NULL.
3912 */
3913static struct lys_node *
3914resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
3915{
3916 struct lys_node *child, *ret;
3917
3918 LY_TREE_FOR(choic->child, child) {
3919 if (child->nodetype == LYS_USES) {
3920 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
3921 if (ret) {
3922 return ret;
3923 }
3924 }
3925
Radek Krejci749190d2016-02-18 16:26:25 +01003926 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYXML | LYS_CASE
Michal Vasko7955b362015-09-04 14:18:15 +02003927 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
3928 return child;
3929 }
3930 }
3931
3932 return NULL;
3933}
3934
3935/**
Michal Vaskobb211122015-08-19 14:03:11 +02003936 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003937 *
Michal Vaskobb211122015-08-19 14:03:11 +02003938 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003939 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003940 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003941 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003942 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003943static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003944resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003945{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003946 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01003947 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02003948
Radek Krejci010e54b2016-03-15 09:40:34 +01003949 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself
3950 * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping)
3951 * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm
3952 * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the
3953 * LYS_USESGRP flag is used. */
3954 for (par_grp = uses->parent; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = par_grp->parent);
Michal Vaskoe91afce2015-08-12 12:21:00 +02003955
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003956 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01003957 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
3958 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003959 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003960 return -1;
3961 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003962 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003963 return -1;
3964 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003965 if (par_grp && !(uses->flags & LYS_USESGRP)) {
3966 par_grp->nacm++;
3967 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02003968 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01003969 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02003970 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003971 }
3972
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003973 if (uses->grp->nacm) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003974 if (par_grp && !(uses->flags & LYS_USESGRP)) {
3975 par_grp->nacm++;
3976 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02003977 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003978 return EXIT_FAILURE;
3979 }
3980
Radek Krejci48464ed2016-03-17 15:44:09 +01003981 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003982 if (!rc) {
3983 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01003984 if (par_grp && (uses->flags & LYS_USESGRP)) {
3985 if (!par_grp->nacm) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003986 LOGINT;
3987 return -1;
3988 }
Radek Krejci010e54b2016-03-15 09:40:34 +01003989 par_grp->nacm--;
3990 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003991 }
Radek Krejcicf509982015-12-15 09:22:44 +01003992
3993 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01003994 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01003995 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003996 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003997 return -1;
3998 }
3999
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004000 return EXIT_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01004001 } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) {
4002 par_grp->nacm++;
4003 uses->flags |= LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004004 }
4005
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004006 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004007}
4008
Michal Vasko730dfdf2015-08-11 14:48:05 +02004009/**
Michal Vasko9957e592015-08-17 15:04:09 +02004010 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004011 *
Michal Vaskobb211122015-08-19 14:03:11 +02004012 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004013 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004014 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004015 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004016 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004017static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004018resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004019{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004020 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01004021 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004022
4023 for (i = 0; i < list->keys_size; ++i) {
4024 /* get the key name */
4025 if ((value = strpbrk(keys_str, " \t\n"))) {
4026 len = value - keys_str;
4027 while (isspace(value[0])) {
4028 value++;
4029 }
4030 } else {
4031 len = strlen(keys_str);
4032 }
4033
Michal Vasko4f0dad02016-02-15 14:08:23 +01004034 rc = lys_get_sibling(list->child, lys_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004035 if (rc) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004036 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004037 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004038 }
4039 return rc;
4040 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004041
Radek Krejci48464ed2016-03-17 15:44:09 +01004042 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004043 /* check_key logs */
4044 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004045 }
4046
Radek Krejcicf509982015-12-15 09:22:44 +01004047 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004048 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004049 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
4050 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01004051 return -1;
4052 }
4053
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004054 /* prepare for next iteration */
4055 while (value && isspace(value[0])) {
4056 value++;
4057 }
4058 keys_str = value;
4059 }
4060
Michal Vaskof02e3742015-08-05 16:27:02 +02004061 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004062}
4063
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004064/**
Michal Vaskobf19d252015-10-08 15:39:17 +02004065 * @brief Resolve (check) all must conditions of \p node.
4066 * Logs directly.
4067 *
4068 * @param[in] node Data node with optional must statements.
Michal Vaskobf19d252015-10-08 15:39:17 +02004069 *
4070 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
4071 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004072static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004073resolve_must(struct lyd_node *node)
Michal Vaskof02e3742015-08-05 16:27:02 +02004074{
Michal Vaskobf19d252015-10-08 15:39:17 +02004075 uint8_t i, must_size;
4076 struct lys_restr *must;
4077 struct lyxp_set set;
4078
4079 assert(node);
4080 memset(&set, 0, sizeof set);
4081
4082 switch (node->schema->nodetype) {
4083 case LYS_CONTAINER:
4084 must_size = ((struct lys_node_container *)node->schema)->must_size;
4085 must = ((struct lys_node_container *)node->schema)->must;
4086 break;
4087 case LYS_LEAF:
4088 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
4089 must = ((struct lys_node_leaf *)node->schema)->must;
4090 break;
4091 case LYS_LEAFLIST:
4092 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
4093 must = ((struct lys_node_leaflist *)node->schema)->must;
4094 break;
4095 case LYS_LIST:
4096 must_size = ((struct lys_node_list *)node->schema)->must_size;
4097 must = ((struct lys_node_list *)node->schema)->must;
4098 break;
4099 case LYS_ANYXML:
4100 must_size = ((struct lys_node_anyxml *)node->schema)->must_size;
4101 must = ((struct lys_node_anyxml *)node->schema)->must;
4102 break;
4103 default:
4104 must_size = 0;
4105 break;
4106 }
4107
4108 for (i = 0; i < must_size; ++i) {
Michal Vasko944a5642016-03-21 11:48:58 +01004109 if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004110 return -1;
4111 }
4112
Michal Vasko944a5642016-03-21 11:48:58 +01004113 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02004114
4115 if (!set.value.bool) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004116 LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "Must", must[i].expr);
Michal Vaskobf19d252015-10-08 15:39:17 +02004117 return 1;
4118 }
4119 }
4120
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004121 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02004122}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004123
Michal Vaskobf19d252015-10-08 15:39:17 +02004124/**
Michal Vaskocf024702015-10-08 15:01:42 +02004125 * @brief Resolve (find) when condition context node. Does not log.
4126 *
4127 * @param[in] node Data node, whose conditional definition is being decided.
4128 * @param[in] schema Schema node with a when condition.
4129 *
4130 * @return Context node.
4131 */
4132static struct lyd_node *
4133resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004134{
Michal Vaskocf024702015-10-08 15:01:42 +02004135 struct lyd_node *parent;
4136 struct lys_node *sparent;
4137 uint16_t i, data_depth, schema_depth;
4138
4139 /* find a not schema-only node */
4140 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
4141 schema = lys_parent(schema);
4142 if (!schema) {
4143 return NULL;
4144 }
4145 }
4146
4147 /* get node depths */
4148 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
4149 for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) {
4150 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) {
4151 ++schema_depth;
4152 }
4153 }
4154 if (data_depth < schema_depth) {
4155 return NULL;
4156 }
4157
4158 /* find the corresponding data node */
4159 for (i = 0; i < data_depth - schema_depth; ++i) {
4160 node = node->parent;
4161 }
4162 if (node->schema != schema) {
4163 return NULL;
4164 }
4165
4166 return node;
4167}
4168
Radek Krejci03b71f72016-03-16 11:10:09 +01004169int
Radek Krejci01696bf2016-03-18 13:19:36 +01004170resolve_applies_must(const struct lyd_node *node)
4171{
4172 switch (node->schema->nodetype) {
4173 case LYS_CONTAINER:
4174 return ((struct lys_node_container *)node->schema)->must_size;
4175 case LYS_LEAF:
4176 return ((struct lys_node_leaf *)node->schema)->must_size;
4177 case LYS_LEAFLIST:
4178 return ((struct lys_node_leaflist *)node->schema)->must_size;
4179 case LYS_LIST:
4180 return ((struct lys_node_list *)node->schema)->must_size;
4181 case LYS_ANYXML:
4182 return ((struct lys_node_anyxml *)node->schema)->must_size;
4183 default:
4184 return 0;
4185 }
4186}
4187
4188int
Radek Krejci03b71f72016-03-16 11:10:09 +01004189resolve_applies_when(const struct lyd_node *node)
4190{
4191 struct lys_node *parent;
4192
4193 assert(node);
4194
4195 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
4196 return 1;
4197 }
4198
4199 parent = node->schema;
4200 goto check_augment;
4201
4202 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4203 if (((struct lys_node_uses *)parent)->when) {
4204 return 1;
4205 }
4206check_augment:
4207
4208 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
4209 (((struct lys_node_augment *)parent->parent)->when))) {
4210
4211 }
4212 parent = lys_parent(parent);
4213 }
4214
4215 return 0;
4216}
4217
Michal Vaskocf024702015-10-08 15:01:42 +02004218/**
4219 * @brief Resolve (check) all when conditions relevant for \p node.
4220 * Logs directly.
4221 *
4222 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02004223 *
Radek Krejci03b71f72016-03-16 11:10:09 +01004224 * @return
4225 * -1 - error, ly_errno is set
4226 * 0 - true "when" statement
4227 * 0, ly_vecode = LYVE_NOCOND - false "when" statement
4228 * 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 +02004229 */
4230static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004231resolve_when(struct lyd_node *node)
Michal Vaskocf024702015-10-08 15:01:42 +02004232{
4233 struct lyd_node *ctx_node = NULL;
4234 struct lys_node *parent;
4235 struct lyxp_set set;
Radek Krejci51093642016-03-29 10:14:59 +02004236 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02004237
4238 assert(node);
4239 memset(&set, 0, sizeof set);
4240
4241 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko944a5642016-03-21 11:48:58 +01004242 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004243 if (rc) {
4244 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004245 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004246 }
Radek Krejci51093642016-03-29 10:14:59 +02004247 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004248 }
4249
Radek Krejci03b71f72016-03-16 11:10:09 +01004250 /* set boolean result of the condition */
Michal Vasko944a5642016-03-21 11:48:58 +01004251 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02004252 if (!set.value.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004253 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01004254 LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004255 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004256 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004257 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004258 }
Radek Krejci51093642016-03-29 10:14:59 +02004259
4260 /* free xpath set content */
4261 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004262 }
4263
4264 parent = node->schema;
4265 goto check_augment;
4266
4267 /* check when in every schema node that affects node */
4268 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4269 if (((struct lys_node_uses *)parent)->when) {
4270 if (!ctx_node) {
4271 ctx_node = resolve_when_ctx_node(node, parent);
4272 if (!ctx_node) {
4273 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004274 rc = -1;
4275 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004276 }
4277 }
Michal Vasko944a5642016-03-21 11:48:58 +01004278 rc = lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004279 if (rc) {
4280 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004281 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004282 }
Radek Krejci51093642016-03-29 10:14:59 +02004283 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004284 }
4285
Michal Vasko944a5642016-03-21 11:48:58 +01004286 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02004287 if (!set.value.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004288 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01004289 LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004290 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004291 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004292 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004293 }
Radek Krejci51093642016-03-29 10:14:59 +02004294
4295 /* free xpath set content */
4296 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004297 }
4298
4299check_augment:
4300 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) {
4301 if (!ctx_node) {
4302 ctx_node = resolve_when_ctx_node(node, parent->parent);
4303 if (!ctx_node) {
4304 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004305 rc = -1;
4306 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004307 }
4308 }
Michal Vasko944a5642016-03-21 11:48:58 +01004309 rc = lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004310 if (rc) {
4311 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004312 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004313 }
Radek Krejci51093642016-03-29 10:14:59 +02004314 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004315 }
4316
Michal Vasko944a5642016-03-21 11:48:58 +01004317 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02004318
4319 if (!set.value.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004320 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01004321 LOGVAL(LYE_NOCOND, LY_VLOG_LYD, node, "When", ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004322 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004323 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004324 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004325 }
Radek Krejci51093642016-03-29 10:14:59 +02004326
4327 /* free xpath set content */
4328 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004329 }
4330
4331 parent = lys_parent(parent);
4332 }
4333
Radek Krejci0b7704f2016-03-18 12:16:14 +01004334 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01004335
Radek Krejci51093642016-03-29 10:14:59 +02004336cleanup:
4337
4338 /* free xpath set content */
4339 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0);
4340
4341 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004342}
4343
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004344/**
Michal Vaskobb211122015-08-19 14:03:11 +02004345 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004346 *
4347 * @param[in] mod Main module.
4348 * @param[in] item Item to resolve. Type determined by \p type.
4349 * @param[in] type Type of the unresolved item.
4350 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02004351 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004352 *
4353 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
4354 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004355static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004356resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Radek Krejci48464ed2016-03-17 15:44:09 +01004357 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004358{
Radek Krejci4f78b532016-02-17 13:43:00 +01004359 int rc = -1, has_str = 0, tpdf_flag = 0;
Michal Vasko563ef092015-09-04 13:17:23 +02004360 struct lys_node *node;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004361 const char *base_name;
4362
4363 struct lys_ident *ident;
4364 struct lys_type *stype;
4365 struct lys_feature **feat_ptr;
4366 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01004367 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01004368 struct yang_type *yang;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004369
4370 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004371 case UNRES_IDENT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004372 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004373 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004374 ident = item;
4375
Radek Krejci48464ed2016-03-17 15:44:09 +01004376 rc = resolve_base_ident(mod, ident, base_name, "identity", NULL);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004377 break;
4378 case UNRES_TYPE_IDENTREF:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004379 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004380 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004381 stype = item;
4382
Radek Krejci48464ed2016-03-17 15:44:09 +01004383 rc = resolve_base_ident(mod, NULL, base_name, "type", stype);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004384 break;
4385 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02004386 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004387 stype = item;
4388
Radek Krejci2f12f852016-01-08 12:59:57 +01004389 /* HACK - when there is no parent, we are in top level typedef and in that
4390 * case, the path has to contain absolute path, so we let the resolve_path_arg_schema()
4391 * know it via tpdf_flag */
4392 if (!node) {
Radek Krejci4f78b532016-02-17 13:43:00 +01004393 tpdf_flag = 1;
Radek Krejci2f12f852016-01-08 12:59:57 +01004394 node = (struct lys_node *)stype->parent;
4395 }
4396
Radek Krejci48464ed2016-03-17 15:44:09 +01004397 rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag,
Michal Vasko1e62a092015-12-01 12:27:20 +01004398 (const struct lys_node **)&stype->info.lref.target);
Radek Krejci46c4cd72016-01-21 15:13:52 +01004399 if (stype->info.lref.target) {
4400 /* store the backlink from leafref target */
4401 if (!stype->info.lref.target->child) {
4402 stype->info.lref.target->child = (void*)ly_set_new();
4403 if (!stype->info.lref.target->child) {
4404 LOGMEM;
4405 return -1;
4406 }
4407 }
4408 ly_set_add((struct ly_set *)stype->info.lref.target->child, stype->parent);
4409 }
4410
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004411 break;
4412 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01004413 /* parent */
4414 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004415 stype = item;
4416
Michal Vasko88c29542015-11-27 14:57:53 +01004417 /* HACK type->der is temporarily unparsed type statement */
4418 yin = (struct lyxml_elem *)stype->der;
4419 stype->der = NULL;
4420
Pavol Vicana0e4e672016-02-24 12:20:04 +01004421 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
4422 yang = (struct yang_type *)yin;
4423 rc = yang_check_type(mod, node, yang, unres);
4424
4425 if (rc) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02004426 if (rc == -1) {
4427 yang->type->base = yang->base;
4428 lydict_remove(mod->ctx, yang->name);
4429 free(yang);
4430 stype->der = NULL;
4431 } else {
4432 /* may try again later */
4433 stype->der = (struct lys_tpdf *)yang;
4434 }
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004435 } else {
4436 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02004437 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004438 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01004439 }
4440
Michal Vasko88c29542015-11-27 14:57:53 +01004441 } else {
Pavol Vicana0e4e672016-02-24 12:20:04 +01004442 rc = fill_yin_type(mod, node, yin, stype, unres);
4443 if (!rc) {
4444 /* we need to always be able to free this, it's safe only in this case */
4445 lyxml_free(mod->ctx, yin);
4446 } else {
4447 /* may try again later, put all back how it was */
4448 stype->der = (struct lys_tpdf *)yin;
4449 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004450 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004451 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004452 case UNRES_IFFEAT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004453 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004454 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004455 feat_ptr = item;
4456
Radek Krejci48464ed2016-03-17 15:44:09 +01004457 rc = resolve_feature(base_name, mod, feat_ptr);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004458 break;
4459 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01004460 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004461 break;
4462 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004463 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004464 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004465 stype = item;
4466
Radek Krejci48464ed2016-03-17 15:44:09 +01004467 rc = check_default(stype, base_name, mod);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004468 break;
4469 case UNRES_CHOICE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004470 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004471 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004472 choic = item;
4473
Michal Vasko7955b362015-09-04 14:18:15 +02004474 choic->dflt = resolve_choice_dflt(choic, base_name);
4475 if (choic->dflt) {
4476 rc = EXIT_SUCCESS;
4477 } else {
4478 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004479 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004480 break;
4481 case UNRES_LIST_KEYS:
Radek Krejci4f78b532016-02-17 13:43:00 +01004482 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01004483 rc = resolve_list_keys(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004484 break;
4485 case UNRES_LIST_UNIQ:
Radek Krejci4f78b532016-02-17 13:43:00 +01004486 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01004487 rc = resolve_unique(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004488 break;
Michal Vasko7178e692016-02-12 15:58:05 +01004489 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004490 rc = resolve_augment(item, NULL);
Michal Vasko7178e692016-02-12 15:58:05 +01004491 break;
Michal Vaskocf024702015-10-08 15:01:42 +02004492 default:
4493 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004494 break;
4495 }
4496
Radek Krejci4f78b532016-02-17 13:43:00 +01004497 if (has_str && !rc) {
4498 lydict_remove(mod->ctx, str_snode);
4499 }
4500
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004501 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004502}
4503
Michal Vaskof02e3742015-08-05 16:27:02 +02004504/* logs directly */
4505static void
Radek Krejci48464ed2016-03-17 15:44:09 +01004506print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004507{
Michal Vaskof02e3742015-08-05 16:27:02 +02004508 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02004509 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004510 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004511 break;
4512 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01004513 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004514 break;
4515 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01004516 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
4517 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02004518 break;
4519 case UNRES_TYPE_DER:
Radek Krejci48464ed2016-03-17 15:44:09 +01004520 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type",
4521 ((struct lyxml_elem *)((struct lys_type *)item)->der)->attr->value);
Michal Vaskof02e3742015-08-05 16:27:02 +02004522 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02004523 case UNRES_IFFEAT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004524 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004525 break;
4526 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01004527 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02004528 break;
4529 case UNRES_TYPE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004530 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004531 break;
4532 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004533 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004534 break;
4535 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01004536 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004537 break;
4538 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01004539 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004540 break;
Michal Vasko7178e692016-02-12 15:58:05 +01004541 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004542 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
4543 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01004544 break;
Michal Vaskocf024702015-10-08 15:01:42 +02004545 default:
4546 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02004547 break;
4548 }
4549}
4550
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004551/**
Michal Vaskobb211122015-08-19 14:03:11 +02004552 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004553 *
4554 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004555 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004556 *
Michal Vasko92b8a382015-08-19 14:03:49 +02004557 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004558 */
Michal Vaskof02e3742015-08-05 16:27:02 +02004559int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004560resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02004561{
Radek Krejci010e54b2016-03-15 09:40:34 +01004562 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004563 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004564
4565 assert(unres);
4566
Radek Krejci010e54b2016-03-15 09:40:34 +01004567 LOGVRB("Resolving unresolved schema nodes and their constraints.");
4568 ly_vlog_hide(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02004569
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004570 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02004571 do {
Michal Vasko88c29542015-11-27 14:57:53 +01004572 unres_count = 0;
4573 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02004574
4575 for (i = 0; i < unres->count; ++i) {
Michal Vasko88c29542015-11-27 14:57:53 +01004576 /* we do not need to have UNRES_TYPE_IDENTREF or UNRES_TYPE_LEAFREF resolved,
4577 * we need every type's base only */
4578 if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_TYPE_DER)) {
Michal Vasko51054ca2015-08-12 12:20:00 +02004579 continue;
4580 }
4581
Michal Vasko88c29542015-11-27 14:57:53 +01004582 ++unres_count;
Radek Krejci48464ed2016-03-17 15:44:09 +01004583 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004584 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02004585 unres->type[i] = UNRES_RESOLVED;
4586 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01004587 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02004588 } else if (rc == -1) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004589 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004590 return -1;
Michal Vasko51054ca2015-08-12 12:20:00 +02004591 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004592 }
Michal Vasko88c29542015-11-27 14:57:53 +01004593 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02004594
Michal Vasko88c29542015-11-27 14:57:53 +01004595 if (res_count < unres_count) {
Michal Vasko92b8a382015-08-19 14:03:49 +02004596 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004597 }
4598
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004599 /* the rest */
4600 for (i = 0; i < unres->count; ++i) {
4601 if (unres->type[i] == UNRES_RESOLVED) {
4602 continue;
4603 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02004604
Radek Krejci48464ed2016-03-17 15:44:09 +01004605 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004606 if (rc == 0) {
4607 unres->type[i] = UNRES_RESOLVED;
4608 ++resolved;
4609 } else if (rc == -1) {
4610 ly_vlog_hide(0);
Michal Vasko184521f2015-09-24 13:14:26 +02004611 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004612 }
4613 }
4614
Radek Krejci010e54b2016-03-15 09:40:34 +01004615 ly_vlog_hide(0);
4616
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004617 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004618 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
4619 * all the validation errors
4620 */
4621 for (i = 0; i < unres->count; ++i) {
4622 if (unres->type[i] == UNRES_RESOLVED) {
4623 continue;
4624 }
Radek Krejci48464ed2016-03-17 15:44:09 +01004625 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004626 }
Michal Vasko92b8a382015-08-19 14:03:49 +02004627 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004628 }
4629
Radek Krejci010e54b2016-03-15 09:40:34 +01004630 LOGVRB("Resolving unresolved schema nodes and their constraints.");
4631
Radek Krejcic071c542016-01-27 14:57:51 +01004632 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004633 return EXIT_SUCCESS;
4634}
4635
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004636/**
Michal Vaskobb211122015-08-19 14:03:11 +02004637 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004638 *
4639 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004640 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004641 * @param[in] item Item to resolve. Type determined by \p type.
4642 * @param[in] type Type of the unresolved item.
4643 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004644 *
4645 * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error.
4646 */
4647int
Radek Krejci48464ed2016-03-17 15:44:09 +01004648unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
4649 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004650{
Radek Krejci48464ed2016-03-17 15:44:09 +01004651 return unres_schema_add_node(mod, unres, item, type, (struct lys_node *)lydict_insert(mod->ctx, str, 0));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004652}
4653
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004654/**
Michal Vaskobb211122015-08-19 14:03:11 +02004655 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004656 *
4657 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004658 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004659 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01004660 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004661 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004662 *
4663 * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error.
4664 */
4665int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004666unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01004667 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004668{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004669 int rc;
Michal Vasko88c29542015-11-27 14:57:53 +01004670 struct lyxml_elem *yin;
Radek Krejci010e54b2016-03-15 09:40:34 +01004671 char *path, *msg;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004672
Michal Vasko9bf425b2015-10-22 11:42:03 +02004673 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
4674 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004675
Radek Krejci010e54b2016-03-15 09:40:34 +01004676 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01004677 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004678 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004679 if (rc != EXIT_FAILURE) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004680 if (rc == -1 && ly_errno == LY_EVALID) {
4681 path = strdup(ly_errpath());
4682 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()),
4683 path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
4684 free(path);
4685 free(msg);
4686 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004687 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004688 }
4689
Radek Krejci48464ed2016-03-17 15:44:09 +01004690 print_unres_schema_item_fail(item, type, snode);
Michal Vaskof02e3742015-08-05 16:27:02 +02004691
Michal Vasko88c29542015-11-27 14:57:53 +01004692 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
4693 if (type == UNRES_TYPE_DER) {
4694 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
Pavol Vicana0e4e672016-02-24 12:20:04 +01004695 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
4696 lyxml_unlink_elem(mod->ctx, yin, 1);
4697 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
4698 }
Michal Vasko88c29542015-11-27 14:57:53 +01004699 }
4700
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004701 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01004702 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
4703 if (!unres->item) {
4704 LOGMEM;
4705 return -1;
4706 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004707 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01004708 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
4709 if (!unres->type) {
4710 LOGMEM;
4711 return -1;
4712 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004713 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01004714 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
4715 if (!unres->str_snode) {
4716 LOGMEM;
4717 return -1;
4718 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004719 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01004720 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
4721 if (!unres->module) {
4722 LOGMEM;
4723 return -1;
4724 }
4725 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004726
4727 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004728}
4729
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004730/**
Michal Vaskobb211122015-08-19 14:03:11 +02004731 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004732 *
4733 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004734 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004735 * @param[in] item Old item to be resolved.
4736 * @param[in] type Type of the old unresolved item.
4737 * @param[in] new_item New item to use in the duplicate.
4738 *
4739 * @return EXIT_SUCCESS on success, -1 on error.
4740 */
Michal Vaskodad19402015-08-06 09:51:53 +02004741int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004742unres_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 +02004743{
4744 int i;
4745
Michal Vaskocf024702015-10-08 15:01:42 +02004746 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004747
Michal Vasko0bd29d12015-08-19 11:45:49 +02004748 i = unres_schema_find(unres, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004749
4750 if (i == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004751 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004752 }
4753
Michal Vasko0d204592015-10-07 09:50:04 +02004754 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004755 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004756 LOGINT;
4757 return -1;
4758 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004759 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004760 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004761 LOGINT;
4762 return -1;
4763 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004764 }
Michal Vaskodad19402015-08-06 09:51:53 +02004765
4766 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004767}
4768
Michal Vaskof02e3742015-08-05 16:27:02 +02004769/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004770int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004771unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004772{
4773 uint32_t ret = -1, i;
4774
4775 for (i = 0; i < unres->count; ++i) {
4776 if ((unres->item[i] == item) && (unres->type[i] == type)) {
4777 ret = i;
4778 break;
4779 }
4780 }
4781
4782 return ret;
4783}
Michal Vasko8bcdf292015-08-19 14:04:43 +02004784
Michal Vasko88c29542015-11-27 14:57:53 +01004785void
Radek Krejcic071c542016-01-27 14:57:51 +01004786unres_schema_free(struct lys_module *module, struct unres_schema **unres)
Michal Vasko88c29542015-11-27 14:57:53 +01004787{
4788 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01004789 unsigned int unresolved = 0;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004790 struct lyxml_elem *yin;
4791 struct yang_type *yang;
Michal Vasko88c29542015-11-27 14:57:53 +01004792
Radek Krejcic071c542016-01-27 14:57:51 +01004793 if (!unres || !(*unres)) {
4794 return;
Michal Vasko88c29542015-11-27 14:57:53 +01004795 }
4796
Radek Krejcic071c542016-01-27 14:57:51 +01004797 assert(module || (*unres)->count == 0);
4798
4799 for (i = 0; i < (*unres)->count; ++i) {
4800 if ((*unres)->module[i] != module) {
4801 if ((*unres)->type[i] != UNRES_RESOLVED) {
4802 unresolved++;
4803 }
4804 continue;
4805 }
4806 if ((*unres)->type[i] == UNRES_TYPE_DER) {
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004807 yin = (struct lyxml_elem *)((struct lys_type *)(*unres)->item[i])->der;
4808 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
4809 yang =(struct yang_type *)yin;
Pavol Vican6b072512016-04-04 10:50:21 +02004810 yang->type->base = yang->base;
Pavol Vican5f0316a2016-04-05 21:21:11 +02004811 lydict_remove(module->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004812 free(yang);
4813 } else {
4814 lyxml_free(module->ctx, yin);
4815 }
Radek Krejcic071c542016-01-27 14:57:51 +01004816 }
4817 (*unres)->type[i] = UNRES_RESOLVED;
4818 }
4819
4820 if (!module || (!unresolved && !module->type)) {
4821 free((*unres)->item);
4822 free((*unres)->type);
4823 free((*unres)->str_snode);
4824 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01004825 free((*unres));
4826 (*unres) = NULL;
4827 }
Michal Vasko88c29542015-11-27 14:57:53 +01004828}
4829
Michal Vasko8bcdf292015-08-19 14:04:43 +02004830/**
4831 * @brief Resolve a single unres data item. Logs directly.
4832 *
Michal Vaskocf024702015-10-08 15:01:42 +02004833 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02004834 * @param[in] type Type of the unresolved item.
Michal Vasko8bcdf292015-08-19 14:04:43 +02004835 *
4836 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
4837 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02004838int
Radek Krejci48464ed2016-03-17 15:44:09 +01004839resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02004840{
4841 uint32_t i;
Michal Vasko0491ab32015-08-19 14:28:29 +02004842 int rc;
Michal Vasko83a6c462015-10-08 16:43:53 +02004843 struct lyd_node_leaf_list *leaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004844 struct lys_node_leaf *sleaf;
4845 struct unres_data matches;
4846
4847 memset(&matches, 0, sizeof matches);
Michal Vasko83a6c462015-10-08 16:43:53 +02004848 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02004849 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004850
Michal Vaskocf024702015-10-08 15:01:42 +02004851 switch (type) {
4852 case UNRES_LEAFREF:
4853 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Radek Krejci48464ed2016-03-17 15:44:09 +01004854 if ((rc = resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches))) {
Michal Vasko0491ab32015-08-19 14:28:29 +02004855 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004856 }
4857
4858 /* check that value matches */
4859 for (i = 0; i < matches.count; ++i) {
Radek Krejci749190d2016-02-18 16:26:25 +01004860 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) {
Michal Vaskocf024702015-10-08 15:01:42 +02004861 leaf->value.leafref = matches.node[i];
Michal Vasko8bcdf292015-08-19 14:04:43 +02004862 break;
4863 }
4864 }
4865
Michal Vaskocf024702015-10-08 15:01:42 +02004866 free(matches.node);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004867 memset(&matches, 0, sizeof matches);
4868
Michal Vaskocf024702015-10-08 15:01:42 +02004869 if (!leaf->value.leafref) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02004870 /* reference not found */
Radek Krejci48464ed2016-03-17 15:44:09 +01004871 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path);
4872 LOGVAL(LYE_SPEC, LY_VLOG_LYD, leaf, "Leafref value \"%s\" did not match any node value.", leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004873 return EXIT_FAILURE;
4874 }
Michal Vaskocf024702015-10-08 15:01:42 +02004875 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004876
Michal Vaskocf024702015-10-08 15:01:42 +02004877 case UNRES_INSTID:
4878 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004879 ly_errno = 0;
Radek Krejci48464ed2016-03-17 15:44:09 +01004880 leaf->value.instance = resolve_instid(node, leaf->value_str);
Radek Krejci40f17b92016-02-03 14:30:43 +01004881 if (!leaf->value.instance) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02004882 if (ly_errno) {
4883 return -1;
4884 } else if (sleaf->type.info.inst.req > -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004885 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, leaf, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004886 return EXIT_FAILURE;
4887 } else {
Radek Krejci4ce42be2016-02-03 13:04:41 +01004888 LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004889 }
4890 }
Michal Vaskocf024702015-10-08 15:01:42 +02004891 break;
4892
4893 case UNRES_WHEN:
Radek Krejci48464ed2016-03-17 15:44:09 +01004894 if ((rc = resolve_when(node))) {
Michal Vaskocf024702015-10-08 15:01:42 +02004895 return rc;
4896 }
4897 break;
4898
Michal Vaskobf19d252015-10-08 15:39:17 +02004899 case UNRES_MUST:
Radek Krejci48464ed2016-03-17 15:44:09 +01004900 if ((rc = resolve_must(node))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004901 return rc;
4902 }
4903 break;
4904
Michal Vaskocf024702015-10-08 15:01:42 +02004905 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02004906 LOGINT;
4907 return -1;
4908 }
4909
4910 return EXIT_SUCCESS;
4911}
4912
4913/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01004914 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02004915 *
4916 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02004917 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02004918 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01004919 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02004920 */
4921int
Radek Krejci0b7704f2016-03-18 12:16:14 +01004922unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02004923{
Radek Krejci03b71f72016-03-16 11:10:09 +01004924 assert(unres && node);
4925 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST));
Michal Vasko8bcdf292015-08-19 14:04:43 +02004926
Radek Krejci03b71f72016-03-16 11:10:09 +01004927 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01004928 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
4929 if (!unres->node) {
4930 LOGMEM;
4931 return -1;
4932 }
Michal Vaskocf024702015-10-08 15:01:42 +02004933 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01004934 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
4935 if (!unres->type) {
4936 LOGMEM;
4937 return -1;
4938 }
Michal Vaskocf024702015-10-08 15:01:42 +02004939 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004940
Radek Krejci0b7704f2016-03-18 12:16:14 +01004941 if (type == UNRES_WHEN) {
4942 /* remove previous result */
4943 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004944 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004945
4946 return EXIT_SUCCESS;
4947}
4948
4949/**
4950 * @brief Resolve every unres data item in the structure. Logs directly.
4951 *
4952 * @param[in] unres Unres data structure to use.
Radek Krejci03b71f72016-03-16 11:10:09 +01004953 * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If
4954 * NULL and when condition is false the error is raised.
Radek Krejci0c0086a2016-03-24 15:20:28 +01004955 * @param[in] options Parer options
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004956 *
4957 * @return EXIT_SUCCESS on success, -1 on error.
4958 */
4959int
Radek Krejci0c0086a2016-03-24 15:20:28 +01004960resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004961{
Radek Krejci0c0086a2016-03-24 15:20:28 +01004962 uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01004963 int rc, progress;
Radek Krejci03b71f72016-03-16 11:10:09 +01004964 char *msg, *path;
Radek Krejci0b7704f2016-03-18 12:16:14 +01004965 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004966
Radek Krejci03b71f72016-03-16 11:10:09 +01004967 assert(unres);
Radek Krejci8ee1b562016-03-31 10:58:31 +02004968 assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL));
Radek Krejci03b71f72016-03-16 11:10:09 +01004969
4970 if (!unres->count) {
4971 return EXIT_SUCCESS;
4972 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004973
Radek Krejci010e54b2016-03-15 09:40:34 +01004974 LOGVRB("Resolving unresolved data nodes and their constraints.");
4975 ly_vlog_hide(1);
4976
Radek Krejci0b7704f2016-03-18 12:16:14 +01004977 /* when-stmt first */
Radek Krejci03b71f72016-03-16 11:10:09 +01004978 ly_errno = LY_SUCCESS;
4979 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01004980 do {
4981 progress = 0;
4982 for(i = 0; i < unres->count; i++) {
4983 if (unres->type[i] != UNRES_WHEN) {
4984 continue;
4985 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01004986 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01004987 /* count when-stmt nodes in unres list */
4988 when_stmt++;
4989 }
4990
4991 /* resolve when condition only when all parent when conditions are already resolved */
4992 for (parent = unres->node[i]->parent;
4993 parent && LYD_WHEN_DONE(parent->when_status);
4994 parent = parent->parent) {
4995 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
4996 /* the parent node was already unlinked, do not resolve this node,
4997 * it will be removed anyway, so just mark it as resolved
4998 */
4999 unres->node[i]->when_status |= LYD_WHEN_FALSE;
5000 unres->type[i] = UNRES_RESOLVED;
5001 resolved++;
5002 break;
5003 }
5004 }
5005 if (parent) {
5006 continue;
5007 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005008
Radek Krejci48464ed2016-03-17 15:44:09 +01005009 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005010 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005011 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
5012 if (!root) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005013 /* false when condition */
5014 ly_vlog_hide(0);
5015 path = strdup(ly_errpath());
5016 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "",
5017 path[0] ? path : "", path[0] ? ")" : "");
5018 free(path);
5019 free(msg);
5020 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005021 } /* follows else */
5022
Radek Krejci0c0086a2016-03-24 15:20:28 +01005023 /* only unlink now, the subtree can contain another nodes stored in the unres list */
5024 /* if it has parent non-presence containers that would be empty, we should actually
5025 * remove the container
5026 */
5027 if (!(options & LYD_OPT_KEEPEMPTYCONT)) {
5028 for (parent = unres->node[i];
5029 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
5030 parent = parent->parent) {
5031 if (((struct lys_node_container *)parent->parent->schema)->presence) {
5032 /* presence container */
5033 break;
5034 }
5035 if (parent->next || parent->prev != parent) {
5036 /* non empty (the child we are in and we are going to remove is not the only child) */
5037 break;
5038 }
5039 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005040 unres->node[i] = parent;
5041 }
5042
Radek Krejci0b7704f2016-03-18 12:16:14 +01005043 /* auto-delete */
5044 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
5045 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
Radek Krejci0c0086a2016-03-24 15:20:28 +01005046 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005047 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01005048 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005049
Radek Krejci0b7704f2016-03-18 12:16:14 +01005050 lyd_unlink(unres->node[i]);
5051 unres->type[i] = UNRES_DELETE;
5052 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01005053
5054 /* update the rest of unres items */
5055 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01005056 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01005057 continue;
5058 }
5059
5060 /* test if the node is in subtree to be deleted */
5061 for (parent = unres->node[j]; parent; parent = parent->parent) {
5062 if (parent == unres->node[i]) {
5063 /* yes, it is */
5064 unres->type[j] = UNRES_RESOLVED;
5065 resolved++;
5066 break;
5067 }
5068 }
5069 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005070 } else {
5071 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01005072 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005073 ly_errno = LY_SUCCESS;
5074 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005075 resolved++;
5076 progress = 1;
5077 } else if (rc == -1) {
5078 ly_vlog_hide(0);
5079 return -1;
5080 }
5081 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005082 first = 0;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005083 } while (progress && resolved < when_stmt);
Radek Krejci010e54b2016-03-15 09:40:34 +01005084
Radek Krejci0b7704f2016-03-18 12:16:14 +01005085 /* do we have some unresolved when-stmt? */
Radek Krejcid940d732016-03-24 16:02:28 +01005086 if (when_stmt > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005087 ly_vlog_hide(0);
5088 path = strdup(ly_errpath());
5089 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "",
5090 path[0] ? path : "", path[0] ? ")" : "");
5091 free(path);
5092 free(msg);
5093 return -1;
5094 }
5095
5096 for (i = 0; del_items && i < unres->count; i++) {
5097 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
5098 if (unres->type[i] != UNRES_DELETE) {
5099 continue;
5100 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005101 if (!unres->node[i]) {
5102 unres->type[i] = UNRES_RESOLVED;
5103 del_items--;
5104 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005105 }
5106
5107 /* really remove the complete subtree */
5108 lyd_free(unres->node[i]);
5109 unres->type[i] = UNRES_RESOLVED;
5110 del_items--;
5111 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005112
5113 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005114 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005115 if (unres->type[i] == UNRES_RESOLVED) {
5116 continue;
5117 }
5118
Radek Krejci48464ed2016-03-17 15:44:09 +01005119 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005120 if (rc == 0) {
5121 unres->type[i] = UNRES_RESOLVED;
5122 resolved++;
5123 } else if (rc == -1) {
5124 ly_vlog_hide(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005125 return -1;
5126 }
5127 }
5128
Radek Krejci010e54b2016-03-15 09:40:34 +01005129 ly_vlog_hide(0);
5130 if (resolved < unres->count) {
5131 /* try to resolve the unresolved data again, it will not resolve anything, but it will print
5132 * all the validation errors
5133 */
5134 for (i = 0; i < unres->count; ++i) {
5135 if (unres->type[i] == UNRES_RESOLVED) {
5136 continue;
5137 }
Radek Krejci48464ed2016-03-17 15:44:09 +01005138 resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005139 }
5140 return -1;
5141 }
5142
5143 LOGVRB("All data nodes and constraints resolved");
5144 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005145 return EXIT_SUCCESS;
5146}