blob: 6079969e1e6e2198d6492e686a7ec3431513c89e [file] [log] [blame]
Michal Vasko730dfdf2015-08-11 14:48:05 +02001/**
2 * @file resolve.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang resolve functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko730dfdf2015-08-11 14:48:05 +020013 */
14
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
16
17#include <stdlib.h>
18#include <assert.h>
19#include <string.h>
20#include <ctype.h>
Michal Vaskoe7fc19c2015-08-05 16:24:39 +020021#include <limits.h>
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020022
23#include "libyang.h"
24#include "resolve.h"
25#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020026#include "xpath.h"
Michal Vasko1dca6882015-10-22 14:29:42 +020027#include "parser.h"
Pavol Vicana0e4e672016-02-24 12:20:04 +010028#include "parser_yang.h"
Michal Vasko88c29542015-11-27 14:57:53 +010029#include "xml_internal.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020030#include "dict_private.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020031#include "tree_internal.h"
32
Michal Vasko730dfdf2015-08-11 14:48:05 +020033/**
Radek Krejci6dc53a22015-08-17 13:27:59 +020034 * @brief Parse an identifier.
35 *
36 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
37 * identifier = (ALPHA / "_")
38 * *(ALPHA / DIGIT / "_" / "-" / ".")
39 *
Michal Vaskobb211122015-08-19 14:03:11 +020040 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +020041 *
42 * @return Number of characters successfully parsed.
43 */
Michal Vasko249e6b52015-08-19 11:08:52 +020044int
Radek Krejci6dc53a22015-08-17 13:27:59 +020045parse_identifier(const char *id)
46{
47 int parsed = 0;
48
Michal Vasko1ab90bc2016-03-15 10:40:22 +010049 assert(id);
50
Radek Krejci6dc53a22015-08-17 13:27:59 +020051 if (((id[0] == 'x') || (id[0] == 'X'))
Michal Vasko1ab90bc2016-03-15 10:40:22 +010052 && (id[0] && ((id[1] == 'm') || (id[0] == 'M')))
53 && (id[1] && ((id[2] == 'l') || (id[2] == 'L')))) {
Radek Krejci6dc53a22015-08-17 13:27:59 +020054 return -parsed;
55 }
56
57 if (!isalpha(id[0]) && (id[0] != '_')) {
58 return -parsed;
59 }
60
61 ++parsed;
62 ++id;
63
64 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
65 ++parsed;
66 ++id;
67 }
68
69 return parsed;
70}
71
72/**
73 * @brief Parse a node-identifier.
74 *
Michal Vasko723e50c2015-10-20 15:20:29 +020075 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +020076 *
Michal Vaskobb211122015-08-19 14:03:11 +020077 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +020078 * @param[out] mod_name Points to the module name, NULL if there is not any.
79 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +020080 * @param[out] name Points to the node name.
81 * @param[out] nam_len Length of the node name.
82 *
83 * @return Number of characters successfully parsed,
84 * positive on success, negative on failure.
85 */
86static int
Michal Vasko723e50c2015-10-20 15:20:29 +020087parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len)
Radek Krejci6dc53a22015-08-17 13:27:59 +020088{
89 int parsed = 0, ret;
90
91 assert(id);
Michal Vasko723e50c2015-10-20 15:20:29 +020092 if (mod_name) {
93 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +020094 }
Michal Vasko723e50c2015-10-20 15:20:29 +020095 if (mod_name_len) {
96 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +020097 }
98 if (name) {
99 *name = NULL;
100 }
101 if (nam_len) {
102 *nam_len = 0;
103 }
104
105 if ((ret = parse_identifier(id)) < 1) {
106 return ret;
107 }
108
Michal Vasko723e50c2015-10-20 15:20:29 +0200109 if (mod_name) {
110 *mod_name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200111 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200112 if (mod_name_len) {
113 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200114 }
115
116 parsed += ret;
117 id += ret;
118
119 /* there is prefix */
120 if (id[0] == ':') {
121 ++parsed;
122 ++id;
123
124 /* there isn't */
125 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200126 if (name && mod_name) {
127 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200128 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200129 if (mod_name) {
130 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200131 }
132
Michal Vasko723e50c2015-10-20 15:20:29 +0200133 if (nam_len && mod_name_len) {
134 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200135 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200136 if (mod_name_len) {
137 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200138 }
139
140 return parsed;
141 }
142
143 /* identifier (node name) */
144 if ((ret = parse_identifier(id)) < 1) {
145 return -parsed+ret;
146 }
147
148 if (name) {
149 *name = id;
150 }
151 if (nam_len) {
152 *nam_len = ret;
153 }
154
155 return parsed+ret;
156}
157
158/**
159 * @brief Parse a path-predicate (leafref).
160 *
161 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
162 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
163 *
Michal Vaskobb211122015-08-19 14:03:11 +0200164 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200165 * @param[out] prefix Points to the prefix, NULL if there is not any.
166 * @param[out] pref_len Length of the prefix, 0 if there is not any.
167 * @param[out] name Points to the node name.
168 * @param[out] nam_len Length of the node name.
169 * @param[out] path_key_expr Points to the path-key-expr.
170 * @param[out] pke_len Length of the path-key-expr.
171 * @param[out] has_predicate Flag to mark whether there is another predicate following.
172 *
173 * @return Number of characters successfully parsed,
174 * positive on success, negative on failure.
175 */
176static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200177parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
178 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200179{
180 const char *ptr;
181 int parsed = 0, ret;
182
183 assert(id);
184 if (prefix) {
185 *prefix = NULL;
186 }
187 if (pref_len) {
188 *pref_len = 0;
189 }
190 if (name) {
191 *name = NULL;
192 }
193 if (nam_len) {
194 *nam_len = 0;
195 }
196 if (path_key_expr) {
197 *path_key_expr = NULL;
198 }
199 if (pke_len) {
200 *pke_len = 0;
201 }
202 if (has_predicate) {
203 *has_predicate = 0;
204 }
205
206 if (id[0] != '[') {
207 return -parsed;
208 }
209
210 ++parsed;
211 ++id;
212
213 while (isspace(id[0])) {
214 ++parsed;
215 ++id;
216 }
217
218 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
219 return -parsed+ret;
220 }
221
222 parsed += ret;
223 id += ret;
224
225 while (isspace(id[0])) {
226 ++parsed;
227 ++id;
228 }
229
230 if (id[0] != '=') {
231 return -parsed;
232 }
233
234 ++parsed;
235 ++id;
236
237 while (isspace(id[0])) {
238 ++parsed;
239 ++id;
240 }
241
242 if ((ptr = strchr(id, ']')) == NULL) {
243 return -parsed;
244 }
245
246 --ptr;
247 while (isspace(ptr[0])) {
248 --ptr;
249 }
250 ++ptr;
251
252 ret = ptr-id;
253 if (path_key_expr) {
254 *path_key_expr = id;
255 }
256 if (pke_len) {
257 *pke_len = ret;
258 }
259
260 parsed += ret;
261 id += ret;
262
263 while (isspace(id[0])) {
264 ++parsed;
265 ++id;
266 }
267
268 assert(id[0] == ']');
269
270 if (id[1] == '[') {
271 *has_predicate = 1;
272 }
273
274 return parsed+1;
275}
276
277/**
278 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
279 * the ".." and the first node-identifier, other calls parse a single
280 * node-identifier each.
281 *
282 * path-key-expr = current-function-invocation *WSP "/" *WSP
283 * rel-path-keyexpr
284 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
285 * *(node-identifier *WSP "/" *WSP)
286 * node-identifier
287 *
Michal Vaskobb211122015-08-19 14:03:11 +0200288 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200289 * @param[out] prefix Points to the prefix, NULL if there is not any.
290 * @param[out] pref_len Length of the prefix, 0 if there is not any.
291 * @param[out] name Points to the node name.
292 * @param[out] nam_len Length of the node name.
293 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
294 * must not be changed between consecutive calls.
295 * @return Number of characters successfully parsed,
296 * positive on success, negative on failure.
297 */
298static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200299parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
300 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200301{
302 int parsed = 0, ret, par_times = 0;
303
304 assert(id);
305 assert(parent_times);
306 if (prefix) {
307 *prefix = NULL;
308 }
309 if (pref_len) {
310 *pref_len = 0;
311 }
312 if (name) {
313 *name = NULL;
314 }
315 if (nam_len) {
316 *nam_len = 0;
317 }
318
319 if (!*parent_times) {
320 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
321 if (strncmp(id, "current()", 9)) {
322 return -parsed;
323 }
324
325 parsed += 9;
326 id += 9;
327
328 while (isspace(id[0])) {
329 ++parsed;
330 ++id;
331 }
332
333 if (id[0] != '/') {
334 return -parsed;
335 }
336
337 ++parsed;
338 ++id;
339
340 while (isspace(id[0])) {
341 ++parsed;
342 ++id;
343 }
344
345 /* rel-path-keyexpr */
346 if (strncmp(id, "..", 2)) {
347 return -parsed;
348 }
349 ++par_times;
350
351 parsed += 2;
352 id += 2;
353
354 while (isspace(id[0])) {
355 ++parsed;
356 ++id;
357 }
358 }
359
360 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
361 *
362 * first parent reference with whitespaces already parsed
363 */
364 if (id[0] != '/') {
365 return -parsed;
366 }
367
368 ++parsed;
369 ++id;
370
371 while (isspace(id[0])) {
372 ++parsed;
373 ++id;
374 }
375
376 while (!strncmp(id, "..", 2) && !*parent_times) {
377 ++par_times;
378
379 parsed += 2;
380 id += 2;
381
382 while (isspace(id[0])) {
383 ++parsed;
384 ++id;
385 }
386
387 if (id[0] != '/') {
388 return -parsed;
389 }
390
391 ++parsed;
392 ++id;
393
394 while (isspace(id[0])) {
395 ++parsed;
396 ++id;
397 }
398 }
399
400 if (!*parent_times) {
401 *parent_times = par_times;
402 }
403
404 /* all parent references must be parsed at this point */
405 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
406 return -parsed+ret;
407 }
408
409 parsed += ret;
410 id += ret;
411
412 return parsed;
413}
414
415/**
416 * @brief Parse path-arg (leafref).
417 *
418 * path-arg = absolute-path / relative-path
419 * absolute-path = 1*("/" (node-identifier *path-predicate))
420 * relative-path = 1*(".." "/") descendant-path
421 *
Michal Vaskobb211122015-08-19 14:03:11 +0200422 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200423 * @param[out] prefix Points to the prefix, NULL if there is not any.
424 * @param[out] pref_len Length of the prefix, 0 if there is not any.
425 * @param[out] name Points to the node name.
426 * @param[out] nam_len Length of the node name.
427 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
428 * must not be changed between consecutive calls. -1 if the
429 * path is relative.
430 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
431 *
432 * @return Number of characters successfully parsed,
433 * positive on success, negative on failure.
434 */
435static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200436parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times,
437 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200438{
439 int parsed = 0, ret, par_times = 0;
440
441 assert(id);
442 assert(parent_times);
443 if (prefix) {
444 *prefix = NULL;
445 }
446 if (pref_len) {
447 *pref_len = 0;
448 }
449 if (name) {
450 *name = NULL;
451 }
452 if (nam_len) {
453 *nam_len = 0;
454 }
455 if (has_predicate) {
456 *has_predicate = 0;
457 }
458
459 if (!*parent_times && !strncmp(id, "..", 2)) {
460 ++par_times;
461
462 parsed += 2;
463 id += 2;
464
465 while (!strncmp(id, "/..", 3)) {
466 ++par_times;
467
468 parsed += 3;
469 id += 3;
470 }
471 }
472
473 if (!*parent_times) {
474 if (par_times) {
475 *parent_times = par_times;
476 } else {
477 *parent_times = -1;
478 }
479 }
480
481 if (id[0] != '/') {
482 return -parsed;
483 }
484
485 /* skip '/' */
486 ++parsed;
487 ++id;
488
489 /* node-identifier ([prefix:]identifier) */
490 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
491 return -parsed-ret;
492 }
493
494 parsed += ret;
495 id += ret;
496
497 /* there is no predicate */
498 if ((id[0] == '/') || !id[0]) {
499 return parsed;
500 } else if (id[0] != '[') {
501 return -parsed;
502 }
503
504 if (has_predicate) {
505 *has_predicate = 1;
506 }
507
508 return parsed;
509}
510
511/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200512 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200513 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200514 *
515 * instance-identifier = 1*("/" (node-identifier *predicate))
516 *
Michal Vaskobb211122015-08-19 14:03:11 +0200517 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200518 * @param[out] model Points to the model name.
519 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200520 * @param[out] name Points to the node name.
521 * @param[out] nam_len Length of the node name.
522 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
523 *
524 * @return Number of characters successfully parsed,
525 * positive on success, negative on failure.
526 */
527static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200528parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
529 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200530{
531 int parsed = 0, ret;
532
533 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200534 if (model) {
535 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200536 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200537 if (mod_len) {
538 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200539 }
540 if (name) {
541 *name = NULL;
542 }
543 if (nam_len) {
544 *nam_len = 0;
545 }
546 if (has_predicate) {
547 *has_predicate = 0;
548 }
549
550 if (id[0] != '/') {
551 return -parsed;
552 }
553
554 ++parsed;
555 ++id;
556
Michal Vasko1f2cc332015-08-19 11:18:32 +0200557 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
Michal Vaskobea08e92016-05-18 13:28:08 +0200558 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200559 }
560
561 parsed += ret;
562 id += ret;
563
564 if ((id[0] == '[') && has_predicate) {
565 *has_predicate = 1;
566 }
567
568 return parsed;
569}
570
571/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200572 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200573 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200574 *
575 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
576 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
577 * ((DQUOTE string DQUOTE) /
578 * (SQUOTE string SQUOTE))
579 * pos = non-negative-integer-value
580 *
Michal Vaskobb211122015-08-19 14:03:11 +0200581 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200582 * @param[out] model Points to the model name.
583 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200584 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
585 * @param[out] nam_len Length of the node name.
586 * @param[out] value Value the node-identifier must have (string from the grammar),
587 * NULL if there is not any.
588 * @param[out] val_len Length of the value, 0 if there is not any.
589 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
590 *
591 * @return Number of characters successfully parsed,
592 * positive on success, negative on failure.
593 */
594static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200595parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
596 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200597{
598 const char *ptr;
599 int parsed = 0, ret;
600 char quote;
601
602 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200603 if (model) {
604 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200605 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200606 if (mod_len) {
607 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200608 }
609 if (name) {
610 *name = NULL;
611 }
612 if (nam_len) {
613 *nam_len = 0;
614 }
615 if (value) {
616 *value = NULL;
617 }
618 if (val_len) {
619 *val_len = 0;
620 }
621 if (has_predicate) {
622 *has_predicate = 0;
623 }
624
625 if (id[0] != '[') {
626 return -parsed;
627 }
628
629 ++parsed;
630 ++id;
631
632 while (isspace(id[0])) {
633 ++parsed;
634 ++id;
635 }
636
637 /* pos */
638 if (isdigit(id[0])) {
639 if (name) {
640 *name = id;
641 }
642
643 if (id[0] == '0') {
644 ++parsed;
645 ++id;
646
647 if (isdigit(id[0])) {
648 return -parsed;
649 }
650 }
651
652 while (isdigit(id[0])) {
653 ++parsed;
654 ++id;
655 }
656
657 if (nam_len) {
658 *nam_len = id-(*name);
659 }
660
661 /* "." */
662 } else if (id[0] == '.') {
663 if (name) {
664 *name = id;
665 }
666 if (nam_len) {
667 *nam_len = 1;
668 }
669
670 ++parsed;
671 ++id;
672
673 /* node-identifier */
674 } else {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200675 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200676 return -parsed+ret;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200677 } else if (model && !*model) {
678 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200679 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200680
681 parsed += ret;
682 id += ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200683 }
684
685 while (isspace(id[0])) {
686 ++parsed;
687 ++id;
688 }
689
690 if (id[0] != '=') {
691 return -parsed;
692 }
693
694 ++parsed;
695 ++id;
696
697 while (isspace(id[0])) {
698 ++parsed;
699 ++id;
700 }
701
702 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
703 if ((id[0] == '\"') || (id[0] == '\'')) {
704 quote = id[0];
705
706 ++parsed;
707 ++id;
708
709 if ((ptr = strchr(id, quote)) == NULL) {
710 return -parsed;
711 }
712 ret = ptr-id;
713
714 if (value) {
715 *value = id;
716 }
717 if (val_len) {
718 *val_len = ret;
719 }
720
721 parsed += ret+1;
722 id += ret+1;
723 } else {
724 return -parsed;
725 }
726
727 while (isspace(id[0])) {
728 ++parsed;
729 ++id;
730 }
731
732 if (id[0] != ']') {
733 return -parsed;
734 }
735
736 ++parsed;
737 ++id;
738
739 if ((id[0] == '[') && has_predicate) {
740 *has_predicate = 1;
741 }
742
743 return parsed;
744}
745
746/**
747 * @brief Parse schema-nodeid.
748 *
749 * schema-nodeid = absolute-schema-nodeid /
750 * descendant-schema-nodeid
751 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200752 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200753 * node-identifier
754 * absolute-schema-nodeid
755 *
Michal Vaskobb211122015-08-19 14:03:11 +0200756 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200757 * @param[out] mod_name Points to the module name, NULL if there is not any.
758 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200759 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200760 * @param[out] nam_len Length of the node name.
761 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
762 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100763 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
764 * based on the grammar, in those cases use NULL.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200765 *
766 * @return Number of characters successfully parsed,
767 * positive on success, negative on failure.
768 */
Michal Vasko22448d32016-03-16 13:17:29 +0100769int
Michal Vasko723e50c2015-10-20 15:20:29 +0200770parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100771 int *is_relative, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200772{
773 int parsed = 0, ret;
774
775 assert(id);
776 assert(is_relative);
Michal Vasko723e50c2015-10-20 15:20:29 +0200777 if (mod_name) {
778 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200779 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200780 if (mod_name_len) {
781 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200782 }
783 if (name) {
784 *name = NULL;
785 }
786 if (nam_len) {
787 *nam_len = 0;
788 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100789 if (has_predicate) {
790 *has_predicate = 0;
791 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200792
793 if (id[0] != '/') {
794 if (*is_relative != -1) {
795 return -parsed;
796 } else {
797 *is_relative = 1;
798 }
Michal Vasko48935352016-03-29 11:52:36 +0200799 if (!strncmp(id, "./", 2)) {
800 parsed += 2;
801 id += 2;
802 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200803 } else {
804 if (*is_relative == -1) {
805 *is_relative = 0;
806 }
807 ++parsed;
808 ++id;
809 }
810
Michal Vasko723e50c2015-10-20 15:20:29 +0200811 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200812 return -parsed+ret;
813 }
814
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100815 parsed += ret;
816 id += ret;
817
818 if ((id[0] == '[') && has_predicate) {
819 *has_predicate = 1;
820 }
821
822 return parsed;
823}
824
825/**
826 * @brief Parse schema predicate (special format internally used).
827 *
828 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200829 * predicate-expr = "." / identifier / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100830 * key-with-value = identifier *WSP "=" *WSP
831 * ((DQUOTE string DQUOTE) /
832 * (SQUOTE string SQUOTE))
833 *
834 * @param[in] id Identifier to use.
835 * @param[out] name Points to the list key name.
836 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100837 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100838 * @param[out] val_len Length of \p value.
839 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
840 */
Michal Vasko22448d32016-03-16 13:17:29 +0100841int
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200842parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len,
Michal Vasko3547c532016-03-14 09:40:50 +0100843 int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100844{
845 const char *ptr;
846 int parsed = 0, ret;
847 char quote;
848
849 assert(id);
850 if (name) {
851 *name = NULL;
852 }
853 if (nam_len) {
854 *nam_len = 0;
855 }
856 if (value) {
857 *value = NULL;
858 }
859 if (val_len) {
860 *val_len = 0;
861 }
862 if (has_predicate) {
863 *has_predicate = 0;
864 }
865
866 if (id[0] != '[') {
867 return -parsed;
868 }
869
870 ++parsed;
871 ++id;
872
873 while (isspace(id[0])) {
874 ++parsed;
875 ++id;
876 }
877
Michal Vasko22448d32016-03-16 13:17:29 +0100878 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200879 if (id[0] == '.') {
880 ret = 1;
881 } else if ((ret = parse_identifier(id)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100882 return -parsed + ret;
883 }
884 if (name) {
885 *name = id;
886 }
887 if (nam_len) {
888 *nam_len = ret;
889 }
890
891 parsed += ret;
892 id += ret;
893
894 while (isspace(id[0])) {
895 ++parsed;
896 ++id;
897 }
898
899 /* there is value as well */
900 if (id[0] == '=') {
901 ++parsed;
902 ++id;
903
904 while (isspace(id[0])) {
905 ++parsed;
906 ++id;
907 }
908
909 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
910 if ((id[0] == '\"') || (id[0] == '\'')) {
911 quote = id[0];
912
913 ++parsed;
914 ++id;
915
916 if ((ptr = strchr(id, quote)) == NULL) {
917 return -parsed;
918 }
919 ret = ptr - id;
920
921 if (value) {
922 *value = id;
923 }
924 if (val_len) {
925 *val_len = ret;
926 }
927
928 parsed += ret + 1;
929 id += ret + 1;
930 } else {
931 return -parsed;
932 }
933
934 while (isspace(id[0])) {
935 ++parsed;
936 ++id;
937 }
Michal Vasko22448d32016-03-16 13:17:29 +0100938 } else if (value) {
939 /* if value was expected, it's mandatory */
940 return -parsed;
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100941 }
942
943 if (id[0] != ']') {
944 return -parsed;
945 }
946
947 ++parsed;
948 ++id;
949
950 if ((id[0] == '[') && has_predicate) {
951 *has_predicate = 1;
952 }
953
954 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200955}
956
957/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200958 * @brief Resolve (find) a feature definition. Logs directly.
959 *
960 * @param[in] feat_name Feature name to resolve.
961 * @param[in] len Length of \p feat_name.
962 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +0200963 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
964 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200965 *
Radek Krejci9ff0a922016-07-14 13:08:05 +0200966 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200967 */
968static int
Radek Krejci9ff0a922016-07-14 13:08:05 +0200969resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200970{
971 char *str;
972 const char *mod_name, *name;
973 int mod_name_len, nam_len, i, j;
974 const struct lys_module *module;
975
Radek Krejci9ff0a922016-07-14 13:08:05 +0200976 assert(feature);
977
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200978 /* check prefix */
979 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
980 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
981 return -1;
982 }
983
984 module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len);
985 if (!module) {
986 /* identity refers unknown data model */
987 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
988 return -1;
989 }
990
Radek Krejci9ff0a922016-07-14 13:08:05 +0200991 if (module != node->module && module == lys_node_module(node)) {
992 /* first, try to search directly in submodule where the feature was mentioned */
993 for (j = 0; j < node->module->features_size; j++) {
994 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
995 /* check status */
996 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
997 node->module->features[j].module, node->module->features[j].name, node)) {
998 return -1;
999 }
1000 *feature = &node->module->features[j];
1001 return 0;
1002 }
1003 }
1004 }
1005
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001006 /* search in the identified module ... */
1007 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001008 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001009 /* check status */
1010 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
1011 module->features[j].module, module->features[j].name, node)) {
1012 return -1;
1013 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001014 *feature = &module->features[j];
1015 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001016 }
1017 }
1018 /* ... and all its submodules */
1019 for (i = 0; i < module->inc_size; i++) {
1020 if (!module->inc[i].submodule) {
1021 /* not yet resolved */
1022 continue;
1023 }
1024 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001025 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1026 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001027 /* check status */
1028 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1029 module->inc[i].submodule->features[j].flags,
1030 module->inc[i].submodule->features[j].module,
1031 module->inc[i].submodule->features[j].name, node)) {
1032 return -1;
1033 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001034 *feature = &module->inc[i].submodule->features[j];
1035 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001036 }
1037 }
1038 }
1039
1040 /* not found */
1041 str = strndup(feat_name, len);
1042 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
1043 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001044 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001045}
1046
Radek Krejci9ff0a922016-07-14 13:08:05 +02001047/*
1048 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001049 * - 1 if enabled
1050 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001051 * - -1 if not usable by its if-feature expression
1052 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001053static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001054resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001055{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001056 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001057
Radek Krejci9ff0a922016-07-14 13:08:05 +02001058 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001059 if (!resolve_iffeature(&feat->iffeature[i])) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001060 return -1;
1061 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001062 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001063
Radek Krejci69b8d922016-07-27 13:13:41 +02001064 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001065}
1066
1067static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001068resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001069{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001070 uint8_t op;
1071 int rc, a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001072
Radek Krejci9ff0a922016-07-14 13:08:05 +02001073 op = iff_getop(expr->expr, *index_e);
1074 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001075
Radek Krejci9ff0a922016-07-14 13:08:05 +02001076 switch (op) {
1077 case LYS_IFF_F:
1078 /* resolve feature */
1079 return resolve_feature_value(expr->features[(*index_f)++]);
1080 case LYS_IFF_NOT:
1081 rc = resolve_iffeature_recursive(expr, index_e, index_f);
1082 if (rc == -1) {
1083 /* one of the referenced feature is hidden by its if-feature,
1084 * so this if-feature expression is always false */
1085 return -1;
1086 } else {
1087 /* invert result */
1088 return rc ? 0 : 1;
1089 }
1090 case LYS_IFF_AND:
1091 case LYS_IFF_OR:
1092 a = resolve_iffeature_recursive(expr, index_e, index_f);
1093 b = resolve_iffeature_recursive(expr, index_e, index_f);
1094 if (a == -1 || b == -1) {
1095 /* one of the referenced feature is hidden by its if-feature,
1096 * so this if-feature expression is always false */
1097 return -1;
1098 } else if (op == LYS_IFF_AND) {
1099 return a && b;
1100 } else { /* LYS_IFF_OR */
1101 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001102 }
1103 }
1104
Radek Krejci9ff0a922016-07-14 13:08:05 +02001105 return -1;
1106}
1107
1108int
1109resolve_iffeature(struct lys_iffeature *expr)
1110{
1111 int rc = -1;
1112 int index_e = 0, index_f = 0;
1113
1114 if (expr->expr) {
1115 rc = resolve_iffeature_recursive(expr, &index_e, &index_f);
1116 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001117 return (rc == 1) ? 1 : 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001118}
1119
1120struct iff_stack {
1121 int size;
1122 int index; /* first empty item */
1123 uint8_t *stack;
1124};
1125
1126static int
1127iff_stack_push(struct iff_stack *stack, uint8_t value)
1128{
1129 if (stack->index == stack->size) {
1130 stack->size += 4;
1131 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
1132 if (!stack->stack) {
1133 LOGMEM;
1134 stack->size = 0;
1135 return EXIT_FAILURE;
1136 }
1137 }
1138
1139 stack->stack[stack->index++] = value;
1140 return EXIT_SUCCESS;
1141}
1142
1143static uint8_t
1144iff_stack_pop(struct iff_stack *stack)
1145{
1146 stack->index--;
1147 return stack->stack[stack->index];
1148}
1149
1150static void
1151iff_stack_clean(struct iff_stack *stack)
1152{
1153 stack->size = 0;
1154 free(stack->stack);
1155}
1156
1157static void
1158iff_setop(uint8_t *list, uint8_t op, int pos)
1159{
1160 uint8_t *item;
1161 uint8_t mask = 3;
1162
1163 assert(pos >= 0);
1164 assert(op <= 3); /* max 2 bits */
1165
1166 item = &list[pos / 4];
1167 mask = mask << 2 * (pos % 4);
1168 *item = (*item) & ~mask;
1169 *item = (*item) | (op << 2 * (pos % 4));
1170}
1171
1172uint8_t
1173iff_getop(uint8_t *list, int pos)
1174{
1175 uint8_t *item;
1176 uint8_t mask = 3, result;
1177
1178 assert(pos >= 0);
1179
1180 item = &list[pos / 4];
1181 result = (*item) & (mask << 2 * (pos % 4));
1182 return result >> 2 * (pos % 4);
1183}
1184
1185#define LYS_IFF_LP 0x04 /* ( */
1186#define LYS_IFF_RP 0x08 /* ) */
1187
1188void
1189resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1190{
1191 unsigned int e = 0, f = 0, r = 0;
1192 uint8_t op;
1193
1194 assert(iffeat);
1195
1196 if (!iffeat->expr) {
1197 goto result;
1198 }
1199
1200 do {
1201 op = iff_getop(iffeat->expr, e++);
1202 switch (op) {
1203 case LYS_IFF_NOT:
1204 if (!r) {
1205 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001206 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001207 break;
1208 case LYS_IFF_AND:
1209 case LYS_IFF_OR:
1210 if (!r) {
1211 r += 2;
1212 } else {
1213 r += 1;
1214 }
1215 break;
1216 case LYS_IFF_F:
1217 f++;
1218 if (r) {
1219 r--;
1220 }
1221 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001222 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001223 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001224
Radek Krejci9ff0a922016-07-14 13:08:05 +02001225result:
1226 if (expr_size) {
1227 *expr_size = e;
1228 }
1229 if (feat_size) {
1230 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001231 }
1232}
1233
1234int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001235resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
1236 struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001237{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001238 const char *c = value;
1239 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001240 int i, j, last_not, checkversion = 0;
1241 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001242 uint8_t op;
1243 struct iff_stack stack = {0, 0, NULL};
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001244
Radek Krejci9ff0a922016-07-14 13:08:05 +02001245 assert(c);
1246
1247 if (isspace(c[0])) {
1248 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
1249 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001250 }
1251
Radek Krejci9ff0a922016-07-14 13:08:05 +02001252 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1253 for (i = j = last_not = 0; c[i]; i++) {
1254 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001255 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001256 j++;
1257 continue;
1258 } else if (c[i] == ')') {
1259 j--;
1260 continue;
1261 } else if (isspace(c[i])) {
1262 continue;
1263 }
1264
1265 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1266 if (c[i + r] == '\0') {
Radek Krejcia98da3f2016-07-27 14:05:22 +02001267 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001268 return EXIT_FAILURE;
1269 } else if (!isspace(c[i + r])) {
1270 /* feature name starting with the not/and/or */
1271 last_not = 0;
1272 f_size++;
1273 } else if (c[i] == 'n') { /* not operation */
1274 if (last_not) {
1275 /* double not */
1276 expr_size = expr_size - 2;
1277 last_not = 0;
1278 } else {
1279 last_not = 1;
1280 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001281 } else { /* and, or */
1282 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001283 /* not a not operation */
1284 last_not = 0;
1285 }
1286 i += r;
1287 } else {
1288 f_size++;
1289 last_not = 0;
1290 }
1291 expr_size++;
1292
1293 while (!isspace(c[i])) {
1294 if (!c[i] || c[i] == ')') {
1295 i--;
1296 break;
1297 }
1298 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001299 }
1300 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001301 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001302 /* not matching count of ( and ) */
1303 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1304 return EXIT_FAILURE;
1305 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001306
Radek Krejci69b8d922016-07-27 13:13:41 +02001307 if (checkversion || expr_size > 1) {
1308 /* check that we have 1.1 module */
1309 if (node->module->version != 2) {
1310 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1311 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
1312 return EXIT_FAILURE;
1313 }
1314 }
1315
Radek Krejci9ff0a922016-07-14 13:08:05 +02001316 /* allocate the memory */
1317 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
1318 iffeat_expr->features = malloc(f_size * sizeof *iffeat_expr->features);
1319 stack.size = expr_size;
1320 stack.stack = malloc(expr_size * sizeof *stack.stack);
1321 if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) {
1322 LOGMEM;
1323 goto error;
1324 }
1325 f_size--; expr_size--; /* used as indexes from now */
1326
1327 for (i--; i >= 0; i--) {
1328 if (c[i] == ')') {
1329 /* push it on stack */
1330 iff_stack_push(&stack, LYS_IFF_RP);
1331 continue;
1332 } else if (c[i] == '(') {
1333 /* pop from the stack into result all operators until ) */
1334 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1335 iff_setop(iffeat_expr->expr, op, expr_size--);
1336 }
1337 continue;
1338 } else if (isspace(c[i])) {
1339 continue;
1340 }
1341
1342 /* end operator or operand -> find beginning and get what is it */
1343 j = i + 1;
1344 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1345 i--;
1346 }
1347 i++; /* get back by one step */
1348
1349 if (!strncmp(&c[i], "not ", 4)) {
1350 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1351 /* double not */
1352 iff_stack_pop(&stack);
1353 } else {
1354 /* not has the highest priority, so do not pop from the stack
1355 * as in case of AND and OR */
1356 iff_stack_push(&stack, LYS_IFF_NOT);
1357 }
1358 } else if (!strncmp(&c[i], "and ", 4)) {
1359 /* as for OR - pop from the stack all operators with the same or higher
1360 * priority and store them to the result, then push the AND to the stack */
1361 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1362 op = iff_stack_pop(&stack);
1363 iff_setop(iffeat_expr->expr, op, expr_size--);
1364 }
1365 iff_stack_push(&stack, LYS_IFF_AND);
1366 } else if (!strncmp(&c[i], "or ", 3)) {
1367 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1368 op = iff_stack_pop(&stack);
1369 iff_setop(iffeat_expr->expr, op, expr_size--);
1370 }
1371 iff_stack_push(&stack, LYS_IFF_OR);
1372 } else {
1373 /* feature name, length is j - i */
1374
1375 /* add it to the result */
1376 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1377
1378 /* now get the link to the feature definition. Since it can be
1379 * forward referenced, we have hack for unres - until resolved,
1380 * the feature name is stored instead of link to the lys_feature */
1381 iffeat_expr->features[f_size] = (void*)strndup(&c[i], j - i);
1382 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT, node);
1383 f_size--;
1384
1385 if (r == -1) {
1386 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001387 }
1388 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001389 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001390 while (stack.index) {
1391 op = iff_stack_pop(&stack);
1392 iff_setop(iffeat_expr->expr, op, expr_size--);
1393 }
1394
1395 if (++expr_size || ++f_size) {
1396 /* not all expected operators and operands found */
1397 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1398 rc = EXIT_FAILURE;
1399 } else {
1400 rc = EXIT_SUCCESS;
1401 }
1402
1403error:
1404 /* cleanup */
1405 iff_stack_clean(&stack);
1406
1407 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001408}
1409
1410/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001411 * @brief Resolve (find) a data node based on a schema-nodeid.
1412 *
1413 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1414 * module).
1415 *
1416 */
1417struct lyd_node *
1418resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1419{
1420 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001421 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001422 const struct lys_node *schema = NULL;
Radek Krejcicc217a62016-04-08 16:58:11 +02001423 int shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001424
1425 assert(nodeid && start);
1426
1427 if (nodeid[0] == '/') {
1428 return NULL;
1429 }
1430
1431 str = p = strdup(nodeid);
1432 if (!str) {
1433 LOGMEM;
1434 return NULL;
1435 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001436
Michal Vasko3edeaf72016-02-11 13:17:43 +01001437 while (p) {
1438 token = p;
1439 p = strchr(p, '/');
1440 if (p) {
1441 *p = '\0';
1442 p++;
1443 }
1444
Radek Krejci5da4eb62016-04-08 14:45:51 +02001445 if (p) {
1446 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001447 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001448 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001449 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001450 result = NULL;
1451 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001452 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001453
Radek Krejci5da4eb62016-04-08 14:45:51 +02001454 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1455 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001456 } else if (lys_parent(schema)->nodetype == LYS_CHOICE) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001457 /* shorthand case */
1458 if (!shorthand) {
1459 shorthand = 1;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001460 schema = lys_parent(schema);
Radek Krejcicc217a62016-04-08 16:58:11 +02001461 continue;
1462 } else {
1463 shorthand = 0;
1464 if (schema->nodetype == LYS_LEAF) {
1465 /* should not be here, since we have leaf, which is not a shorthand nor final node */
1466 result = NULL;
1467 break;
1468 }
1469 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001470 }
1471 } else {
1472 /* final node */
Radek Krejcif3c71de2016-04-11 12:45:46 +02001473 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF,
1474 shorthand ? 0 : 1, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001475 || !schema) {
1476 result = NULL;
1477 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001478 }
1479 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001480 LY_TREE_FOR(result ? result->child : start, iter) {
1481 if (iter->schema == schema) {
1482 /* move in data tree according to returned schema */
1483 result = iter;
1484 break;
1485 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001486 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001487 if (!iter) {
1488 /* instance not found */
1489 result = NULL;
1490 break;
1491 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001492 }
1493 free(str);
1494
1495 return result;
1496}
1497
Radek Krejcibdf92362016-04-08 14:43:34 +02001498/*
1499 * 0 - ok (done)
1500 * 1 - continue
1501 * 2 - break
1502 * -1 - error
1503 */
1504static int
Radek Krejcicc217a62016-04-08 16:58:11 +02001505schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id,
Radek Krejcibdf92362016-04-08 14:43:34 +02001506 const struct lys_module *module, const char *mod_name, int mod_name_len,
1507 const struct lys_node **start)
1508{
1509 const struct lys_module *prefix_mod;
Radek Krejcicc217a62016-04-08 16:58:11 +02001510 int sh = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001511
1512 /* module check */
1513 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1514 if (!prefix_mod) {
1515 return -1;
1516 }
1517 if (prefix_mod != lys_node_module(sibling)) {
1518 return 1;
1519 }
1520
1521 /* check for shorthand cases - then 'start' does not change */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001522 if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001523 if (*shorthand != -1) {
1524 *shorthand = *shorthand ? 0 : 1;
1525 }
1526 sh = 1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001527 }
1528
1529 /* the result node? */
1530 if (!id[0]) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001531 if (*shorthand == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001532 return 1;
1533 }
1534 return 0;
1535 }
1536
Radek Krejcicc217a62016-04-08 16:58:11 +02001537 if (!sh) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001538 /* move down the tree, if possible */
1539 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
1540 return -1;
1541 }
1542 *start = sibling->child;
1543 }
1544
1545 return 2;
1546}
1547
Michal Vasko3edeaf72016-02-11 13:17:43 +01001548/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1549int
1550resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module,
1551 const struct lys_node **ret)
1552{
1553 const char *name, *mod_name, *id;
1554 const struct lys_node *sibling;
1555 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001556 int8_t shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001557 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001558 const struct lys_module *start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001559
1560 assert(nodeid && (start || module) && !(start && module) && ret);
1561
1562 id = nodeid;
1563
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001564 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001565 return ((id - nodeid) - r) + 1;
1566 }
1567 id += r;
1568
1569 if ((is_relative && !start) || (!is_relative && !module)) {
1570 return -1;
1571 }
1572
1573 /* descendant-schema-nodeid */
1574 if (is_relative) {
Michal Vasko4c06fd82016-02-17 13:43:38 +01001575 module = start_mod = start->module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001576
1577 /* absolute-schema-nodeid */
1578 } else {
1579 start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001580 if (!start_mod) {
1581 return -1;
1582 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001583 start = start_mod->data;
1584 }
1585
1586 while (1) {
1587 sibling = NULL;
1588 while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod,
1589 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
1590 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001591 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001592 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1593 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001594 *ret = sibling;
1595 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001596 } else if (r == 1) {
1597 continue;
1598 } else if (r == 2) {
1599 break;
1600 } else {
1601 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001602 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001603 }
1604 }
1605
1606 /* no match */
1607 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001608 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001609 return EXIT_SUCCESS;
1610 }
1611
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001612 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001613 return ((id - nodeid) - r) + 1;
1614 }
1615 id += r;
1616 }
1617
1618 /* cannot get here */
1619 LOGINT;
1620 return -1;
1621}
1622
Radek Krejcif3c71de2016-04-11 12:45:46 +02001623/* unique, refine,
1624 * >0 - unexpected char on position (ret - 1),
1625 * 0 - ok (but ret can still be NULL),
1626 * -1 - error,
1627 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001628int
1629resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001630 int check_shorthand, int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001631{
1632 const char *name, *mod_name, *id;
1633 const struct lys_node *sibling;
1634 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001635 int8_t shorthand = check_shorthand ? 0 : -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001636 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001637 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001638
1639 assert(nodeid && start && ret);
1640 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1641
1642 id = nodeid;
1643 module = start->module;
1644
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001645 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001646 return ((id - nodeid) - r) + 1;
1647 }
1648 id += r;
1649
1650 if (!is_relative) {
1651 return -1;
1652 }
1653
1654 while (1) {
1655 sibling = NULL;
1656 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
1657 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
1658 /* name match */
1659 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001660 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1661 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001662 if (!(sibling->nodetype & ret_nodetype)) {
1663 /* wrong node type, too bad */
1664 continue;
1665 }
1666 *ret = sibling;
1667 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001668 } else if (r == 1) {
1669 continue;
1670 } else if (r == 2) {
1671 break;
1672 } else {
1673 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001674 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001675 }
1676 }
1677
1678 /* no match */
1679 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001680 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001681 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001682 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1683 *ret = NULL;
1684 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001685 }
1686
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001687 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001688 return ((id - nodeid) - r) + 1;
1689 }
1690 id += r;
1691 }
1692
1693 /* cannot get here */
1694 LOGINT;
1695 return -1;
1696}
1697
1698/* choice default */
1699int
1700resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1701{
1702 /* cannot actually be a path */
1703 if (strchr(nodeid, '/')) {
1704 return -1;
1705 }
1706
Radek Krejcif3c71de2016-04-11 12:45:46 +02001707 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001708}
1709
1710/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1711static int
1712resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1713{
1714 const struct lys_module *module;
1715 const char *mod_prefix, *name;
1716 int i, mod_prefix_len, nam_len;
1717
1718 /* parse the identifier, it must be parsed on one call */
1719 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1720 return -i + 1;
1721 }
1722
1723 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1724 if (!module) {
1725 return -1;
1726 }
1727 if (module != start->module) {
1728 start = module->data;
1729 }
1730
1731 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1732
1733 return EXIT_SUCCESS;
1734}
1735
1736int
1737resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1738 const struct lys_node **ret)
1739{
1740 const char *name, *mod_name, *id;
1741 const struct lys_node *sibling, *start;
1742 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001743 int8_t shorthand = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001744 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001745
1746 assert(nodeid && module && ret);
1747 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1748
1749 id = nodeid;
1750 start = module->data;
1751
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001752 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001753 return ((id - nodeid) - r) + 1;
1754 }
1755 id += r;
1756
1757 if (is_relative) {
1758 return -1;
1759 }
1760
1761 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001762 if (!abs_start_mod) {
1763 return -1;
1764 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001765
1766 while (1) {
1767 sibling = NULL;
1768 while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE
1769 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1770 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001771 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001772 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1773 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001774 if (!(sibling->nodetype & ret_nodetype)) {
1775 /* wrong node type, too bad */
1776 continue;
1777 }
1778 *ret = sibling;
1779 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001780 } else if (r == 1) {
1781 continue;
1782 } else if (r == 2) {
1783 break;
1784 } else {
1785 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001786 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001787 }
1788 }
1789
1790 /* no match */
1791 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001792 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001793 return EXIT_SUCCESS;
1794 }
1795
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001796 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001797 return ((id - nodeid) - r) + 1;
1798 }
1799 id += r;
1800 }
1801
1802 /* cannot get here */
1803 LOGINT;
1804 return -1;
1805}
1806
Michal Vaskoe733d682016-03-14 09:08:27 +01001807static int
Michal Vasko3547c532016-03-14 09:40:50 +01001808resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001809{
1810 const char *name;
1811 int nam_len, has_predicate, i;
1812
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001813 if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
1814 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001815 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001816 return -1;
1817 }
1818
1819 predicate += i;
1820 *parsed += i;
1821
1822 for (i = 0; i < list->keys_size; ++i) {
1823 if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) {
1824 break;
1825 }
1826 }
1827
1828 if (i == list->keys_size) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001829 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskoe733d682016-03-14 09:08:27 +01001830 return -1;
1831 }
1832
1833 /* more predicates? */
1834 if (has_predicate) {
Michal Vasko3547c532016-03-14 09:40:50 +01001835 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001836 }
1837
1838 return 0;
1839}
1840
Michal Vasko9fd98e22016-04-07 15:44:19 +02001841/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly
1842 * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */
Michal Vaskoe733d682016-03-14 09:08:27 +01001843const struct lys_node *
Michal Vaskoa9c8e992016-07-26 16:27:05 +02001844resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001845{
Michal Vasko10728b52016-04-07 14:26:29 +02001846 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001847 const char *name, *mod_name, *id;
Michal Vasko3547c532016-03-14 09:40:50 +01001848 const struct lys_node *sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001849 int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001850 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskoe733d682016-03-14 09:08:27 +01001851 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001852
Michal Vasko3547c532016-03-14 09:40:50 +01001853 assert(nodeid && (ctx || start));
1854 if (!ctx) {
1855 ctx = start->module->ctx;
1856 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001857
1858 id = nodeid;
1859
Michal Vaskoe733d682016-03-14 09:08:27 +01001860 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001861 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001862 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001863 }
1864 id += r;
1865
1866 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01001867 assert(start);
1868 start = start->child;
1869 if (!start) {
1870 /* no descendants, fail for sure */
Michal Vasko10728b52016-04-07 14:26:29 +02001871 str = strndup(nodeid, (name + nam_len) - nodeid);
1872 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1873 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001874 return NULL;
1875 }
1876 module = start->module;
1877 } else {
1878 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02001879 str = strndup(nodeid, (name + nam_len) - nodeid);
1880 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
1881 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001882 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02001883 } else if (mod_name_len > LY_BUF_SIZE - 1) {
1884 LOGINT;
1885 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01001886 }
1887
Michal Vasko971a3ca2016-04-01 13:09:29 +02001888 if (ly_buf_used && module_name[0]) {
1889 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1890 }
1891 ly_buf_used++;
1892
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001893 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001894 module_name[mod_name_len] = '\0';
1895 module = ly_ctx_get_module(ctx, module_name, NULL);
1896
1897 if (buf_backup) {
1898 /* return previous internal buffer content */
1899 strcpy(module_name, buf_backup);
1900 free(buf_backup);
1901 buf_backup = NULL;
1902 }
1903 ly_buf_used--;
1904
Michal Vasko3547c532016-03-14 09:40:50 +01001905 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02001906 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1907 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1908 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001909 return NULL;
1910 }
1911 start = module->data;
1912
1913 /* now it's as if there was no module name */
1914 mod_name = NULL;
1915 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01001916 }
1917
Michal Vaskoe733d682016-03-14 09:08:27 +01001918 prev_mod = module;
1919
Michal Vasko3edeaf72016-02-11 13:17:43 +01001920 while (1) {
1921 sibling = NULL;
Michal Vasko9fd98e22016-04-07 15:44:19 +02001922 while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ?
1923 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001924 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001925 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001926
1927 /* data RPC input/output check */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001928 if ((data_nodeid == 1) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_OUTPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001929 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001930 } else if ((data_nodeid == 2) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_INPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001931 continue;
1932 }
1933
Michal Vasko3edeaf72016-02-11 13:17:43 +01001934 /* module check */
1935 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001936 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01001937 LOGINT;
1938 return NULL;
1939 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001940
1941 if (ly_buf_used && module_name[0]) {
1942 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1943 }
1944 ly_buf_used++;
1945
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001946 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01001947 module_name[mod_name_len] = '\0';
1948 /* will also find an augment module */
1949 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001950
1951 if (buf_backup) {
1952 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001953 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001954 free(buf_backup);
1955 buf_backup = NULL;
1956 }
1957 ly_buf_used--;
1958
Michal Vasko3edeaf72016-02-11 13:17:43 +01001959 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001960 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1961 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1962 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001963 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001964 }
1965 } else {
1966 prefix_mod = prev_mod;
1967 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01001968 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001969 continue;
1970 }
1971
Michal Vaskoe733d682016-03-14 09:08:27 +01001972 /* do we have some predicates on it? */
1973 if (has_predicate) {
1974 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001975 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1976 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
1977 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
1978 return NULL;
1979 }
1980 } else if (sibling->nodetype == LYS_LIST) {
1981 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
1982 return NULL;
1983 }
1984 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01001985 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01001986 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01001987 }
1988 id += r;
1989 }
1990
Radek Krejcibdf92362016-04-08 14:43:34 +02001991 /* check for shorthand cases - then 'start' does not change */
Michal Vasko3c0f9f52016-05-17 16:38:10 +02001992 if (!data_nodeid && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001993 shorthand = ~shorthand;
1994 }
1995
Michal Vasko3edeaf72016-02-11 13:17:43 +01001996 /* the result node? */
1997 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001998 if (shorthand) {
1999 /* wrong path for shorthand */
Michal Vasko025e0452016-05-17 16:14:20 +02002000 str = strndup(nodeid, (name + nam_len) - nodeid);
2001 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko3c0f9f52016-05-17 16:38:10 +02002002 LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement.");
Radek Krejci9a5fccc2016-05-18 15:44:58 +02002003 free(str);
Michal Vasko025e0452016-05-17 16:14:20 +02002004 return NULL;
Radek Krejcibdf92362016-04-08 14:43:34 +02002005 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002006 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002007 }
2008
Michal Vasko3c0f9f52016-05-17 16:38:10 +02002009 if (data_nodeid || !shorthand) {
Michal Vasko7dc71d02016-03-15 10:42:28 +01002010 /* move down the tree, if possible */
2011 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002012 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko7dc71d02016-03-15 10:42:28 +01002013 return NULL;
2014 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002015 start = sibling->child;
2016 }
2017
2018 /* update prev mod */
2019 prev_mod = start->module;
2020 break;
2021 }
2022 }
2023
2024 /* no match */
2025 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002026 str = strndup(nodeid, (name + nam_len) - nodeid);
2027 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2028 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002029 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002030 }
2031
Michal Vaskoe733d682016-03-14 09:08:27 +01002032 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002033 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002034 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002035 }
2036 id += r;
2037 }
2038
2039 /* cannot get here */
2040 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01002041 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002042}
2043
Michal Vasko22448d32016-03-16 13:17:29 +01002044static int
2045resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed)
2046{
2047 const char *name, *value;
2048 int nam_len, val_len, has_predicate = 1, r;
2049 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002050 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01002051
Radek Krejci61a86c62016-03-24 11:06:44 +01002052 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002053 assert(node->schema->nodetype == LYS_LIST);
2054
Michal Vaskof29903d2016-04-18 13:13:10 +02002055 key = (struct lyd_node_leaf_list *)node->child;
2056 for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
2057 if (!key) {
2058 /* invalid data */
2059 LOGINT;
2060 return -1;
2061 }
Michal Vasko22448d32016-03-16 13:17:29 +01002062
Michal Vasko22448d32016-03-16 13:17:29 +01002063 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002064 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002065 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002066 }
2067
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002068 if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
2069 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002070 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002071 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002072 }
2073
2074 predicate += r;
2075 *parsed += r;
2076
Michal Vaskof29903d2016-04-18 13:13:10 +02002077 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002078 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002079 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002080 }
2081
2082 /* value does not match */
Michal Vaskof29903d2016-04-18 13:13:10 +02002083 if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002084 return 1;
2085 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002086
2087 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002088 }
2089
2090 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002091 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002092 return -1;
2093 }
2094
2095 return 0;
2096}
2097
2098struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002099resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2100 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002101{
Michal Vasko10728b52016-04-07 14:26:29 +02002102 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko22448d32016-03-16 13:17:29 +01002103 const char *id, *mod_name, *name;
2104 int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002105 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002106 struct lyd_node_leaf_list *llist;
Michal Vasko22448d32016-03-16 13:17:29 +01002107 const struct lys_module *prefix_mod, *prev_mod;
2108 struct ly_ctx *ctx;
2109
2110 assert(nodeid && start && parsed);
2111
2112 ctx = start->schema->module->ctx;
2113 id = nodeid;
2114
2115 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 +01002116 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002117 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002118 return NULL;
2119 }
2120 id += r;
2121 /* add it to parsed only after the data node was actually found */
2122 last_parsed = r;
2123
2124 if (is_relative) {
2125 prev_mod = start->schema->module;
Michal Vasko22448d32016-03-16 13:17:29 +01002126 start = start->child;
2127 } else {
2128 for (; start->parent; start = start->parent);
Michal Vasko22448d32016-03-16 13:17:29 +01002129 prev_mod = start->schema->module;
2130 }
2131
2132 while (1) {
2133 LY_TREE_FOR(start, sibling) {
Michal Vasko2411b942016-03-23 13:50:03 +01002134 /* RPC data check, return simply invalid argument, because the data tree is invalid */
2135 if (lys_parent(sibling->schema)) {
2136 if (options & LYD_PATH_OPT_OUTPUT) {
2137 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002138 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002139 *parsed = -1;
2140 return NULL;
2141 }
2142 } else {
2143 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002144 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002145 *parsed = -1;
2146 return NULL;
2147 }
2148 }
2149 }
2150
Michal Vasko22448d32016-03-16 13:17:29 +01002151 /* name match */
2152 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2153
2154 /* module check */
2155 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002156 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01002157 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002158 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002159 return NULL;
2160 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002161
2162 if (ly_buf_used && module_name[0]) {
2163 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2164 }
2165 ly_buf_used++;
2166
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002167 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01002168 module_name[mod_name_len] = '\0';
2169 /* will also find an augment module */
2170 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002171
2172 if (buf_backup) {
2173 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002174 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002175 free(buf_backup);
2176 buf_backup = NULL;
2177 }
2178 ly_buf_used--;
2179
Michal Vasko22448d32016-03-16 13:17:29 +01002180 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002181 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2182 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2183 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002184 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002185 return NULL;
2186 }
2187 } else {
2188 prefix_mod = prev_mod;
2189 }
2190 if (prefix_mod != lys_node_module(sibling->schema)) {
2191 continue;
2192 }
2193
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002194 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002195 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002196 llist = (struct lyd_node_leaf_list *)sibling;
2197 if ((!llist_value && llist->value_str && llist->value_str[0])
2198 || (llist_value && strcmp(llist_value, llist->value_str))) {
2199 continue;
2200 }
Michal Vasko22448d32016-03-16 13:17:29 +01002201 }
2202
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002203 /* list, we need predicates'n'stuff then */
Michal Vasko22448d32016-03-16 13:17:29 +01002204 if (sibling->schema->nodetype == LYS_LIST) {
2205 r = 0;
2206 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002207 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002208 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002209 return NULL;
2210 }
2211 ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r);
2212 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002213 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002214 return NULL;
2215 } else if (ret == 1) {
2216 /* this list instance does not match */
2217 continue;
2218 }
2219 id += r;
2220 last_parsed += r;
2221 }
2222
2223 *parsed += last_parsed;
2224
2225 /* the result node? */
2226 if (!id[0]) {
2227 return sibling;
2228 }
2229
2230 /* move down the tree, if possible */
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002231 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002232 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002233 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002234 return NULL;
2235 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002236 last_match = sibling;
Michal Vasko22448d32016-03-16 13:17:29 +01002237 start = sibling->child;
2238 if (start) {
2239 prev_mod = start->schema->module;
2240 }
2241 break;
2242 }
2243 }
2244
2245 /* no match, return last match */
2246 if (!sibling) {
2247 return last_match;
2248 }
2249
2250 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 +01002251 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002252 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002253 return NULL;
2254 }
2255 id += r;
2256 last_parsed = r;
2257 }
2258
2259 /* cannot get here */
2260 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002261 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002262 return NULL;
2263}
2264
Michal Vasko3edeaf72016-02-11 13:17:43 +01002265/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002266 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002267 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002268 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02002269 * @param[in] str_restr Restriction as a string.
2270 * @param[in] type Type of the restriction.
2271 * @param[out] ret Final interval structure that starts with
2272 * the interval of the initial type, continues with intervals
2273 * of any superior types derived from the initial one, and
2274 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002275 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002276 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002277 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002278int
Michal Vaskoaeb51802016-04-11 10:58:47 +02002279resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002280{
2281 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002282 int kind;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002283 int64_t local_smin, local_smax;
2284 uint64_t local_umin, local_umax;
2285 long double local_fmin, local_fmax;
2286 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002287 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002288
2289 switch (type->base) {
2290 case LY_TYPE_BINARY:
2291 kind = 0;
2292 local_umin = 0;
2293 local_umax = 18446744073709551615UL;
2294
2295 if (!str_restr && type->info.binary.length) {
2296 str_restr = type->info.binary.length->expr;
2297 }
2298 break;
2299 case LY_TYPE_DEC64:
2300 kind = 2;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02002301 local_fmin = ((long double)-9223372036854775808.0) / type->info.dec64.div;
2302 local_fmax = ((long double)9223372036854775807.0) / type->info.dec64.div;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002303
2304 if (!str_restr && type->info.dec64.range) {
2305 str_restr = type->info.dec64.range->expr;
2306 }
2307 break;
2308 case LY_TYPE_INT8:
2309 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002310 local_smin = __INT64_C(-128);
2311 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002312
2313 if (!str_restr && type->info.num.range) {
2314 str_restr = type->info.num.range->expr;
2315 }
2316 break;
2317 case LY_TYPE_INT16:
2318 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002319 local_smin = __INT64_C(-32768);
2320 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002321
2322 if (!str_restr && type->info.num.range) {
2323 str_restr = type->info.num.range->expr;
2324 }
2325 break;
2326 case LY_TYPE_INT32:
2327 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002328 local_smin = __INT64_C(-2147483648);
2329 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002330
2331 if (!str_restr && type->info.num.range) {
2332 str_restr = type->info.num.range->expr;
2333 }
2334 break;
2335 case LY_TYPE_INT64:
2336 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002337 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2338 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002339
2340 if (!str_restr && type->info.num.range) {
2341 str_restr = type->info.num.range->expr;
2342 }
2343 break;
2344 case LY_TYPE_UINT8:
2345 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002346 local_umin = __UINT64_C(0);
2347 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002348
2349 if (!str_restr && type->info.num.range) {
2350 str_restr = type->info.num.range->expr;
2351 }
2352 break;
2353 case LY_TYPE_UINT16:
2354 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002355 local_umin = __UINT64_C(0);
2356 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002357
2358 if (!str_restr && type->info.num.range) {
2359 str_restr = type->info.num.range->expr;
2360 }
2361 break;
2362 case LY_TYPE_UINT32:
2363 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002364 local_umin = __UINT64_C(0);
2365 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002366
2367 if (!str_restr && type->info.num.range) {
2368 str_restr = type->info.num.range->expr;
2369 }
2370 break;
2371 case LY_TYPE_UINT64:
2372 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002373 local_umin = __UINT64_C(0);
2374 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002375
2376 if (!str_restr && type->info.num.range) {
2377 str_restr = type->info.num.range->expr;
2378 }
2379 break;
2380 case LY_TYPE_STRING:
2381 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002382 local_umin = __UINT64_C(0);
2383 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002384
2385 if (!str_restr && type->info.str.length) {
2386 str_restr = type->info.str.length->expr;
2387 }
2388 break;
2389 default:
2390 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002391 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002392 }
2393
2394 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002395 if (type->der) {
2396 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002397 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002398 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002399 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002400 assert(!intv || (intv->kind == kind));
2401 }
2402
2403 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002404 /* we do not have any restriction, return superior ones */
2405 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002406 return EXIT_SUCCESS;
2407 }
2408
2409 /* adjust local min and max */
2410 if (intv) {
2411 tmp_intv = intv;
2412
2413 if (kind == 0) {
2414 local_umin = tmp_intv->value.uval.min;
2415 } else if (kind == 1) {
2416 local_smin = tmp_intv->value.sval.min;
2417 } else if (kind == 2) {
2418 local_fmin = tmp_intv->value.fval.min;
2419 }
2420
2421 while (tmp_intv->next) {
2422 tmp_intv = tmp_intv->next;
2423 }
2424
2425 if (kind == 0) {
2426 local_umax = tmp_intv->value.uval.max;
2427 } else if (kind == 1) {
2428 local_smax = tmp_intv->value.sval.max;
2429 } else if (kind == 2) {
2430 local_fmax = tmp_intv->value.fval.max;
2431 }
2432 }
2433
2434 /* finally parse our restriction */
2435 seg_ptr = str_restr;
2436 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002437 if (!tmp_local_intv) {
2438 assert(!local_intv);
2439 local_intv = malloc(sizeof *local_intv);
2440 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002441 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002442 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002443 tmp_local_intv = tmp_local_intv->next;
2444 }
Michal Vasko253035f2015-12-17 16:58:13 +01002445 if (!tmp_local_intv) {
2446 LOGMEM;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002447 goto error;
Michal Vasko253035f2015-12-17 16:58:13 +01002448 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002449
2450 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002451 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002452 tmp_local_intv->next = NULL;
2453
2454 /* min */
2455 ptr = seg_ptr;
2456 while (isspace(ptr[0])) {
2457 ++ptr;
2458 }
2459 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2460 if (kind == 0) {
2461 tmp_local_intv->value.uval.min = atoll(ptr);
2462 } else if (kind == 1) {
2463 tmp_local_intv->value.sval.min = atoll(ptr);
2464 } else if (kind == 2) {
2465 tmp_local_intv->value.fval.min = atoll(ptr);
2466 }
2467
2468 if ((ptr[0] == '+') || (ptr[0] == '-')) {
2469 ++ptr;
2470 }
2471 while (isdigit(ptr[0])) {
2472 ++ptr;
2473 }
2474 } else if (!strncmp(ptr, "min", 3)) {
2475 if (kind == 0) {
2476 tmp_local_intv->value.uval.min = local_umin;
2477 } else if (kind == 1) {
2478 tmp_local_intv->value.sval.min = local_smin;
2479 } else if (kind == 2) {
2480 tmp_local_intv->value.fval.min = local_fmin;
2481 }
2482
2483 ptr += 3;
2484 } else if (!strncmp(ptr, "max", 3)) {
2485 if (kind == 0) {
2486 tmp_local_intv->value.uval.min = local_umax;
2487 } else if (kind == 1) {
2488 tmp_local_intv->value.sval.min = local_smax;
2489 } else if (kind == 2) {
2490 tmp_local_intv->value.fval.min = local_fmax;
2491 }
2492
2493 ptr += 3;
2494 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002495 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002496 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002497 }
2498
2499 while (isspace(ptr[0])) {
2500 ptr++;
2501 }
2502
2503 /* no interval or interval */
2504 if ((ptr[0] == '|') || !ptr[0]) {
2505 if (kind == 0) {
2506 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2507 } else if (kind == 1) {
2508 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2509 } else if (kind == 2) {
2510 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2511 }
2512 } else if (!strncmp(ptr, "..", 2)) {
2513 /* skip ".." */
2514 ptr += 2;
2515 while (isspace(ptr[0])) {
2516 ++ptr;
2517 }
2518
2519 /* max */
2520 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2521 if (kind == 0) {
2522 tmp_local_intv->value.uval.max = atoll(ptr);
2523 } else if (kind == 1) {
2524 tmp_local_intv->value.sval.max = atoll(ptr);
2525 } else if (kind == 2) {
2526 tmp_local_intv->value.fval.max = atoll(ptr);
2527 }
2528 } else if (!strncmp(ptr, "max", 3)) {
2529 if (kind == 0) {
2530 tmp_local_intv->value.uval.max = local_umax;
2531 } else if (kind == 1) {
2532 tmp_local_intv->value.sval.max = local_smax;
2533 } else if (kind == 2) {
2534 tmp_local_intv->value.fval.max = local_fmax;
2535 }
2536 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002537 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002538 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002539 }
2540 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002541 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002542 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002543 }
2544
2545 /* next segment (next OR) */
2546 seg_ptr = strchr(seg_ptr, '|');
2547 if (!seg_ptr) {
2548 break;
2549 }
2550 seg_ptr++;
2551 }
2552
2553 /* check local restrictions against superior ones */
2554 if (intv) {
2555 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002556 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002557
2558 while (tmp_local_intv && tmp_intv) {
2559 /* reuse local variables */
2560 if (kind == 0) {
2561 local_umin = tmp_local_intv->value.uval.min;
2562 local_umax = tmp_local_intv->value.uval.max;
2563
2564 /* it must be in this interval */
2565 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2566 /* this interval is covered, next one */
2567 if (local_umax <= tmp_intv->value.uval.max) {
2568 tmp_local_intv = tmp_local_intv->next;
2569 continue;
2570 /* ascending order of restrictions -> fail */
2571 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002572 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002573 }
2574 }
2575 } else if (kind == 1) {
2576 local_smin = tmp_local_intv->value.sval.min;
2577 local_smax = tmp_local_intv->value.sval.max;
2578
2579 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2580 if (local_smax <= tmp_intv->value.sval.max) {
2581 tmp_local_intv = tmp_local_intv->next;
2582 continue;
2583 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002584 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002585 }
2586 }
2587 } else if (kind == 2) {
2588 local_fmin = tmp_local_intv->value.fval.min;
2589 local_fmax = tmp_local_intv->value.fval.max;
2590
2591 if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) {
2592 if (local_fmax <= tmp_intv->value.fval.max) {
2593 tmp_local_intv = tmp_local_intv->next;
2594 continue;
2595 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002596 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002597 }
2598 }
2599 }
2600
2601 tmp_intv = tmp_intv->next;
2602 }
2603
2604 /* some interval left uncovered -> fail */
2605 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002606 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002607 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002608 }
2609
Michal Vaskoaeb51802016-04-11 10:58:47 +02002610 /* append the local intervals to all the intervals of the superior types, return it all */
2611 if (intv) {
2612 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2613 tmp_intv->next = local_intv;
2614 } else {
2615 intv = local_intv;
2616 }
2617 *ret = intv;
2618
2619 return EXIT_SUCCESS;
2620
2621error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002622 while (intv) {
2623 tmp_intv = intv->next;
2624 free(intv);
2625 intv = tmp_intv;
2626 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02002627 while (local_intv) {
2628 tmp_local_intv = local_intv->next;
2629 free(local_intv);
2630 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002631 }
2632
Michal Vaskoaeb51802016-04-11 10:58:47 +02002633 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002634}
2635
Michal Vasko730dfdf2015-08-11 14:48:05 +02002636/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02002637 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
2638 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002639 *
2640 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002641 * @param[in] mod_name Typedef name module name.
2642 * @param[in] module Main module.
2643 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002644 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002645 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002646 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002647 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002648int
Michal Vasko1e62a092015-12-01 12:27:20 +01002649resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2650 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002651{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002652 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002653 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002654 int tpdf_size;
2655
Michal Vasko1dca6882015-10-22 14:29:42 +02002656 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002657 /* no prefix, try built-in types */
2658 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
2659 if (!strcmp(ly_types[i].def->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002660 if (ret) {
2661 *ret = ly_types[i].def;
2662 }
2663 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002664 }
2665 }
2666 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002667 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002668 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002669 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002670 }
2671 }
2672
Michal Vasko1dca6882015-10-22 14:29:42 +02002673 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002674 /* search in local typedefs */
2675 while (parent) {
2676 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002677 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002678 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2679 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002680 break;
2681
Radek Krejci76512572015-08-04 09:47:08 +02002682 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002683 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2684 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002685 break;
2686
Radek Krejci76512572015-08-04 09:47:08 +02002687 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002688 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2689 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002690 break;
2691
Radek Krejci76512572015-08-04 09:47:08 +02002692 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002693 case LYS_ACTION:
2694 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
2695 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002696 break;
2697
Radek Krejci76512572015-08-04 09:47:08 +02002698 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002699 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2700 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002701 break;
2702
Radek Krejci76512572015-08-04 09:47:08 +02002703 case LYS_INPUT:
2704 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02002705 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
2706 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002707 break;
2708
2709 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02002710 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002711 continue;
2712 }
2713
2714 for (i = 0; i < tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002715 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002716 match = &tpdf[i];
2717 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002718 }
2719 }
2720
Michal Vaskodcf98e62016-05-05 17:53:53 +02002721 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002722 }
Radek Krejcic071c542016-01-27 14:57:51 +01002723 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002724 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002725 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002726 if (!module) {
2727 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002728 }
2729 }
2730
2731 /* search in top level typedefs */
2732 for (i = 0; i < module->tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002733 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002734 match = &module->tpdf[i];
2735 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002736 }
2737 }
2738
2739 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01002740 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002741 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002742 if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002743 match = &module->inc[i].submodule->tpdf[j];
2744 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002745 }
2746 }
2747 }
2748
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002749 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002750
2751check_leafref:
2752 if (ret) {
2753 *ret = match;
2754 }
2755 if (match->type.base == LY_TYPE_LEAFREF) {
2756 while (!match->type.info.lref.path) {
2757 match = match->type.der;
2758 assert(match);
2759 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02002760 }
2761 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002762}
2763
Michal Vasko1dca6882015-10-22 14:29:42 +02002764/**
2765 * @brief Check the default \p value of the \p type. Logs directly.
2766 *
2767 * @param[in] type Type definition to use.
2768 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01002769 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02002770 *
2771 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
2772 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002773static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002774check_default(struct lys_type *type, const char *value, struct lys_module *module)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002775{
Michal Vasko478c4652016-07-21 12:55:01 +02002776 struct lys_tpdf *base_tpdf;
Michal Vasko1dca6882015-10-22 14:29:42 +02002777 struct lyd_node_leaf_list node;
Radek Krejci37b756f2016-01-18 10:15:03 +01002778 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02002779
Michal Vasko478c4652016-07-21 12:55:01 +02002780 if (type->base == LY_TYPE_DER) {
2781 /* the type was not resolved yet, nothing to do for now */
2782 return EXIT_FAILURE;
2783 }
2784
2785 if (!value) {
2786 /* we do not have a new default value, so is there any to check even, in some base type? */
2787 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
2788 if (base_tpdf->dflt) {
2789 value = base_tpdf->dflt;
2790 break;
2791 }
2792 }
2793
2794 if (!value) {
2795 /* no default value, nothing to check, all is well */
2796 return EXIT_SUCCESS;
2797 }
2798
2799 /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */
2800 switch (type->base) {
2801 case LY_TYPE_BITS:
2802 case LY_TYPE_ENUM:
2803 case LY_TYPE_IDENT:
2804 case LY_TYPE_INST:
2805 case LY_TYPE_LEAFREF:
2806 case LY_TYPE_BOOL:
2807 case LY_TYPE_EMPTY:
2808 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
2809 return EXIT_SUCCESS;
2810 case LY_TYPE_DEC64:
2811 if (type->info.dec64.range) {
2812 break;
2813 }
2814 return EXIT_SUCCESS;
2815 case LY_TYPE_BINARY:
2816 if (type->info.binary.length) {
2817 break;
2818 }
2819 return EXIT_SUCCESS;
2820 case LY_TYPE_INT8:
2821 case LY_TYPE_INT16:
2822 case LY_TYPE_INT32:
2823 case LY_TYPE_INT64:
2824 case LY_TYPE_UINT8:
2825 case LY_TYPE_UINT16:
2826 case LY_TYPE_UINT32:
2827 case LY_TYPE_UINT64:
2828 if (type->info.num.range) {
2829 break;
2830 }
2831 return EXIT_SUCCESS;
2832 case LY_TYPE_STRING:
2833 if (type->info.str.length || type->info.str.patterns) {
2834 break;
2835 }
2836 return EXIT_SUCCESS;
2837 case LY_TYPE_UNION:
2838 /* way too much trouble learning whether we need to check the default again, so just do it */
2839 break;
2840 default:
2841 LOGINT;
2842 return -1;
2843 }
2844 }
2845
Michal Vasko1dca6882015-10-22 14:29:42 +02002846 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01002847 memset(&node, 0, sizeof node);
Michal Vasko1dca6882015-10-22 14:29:42 +02002848 node.value_str = value;
2849 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01002850 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko253035f2015-12-17 16:58:13 +01002851 if (!node.schema) {
2852 LOGMEM;
2853 return -1;
2854 }
Michal Vasko1dca6882015-10-22 14:29:42 +02002855 node.schema->name = strdup("default");
Michal Vasko253035f2015-12-17 16:58:13 +01002856 if (!node.schema->name) {
2857 LOGMEM;
Michal Vaskof49eda02016-07-21 12:17:01 +02002858 free(node.schema);
Michal Vasko253035f2015-12-17 16:58:13 +01002859 return -1;
2860 }
Michal Vasko56826402016-03-02 11:11:37 +01002861 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01002862 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02002863
Radek Krejci37b756f2016-01-18 10:15:03 +01002864 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02002865 if (!type->info.lref.target) {
2866 ret = EXIT_FAILURE;
2867 goto finish;
2868 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002869 ret = check_default(&type->info.lref.target->type, value, module);
Michal Vasko1dca6882015-10-22 14:29:42 +02002870
2871 } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) {
2872 /* it was converted to JSON format before, nothing else sensible we can do */
2873
2874 } else {
Michal Vasko3767fb22016-07-21 12:10:57 +02002875 if (lyp_parse_value(&node, NULL, 1)) {
2876 ret = -1;
2877 }
Michal Vasko1dca6882015-10-22 14:29:42 +02002878 }
2879
2880finish:
2881 if (node.value_type == LY_TYPE_BITS) {
2882 free(node.value.bit);
2883 }
2884 free((char *)node.schema->name);
2885 free(node.schema);
2886
2887 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002888}
2889
Michal Vasko730dfdf2015-08-11 14:48:05 +02002890/**
2891 * @brief Check a key for mandatory attributes. Logs directly.
2892 *
2893 * @param[in] key The key to check.
2894 * @param[in] flags What flags to check.
2895 * @param[in] list The list of all the keys.
2896 * @param[in] index Index of the key in the key list.
2897 * @param[in] name The name of the keys.
2898 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002899 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002900 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002901 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002902static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002903check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002904{
Radek Krejciadb57612016-02-16 13:34:34 +01002905 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002906 char *dup = NULL;
2907 int j;
2908
2909 /* existence */
2910 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02002911 if (name[len] != '\0') {
2912 dup = strdup(name);
Michal Vasko253035f2015-12-17 16:58:13 +01002913 if (!dup) {
2914 LOGMEM;
2915 return -1;
2916 }
Michal Vaskof02e3742015-08-05 16:27:02 +02002917 dup[len] = '\0';
2918 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002919 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002920 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002921 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002922 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002923 }
2924
2925 /* uniqueness */
2926 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01002927 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002928 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002929 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002930 }
2931 }
2932
2933 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02002934 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002935 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002936 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002937 }
2938
2939 /* type of the leaf is not built-in empty */
2940 if (key->type.base == LY_TYPE_EMPTY) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002941 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002942 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002943 }
2944
2945 /* config attribute is the same as of the list */
Radek Krejciadb57612016-02-16 13:34:34 +01002946 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002947 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002948 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002949 }
2950
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002951 /* key is not placed from augment */
2952 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002953 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
2954 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002955 return -1;
2956 }
2957
Michal Vaskocca47842016-03-17 10:31:07 +01002958 /* key is not when-conditional */
2959 if (key->when) {
Radek Krejci02a04992016-03-17 16:06:37 +01002960 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, "when", "leaf");
2961 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"when\" condition.");
Radek Krejci581ce772015-11-10 17:22:40 +01002962 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02002963 }
2964
Michal Vasko0b85aa82016-03-07 14:37:43 +01002965 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02002966}
Michal Vasko730dfdf2015-08-11 14:48:05 +02002967
2968/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002969 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002970 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002971 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01002972 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002973 *
2974 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
2975 */
2976int
Radek Krejci48464ed2016-03-17 15:44:09 +01002977resolve_unique(struct lys_node *parent, const char *uniq_str_path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002978{
Radek Krejci581ce772015-11-10 17:22:40 +01002979 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01002980 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002981
Radek Krejcif3c71de2016-04-11 12:45:46 +02002982 rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01002983 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01002984 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002985 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002986 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002987 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02002988 } else if (rc == -2) {
Michal Vaskoc66c6d82016-04-12 11:37:31 +02002989 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01002990 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01002991 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01002992 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002993 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
2994 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002995 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002996 }
Radek Krejci581ce772015-11-10 17:22:40 +01002997 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002998 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01002999 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003000 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3001 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf.");
Radek Krejci581ce772015-11-10 17:22:40 +01003002 rc = -1;
3003 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003004 }
3005
Radek Krejcicf509982015-12-15 09:22:44 +01003006 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01003007 if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003008 return -1;
3009 }
3010
Radek Krejcica7efb72016-01-18 13:06:01 +01003011 /* set leaf's unique flag */
3012 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3013
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003014 return EXIT_SUCCESS;
3015
3016error:
3017
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003018 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003019}
3020
Radek Krejci0c0086a2016-03-24 15:20:28 +01003021void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003022unres_data_del(struct unres_data *unres, uint32_t i)
3023{
3024 /* there are items after the one deleted */
3025 if (i+1 < unres->count) {
3026 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003027 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003028
3029 /* deleting the last item */
3030 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003031 free(unres->node);
3032 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003033 }
3034
3035 /* if there are no items after and it is not the last one, just move the counter */
3036 --unres->count;
3037}
3038
Michal Vasko0491ab32015-08-19 14:28:29 +02003039/**
3040 * @brief Resolve (find) a data node from a specific module. Does not log.
3041 *
3042 * @param[in] mod Module to search in.
3043 * @param[in] name Name of the data node.
3044 * @param[in] nam_len Length of the name.
3045 * @param[in] start Data node to start the search from.
3046 * @param[in,out] parents Resolved nodes. If there are some parents,
3047 * they are replaced (!!) with the resolvents.
3048 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003049 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003050 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003051static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003052resolve_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 +02003053{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003054 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003055 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003056 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003057
Michal Vasko23b61ec2015-08-19 11:19:50 +02003058 if (!parents->count) {
3059 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003060 parents->node = malloc(sizeof *parents->node);
Michal Vasko253035f2015-12-17 16:58:13 +01003061 if (!parents->node) {
3062 LOGMEM;
Michal Vasko2471e7f2016-04-11 11:00:15 +02003063 return -1;
Michal Vasko253035f2015-12-17 16:58:13 +01003064 }
Michal Vaskocf024702015-10-08 15:01:42 +02003065 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003066 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003067 for (i = 0; i < parents->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003068 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003069 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003070 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003071 continue;
3072 }
3073 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003074 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003075 if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len)
3076 && node->schema->name[nam_len] == '\0') {
3077 /* matching target */
3078 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003079 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003080 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003081 flag = 1;
3082 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003083 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003084 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003085 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
3086 if (!parents->node) {
3087 return EXIT_FAILURE;
3088 }
Michal Vaskocf024702015-10-08 15:01:42 +02003089 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003090 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003091 }
3092 }
3093 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003094
3095 if (!flag) {
3096 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003097 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003098 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003099 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003100 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003101 }
3102
Michal Vasko0491ab32015-08-19 14:28:29 +02003103 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003104}
3105
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003106/**
3107 * @brief Resolve (find) a data node. Does not log.
3108 *
Radek Krejci581ce772015-11-10 17:22:40 +01003109 * @param[in] mod_name Module name of the data node.
3110 * @param[in] mod_name_len Length of the module name.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003111 * @param[in] name Name of the data node.
3112 * @param[in] nam_len Length of the name.
3113 * @param[in] start Data node to start the search from.
3114 * @param[in,out] parents Resolved nodes. If there are some parents,
3115 * they are replaced (!!) with the resolvents.
3116 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003117 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003118 */
Radek Krejcic5090c32015-08-12 09:46:19 +02003119static int
Radek Krejci581ce772015-11-10 17:22:40 +01003120resolve_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 +02003121 struct unres_data *parents)
Radek Krejcic5090c32015-08-12 09:46:19 +02003122{
Michal Vasko1e62a092015-12-01 12:27:20 +01003123 const struct lys_module *mod;
Michal Vasko31fc3672015-10-21 12:08:13 +02003124 char *str;
Radek Krejcic5090c32015-08-12 09:46:19 +02003125
Michal Vasko23b61ec2015-08-19 11:19:50 +02003126 assert(start);
3127
Michal Vasko31fc3672015-10-21 12:08:13 +02003128 if (mod_name) {
3129 /* we have mod_name, find appropriate module */
3130 str = strndup(mod_name, mod_name_len);
Michal Vasko253035f2015-12-17 16:58:13 +01003131 if (!str) {
3132 LOGMEM;
3133 return -1;
3134 }
Michal Vasko31fc3672015-10-21 12:08:13 +02003135 mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL);
3136 free(str);
Radek Krejcic5090c32015-08-12 09:46:19 +02003137 if (!mod) {
3138 /* invalid prefix */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003139 return -1;
Radek Krejcic5090c32015-08-12 09:46:19 +02003140 }
3141 } else {
3142 /* no prefix, module is the same as of current node */
3143 mod = start->schema->module;
3144 }
3145
3146 return resolve_data(mod, name, name_len, start, parents);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003147}
3148
Michal Vasko730dfdf2015-08-11 14:48:05 +02003149/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003150 * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly
Radek Krejci48464ed2016-03-17 15:44:09 +01003151 * only specific errors, general no-resolvent error is left to the caller.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003152 *
Michal Vaskobb211122015-08-19 14:03:11 +02003153 * @param[in] pred Predicate to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003154 * @param[in] node Node from which the predicate is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02003155 * @param[in,out] node_match Nodes satisfying the restriction
3156 * without the predicate. Nodes not
3157 * satisfying the predicate are removed.
Michal Vasko0491ab32015-08-19 14:28:29 +02003158 * @param[out] parsed Number of characters parsed, negative on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003159 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003160 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003161 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003162static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003163resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match,
Radek Krejci010e54b2016-03-15 09:40:34 +01003164 int *parsed)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003165{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003166 /* ... /node[source = destination] ... */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003167 struct unres_data source_match, dest_match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003168 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vasko0491ab32015-08-19 14:28:29 +02003169 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0;
3170 int has_predicate, dest_parent_times, i, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003171 uint32_t j;
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003172 struct lyd_node_leaf_list *leaf_dst, *leaf_src;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003173
3174 source_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003175 source_match.node = malloc(sizeof *source_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003176 if (!source_match.node) {
3177 LOGMEM;
3178 return -1;
3179 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003180 dest_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003181 dest_match.node = malloc(sizeof *dest_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003182 if (!dest_match.node) {
3183 LOGMEM;
3184 return -1;
3185 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003186
3187 do {
3188 if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
3189 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003190 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003191 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003192 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003193 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003194 parsed_loc += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003195 pred += i;
3196
Michal Vasko23b61ec2015-08-19 11:19:50 +02003197 for (j = 0; j < node_match->count;) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003198 /* source */
Michal Vaskocf024702015-10-08 15:01:42 +02003199 source_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02003200
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003201 /* must be leaf (key of a list) */
Radek Krejci581ce772015-11-10 17:22:40 +01003202 if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j],
Michal Vaskocf024702015-10-08 15:01:42 +02003203 &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003204 i = 0;
3205 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003206 }
3207
3208 /* destination */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003209 dest_match.node[0] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003210 dest_parent_times = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003211 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3212 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003213 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003214 rc = -1;
3215 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003216 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003217 pke_parsed = i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003218 for (i = 0; i < dest_parent_times; ++i) {
Michal Vaskocf024702015-10-08 15:01:42 +02003219 dest_match.node[0] = dest_match.node[0]->parent;
3220 if (!dest_match.node[0]) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003221 i = 0;
Michal Vasko0491ab32015-08-19 14:28:29 +02003222 rc = EXIT_FAILURE;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003223 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003224 }
3225 }
3226 while (1) {
Radek Krejci581ce772015-11-10 17:22:40 +01003227 if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0],
Michal Vasko0491ab32015-08-19 14:28:29 +02003228 &dest_match)) || (dest_match.count != 1)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003229 i = 0;
3230 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003231 }
3232
3233 if (pke_len == pke_parsed) {
3234 break;
3235 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003236 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 +02003237 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003238 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003239 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003240 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003241 }
3242 pke_parsed += i;
3243 }
3244
3245 /* check match between source and destination nodes */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003246 leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0];
3247 while (leaf_dst->value_type == LY_TYPE_LEAFREF) {
3248 leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref;
3249 }
3250 leaf_src = (struct lyd_node_leaf_list *)source_match.node[0];
3251 while (leaf_src->value_type == LY_TYPE_LEAFREF) {
3252 leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref;
3253 }
3254 if (leaf_src->value_type != leaf_dst->value_type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003255 goto remove_leafref;
3256 }
3257
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003258 if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003259 goto remove_leafref;
3260 }
3261
3262 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003263 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003264 continue;
3265
3266remove_leafref:
3267 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003268 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003269 }
3270 } while (has_predicate);
3271
Michal Vaskocf024702015-10-08 15:01:42 +02003272 free(source_match.node);
3273 free(dest_match.node);
Michal Vasko0491ab32015-08-19 14:28:29 +02003274 if (parsed) {
3275 *parsed = parsed_loc;
3276 }
3277 return EXIT_SUCCESS;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003278
3279error:
3280
3281 if (source_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003282 free(source_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003283 }
3284 if (dest_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003285 free(dest_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003286 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003287 if (parsed) {
3288 *parsed = -parsed_loc+i;
3289 }
3290 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003291}
3292
Michal Vasko730dfdf2015-08-11 14:48:05 +02003293/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003294 * @brief Resolve a path (leafref) in JSON data context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003295 *
Michal Vaskocf024702015-10-08 15:01:42 +02003296 * @param[in] node Leafref data node.
Michal Vasko23b61ec2015-08-19 11:19:50 +02003297 * @param[in] path Path of the leafref.
Radek Krejci48464ed2016-03-17 15:44:09 +01003298 * @param[out] ret Matching nodes. Expects an empty, but allocated structure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003299 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003300 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003301 */
Michal Vasko184521f2015-09-24 13:14:26 +02003302static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003303resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003304{
Radek Krejci71b795b2015-08-10 16:20:39 +02003305 struct lyd_node *data = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003306 const char *prefix, *name;
Michal Vasko0491ab32015-08-19 14:28:29 +02003307 int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003308 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003309
Michal Vaskocf024702015-10-08 15:01:42 +02003310 assert(node && path && ret && !ret->count);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003311
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003312 parent_times = 0;
Michal Vaskod9173342015-08-17 14:35:36 +02003313 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003314
3315 /* searching for nodeset */
3316 do {
3317 if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003318 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003319 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003320 goto error;
3321 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003322 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003323 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003324
Michal Vasko23b61ec2015-08-19 11:19:50 +02003325 if (!ret->count) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02003326 if (parent_times != -1) {
3327 ret->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003328 ret->node = calloc(1, sizeof *ret->node);
Michal Vasko253035f2015-12-17 16:58:13 +01003329 if (!ret->node) {
3330 LOGMEM;
Radek Krejci50501732016-01-07 13:06:39 +01003331 rc = -1;
Michal Vasko253035f2015-12-17 16:58:13 +01003332 goto error;
3333 }
Michal Vasko8bcdf292015-08-19 14:04:43 +02003334 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003335 for (i = 0; i < parent_times; ++i) {
3336 /* relative path */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003337 if (!ret->count) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003338 /* error, too many .. */
Radek Krejci48464ed2016-03-17 15:44:09 +01003339 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name);
Michal Vasko0491ab32015-08-19 14:28:29 +02003340 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003341 goto error;
Michal Vaskocf024702015-10-08 15:01:42 +02003342 } else if (!ret->node[0]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003343 /* first .. */
Michal Vaskocf024702015-10-08 15:01:42 +02003344 data = ret->node[0] = node->parent;
3345 } else if (!ret->node[0]->parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003346 /* we are in root */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003347 ret->count = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003348 free(ret->node);
3349 ret->node = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003350 } else {
3351 /* multiple .. */
Michal Vaskocf024702015-10-08 15:01:42 +02003352 data = ret->node[0] = ret->node[0]->parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003353 }
3354 }
3355
3356 /* absolute path */
3357 if (parent_times == -1) {
Michal Vaskocf024702015-10-08 15:01:42 +02003358 for (data = node; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003359 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko8bcdf292015-08-19 14:04:43 +02003360 if (data->prev) {
3361 for (; data->prev->next; data = data->prev);
3362 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003363 }
3364 }
3365
3366 /* node identifier */
Radek Krejci581ce772015-11-10 17:22:40 +01003367 if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003368 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003369 LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name);
Michal Vasko184521f2015-09-24 13:14:26 +02003370 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003371 goto error;
3372 }
3373
3374 if (has_predicate) {
3375 /* we have predicate, so the current results must be lists */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003376 for (j = 0; j < ret->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003377 if (ret->node[j]->schema->nodetype == LYS_LIST &&
3378 ((struct lys_node_list *)ret->node[0]->schema)->keys) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003379 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003380 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003381 continue;
3382 }
3383
3384 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003385 unres_data_del(ret, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003386 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003387 if ((rc = resolve_path_predicate_data(path, node, ret, &i))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003388 if (rc == -1) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003389 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path);
Michal Vasko184521f2015-09-24 13:14:26 +02003390 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003391 goto error;
3392 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003393 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003394 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003395
Michal Vasko23b61ec2015-08-19 11:19:50 +02003396 if (!ret->count) {
Michal Vasko0491ab32015-08-19 14:28:29 +02003397 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003398 goto error;
3399 }
3400 }
3401 } while (path[0] != '\0');
3402
Michal Vaskof02e3742015-08-05 16:27:02 +02003403 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003404
3405error:
3406
Michal Vaskocf024702015-10-08 15:01:42 +02003407 free(ret->node);
3408 ret->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003409 ret->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003410
Michal Vasko0491ab32015-08-19 14:28:29 +02003411 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003412}
3413
Michal Vasko730dfdf2015-08-11 14:48:05 +02003414/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003415 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003416 *
Michal Vaskobb211122015-08-19 14:03:11 +02003417 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003418 * @param[in] context_node Predicate context node (where the predicate is placed).
3419 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003420 *
Michal Vasko184521f2015-09-24 13:14:26 +02003421 * @return 0 on forward reference, otherwise the number
3422 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003423 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003424 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003425static int
Radek Krejciadb57612016-02-16 13:34:34 +01003426resolve_path_predicate_schema(const char *path, const struct lys_node *context_node,
Radek Krejci48464ed2016-03-17 15:44:09 +01003427 struct lys_node *parent)
Michal Vasko1f76a282015-08-04 16:16:53 +02003428{
Michal Vasko1e62a092015-12-01 12:27:20 +01003429 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003430 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
3431 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 +02003432 int has_predicate, dest_parent_times = 0, i, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003433
3434 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003435 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003436 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003437 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003438 return -parsed+i;
3439 }
3440 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003441 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003442
Michal Vasko58090902015-08-13 14:04:15 +02003443 /* source (must be leaf) */
Michal Vasko36cbaa42015-12-14 13:15:48 +01003444 if (!sour_pref) {
Radek Krejciadb57612016-02-16 13:34:34 +01003445 sour_pref = context_node->module->name;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003446 }
Radek Krejciadb57612016-02-16 13:34:34 +01003447 rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003448 LYS_LEAF | LYS_AUGMENT, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003449 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003450 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003451 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003452 }
3453
3454 /* destination */
3455 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3456 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003457 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 +02003458 return -parsed;
3459 }
3460 pke_parsed += i;
3461
Radek Krejciadb57612016-02-16 13:34:34 +01003462 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003463 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003464 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003465 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003466 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02003467 /* path is supposed to be evaluated in data tree, so we have to skip
3468 * all schema nodes that cannot be instantiated in data tree */
3469 for (dst_node = lys_parent(dst_node);
3470 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST));
3471 dst_node = lys_parent(dst_node));
Michal Vasko1f76a282015-08-04 16:16:53 +02003472 }
3473 while (1) {
Michal Vasko36cbaa42015-12-14 13:15:48 +01003474 if (!dest_pref) {
3475 dest_pref = dst_node->module->name;
3476 }
3477 rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003478 LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003479 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003480 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003481 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003482 }
3483
3484 if (pke_len == pke_parsed) {
3485 break;
3486 }
3487
3488 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
3489 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003490 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Radek Krejciadb57612016-02-16 13:34:34 +01003491 (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003492 return -parsed;
3493 }
3494 pke_parsed += i;
3495 }
3496
3497 /* check source - dest match */
Michal Vasko184521f2015-09-24 13:14:26 +02003498 if (dst_node->nodetype != LYS_LEAF) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003499 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Radek Krejci48464ed2016-03-17 15:44:09 +01003500 LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
3501 "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003502 return -parsed;
3503 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003504 } while (has_predicate);
3505
3506 return parsed;
3507}
3508
Michal Vasko730dfdf2015-08-11 14:48:05 +02003509/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003510 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003511 *
Michal Vaskobb211122015-08-19 14:03:11 +02003512 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003513 * @param[in] parent_node Parent of the leafref.
Radek Krejci2f12f852016-01-08 12:59:57 +01003514 * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path
3515 * has to contain absolute path
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003516 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003517 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003518 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003519 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003520static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003521resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf,
Michal Vasko36cbaa42015-12-14 13:15:48 +01003522 const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003523{
Michal Vasko1e62a092015-12-01 12:27:20 +01003524 const struct lys_node *node;
Radek Krejcic071c542016-01-27 14:57:51 +01003525 const struct lys_module *mod;
Michal Vasko1f76a282015-08-04 16:16:53 +02003526 const char *id, *prefix, *name;
3527 int pref_len, nam_len, parent_times, has_predicate;
Michal Vasko184521f2015-09-24 13:14:26 +02003528 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003529
Michal Vasko184521f2015-09-24 13:14:26 +02003530 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003531 parent_times = 0;
3532 id = path;
3533
3534 do {
3535 if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003536 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 +02003537 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003538 }
3539 id += i;
3540
Michal Vasko184521f2015-09-24 13:14:26 +02003541 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003542 if (parent_times == -1) {
Radek Krejcic071c542016-01-27 14:57:51 +01003543 /* resolve prefix of the module */
Radek Krejciadb57612016-02-16 13:34:34 +01003544 mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len);
Radek Krejcic071c542016-01-27 14:57:51 +01003545 /* get start node */
3546 node = mod ? mod->data : NULL;
Michal Vasko58090902015-08-13 14:04:15 +02003547 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003548 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3549 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003550 return EXIT_FAILURE;
Michal Vasko58090902015-08-13 14:04:15 +02003551 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003552 } else if (parent_times > 0) {
Radek Krejci2f12f852016-01-08 12:59:57 +01003553 if (parent_tpdf) {
3554 /* the path is not allowed to contain relative path since we are in top level typedef */
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003555 LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path);
Radek Krejci2f12f852016-01-08 12:59:57 +01003556 return -1;
3557 }
3558
Michal Vaskodf053d12016-07-21 13:33:31 +02003559 /* node is the parent already, skip one ".." */
3560 for (i = 1, node = parent; i < parent_times; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02003561 /* path is supposed to be evaluated in data tree, so we have to skip
3562 * all schema nodes that cannot be instantiated in data tree */
3563 for (node = lys_parent(node);
3564 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST));
3565 node = lys_parent(node));
3566
Michal Vasko1f76a282015-08-04 16:16:53 +02003567 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003568 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3569 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003570 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003571 }
Michal Vasko58090902015-08-13 14:04:15 +02003572
Michal Vasko1f76a282015-08-04 16:16:53 +02003573 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003574 } else {
3575 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003576 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003577 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003578
Michal Vasko184521f2015-09-24 13:14:26 +02003579 first_iter = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003580 } else {
Michal Vasko7dc71d02016-03-15 10:42:28 +01003581 /* move down the tree, if possible */
3582 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko2f5aceb2016-03-22 10:24:14 +01003583 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 +01003584 return -1;
3585 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003586 node = node->child;
3587 }
3588
Michal Vasko4f0dad02016-02-15 14:08:23 +01003589 if (!prefix) {
Radek Krejciadb57612016-02-16 13:34:34 +01003590 prefix = lys_node_module(parent)->name;
Michal Vasko4f0dad02016-02-15 14:08:23 +01003591 }
3592
Michal Vasko36cbaa42015-12-14 13:15:48 +01003593 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 +02003594 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003595 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003596 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003597 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003598
3599 if (has_predicate) {
3600 /* we have predicate, so the current result must be list */
3601 if (node->nodetype != LYS_LIST) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003602 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003603 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003604 }
3605
Radek Krejci48464ed2016-03-17 15:44:09 +01003606 i = resolve_path_predicate_schema(id, node, parent);
Michal Vasko184521f2015-09-24 13:14:26 +02003607 if (!i) {
Michal Vaskof9664da2015-08-24 15:03:30 +02003608 return EXIT_FAILURE;
Michal Vasko184521f2015-09-24 13:14:26 +02003609 } else if (i < 0) {
3610 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003611 }
3612 id += i;
3613 }
3614 } while (id[0]);
3615
Michal Vaskoca917682016-07-25 11:00:37 +02003616 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
3617 if ((node->nodetype != LYS_LEAF) && ((lys_node_module(parent)->version != 2) || (node->nodetype != LYS_LEAFLIST))) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003618 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003619 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003620 }
3621
Radek Krejcicf509982015-12-15 09:22:44 +01003622 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003623 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003624 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003625 return -1;
3626 }
3627
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003628 if (ret) {
3629 *ret = node;
3630 }
3631 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003632}
3633
Michal Vasko730dfdf2015-08-11 14:48:05 +02003634/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003635 * @brief Resolve instance-identifier predicate in JSON data format.
3636 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003637 *
Michal Vaskobb211122015-08-19 14:03:11 +02003638 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003639 * @param[in,out] node_match Nodes matching the restriction without
3640 * the predicate. Nodes not satisfying
3641 * the predicate are removed.
3642 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003643 * @return Number of characters successfully parsed,
3644 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003645 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003646static int
Michal Vaskof39142b2015-10-21 11:40:05 +02003647resolve_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003648{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003649 /* ... /node[target = value] ... */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003650 struct unres_data target_match;
3651 struct ly_ctx *ctx;
Michal Vasko1e62a092015-12-01 12:27:20 +01003652 const struct lys_module *mod;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003653 const char *model, *name, *value;
3654 char *str;
3655 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed;
3656 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003657
Michal Vasko1f2cc332015-08-19 11:18:32 +02003658 assert(pred && node_match->count);
3659
Michal Vaskocf024702015-10-08 15:01:42 +02003660 ctx = node_match->node[0]->schema->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003661 idx = -1;
3662 parsed = 0;
3663
3664 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02003665 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003666 return -parsed+i;
3667 }
3668 parsed += i;
3669 pred += i;
3670
Michal Vasko1f2cc332015-08-19 11:18:32 +02003671 /* pos */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003672 if (isdigit(name[0])) {
3673 idx = atoi(name);
3674 }
3675
Michal Vasko1f2cc332015-08-19 11:18:32 +02003676 for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003677 /* target */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003678 memset(&target_match, 0, sizeof target_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003679 if ((name[0] == '.') || !value) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003680 target_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003681 target_match.node = malloc(sizeof *target_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003682 if (!target_match.node) {
3683 LOGMEM;
3684 return -1;
3685 }
Michal Vaskocf024702015-10-08 15:01:42 +02003686 target_match.node[0] = node_match->node[j];
Michal Vasko1f2cc332015-08-19 11:18:32 +02003687 } else {
3688 str = strndup(model, mod_len);
3689 mod = ly_ctx_get_module(ctx, str, NULL);
3690 free(str);
3691
Radek Krejci804836a2016-02-03 10:39:55 +01003692 if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003693 goto remove_instid;
3694 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003695 }
3696
3697 /* check that we have the correct type */
3698 if (name[0] == '.') {
Michal Vaskocf024702015-10-08 15:01:42 +02003699 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003700 goto remove_instid;
3701 }
3702 } else if (value) {
Michal Vaskocf024702015-10-08 15:01:42 +02003703 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003704 goto remove_instid;
3705 }
3706 }
3707
Michal Vasko83a6c462015-10-08 16:43:53 +02003708 if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len)
3709 || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len]))
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003710 || (!value && (idx != cur_idx))) {
3711 goto remove_instid;
3712 }
3713
Michal Vaskocf024702015-10-08 15:01:42 +02003714 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003715
3716 /* leafref is ok, continue check with next leafref */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003717 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003718 continue;
3719
3720remove_instid:
Michal Vaskocf024702015-10-08 15:01:42 +02003721 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003722
3723 /* does not fulfill conditions, remove leafref record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003724 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003725 }
3726 } while (has_predicate);
3727
3728 return parsed;
3729}
3730
Michal Vasko730dfdf2015-08-11 14:48:05 +02003731/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003732 * @brief Resolve instance-identifier in JSON data format. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003733 *
Radek Krejciadb57612016-02-16 13:34:34 +01003734 * @param[in] data Data node where the path is used
Michal Vasko730dfdf2015-08-11 14:48:05 +02003735 * @param[in] path Instance-identifier node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003736 *
Radek Krejcic5090c32015-08-12 09:46:19 +02003737 * @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 +02003738 */
Michal Vasko184521f2015-09-24 13:14:26 +02003739static struct lyd_node *
Radek Krejci48464ed2016-03-17 15:44:09 +01003740resolve_instid(struct lyd_node *data, const char *path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003741{
Radek Krejcic5090c32015-08-12 09:46:19 +02003742 int i = 0, j;
3743 struct lyd_node *result = NULL;
Michal Vaskobea08e92016-05-18 13:28:08 +02003744 const struct lys_module *mod, *prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003745 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003746 const char *model, *name;
Radek Krejcic5090c32015-08-12 09:46:19 +02003747 char *str;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003748 int mod_len, name_len, has_predicate;
3749 struct unres_data node_match;
3750 uint32_t k;
3751
3752 memset(&node_match, 0, sizeof node_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003753
Radek Krejcic5090c32015-08-12 09:46:19 +02003754 /* we need root to resolve absolute path */
3755 for (; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003756 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko0491ab32015-08-19 14:28:29 +02003757 if (data->prev) {
3758 for (; data->prev->next; data = data->prev);
3759 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003760
Michal Vaskobea08e92016-05-18 13:28:08 +02003761 prev_mod = lyd_node_module(data);
3762
Radek Krejcic5090c32015-08-12 09:46:19 +02003763 /* search for the instance node */
3764 while (path[i]) {
Michal Vaskof39142b2015-10-21 11:40:05 +02003765 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
Radek Krejcic5090c32015-08-12 09:46:19 +02003766 if (j <= 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003767 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003768 goto error;
3769 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003770 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003771
Michal Vaskobea08e92016-05-18 13:28:08 +02003772 if (model) {
3773 str = strndup(model, mod_len);
3774 if (!str) {
3775 LOGMEM;
3776 goto error;
3777 }
3778 mod = ly_ctx_get_module(ctx, str, NULL);
3779 free(str);
3780 } else {
3781 mod = prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003782 }
3783
Michal Vasko1f2cc332015-08-19 11:18:32 +02003784 if (resolve_data(mod, name, name_len, data, &node_match)) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003785 /* no instance exists */
3786 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003787 }
3788
3789 if (has_predicate) {
3790 /* we have predicate, so the current results must be list or leaf-list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003791 for (k = 0; k < node_match.count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003792 if ((node_match.node[k]->schema->nodetype == LYS_LIST &&
3793 ((struct lys_node_list *)node_match.node[k]->schema)->keys)
3794 || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003795 /* instid is ok, continue check with next instid */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003796 ++k;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003797 continue;
3798 }
3799
3800 /* does not fulfill conditions, remove inst record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003801 unres_data_del(&node_match, k);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003802 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003803
Michal Vaskof39142b2015-10-21 11:40:05 +02003804 j = resolve_predicate(&path[i], &node_match);
Radek Krejcic5090c32015-08-12 09:46:19 +02003805 if (j < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003806 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003807 goto error;
3808 }
Michal Vasko1f2cc332015-08-19 11:18:32 +02003809 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003810
Michal Vasko1f2cc332015-08-19 11:18:32 +02003811 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003812 /* no instance exists */
3813 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003814 }
3815 }
Michal Vaskobea08e92016-05-18 13:28:08 +02003816
3817 prev_mod = mod;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003818 }
3819
Michal Vasko1f2cc332015-08-19 11:18:32 +02003820 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003821 /* no instance exists */
3822 return NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003823 } else if (node_match.count > 1) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003824 /* instance identifier must resolve to a single node */
Radek Krejci48464ed2016-03-17 15:44:09 +01003825 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Radek Krejcic5090c32015-08-12 09:46:19 +02003826
Michal Vaskod6adbaa2016-04-11 11:01:09 +02003827 goto error;
Radek Krejcic5090c32015-08-12 09:46:19 +02003828 } else {
3829 /* we have required result, remember it and cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003830 result = node_match.node[0];
3831 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003832
3833 return result;
3834 }
3835
3836error:
3837
3838 /* cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003839 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003840
3841 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003842}
3843
Michal Vasko730dfdf2015-08-11 14:48:05 +02003844/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003845 * @brief Passes config flag down to children, skips nodes without config flags.
3846 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003847 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003848 * @param[in] node Siblings and their children to have flags changed.
3849 * @param[in] flags Flags to assign to all the nodes.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003850 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003851static void
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003852inherit_config_flag(struct lys_node *node, int flags)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003853{
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003854 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02003855 LY_TREE_FOR(node, node) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003856 if (node->flags & LYS_CONFIG_SET) {
3857 /* skip nodes with an explicit config value */
3858 continue;
3859 }
3860 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
Radek Krejci43f548d2016-06-16 11:06:38 +02003861 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003862 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02003863 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
3864 inherit_config_flag(node->child, flags);
3865 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003866 }
3867}
3868
Michal Vasko730dfdf2015-08-11 14:48:05 +02003869/**
Michal Vasko7178e692016-02-12 15:58:05 +01003870 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003871 *
Michal Vaskobb211122015-08-19 14:03:11 +02003872 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01003873 * @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 +02003874 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003875 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003876 */
Michal Vasko7178e692016-02-12 15:58:05 +01003877static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003878resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003879{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003880 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02003881 struct lys_node *sub;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003882
Michal Vasko1d87a922015-08-21 12:57:16 +02003883 assert(aug);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003884
3885 /* resolve target node */
Michal Vasko3edeaf72016-02-11 13:17:43 +01003886 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 +01003887 if (rc == -1) {
3888 return -1;
3889 }
3890 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003891 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003892 return -1;
3893 }
3894 if (!aug->target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003895 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003896 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003897 }
3898
3899 if (!aug->child) {
3900 /* nothing to do */
Michal Vasko1d87a922015-08-21 12:57:16 +02003901 LOGWRN("Augment \"%s\" without children.", aug->target_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003902 return EXIT_SUCCESS;
3903 }
3904
Michal Vaskod58d5962016-03-02 14:29:41 +01003905 /* check for mandatory nodes - if the target node is in another module
3906 * the added nodes cannot be mandatory
3907 */
3908 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
3909 && lyp_check_mandatory((struct lys_node *)aug)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003910 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, "mandatory", "augment node");
3911 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 +01003912 return -1;
3913 }
3914
Michal Vasko07e89ef2016-03-03 13:28:57 +01003915 /* check augment target type and then augment nodes type */
3916 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
3917 LY_TREE_FOR(aug->child, sub) {
3918 if (!(sub->nodetype & (LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003919 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3920 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003921 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3922 return -1;
3923 }
3924 }
3925 } else if (aug->target->nodetype == LYS_CHOICE) {
3926 LY_TREE_FOR(aug->child, sub) {
3927 if (!(sub->nodetype & (LYS_CASE | LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003928 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3929 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003930 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3931 return -1;
3932 }
3933 }
3934 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003935 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
3936 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01003937 return -1;
3938 }
3939
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003940 /* inherit config information from actual parent */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003941 LY_TREE_FOR(aug->child, sub) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003942 inherit_config_flag(sub, aug->target->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003943 }
3944
Radek Krejcic071c542016-01-27 14:57:51 +01003945 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02003946 LY_TREE_FOR(aug->child, sub) {
Michal Vasko2afc1ca2016-05-03 11:38:53 +02003947 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02003948 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02003949 }
3950 }
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003951 /* reconnect augmenting data into the target - add them to the target child list */
3952 if (aug->target->child) {
Michal Vasko1d87a922015-08-21 12:57:16 +02003953 sub = aug->target->child->prev; /* remember current target's last node */
3954 sub->next = aug->child; /* connect augmenting data after target's last node */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003955 aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */
Michal Vasko1d87a922015-08-21 12:57:16 +02003956 aug->child->prev = sub; /* finish connecting of both child lists */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003957 } else {
3958 aug->target->child = aug->child;
3959 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003960
3961 return EXIT_SUCCESS;
3962}
3963
Michal Vasko730dfdf2015-08-11 14:48:05 +02003964/**
3965 * @brief Resolve uses, apply augments, refines. Logs directly.
3966 *
Michal Vaskobb211122015-08-19 14:03:11 +02003967 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003968 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003969 *
Michal Vaskodef0db12015-10-07 13:22:48 +02003970 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003971 */
Michal Vasko184521f2015-09-24 13:14:26 +02003972static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003973resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003974{
3975 struct ly_ctx *ctx;
Radek Krejci989790c2016-04-14 17:49:41 +02003976 struct lys_node *node = NULL, *next, *iter;
Pavol Vican55abd332016-07-12 15:54:49 +02003977 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci76512572015-08-04 09:47:08 +02003978 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003979 struct lys_restr *must, **old_must;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003980 int i, j, rc, parent_flags;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003981 uint8_t size, *old_size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003982
Michal Vasko71e1aa82015-08-12 12:17:51 +02003983 assert(uses->grp);
Michal Vaskoe7708512016-03-11 10:26:55 +01003984 /* HACK just check that the grouping is resolved */
Michal Vaskodef0db12015-10-07 13:22:48 +02003985 assert(!uses->grp->nacm);
Michal Vasko71e1aa82015-08-12 12:17:51 +02003986
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003987 if (!uses->grp->child) {
3988 /* grouping without children, warning was already displayed */
3989 return EXIT_SUCCESS;
3990 }
3991
3992 /* get proper parent (config) flags */
3993 for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux));
3994 if (node_aux) {
3995 parent_flags = node_aux->flags & LYS_CONFIG_MASK;
3996 } else {
3997 /* default */
3998 parent_flags = LYS_CONFIG_W;
3999 }
4000
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004001 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004002 LY_TREE_FOR(uses->grp->child, node_aux) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004003 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, 0, uses->nacm, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004004 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004005 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
4006 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004007 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004008 }
Pavol Vican55abd332016-07-12 15:54:49 +02004009 /* test the name of siblings */
4010 LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02004011 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Pavol Vican55abd332016-07-12 15:54:49 +02004012 return -1;
4013 }
4014 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004015 }
4016 ctx = uses->module->ctx;
4017
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004018 if (parent_flags) {
4019 assert(uses->child);
4020 inherit_config_flag(uses->child, parent_flags);
4021 }
4022
Michal Vaskodef0db12015-10-07 13:22:48 +02004023 /* we managed to copy the grouping, the rest must be possible to resolve */
4024
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004025 /* apply refines */
4026 for (i = 0; i < uses->refine_size; i++) {
4027 rfn = &uses->refine[i];
Michal Vasko3edeaf72016-02-11 13:17:43 +01004028 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE,
Radek Krejcif3c71de2016-04-11 12:45:46 +02004029 1, 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004030 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004031 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskodef0db12015-10-07 13:22:48 +02004032 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004033 }
4034
Radek Krejci1d82ef62015-08-07 14:44:40 +02004035 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004036 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
4037 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004038 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004039 }
4040
4041 /* description on any nodetype */
4042 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004043 lydict_remove(ctx, node->dsc);
4044 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004045 }
4046
4047 /* reference on any nodetype */
4048 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004049 lydict_remove(ctx, node->ref);
4050 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004051 }
4052
4053 /* config on any nodetype */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004054 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejcia881e3e2016-06-23 17:02:39 +02004055 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
4056 if (parent && parent->nodetype != LYS_GROUPING &&
4057 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
Radek Krejci989790c2016-04-14 17:49:41 +02004058 (rfn->flags & LYS_CONFIG_W)) {
4059 /* setting config true under config false is prohibited */
4060 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4061 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4062 "changing config from 'false' to 'true' is prohibited while "
4063 "the target's parent is still config 'false'.");
4064 return -1;
4065 }
4066
Radek Krejci1d82ef62015-08-07 14:44:40 +02004067 node->flags &= ~LYS_CONFIG_MASK;
4068 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Radek Krejci989790c2016-04-14 17:49:41 +02004069
4070 /* inherit config change to the target children */
4071 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4072 if (rfn->flags & LYS_CONFIG_W) {
4073 if (iter->flags & LYS_CONFIG_SET) {
4074 /* config is set explicitely, go to next sibling */
4075 next = NULL;
4076 goto nextsibling;
4077 }
4078 } else { /* LYS_CONFIG_R */
4079 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4080 /* error - we would have config data under status data */
4081 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4082 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4083 "changing config from 'true' to 'false' is prohibited while the target "
4084 "has still a children with explicit config 'true'.");
4085 return -1;
4086 }
4087 }
4088 /* change config */
4089 iter->flags &= ~LYS_CONFIG_MASK;
4090 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4091
4092 /* select next iter - modified LY_TREE_DFS_END */
4093 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
4094 next = NULL;
4095 } else {
4096 next = iter->child;
4097 }
4098nextsibling:
4099 if (!next) {
4100 /* no children */
4101 if (iter == node->child) {
4102 /* we are done, (START) has no children */
4103 break;
4104 }
4105 /* try siblings */
4106 next = iter->next;
4107 }
4108 while (!next) {
4109 /* parent is already processed, go to its sibling */
4110 iter = lys_parent(iter);
4111
4112 /* no siblings, go back through parents */
4113 if (iter == node) {
4114 /* we are done, no next element to process */
4115 break;
4116 }
4117 next = iter->next;
4118 }
4119 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004120 }
4121
4122 /* default value ... */
4123 if (rfn->mod.dflt) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004124 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004125 /* leaf */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004126 lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt);
4127 ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0);
4128 } else if (node->nodetype == LYS_CHOICE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004129 /* choice */
Michal Vasko3edeaf72016-02-11 13:17:43 +01004130 rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child,
4131 (const struct lys_node **)&((struct lys_node_choice *)node)->dflt);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004132 if (rc || !((struct lys_node_choice *)node)->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004133 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default");
Michal Vaskodef0db12015-10-07 13:22:48 +02004134 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004135 }
4136 }
4137 }
4138
4139 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004140 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004141 if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004142 /* remove current value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004143 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004144
4145 /* set new value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004146 node->flags |= (rfn->flags & LYS_MAND_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004147 }
4148 }
4149
4150 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004151 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4152 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4153 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004154 }
4155
4156 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004157 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004158 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004159 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004160 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004161 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004162 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004163 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004164 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004165 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004166 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004167 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004168 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004169 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004170 }
4171 }
4172
4173 /* must in leaf, leaf-list, list, container or anyxml */
4174 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004175 switch (node->nodetype) {
4176 case LYS_LEAF:
4177 old_size = &((struct lys_node_leaf *)node)->must_size;
4178 old_must = &((struct lys_node_leaf *)node)->must;
4179 break;
4180 case LYS_LEAFLIST:
4181 old_size = &((struct lys_node_leaflist *)node)->must_size;
4182 old_must = &((struct lys_node_leaflist *)node)->must;
4183 break;
4184 case LYS_LIST:
4185 old_size = &((struct lys_node_list *)node)->must_size;
4186 old_must = &((struct lys_node_list *)node)->must;
4187 break;
4188 case LYS_CONTAINER:
4189 old_size = &((struct lys_node_container *)node)->must_size;
4190 old_must = &((struct lys_node_container *)node)->must;
4191 break;
4192 case LYS_ANYXML:
4193 old_size = &((struct lys_node_anyxml *)node)->must_size;
4194 old_must = &((struct lys_node_anyxml *)node)->must;
4195 break;
4196 default:
4197 LOGINT;
Radek Krejcie4e4d722015-10-05 16:53:50 +02004198 return -1;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004199 }
4200
4201 size = *old_size + rfn->must_size;
4202 must = realloc(*old_must, size * sizeof *rfn->must);
4203 if (!must) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004204 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004205 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004206 }
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004207 for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) {
4208 must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0);
4209 must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0);
4210 must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0);
4211 must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0);
4212 must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004213 }
4214
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004215 *old_must = must;
4216 *old_size = size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004217 }
4218 }
4219
4220 /* apply augments */
4221 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004222 rc = resolve_augment(&uses->augment[i], uses->child);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004223 if (rc) {
Michal Vaskodef0db12015-10-07 13:22:48 +02004224 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004225 }
4226 }
4227
4228 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004229}
4230
Michal Vasko730dfdf2015-08-11 14:48:05 +02004231/**
4232 * @brief Resolve base identity recursively. Does not log.
4233 *
4234 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004235 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004236 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004237 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004238 *
Michal Vaskof2006002016-04-21 16:28:15 +02004239 * @return EXIT_SUCCESS on success (but ret can still be NULL), EXIT_FAILURE on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004240 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004241static int
Michal Vasko1e62a092015-12-01 12:27:20 +01004242resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004243 struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004244{
Michal Vaskof02e3742015-08-05 16:27:02 +02004245 uint32_t i, j;
Radek Krejcibabbff82016-02-19 13:31:37 +01004246 struct lys_ident *base = NULL, *base_iter;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004247
Radek Krejcicf509982015-12-15 09:22:44 +01004248 assert(ret);
4249
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004250 /* search module */
4251 for (i = 0; i < module->ident_size; i++) {
4252 if (!strcmp(basename, module->ident[i].name)) {
4253
4254 if (!ident) {
4255 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004256 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01004257 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004258 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004259 }
4260
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004261 base = &module->ident[i];
4262 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004263 }
4264 }
4265
4266 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004267 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
4268 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
4269 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004270
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004271 if (!ident) {
4272 *ret = &module->inc[j].submodule->ident[i];
4273 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004274 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004275
4276 base = &module->inc[j].submodule->ident[i];
4277 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004278 }
4279 }
4280 }
4281
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004282matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004283 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01004284 if (base) {
4285 /* check for circular reference */
4286 for (base_iter = base; base_iter; base_iter = base_iter->base) {
4287 if (ident == base_iter) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004288 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, base_iter->name, "base");
4289 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejcibabbff82016-02-19 13:31:37 +01004290 return EXIT_FAILURE;
4291 }
4292 }
4293 /* checks done, store the result */
4294 ident->base = base;
4295
4296 /* maintain backlinks to the derived identitise */
4297 while (base) {
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004298 /* 1. get current number of backlinks */
4299 if (base->der) {
4300 for (i = 0; base->der[i]; i++);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004301 } else {
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004302 i = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004303 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004304 base->der = ly_realloc(base->der, (i + 2) * sizeof *(base->der));
4305 if (!base->der) {
Michal Vasko253035f2015-12-17 16:58:13 +01004306 LOGMEM;
4307 return EXIT_FAILURE;
4308 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004309 base->der[i] = ident;
4310 base->der[i + 1] = NULL; /* array termination */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004311
Radek Krejcibabbff82016-02-19 13:31:37 +01004312 base = base->base;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004313 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004314
Radek Krejcicf509982015-12-15 09:22:44 +01004315 *ret = ident->base;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004316 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004317 }
4318
Michal Vaskof2006002016-04-21 16:28:15 +02004319 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004320}
4321
Michal Vasko730dfdf2015-08-11 14:48:05 +02004322/**
4323 * @brief Resolve base identity. Logs directly.
4324 *
4325 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004326 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004327 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01004328 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01004329 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004330 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004331 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004332 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004333static int
Michal Vasko1e62a092015-12-01 12:27:20 +01004334resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent,
Radek Krejci48464ed2016-03-17 15:44:09 +01004335 struct lys_type *type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004336{
4337 const char *name;
Michal Vasko2d851a92015-10-20 16:16:36 +02004338 int i, mod_name_len = 0;
Radek Krejcicf509982015-12-15 09:22:44 +01004339 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02004340 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01004341 struct lys_module *mod;
4342
4343 assert((ident && !type) || (!ident && type));
4344
4345 if (!type) {
4346 /* have ident to resolve */
4347 ret = &target;
4348 flags = ident->flags;
4349 mod = ident->module;
4350 } else {
4351 /* have type to fill */
4352 ret = &type->info.ident.ref;
4353 flags = type->parent->flags;
4354 mod = type->parent->module;
4355 }
Michal Vaskof2006002016-04-21 16:28:15 +02004356 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004357
4358 /* search for the base identity */
4359 name = strchr(basename, ':');
4360 if (name) {
4361 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02004362 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004363 name++;
4364
Michal Vasko2d851a92015-10-20 16:16:36 +02004365 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004366 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02004367 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004368 }
4369 } else {
4370 name = basename;
4371 }
4372
Radek Krejcic071c542016-01-27 14:57:51 +01004373 /* get module where to search */
4374 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
4375 if (!module) {
4376 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01004377 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01004378 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004379 }
4380
Radek Krejcic071c542016-01-27 14:57:51 +01004381 /* search in the identified module ... */
Michal Vaskof2006002016-04-21 16:28:15 +02004382 if (resolve_base_ident_sub(module, ident, name, ret)) {
Radek Krejcibabbff82016-02-19 13:31:37 +01004383 return EXIT_FAILURE;
Michal Vaskof2006002016-04-21 16:28:15 +02004384 } else if (*ret) {
4385 goto success;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004386 }
Radek Krejcic071c542016-01-27 14:57:51 +01004387 /* and all its submodules */
4388 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskof2006002016-04-21 16:28:15 +02004389 if (resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) {
Radek Krejcibabbff82016-02-19 13:31:37 +01004390 return EXIT_FAILURE;
Michal Vaskof2006002016-04-21 16:28:15 +02004391 } else if (*ret) {
4392 goto success;
Radek Krejcic071c542016-01-27 14:57:51 +01004393 }
4394 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004395
Michal Vaskof2006002016-04-21 16:28:15 +02004396 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004397 return EXIT_FAILURE;
Radek Krejcicf509982015-12-15 09:22:44 +01004398
4399success:
4400 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01004401 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
4402 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
Radek Krejcicf509982015-12-15 09:22:44 +01004403 return -1;
4404 }
4405
4406 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004407}
4408
Michal Vasko730dfdf2015-08-11 14:48:05 +02004409/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004410 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004411 *
4412 * @param[in] base Base identity.
Michal Vaskofb0873c2015-08-21 09:00:07 +02004413 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01004414 * @param[in] node Node where the identityref is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02004415 *
4416 * @return Pointer to the identity resolvent, NULL on error.
4417 */
Radek Krejcia52656e2015-08-05 13:41:50 +02004418struct lys_ident *
Radek Krejci48464ed2016-03-17 15:44:09 +01004419resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004420{
Michal Vaskoc633ca02015-08-21 14:03:51 +02004421 const char *mod_name, *name;
4422 int mod_name_len, rc;
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004423 int i;
4424 struct lys_ident *der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004425
Michal Vaskofb0873c2015-08-21 09:00:07 +02004426 if (!base || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004427 return NULL;
4428 }
4429
Michal Vaskoc633ca02015-08-21 14:03:51 +02004430 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01004431 if (rc < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004432 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02004433 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01004434 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejci02a04992016-03-17 16:06:37 +01004435 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02004436 return NULL;
4437 }
4438
Michal Vaskoc633ca02015-08-21 14:03:51 +02004439 if (!strcmp(base->name, name) && (!mod_name
4440 || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) {
Michal Vaskofb0873c2015-08-21 09:00:07 +02004441 return base;
4442 }
4443
Radek Krejci1b61d0e2016-04-15 13:55:44 +02004444 if (base->der) {
4445 for (der = base->der[i = 0]; base->der[i]; der = base->der[++i]) {
4446 if (!strcmp(der->name, name) &&
4447 (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) {
4448 /* we have match */
4449 return der;
4450 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004451 }
4452 }
4453
Radek Krejci48464ed2016-03-17 15:44:09 +01004454 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004455 return NULL;
4456}
4457
Michal Vasko730dfdf2015-08-11 14:48:05 +02004458/**
Michal Vasko7955b362015-09-04 14:18:15 +02004459 * @brief Resolve (find) choice default case. Does not log.
4460 *
4461 * @param[in] choic Choice to use.
4462 * @param[in] dflt Name of the default case.
4463 *
4464 * @return Pointer to the default node or NULL.
4465 */
4466static struct lys_node *
4467resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4468{
4469 struct lys_node *child, *ret;
4470
4471 LY_TREE_FOR(choic->child, child) {
4472 if (child->nodetype == LYS_USES) {
4473 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4474 if (ret) {
4475 return ret;
4476 }
4477 }
4478
Radek Krejci749190d2016-02-18 16:26:25 +01004479 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYXML | LYS_CASE
Michal Vasko7955b362015-09-04 14:18:15 +02004480 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
4481 return child;
4482 }
4483 }
4484
4485 return NULL;
4486}
4487
4488/**
Michal Vaskobb211122015-08-19 14:03:11 +02004489 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004490 *
Michal Vaskobb211122015-08-19 14:03:11 +02004491 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004492 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004493 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004494 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004495 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004496static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004497resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004498{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004499 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01004500 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02004501
Radek Krejci010e54b2016-03-15 09:40:34 +01004502 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself
4503 * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping)
4504 * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm
4505 * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the
4506 * LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02004507 for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
Michal Vaskoe91afce2015-08-12 12:21:00 +02004508
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004509 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01004510 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
4511 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004512 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004513 return -1;
4514 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004515 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004516 return -1;
4517 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004518 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004519 /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field
4520 * (and smaller flags - it uses only a limited set of flags)
4521 */
4522 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004523 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004524 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01004525 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02004526 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004527 }
4528
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004529 if (uses->grp->nacm) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004530 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004531 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004532 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004533 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004534 return EXIT_FAILURE;
4535 }
4536
Radek Krejci48464ed2016-03-17 15:44:09 +01004537 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004538 if (!rc) {
4539 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01004540 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004541 if (!((struct lys_node_grp *)par_grp)->nacm) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004542 LOGINT;
4543 return -1;
4544 }
Radek Krejci4372b4e2016-04-14 17:42:16 +02004545 ((struct lys_node_grp *)par_grp)->nacm--;
Radek Krejci010e54b2016-03-15 09:40:34 +01004546 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004547 }
Radek Krejcicf509982015-12-15 09:22:44 +01004548
4549 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004550 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01004551 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004552 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01004553 return -1;
4554 }
4555
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004556 return EXIT_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01004557 } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004558 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004559 uses->flags |= LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004560 }
4561
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004562 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004563}
4564
Michal Vasko730dfdf2015-08-11 14:48:05 +02004565/**
Michal Vasko9957e592015-08-17 15:04:09 +02004566 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004567 *
Michal Vaskobb211122015-08-19 14:03:11 +02004568 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004569 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004570 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004571 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004572 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004573static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004574resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004575{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004576 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01004577 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004578
4579 for (i = 0; i < list->keys_size; ++i) {
4580 /* get the key name */
4581 if ((value = strpbrk(keys_str, " \t\n"))) {
4582 len = value - keys_str;
4583 while (isspace(value[0])) {
4584 value++;
4585 }
4586 } else {
4587 len = strlen(keys_str);
4588 }
4589
Radek Krejcic4283442016-04-22 09:19:27 +02004590 rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004591 if (rc) {
Michal Vasko7a55bea2016-05-02 14:51:20 +02004592 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
4593 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004594 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004595
Radek Krejci48464ed2016-03-17 15:44:09 +01004596 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004597 /* check_key logs */
4598 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004599 }
4600
Radek Krejcicf509982015-12-15 09:22:44 +01004601 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004602 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004603 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
4604 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01004605 return -1;
4606 }
4607
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004608 /* prepare for next iteration */
4609 while (value && isspace(value[0])) {
4610 value++;
4611 }
4612 keys_str = value;
4613 }
4614
Michal Vaskof02e3742015-08-05 16:27:02 +02004615 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004616}
4617
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004618/**
Michal Vaskobf19d252015-10-08 15:39:17 +02004619 * @brief Resolve (check) all must conditions of \p node.
4620 * Logs directly.
4621 *
4622 * @param[in] node Data node with optional must statements.
Michal Vaskobf19d252015-10-08 15:39:17 +02004623 *
4624 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
4625 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004626static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004627resolve_must(struct lyd_node *node)
Michal Vaskof02e3742015-08-05 16:27:02 +02004628{
Michal Vaskobf19d252015-10-08 15:39:17 +02004629 uint8_t i, must_size;
4630 struct lys_restr *must;
4631 struct lyxp_set set;
4632
4633 assert(node);
4634 memset(&set, 0, sizeof set);
4635
4636 switch (node->schema->nodetype) {
4637 case LYS_CONTAINER:
4638 must_size = ((struct lys_node_container *)node->schema)->must_size;
4639 must = ((struct lys_node_container *)node->schema)->must;
4640 break;
4641 case LYS_LEAF:
4642 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
4643 must = ((struct lys_node_leaf *)node->schema)->must;
4644 break;
4645 case LYS_LEAFLIST:
4646 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
4647 must = ((struct lys_node_leaflist *)node->schema)->must;
4648 break;
4649 case LYS_LIST:
4650 must_size = ((struct lys_node_list *)node->schema)->must_size;
4651 must = ((struct lys_node_list *)node->schema)->must;
4652 break;
4653 case LYS_ANYXML:
4654 must_size = ((struct lys_node_anyxml *)node->schema)->must_size;
4655 must = ((struct lys_node_anyxml *)node->schema)->must;
4656 break;
4657 default:
4658 must_size = 0;
4659 break;
4660 }
4661
4662 for (i = 0; i < must_size; ++i) {
Michal Vasko944a5642016-03-21 11:48:58 +01004663 if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004664 return -1;
4665 }
4666
Michal Vasko944a5642016-03-21 11:48:58 +01004667 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02004668
Michal Vasko8146d4c2016-05-09 15:50:29 +02004669 if (!set.val.bool) {
Michal Vasko6ac68282016-04-11 10:56:47 +02004670 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
4671 if (must[i].emsg) {
4672 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg);
4673 }
4674 if (must[i].eapptag) {
4675 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
4676 }
Michal Vaskobf19d252015-10-08 15:39:17 +02004677 return 1;
4678 }
4679 }
4680
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004681 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02004682}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004683
Michal Vaskobf19d252015-10-08 15:39:17 +02004684/**
Michal Vaskocf024702015-10-08 15:01:42 +02004685 * @brief Resolve (find) when condition context node. Does not log.
4686 *
4687 * @param[in] node Data node, whose conditional definition is being decided.
4688 * @param[in] schema Schema node with a when condition.
4689 *
4690 * @return Context node.
4691 */
4692static struct lyd_node *
4693resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004694{
Michal Vaskocf024702015-10-08 15:01:42 +02004695 struct lyd_node *parent;
4696 struct lys_node *sparent;
4697 uint16_t i, data_depth, schema_depth;
4698
4699 /* find a not schema-only node */
4700 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
4701 schema = lys_parent(schema);
4702 if (!schema) {
4703 return NULL;
4704 }
4705 }
4706
4707 /* get node depths */
4708 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
4709 for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) {
4710 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) {
4711 ++schema_depth;
4712 }
4713 }
4714 if (data_depth < schema_depth) {
4715 return NULL;
4716 }
4717
4718 /* find the corresponding data node */
4719 for (i = 0; i < data_depth - schema_depth; ++i) {
4720 node = node->parent;
4721 }
4722 if (node->schema != schema) {
4723 return NULL;
4724 }
4725
4726 return node;
4727}
4728
Radek Krejci03b71f72016-03-16 11:10:09 +01004729int
Radek Krejci01696bf2016-03-18 13:19:36 +01004730resolve_applies_must(const struct lyd_node *node)
4731{
4732 switch (node->schema->nodetype) {
4733 case LYS_CONTAINER:
4734 return ((struct lys_node_container *)node->schema)->must_size;
4735 case LYS_LEAF:
4736 return ((struct lys_node_leaf *)node->schema)->must_size;
4737 case LYS_LEAFLIST:
4738 return ((struct lys_node_leaflist *)node->schema)->must_size;
4739 case LYS_LIST:
4740 return ((struct lys_node_list *)node->schema)->must_size;
4741 case LYS_ANYXML:
4742 return ((struct lys_node_anyxml *)node->schema)->must_size;
4743 default:
4744 return 0;
4745 }
4746}
4747
4748int
Radek Krejci03b71f72016-03-16 11:10:09 +01004749resolve_applies_when(const struct lyd_node *node)
4750{
4751 struct lys_node *parent;
4752
4753 assert(node);
4754
4755 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
4756 return 1;
4757 }
4758
4759 parent = node->schema;
4760 goto check_augment;
4761
4762 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4763 if (((struct lys_node_uses *)parent)->when) {
4764 return 1;
4765 }
4766check_augment:
4767
4768 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
4769 (((struct lys_node_augment *)parent->parent)->when))) {
4770
4771 }
4772 parent = lys_parent(parent);
4773 }
4774
4775 return 0;
4776}
4777
Michal Vaskocf024702015-10-08 15:01:42 +02004778/**
4779 * @brief Resolve (check) all when conditions relevant for \p node.
4780 * Logs directly.
4781 *
4782 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02004783 *
Radek Krejci03b71f72016-03-16 11:10:09 +01004784 * @return
4785 * -1 - error, ly_errno is set
4786 * 0 - true "when" statement
4787 * 0, ly_vecode = LYVE_NOCOND - false "when" statement
4788 * 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 +02004789 */
4790static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004791resolve_when(struct lyd_node *node)
Michal Vaskocf024702015-10-08 15:01:42 +02004792{
4793 struct lyd_node *ctx_node = NULL;
4794 struct lys_node *parent;
4795 struct lyxp_set set;
Radek Krejci51093642016-03-29 10:14:59 +02004796 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02004797
4798 assert(node);
4799 memset(&set, 0, sizeof set);
4800
4801 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko944a5642016-03-21 11:48:58 +01004802 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004803 if (rc) {
4804 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004805 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004806 }
Radek Krejci51093642016-03-29 10:14:59 +02004807 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004808 }
4809
Radek Krejci03b71f72016-03-16 11:10:09 +01004810 /* set boolean result of the condition */
Michal Vasko944a5642016-03-21 11:48:58 +01004811 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02004812 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004813 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004814 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004815 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004816 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004817 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004818 }
Radek Krejci51093642016-03-29 10:14:59 +02004819
4820 /* free xpath set content */
4821 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004822 }
4823
4824 parent = node->schema;
4825 goto check_augment;
4826
4827 /* check when in every schema node that affects node */
4828 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4829 if (((struct lys_node_uses *)parent)->when) {
4830 if (!ctx_node) {
4831 ctx_node = resolve_when_ctx_node(node, parent);
4832 if (!ctx_node) {
4833 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004834 rc = -1;
4835 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004836 }
4837 }
Michal Vasko944a5642016-03-21 11:48:58 +01004838 rc = lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004839 if (rc) {
4840 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004841 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004842 }
Radek Krejci51093642016-03-29 10:14:59 +02004843 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004844 }
4845
Michal Vasko944a5642016-03-21 11:48:58 +01004846 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02004847 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004848 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004849 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004850 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004851 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004852 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004853 }
Radek Krejci51093642016-03-29 10:14:59 +02004854
4855 /* free xpath set content */
4856 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004857 }
4858
4859check_augment:
4860 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) {
4861 if (!ctx_node) {
4862 ctx_node = resolve_when_ctx_node(node, parent->parent);
4863 if (!ctx_node) {
4864 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004865 rc = -1;
4866 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004867 }
4868 }
Michal Vasko944a5642016-03-21 11:48:58 +01004869 rc = lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004870 if (rc) {
4871 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004872 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004873 }
Radek Krejci51093642016-03-29 10:14:59 +02004874 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004875 }
4876
Michal Vasko944a5642016-03-21 11:48:58 +01004877 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02004878
Michal Vasko8146d4c2016-05-09 15:50:29 +02004879 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004880 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004881 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004882 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004883 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004884 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004885 }
Radek Krejci51093642016-03-29 10:14:59 +02004886
4887 /* free xpath set content */
4888 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004889 }
4890
4891 parent = lys_parent(parent);
4892 }
4893
Radek Krejci0b7704f2016-03-18 12:16:14 +01004894 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01004895
Radek Krejci51093642016-03-29 10:14:59 +02004896cleanup:
4897
4898 /* free xpath set content */
4899 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0);
4900
4901 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004902}
4903
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004904/**
Michal Vaskobb211122015-08-19 14:03:11 +02004905 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004906 *
4907 * @param[in] mod Main module.
4908 * @param[in] item Item to resolve. Type determined by \p type.
4909 * @param[in] type Type of the unresolved item.
4910 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02004911 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004912 *
4913 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
4914 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004915static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004916resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Radek Krejci48464ed2016-03-17 15:44:09 +01004917 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004918{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004919 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Radek Krejcic79c6b12016-07-26 15:11:49 +02004920 int rc = -1, has_str = 0, tpdf_flag = 0, i, k;
4921 unsigned int j;
Michal Vasko563ef092015-09-04 13:17:23 +02004922 struct lys_node *node;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004923 const char *expr;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004924
Radek Krejcic79c6b12016-07-26 15:11:49 +02004925 struct ly_set *refs, *procs;
4926 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004927 struct lys_ident *ident;
4928 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004929 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01004930 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01004931 struct yang_type *yang;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004932
4933 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004934 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004935 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004936 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004937 ident = item;
4938
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004939 rc = resolve_base_ident(mod, ident, expr, "identity", NULL);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004940 break;
4941 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004942 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004943 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004944 stype = item;
4945
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004946 rc = resolve_base_ident(mod, NULL, expr, "type", stype);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004947 break;
4948 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02004949 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004950 stype = item;
4951
Radek Krejci2f12f852016-01-08 12:59:57 +01004952 /* HACK - when there is no parent, we are in top level typedef and in that
4953 * case, the path has to contain absolute path, so we let the resolve_path_arg_schema()
4954 * know it via tpdf_flag */
4955 if (!node) {
Radek Krejci4f78b532016-02-17 13:43:00 +01004956 tpdf_flag = 1;
Radek Krejci2f12f852016-01-08 12:59:57 +01004957 node = (struct lys_node *)stype->parent;
4958 }
4959
Radek Krejci48464ed2016-03-17 15:44:09 +01004960 rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag,
Michal Vasko1e62a092015-12-01 12:27:20 +01004961 (const struct lys_node **)&stype->info.lref.target);
Michal Vasko01c6fd22016-05-20 11:43:05 +02004962 if (!tpdf_flag && !rc) {
4963 assert(stype->info.lref.target);
Radek Krejci46c4cd72016-01-21 15:13:52 +01004964 /* store the backlink from leafref target */
Michal Vasko01c6fd22016-05-20 11:43:05 +02004965 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
4966 rc = -1;
Radek Krejci46c4cd72016-01-21 15:13:52 +01004967 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01004968 }
4969
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004970 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02004971 case UNRES_TYPE_DER_TPDF:
4972 tpdf_flag = 1;
4973 /* no break */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004974 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01004975 /* parent */
4976 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004977 stype = item;
4978
Michal Vasko88c29542015-11-27 14:57:53 +01004979 /* HACK type->der is temporarily unparsed type statement */
4980 yin = (struct lyxml_elem *)stype->der;
4981 stype->der = NULL;
4982
Pavol Vicana0e4e672016-02-24 12:20:04 +01004983 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
4984 yang = (struct yang_type *)yin;
Radek Krejci3a5501d2016-07-18 22:03:34 +02004985 rc = yang_check_type(mod, node, yang, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01004986
4987 if (rc) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02004988 if (rc == -1) {
4989 yang->type->base = yang->base;
4990 lydict_remove(mod->ctx, yang->name);
4991 free(yang);
4992 stype->der = NULL;
4993 } else {
4994 /* may try again later */
4995 stype->der = (struct lys_tpdf *)yang;
4996 }
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004997 } else {
4998 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02004999 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01005000 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01005001 }
5002
Michal Vasko88c29542015-11-27 14:57:53 +01005003 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02005004 rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01005005 if (!rc) {
5006 /* we need to always be able to free this, it's safe only in this case */
5007 lyxml_free(mod->ctx, yin);
5008 } else {
5009 /* may try again later, put all back how it was */
5010 stype->der = (struct lys_tpdf *)yin;
5011 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005012 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005013 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005014 case UNRES_IFFEAT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005015 node = str_snode;
Radek Krejci9ff0a922016-07-14 13:08:05 +02005016 expr = *((const char **)item);
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005017
Radek Krejci9ff0a922016-07-14 13:08:05 +02005018 rc = resolve_feature(expr, strlen(expr), node, item);
5019 if (!rc) {
5020 /* success */
5021 free((char *)expr);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005022 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005023 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02005024 case UNRES_FEATURE:
5025 feat = (struct lys_feature *)item;
5026
5027 if (feat->iffeature_size) {
5028 refs = ly_set_new();
5029 procs = ly_set_new();
5030 ly_set_add(procs, feat, 0);
5031
5032 while (procs->number) {
5033 ref = procs->set.g[procs->number - 1];
5034 ly_set_rm_index(procs, procs->number - 1);
5035
5036 for (i = 0; i < ref->iffeature_size; i++) {
5037 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
5038 for (; j > 0 ; j--) {
5039 if (unres_schema_find(unres, &ref->iffeature[i].features[j - 1], UNRES_IFFEAT) == -1) {
5040 if (ref->iffeature[i].features[j - 1] == feat) {
5041 LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
5042 goto featurecheckdone;
5043 }
5044
5045 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
5046 k = refs->number;
5047 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
5048 /* not yet seen feature, add it for processing */
5049 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
5050 }
5051 }
5052 } else {
5053 /* forward reference */
5054 rc = EXIT_FAILURE;
5055 goto featurecheckdone;
5056 }
5057 }
5058
5059 }
5060 }
5061 rc = EXIT_SUCCESS;
5062
5063featurecheckdone:
5064 ly_set_free(refs);
5065 ly_set_free(procs);
5066 }
5067
5068 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005069 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01005070 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005071 break;
5072 case UNRES_TYPE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005073 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005074 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005075 stype = item;
5076
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005077 rc = check_default(stype, expr, mod);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005078 break;
5079 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005080 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01005081 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005082 choic = item;
5083
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005084 choic->dflt = resolve_choice_dflt(choic, expr);
Michal Vasko7955b362015-09-04 14:18:15 +02005085 if (choic->dflt) {
5086 rc = EXIT_SUCCESS;
5087 } else {
5088 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02005089 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005090 break;
5091 case UNRES_LIST_KEYS:
Radek Krejci4f78b532016-02-17 13:43:00 +01005092 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01005093 rc = resolve_list_keys(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005094 break;
5095 case UNRES_LIST_UNIQ:
Radek Krejci4f78b532016-02-17 13:43:00 +01005096 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01005097 rc = resolve_unique(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005098 break;
Michal Vasko7178e692016-02-12 15:58:05 +01005099 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005100 rc = resolve_augment(item, NULL);
Michal Vasko7178e692016-02-12 15:58:05 +01005101 break;
Michal Vaskocf024702015-10-08 15:01:42 +02005102 default:
5103 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005104 break;
5105 }
5106
Radek Krejci054d5ac2016-06-07 10:24:03 +02005107 if (has_str && (!rc || rc != EXIT_FAILURE)) {
5108 /* the string is no more needed in case of success
5109 * or fatal error. In case of forward reference,
5110 * we will try to resolve the string later */
Radek Krejci4f78b532016-02-17 13:43:00 +01005111 lydict_remove(mod->ctx, str_snode);
5112 }
5113
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005114 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005115}
5116
Michal Vaskof02e3742015-08-05 16:27:02 +02005117/* logs directly */
5118static void
Radek Krejci48464ed2016-03-17 15:44:09 +01005119print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005120{
Michal Vaskocb34dc62016-05-20 14:38:37 +02005121 struct lyxml_elem *xml;
5122 struct lyxml_attr *attr;
Radek Krejci76e15e12016-06-22 11:02:24 +02005123 const char *type_name = NULL;
Michal Vaskocb34dc62016-05-20 14:38:37 +02005124
Michal Vaskof02e3742015-08-05 16:27:02 +02005125 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02005126 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005127 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005128 break;
5129 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01005130 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005131 break;
5132 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01005133 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
5134 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02005135 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02005136 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02005137 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02005138 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
5139 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
5140 type_name = ((struct yang_type *)xml)->name;
5141 } else {
5142 LY_TREE_FOR(xml->attr, attr) {
5143 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
5144 type_name = attr->value;
5145 break;
5146 }
5147 }
5148 assert(attr);
5149 }
5150 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", type_name);
Michal Vaskof02e3742015-08-05 16:27:02 +02005151 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02005152 case UNRES_IFFEAT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005153 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)item);
Michal Vaskof02e3742015-08-05 16:27:02 +02005154 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02005155 case UNRES_FEATURE:
5156 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
5157 ((struct lys_feature *)item)->name);
5158 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02005159 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01005160 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02005161 break;
5162 case UNRES_TYPE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005163 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005164 break;
5165 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005166 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005167 break;
5168 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01005169 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005170 break;
5171 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01005172 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02005173 break;
Michal Vasko7178e692016-02-12 15:58:05 +01005174 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01005175 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
5176 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01005177 break;
Michal Vaskocf024702015-10-08 15:01:42 +02005178 default:
5179 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02005180 break;
5181 }
5182}
5183
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005184/**
Michal Vaskobb211122015-08-19 14:03:11 +02005185 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005186 *
5187 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005188 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005189 *
Michal Vasko92b8a382015-08-19 14:03:49 +02005190 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005191 */
Michal Vaskof02e3742015-08-05 16:27:02 +02005192int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005193resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02005194{
Radek Krejci010e54b2016-03-15 09:40:34 +01005195 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005196 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005197
5198 assert(unres);
5199
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005200 LOGVRB("Resolving unresolved schema nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01005201 ly_vlog_hide(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02005202
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005203 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02005204 do {
Michal Vasko88c29542015-11-27 14:57:53 +01005205 unres_count = 0;
5206 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02005207
5208 for (i = 0; i < unres->count; ++i) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02005209 /* we do not need to have UNRES_TYPE_IDENTREF resolved, we need its type's base only,
Radek Krejcic79c6b12016-07-26 15:11:49 +02005210 * but UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
5211 * if-features are resolved here to make sure that we will have all if-features for
5212 * later check of feature circular dependency */
5213 if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_IFFEAT) && (unres->type[i] != UNRES_TYPE_DER)
Radek Krejci3a5501d2016-07-18 22:03:34 +02005214 && (unres->type[i] != UNRES_TYPE_DER_TPDF) && (unres->type[i] != UNRES_TYPE_LEAFREF)) {
Michal Vasko51054ca2015-08-12 12:20:00 +02005215 continue;
5216 }
5217
Michal Vasko88c29542015-11-27 14:57:53 +01005218 ++unres_count;
Radek Krejci48464ed2016-03-17 15:44:09 +01005219 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005220 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02005221 unres->type[i] = UNRES_RESOLVED;
5222 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01005223 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02005224 } else if (rc == -1) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005225 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02005226 /* print the error */
5227 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005228 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02005229 } else {
5230 /* forward reference, erase ly_errno */
5231 ly_errno = LY_SUCCESS;
5232 ly_vecode = LYVE_SUCCESS;
Michal Vasko51054ca2015-08-12 12:20:00 +02005233 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005234 }
Michal Vasko88c29542015-11-27 14:57:53 +01005235 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02005236
Michal Vasko88c29542015-11-27 14:57:53 +01005237 if (res_count < unres_count) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02005238 /* just print the errors */
5239 ly_vlog_hide(0);
5240
5241 for (i = 0; i < unres->count; ++i) {
5242 if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_TYPE_DER)
Radek Krejci3a5501d2016-07-18 22:03:34 +02005243 && (unres->type[i] != UNRES_TYPE_DER_TPDF) && (unres->type[i] != UNRES_TYPE_LEAFREF)) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02005244 continue;
5245 }
5246 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
5247 }
Michal Vasko92b8a382015-08-19 14:03:49 +02005248 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005249 }
5250
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005251 /* the rest */
5252 for (i = 0; i < unres->count; ++i) {
5253 if (unres->type[i] == UNRES_RESOLVED) {
5254 continue;
5255 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02005256
Radek Krejci48464ed2016-03-17 15:44:09 +01005257 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005258 if (rc == 0) {
5259 unres->type[i] = UNRES_RESOLVED;
5260 ++resolved;
5261 } else if (rc == -1) {
5262 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02005263 /* print the error */
5264 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
5265 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005266 }
5267 }
5268
Radek Krejci010e54b2016-03-15 09:40:34 +01005269 ly_vlog_hide(0);
5270
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005271 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005272 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
5273 * all the validation errors
5274 */
5275 for (i = 0; i < unres->count; ++i) {
5276 if (unres->type[i] == UNRES_RESOLVED) {
5277 continue;
5278 }
Radek Krejci48464ed2016-03-17 15:44:09 +01005279 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005280 }
Michal Vasko92b8a382015-08-19 14:03:49 +02005281 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005282 }
5283
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005284 LOGVRB("All schema nodes and constraints resolved.");
Radek Krejcic071c542016-01-27 14:57:51 +01005285 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005286 return EXIT_SUCCESS;
5287}
5288
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005289/**
Michal Vaskobb211122015-08-19 14:03:11 +02005290 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005291 *
5292 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005293 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005294 * @param[in] item Item to resolve. Type determined by \p type.
5295 * @param[in] type Type of the unresolved item.
5296 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005297 *
Michal Vasko3767fb22016-07-21 12:10:57 +02005298 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005299 */
5300int
Radek Krejci48464ed2016-03-17 15:44:09 +01005301unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
5302 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005303{
Radek Krejci48464ed2016-03-17 15:44:09 +01005304 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 +02005305}
5306
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005307/**
Michal Vaskobb211122015-08-19 14:03:11 +02005308 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005309 *
5310 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005311 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005312 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01005313 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005314 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005315 *
Michal Vasko3767fb22016-07-21 12:10:57 +02005316 * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005317 */
5318int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005319unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01005320 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005321{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005322 int rc;
Michal Vasko88c29542015-11-27 14:57:53 +01005323 struct lyxml_elem *yin;
Radek Krejci010e54b2016-03-15 09:40:34 +01005324 char *path, *msg;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005325
Michal Vasko9bf425b2015-10-22 11:42:03 +02005326 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
5327 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005328
Radek Krejci010e54b2016-03-15 09:40:34 +01005329 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01005330 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01005331 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005332 if (rc != EXIT_FAILURE) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005333 if (rc == -1 && ly_errno == LY_EVALID) {
Radek Krejci76e15e12016-06-22 11:02:24 +02005334 if (ly_log_level >= LY_LLERR) {
5335 path = strdup(ly_errpath());
5336 msg = strdup(ly_errmsg());
5337 LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
5338 free(path);
5339 free(msg);
5340 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005341 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005342 return rc;
Radek Krejcif347abc2016-06-22 10:18:47 +02005343 } else {
5344 /* erase info about validation errors */
5345 ly_errno = LY_SUCCESS;
5346 ly_vecode = LYVE_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005347 }
5348
Radek Krejci48464ed2016-03-17 15:44:09 +01005349 print_unres_schema_item_fail(item, type, snode);
Michal Vaskof02e3742015-08-05 16:27:02 +02005350
Michal Vasko88c29542015-11-27 14:57:53 +01005351 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
Radek Krejci3a5501d2016-07-18 22:03:34 +02005352 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
Michal Vasko88c29542015-11-27 14:57:53 +01005353 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
Pavol Vicana0e4e672016-02-24 12:20:04 +01005354 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
5355 lyxml_unlink_elem(mod->ctx, yin, 1);
5356 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
5357 }
Michal Vasko88c29542015-11-27 14:57:53 +01005358 }
5359
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005360 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01005361 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
5362 if (!unres->item) {
5363 LOGMEM;
5364 return -1;
5365 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005366 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01005367 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
5368 if (!unres->type) {
5369 LOGMEM;
5370 return -1;
5371 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005372 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01005373 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
5374 if (!unres->str_snode) {
5375 LOGMEM;
5376 return -1;
5377 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005378 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01005379 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
5380 if (!unres->module) {
5381 LOGMEM;
5382 return -1;
5383 }
5384 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005385
Michal Vasko3767fb22016-07-21 12:10:57 +02005386 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005387}
5388
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005389/**
Michal Vaskobb211122015-08-19 14:03:11 +02005390 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005391 *
5392 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005393 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005394 * @param[in] item Old item to be resolved.
5395 * @param[in] type Type of the old unresolved item.
5396 * @param[in] new_item New item to use in the duplicate.
5397 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02005398 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005399 */
Michal Vaskodad19402015-08-06 09:51:53 +02005400int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005401unres_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 +02005402{
5403 int i;
5404
Michal Vaskocf024702015-10-08 15:01:42 +02005405 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005406
Michal Vasko0bd29d12015-08-19 11:45:49 +02005407 i = unres_schema_find(unres, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005408
5409 if (i == -1) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02005410 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005411 }
5412
Radek Krejcic79c6b12016-07-26 15:11:49 +02005413 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
5414 (type == UNRES_IFFEAT) || (type == UNRES_FEATURE)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005415 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005416 LOGINT;
5417 return -1;
5418 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005419 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005420 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005421 LOGINT;
5422 return -1;
5423 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005424 }
Michal Vaskodad19402015-08-06 09:51:53 +02005425
5426 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005427}
5428
Michal Vaskof02e3742015-08-05 16:27:02 +02005429/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005430int
Michal Vasko0bd29d12015-08-19 11:45:49 +02005431unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005432{
5433 uint32_t ret = -1, i;
5434
Radek Krejcic79c6b12016-07-26 15:11:49 +02005435 for (i = unres->count; i > 0; i--) {
5436 if ((unres->item[i - 1] == item) && (unres->type[i - 1] == type)) {
5437 ret = i - 1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005438 break;
5439 }
5440 }
5441
5442 return ret;
5443}
Michal Vasko8bcdf292015-08-19 14:04:43 +02005444
Michal Vaskoede9c472016-06-07 09:38:15 +02005445static void
5446unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
5447{
5448 struct lyxml_elem *yin;
5449 struct yang_type *yang;
5450
5451 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02005452 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02005453 case UNRES_TYPE_DER:
5454 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
5455 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
5456 yang =(struct yang_type *)yin;
5457 yang->type->base = yang->base;
5458 lydict_remove(ctx, yang->name);
5459 free(yang);
5460 } else {
5461 lyxml_free(ctx, yin);
5462 }
5463 break;
5464 case UNRES_IDENT:
5465 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02005466 case UNRES_TYPE_DFLT:
5467 case UNRES_CHOICE_DFLT:
5468 case UNRES_LIST_KEYS:
5469 case UNRES_LIST_UNIQ:
5470 lydict_remove(ctx, (const char *)unres->str_snode[i]);
5471 break;
5472 default:
5473 break;
5474 }
5475 unres->type[i] = UNRES_RESOLVED;
5476}
5477
Michal Vasko88c29542015-11-27 14:57:53 +01005478void
Radek Krejcic071c542016-01-27 14:57:51 +01005479unres_schema_free(struct lys_module *module, struct unres_schema **unres)
Michal Vasko88c29542015-11-27 14:57:53 +01005480{
5481 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01005482 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01005483
Radek Krejcic071c542016-01-27 14:57:51 +01005484 if (!unres || !(*unres)) {
5485 return;
Michal Vasko88c29542015-11-27 14:57:53 +01005486 }
5487
Radek Krejcic071c542016-01-27 14:57:51 +01005488 assert(module || (*unres)->count == 0);
5489
5490 for (i = 0; i < (*unres)->count; ++i) {
5491 if ((*unres)->module[i] != module) {
5492 if ((*unres)->type[i] != UNRES_RESOLVED) {
5493 unresolved++;
5494 }
5495 continue;
5496 }
Michal Vaskoede9c472016-06-07 09:38:15 +02005497
5498 /* free heap memory for the specific item */
5499 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01005500 }
5501
Michal Vaskoede9c472016-06-07 09:38:15 +02005502 /* free it all */
Radek Krejcic071c542016-01-27 14:57:51 +01005503 if (!module || (!unresolved && !module->type)) {
5504 free((*unres)->item);
5505 free((*unres)->type);
5506 free((*unres)->str_snode);
5507 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01005508 free((*unres));
5509 (*unres) = NULL;
5510 }
Michal Vasko88c29542015-11-27 14:57:53 +01005511}
5512
Michal Vasko8bcdf292015-08-19 14:04:43 +02005513/**
5514 * @brief Resolve a single unres data item. Logs directly.
5515 *
Michal Vaskocf024702015-10-08 15:01:42 +02005516 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02005517 * @param[in] type Type of the unresolved item.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005518 *
5519 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
5520 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02005521int
Radek Krejci48464ed2016-03-17 15:44:09 +01005522resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02005523{
5524 uint32_t i;
Michal Vasko0491ab32015-08-19 14:28:29 +02005525 int rc;
Michal Vasko83a6c462015-10-08 16:43:53 +02005526 struct lyd_node_leaf_list *leaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005527 struct lys_node_leaf *sleaf;
Michal Vaskoc4280842016-04-19 16:10:42 +02005528 struct lyd_node *parent;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005529 struct unres_data matches;
5530
5531 memset(&matches, 0, sizeof matches);
Michal Vasko83a6c462015-10-08 16:43:53 +02005532 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02005533 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005534
Michal Vaskocf024702015-10-08 15:01:42 +02005535 switch (type) {
5536 case UNRES_LEAFREF:
5537 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vasko2471e7f2016-04-11 11:00:15 +02005538 /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */
5539 if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) {
5540 return -1;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005541 }
5542
5543 /* check that value matches */
5544 for (i = 0; i < matches.count; ++i) {
Radek Krejci749190d2016-02-18 16:26:25 +01005545 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) {
Michal Vaskocf024702015-10-08 15:01:42 +02005546 leaf->value.leafref = matches.node[i];
Michal Vasko8bcdf292015-08-19 14:04:43 +02005547 break;
5548 }
5549 }
5550
Michal Vaskocf024702015-10-08 15:01:42 +02005551 free(matches.node);
Michal Vasko8bcdf292015-08-19 14:04:43 +02005552
Michal Vaskocf024702015-10-08 15:01:42 +02005553 if (!leaf->value.leafref) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02005554 /* reference not found */
Michal Vasko6ac68282016-04-11 10:56:47 +02005555 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02005556 return EXIT_FAILURE;
5557 }
Michal Vaskocf024702015-10-08 15:01:42 +02005558 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005559
Michal Vaskocf024702015-10-08 15:01:42 +02005560 case UNRES_INSTID:
Michal Vasko976a5f22016-05-18 13:28:42 +02005561 assert((sleaf->type.base == LY_TYPE_INST) || (sleaf->type.base == LY_TYPE_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02005562 ly_errno = 0;
Radek Krejci48464ed2016-03-17 15:44:09 +01005563 leaf->value.instance = resolve_instid(node, leaf->value_str);
Radek Krejci40f17b92016-02-03 14:30:43 +01005564 if (!leaf->value.instance) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02005565 if (ly_errno) {
5566 return -1;
5567 } else if (sleaf->type.info.inst.req > -1) {
Michal Vasko6ac68282016-04-11 10:56:47 +02005568 LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02005569 return EXIT_FAILURE;
5570 } else {
Radek Krejci4ce42be2016-02-03 13:04:41 +01005571 LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02005572 }
5573 }
Michal Vaskocf024702015-10-08 15:01:42 +02005574 break;
5575
5576 case UNRES_WHEN:
Radek Krejci48464ed2016-03-17 15:44:09 +01005577 if ((rc = resolve_when(node))) {
Michal Vaskocf024702015-10-08 15:01:42 +02005578 return rc;
5579 }
5580 break;
5581
Michal Vaskobf19d252015-10-08 15:39:17 +02005582 case UNRES_MUST:
Radek Krejci48464ed2016-03-17 15:44:09 +01005583 if ((rc = resolve_must(node))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005584 return rc;
5585 }
5586 break;
5587
Michal Vaskoc4280842016-04-19 16:10:42 +02005588 case UNRES_EMPTYCONT:
5589 do {
5590 parent = node->parent;
5591 lyd_free(node);
5592 node = parent;
5593 } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child
5594 && !((struct lys_node_container *)node->schema)->presence);
5595 break;
5596
Michal Vaskocf024702015-10-08 15:01:42 +02005597 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02005598 LOGINT;
5599 return -1;
5600 }
5601
5602 return EXIT_SUCCESS;
5603}
5604
5605/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01005606 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02005607 *
5608 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02005609 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005610 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01005611 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005612 */
5613int
Radek Krejci0b7704f2016-03-18 12:16:14 +01005614unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02005615{
Radek Krejci03b71f72016-03-16 11:10:09 +01005616 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02005617 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
5618 || (type == UNRES_EMPTYCONT));
Michal Vasko8bcdf292015-08-19 14:04:43 +02005619
Radek Krejci03b71f72016-03-16 11:10:09 +01005620 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01005621 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
5622 if (!unres->node) {
5623 LOGMEM;
5624 return -1;
5625 }
Michal Vaskocf024702015-10-08 15:01:42 +02005626 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01005627 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
5628 if (!unres->type) {
5629 LOGMEM;
5630 return -1;
5631 }
Michal Vaskocf024702015-10-08 15:01:42 +02005632 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005633
Radek Krejci0b7704f2016-03-18 12:16:14 +01005634 if (type == UNRES_WHEN) {
5635 /* remove previous result */
5636 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005637 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005638
5639 return EXIT_SUCCESS;
5640}
5641
5642/**
5643 * @brief Resolve every unres data item in the structure. Logs directly.
5644 *
5645 * @param[in] unres Unres data structure to use.
Radek Krejci03b71f72016-03-16 11:10:09 +01005646 * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If
5647 * NULL and when condition is false the error is raised.
Radek Krejci0c0086a2016-03-24 15:20:28 +01005648 * @param[in] options Parer options
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005649 *
5650 * @return EXIT_SUCCESS on success, -1 on error.
5651 */
5652int
Radek Krejci0c0086a2016-03-24 15:20:28 +01005653resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005654{
Radek Krejci0c0086a2016-03-24 15:20:28 +01005655 uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01005656 int rc, progress;
Radek Krejci03b71f72016-03-16 11:10:09 +01005657 char *msg, *path;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005658 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005659
Radek Krejci03b71f72016-03-16 11:10:09 +01005660 assert(unres);
Radek Krejci8ee1b562016-03-31 10:58:31 +02005661 assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL));
Radek Krejci03b71f72016-03-16 11:10:09 +01005662
5663 if (!unres->count) {
5664 return EXIT_SUCCESS;
5665 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005666
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005667 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01005668 ly_vlog_hide(1);
5669
Radek Krejci0b7704f2016-03-18 12:16:14 +01005670 /* when-stmt first */
Radek Krejci03b71f72016-03-16 11:10:09 +01005671 ly_errno = LY_SUCCESS;
5672 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005673 do {
5674 progress = 0;
5675 for(i = 0; i < unres->count; i++) {
5676 if (unres->type[i] != UNRES_WHEN) {
5677 continue;
5678 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005679 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005680 /* count when-stmt nodes in unres list */
5681 when_stmt++;
5682 }
5683
5684 /* resolve when condition only when all parent when conditions are already resolved */
5685 for (parent = unres->node[i]->parent;
5686 parent && LYD_WHEN_DONE(parent->when_status);
5687 parent = parent->parent) {
5688 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
5689 /* the parent node was already unlinked, do not resolve this node,
5690 * it will be removed anyway, so just mark it as resolved
5691 */
5692 unres->node[i]->when_status |= LYD_WHEN_FALSE;
5693 unres->type[i] = UNRES_RESOLVED;
5694 resolved++;
5695 break;
5696 }
5697 }
5698 if (parent) {
5699 continue;
5700 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005701
Radek Krejci48464ed2016-03-17 15:44:09 +01005702 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005703 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005704 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
5705 if (!root) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005706 /* false when condition */
5707 ly_vlog_hide(0);
Radek Krejci76e15e12016-06-22 11:02:24 +02005708 if (ly_log_level >= LY_LLERR) {
5709 path = strdup(ly_errpath());
5710 msg = strdup(ly_errmsg());
5711 LOGERR(LY_EVALID, "%s%s%s%s", msg,
5712 path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
5713 free(path);
5714 free(msg);
5715 }
Radek Krejci03b71f72016-03-16 11:10:09 +01005716 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005717 } /* follows else */
5718
Radek Krejci0c0086a2016-03-24 15:20:28 +01005719 /* only unlink now, the subtree can contain another nodes stored in the unres list */
5720 /* if it has parent non-presence containers that would be empty, we should actually
5721 * remove the container
5722 */
5723 if (!(options & LYD_OPT_KEEPEMPTYCONT)) {
5724 for (parent = unres->node[i];
5725 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
5726 parent = parent->parent) {
5727 if (((struct lys_node_container *)parent->parent->schema)->presence) {
5728 /* presence container */
5729 break;
5730 }
5731 if (parent->next || parent->prev != parent) {
5732 /* non empty (the child we are in and we are going to remove is not the only child) */
5733 break;
5734 }
5735 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005736 unres->node[i] = parent;
5737 }
5738
Radek Krejci0b7704f2016-03-18 12:16:14 +01005739 /* auto-delete */
5740 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
5741 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
Radek Krejci0c0086a2016-03-24 15:20:28 +01005742 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005743 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01005744 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005745
Radek Krejci0b7704f2016-03-18 12:16:14 +01005746 lyd_unlink(unres->node[i]);
5747 unres->type[i] = UNRES_DELETE;
5748 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01005749
5750 /* update the rest of unres items */
5751 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01005752 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01005753 continue;
5754 }
5755
5756 /* test if the node is in subtree to be deleted */
5757 for (parent = unres->node[j]; parent; parent = parent->parent) {
5758 if (parent == unres->node[i]) {
5759 /* yes, it is */
5760 unres->type[j] = UNRES_RESOLVED;
5761 resolved++;
5762 break;
5763 }
5764 }
5765 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005766 } else {
5767 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01005768 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005769 ly_errno = LY_SUCCESS;
5770 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005771 resolved++;
5772 progress = 1;
5773 } else if (rc == -1) {
5774 ly_vlog_hide(0);
5775 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02005776 } else {
5777 /* forward reference, erase ly_errno */
5778 ly_errno = LY_SUCCESS;
5779 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005780 }
5781 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005782 first = 0;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005783 } while (progress && resolved < when_stmt);
Radek Krejci010e54b2016-03-15 09:40:34 +01005784
Radek Krejci0b7704f2016-03-18 12:16:14 +01005785 /* do we have some unresolved when-stmt? */
Radek Krejcid940d732016-03-24 16:02:28 +01005786 if (when_stmt > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005787 ly_vlog_hide(0);
Radek Krejci76e15e12016-06-22 11:02:24 +02005788 if (ly_log_level >= LY_LLERR) {
5789 path = strdup(ly_errpath());
5790 msg = strdup(ly_errmsg());
5791 LOGERR(LY_EVALID, "%s%s%s%s", msg, path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
5792 free(path);
5793 free(msg);
5794 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005795 return -1;
5796 }
5797
5798 for (i = 0; del_items && i < unres->count; i++) {
5799 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
5800 if (unres->type[i] != UNRES_DELETE) {
5801 continue;
5802 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005803 if (!unres->node[i]) {
5804 unres->type[i] = UNRES_RESOLVED;
5805 del_items--;
5806 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005807 }
5808
5809 /* really remove the complete subtree */
5810 lyd_free(unres->node[i]);
5811 unres->type[i] = UNRES_RESOLVED;
5812 del_items--;
5813 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005814
5815 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005816 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005817 if (unres->type[i] == UNRES_RESOLVED) {
5818 continue;
5819 }
5820
Radek Krejci48464ed2016-03-17 15:44:09 +01005821 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005822 if (rc == 0) {
5823 unres->type[i] = UNRES_RESOLVED;
5824 resolved++;
5825 } else if (rc == -1) {
5826 ly_vlog_hide(0);
Michal Vasko96b846c2016-05-18 13:28:58 +02005827 /* print only this last error */
5828 resolve_unres_data_item(unres->node[i], unres->type[i]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005829 return -1;
5830 }
5831 }
5832
Radek Krejci010e54b2016-03-15 09:40:34 +01005833 ly_vlog_hide(0);
5834 if (resolved < unres->count) {
5835 /* try to resolve the unresolved data again, it will not resolve anything, but it will print
5836 * all the validation errors
5837 */
5838 for (i = 0; i < unres->count; ++i) {
5839 if (unres->type[i] == UNRES_RESOLVED) {
5840 continue;
5841 }
Radek Krejci48464ed2016-03-17 15:44:09 +01005842 resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005843 }
5844 return -1;
5845 }
5846
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005847 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01005848 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005849 return EXIT_SUCCESS;
5850}