blob: 2cb2436e998bac5d5b0a822414103185eef8bd15 [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 Vasko3edeaf72016-02-11 13:17:43 +0100958 * @brief Resolve (find) a data node based on a schema-nodeid.
959 *
960 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
961 * module).
962 *
963 */
964struct lyd_node *
965resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
966{
967 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +0200968 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +0100969 const struct lys_node *schema = NULL;
Radek Krejcicc217a62016-04-08 16:58:11 +0200970 int shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +0100971
972 assert(nodeid && start);
973
974 if (nodeid[0] == '/') {
975 return NULL;
976 }
977
978 str = p = strdup(nodeid);
979 if (!str) {
980 LOGMEM;
981 return NULL;
982 }
Radek Krejci5da4eb62016-04-08 14:45:51 +0200983
Michal Vasko3edeaf72016-02-11 13:17:43 +0100984 while (p) {
985 token = p;
986 p = strchr(p, '/');
987 if (p) {
988 *p = '\0';
989 p++;
990 }
991
Radek Krejci5da4eb62016-04-08 14:45:51 +0200992 if (p) {
993 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +0200994 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Radek Krejcif3c71de2016-04-11 12:45:46 +0200995 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +0200996 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +0200997 result = NULL;
998 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +0200999 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001000
Radek Krejci5da4eb62016-04-08 14:45:51 +02001001 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1002 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001003 } else if (lys_parent(schema)->nodetype == LYS_CHOICE) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001004 /* shorthand case */
1005 if (!shorthand) {
1006 shorthand = 1;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001007 schema = lys_parent(schema);
Radek Krejcicc217a62016-04-08 16:58:11 +02001008 continue;
1009 } else {
1010 shorthand = 0;
1011 if (schema->nodetype == LYS_LEAF) {
1012 /* should not be here, since we have leaf, which is not a shorthand nor final node */
1013 result = NULL;
1014 break;
1015 }
1016 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001017 }
1018 } else {
1019 /* final node */
Radek Krejcif3c71de2016-04-11 12:45:46 +02001020 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF,
1021 shorthand ? 0 : 1, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001022 || !schema) {
1023 result = NULL;
1024 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001025 }
1026 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001027 LY_TREE_FOR(result ? result->child : start, iter) {
1028 if (iter->schema == schema) {
1029 /* move in data tree according to returned schema */
1030 result = iter;
1031 break;
1032 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001033 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001034 if (!iter) {
1035 /* instance not found */
1036 result = NULL;
1037 break;
1038 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001039 }
1040 free(str);
1041
1042 return result;
1043}
1044
Radek Krejcibdf92362016-04-08 14:43:34 +02001045/*
1046 * 0 - ok (done)
1047 * 1 - continue
1048 * 2 - break
1049 * -1 - error
1050 */
1051static int
Radek Krejcicc217a62016-04-08 16:58:11 +02001052schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id,
Radek Krejcibdf92362016-04-08 14:43:34 +02001053 const struct lys_module *module, const char *mod_name, int mod_name_len,
1054 const struct lys_node **start)
1055{
1056 const struct lys_module *prefix_mod;
Radek Krejcicc217a62016-04-08 16:58:11 +02001057 int sh = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001058
1059 /* module check */
1060 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1061 if (!prefix_mod) {
1062 return -1;
1063 }
1064 if (prefix_mod != lys_node_module(sibling)) {
1065 return 1;
1066 }
1067
1068 /* check for shorthand cases - then 'start' does not change */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001069 if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001070 if (*shorthand != -1) {
1071 *shorthand = *shorthand ? 0 : 1;
1072 }
1073 sh = 1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001074 }
1075
1076 /* the result node? */
1077 if (!id[0]) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001078 if (*shorthand == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001079 return 1;
1080 }
1081 return 0;
1082 }
1083
Radek Krejcicc217a62016-04-08 16:58:11 +02001084 if (!sh) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001085 /* move down the tree, if possible */
1086 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
1087 return -1;
1088 }
1089 *start = sibling->child;
1090 }
1091
1092 return 2;
1093}
1094
Michal Vasko3edeaf72016-02-11 13:17:43 +01001095/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1096int
1097resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module,
1098 const struct lys_node **ret)
1099{
1100 const char *name, *mod_name, *id;
1101 const struct lys_node *sibling;
1102 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001103 int8_t shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001104 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001105 const struct lys_module *start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001106
1107 assert(nodeid && (start || module) && !(start && module) && ret);
1108
1109 id = nodeid;
1110
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001111 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 +01001112 return ((id - nodeid) - r) + 1;
1113 }
1114 id += r;
1115
1116 if ((is_relative && !start) || (!is_relative && !module)) {
1117 return -1;
1118 }
1119
1120 /* descendant-schema-nodeid */
1121 if (is_relative) {
Michal Vasko4c06fd82016-02-17 13:43:38 +01001122 module = start_mod = start->module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001123
1124 /* absolute-schema-nodeid */
1125 } else {
1126 start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001127 if (!start_mod) {
1128 return -1;
1129 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001130 start = start_mod->data;
1131 }
1132
1133 while (1) {
1134 sibling = NULL;
1135 while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod,
1136 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
1137 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001138 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001139 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1140 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001141 *ret = sibling;
1142 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001143 } else if (r == 1) {
1144 continue;
1145 } else if (r == 2) {
1146 break;
1147 } else {
1148 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001149 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001150 }
1151 }
1152
1153 /* no match */
1154 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001155 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001156 return EXIT_SUCCESS;
1157 }
1158
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001159 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 +01001160 return ((id - nodeid) - r) + 1;
1161 }
1162 id += r;
1163 }
1164
1165 /* cannot get here */
1166 LOGINT;
1167 return -1;
1168}
1169
Radek Krejcif3c71de2016-04-11 12:45:46 +02001170/* unique, refine,
1171 * >0 - unexpected char on position (ret - 1),
1172 * 0 - ok (but ret can still be NULL),
1173 * -1 - error,
1174 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001175int
1176resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001177 int check_shorthand, int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001178{
1179 const char *name, *mod_name, *id;
1180 const struct lys_node *sibling;
1181 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001182 int8_t shorthand = check_shorthand ? 0 : -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001183 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001184 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001185
1186 assert(nodeid && start && ret);
1187 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1188
1189 id = nodeid;
1190 module = start->module;
1191
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001192 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 +01001193 return ((id - nodeid) - r) + 1;
1194 }
1195 id += r;
1196
1197 if (!is_relative) {
1198 return -1;
1199 }
1200
1201 while (1) {
1202 sibling = NULL;
1203 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
1204 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
1205 /* name match */
1206 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001207 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1208 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001209 if (!(sibling->nodetype & ret_nodetype)) {
1210 /* wrong node type, too bad */
1211 continue;
1212 }
1213 *ret = sibling;
1214 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001215 } else if (r == 1) {
1216 continue;
1217 } else if (r == 2) {
1218 break;
1219 } else {
1220 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001221 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001222 }
1223 }
1224
1225 /* no match */
1226 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001227 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001228 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001229 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1230 *ret = NULL;
1231 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001232 }
1233
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001234 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 +01001235 return ((id - nodeid) - r) + 1;
1236 }
1237 id += r;
1238 }
1239
1240 /* cannot get here */
1241 LOGINT;
1242 return -1;
1243}
1244
1245/* choice default */
1246int
1247resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1248{
1249 /* cannot actually be a path */
1250 if (strchr(nodeid, '/')) {
1251 return -1;
1252 }
1253
Radek Krejcif3c71de2016-04-11 12:45:46 +02001254 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001255}
1256
1257/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1258static int
1259resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1260{
1261 const struct lys_module *module;
1262 const char *mod_prefix, *name;
1263 int i, mod_prefix_len, nam_len;
1264
1265 /* parse the identifier, it must be parsed on one call */
1266 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1267 return -i + 1;
1268 }
1269
1270 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1271 if (!module) {
1272 return -1;
1273 }
1274 if (module != start->module) {
1275 start = module->data;
1276 }
1277
1278 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1279
1280 return EXIT_SUCCESS;
1281}
1282
1283int
1284resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1285 const struct lys_node **ret)
1286{
1287 const char *name, *mod_name, *id;
1288 const struct lys_node *sibling, *start;
1289 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001290 int8_t shorthand = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001291 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001292
1293 assert(nodeid && module && ret);
1294 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1295
1296 id = nodeid;
1297 start = module->data;
1298
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001299 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 +01001300 return ((id - nodeid) - r) + 1;
1301 }
1302 id += r;
1303
1304 if (is_relative) {
1305 return -1;
1306 }
1307
1308 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001309 if (!abs_start_mod) {
1310 return -1;
1311 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001312
1313 while (1) {
1314 sibling = NULL;
1315 while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE
1316 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1317 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001318 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001319 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start);
1320 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001321 if (!(sibling->nodetype & ret_nodetype)) {
1322 /* wrong node type, too bad */
1323 continue;
1324 }
1325 *ret = sibling;
1326 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001327 } else if (r == 1) {
1328 continue;
1329 } else if (r == 2) {
1330 break;
1331 } else {
1332 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001333 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001334 }
1335 }
1336
1337 /* no match */
1338 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001339 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001340 return EXIT_SUCCESS;
1341 }
1342
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001343 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 +01001344 return ((id - nodeid) - r) + 1;
1345 }
1346 id += r;
1347 }
1348
1349 /* cannot get here */
1350 LOGINT;
1351 return -1;
1352}
1353
Michal Vaskoe733d682016-03-14 09:08:27 +01001354static int
Michal Vasko3547c532016-03-14 09:40:50 +01001355resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001356{
1357 const char *name;
1358 int nam_len, has_predicate, i;
1359
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001360 if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
1361 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001362 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001363 return -1;
1364 }
1365
1366 predicate += i;
1367 *parsed += i;
1368
1369 for (i = 0; i < list->keys_size; ++i) {
1370 if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) {
1371 break;
1372 }
1373 }
1374
1375 if (i == list->keys_size) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001376 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskoe733d682016-03-14 09:08:27 +01001377 return -1;
1378 }
1379
1380 /* more predicates? */
1381 if (has_predicate) {
Michal Vasko3547c532016-03-14 09:40:50 +01001382 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001383 }
1384
1385 return 0;
1386}
1387
Michal Vasko9fd98e22016-04-07 15:44:19 +02001388/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly
1389 * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */
Michal Vaskoe733d682016-03-14 09:08:27 +01001390const struct lys_node *
Michal Vasko9fd98e22016-04-07 15:44:19 +02001391resolve_json_schema_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001392{
Michal Vasko10728b52016-04-07 14:26:29 +02001393 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001394 const char *name, *mod_name, *id;
Michal Vasko3547c532016-03-14 09:40:50 +01001395 const struct lys_node *sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001396 int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001397 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskoe733d682016-03-14 09:08:27 +01001398 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001399
Michal Vasko3547c532016-03-14 09:40:50 +01001400 assert(nodeid && (ctx || start));
1401 if (!ctx) {
1402 ctx = start->module->ctx;
1403 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001404
1405 id = nodeid;
1406
Michal Vaskoe733d682016-03-14 09:08:27 +01001407 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 +01001408 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001409 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001410 }
1411 id += r;
1412
1413 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01001414 assert(start);
1415 start = start->child;
1416 if (!start) {
1417 /* no descendants, fail for sure */
Michal Vasko10728b52016-04-07 14:26:29 +02001418 str = strndup(nodeid, (name + nam_len) - nodeid);
1419 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1420 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001421 return NULL;
1422 }
1423 module = start->module;
1424 } else {
1425 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02001426 str = strndup(nodeid, (name + nam_len) - nodeid);
1427 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
1428 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001429 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02001430 } else if (mod_name_len > LY_BUF_SIZE - 1) {
1431 LOGINT;
1432 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01001433 }
1434
Michal Vasko971a3ca2016-04-01 13:09:29 +02001435 if (ly_buf_used && module_name[0]) {
1436 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1437 }
1438 ly_buf_used++;
1439
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001440 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001441 module_name[mod_name_len] = '\0';
1442 module = ly_ctx_get_module(ctx, module_name, NULL);
1443
1444 if (buf_backup) {
1445 /* return previous internal buffer content */
1446 strcpy(module_name, buf_backup);
1447 free(buf_backup);
1448 buf_backup = NULL;
1449 }
1450 ly_buf_used--;
1451
Michal Vasko3547c532016-03-14 09:40:50 +01001452 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02001453 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1454 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1455 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001456 return NULL;
1457 }
1458 start = module->data;
1459
1460 /* now it's as if there was no module name */
1461 mod_name = NULL;
1462 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01001463 }
1464
Michal Vaskoe733d682016-03-14 09:08:27 +01001465 prev_mod = module;
1466
Michal Vasko3edeaf72016-02-11 13:17:43 +01001467 while (1) {
1468 sibling = NULL;
Michal Vasko9fd98e22016-04-07 15:44:19 +02001469 while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ?
1470 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001471 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001472 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001473
1474 /* data RPC input/output check */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001475 if ((data_nodeid == 1) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_OUTPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001476 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001477 } else if ((data_nodeid == 2) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_INPUT)) {
Michal Vasko9fd98e22016-04-07 15:44:19 +02001478 continue;
1479 }
1480
Michal Vasko3edeaf72016-02-11 13:17:43 +01001481 /* module check */
1482 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001483 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01001484 LOGINT;
1485 return NULL;
1486 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001487
1488 if (ly_buf_used && module_name[0]) {
1489 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1490 }
1491 ly_buf_used++;
1492
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001493 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01001494 module_name[mod_name_len] = '\0';
1495 /* will also find an augment module */
1496 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001497
1498 if (buf_backup) {
1499 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001500 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001501 free(buf_backup);
1502 buf_backup = NULL;
1503 }
1504 ly_buf_used--;
1505
Michal Vasko3edeaf72016-02-11 13:17:43 +01001506 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001507 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1508 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1509 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001510 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001511 }
1512 } else {
1513 prefix_mod = prev_mod;
1514 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01001515 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001516 continue;
1517 }
1518
Michal Vaskoe733d682016-03-14 09:08:27 +01001519 /* do we have some predicates on it? */
1520 if (has_predicate) {
1521 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001522 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1523 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
1524 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
1525 return NULL;
1526 }
1527 } else if (sibling->nodetype == LYS_LIST) {
1528 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
1529 return NULL;
1530 }
1531 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01001532 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01001533 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01001534 }
1535 id += r;
1536 }
1537
Radek Krejcibdf92362016-04-08 14:43:34 +02001538 /* check for shorthand cases - then 'start' does not change */
Michal Vasko3c0f9f52016-05-17 16:38:10 +02001539 if (!data_nodeid && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001540 shorthand = ~shorthand;
1541 }
1542
Michal Vasko3edeaf72016-02-11 13:17:43 +01001543 /* the result node? */
1544 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001545 if (shorthand) {
1546 /* wrong path for shorthand */
Michal Vasko025e0452016-05-17 16:14:20 +02001547 str = strndup(nodeid, (name + nam_len) - nodeid);
1548 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko3c0f9f52016-05-17 16:38:10 +02001549 LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement.");
Radek Krejci9a5fccc2016-05-18 15:44:58 +02001550 free(str);
Michal Vasko025e0452016-05-17 16:14:20 +02001551 return NULL;
Radek Krejcibdf92362016-04-08 14:43:34 +02001552 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001553 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001554 }
1555
Michal Vasko3c0f9f52016-05-17 16:38:10 +02001556 if (data_nodeid || !shorthand) {
Michal Vasko7dc71d02016-03-15 10:42:28 +01001557 /* move down the tree, if possible */
1558 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001559 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko7dc71d02016-03-15 10:42:28 +01001560 return NULL;
1561 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001562 start = sibling->child;
1563 }
1564
1565 /* update prev mod */
1566 prev_mod = start->module;
1567 break;
1568 }
1569 }
1570
1571 /* no match */
1572 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02001573 str = strndup(nodeid, (name + nam_len) - nodeid);
1574 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
1575 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01001576 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001577 }
1578
Michal Vaskoe733d682016-03-14 09:08:27 +01001579 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 +01001580 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001581 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001582 }
1583 id += r;
1584 }
1585
1586 /* cannot get here */
1587 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01001588 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001589}
1590
Michal Vasko22448d32016-03-16 13:17:29 +01001591static int
1592resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed)
1593{
1594 const char *name, *value;
1595 int nam_len, val_len, has_predicate = 1, r;
1596 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02001597 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01001598
Radek Krejci61a86c62016-03-24 11:06:44 +01001599 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01001600 assert(node->schema->nodetype == LYS_LIST);
1601
Michal Vaskof29903d2016-04-18 13:13:10 +02001602 key = (struct lyd_node_leaf_list *)node->child;
1603 for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
1604 if (!key) {
1605 /* invalid data */
1606 LOGINT;
1607 return -1;
1608 }
Michal Vasko22448d32016-03-16 13:17:29 +01001609
Michal Vasko22448d32016-03-16 13:17:29 +01001610 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001611 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02001612 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001613 }
1614
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001615 if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
1616 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001617 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02001618 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001619 }
1620
1621 predicate += r;
1622 *parsed += r;
1623
Michal Vaskof29903d2016-04-18 13:13:10 +02001624 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001625 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02001626 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001627 }
1628
1629 /* value does not match */
Michal Vaskof29903d2016-04-18 13:13:10 +02001630 if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01001631 return 1;
1632 }
Michal Vaskof29903d2016-04-18 13:13:10 +02001633
1634 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01001635 }
1636
1637 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001638 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01001639 return -1;
1640 }
1641
1642 return 0;
1643}
1644
1645struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001646resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
1647 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01001648{
Michal Vasko10728b52016-04-07 14:26:29 +02001649 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko22448d32016-03-16 13:17:29 +01001650 const char *id, *mod_name, *name;
1651 int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001652 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001653 struct lyd_node_leaf_list *llist;
Michal Vasko22448d32016-03-16 13:17:29 +01001654 const struct lys_module *prefix_mod, *prev_mod;
1655 struct ly_ctx *ctx;
1656
1657 assert(nodeid && start && parsed);
1658
1659 ctx = start->schema->module->ctx;
1660 id = nodeid;
1661
1662 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 +01001663 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001664 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001665 return NULL;
1666 }
1667 id += r;
1668 /* add it to parsed only after the data node was actually found */
1669 last_parsed = r;
1670
1671 if (is_relative) {
1672 prev_mod = start->schema->module;
Michal Vasko22448d32016-03-16 13:17:29 +01001673 start = start->child;
1674 } else {
1675 for (; start->parent; start = start->parent);
Michal Vasko22448d32016-03-16 13:17:29 +01001676 prev_mod = start->schema->module;
1677 }
1678
1679 while (1) {
1680 LY_TREE_FOR(start, sibling) {
Michal Vasko2411b942016-03-23 13:50:03 +01001681 /* RPC data check, return simply invalid argument, because the data tree is invalid */
1682 if (lys_parent(sibling->schema)) {
1683 if (options & LYD_PATH_OPT_OUTPUT) {
1684 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02001685 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01001686 *parsed = -1;
1687 return NULL;
1688 }
1689 } else {
1690 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02001691 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01001692 *parsed = -1;
1693 return NULL;
1694 }
1695 }
1696 }
1697
Michal Vasko22448d32016-03-16 13:17:29 +01001698 /* name match */
1699 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
1700
1701 /* module check */
1702 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02001703 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01001704 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001705 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001706 return NULL;
1707 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02001708
1709 if (ly_buf_used && module_name[0]) {
1710 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
1711 }
1712 ly_buf_used++;
1713
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02001714 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01001715 module_name[mod_name_len] = '\0';
1716 /* will also find an augment module */
1717 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001718
1719 if (buf_backup) {
1720 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02001721 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02001722 free(buf_backup);
1723 buf_backup = NULL;
1724 }
1725 ly_buf_used--;
1726
Michal Vasko22448d32016-03-16 13:17:29 +01001727 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02001728 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
1729 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
1730 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001731 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001732 return NULL;
1733 }
1734 } else {
1735 prefix_mod = prev_mod;
1736 }
1737 if (prefix_mod != lys_node_module(sibling->schema)) {
1738 continue;
1739 }
1740
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001741 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01001742 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001743 llist = (struct lyd_node_leaf_list *)sibling;
1744 if ((!llist_value && llist->value_str && llist->value_str[0])
1745 || (llist_value && strcmp(llist_value, llist->value_str))) {
1746 continue;
1747 }
Michal Vasko22448d32016-03-16 13:17:29 +01001748 }
1749
Michal Vasko13eb4ac2016-04-06 12:19:37 +02001750 /* list, we need predicates'n'stuff then */
Michal Vasko22448d32016-03-16 13:17:29 +01001751 if (sibling->schema->nodetype == LYS_LIST) {
1752 r = 0;
1753 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001754 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001755 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001756 return NULL;
1757 }
1758 ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r);
1759 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01001760 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001761 return NULL;
1762 } else if (ret == 1) {
1763 /* this list instance does not match */
1764 continue;
1765 }
1766 id += r;
1767 last_parsed += r;
1768 }
1769
1770 *parsed += last_parsed;
1771
1772 /* the result node? */
1773 if (!id[0]) {
1774 return sibling;
1775 }
1776
1777 /* move down the tree, if possible */
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02001778 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001779 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001780 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001781 return NULL;
1782 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02001783 last_match = sibling;
Michal Vasko22448d32016-03-16 13:17:29 +01001784 start = sibling->child;
1785 if (start) {
1786 prev_mod = start->schema->module;
1787 }
1788 break;
1789 }
1790 }
1791
1792 /* no match, return last match */
1793 if (!sibling) {
1794 return last_match;
1795 }
1796
1797 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 +01001798 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01001799 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001800 return NULL;
1801 }
1802 id += r;
1803 last_parsed = r;
1804 }
1805
1806 /* cannot get here */
1807 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01001808 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01001809 return NULL;
1810}
1811
Michal Vasko3edeaf72016-02-11 13:17:43 +01001812/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02001813 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02001814 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02001815 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02001816 * @param[in] str_restr Restriction as a string.
1817 * @param[in] type Type of the restriction.
1818 * @param[out] ret Final interval structure that starts with
1819 * the interval of the initial type, continues with intervals
1820 * of any superior types derived from the initial one, and
1821 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02001822 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001823 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02001824 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001825int
Michal Vaskoaeb51802016-04-11 10:58:47 +02001826resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001827{
1828 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02001829 int kind;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001830 int64_t local_smin, local_smax;
1831 uint64_t local_umin, local_umax;
1832 long double local_fmin, local_fmax;
1833 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02001834 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001835
1836 switch (type->base) {
1837 case LY_TYPE_BINARY:
1838 kind = 0;
1839 local_umin = 0;
1840 local_umax = 18446744073709551615UL;
1841
1842 if (!str_restr && type->info.binary.length) {
1843 str_restr = type->info.binary.length->expr;
1844 }
1845 break;
1846 case LY_TYPE_DEC64:
1847 kind = 2;
1848 local_fmin = -9223372036854775808.0;
1849 local_fmin /= 1 << type->info.dec64.dig;
1850 local_fmax = 9223372036854775807.0;
1851 local_fmax /= 1 << type->info.dec64.dig;
1852
1853 if (!str_restr && type->info.dec64.range) {
1854 str_restr = type->info.dec64.range->expr;
1855 }
1856 break;
1857 case LY_TYPE_INT8:
1858 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001859 local_smin = __INT64_C(-128);
1860 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001861
1862 if (!str_restr && type->info.num.range) {
1863 str_restr = type->info.num.range->expr;
1864 }
1865 break;
1866 case LY_TYPE_INT16:
1867 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001868 local_smin = __INT64_C(-32768);
1869 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001870
1871 if (!str_restr && type->info.num.range) {
1872 str_restr = type->info.num.range->expr;
1873 }
1874 break;
1875 case LY_TYPE_INT32:
1876 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001877 local_smin = __INT64_C(-2147483648);
1878 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001879
1880 if (!str_restr && type->info.num.range) {
1881 str_restr = type->info.num.range->expr;
1882 }
1883 break;
1884 case LY_TYPE_INT64:
1885 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01001886 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
1887 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001888
1889 if (!str_restr && type->info.num.range) {
1890 str_restr = type->info.num.range->expr;
1891 }
1892 break;
1893 case LY_TYPE_UINT8:
1894 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001895 local_umin = __UINT64_C(0);
1896 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001897
1898 if (!str_restr && type->info.num.range) {
1899 str_restr = type->info.num.range->expr;
1900 }
1901 break;
1902 case LY_TYPE_UINT16:
1903 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001904 local_umin = __UINT64_C(0);
1905 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001906
1907 if (!str_restr && type->info.num.range) {
1908 str_restr = type->info.num.range->expr;
1909 }
1910 break;
1911 case LY_TYPE_UINT32:
1912 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001913 local_umin = __UINT64_C(0);
1914 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001915
1916 if (!str_restr && type->info.num.range) {
1917 str_restr = type->info.num.range->expr;
1918 }
1919 break;
1920 case LY_TYPE_UINT64:
1921 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001922 local_umin = __UINT64_C(0);
1923 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001924
1925 if (!str_restr && type->info.num.range) {
1926 str_restr = type->info.num.range->expr;
1927 }
1928 break;
1929 case LY_TYPE_STRING:
1930 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01001931 local_umin = __UINT64_C(0);
1932 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001933
1934 if (!str_restr && type->info.str.length) {
1935 str_restr = type->info.str.length->expr;
1936 }
1937 break;
1938 default:
1939 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001940 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001941 }
1942
1943 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02001944 if (type->der) {
1945 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02001946 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001947 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02001948 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001949 assert(!intv || (intv->kind == kind));
1950 }
1951
1952 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02001953 /* we do not have any restriction, return superior ones */
1954 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001955 return EXIT_SUCCESS;
1956 }
1957
1958 /* adjust local min and max */
1959 if (intv) {
1960 tmp_intv = intv;
1961
1962 if (kind == 0) {
1963 local_umin = tmp_intv->value.uval.min;
1964 } else if (kind == 1) {
1965 local_smin = tmp_intv->value.sval.min;
1966 } else if (kind == 2) {
1967 local_fmin = tmp_intv->value.fval.min;
1968 }
1969
1970 while (tmp_intv->next) {
1971 tmp_intv = tmp_intv->next;
1972 }
1973
1974 if (kind == 0) {
1975 local_umax = tmp_intv->value.uval.max;
1976 } else if (kind == 1) {
1977 local_smax = tmp_intv->value.sval.max;
1978 } else if (kind == 2) {
1979 local_fmax = tmp_intv->value.fval.max;
1980 }
1981 }
1982
1983 /* finally parse our restriction */
1984 seg_ptr = str_restr;
1985 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02001986 if (!tmp_local_intv) {
1987 assert(!local_intv);
1988 local_intv = malloc(sizeof *local_intv);
1989 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001990 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02001991 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001992 tmp_local_intv = tmp_local_intv->next;
1993 }
Michal Vasko253035f2015-12-17 16:58:13 +01001994 if (!tmp_local_intv) {
1995 LOGMEM;
Michal Vaskoaeb51802016-04-11 10:58:47 +02001996 goto error;
Michal Vasko253035f2015-12-17 16:58:13 +01001997 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02001998
1999 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002000 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002001 tmp_local_intv->next = NULL;
2002
2003 /* min */
2004 ptr = seg_ptr;
2005 while (isspace(ptr[0])) {
2006 ++ptr;
2007 }
2008 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2009 if (kind == 0) {
2010 tmp_local_intv->value.uval.min = atoll(ptr);
2011 } else if (kind == 1) {
2012 tmp_local_intv->value.sval.min = atoll(ptr);
2013 } else if (kind == 2) {
2014 tmp_local_intv->value.fval.min = atoll(ptr);
2015 }
2016
2017 if ((ptr[0] == '+') || (ptr[0] == '-')) {
2018 ++ptr;
2019 }
2020 while (isdigit(ptr[0])) {
2021 ++ptr;
2022 }
2023 } else if (!strncmp(ptr, "min", 3)) {
2024 if (kind == 0) {
2025 tmp_local_intv->value.uval.min = local_umin;
2026 } else if (kind == 1) {
2027 tmp_local_intv->value.sval.min = local_smin;
2028 } else if (kind == 2) {
2029 tmp_local_intv->value.fval.min = local_fmin;
2030 }
2031
2032 ptr += 3;
2033 } else if (!strncmp(ptr, "max", 3)) {
2034 if (kind == 0) {
2035 tmp_local_intv->value.uval.min = local_umax;
2036 } else if (kind == 1) {
2037 tmp_local_intv->value.sval.min = local_smax;
2038 } else if (kind == 2) {
2039 tmp_local_intv->value.fval.min = local_fmax;
2040 }
2041
2042 ptr += 3;
2043 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002044 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002045 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002046 }
2047
2048 while (isspace(ptr[0])) {
2049 ptr++;
2050 }
2051
2052 /* no interval or interval */
2053 if ((ptr[0] == '|') || !ptr[0]) {
2054 if (kind == 0) {
2055 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2056 } else if (kind == 1) {
2057 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2058 } else if (kind == 2) {
2059 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2060 }
2061 } else if (!strncmp(ptr, "..", 2)) {
2062 /* skip ".." */
2063 ptr += 2;
2064 while (isspace(ptr[0])) {
2065 ++ptr;
2066 }
2067
2068 /* max */
2069 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2070 if (kind == 0) {
2071 tmp_local_intv->value.uval.max = atoll(ptr);
2072 } else if (kind == 1) {
2073 tmp_local_intv->value.sval.max = atoll(ptr);
2074 } else if (kind == 2) {
2075 tmp_local_intv->value.fval.max = atoll(ptr);
2076 }
2077 } else if (!strncmp(ptr, "max", 3)) {
2078 if (kind == 0) {
2079 tmp_local_intv->value.uval.max = local_umax;
2080 } else if (kind == 1) {
2081 tmp_local_intv->value.sval.max = local_smax;
2082 } else if (kind == 2) {
2083 tmp_local_intv->value.fval.max = local_fmax;
2084 }
2085 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002086 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002087 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002088 }
2089 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002090 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002091 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002092 }
2093
2094 /* next segment (next OR) */
2095 seg_ptr = strchr(seg_ptr, '|');
2096 if (!seg_ptr) {
2097 break;
2098 }
2099 seg_ptr++;
2100 }
2101
2102 /* check local restrictions against superior ones */
2103 if (intv) {
2104 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002105 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002106
2107 while (tmp_local_intv && tmp_intv) {
2108 /* reuse local variables */
2109 if (kind == 0) {
2110 local_umin = tmp_local_intv->value.uval.min;
2111 local_umax = tmp_local_intv->value.uval.max;
2112
2113 /* it must be in this interval */
2114 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2115 /* this interval is covered, next one */
2116 if (local_umax <= tmp_intv->value.uval.max) {
2117 tmp_local_intv = tmp_local_intv->next;
2118 continue;
2119 /* ascending order of restrictions -> fail */
2120 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002121 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002122 }
2123 }
2124 } else if (kind == 1) {
2125 local_smin = tmp_local_intv->value.sval.min;
2126 local_smax = tmp_local_intv->value.sval.max;
2127
2128 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2129 if (local_smax <= tmp_intv->value.sval.max) {
2130 tmp_local_intv = tmp_local_intv->next;
2131 continue;
2132 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002133 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002134 }
2135 }
2136 } else if (kind == 2) {
2137 local_fmin = tmp_local_intv->value.fval.min;
2138 local_fmax = tmp_local_intv->value.fval.max;
2139
2140 if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) {
2141 if (local_fmax <= tmp_intv->value.fval.max) {
2142 tmp_local_intv = tmp_local_intv->next;
2143 continue;
2144 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002145 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002146 }
2147 }
2148 }
2149
2150 tmp_intv = tmp_intv->next;
2151 }
2152
2153 /* some interval left uncovered -> fail */
2154 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002155 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002156 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002157 }
2158
Michal Vaskoaeb51802016-04-11 10:58:47 +02002159 /* append the local intervals to all the intervals of the superior types, return it all */
2160 if (intv) {
2161 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2162 tmp_intv->next = local_intv;
2163 } else {
2164 intv = local_intv;
2165 }
2166 *ret = intv;
2167
2168 return EXIT_SUCCESS;
2169
2170error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002171 while (intv) {
2172 tmp_intv = intv->next;
2173 free(intv);
2174 intv = tmp_intv;
2175 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02002176 while (local_intv) {
2177 tmp_local_intv = local_intv->next;
2178 free(local_intv);
2179 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002180 }
2181
Michal Vaskoaeb51802016-04-11 10:58:47 +02002182 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002183}
2184
Michal Vasko730dfdf2015-08-11 14:48:05 +02002185/**
Michal Vasko88c29542015-11-27 14:57:53 +01002186 * @brief Resolve a typedef, return only resolved typedefs if derived. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002187 *
2188 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002189 * @param[in] mod_name Typedef name module name.
2190 * @param[in] module Main module.
2191 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002192 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002193 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002194 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002195 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002196int
Michal Vasko1e62a092015-12-01 12:27:20 +01002197resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2198 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002199{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002200 int i, j;
Radek Krejci1574a8d2015-08-03 14:16:52 +02002201 struct lys_tpdf *tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002202 int tpdf_size;
2203
Michal Vasko1dca6882015-10-22 14:29:42 +02002204 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002205 /* no prefix, try built-in types */
2206 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
2207 if (!strcmp(ly_types[i].def->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002208 if (ret) {
2209 *ret = ly_types[i].def;
2210 }
2211 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002212 }
2213 }
2214 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002215 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002216 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002217 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002218 }
2219 }
2220
Michal Vasko1dca6882015-10-22 14:29:42 +02002221 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002222 /* search in local typedefs */
2223 while (parent) {
2224 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002225 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002226 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2227 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002228 break;
2229
Radek Krejci76512572015-08-04 09:47:08 +02002230 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002231 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2232 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002233 break;
2234
Radek Krejci76512572015-08-04 09:47:08 +02002235 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002236 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2237 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002238 break;
2239
Radek Krejci76512572015-08-04 09:47:08 +02002240 case LYS_RPC:
Radek Krejcib8048692015-08-05 13:36:34 +02002241 tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size;
2242 tpdf = ((struct lys_node_rpc *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002243 break;
2244
Radek Krejci76512572015-08-04 09:47:08 +02002245 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002246 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2247 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002248 break;
2249
Radek Krejci76512572015-08-04 09:47:08 +02002250 case LYS_INPUT:
2251 case LYS_OUTPUT:
Radek Krejci4608ada2015-08-05 16:04:37 +02002252 tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size;
2253 tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002254 break;
2255
2256 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02002257 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002258 continue;
2259 }
2260
2261 for (i = 0; i < tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002262 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002263 if (ret) {
2264 *ret = &tpdf[i];
2265 }
2266 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002267 }
2268 }
2269
Michal Vaskodcf98e62016-05-05 17:53:53 +02002270 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002271 }
Radek Krejcic071c542016-01-27 14:57:51 +01002272 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002273 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002274 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002275 if (!module) {
2276 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002277 }
2278 }
2279
2280 /* search in top level typedefs */
2281 for (i = 0; i < module->tpdf_size; i++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002282 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002283 if (ret) {
2284 *ret = &module->tpdf[i];
2285 }
2286 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002287 }
2288 }
2289
2290 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01002291 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002292 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Michal Vasko88c29542015-11-27 14:57:53 +01002293 if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002294 if (ret) {
2295 *ret = &module->inc[i].submodule->tpdf[j];
2296 }
2297 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002298 }
2299 }
2300 }
2301
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002302 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002303}
2304
Michal Vasko1dca6882015-10-22 14:29:42 +02002305/**
2306 * @brief Check the default \p value of the \p type. Logs directly.
2307 *
2308 * @param[in] type Type definition to use.
2309 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01002310 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02002311 *
2312 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
2313 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002314static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002315check_default(struct lys_type *type, const char *value, struct lys_module *module)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002316{
Michal Vasko1dca6882015-10-22 14:29:42 +02002317 struct lyd_node_leaf_list node;
Radek Krejci37b756f2016-01-18 10:15:03 +01002318 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02002319
2320 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01002321 memset(&node, 0, sizeof node);
Michal Vasko1dca6882015-10-22 14:29:42 +02002322 node.value_str = value;
2323 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01002324 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko253035f2015-12-17 16:58:13 +01002325 if (!node.schema) {
2326 LOGMEM;
2327 return -1;
2328 }
Michal Vasko1dca6882015-10-22 14:29:42 +02002329 node.schema->name = strdup("default");
Michal Vasko253035f2015-12-17 16:58:13 +01002330 if (!node.schema->name) {
2331 LOGMEM;
2332 return -1;
2333 }
Michal Vasko56826402016-03-02 11:11:37 +01002334 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01002335 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02002336
Radek Krejci37b756f2016-01-18 10:15:03 +01002337 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02002338 if (!type->info.lref.target) {
2339 ret = EXIT_FAILURE;
2340 goto finish;
2341 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002342 ret = check_default(&type->info.lref.target->type, value, module);
Michal Vasko1dca6882015-10-22 14:29:42 +02002343
2344 } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) {
2345 /* it was converted to JSON format before, nothing else sensible we can do */
2346
2347 } else {
Radek Krejci0b7704f2016-03-18 12:16:14 +01002348 ret = lyp_parse_value(&node, NULL, 1);
Michal Vasko1dca6882015-10-22 14:29:42 +02002349 }
2350
2351finish:
2352 if (node.value_type == LY_TYPE_BITS) {
2353 free(node.value.bit);
2354 }
2355 free((char *)node.schema->name);
2356 free(node.schema);
2357
2358 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002359}
2360
Michal Vasko730dfdf2015-08-11 14:48:05 +02002361/**
2362 * @brief Check a key for mandatory attributes. Logs directly.
2363 *
2364 * @param[in] key The key to check.
2365 * @param[in] flags What flags to check.
2366 * @param[in] list The list of all the keys.
2367 * @param[in] index Index of the key in the key list.
2368 * @param[in] name The name of the keys.
2369 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002370 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002371 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002372 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002373static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002374check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002375{
Radek Krejciadb57612016-02-16 13:34:34 +01002376 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002377 char *dup = NULL;
2378 int j;
2379
2380 /* existence */
2381 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02002382 if (name[len] != '\0') {
2383 dup = strdup(name);
Michal Vasko253035f2015-12-17 16:58:13 +01002384 if (!dup) {
2385 LOGMEM;
2386 return -1;
2387 }
Michal Vaskof02e3742015-08-05 16:27:02 +02002388 dup[len] = '\0';
2389 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002390 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002391 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002392 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002393 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002394 }
2395
2396 /* uniqueness */
2397 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01002398 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002399 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002400 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002401 }
2402 }
2403
2404 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02002405 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002406 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002407 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002408 }
2409
2410 /* type of the leaf is not built-in empty */
2411 if (key->type.base == LY_TYPE_EMPTY) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002412 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002413 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002414 }
2415
2416 /* config attribute is the same as of the list */
Radek Krejciadb57612016-02-16 13:34:34 +01002417 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002418 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002419 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002420 }
2421
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002422 /* key is not placed from augment */
2423 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002424 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
2425 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01002426 return -1;
2427 }
2428
Michal Vaskocca47842016-03-17 10:31:07 +01002429 /* key is not when-conditional */
2430 if (key->when) {
Radek Krejci02a04992016-03-17 16:06:37 +01002431 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, "when", "leaf");
2432 LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"when\" condition.");
Radek Krejci581ce772015-11-10 17:22:40 +01002433 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02002434 }
2435
Michal Vasko0b85aa82016-03-07 14:37:43 +01002436 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02002437}
Michal Vasko730dfdf2015-08-11 14:48:05 +02002438
2439/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002440 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002441 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002442 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01002443 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002444 *
2445 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
2446 */
2447int
Radek Krejci48464ed2016-03-17 15:44:09 +01002448resolve_unique(struct lys_node *parent, const char *uniq_str_path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002449{
Radek Krejci581ce772015-11-10 17:22:40 +01002450 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01002451 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002452
Radek Krejcif3c71de2016-04-11 12:45:46 +02002453 rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01002454 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01002455 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002456 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002457 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002458 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02002459 } else if (rc == -2) {
Michal Vaskoc66c6d82016-04-12 11:37:31 +02002460 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01002461 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01002462 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01002463 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002464 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
2465 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01002466 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002467 }
Radek Krejci581ce772015-11-10 17:22:40 +01002468 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002469 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01002470 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002471 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
2472 LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf.");
Radek Krejci581ce772015-11-10 17:22:40 +01002473 rc = -1;
2474 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002475 }
2476
Radek Krejcicf509982015-12-15 09:22:44 +01002477 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01002478 if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01002479 return -1;
2480 }
2481
Radek Krejcica7efb72016-01-18 13:06:01 +01002482 /* set leaf's unique flag */
2483 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
2484
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002485 return EXIT_SUCCESS;
2486
2487error:
2488
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002489 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002490}
2491
Michal Vasko730dfdf2015-08-11 14:48:05 +02002492/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002493 * @brief Resolve (find) a feature definition. Logs directly.
2494 *
2495 * @param[in] name Feature name.
2496 * @param[in] module Module to search in.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002497 * @param[out] ret Pointer to the resolved feature. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002498 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002499 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002500 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002501static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002502resolve_feature(const char *id, const struct lys_module *module, struct lys_feature **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002503{
Michal Vasko2d851a92015-10-20 16:16:36 +02002504 const char *mod_name, *name;
Michal Vasko2d851a92015-10-20 16:16:36 +02002505 int mod_name_len, nam_len, i, j;
Radek Krejcicf509982015-12-15 09:22:44 +01002506 struct lys_node *node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002507
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002508 assert(id);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002509 assert(module);
2510
2511 /* check prefix */
Michal Vasko2d851a92015-10-20 16:16:36 +02002512 if ((i = parse_node_identifier(id, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002513 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002514 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002515 }
2516
Radek Krejcic071c542016-01-27 14:57:51 +01002517 module = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
2518 if (!module) {
2519 /* identity refers unknown data model */
Radek Krejci48464ed2016-03-17 15:44:09 +01002520 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Radek Krejcic071c542016-01-27 14:57:51 +01002521 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002522 }
2523
Radek Krejcic071c542016-01-27 14:57:51 +01002524 /* search in the identified module ... */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002525 for (j = 0; j < module->features_size; j++) {
2526 if (!strcmp(name, module->features[j].name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002527 if (ret) {
Radek Krejcicf509982015-12-15 09:22:44 +01002528 /* check status */
2529 node = (struct lys_node *)*ret;
Radek Krejcic6556022016-01-27 15:16:45 +01002530 if (lyp_check_status(node->flags, node->module, node->name, module->features[j].flags,
Radek Krejci48464ed2016-03-17 15:44:09 +01002531 module->features[j].module, module->features[j].name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01002532 return -1;
2533 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002534 *ret = &module->features[j];
2535 }
2536 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002537 }
2538 }
Radek Krejcic071c542016-01-27 14:57:51 +01002539 /* ... and all its submodules */
Michal Vasko27ab8222016-02-12 09:33:52 +01002540 for (i = 0; i < module->inc_size; i++) {
2541 if (!module->inc[i].submodule) {
2542 /* not yet resolved */
2543 continue;
2544 }
Radek Krejcic071c542016-01-27 14:57:51 +01002545 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
2546 if (!strcmp(name, module->inc[i].submodule->features[j].name)) {
2547 if (ret) {
2548 /* check status */
2549 node = (struct lys_node *)*ret;
Radek Krejcic6556022016-01-27 15:16:45 +01002550 if (lyp_check_status(node->flags, node->module, node->name,
Radek Krejcic071c542016-01-27 14:57:51 +01002551 module->inc[i].submodule->features[j].flags,
2552 module->inc[i].submodule->features[j].module,
Radek Krejci48464ed2016-03-17 15:44:09 +01002553 module->inc[i].submodule->features[j].name, node)) {
Radek Krejcic071c542016-01-27 14:57:51 +01002554 return -1;
2555 }
2556 *ret = &(module->inc[i].submodule->features[j]);
2557 }
2558 return EXIT_SUCCESS;
2559 }
2560 }
2561 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002562
2563 /* not found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002564 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", id);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002565 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002566}
2567
Radek Krejci0c0086a2016-03-24 15:20:28 +01002568void
Michal Vasko23b61ec2015-08-19 11:19:50 +02002569unres_data_del(struct unres_data *unres, uint32_t i)
2570{
2571 /* there are items after the one deleted */
2572 if (i+1 < unres->count) {
2573 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02002574 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002575
2576 /* deleting the last item */
2577 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02002578 free(unres->node);
2579 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002580 }
2581
2582 /* if there are no items after and it is not the last one, just move the counter */
2583 --unres->count;
2584}
2585
Michal Vasko0491ab32015-08-19 14:28:29 +02002586/**
2587 * @brief Resolve (find) a data node from a specific module. Does not log.
2588 *
2589 * @param[in] mod Module to search in.
2590 * @param[in] name Name of the data node.
2591 * @param[in] nam_len Length of the name.
2592 * @param[in] start Data node to start the search from.
2593 * @param[in,out] parents Resolved nodes. If there are some parents,
2594 * they are replaced (!!) with the resolvents.
2595 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02002596 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02002597 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002598static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002599resolve_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 +02002600{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002601 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02002602 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002603 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002604
Michal Vasko23b61ec2015-08-19 11:19:50 +02002605 if (!parents->count) {
2606 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002607 parents->node = malloc(sizeof *parents->node);
Michal Vasko253035f2015-12-17 16:58:13 +01002608 if (!parents->node) {
2609 LOGMEM;
Michal Vasko2471e7f2016-04-11 11:00:15 +02002610 return -1;
Michal Vasko253035f2015-12-17 16:58:13 +01002611 }
Michal Vaskocf024702015-10-08 15:01:42 +02002612 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002613 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002614 for (i = 0; i < parents->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02002615 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002616 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002617 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002618 continue;
2619 }
2620 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02002621 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002622 if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len)
2623 && node->schema->name[nam_len] == '\0') {
2624 /* matching target */
2625 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02002626 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02002627 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002628 flag = 1;
2629 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02002630 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002631 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01002632 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
2633 if (!parents->node) {
2634 return EXIT_FAILURE;
2635 }
Michal Vaskocf024702015-10-08 15:01:42 +02002636 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002637 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002638 }
2639 }
2640 }
Radek Krejcic5090c32015-08-12 09:46:19 +02002641
2642 if (!flag) {
2643 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002644 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02002645 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002646 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02002647 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002648 }
2649
Michal Vasko0491ab32015-08-19 14:28:29 +02002650 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02002651}
2652
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002653/**
2654 * @brief Resolve (find) a data node. Does not log.
2655 *
Radek Krejci581ce772015-11-10 17:22:40 +01002656 * @param[in] mod_name Module name of the data node.
2657 * @param[in] mod_name_len Length of the module name.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002658 * @param[in] name Name of the data node.
2659 * @param[in] nam_len Length of the name.
2660 * @param[in] start Data node to start the search from.
2661 * @param[in,out] parents Resolved nodes. If there are some parents,
2662 * they are replaced (!!) with the resolvents.
2663 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002664 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002665 */
Radek Krejcic5090c32015-08-12 09:46:19 +02002666static int
Radek Krejci581ce772015-11-10 17:22:40 +01002667resolve_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 +02002668 struct unres_data *parents)
Radek Krejcic5090c32015-08-12 09:46:19 +02002669{
Michal Vasko1e62a092015-12-01 12:27:20 +01002670 const struct lys_module *mod;
Michal Vasko31fc3672015-10-21 12:08:13 +02002671 char *str;
Radek Krejcic5090c32015-08-12 09:46:19 +02002672
Michal Vasko23b61ec2015-08-19 11:19:50 +02002673 assert(start);
2674
Michal Vasko31fc3672015-10-21 12:08:13 +02002675 if (mod_name) {
2676 /* we have mod_name, find appropriate module */
2677 str = strndup(mod_name, mod_name_len);
Michal Vasko253035f2015-12-17 16:58:13 +01002678 if (!str) {
2679 LOGMEM;
2680 return -1;
2681 }
Michal Vasko31fc3672015-10-21 12:08:13 +02002682 mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL);
2683 free(str);
Radek Krejcic5090c32015-08-12 09:46:19 +02002684 if (!mod) {
2685 /* invalid prefix */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002686 return -1;
Radek Krejcic5090c32015-08-12 09:46:19 +02002687 }
2688 } else {
2689 /* no prefix, module is the same as of current node */
2690 mod = start->schema->module;
2691 }
2692
2693 return resolve_data(mod, name, name_len, start, parents);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002694}
2695
Michal Vasko730dfdf2015-08-11 14:48:05 +02002696/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002697 * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly
Radek Krejci48464ed2016-03-17 15:44:09 +01002698 * only specific errors, general no-resolvent error is left to the caller.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002699 *
Michal Vaskobb211122015-08-19 14:03:11 +02002700 * @param[in] pred Predicate to use.
Radek Krejciadb57612016-02-16 13:34:34 +01002701 * @param[in] node Node from which the predicate is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02002702 * @param[in,out] node_match Nodes satisfying the restriction
2703 * without the predicate. Nodes not
2704 * satisfying the predicate are removed.
Michal Vasko0491ab32015-08-19 14:28:29 +02002705 * @param[out] parsed Number of characters parsed, negative on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002706 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002707 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002708 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002709static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002710resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match,
Radek Krejci010e54b2016-03-15 09:40:34 +01002711 int *parsed)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002712{
Michal Vasko730dfdf2015-08-11 14:48:05 +02002713 /* ... /node[source = destination] ... */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002714 struct unres_data source_match, dest_match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002715 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vasko0491ab32015-08-19 14:28:29 +02002716 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0;
2717 int has_predicate, dest_parent_times, i, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002718 uint32_t j;
2719
2720 source_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002721 source_match.node = malloc(sizeof *source_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01002722 if (!source_match.node) {
2723 LOGMEM;
2724 return -1;
2725 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002726 dest_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002727 dest_match.node = malloc(sizeof *dest_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01002728 if (!dest_match.node) {
2729 LOGMEM;
2730 return -1;
2731 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002732
2733 do {
2734 if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
2735 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002736 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002737 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002738 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002739 }
Michal Vasko0491ab32015-08-19 14:28:29 +02002740 parsed_loc += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002741 pred += i;
2742
Michal Vasko23b61ec2015-08-19 11:19:50 +02002743 for (j = 0; j < node_match->count;) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002744 /* source */
Michal Vaskocf024702015-10-08 15:01:42 +02002745 source_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02002746
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002747 /* must be leaf (key of a list) */
Radek Krejci581ce772015-11-10 17:22:40 +01002748 if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j],
Michal Vaskocf024702015-10-08 15:01:42 +02002749 &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002750 i = 0;
2751 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002752 }
2753
2754 /* destination */
Michal Vaskocf024702015-10-08 15:01:42 +02002755 dest_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02002756 dest_parent_times = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002757 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
2758 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002759 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002760 rc = -1;
2761 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002762 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02002763 pke_parsed = i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002764 for (i = 0; i < dest_parent_times; ++i) {
Michal Vaskocf024702015-10-08 15:01:42 +02002765 dest_match.node[0] = dest_match.node[0]->parent;
2766 if (!dest_match.node[0]) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002767 i = 0;
Michal Vasko0491ab32015-08-19 14:28:29 +02002768 rc = EXIT_FAILURE;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002769 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002770 }
2771 }
2772 while (1) {
Radek Krejci581ce772015-11-10 17:22:40 +01002773 if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0],
Michal Vasko0491ab32015-08-19 14:28:29 +02002774 &dest_match)) || (dest_match.count != 1)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02002775 i = 0;
2776 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002777 }
2778
2779 if (pke_len == pke_parsed) {
2780 break;
2781 }
Michal Vasko1f76a282015-08-04 16:16:53 +02002782 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 +02002783 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002784 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002785 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002786 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002787 }
2788 pke_parsed += i;
2789 }
2790
2791 /* check match between source and destination nodes */
Michal Vaskocf024702015-10-08 15:01:42 +02002792 if (((struct lys_node_leaf *)source_match.node[0]->schema)->type.base
2793 != ((struct lys_node_leaf *)dest_match.node[0]->schema)->type.base) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002794 goto remove_leafref;
2795 }
2796
Radek Krejcic1ffa4d2016-02-17 13:11:11 +01002797 if (!ly_strequal(((struct lyd_node_leaf_list *)source_match.node[0])->value_str,
Radek Krejci749190d2016-02-18 16:26:25 +01002798 ((struct lyd_node_leaf_list *)dest_match.node[0])->value_str, 1)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002799 goto remove_leafref;
2800 }
2801
2802 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002803 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002804 continue;
2805
2806remove_leafref:
2807 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002808 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002809 }
2810 } while (has_predicate);
2811
Michal Vaskocf024702015-10-08 15:01:42 +02002812 free(source_match.node);
2813 free(dest_match.node);
Michal Vasko0491ab32015-08-19 14:28:29 +02002814 if (parsed) {
2815 *parsed = parsed_loc;
2816 }
2817 return EXIT_SUCCESS;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002818
2819error:
2820
2821 if (source_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02002822 free(source_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002823 }
2824 if (dest_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02002825 free(dest_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002826 }
Michal Vasko0491ab32015-08-19 14:28:29 +02002827 if (parsed) {
2828 *parsed = -parsed_loc+i;
2829 }
2830 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002831}
2832
Michal Vasko730dfdf2015-08-11 14:48:05 +02002833/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002834 * @brief Resolve a path (leafref) in JSON data context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002835 *
Michal Vaskocf024702015-10-08 15:01:42 +02002836 * @param[in] node Leafref data node.
Michal Vasko23b61ec2015-08-19 11:19:50 +02002837 * @param[in] path Path of the leafref.
Radek Krejci48464ed2016-03-17 15:44:09 +01002838 * @param[out] ret Matching nodes. Expects an empty, but allocated structure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002839 *
Michal Vasko0491ab32015-08-19 14:28:29 +02002840 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002841 */
Michal Vasko184521f2015-09-24 13:14:26 +02002842static int
Radek Krejci48464ed2016-03-17 15:44:09 +01002843resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002844{
Radek Krejci71b795b2015-08-10 16:20:39 +02002845 struct lyd_node *data = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002846 const char *prefix, *name;
Michal Vasko0491ab32015-08-19 14:28:29 +02002847 int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002848 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002849
Michal Vaskocf024702015-10-08 15:01:42 +02002850 assert(node && path && ret && !ret->count);
Michal Vasko23b61ec2015-08-19 11:19:50 +02002851
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002852 parent_times = 0;
Michal Vaskod9173342015-08-17 14:35:36 +02002853 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002854
2855 /* searching for nodeset */
2856 do {
2857 if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002858 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02002859 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002860 goto error;
2861 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002862 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02002863 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002864
Michal Vasko23b61ec2015-08-19 11:19:50 +02002865 if (!ret->count) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02002866 if (parent_times != -1) {
2867 ret->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02002868 ret->node = calloc(1, sizeof *ret->node);
Michal Vasko253035f2015-12-17 16:58:13 +01002869 if (!ret->node) {
2870 LOGMEM;
Radek Krejci50501732016-01-07 13:06:39 +01002871 rc = -1;
Michal Vasko253035f2015-12-17 16:58:13 +01002872 goto error;
2873 }
Michal Vasko8bcdf292015-08-19 14:04:43 +02002874 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002875 for (i = 0; i < parent_times; ++i) {
2876 /* relative path */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002877 if (!ret->count) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002878 /* error, too many .. */
Radek Krejci48464ed2016-03-17 15:44:09 +01002879 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name);
Michal Vasko0491ab32015-08-19 14:28:29 +02002880 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002881 goto error;
Michal Vaskocf024702015-10-08 15:01:42 +02002882 } else if (!ret->node[0]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002883 /* first .. */
Michal Vaskocf024702015-10-08 15:01:42 +02002884 data = ret->node[0] = node->parent;
2885 } else if (!ret->node[0]->parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002886 /* we are in root */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002887 ret->count = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02002888 free(ret->node);
2889 ret->node = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002890 } else {
2891 /* multiple .. */
Michal Vaskocf024702015-10-08 15:01:42 +02002892 data = ret->node[0] = ret->node[0]->parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002893 }
2894 }
2895
2896 /* absolute path */
2897 if (parent_times == -1) {
Michal Vaskocf024702015-10-08 15:01:42 +02002898 for (data = node; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02002899 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko8bcdf292015-08-19 14:04:43 +02002900 if (data->prev) {
2901 for (; data->prev->next; data = data->prev);
2902 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002903 }
2904 }
2905
2906 /* node identifier */
Radek Krejci581ce772015-11-10 17:22:40 +01002907 if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01002908 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002909 LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name);
Michal Vasko184521f2015-09-24 13:14:26 +02002910 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002911 goto error;
2912 }
2913
2914 if (has_predicate) {
2915 /* we have predicate, so the current results must be lists */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002916 for (j = 0; j < ret->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02002917 if (ret->node[j]->schema->nodetype == LYS_LIST &&
2918 ((struct lys_node_list *)ret->node[0]->schema)->keys) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002919 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002920 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002921 continue;
2922 }
2923
2924 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02002925 unres_data_del(ret, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002926 }
Radek Krejci48464ed2016-03-17 15:44:09 +01002927 if ((rc = resolve_path_predicate_data(path, node, ret, &i))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01002928 if (rc == -1) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02002929 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path);
Michal Vasko184521f2015-09-24 13:14:26 +02002930 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002931 goto error;
2932 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002933 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02002934 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002935
Michal Vasko23b61ec2015-08-19 11:19:50 +02002936 if (!ret->count) {
Michal Vasko0491ab32015-08-19 14:28:29 +02002937 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002938 goto error;
2939 }
2940 }
2941 } while (path[0] != '\0');
2942
Michal Vaskof02e3742015-08-05 16:27:02 +02002943 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002944
2945error:
2946
Michal Vaskocf024702015-10-08 15:01:42 +02002947 free(ret->node);
2948 ret->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02002949 ret->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002950
Michal Vasko0491ab32015-08-19 14:28:29 +02002951 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002952}
2953
Michal Vasko730dfdf2015-08-11 14:48:05 +02002954/**
Michal Vaskof39142b2015-10-21 11:40:05 +02002955 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002956 *
Michal Vaskobb211122015-08-19 14:03:11 +02002957 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01002958 * @param[in] context_node Predicate context node (where the predicate is placed).
2959 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vasko730dfdf2015-08-11 14:48:05 +02002960 *
Michal Vasko184521f2015-09-24 13:14:26 +02002961 * @return 0 on forward reference, otherwise the number
2962 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002963 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002964 */
Michal Vasko1f76a282015-08-04 16:16:53 +02002965static int
Radek Krejciadb57612016-02-16 13:34:34 +01002966resolve_path_predicate_schema(const char *path, const struct lys_node *context_node,
Radek Krejci48464ed2016-03-17 15:44:09 +01002967 struct lys_node *parent)
Michal Vasko1f76a282015-08-04 16:16:53 +02002968{
Michal Vasko1e62a092015-12-01 12:27:20 +01002969 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02002970 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
2971 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 +02002972 int has_predicate, dest_parent_times = 0, i, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02002973
2974 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02002975 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02002976 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002977 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02002978 return -parsed+i;
2979 }
2980 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02002981 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02002982
Michal Vasko58090902015-08-13 14:04:15 +02002983 /* source (must be leaf) */
Michal Vasko36cbaa42015-12-14 13:15:48 +01002984 if (!sour_pref) {
Radek Krejciadb57612016-02-16 13:34:34 +01002985 sour_pref = context_node->module->name;
Michal Vasko36cbaa42015-12-14 13:15:48 +01002986 }
Radek Krejciadb57612016-02-16 13:34:34 +01002987 rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02002988 LYS_LEAF | LYS_AUGMENT, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002989 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02002990 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02002991 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02002992 }
2993
2994 /* destination */
2995 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
2996 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002997 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 +02002998 return -parsed;
2999 }
3000 pke_parsed += i;
3001
Radek Krejciadb57612016-02-16 13:34:34 +01003002 /* parent is actually the parent of this leaf, so skip the first ".." */
3003 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003004 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003005 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003006 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003007 }
Michal Vaskodcf98e62016-05-05 17:53:53 +02003008 dst_node = lys_parent(dst_node);
Michal Vasko1f76a282015-08-04 16:16:53 +02003009 }
3010 while (1) {
Michal Vasko36cbaa42015-12-14 13:15:48 +01003011 if (!dest_pref) {
3012 dest_pref = dst_node->module->name;
3013 }
3014 rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len,
Michal Vasko165dc4a2015-10-23 09:44:27 +02003015 LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003016 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003017 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003018 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003019 }
3020
3021 if (pke_len == pke_parsed) {
3022 break;
3023 }
3024
3025 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
3026 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003027 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Radek Krejciadb57612016-02-16 13:34:34 +01003028 (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003029 return -parsed;
3030 }
3031 pke_parsed += i;
3032 }
3033
3034 /* check source - dest match */
Michal Vasko184521f2015-09-24 13:14:26 +02003035 if (dst_node->nodetype != LYS_LEAF) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003036 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Radek Krejci48464ed2016-03-17 15:44:09 +01003037 LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
3038 "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003039 return -parsed;
3040 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003041 } while (has_predicate);
3042
3043 return parsed;
3044}
3045
Michal Vasko730dfdf2015-08-11 14:48:05 +02003046/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003047 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003048 *
Michal Vaskobb211122015-08-19 14:03:11 +02003049 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003050 * @param[in] parent_node Parent of the leafref.
Radek Krejci2f12f852016-01-08 12:59:57 +01003051 * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path
3052 * has to contain absolute path
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003053 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003054 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003055 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003056 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003057static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003058resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf,
Michal Vasko36cbaa42015-12-14 13:15:48 +01003059 const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003060{
Michal Vasko1e62a092015-12-01 12:27:20 +01003061 const struct lys_node *node;
Radek Krejcic071c542016-01-27 14:57:51 +01003062 const struct lys_module *mod;
Michal Vasko1f76a282015-08-04 16:16:53 +02003063 const char *id, *prefix, *name;
3064 int pref_len, nam_len, parent_times, has_predicate;
Michal Vasko184521f2015-09-24 13:14:26 +02003065 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003066
Michal Vasko184521f2015-09-24 13:14:26 +02003067 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003068 parent_times = 0;
3069 id = path;
3070
3071 do {
3072 if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003073 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 +02003074 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003075 }
3076 id += i;
3077
Michal Vasko184521f2015-09-24 13:14:26 +02003078 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003079 if (parent_times == -1) {
Radek Krejcic071c542016-01-27 14:57:51 +01003080 /* resolve prefix of the module */
Radek Krejciadb57612016-02-16 13:34:34 +01003081 mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len);
Radek Krejcic071c542016-01-27 14:57:51 +01003082 /* get start node */
3083 node = mod ? mod->data : NULL;
Michal Vasko58090902015-08-13 14:04:15 +02003084 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003085 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3086 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003087 return EXIT_FAILURE;
Michal Vasko58090902015-08-13 14:04:15 +02003088 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003089 } else if (parent_times > 0) {
Michal Vasko73ae2562015-08-06 11:58:13 +02003090 /* node is the parent already, skip one ".." */
Radek Krejci2f12f852016-01-08 12:59:57 +01003091 if (parent_tpdf) {
3092 /* the path is not allowed to contain relative path since we are in top level typedef */
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003093 LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path);
Radek Krejci2f12f852016-01-08 12:59:57 +01003094 return -1;
3095 }
3096
Radek Krejciadb57612016-02-16 13:34:34 +01003097 node = parent;
Michal Vasko58090902015-08-13 14:04:15 +02003098 i = 0;
3099 while (1) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003100 if (!node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003101 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3102 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003103 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003104 }
Michal Vasko58090902015-08-13 14:04:15 +02003105
3106 /* this node is a wrong node, we actually need the augment target */
3107 if (node->nodetype == LYS_AUGMENT) {
3108 node = ((struct lys_node_augment *)node)->target;
3109 if (!node) {
3110 continue;
3111 }
3112 }
3113
3114 ++i;
3115 if (i == parent_times) {
3116 break;
3117 }
Michal Vaskodcf98e62016-05-05 17:53:53 +02003118 node = lys_parent(node);
Michal Vasko1f76a282015-08-04 16:16:53 +02003119 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003120
Michal Vasko1f76a282015-08-04 16:16:53 +02003121 node = node->child;
Michal Vaskoe01eca52015-08-13 14:42:02 +02003122 } else {
3123 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003124 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003125 }
Michal Vasko36cbaa42015-12-14 13:15:48 +01003126
Michal Vasko184521f2015-09-24 13:14:26 +02003127 first_iter = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003128 } else {
Michal Vasko7dc71d02016-03-15 10:42:28 +01003129 /* move down the tree, if possible */
3130 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
Michal Vasko2f5aceb2016-03-22 10:24:14 +01003131 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 +01003132 return -1;
3133 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003134 node = node->child;
3135 }
3136
Michal Vasko4f0dad02016-02-15 14:08:23 +01003137 if (!prefix) {
Radek Krejciadb57612016-02-16 13:34:34 +01003138 prefix = lys_node_module(parent)->name;
Michal Vasko4f0dad02016-02-15 14:08:23 +01003139 }
3140
Michal Vasko36cbaa42015-12-14 13:15:48 +01003141 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 +02003142 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003143 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003144 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003145 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003146
3147 if (has_predicate) {
3148 /* we have predicate, so the current result must be list */
3149 if (node->nodetype != LYS_LIST) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003150 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003151 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003152 }
3153
Radek Krejci48464ed2016-03-17 15:44:09 +01003154 i = resolve_path_predicate_schema(id, node, parent);
Michal Vasko184521f2015-09-24 13:14:26 +02003155 if (!i) {
Michal Vaskof9664da2015-08-24 15:03:30 +02003156 return EXIT_FAILURE;
Michal Vasko184521f2015-09-24 13:14:26 +02003157 } else if (i < 0) {
3158 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003159 }
3160 id += i;
3161 }
3162 } while (id[0]);
3163
Radek Krejcib1c12512015-08-11 11:22:04 +02003164 /* the target must be leaf or leaf-list */
3165 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003166 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003167 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003168 }
3169
Radek Krejcicf509982015-12-15 09:22:44 +01003170 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003171 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003172 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003173 return -1;
3174 }
3175
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003176 if (ret) {
3177 *ret = node;
3178 }
3179 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003180}
3181
Michal Vasko730dfdf2015-08-11 14:48:05 +02003182/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003183 * @brief Resolve instance-identifier predicate in JSON data format.
3184 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003185 *
Michal Vaskobb211122015-08-19 14:03:11 +02003186 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003187 * @param[in,out] node_match Nodes matching the restriction without
3188 * the predicate. Nodes not satisfying
3189 * the predicate are removed.
3190 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003191 * @return Number of characters successfully parsed,
3192 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003193 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003194static int
Michal Vaskof39142b2015-10-21 11:40:05 +02003195resolve_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003196{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003197 /* ... /node[target = value] ... */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003198 struct unres_data target_match;
3199 struct ly_ctx *ctx;
Michal Vasko1e62a092015-12-01 12:27:20 +01003200 const struct lys_module *mod;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003201 const char *model, *name, *value;
3202 char *str;
3203 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed;
3204 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003205
Michal Vasko1f2cc332015-08-19 11:18:32 +02003206 assert(pred && node_match->count);
3207
Michal Vaskocf024702015-10-08 15:01:42 +02003208 ctx = node_match->node[0]->schema->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003209 idx = -1;
3210 parsed = 0;
3211
3212 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02003213 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003214 return -parsed+i;
3215 }
3216 parsed += i;
3217 pred += i;
3218
Michal Vasko1f2cc332015-08-19 11:18:32 +02003219 /* pos */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003220 if (isdigit(name[0])) {
3221 idx = atoi(name);
3222 }
3223
Michal Vasko1f2cc332015-08-19 11:18:32 +02003224 for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003225 /* target */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003226 memset(&target_match, 0, sizeof target_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003227 if ((name[0] == '.') || !value) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003228 target_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003229 target_match.node = malloc(sizeof *target_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003230 if (!target_match.node) {
3231 LOGMEM;
3232 return -1;
3233 }
Michal Vaskocf024702015-10-08 15:01:42 +02003234 target_match.node[0] = node_match->node[j];
Michal Vasko1f2cc332015-08-19 11:18:32 +02003235 } else {
3236 str = strndup(model, mod_len);
3237 mod = ly_ctx_get_module(ctx, str, NULL);
3238 free(str);
3239
Radek Krejci804836a2016-02-03 10:39:55 +01003240 if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) {
Michal Vasko1f2cc332015-08-19 11:18:32 +02003241 goto remove_instid;
3242 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003243 }
3244
3245 /* check that we have the correct type */
3246 if (name[0] == '.') {
Michal Vaskocf024702015-10-08 15:01:42 +02003247 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003248 goto remove_instid;
3249 }
3250 } else if (value) {
Michal Vaskocf024702015-10-08 15:01:42 +02003251 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003252 goto remove_instid;
3253 }
3254 }
3255
Michal Vasko83a6c462015-10-08 16:43:53 +02003256 if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len)
3257 || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len]))
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003258 || (!value && (idx != cur_idx))) {
3259 goto remove_instid;
3260 }
3261
Michal Vaskocf024702015-10-08 15:01:42 +02003262 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003263
3264 /* leafref is ok, continue check with next leafref */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003265 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003266 continue;
3267
3268remove_instid:
Michal Vaskocf024702015-10-08 15:01:42 +02003269 free(target_match.node);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003270
3271 /* does not fulfill conditions, remove leafref record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003272 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003273 }
3274 } while (has_predicate);
3275
3276 return parsed;
3277}
3278
Michal Vasko730dfdf2015-08-11 14:48:05 +02003279/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003280 * @brief Resolve instance-identifier in JSON data format. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003281 *
Radek Krejciadb57612016-02-16 13:34:34 +01003282 * @param[in] data Data node where the path is used
Michal Vasko730dfdf2015-08-11 14:48:05 +02003283 * @param[in] path Instance-identifier node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003284 *
Radek Krejcic5090c32015-08-12 09:46:19 +02003285 * @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 +02003286 */
Michal Vasko184521f2015-09-24 13:14:26 +02003287static struct lyd_node *
Radek Krejci48464ed2016-03-17 15:44:09 +01003288resolve_instid(struct lyd_node *data, const char *path)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003289{
Radek Krejcic5090c32015-08-12 09:46:19 +02003290 int i = 0, j;
3291 struct lyd_node *result = NULL;
Michal Vaskobea08e92016-05-18 13:28:08 +02003292 const struct lys_module *mod, *prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003293 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003294 const char *model, *name;
Radek Krejcic5090c32015-08-12 09:46:19 +02003295 char *str;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003296 int mod_len, name_len, has_predicate;
3297 struct unres_data node_match;
3298 uint32_t k;
3299
3300 memset(&node_match, 0, sizeof node_match);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003301
Radek Krejcic5090c32015-08-12 09:46:19 +02003302 /* we need root to resolve absolute path */
3303 for (; data->parent; data = data->parent);
Michal Vaskodb9323c2015-10-16 10:32:44 +02003304 /* we're still parsing it and the pointer is not correct yet */
Michal Vasko0491ab32015-08-19 14:28:29 +02003305 if (data->prev) {
3306 for (; data->prev->next; data = data->prev);
3307 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003308
Michal Vaskobea08e92016-05-18 13:28:08 +02003309 prev_mod = lyd_node_module(data);
3310
Radek Krejcic5090c32015-08-12 09:46:19 +02003311 /* search for the instance node */
3312 while (path[i]) {
Michal Vaskof39142b2015-10-21 11:40:05 +02003313 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
Radek Krejcic5090c32015-08-12 09:46:19 +02003314 if (j <= 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003315 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003316 goto error;
3317 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003318 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003319
Michal Vaskobea08e92016-05-18 13:28:08 +02003320 if (model) {
3321 str = strndup(model, mod_len);
3322 if (!str) {
3323 LOGMEM;
3324 goto error;
3325 }
3326 mod = ly_ctx_get_module(ctx, str, NULL);
3327 free(str);
3328 } else {
3329 mod = prev_mod;
Radek Krejcic5090c32015-08-12 09:46:19 +02003330 }
3331
Michal Vasko1f2cc332015-08-19 11:18:32 +02003332 if (resolve_data(mod, name, name_len, data, &node_match)) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003333 /* no instance exists */
3334 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003335 }
3336
3337 if (has_predicate) {
3338 /* we have predicate, so the current results must be list or leaf-list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003339 for (k = 0; k < node_match.count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003340 if ((node_match.node[k]->schema->nodetype == LYS_LIST &&
3341 ((struct lys_node_list *)node_match.node[k]->schema)->keys)
3342 || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003343 /* instid is ok, continue check with next instid */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003344 ++k;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003345 continue;
3346 }
3347
3348 /* does not fulfill conditions, remove inst record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003349 unres_data_del(&node_match, k);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003350 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003351
Michal Vaskof39142b2015-10-21 11:40:05 +02003352 j = resolve_predicate(&path[i], &node_match);
Radek Krejcic5090c32015-08-12 09:46:19 +02003353 if (j < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003354 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003355 goto error;
3356 }
Michal Vasko1f2cc332015-08-19 11:18:32 +02003357 i += j;
Michal Vaskob387c482015-08-12 09:32:59 +02003358
Michal Vasko1f2cc332015-08-19 11:18:32 +02003359 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003360 /* no instance exists */
3361 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003362 }
3363 }
Michal Vaskobea08e92016-05-18 13:28:08 +02003364
3365 prev_mod = mod;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003366 }
3367
Michal Vasko1f2cc332015-08-19 11:18:32 +02003368 if (!node_match.count) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003369 /* no instance exists */
3370 return NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003371 } else if (node_match.count > 1) {
Radek Krejcic5090c32015-08-12 09:46:19 +02003372 /* instance identifier must resolve to a single node */
Radek Krejci48464ed2016-03-17 15:44:09 +01003373 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Radek Krejcic5090c32015-08-12 09:46:19 +02003374
Michal Vaskod6adbaa2016-04-11 11:01:09 +02003375 goto error;
Radek Krejcic5090c32015-08-12 09:46:19 +02003376 } else {
3377 /* we have required result, remember it and cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003378 result = node_match.node[0];
3379 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003380
3381 return result;
3382 }
3383
3384error:
3385
3386 /* cleanup */
Michal Vaskocf024702015-10-08 15:01:42 +02003387 free(node_match.node);
Radek Krejcic5090c32015-08-12 09:46:19 +02003388
3389 return NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003390}
3391
Michal Vasko730dfdf2015-08-11 14:48:05 +02003392/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003393 * @brief Passes config flag down to children, skips nodes without config flags.
3394 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003395 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003396 * @param[in] node Siblings and their children to have flags changed.
3397 * @param[in] flags Flags to assign to all the nodes.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003398 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003399static void
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003400inherit_config_flag(struct lys_node *node, int flags)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003401{
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003402 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02003403 LY_TREE_FOR(node, node) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003404 if (node->flags & LYS_CONFIG_SET) {
3405 /* skip nodes with an explicit config value */
3406 continue;
3407 }
3408 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
3409 node->flags |= flags;
3410 }
3411 inherit_config_flag(node->child, flags);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003412 }
3413}
3414
Michal Vasko730dfdf2015-08-11 14:48:05 +02003415/**
Michal Vasko7178e692016-02-12 15:58:05 +01003416 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003417 *
Michal Vaskobb211122015-08-19 14:03:11 +02003418 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01003419 * @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 +02003420 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003421 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003422 */
Michal Vasko7178e692016-02-12 15:58:05 +01003423static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003424resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003425{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003426 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02003427 struct lys_node *sub;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003428
Michal Vasko1d87a922015-08-21 12:57:16 +02003429 assert(aug);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003430
3431 /* resolve target node */
Michal Vasko3edeaf72016-02-11 13:17:43 +01003432 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 +01003433 if (rc == -1) {
3434 return -1;
3435 }
3436 if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003437 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003438 return -1;
3439 }
3440 if (!aug->target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003441 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01003442 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003443 }
3444
3445 if (!aug->child) {
3446 /* nothing to do */
Michal Vasko1d87a922015-08-21 12:57:16 +02003447 LOGWRN("Augment \"%s\" without children.", aug->target_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003448 return EXIT_SUCCESS;
3449 }
3450
Michal Vaskod58d5962016-03-02 14:29:41 +01003451 /* check for mandatory nodes - if the target node is in another module
3452 * the added nodes cannot be mandatory
3453 */
3454 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
3455 && lyp_check_mandatory((struct lys_node *)aug)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003456 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, "mandatory", "augment node");
3457 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 +01003458 return -1;
3459 }
3460
Michal Vasko07e89ef2016-03-03 13:28:57 +01003461 /* check augment target type and then augment nodes type */
3462 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
3463 LY_TREE_FOR(aug->child, sub) {
3464 if (!(sub->nodetype & (LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003465 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3466 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003467 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3468 return -1;
3469 }
3470 }
3471 } else if (aug->target->nodetype == LYS_CHOICE) {
3472 LY_TREE_FOR(aug->child, sub) {
3473 if (!(sub->nodetype & (LYS_CASE | LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003474 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
3475 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko07e89ef2016-03-03 13:28:57 +01003476 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
3477 return -1;
3478 }
3479 }
3480 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003481 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
3482 LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01003483 return -1;
3484 }
3485
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003486 /* inherit config information from actual parent */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003487 LY_TREE_FOR(aug->child, sub) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003488 inherit_config_flag(sub, aug->target->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003489 }
3490
Radek Krejcic071c542016-01-27 14:57:51 +01003491 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02003492 LY_TREE_FOR(aug->child, sub) {
Michal Vasko2afc1ca2016-05-03 11:38:53 +02003493 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02003494 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02003495 }
3496 }
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003497 /* reconnect augmenting data into the target - add them to the target child list */
3498 if (aug->target->child) {
Michal Vasko1d87a922015-08-21 12:57:16 +02003499 sub = aug->target->child->prev; /* remember current target's last node */
3500 sub->next = aug->child; /* connect augmenting data after target's last node */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003501 aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */
Michal Vasko1d87a922015-08-21 12:57:16 +02003502 aug->child->prev = sub; /* finish connecting of both child lists */
Radek Krejci0acbe1b2015-08-04 09:33:49 +02003503 } else {
3504 aug->target->child = aug->child;
3505 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003506
3507 return EXIT_SUCCESS;
3508}
3509
Michal Vasko730dfdf2015-08-11 14:48:05 +02003510/**
3511 * @brief Resolve uses, apply augments, refines. Logs directly.
3512 *
Michal Vaskobb211122015-08-19 14:03:11 +02003513 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003514 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003515 *
Michal Vaskodef0db12015-10-07 13:22:48 +02003516 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003517 */
Michal Vasko184521f2015-09-24 13:14:26 +02003518static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003519resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003520{
3521 struct ly_ctx *ctx;
Radek Krejci989790c2016-04-14 17:49:41 +02003522 struct lys_node *node = NULL, *next, *iter;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003523 struct lys_node *node_aux;
Radek Krejci76512572015-08-04 09:47:08 +02003524 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003525 struct lys_restr *must, **old_must;
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003526 int i, j, rc, parent_flags;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003527 uint8_t size, *old_size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003528
Michal Vasko71e1aa82015-08-12 12:17:51 +02003529 assert(uses->grp);
Michal Vaskoe7708512016-03-11 10:26:55 +01003530 /* HACK just check that the grouping is resolved */
Michal Vaskodef0db12015-10-07 13:22:48 +02003531 assert(!uses->grp->nacm);
Michal Vasko71e1aa82015-08-12 12:17:51 +02003532
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003533 if (!uses->grp->child) {
3534 /* grouping without children, warning was already displayed */
3535 return EXIT_SUCCESS;
3536 }
3537
3538 /* get proper parent (config) flags */
3539 for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux));
3540 if (node_aux) {
3541 parent_flags = node_aux->flags & LYS_CONFIG_MASK;
3542 } else {
3543 /* default */
3544 parent_flags = LYS_CONFIG_W;
3545 }
3546
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003547 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01003548 LY_TREE_FOR(uses->grp->child, node_aux) {
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003549 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, 0, uses->nacm, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01003550 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003551 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
3552 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003553 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003554 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003555 }
3556 ctx = uses->module->ctx;
3557
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003558 if (parent_flags) {
3559 assert(uses->child);
3560 inherit_config_flag(uses->child, parent_flags);
3561 }
3562
Michal Vaskodef0db12015-10-07 13:22:48 +02003563 /* we managed to copy the grouping, the rest must be possible to resolve */
3564
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003565 /* apply refines */
3566 for (i = 0; i < uses->refine_size; i++) {
3567 rfn = &uses->refine[i];
Michal Vasko3edeaf72016-02-11 13:17:43 +01003568 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE,
Radek Krejcif3c71de2016-04-11 12:45:46 +02003569 1, 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003570 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003571 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskodef0db12015-10-07 13:22:48 +02003572 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003573 }
3574
Radek Krejci1d82ef62015-08-07 14:44:40 +02003575 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003576 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
3577 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node.");
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003578 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003579 }
3580
3581 /* description on any nodetype */
3582 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003583 lydict_remove(ctx, node->dsc);
3584 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003585 }
3586
3587 /* reference on any nodetype */
3588 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003589 lydict_remove(ctx, node->ref);
3590 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003591 }
3592
3593 /* config on any nodetype */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003594 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vaskodcf98e62016-05-05 17:53:53 +02003595 if (lys_parent(node) &&
3596 ((lys_parent(node)->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
Radek Krejci989790c2016-04-14 17:49:41 +02003597 (rfn->flags & LYS_CONFIG_W)) {
3598 /* setting config true under config false is prohibited */
3599 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
3600 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
3601 "changing config from 'false' to 'true' is prohibited while "
3602 "the target's parent is still config 'false'.");
3603 return -1;
3604 }
3605
Radek Krejci1d82ef62015-08-07 14:44:40 +02003606 node->flags &= ~LYS_CONFIG_MASK;
3607 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Radek Krejci989790c2016-04-14 17:49:41 +02003608
3609 /* inherit config change to the target children */
3610 LY_TREE_DFS_BEGIN(node->child, next, iter) {
3611 if (rfn->flags & LYS_CONFIG_W) {
3612 if (iter->flags & LYS_CONFIG_SET) {
3613 /* config is set explicitely, go to next sibling */
3614 next = NULL;
3615 goto nextsibling;
3616 }
3617 } else { /* LYS_CONFIG_R */
3618 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
3619 /* error - we would have config data under status data */
3620 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
3621 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
3622 "changing config from 'true' to 'false' is prohibited while the target "
3623 "has still a children with explicit config 'true'.");
3624 return -1;
3625 }
3626 }
3627 /* change config */
3628 iter->flags &= ~LYS_CONFIG_MASK;
3629 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
3630
3631 /* select next iter - modified LY_TREE_DFS_END */
3632 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) {
3633 next = NULL;
3634 } else {
3635 next = iter->child;
3636 }
3637nextsibling:
3638 if (!next) {
3639 /* no children */
3640 if (iter == node->child) {
3641 /* we are done, (START) has no children */
3642 break;
3643 }
3644 /* try siblings */
3645 next = iter->next;
3646 }
3647 while (!next) {
3648 /* parent is already processed, go to its sibling */
3649 iter = lys_parent(iter);
3650
3651 /* no siblings, go back through parents */
3652 if (iter == node) {
3653 /* we are done, no next element to process */
3654 break;
3655 }
3656 next = iter->next;
3657 }
3658 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003659 }
3660
3661 /* default value ... */
3662 if (rfn->mod.dflt) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003663 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003664 /* leaf */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003665 lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt);
3666 ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0);
3667 } else if (node->nodetype == LYS_CHOICE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003668 /* choice */
Michal Vasko3edeaf72016-02-11 13:17:43 +01003669 rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child,
3670 (const struct lys_node **)&((struct lys_node_choice *)node)->dflt);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003671 if (rc || !((struct lys_node_choice *)node)->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003672 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default");
Michal Vaskodef0db12015-10-07 13:22:48 +02003673 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003674 }
3675 }
3676 }
3677
3678 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003679 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003680 if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003681 /* remove current value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003682 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003683
3684 /* set new value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003685 node->flags |= (rfn->flags & LYS_MAND_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003686 }
3687 }
3688
3689 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003690 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
3691 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
3692 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003693 }
3694
3695 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003696 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003697 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003698 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003699 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003700 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003701 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003702 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003703 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003704 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003705 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003706 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003707 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003708 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003709 }
3710 }
3711
3712 /* must in leaf, leaf-list, list, container or anyxml */
3713 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003714 switch (node->nodetype) {
3715 case LYS_LEAF:
3716 old_size = &((struct lys_node_leaf *)node)->must_size;
3717 old_must = &((struct lys_node_leaf *)node)->must;
3718 break;
3719 case LYS_LEAFLIST:
3720 old_size = &((struct lys_node_leaflist *)node)->must_size;
3721 old_must = &((struct lys_node_leaflist *)node)->must;
3722 break;
3723 case LYS_LIST:
3724 old_size = &((struct lys_node_list *)node)->must_size;
3725 old_must = &((struct lys_node_list *)node)->must;
3726 break;
3727 case LYS_CONTAINER:
3728 old_size = &((struct lys_node_container *)node)->must_size;
3729 old_must = &((struct lys_node_container *)node)->must;
3730 break;
3731 case LYS_ANYXML:
3732 old_size = &((struct lys_node_anyxml *)node)->must_size;
3733 old_must = &((struct lys_node_anyxml *)node)->must;
3734 break;
3735 default:
3736 LOGINT;
Radek Krejcie4e4d722015-10-05 16:53:50 +02003737 return -1;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003738 }
3739
3740 size = *old_size + rfn->must_size;
3741 must = realloc(*old_must, size * sizeof *rfn->must);
3742 if (!must) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003743 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003744 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003745 }
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003746 for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) {
3747 must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0);
3748 must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0);
3749 must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0);
3750 must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0);
3751 must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003752 }
3753
Michal Vaskoef2fdc82015-09-24 09:54:42 +02003754 *old_must = must;
3755 *old_size = size;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003756 }
3757 }
3758
3759 /* apply augments */
3760 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003761 rc = resolve_augment(&uses->augment[i], uses->child);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003762 if (rc) {
Michal Vaskodef0db12015-10-07 13:22:48 +02003763 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003764 }
3765 }
3766
3767 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003768}
3769
Michal Vasko730dfdf2015-08-11 14:48:05 +02003770/**
3771 * @brief Resolve base identity recursively. Does not log.
3772 *
3773 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02003774 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003775 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003776 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003777 *
Michal Vaskof2006002016-04-21 16:28:15 +02003778 * @return EXIT_SUCCESS on success (but ret can still be NULL), EXIT_FAILURE on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003779 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003780static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003781resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003782 struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003783{
Michal Vaskof02e3742015-08-05 16:27:02 +02003784 uint32_t i, j;
Radek Krejcibabbff82016-02-19 13:31:37 +01003785 struct lys_ident *base = NULL, *base_iter;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003786
Radek Krejcicf509982015-12-15 09:22:44 +01003787 assert(ret);
3788
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003789 /* search module */
3790 for (i = 0; i < module->ident_size; i++) {
3791 if (!strcmp(basename, module->ident[i].name)) {
3792
3793 if (!ident) {
3794 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003795 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01003796 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003797 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003798 }
3799
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003800 base = &module->ident[i];
3801 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003802 }
3803 }
3804
3805 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003806 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
3807 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
3808 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003809
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003810 if (!ident) {
3811 *ret = &module->inc[j].submodule->ident[i];
3812 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003813 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003814
3815 base = &module->inc[j].submodule->ident[i];
3816 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003817 }
3818 }
3819 }
3820
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01003821matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003822 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01003823 if (base) {
3824 /* check for circular reference */
3825 for (base_iter = base; base_iter; base_iter = base_iter->base) {
3826 if (ident == base_iter) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003827 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, base_iter->name, "base");
3828 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejcibabbff82016-02-19 13:31:37 +01003829 return EXIT_FAILURE;
3830 }
3831 }
3832 /* checks done, store the result */
3833 ident->base = base;
3834
3835 /* maintain backlinks to the derived identitise */
3836 while (base) {
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003837 /* 1. get current number of backlinks */
3838 if (base->der) {
3839 for (i = 0; base->der[i]; i++);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003840 } else {
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003841 i = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003842 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003843 base->der = ly_realloc(base->der, (i + 2) * sizeof *(base->der));
3844 if (!base->der) {
Michal Vasko253035f2015-12-17 16:58:13 +01003845 LOGMEM;
3846 return EXIT_FAILURE;
3847 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003848 base->der[i] = ident;
3849 base->der[i + 1] = NULL; /* array termination */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003850
Radek Krejcibabbff82016-02-19 13:31:37 +01003851 base = base->base;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003852 }
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003853
Radek Krejcicf509982015-12-15 09:22:44 +01003854 *ret = ident->base;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003855 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003856 }
3857
Michal Vaskof2006002016-04-21 16:28:15 +02003858 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003859}
3860
Michal Vasko730dfdf2015-08-11 14:48:05 +02003861/**
3862 * @brief Resolve base identity. Logs directly.
3863 *
3864 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02003865 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003866 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01003867 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01003868 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003869 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003870 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003871 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003872static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003873resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent,
Radek Krejci48464ed2016-03-17 15:44:09 +01003874 struct lys_type *type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003875{
3876 const char *name;
Michal Vasko2d851a92015-10-20 16:16:36 +02003877 int i, mod_name_len = 0;
Radek Krejcicf509982015-12-15 09:22:44 +01003878 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02003879 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01003880 struct lys_module *mod;
3881
3882 assert((ident && !type) || (!ident && type));
3883
3884 if (!type) {
3885 /* have ident to resolve */
3886 ret = &target;
3887 flags = ident->flags;
3888 mod = ident->module;
3889 } else {
3890 /* have type to fill */
3891 ret = &type->info.ident.ref;
3892 flags = type->parent->flags;
3893 mod = type->parent->module;
3894 }
Michal Vaskof2006002016-04-21 16:28:15 +02003895 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003896
3897 /* search for the base identity */
3898 name = strchr(basename, ':');
3899 if (name) {
3900 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02003901 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003902 name++;
3903
Michal Vasko2d851a92015-10-20 16:16:36 +02003904 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003905 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02003906 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003907 }
3908 } else {
3909 name = basename;
3910 }
3911
Radek Krejcic071c542016-01-27 14:57:51 +01003912 /* get module where to search */
3913 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
3914 if (!module) {
3915 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01003916 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01003917 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003918 }
3919
Radek Krejcic071c542016-01-27 14:57:51 +01003920 /* search in the identified module ... */
Michal Vaskof2006002016-04-21 16:28:15 +02003921 if (resolve_base_ident_sub(module, ident, name, ret)) {
Radek Krejcibabbff82016-02-19 13:31:37 +01003922 return EXIT_FAILURE;
Michal Vaskof2006002016-04-21 16:28:15 +02003923 } else if (*ret) {
3924 goto success;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003925 }
Radek Krejcic071c542016-01-27 14:57:51 +01003926 /* and all its submodules */
3927 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskof2006002016-04-21 16:28:15 +02003928 if (resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) {
Radek Krejcibabbff82016-02-19 13:31:37 +01003929 return EXIT_FAILURE;
Michal Vaskof2006002016-04-21 16:28:15 +02003930 } else if (*ret) {
3931 goto success;
Radek Krejcic071c542016-01-27 14:57:51 +01003932 }
3933 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003934
Michal Vaskof2006002016-04-21 16:28:15 +02003935 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003936 return EXIT_FAILURE;
Radek Krejcicf509982015-12-15 09:22:44 +01003937
3938success:
3939 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01003940 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
3941 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003942 return -1;
3943 }
3944
3945 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003946}
3947
Michal Vasko730dfdf2015-08-11 14:48:05 +02003948/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003949 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003950 *
3951 * @param[in] base Base identity.
Michal Vaskofb0873c2015-08-21 09:00:07 +02003952 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01003953 * @param[in] node Node where the identityref is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02003954 *
3955 * @return Pointer to the identity resolvent, NULL on error.
3956 */
Radek Krejcia52656e2015-08-05 13:41:50 +02003957struct lys_ident *
Radek Krejci48464ed2016-03-17 15:44:09 +01003958resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003959{
Michal Vaskoc633ca02015-08-21 14:03:51 +02003960 const char *mod_name, *name;
3961 int mod_name_len, rc;
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003962 int i;
3963 struct lys_ident *der;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003964
Michal Vaskofb0873c2015-08-21 09:00:07 +02003965 if (!base || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003966 return NULL;
3967 }
3968
Michal Vaskoc633ca02015-08-21 14:03:51 +02003969 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01003970 if (rc < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003971 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02003972 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01003973 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejci02a04992016-03-17 16:06:37 +01003974 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02003975 return NULL;
3976 }
3977
Michal Vaskoc633ca02015-08-21 14:03:51 +02003978 if (!strcmp(base->name, name) && (!mod_name
3979 || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) {
Michal Vaskofb0873c2015-08-21 09:00:07 +02003980 return base;
3981 }
3982
Radek Krejci1b61d0e2016-04-15 13:55:44 +02003983 if (base->der) {
3984 for (der = base->der[i = 0]; base->der[i]; der = base->der[++i]) {
3985 if (!strcmp(der->name, name) &&
3986 (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) {
3987 /* we have match */
3988 return der;
3989 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003990 }
3991 }
3992
Radek Krejci48464ed2016-03-17 15:44:09 +01003993 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003994 return NULL;
3995}
3996
Michal Vasko730dfdf2015-08-11 14:48:05 +02003997/**
Michal Vasko7955b362015-09-04 14:18:15 +02003998 * @brief Resolve (find) choice default case. Does not log.
3999 *
4000 * @param[in] choic Choice to use.
4001 * @param[in] dflt Name of the default case.
4002 *
4003 * @return Pointer to the default node or NULL.
4004 */
4005static struct lys_node *
4006resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4007{
4008 struct lys_node *child, *ret;
4009
4010 LY_TREE_FOR(choic->child, child) {
4011 if (child->nodetype == LYS_USES) {
4012 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4013 if (ret) {
4014 return ret;
4015 }
4016 }
4017
Radek Krejci749190d2016-02-18 16:26:25 +01004018 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYXML | LYS_CASE
Michal Vasko7955b362015-09-04 14:18:15 +02004019 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
4020 return child;
4021 }
4022 }
4023
4024 return NULL;
4025}
4026
4027/**
Michal Vaskobb211122015-08-19 14:03:11 +02004028 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004029 *
Michal Vaskobb211122015-08-19 14:03:11 +02004030 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004031 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004032 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004033 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004034 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004035static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004036resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004037{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004038 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01004039 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02004040
Radek Krejci010e54b2016-03-15 09:40:34 +01004041 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself
4042 * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping)
4043 * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm
4044 * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the
4045 * LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02004046 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 +02004047
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004048 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01004049 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
4050 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004051 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004052 return -1;
4053 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004054 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01004055 return -1;
4056 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004057 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004058 /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field
4059 * (and smaller flags - it uses only a limited set of flags)
4060 */
4061 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004062 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004063 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01004064 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02004065 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004066 }
4067
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004068 if (uses->grp->nacm) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004069 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004070 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004071 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02004072 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004073 return EXIT_FAILURE;
4074 }
4075
Radek Krejci48464ed2016-03-17 15:44:09 +01004076 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004077 if (!rc) {
4078 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01004079 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004080 if (!((struct lys_node_grp *)par_grp)->nacm) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004081 LOGINT;
4082 return -1;
4083 }
Radek Krejci4372b4e2016-04-14 17:42:16 +02004084 ((struct lys_node_grp *)par_grp)->nacm--;
Radek Krejci010e54b2016-03-15 09:40:34 +01004085 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004086 }
Radek Krejcicf509982015-12-15 09:22:44 +01004087
4088 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004089 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01004090 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004091 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01004092 return -1;
4093 }
4094
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004095 return EXIT_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01004096 } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02004097 ((struct lys_node_grp *)par_grp)->nacm++;
Radek Krejci010e54b2016-03-15 09:40:34 +01004098 uses->flags |= LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004099 }
4100
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004101 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004102}
4103
Michal Vasko730dfdf2015-08-11 14:48:05 +02004104/**
Michal Vasko9957e592015-08-17 15:04:09 +02004105 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004106 *
Michal Vaskobb211122015-08-19 14:03:11 +02004107 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004108 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004109 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004110 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004111 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004112static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004113resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004114{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004115 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01004116 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004117
4118 for (i = 0; i < list->keys_size; ++i) {
4119 /* get the key name */
4120 if ((value = strpbrk(keys_str, " \t\n"))) {
4121 len = value - keys_str;
4122 while (isspace(value[0])) {
4123 value++;
4124 }
4125 } else {
4126 len = strlen(keys_str);
4127 }
4128
Radek Krejcic4283442016-04-22 09:19:27 +02004129 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 +02004130 if (rc) {
Michal Vasko7a55bea2016-05-02 14:51:20 +02004131 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
4132 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004133 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004134
Radek Krejci48464ed2016-03-17 15:44:09 +01004135 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004136 /* check_key logs */
4137 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004138 }
4139
Radek Krejcicf509982015-12-15 09:22:44 +01004140 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01004141 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004142 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
4143 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01004144 return -1;
4145 }
4146
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004147 /* prepare for next iteration */
4148 while (value && isspace(value[0])) {
4149 value++;
4150 }
4151 keys_str = value;
4152 }
4153
Michal Vaskof02e3742015-08-05 16:27:02 +02004154 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004155}
4156
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004157/**
Michal Vaskobf19d252015-10-08 15:39:17 +02004158 * @brief Resolve (check) all must conditions of \p node.
4159 * Logs directly.
4160 *
4161 * @param[in] node Data node with optional must statements.
Michal Vaskobf19d252015-10-08 15:39:17 +02004162 *
4163 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
4164 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004165static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004166resolve_must(struct lyd_node *node)
Michal Vaskof02e3742015-08-05 16:27:02 +02004167{
Michal Vaskobf19d252015-10-08 15:39:17 +02004168 uint8_t i, must_size;
4169 struct lys_restr *must;
4170 struct lyxp_set set;
4171
4172 assert(node);
4173 memset(&set, 0, sizeof set);
4174
4175 switch (node->schema->nodetype) {
4176 case LYS_CONTAINER:
4177 must_size = ((struct lys_node_container *)node->schema)->must_size;
4178 must = ((struct lys_node_container *)node->schema)->must;
4179 break;
4180 case LYS_LEAF:
4181 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
4182 must = ((struct lys_node_leaf *)node->schema)->must;
4183 break;
4184 case LYS_LEAFLIST:
4185 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
4186 must = ((struct lys_node_leaflist *)node->schema)->must;
4187 break;
4188 case LYS_LIST:
4189 must_size = ((struct lys_node_list *)node->schema)->must_size;
4190 must = ((struct lys_node_list *)node->schema)->must;
4191 break;
4192 case LYS_ANYXML:
4193 must_size = ((struct lys_node_anyxml *)node->schema)->must_size;
4194 must = ((struct lys_node_anyxml *)node->schema)->must;
4195 break;
4196 default:
4197 must_size = 0;
4198 break;
4199 }
4200
4201 for (i = 0; i < must_size; ++i) {
Michal Vasko944a5642016-03-21 11:48:58 +01004202 if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004203 return -1;
4204 }
4205
Michal Vasko944a5642016-03-21 11:48:58 +01004206 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02004207
Michal Vasko8146d4c2016-05-09 15:50:29 +02004208 if (!set.val.bool) {
Michal Vasko6ac68282016-04-11 10:56:47 +02004209 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
4210 if (must[i].emsg) {
4211 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg);
4212 }
4213 if (must[i].eapptag) {
4214 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
4215 }
Michal Vaskobf19d252015-10-08 15:39:17 +02004216 return 1;
4217 }
4218 }
4219
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004220 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02004221}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004222
Michal Vaskobf19d252015-10-08 15:39:17 +02004223/**
Michal Vaskocf024702015-10-08 15:01:42 +02004224 * @brief Resolve (find) when condition context node. Does not log.
4225 *
4226 * @param[in] node Data node, whose conditional definition is being decided.
4227 * @param[in] schema Schema node with a when condition.
4228 *
4229 * @return Context node.
4230 */
4231static struct lyd_node *
4232resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004233{
Michal Vaskocf024702015-10-08 15:01:42 +02004234 struct lyd_node *parent;
4235 struct lys_node *sparent;
4236 uint16_t i, data_depth, schema_depth;
4237
4238 /* find a not schema-only node */
4239 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
4240 schema = lys_parent(schema);
4241 if (!schema) {
4242 return NULL;
4243 }
4244 }
4245
4246 /* get node depths */
4247 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
4248 for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) {
4249 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) {
4250 ++schema_depth;
4251 }
4252 }
4253 if (data_depth < schema_depth) {
4254 return NULL;
4255 }
4256
4257 /* find the corresponding data node */
4258 for (i = 0; i < data_depth - schema_depth; ++i) {
4259 node = node->parent;
4260 }
4261 if (node->schema != schema) {
4262 return NULL;
4263 }
4264
4265 return node;
4266}
4267
Radek Krejci03b71f72016-03-16 11:10:09 +01004268int
Radek Krejci01696bf2016-03-18 13:19:36 +01004269resolve_applies_must(const struct lyd_node *node)
4270{
4271 switch (node->schema->nodetype) {
4272 case LYS_CONTAINER:
4273 return ((struct lys_node_container *)node->schema)->must_size;
4274 case LYS_LEAF:
4275 return ((struct lys_node_leaf *)node->schema)->must_size;
4276 case LYS_LEAFLIST:
4277 return ((struct lys_node_leaflist *)node->schema)->must_size;
4278 case LYS_LIST:
4279 return ((struct lys_node_list *)node->schema)->must_size;
4280 case LYS_ANYXML:
4281 return ((struct lys_node_anyxml *)node->schema)->must_size;
4282 default:
4283 return 0;
4284 }
4285}
4286
4287int
Radek Krejci03b71f72016-03-16 11:10:09 +01004288resolve_applies_when(const struct lyd_node *node)
4289{
4290 struct lys_node *parent;
4291
4292 assert(node);
4293
4294 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
4295 return 1;
4296 }
4297
4298 parent = node->schema;
4299 goto check_augment;
4300
4301 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4302 if (((struct lys_node_uses *)parent)->when) {
4303 return 1;
4304 }
4305check_augment:
4306
4307 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
4308 (((struct lys_node_augment *)parent->parent)->when))) {
4309
4310 }
4311 parent = lys_parent(parent);
4312 }
4313
4314 return 0;
4315}
4316
Michal Vaskocf024702015-10-08 15:01:42 +02004317/**
4318 * @brief Resolve (check) all when conditions relevant for \p node.
4319 * Logs directly.
4320 *
4321 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02004322 *
Radek Krejci03b71f72016-03-16 11:10:09 +01004323 * @return
4324 * -1 - error, ly_errno is set
4325 * 0 - true "when" statement
4326 * 0, ly_vecode = LYVE_NOCOND - false "when" statement
4327 * 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 +02004328 */
4329static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004330resolve_when(struct lyd_node *node)
Michal Vaskocf024702015-10-08 15:01:42 +02004331{
4332 struct lyd_node *ctx_node = NULL;
4333 struct lys_node *parent;
4334 struct lyxp_set set;
Radek Krejci51093642016-03-29 10:14:59 +02004335 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02004336
4337 assert(node);
4338 memset(&set, 0, sizeof set);
4339
4340 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko944a5642016-03-21 11:48:58 +01004341 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004342 if (rc) {
4343 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004344 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004345 }
Radek Krejci51093642016-03-29 10:14:59 +02004346 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004347 }
4348
Radek Krejci03b71f72016-03-16 11:10:09 +01004349 /* set boolean result of the condition */
Michal Vasko944a5642016-03-21 11:48:58 +01004350 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02004351 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004352 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004353 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004354 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004355 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004356 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004357 }
Radek Krejci51093642016-03-29 10:14:59 +02004358
4359 /* free xpath set content */
4360 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004361 }
4362
4363 parent = node->schema;
4364 goto check_augment;
4365
4366 /* check when in every schema node that affects node */
4367 while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
4368 if (((struct lys_node_uses *)parent)->when) {
4369 if (!ctx_node) {
4370 ctx_node = resolve_when_ctx_node(node, parent);
4371 if (!ctx_node) {
4372 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004373 rc = -1;
4374 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004375 }
4376 }
Michal Vasko944a5642016-03-21 11:48:58 +01004377 rc = lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004378 if (rc) {
4379 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004380 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004381 }
Radek Krejci51093642016-03-29 10:14:59 +02004382 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004383 }
4384
Michal Vasko944a5642016-03-21 11:48:58 +01004385 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02004386 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004387 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004388 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004389 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004390 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004391 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004392 }
Radek Krejci51093642016-03-29 10:14:59 +02004393
4394 /* free xpath set content */
4395 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004396 }
4397
4398check_augment:
4399 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) {
4400 if (!ctx_node) {
4401 ctx_node = resolve_when_ctx_node(node, parent->parent);
4402 if (!ctx_node) {
4403 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02004404 rc = -1;
4405 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004406 }
4407 }
Michal Vasko944a5642016-03-21 11:48:58 +01004408 rc = lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, LYXP_WHEN);
Radek Krejci03b71f72016-03-16 11:10:09 +01004409 if (rc) {
4410 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004411 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004412 }
Radek Krejci51093642016-03-29 10:14:59 +02004413 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004414 }
4415
Michal Vasko944a5642016-03-21 11:48:58 +01004416 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN);
Michal Vaskocf024702015-10-08 15:01:42 +02004417
Michal Vasko8146d4c2016-05-09 15:50:29 +02004418 if (!set.val.bool) {
Radek Krejci03b71f72016-03-16 11:10:09 +01004419 ly_vlog_hide(1);
Michal Vasko6ac68282016-04-11 10:56:47 +02004420 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01004421 ly_vlog_hide(0);
Radek Krejci0b7704f2016-03-18 12:16:14 +01004422 node->when_status |= LYD_WHEN_FALSE;
Radek Krejci51093642016-03-29 10:14:59 +02004423 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02004424 }
Radek Krejci51093642016-03-29 10:14:59 +02004425
4426 /* free xpath set content */
4427 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0);
Michal Vaskocf024702015-10-08 15:01:42 +02004428 }
4429
4430 parent = lys_parent(parent);
4431 }
4432
Radek Krejci0b7704f2016-03-18 12:16:14 +01004433 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01004434
Radek Krejci51093642016-03-29 10:14:59 +02004435cleanup:
4436
4437 /* free xpath set content */
4438 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0);
4439
4440 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004441}
4442
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004443/**
Michal Vaskobb211122015-08-19 14:03:11 +02004444 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004445 *
4446 * @param[in] mod Main module.
4447 * @param[in] item Item to resolve. Type determined by \p type.
4448 * @param[in] type Type of the unresolved item.
4449 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02004450 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004451 *
4452 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
4453 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004454static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004455resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Radek Krejci48464ed2016-03-17 15:44:09 +01004456 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004457{
Radek Krejci4f78b532016-02-17 13:43:00 +01004458 int rc = -1, has_str = 0, tpdf_flag = 0;
Michal Vasko563ef092015-09-04 13:17:23 +02004459 struct lys_node *node;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004460 const char *base_name;
4461
4462 struct lys_ident *ident;
4463 struct lys_type *stype;
4464 struct lys_feature **feat_ptr;
4465 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01004466 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01004467 struct yang_type *yang;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004468
4469 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004470 case UNRES_IDENT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004471 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004472 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004473 ident = item;
4474
Radek Krejci48464ed2016-03-17 15:44:09 +01004475 rc = resolve_base_ident(mod, ident, base_name, "identity", NULL);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004476 break;
4477 case UNRES_TYPE_IDENTREF:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004478 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004479 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004480 stype = item;
4481
Radek Krejci48464ed2016-03-17 15:44:09 +01004482 rc = resolve_base_ident(mod, NULL, base_name, "type", stype);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004483 break;
4484 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02004485 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004486 stype = item;
4487
Radek Krejci2f12f852016-01-08 12:59:57 +01004488 /* HACK - when there is no parent, we are in top level typedef and in that
4489 * case, the path has to contain absolute path, so we let the resolve_path_arg_schema()
4490 * know it via tpdf_flag */
4491 if (!node) {
Radek Krejci4f78b532016-02-17 13:43:00 +01004492 tpdf_flag = 1;
Radek Krejci2f12f852016-01-08 12:59:57 +01004493 node = (struct lys_node *)stype->parent;
4494 }
4495
Radek Krejci48464ed2016-03-17 15:44:09 +01004496 rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag,
Michal Vasko1e62a092015-12-01 12:27:20 +01004497 (const struct lys_node **)&stype->info.lref.target);
Radek Krejci46c4cd72016-01-21 15:13:52 +01004498 if (stype->info.lref.target) {
4499 /* store the backlink from leafref target */
4500 if (!stype->info.lref.target->child) {
4501 stype->info.lref.target->child = (void*)ly_set_new();
4502 if (!stype->info.lref.target->child) {
4503 LOGMEM;
4504 return -1;
4505 }
4506 }
4507 ly_set_add((struct ly_set *)stype->info.lref.target->child, stype->parent);
4508 }
4509
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004510 break;
4511 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01004512 /* parent */
4513 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004514 stype = item;
4515
Michal Vasko88c29542015-11-27 14:57:53 +01004516 /* HACK type->der is temporarily unparsed type statement */
4517 yin = (struct lyxml_elem *)stype->der;
4518 stype->der = NULL;
4519
Pavol Vicana0e4e672016-02-24 12:20:04 +01004520 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
4521 yang = (struct yang_type *)yin;
4522 rc = yang_check_type(mod, node, yang, unres);
4523
4524 if (rc) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02004525 if (rc == -1) {
4526 yang->type->base = yang->base;
4527 lydict_remove(mod->ctx, yang->name);
4528 free(yang);
4529 stype->der = NULL;
4530 } else {
4531 /* may try again later */
4532 stype->der = (struct lys_tpdf *)yang;
4533 }
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004534 } else {
4535 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02004536 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004537 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01004538 }
4539
Michal Vasko88c29542015-11-27 14:57:53 +01004540 } else {
Pavol Vicana0e4e672016-02-24 12:20:04 +01004541 rc = fill_yin_type(mod, node, yin, stype, unres);
4542 if (!rc) {
4543 /* we need to always be able to free this, it's safe only in this case */
4544 lyxml_free(mod->ctx, yin);
4545 } else {
4546 /* may try again later, put all back how it was */
4547 stype->der = (struct lys_tpdf *)yin;
4548 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004549 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004550 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004551 case UNRES_IFFEAT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004552 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004553 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004554 feat_ptr = item;
4555
Radek Krejci48464ed2016-03-17 15:44:09 +01004556 rc = resolve_feature(base_name, mod, feat_ptr);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004557 break;
4558 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01004559 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004560 break;
4561 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004562 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004563 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004564 stype = item;
4565
Radek Krejci48464ed2016-03-17 15:44:09 +01004566 rc = check_default(stype, base_name, mod);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004567 break;
4568 case UNRES_CHOICE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004569 base_name = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01004570 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004571 choic = item;
4572
Michal Vasko7955b362015-09-04 14:18:15 +02004573 choic->dflt = resolve_choice_dflt(choic, base_name);
4574 if (choic->dflt) {
4575 rc = EXIT_SUCCESS;
4576 } else {
4577 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02004578 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004579 break;
4580 case UNRES_LIST_KEYS:
Radek Krejci4f78b532016-02-17 13:43:00 +01004581 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01004582 rc = resolve_list_keys(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004583 break;
4584 case UNRES_LIST_UNIQ:
Radek Krejci4f78b532016-02-17 13:43:00 +01004585 has_str = 1;
Radek Krejci48464ed2016-03-17 15:44:09 +01004586 rc = resolve_unique(item, str_snode);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004587 break;
Michal Vasko7178e692016-02-12 15:58:05 +01004588 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004589 rc = resolve_augment(item, NULL);
Michal Vasko7178e692016-02-12 15:58:05 +01004590 break;
Michal Vaskocf024702015-10-08 15:01:42 +02004591 default:
4592 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004593 break;
4594 }
4595
Radek Krejci4f78b532016-02-17 13:43:00 +01004596 if (has_str && !rc) {
4597 lydict_remove(mod->ctx, str_snode);
4598 }
4599
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004600 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004601}
4602
Michal Vaskof02e3742015-08-05 16:27:02 +02004603/* logs directly */
4604static void
Radek Krejci48464ed2016-03-17 15:44:09 +01004605print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004606{
Michal Vaskof02e3742015-08-05 16:27:02 +02004607 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02004608 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004609 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004610 break;
4611 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01004612 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004613 break;
4614 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01004615 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
4616 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02004617 break;
4618 case UNRES_TYPE_DER:
Radek Krejci48464ed2016-03-17 15:44:09 +01004619 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type",
4620 ((struct lyxml_elem *)((struct lys_type *)item)->der)->attr->value);
Michal Vaskof02e3742015-08-05 16:27:02 +02004621 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02004622 case UNRES_IFFEAT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004623 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004624 break;
4625 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01004626 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02004627 break;
4628 case UNRES_TYPE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004629 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004630 break;
4631 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004632 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004633 break;
4634 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01004635 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004636 break;
4637 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01004638 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02004639 break;
Michal Vasko7178e692016-02-12 15:58:05 +01004640 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01004641 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
4642 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01004643 break;
Michal Vaskocf024702015-10-08 15:01:42 +02004644 default:
4645 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02004646 break;
4647 }
4648}
4649
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004650/**
Michal Vaskobb211122015-08-19 14:03:11 +02004651 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004652 *
4653 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004654 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004655 *
Michal Vasko92b8a382015-08-19 14:03:49 +02004656 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004657 */
Michal Vaskof02e3742015-08-05 16:27:02 +02004658int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004659resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02004660{
Radek Krejci010e54b2016-03-15 09:40:34 +01004661 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004662 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004663
4664 assert(unres);
4665
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02004666 LOGVRB("Resolving unresolved schema nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01004667 ly_vlog_hide(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02004668
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004669 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02004670 do {
Michal Vasko88c29542015-11-27 14:57:53 +01004671 unres_count = 0;
4672 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02004673
4674 for (i = 0; i < unres->count; ++i) {
Michal Vasko88c29542015-11-27 14:57:53 +01004675 /* we do not need to have UNRES_TYPE_IDENTREF or UNRES_TYPE_LEAFREF resolved,
4676 * we need every type's base only */
4677 if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_TYPE_DER)) {
Michal Vasko51054ca2015-08-12 12:20:00 +02004678 continue;
4679 }
4680
Michal Vasko88c29542015-11-27 14:57:53 +01004681 ++unres_count;
Radek Krejci48464ed2016-03-17 15:44:09 +01004682 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004683 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02004684 unres->type[i] = UNRES_RESOLVED;
4685 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01004686 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02004687 } else if (rc == -1) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004688 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004689 return -1;
Michal Vasko51054ca2015-08-12 12:20:00 +02004690 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004691 }
Michal Vasko88c29542015-11-27 14:57:53 +01004692 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02004693
Michal Vasko88c29542015-11-27 14:57:53 +01004694 if (res_count < unres_count) {
Michal Vasko92b8a382015-08-19 14:03:49 +02004695 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004696 }
4697
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004698 /* the rest */
4699 for (i = 0; i < unres->count; ++i) {
4700 if (unres->type[i] == UNRES_RESOLVED) {
4701 continue;
4702 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02004703
Radek Krejci48464ed2016-03-17 15:44:09 +01004704 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004705 if (rc == 0) {
4706 unres->type[i] = UNRES_RESOLVED;
4707 ++resolved;
4708 } else if (rc == -1) {
4709 ly_vlog_hide(0);
Michal Vasko184521f2015-09-24 13:14:26 +02004710 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004711 }
4712 }
4713
Radek Krejci010e54b2016-03-15 09:40:34 +01004714 ly_vlog_hide(0);
4715
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004716 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004717 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
4718 * all the validation errors
4719 */
4720 for (i = 0; i < unres->count; ++i) {
4721 if (unres->type[i] == UNRES_RESOLVED) {
4722 continue;
4723 }
Radek Krejci48464ed2016-03-17 15:44:09 +01004724 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004725 }
Michal Vasko92b8a382015-08-19 14:03:49 +02004726 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004727 }
4728
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02004729 LOGVRB("All schema nodes and constraints resolved.");
Radek Krejcic071c542016-01-27 14:57:51 +01004730 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004731 return EXIT_SUCCESS;
4732}
4733
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004734/**
Michal Vaskobb211122015-08-19 14:03:11 +02004735 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004736 *
4737 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004738 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004739 * @param[in] item Item to resolve. Type determined by \p type.
4740 * @param[in] type Type of the unresolved item.
4741 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004742 *
4743 * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error.
4744 */
4745int
Radek Krejci48464ed2016-03-17 15:44:09 +01004746unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
4747 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004748{
Radek Krejci48464ed2016-03-17 15:44:09 +01004749 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 +02004750}
4751
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004752/**
Michal Vaskobb211122015-08-19 14:03:11 +02004753 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004754 *
4755 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004756 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004757 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01004758 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004759 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004760 *
4761 * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error.
4762 */
4763int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004764unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01004765 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004766{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004767 int rc;
Michal Vasko88c29542015-11-27 14:57:53 +01004768 struct lyxml_elem *yin;
Radek Krejci010e54b2016-03-15 09:40:34 +01004769 char *path, *msg;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004770
Michal Vasko9bf425b2015-10-22 11:42:03 +02004771 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
4772 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004773
Radek Krejci010e54b2016-03-15 09:40:34 +01004774 ly_vlog_hide(1);
Radek Krejci48464ed2016-03-17 15:44:09 +01004775 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01004776 ly_vlog_hide(0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004777 if (rc != EXIT_FAILURE) {
Radek Krejci010e54b2016-03-15 09:40:34 +01004778 if (rc == -1 && ly_errno == LY_EVALID) {
4779 path = strdup(ly_errpath());
4780 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()),
4781 path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : "");
4782 free(path);
4783 free(msg);
4784 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004785 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004786 }
4787
Radek Krejci48464ed2016-03-17 15:44:09 +01004788 print_unres_schema_item_fail(item, type, snode);
Michal Vaskof02e3742015-08-05 16:27:02 +02004789
Michal Vasko88c29542015-11-27 14:57:53 +01004790 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
4791 if (type == UNRES_TYPE_DER) {
4792 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
Pavol Vicana0e4e672016-02-24 12:20:04 +01004793 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
4794 lyxml_unlink_elem(mod->ctx, yin, 1);
4795 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
4796 }
Michal Vasko88c29542015-11-27 14:57:53 +01004797 }
4798
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004799 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01004800 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
4801 if (!unres->item) {
4802 LOGMEM;
4803 return -1;
4804 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004805 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01004806 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
4807 if (!unres->type) {
4808 LOGMEM;
4809 return -1;
4810 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004811 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01004812 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
4813 if (!unres->str_snode) {
4814 LOGMEM;
4815 return -1;
4816 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004817 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01004818 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
4819 if (!unres->module) {
4820 LOGMEM;
4821 return -1;
4822 }
4823 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004824
4825 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004826}
4827
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004828/**
Michal Vaskobb211122015-08-19 14:03:11 +02004829 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004830 *
4831 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004832 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004833 * @param[in] item Old item to be resolved.
4834 * @param[in] type Type of the old unresolved item.
4835 * @param[in] new_item New item to use in the duplicate.
4836 *
4837 * @return EXIT_SUCCESS on success, -1 on error.
4838 */
Michal Vaskodad19402015-08-06 09:51:53 +02004839int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004840unres_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 +02004841{
4842 int i;
4843
Michal Vaskocf024702015-10-08 15:01:42 +02004844 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004845
Michal Vasko0bd29d12015-08-19 11:45:49 +02004846 i = unres_schema_find(unres, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004847
4848 if (i == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004849 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004850 }
4851
Michal Vasko0d204592015-10-07 09:50:04 +02004852 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004853 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004854 LOGINT;
4855 return -1;
4856 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004857 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004858 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004859 LOGINT;
4860 return -1;
4861 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004862 }
Michal Vaskodad19402015-08-06 09:51:53 +02004863
4864 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004865}
4866
Michal Vaskof02e3742015-08-05 16:27:02 +02004867/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004868int
Michal Vasko0bd29d12015-08-19 11:45:49 +02004869unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004870{
4871 uint32_t ret = -1, i;
4872
4873 for (i = 0; i < unres->count; ++i) {
4874 if ((unres->item[i] == item) && (unres->type[i] == type)) {
4875 ret = i;
4876 break;
4877 }
4878 }
4879
4880 return ret;
4881}
Michal Vasko8bcdf292015-08-19 14:04:43 +02004882
Michal Vasko88c29542015-11-27 14:57:53 +01004883void
Radek Krejcic071c542016-01-27 14:57:51 +01004884unres_schema_free(struct lys_module *module, struct unres_schema **unres)
Michal Vasko88c29542015-11-27 14:57:53 +01004885{
4886 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01004887 unsigned int unresolved = 0;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004888 struct lyxml_elem *yin;
4889 struct yang_type *yang;
Michal Vasko88c29542015-11-27 14:57:53 +01004890
Radek Krejcic071c542016-01-27 14:57:51 +01004891 if (!unres || !(*unres)) {
4892 return;
Michal Vasko88c29542015-11-27 14:57:53 +01004893 }
4894
Radek Krejcic071c542016-01-27 14:57:51 +01004895 assert(module || (*unres)->count == 0);
4896
4897 for (i = 0; i < (*unres)->count; ++i) {
4898 if ((*unres)->module[i] != module) {
4899 if ((*unres)->type[i] != UNRES_RESOLVED) {
4900 unresolved++;
4901 }
4902 continue;
4903 }
4904 if ((*unres)->type[i] == UNRES_TYPE_DER) {
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004905 yin = (struct lyxml_elem *)((struct lys_type *)(*unres)->item[i])->der;
4906 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
4907 yang =(struct yang_type *)yin;
Pavol Vican6b072512016-04-04 10:50:21 +02004908 yang->type->base = yang->base;
Pavol Vican5f0316a2016-04-05 21:21:11 +02004909 lydict_remove(module->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01004910 free(yang);
4911 } else {
4912 lyxml_free(module->ctx, yin);
4913 }
Radek Krejcic071c542016-01-27 14:57:51 +01004914 }
4915 (*unres)->type[i] = UNRES_RESOLVED;
4916 }
4917
4918 if (!module || (!unresolved && !module->type)) {
4919 free((*unres)->item);
4920 free((*unres)->type);
4921 free((*unres)->str_snode);
4922 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01004923 free((*unres));
4924 (*unres) = NULL;
4925 }
Michal Vasko88c29542015-11-27 14:57:53 +01004926}
4927
Michal Vasko8bcdf292015-08-19 14:04:43 +02004928/**
4929 * @brief Resolve a single unres data item. Logs directly.
4930 *
Michal Vaskocf024702015-10-08 15:01:42 +02004931 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02004932 * @param[in] type Type of the unresolved item.
Michal Vasko8bcdf292015-08-19 14:04:43 +02004933 *
4934 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
4935 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02004936int
Radek Krejci48464ed2016-03-17 15:44:09 +01004937resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02004938{
4939 uint32_t i;
Michal Vasko0491ab32015-08-19 14:28:29 +02004940 int rc;
Michal Vasko83a6c462015-10-08 16:43:53 +02004941 struct lyd_node_leaf_list *leaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004942 struct lys_node_leaf *sleaf;
Michal Vaskoc4280842016-04-19 16:10:42 +02004943 struct lyd_node *parent;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004944 struct unres_data matches;
4945
4946 memset(&matches, 0, sizeof matches);
Michal Vasko83a6c462015-10-08 16:43:53 +02004947 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02004948 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004949
Michal Vaskocf024702015-10-08 15:01:42 +02004950 switch (type) {
4951 case UNRES_LEAFREF:
4952 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vasko2471e7f2016-04-11 11:00:15 +02004953 /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */
4954 if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) {
4955 return -1;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004956 }
4957
4958 /* check that value matches */
4959 for (i = 0; i < matches.count; ++i) {
Radek Krejci749190d2016-02-18 16:26:25 +01004960 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) {
Michal Vaskocf024702015-10-08 15:01:42 +02004961 leaf->value.leafref = matches.node[i];
Michal Vasko8bcdf292015-08-19 14:04:43 +02004962 break;
4963 }
4964 }
4965
Michal Vaskocf024702015-10-08 15:01:42 +02004966 free(matches.node);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004967
Michal Vaskocf024702015-10-08 15:01:42 +02004968 if (!leaf->value.leafref) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02004969 /* reference not found */
Michal Vasko6ac68282016-04-11 10:56:47 +02004970 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004971 return EXIT_FAILURE;
4972 }
Michal Vaskocf024702015-10-08 15:01:42 +02004973 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02004974
Michal Vaskocf024702015-10-08 15:01:42 +02004975 case UNRES_INSTID:
Michal Vasko976a5f22016-05-18 13:28:42 +02004976 assert((sleaf->type.base == LY_TYPE_INST) || (sleaf->type.base == LY_TYPE_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02004977 ly_errno = 0;
Radek Krejci48464ed2016-03-17 15:44:09 +01004978 leaf->value.instance = resolve_instid(node, leaf->value_str);
Radek Krejci40f17b92016-02-03 14:30:43 +01004979 if (!leaf->value.instance) {
Michal Vasko8bcdf292015-08-19 14:04:43 +02004980 if (ly_errno) {
4981 return -1;
4982 } else if (sleaf->type.info.inst.req > -1) {
Michal Vasko6ac68282016-04-11 10:56:47 +02004983 LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004984 return EXIT_FAILURE;
4985 } else {
Radek Krejci4ce42be2016-02-03 13:04:41 +01004986 LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str);
Michal Vasko8bcdf292015-08-19 14:04:43 +02004987 }
4988 }
Michal Vaskocf024702015-10-08 15:01:42 +02004989 break;
4990
4991 case UNRES_WHEN:
Radek Krejci48464ed2016-03-17 15:44:09 +01004992 if ((rc = resolve_when(node))) {
Michal Vaskocf024702015-10-08 15:01:42 +02004993 return rc;
4994 }
4995 break;
4996
Michal Vaskobf19d252015-10-08 15:39:17 +02004997 case UNRES_MUST:
Radek Krejci48464ed2016-03-17 15:44:09 +01004998 if ((rc = resolve_must(node))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02004999 return rc;
5000 }
5001 break;
5002
Michal Vaskoc4280842016-04-19 16:10:42 +02005003 case UNRES_EMPTYCONT:
5004 do {
5005 parent = node->parent;
5006 lyd_free(node);
5007 node = parent;
5008 } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child
5009 && !((struct lys_node_container *)node->schema)->presence);
5010 break;
5011
Michal Vaskocf024702015-10-08 15:01:42 +02005012 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02005013 LOGINT;
5014 return -1;
5015 }
5016
5017 return EXIT_SUCCESS;
5018}
5019
5020/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01005021 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02005022 *
5023 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02005024 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005025 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01005026 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02005027 */
5028int
Radek Krejci0b7704f2016-03-18 12:16:14 +01005029unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02005030{
Radek Krejci03b71f72016-03-16 11:10:09 +01005031 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02005032 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
5033 || (type == UNRES_EMPTYCONT));
Michal Vasko8bcdf292015-08-19 14:04:43 +02005034
Radek Krejci03b71f72016-03-16 11:10:09 +01005035 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01005036 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
5037 if (!unres->node) {
5038 LOGMEM;
5039 return -1;
5040 }
Michal Vaskocf024702015-10-08 15:01:42 +02005041 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01005042 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
5043 if (!unres->type) {
5044 LOGMEM;
5045 return -1;
5046 }
Michal Vaskocf024702015-10-08 15:01:42 +02005047 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02005048
Radek Krejci0b7704f2016-03-18 12:16:14 +01005049 if (type == UNRES_WHEN) {
5050 /* remove previous result */
5051 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005052 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005053
5054 return EXIT_SUCCESS;
5055}
5056
5057/**
5058 * @brief Resolve every unres data item in the structure. Logs directly.
5059 *
5060 * @param[in] unres Unres data structure to use.
Radek Krejci03b71f72016-03-16 11:10:09 +01005061 * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If
5062 * NULL and when condition is false the error is raised.
Radek Krejci0c0086a2016-03-24 15:20:28 +01005063 * @param[in] options Parer options
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005064 *
5065 * @return EXIT_SUCCESS on success, -1 on error.
5066 */
5067int
Radek Krejci0c0086a2016-03-24 15:20:28 +01005068resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005069{
Radek Krejci0c0086a2016-03-24 15:20:28 +01005070 uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01005071 int rc, progress;
Radek Krejci03b71f72016-03-16 11:10:09 +01005072 char *msg, *path;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005073 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005074
Radek Krejci03b71f72016-03-16 11:10:09 +01005075 assert(unres);
Radek Krejci8ee1b562016-03-31 10:58:31 +02005076 assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL));
Radek Krejci03b71f72016-03-16 11:10:09 +01005077
5078 if (!unres->count) {
5079 return EXIT_SUCCESS;
5080 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005081
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005082 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01005083 ly_vlog_hide(1);
5084
Radek Krejci0b7704f2016-03-18 12:16:14 +01005085 /* when-stmt first */
Radek Krejci03b71f72016-03-16 11:10:09 +01005086 ly_errno = LY_SUCCESS;
5087 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005088 do {
5089 progress = 0;
5090 for(i = 0; i < unres->count; i++) {
5091 if (unres->type[i] != UNRES_WHEN) {
5092 continue;
5093 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005094 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005095 /* count when-stmt nodes in unres list */
5096 when_stmt++;
5097 }
5098
5099 /* resolve when condition only when all parent when conditions are already resolved */
5100 for (parent = unres->node[i]->parent;
5101 parent && LYD_WHEN_DONE(parent->when_status);
5102 parent = parent->parent) {
5103 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
5104 /* the parent node was already unlinked, do not resolve this node,
5105 * it will be removed anyway, so just mark it as resolved
5106 */
5107 unres->node[i]->when_status |= LYD_WHEN_FALSE;
5108 unres->type[i] = UNRES_RESOLVED;
5109 resolved++;
5110 break;
5111 }
5112 }
5113 if (parent) {
5114 continue;
5115 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005116
Radek Krejci48464ed2016-03-17 15:44:09 +01005117 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005118 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005119 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
5120 if (!root) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005121 /* false when condition */
5122 ly_vlog_hide(0);
5123 path = strdup(ly_errpath());
5124 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "",
5125 path[0] ? path : "", path[0] ? ")" : "");
5126 free(path);
5127 free(msg);
5128 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005129 } /* follows else */
5130
Radek Krejci0c0086a2016-03-24 15:20:28 +01005131 /* only unlink now, the subtree can contain another nodes stored in the unres list */
5132 /* if it has parent non-presence containers that would be empty, we should actually
5133 * remove the container
5134 */
5135 if (!(options & LYD_OPT_KEEPEMPTYCONT)) {
5136 for (parent = unres->node[i];
5137 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
5138 parent = parent->parent) {
5139 if (((struct lys_node_container *)parent->parent->schema)->presence) {
5140 /* presence container */
5141 break;
5142 }
5143 if (parent->next || parent->prev != parent) {
5144 /* non empty (the child we are in and we are going to remove is not the only child) */
5145 break;
5146 }
5147 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005148 unres->node[i] = parent;
5149 }
5150
Radek Krejci0b7704f2016-03-18 12:16:14 +01005151 /* auto-delete */
5152 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
5153 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
Radek Krejci0c0086a2016-03-24 15:20:28 +01005154 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005155 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01005156 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005157
Radek Krejci0b7704f2016-03-18 12:16:14 +01005158 lyd_unlink(unres->node[i]);
5159 unres->type[i] = UNRES_DELETE;
5160 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01005161
5162 /* update the rest of unres items */
5163 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01005164 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01005165 continue;
5166 }
5167
5168 /* test if the node is in subtree to be deleted */
5169 for (parent = unres->node[j]; parent; parent = parent->parent) {
5170 if (parent == unres->node[i]) {
5171 /* yes, it is */
5172 unres->type[j] = UNRES_RESOLVED;
5173 resolved++;
5174 break;
5175 }
5176 }
5177 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005178 } else {
5179 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01005180 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01005181 ly_errno = LY_SUCCESS;
5182 ly_vecode = LYVE_SUCCESS;
Radek Krejci010e54b2016-03-15 09:40:34 +01005183 resolved++;
5184 progress = 1;
5185 } else if (rc == -1) {
5186 ly_vlog_hide(0);
5187 return -1;
5188 }
5189 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005190 first = 0;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005191 } while (progress && resolved < when_stmt);
Radek Krejci010e54b2016-03-15 09:40:34 +01005192
Radek Krejci0b7704f2016-03-18 12:16:14 +01005193 /* do we have some unresolved when-stmt? */
Radek Krejcid940d732016-03-24 16:02:28 +01005194 if (when_stmt > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005195 ly_vlog_hide(0);
5196 path = strdup(ly_errpath());
5197 LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "",
5198 path[0] ? path : "", path[0] ? ")" : "");
5199 free(path);
5200 free(msg);
5201 return -1;
5202 }
5203
5204 for (i = 0; del_items && i < unres->count; i++) {
5205 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
5206 if (unres->type[i] != UNRES_DELETE) {
5207 continue;
5208 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01005209 if (!unres->node[i]) {
5210 unres->type[i] = UNRES_RESOLVED;
5211 del_items--;
5212 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01005213 }
5214
5215 /* really remove the complete subtree */
5216 lyd_free(unres->node[i]);
5217 unres->type[i] = UNRES_RESOLVED;
5218 del_items--;
5219 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005220
5221 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005222 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005223 if (unres->type[i] == UNRES_RESOLVED) {
5224 continue;
5225 }
5226
Radek Krejci48464ed2016-03-17 15:44:09 +01005227 rc = resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005228 if (rc == 0) {
5229 unres->type[i] = UNRES_RESOLVED;
5230 resolved++;
5231 } else if (rc == -1) {
5232 ly_vlog_hide(0);
Michal Vasko96b846c2016-05-18 13:28:58 +02005233 /* print only this last error */
5234 resolve_unres_data_item(unres->node[i], unres->type[i]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005235 return -1;
5236 }
5237 }
5238
Radek Krejci010e54b2016-03-15 09:40:34 +01005239 ly_vlog_hide(0);
5240 if (resolved < unres->count) {
5241 /* try to resolve the unresolved data again, it will not resolve anything, but it will print
5242 * all the validation errors
5243 */
5244 for (i = 0; i < unres->count; ++i) {
5245 if (unres->type[i] == UNRES_RESOLVED) {
5246 continue;
5247 }
Radek Krejci48464ed2016-03-17 15:44:09 +01005248 resolve_unres_data_item(unres->node[i], unres->type[i]);
Radek Krejci010e54b2016-03-15 09:40:34 +01005249 }
5250 return -1;
5251 }
5252
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02005253 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01005254 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005255 return EXIT_SUCCESS;
5256}