blob: a9ea9ea10b9f165671ae03b5aaf98a5b1deea046 [file] [log] [blame]
Michal Vasko730dfdf2015-08-11 14:48:05 +02001/**
2 * @file resolve.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang resolve functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko730dfdf2015-08-11 14:48:05 +020013 */
14
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
16
17#include <stdlib.h>
18#include <assert.h>
19#include <string.h>
20#include <ctype.h>
Michal Vaskoe7fc19c2015-08-05 16:24:39 +020021#include <limits.h>
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020022
23#include "libyang.h"
24#include "resolve.h"
25#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020026#include "xpath.h"
Michal Vasko1dca6882015-10-22 14:29:42 +020027#include "parser.h"
Pavol Vicana0e4e672016-02-24 12:20:04 +010028#include "parser_yang.h"
Michal Vasko88c29542015-11-27 14:57:53 +010029#include "xml_internal.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020030#include "dict_private.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020031#include "tree_internal.h"
Radek Krejcie534c132016-11-23 13:32:31 +010032#include "extensions.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020033
Michal Vaskod24dd012016-09-30 12:20:22 +020034int
35parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num)
Michal Vasko4d1f0482016-09-19 14:35:06 +020036{
37 const char *ptr;
38 int minus = 0;
39 int64_t ret = 0;
Radek Krejcibf47a822016-11-04 10:06:08 +010040 int8_t str_exp, str_dig = -1, trailing_zeros = 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +020041
42 ptr = *str_num;
43
44 if (ptr[0] == '-') {
45 minus = 1;
46 ++ptr;
Radek Krejci51673202016-11-01 17:00:32 +010047 } else if (ptr[0] == '+') {
48 ++ptr;
Michal Vasko4d1f0482016-09-19 14:35:06 +020049 }
50
Michal Vaskod24dd012016-09-30 12:20:22 +020051 if (!isdigit(ptr[0])) {
52 /* there must be at least one */
53 return 1;
54 }
55
Michal Vasko4d1f0482016-09-19 14:35:06 +020056 for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) {
57 if (str_exp > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020058 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020059 }
60
61 if (ptr[0] == '.') {
62 if (ptr[1] == '.') {
63 /* it's the next interval */
64 break;
65 }
66 ++str_dig;
67 } else {
Radek Krejcibf47a822016-11-04 10:06:08 +010068 ret = ret * 10 + (ptr[0] - '0');
Michal Vasko4d1f0482016-09-19 14:35:06 +020069 if (str_dig > -1) {
70 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010071 if (ptr[0] == '0') {
72 /* possibly trailing zero */
73 trailing_zeros++;
74 } else {
75 trailing_zeros = 0;
76 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020077 }
78 ++str_exp;
79 }
80 }
Michal Vaskod24dd012016-09-30 12:20:22 +020081 if (str_dig == 0) {
82 /* no digits after '.' */
83 return 1;
84 } else if (str_dig == -1) {
85 /* there are 0 numbers after the floating point */
Michal Vasko4d1f0482016-09-19 14:35:06 +020086 str_dig = 0;
87 }
Radek Krejcibf47a822016-11-04 10:06:08 +010088 /* remove trailing zeros */
89 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +010090 str_dig -= trailing_zeros;
91 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +010092 ret = ret / dec_pow(trailing_zeros);
93 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020094
95 /* it's parsed, now adjust the number based on fraction-digits, if needed */
96 if (str_dig < dig) {
97 if ((str_exp - 1) + (dig - str_dig) > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020098 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020099 }
100 ret *= dec_pow(dig - str_dig);
101 }
102 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200103 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200104 }
105
106 if (minus) {
107 ret *= -1;
108 }
109 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200110 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200111
Michal Vaskod24dd012016-09-30 12:20:22 +0200112 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200113}
114
115/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200116 * @brief Parse an identifier.
117 *
118 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
119 * identifier = (ALPHA / "_")
120 * *(ALPHA / DIGIT / "_" / "-" / ".")
121 *
Michal Vaskobb211122015-08-19 14:03:11 +0200122 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200123 *
124 * @return Number of characters successfully parsed.
125 */
Michal Vasko249e6b52015-08-19 11:08:52 +0200126int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200127parse_identifier(const char *id)
128{
129 int parsed = 0;
130
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100131 assert(id);
132
Radek Krejci6dc53a22015-08-17 13:27:59 +0200133 if (!isalpha(id[0]) && (id[0] != '_')) {
134 return -parsed;
135 }
136
137 ++parsed;
138 ++id;
139
140 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
141 ++parsed;
142 ++id;
143 }
144
145 return parsed;
146}
147
148/**
149 * @brief Parse a node-identifier.
150 *
Michal Vasko723e50c2015-10-20 15:20:29 +0200151 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200152 *
Michal Vaskobb211122015-08-19 14:03:11 +0200153 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200154 * @param[out] mod_name Points to the module name, NULL if there is not any.
155 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200156 * @param[out] name Points to the node name.
157 * @param[out] nam_len Length of the node name.
158 *
159 * @return Number of characters successfully parsed,
160 * positive on success, negative on failure.
161 */
162static int
Michal Vasko723e50c2015-10-20 15:20:29 +0200163parse_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 +0200164{
165 int parsed = 0, ret;
166
167 assert(id);
Michal Vasko723e50c2015-10-20 15:20:29 +0200168 if (mod_name) {
169 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200170 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200171 if (mod_name_len) {
172 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200173 }
174 if (name) {
175 *name = NULL;
176 }
177 if (nam_len) {
178 *nam_len = 0;
179 }
180
181 if ((ret = parse_identifier(id)) < 1) {
182 return ret;
183 }
184
Michal Vasko723e50c2015-10-20 15:20:29 +0200185 if (mod_name) {
186 *mod_name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200187 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200188 if (mod_name_len) {
189 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200190 }
191
192 parsed += ret;
193 id += ret;
194
195 /* there is prefix */
196 if (id[0] == ':') {
197 ++parsed;
198 ++id;
199
200 /* there isn't */
201 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200202 if (name && mod_name) {
203 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200204 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200205 if (mod_name) {
206 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200207 }
208
Michal Vasko723e50c2015-10-20 15:20:29 +0200209 if (nam_len && mod_name_len) {
210 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200211 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200212 if (mod_name_len) {
213 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200214 }
215
216 return parsed;
217 }
218
219 /* identifier (node name) */
220 if ((ret = parse_identifier(id)) < 1) {
221 return -parsed+ret;
222 }
223
224 if (name) {
225 *name = id;
226 }
227 if (nam_len) {
228 *nam_len = ret;
229 }
230
231 return parsed+ret;
232}
233
234/**
235 * @brief Parse a path-predicate (leafref).
236 *
237 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
238 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
239 *
Michal Vaskobb211122015-08-19 14:03:11 +0200240 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200241 * @param[out] prefix Points to the prefix, NULL if there is not any.
242 * @param[out] pref_len Length of the prefix, 0 if there is not any.
243 * @param[out] name Points to the node name.
244 * @param[out] nam_len Length of the node name.
245 * @param[out] path_key_expr Points to the path-key-expr.
246 * @param[out] pke_len Length of the path-key-expr.
247 * @param[out] has_predicate Flag to mark whether there is another predicate following.
248 *
249 * @return Number of characters successfully parsed,
250 * positive on success, negative on failure.
251 */
252static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200253parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
254 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200255{
256 const char *ptr;
257 int parsed = 0, ret;
258
259 assert(id);
260 if (prefix) {
261 *prefix = NULL;
262 }
263 if (pref_len) {
264 *pref_len = 0;
265 }
266 if (name) {
267 *name = NULL;
268 }
269 if (nam_len) {
270 *nam_len = 0;
271 }
272 if (path_key_expr) {
273 *path_key_expr = NULL;
274 }
275 if (pke_len) {
276 *pke_len = 0;
277 }
278 if (has_predicate) {
279 *has_predicate = 0;
280 }
281
282 if (id[0] != '[') {
283 return -parsed;
284 }
285
286 ++parsed;
287 ++id;
288
289 while (isspace(id[0])) {
290 ++parsed;
291 ++id;
292 }
293
294 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
295 return -parsed+ret;
296 }
297
298 parsed += ret;
299 id += ret;
300
301 while (isspace(id[0])) {
302 ++parsed;
303 ++id;
304 }
305
306 if (id[0] != '=') {
307 return -parsed;
308 }
309
310 ++parsed;
311 ++id;
312
313 while (isspace(id[0])) {
314 ++parsed;
315 ++id;
316 }
317
318 if ((ptr = strchr(id, ']')) == NULL) {
319 return -parsed;
320 }
321
322 --ptr;
323 while (isspace(ptr[0])) {
324 --ptr;
325 }
326 ++ptr;
327
328 ret = ptr-id;
329 if (path_key_expr) {
330 *path_key_expr = id;
331 }
332 if (pke_len) {
333 *pke_len = ret;
334 }
335
336 parsed += ret;
337 id += ret;
338
339 while (isspace(id[0])) {
340 ++parsed;
341 ++id;
342 }
343
344 assert(id[0] == ']');
345
346 if (id[1] == '[') {
347 *has_predicate = 1;
348 }
349
350 return parsed+1;
351}
352
353/**
354 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
355 * the ".." and the first node-identifier, other calls parse a single
356 * node-identifier each.
357 *
358 * path-key-expr = current-function-invocation *WSP "/" *WSP
359 * rel-path-keyexpr
360 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
361 * *(node-identifier *WSP "/" *WSP)
362 * node-identifier
363 *
Michal Vaskobb211122015-08-19 14:03:11 +0200364 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200365 * @param[out] prefix Points to the prefix, NULL if there is not any.
366 * @param[out] pref_len Length of the prefix, 0 if there is not any.
367 * @param[out] name Points to the node name.
368 * @param[out] nam_len Length of the node name.
369 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
370 * must not be changed between consecutive calls.
371 * @return Number of characters successfully parsed,
372 * positive on success, negative on failure.
373 */
374static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200375parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
376 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200377{
378 int parsed = 0, ret, par_times = 0;
379
380 assert(id);
381 assert(parent_times);
382 if (prefix) {
383 *prefix = NULL;
384 }
385 if (pref_len) {
386 *pref_len = 0;
387 }
388 if (name) {
389 *name = NULL;
390 }
391 if (nam_len) {
392 *nam_len = 0;
393 }
394
395 if (!*parent_times) {
396 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
397 if (strncmp(id, "current()", 9)) {
398 return -parsed;
399 }
400
401 parsed += 9;
402 id += 9;
403
404 while (isspace(id[0])) {
405 ++parsed;
406 ++id;
407 }
408
409 if (id[0] != '/') {
410 return -parsed;
411 }
412
413 ++parsed;
414 ++id;
415
416 while (isspace(id[0])) {
417 ++parsed;
418 ++id;
419 }
420
421 /* rel-path-keyexpr */
422 if (strncmp(id, "..", 2)) {
423 return -parsed;
424 }
425 ++par_times;
426
427 parsed += 2;
428 id += 2;
429
430 while (isspace(id[0])) {
431 ++parsed;
432 ++id;
433 }
434 }
435
436 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
437 *
438 * first parent reference with whitespaces already parsed
439 */
440 if (id[0] != '/') {
441 return -parsed;
442 }
443
444 ++parsed;
445 ++id;
446
447 while (isspace(id[0])) {
448 ++parsed;
449 ++id;
450 }
451
452 while (!strncmp(id, "..", 2) && !*parent_times) {
453 ++par_times;
454
455 parsed += 2;
456 id += 2;
457
458 while (isspace(id[0])) {
459 ++parsed;
460 ++id;
461 }
462
463 if (id[0] != '/') {
464 return -parsed;
465 }
466
467 ++parsed;
468 ++id;
469
470 while (isspace(id[0])) {
471 ++parsed;
472 ++id;
473 }
474 }
475
476 if (!*parent_times) {
477 *parent_times = par_times;
478 }
479
480 /* all parent references must be parsed at this point */
481 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
482 return -parsed+ret;
483 }
484
485 parsed += ret;
486 id += ret;
487
488 return parsed;
489}
490
491/**
492 * @brief Parse path-arg (leafref).
493 *
494 * path-arg = absolute-path / relative-path
495 * absolute-path = 1*("/" (node-identifier *path-predicate))
496 * relative-path = 1*(".." "/") descendant-path
497 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200498 * @param[in] mod Module of the context node to get correct prefix in case it is not explicitly specified
Michal Vaskobb211122015-08-19 14:03:11 +0200499 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200500 * @param[out] prefix Points to the prefix, NULL if there is not any.
501 * @param[out] pref_len Length of the prefix, 0 if there is not any.
502 * @param[out] name Points to the node name.
503 * @param[out] nam_len Length of the node name.
504 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
505 * must not be changed between consecutive calls. -1 if the
506 * path is relative.
507 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
508 *
509 * @return Number of characters successfully parsed,
510 * positive on success, negative on failure.
511 */
512static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200513parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200514 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200515{
516 int parsed = 0, ret, par_times = 0;
517
518 assert(id);
519 assert(parent_times);
520 if (prefix) {
521 *prefix = NULL;
522 }
523 if (pref_len) {
524 *pref_len = 0;
525 }
526 if (name) {
527 *name = NULL;
528 }
529 if (nam_len) {
530 *nam_len = 0;
531 }
532 if (has_predicate) {
533 *has_predicate = 0;
534 }
535
536 if (!*parent_times && !strncmp(id, "..", 2)) {
537 ++par_times;
538
539 parsed += 2;
540 id += 2;
541
542 while (!strncmp(id, "/..", 3)) {
543 ++par_times;
544
545 parsed += 3;
546 id += 3;
547 }
548 }
549
550 if (!*parent_times) {
551 if (par_times) {
552 *parent_times = par_times;
553 } else {
554 *parent_times = -1;
555 }
556 }
557
558 if (id[0] != '/') {
559 return -parsed;
560 }
561
562 /* skip '/' */
563 ++parsed;
564 ++id;
565
566 /* node-identifier ([prefix:]identifier) */
567 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) {
568 return -parsed-ret;
569 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200570 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200571 /* actually we always need prefix even it is not specified */
572 *prefix = lys_main_module(mod)->name;
573 *pref_len = strlen(*prefix);
574 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200575
576 parsed += ret;
577 id += ret;
578
579 /* there is no predicate */
580 if ((id[0] == '/') || !id[0]) {
581 return parsed;
582 } else if (id[0] != '[') {
583 return -parsed;
584 }
585
586 if (has_predicate) {
587 *has_predicate = 1;
588 }
589
590 return parsed;
591}
592
593/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200594 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vaskob2f40be2016-09-08 16:03:48 +0200595 * (which are mandatory for every node-identifier) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200596 *
597 * instance-identifier = 1*("/" (node-identifier *predicate))
598 *
Michal Vaskobb211122015-08-19 14:03:11 +0200599 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200600 * @param[out] model Points to the model name.
601 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200602 * @param[out] name Points to the node name.
603 * @param[out] nam_len Length of the node name.
604 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
605 *
606 * @return Number of characters successfully parsed,
607 * positive on success, negative on failure.
608 */
609static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200610parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
611 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200612{
613 int parsed = 0, ret;
614
Radek Krejci6dc53a22015-08-17 13:27:59 +0200615 if (has_predicate) {
616 *has_predicate = 0;
617 }
618
619 if (id[0] != '/') {
620 return -parsed;
621 }
622
623 ++parsed;
624 ++id;
625
Michal Vaskob2f40be2016-09-08 16:03:48 +0200626 if ((ret = parse_identifier(id)) < 1) {
627 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200628 }
629
Michal Vaskob2f40be2016-09-08 16:03:48 +0200630 *model = id;
631 *mod_len = ret;
632
Radek Krejci6dc53a22015-08-17 13:27:59 +0200633 parsed += ret;
634 id += ret;
635
Michal Vaskob2f40be2016-09-08 16:03:48 +0200636 if (id[0] != ':') {
637 return -parsed;
638 }
639
640 ++parsed;
641 ++id;
642
643 if ((ret = parse_identifier(id)) < 1) {
644 return ret;
645 }
646
647 *name = id;
648 *nam_len = ret;
649
650 parsed += ret;
651 id += ret;
652
Radek Krejci4967cb62016-09-14 16:40:28 +0200653 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200654 *has_predicate = 1;
655 }
656
657 return parsed;
658}
659
660/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200661 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200662 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200663 *
664 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
665 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
666 * ((DQUOTE string DQUOTE) /
667 * (SQUOTE string SQUOTE))
668 * pos = non-negative-integer-value
669 *
Michal Vaskobb211122015-08-19 14:03:11 +0200670 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200671 * @param[out] model Points to the model name.
672 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200673 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
674 * @param[out] nam_len Length of the node name.
675 * @param[out] value Value the node-identifier must have (string from the grammar),
676 * NULL if there is not any.
677 * @param[out] val_len Length of the value, 0 if there is not any.
678 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
679 *
680 * @return Number of characters successfully parsed,
681 * positive on success, negative on failure.
682 */
683static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200684parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
685 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200686{
687 const char *ptr;
688 int parsed = 0, ret;
689 char quote;
690
691 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200692 if (model) {
693 *model = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200694 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200695 if (mod_len) {
696 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200697 }
698 if (name) {
699 *name = NULL;
700 }
701 if (nam_len) {
702 *nam_len = 0;
703 }
704 if (value) {
705 *value = NULL;
706 }
707 if (val_len) {
708 *val_len = 0;
709 }
710 if (has_predicate) {
711 *has_predicate = 0;
712 }
713
714 if (id[0] != '[') {
715 return -parsed;
716 }
717
718 ++parsed;
719 ++id;
720
721 while (isspace(id[0])) {
722 ++parsed;
723 ++id;
724 }
725
726 /* pos */
727 if (isdigit(id[0])) {
728 if (name) {
729 *name = id;
730 }
731
732 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200733 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200734 }
735
736 while (isdigit(id[0])) {
737 ++parsed;
738 ++id;
739 }
740
741 if (nam_len) {
742 *nam_len = id-(*name);
743 }
744
Michal Vaskof2f28a12016-09-09 12:43:06 +0200745 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200746 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200747 if (id[0] == '.') {
748 if (name) {
749 *name = id;
750 }
751 if (nam_len) {
752 *nam_len = 1;
753 }
754
755 ++parsed;
756 ++id;
757
758 } else {
759 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) {
760 return -parsed+ret;
761 } else if (model && !*model) {
762 return -parsed;
763 }
764
765 parsed += ret;
766 id += ret;
767 }
768
769 while (isspace(id[0])) {
770 ++parsed;
771 ++id;
772 }
773
774 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200775 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200776 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200777
Radek Krejci6dc53a22015-08-17 13:27:59 +0200778 ++parsed;
779 ++id;
780
Michal Vaskof2f28a12016-09-09 12:43:06 +0200781 while (isspace(id[0])) {
782 ++parsed;
783 ++id;
784 }
785
786 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
787 if ((id[0] == '\"') || (id[0] == '\'')) {
788 quote = id[0];
789
790 ++parsed;
791 ++id;
792
793 if ((ptr = strchr(id, quote)) == NULL) {
794 return -parsed;
795 }
796 ret = ptr-id;
797
798 if (value) {
799 *value = id;
800 }
801 if (val_len) {
802 *val_len = ret;
803 }
804
805 parsed += ret+1;
806 id += ret+1;
807 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200808 return -parsed;
809 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200810 }
811
812 while (isspace(id[0])) {
813 ++parsed;
814 ++id;
815 }
816
817 if (id[0] != ']') {
818 return -parsed;
819 }
820
821 ++parsed;
822 ++id;
823
824 if ((id[0] == '[') && has_predicate) {
825 *has_predicate = 1;
826 }
827
828 return parsed;
829}
830
831/**
832 * @brief Parse schema-nodeid.
833 *
834 * schema-nodeid = absolute-schema-nodeid /
835 * descendant-schema-nodeid
836 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200837 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200838 * node-identifier
839 * absolute-schema-nodeid
840 *
Michal Vaskobb211122015-08-19 14:03:11 +0200841 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200842 * @param[out] mod_name Points to the module name, NULL if there is not any.
843 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200844 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200845 * @param[out] nam_len Length of the node name.
846 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
847 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100848 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
849 * based on the grammar, in those cases use NULL.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200850 *
851 * @return Number of characters successfully parsed,
852 * positive on success, negative on failure.
853 */
Michal Vasko22448d32016-03-16 13:17:29 +0100854int
Michal Vasko723e50c2015-10-20 15:20:29 +0200855parse_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 +0100856 int *is_relative, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200857{
858 int parsed = 0, ret;
859
860 assert(id);
861 assert(is_relative);
Michal Vasko723e50c2015-10-20 15:20:29 +0200862 if (mod_name) {
863 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200864 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200865 if (mod_name_len) {
866 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200867 }
868 if (name) {
869 *name = NULL;
870 }
871 if (nam_len) {
872 *nam_len = 0;
873 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100874 if (has_predicate) {
875 *has_predicate = 0;
876 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200877
878 if (id[0] != '/') {
879 if (*is_relative != -1) {
880 return -parsed;
881 } else {
882 *is_relative = 1;
883 }
Michal Vasko48935352016-03-29 11:52:36 +0200884 if (!strncmp(id, "./", 2)) {
885 parsed += 2;
886 id += 2;
887 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200888 } else {
889 if (*is_relative == -1) {
890 *is_relative = 0;
891 }
892 ++parsed;
893 ++id;
894 }
895
Michal Vasko723e50c2015-10-20 15:20:29 +0200896 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200897 return -parsed+ret;
898 }
899
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100900 parsed += ret;
901 id += ret;
902
903 if ((id[0] == '[') && has_predicate) {
904 *has_predicate = 1;
905 }
906
907 return parsed;
908}
909
910/**
911 * @brief Parse schema predicate (special format internally used).
912 *
913 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vaskof359b022017-07-04 13:50:04 +0200914 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100915 * key-with-value = identifier *WSP "=" *WSP
916 * ((DQUOTE string DQUOTE) /
917 * (SQUOTE string SQUOTE))
918 *
919 * @param[in] id Identifier to use.
Michal Vaskof359b022017-07-04 13:50:04 +0200920 * @param[out] mod_name Points to the list key module name.
921 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100922 * @param[out] name Points to the list key name.
923 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100924 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100925 * @param[out] val_len Length of \p value.
926 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
927 */
Michal Vasko22448d32016-03-16 13:17:29 +0100928int
Michal Vaskof359b022017-07-04 13:50:04 +0200929parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
930 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100931{
932 const char *ptr;
933 int parsed = 0, ret;
934 char quote;
935
936 assert(id);
Michal Vaskof359b022017-07-04 13:50:04 +0200937 if (mod_name) {
938 *mod_name = NULL;
939 }
940 if (mod_name_len) {
941 *mod_name_len = 0;
942 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100943 if (name) {
944 *name = NULL;
945 }
946 if (nam_len) {
947 *nam_len = 0;
948 }
949 if (value) {
950 *value = NULL;
951 }
952 if (val_len) {
953 *val_len = 0;
954 }
955 if (has_predicate) {
956 *has_predicate = 0;
957 }
958
959 if (id[0] != '[') {
960 return -parsed;
961 }
962
963 ++parsed;
964 ++id;
965
966 while (isspace(id[0])) {
967 ++parsed;
968 ++id;
969 }
970
Michal Vasko22448d32016-03-16 13:17:29 +0100971 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200972 if (id[0] == '.') {
973 ret = 1;
Michal Vaskof359b022017-07-04 13:50:04 +0200974
975 if (name) {
976 *name = id;
977 }
978 if (nam_len) {
979 *nam_len = ret;
980 }
Michal Vasko58c2aab2017-01-05 10:02:05 +0100981 } else if (isdigit(id[0])) {
982 if (id[0] == '0') {
983 return -parsed;
984 }
985 ret = 1;
986 while (isdigit(id[ret])) {
987 ++ret;
988 }
Michal Vaskof359b022017-07-04 13:50:04 +0200989
990 if (name) {
991 *name = id;
992 }
993 if (nam_len) {
994 *nam_len = ret;
995 }
996 } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100997 return -parsed + ret;
998 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100999
1000 parsed += ret;
1001 id += ret;
1002
1003 while (isspace(id[0])) {
1004 ++parsed;
1005 ++id;
1006 }
1007
1008 /* there is value as well */
1009 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001010 if (name && isdigit(**name)) {
1011 return -parsed;
1012 }
1013
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001014 ++parsed;
1015 ++id;
1016
1017 while (isspace(id[0])) {
1018 ++parsed;
1019 ++id;
1020 }
1021
1022 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1023 if ((id[0] == '\"') || (id[0] == '\'')) {
1024 quote = id[0];
1025
1026 ++parsed;
1027 ++id;
1028
1029 if ((ptr = strchr(id, quote)) == NULL) {
1030 return -parsed;
1031 }
1032 ret = ptr - id;
1033
1034 if (value) {
1035 *value = id;
1036 }
1037 if (val_len) {
1038 *val_len = ret;
1039 }
1040
1041 parsed += ret + 1;
1042 id += ret + 1;
1043 } else {
1044 return -parsed;
1045 }
1046
1047 while (isspace(id[0])) {
1048 ++parsed;
1049 ++id;
1050 }
1051 }
1052
1053 if (id[0] != ']') {
1054 return -parsed;
1055 }
1056
1057 ++parsed;
1058 ++id;
1059
1060 if ((id[0] == '[') && has_predicate) {
1061 *has_predicate = 1;
1062 }
1063
1064 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001065}
1066
1067/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001068 * @brief Resolve (find) a feature definition. Logs directly.
1069 *
1070 * @param[in] feat_name Feature name to resolve.
1071 * @param[in] len Length of \p feat_name.
1072 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001073 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1074 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001075 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001076 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001077 */
1078static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001079resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001080{
1081 char *str;
1082 const char *mod_name, *name;
1083 int mod_name_len, nam_len, i, j;
1084 const struct lys_module *module;
1085
Radek Krejci9ff0a922016-07-14 13:08:05 +02001086 assert(feature);
1087
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001088 /* check prefix */
1089 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
1090 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
1091 return -1;
1092 }
1093
1094 module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len);
1095 if (!module) {
1096 /* identity refers unknown data model */
1097 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
1098 return -1;
1099 }
1100
Radek Krejci9ff0a922016-07-14 13:08:05 +02001101 if (module != node->module && module == lys_node_module(node)) {
1102 /* first, try to search directly in submodule where the feature was mentioned */
1103 for (j = 0; j < node->module->features_size; j++) {
1104 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1105 /* check status */
1106 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001107 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001108 return -1;
1109 }
1110 *feature = &node->module->features[j];
1111 return 0;
1112 }
1113 }
1114 }
1115
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001116 /* search in the identified module ... */
1117 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001118 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001119 /* check status */
1120 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001121 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001122 return -1;
1123 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001124 *feature = &module->features[j];
1125 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001126 }
1127 }
1128 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001129 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001130 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001131 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1132 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001133 /* check status */
1134 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1135 module->inc[i].submodule->features[j].flags,
1136 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001137 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001138 return -1;
1139 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001140 *feature = &module->inc[i].submodule->features[j];
1141 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001142 }
1143 }
1144 }
1145
1146 /* not found */
1147 str = strndup(feat_name, len);
1148 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
1149 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001150 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001151}
1152
Radek Krejci9ff0a922016-07-14 13:08:05 +02001153/*
1154 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001155 * - 1 if enabled
1156 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001157 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001158static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001159resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001160{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001161 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001162
Radek Krejci9ff0a922016-07-14 13:08:05 +02001163 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001164 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001165 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001166 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001167 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001168
Radek Krejci69b8d922016-07-27 13:13:41 +02001169 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001170}
1171
1172static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001173resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001174{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001175 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001176 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001177
Radek Krejci9ff0a922016-07-14 13:08:05 +02001178 op = iff_getop(expr->expr, *index_e);
1179 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001180
Radek Krejci9ff0a922016-07-14 13:08:05 +02001181 switch (op) {
1182 case LYS_IFF_F:
1183 /* resolve feature */
1184 return resolve_feature_value(expr->features[(*index_f)++]);
1185 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001186 /* invert result */
1187 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001188 case LYS_IFF_AND:
1189 case LYS_IFF_OR:
1190 a = resolve_iffeature_recursive(expr, index_e, index_f);
1191 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001192 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001193 return a && b;
1194 } else { /* LYS_IFF_OR */
1195 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001196 }
1197 }
1198
Radek Krejciaf566332017-02-07 15:56:59 +01001199 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001200}
1201
1202int
1203resolve_iffeature(struct lys_iffeature *expr)
1204{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001205 int index_e = 0, index_f = 0;
1206
1207 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001208 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001209 }
Radek Krejciaf566332017-02-07 15:56:59 +01001210 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001211}
1212
1213struct iff_stack {
1214 int size;
1215 int index; /* first empty item */
1216 uint8_t *stack;
1217};
1218
1219static int
1220iff_stack_push(struct iff_stack *stack, uint8_t value)
1221{
1222 if (stack->index == stack->size) {
1223 stack->size += 4;
1224 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001225 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM; stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001226 }
1227
1228 stack->stack[stack->index++] = value;
1229 return EXIT_SUCCESS;
1230}
1231
1232static uint8_t
1233iff_stack_pop(struct iff_stack *stack)
1234{
1235 stack->index--;
1236 return stack->stack[stack->index];
1237}
1238
1239static void
1240iff_stack_clean(struct iff_stack *stack)
1241{
1242 stack->size = 0;
1243 free(stack->stack);
1244}
1245
1246static void
1247iff_setop(uint8_t *list, uint8_t op, int pos)
1248{
1249 uint8_t *item;
1250 uint8_t mask = 3;
1251
1252 assert(pos >= 0);
1253 assert(op <= 3); /* max 2 bits */
1254
1255 item = &list[pos / 4];
1256 mask = mask << 2 * (pos % 4);
1257 *item = (*item) & ~mask;
1258 *item = (*item) | (op << 2 * (pos % 4));
1259}
1260
1261uint8_t
1262iff_getop(uint8_t *list, int pos)
1263{
1264 uint8_t *item;
1265 uint8_t mask = 3, result;
1266
1267 assert(pos >= 0);
1268
1269 item = &list[pos / 4];
1270 result = (*item) & (mask << 2 * (pos % 4));
1271 return result >> 2 * (pos % 4);
1272}
1273
1274#define LYS_IFF_LP 0x04 /* ( */
1275#define LYS_IFF_RP 0x08 /* ) */
1276
Radek Krejcicbb473e2016-09-16 14:48:32 +02001277/* internal structure for passing data for UNRES_IFFEAT */
1278struct unres_iffeat_data {
1279 struct lys_node *node;
1280 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001281 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001282};
1283
Radek Krejci9ff0a922016-07-14 13:08:05 +02001284void
1285resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1286{
1287 unsigned int e = 0, f = 0, r = 0;
1288 uint8_t op;
1289
1290 assert(iffeat);
1291
1292 if (!iffeat->expr) {
1293 goto result;
1294 }
1295
1296 do {
1297 op = iff_getop(iffeat->expr, e++);
1298 switch (op) {
1299 case LYS_IFF_NOT:
1300 if (!r) {
1301 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001302 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001303 break;
1304 case LYS_IFF_AND:
1305 case LYS_IFF_OR:
1306 if (!r) {
1307 r += 2;
1308 } else {
1309 r += 1;
1310 }
1311 break;
1312 case LYS_IFF_F:
1313 f++;
1314 if (r) {
1315 r--;
1316 }
1317 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001318 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001319 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001320
Radek Krejci9ff0a922016-07-14 13:08:05 +02001321result:
1322 if (expr_size) {
1323 *expr_size = e;
1324 }
1325 if (feat_size) {
1326 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001327 }
1328}
1329
1330int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001331resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001332 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001333{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001334 const char *c = value;
1335 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001336 int i, j, last_not, checkversion = 0;
1337 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001338 uint8_t op;
1339 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001340 struct unres_iffeat_data *iff_data;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001341
Radek Krejci9ff0a922016-07-14 13:08:05 +02001342 assert(c);
1343
1344 if (isspace(c[0])) {
1345 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
1346 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001347 }
1348
Radek Krejci9ff0a922016-07-14 13:08:05 +02001349 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1350 for (i = j = last_not = 0; c[i]; i++) {
1351 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001352 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001353 j++;
1354 continue;
1355 } else if (c[i] == ')') {
1356 j--;
1357 continue;
1358 } else if (isspace(c[i])) {
1359 continue;
1360 }
1361
1362 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1363 if (c[i + r] == '\0') {
Radek Krejcia98da3f2016-07-27 14:05:22 +02001364 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001365 return EXIT_FAILURE;
1366 } else if (!isspace(c[i + r])) {
1367 /* feature name starting with the not/and/or */
1368 last_not = 0;
1369 f_size++;
1370 } else if (c[i] == 'n') { /* not operation */
1371 if (last_not) {
1372 /* double not */
1373 expr_size = expr_size - 2;
1374 last_not = 0;
1375 } else {
1376 last_not = 1;
1377 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001378 } else { /* and, or */
1379 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001380 /* not a not operation */
1381 last_not = 0;
1382 }
1383 i += r;
1384 } else {
1385 f_size++;
1386 last_not = 0;
1387 }
1388 expr_size++;
1389
1390 while (!isspace(c[i])) {
1391 if (!c[i] || c[i] == ')') {
1392 i--;
1393 break;
1394 }
1395 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001396 }
1397 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001398 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001399 /* not matching count of ( and ) */
1400 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1401 return EXIT_FAILURE;
1402 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001403
Radek Krejci69b8d922016-07-27 13:13:41 +02001404 if (checkversion || expr_size > 1) {
1405 /* check that we have 1.1 module */
1406 if (node->module->version != 2) {
1407 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1408 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
1409 return EXIT_FAILURE;
1410 }
1411 }
1412
Radek Krejci9ff0a922016-07-14 13:08:05 +02001413 /* allocate the memory */
1414 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001415 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001416 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001417 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM, error);
1418 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001419 f_size--; expr_size--; /* used as indexes from now */
1420
1421 for (i--; i >= 0; i--) {
1422 if (c[i] == ')') {
1423 /* push it on stack */
1424 iff_stack_push(&stack, LYS_IFF_RP);
1425 continue;
1426 } else if (c[i] == '(') {
1427 /* pop from the stack into result all operators until ) */
1428 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1429 iff_setop(iffeat_expr->expr, op, expr_size--);
1430 }
1431 continue;
1432 } else if (isspace(c[i])) {
1433 continue;
1434 }
1435
1436 /* end operator or operand -> find beginning and get what is it */
1437 j = i + 1;
1438 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1439 i--;
1440 }
1441 i++; /* get back by one step */
1442
1443 if (!strncmp(&c[i], "not ", 4)) {
1444 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1445 /* double not */
1446 iff_stack_pop(&stack);
1447 } else {
1448 /* not has the highest priority, so do not pop from the stack
1449 * as in case of AND and OR */
1450 iff_stack_push(&stack, LYS_IFF_NOT);
1451 }
1452 } else if (!strncmp(&c[i], "and ", 4)) {
1453 /* as for OR - pop from the stack all operators with the same or higher
1454 * priority and store them to the result, then push the AND to the stack */
1455 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1456 op = iff_stack_pop(&stack);
1457 iff_setop(iffeat_expr->expr, op, expr_size--);
1458 }
1459 iff_stack_push(&stack, LYS_IFF_AND);
1460 } else if (!strncmp(&c[i], "or ", 3)) {
1461 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1462 op = iff_stack_pop(&stack);
1463 iff_setop(iffeat_expr->expr, op, expr_size--);
1464 }
1465 iff_stack_push(&stack, LYS_IFF_OR);
1466 } else {
1467 /* feature name, length is j - i */
1468
1469 /* add it to the result */
1470 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1471
1472 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001473 * forward referenced, we have to keep the feature name in auxiliary
1474 * structure passed into unres */
1475 iff_data = malloc(sizeof *iff_data);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001476 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM, error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001477 iff_data->node = node;
1478 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001479 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001480 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1481 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001482 f_size--;
1483
1484 if (r == -1) {
Pavol Vican4d084512016-09-29 16:38:12 +02001485 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001486 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001487 }
1488 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001489 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001490 while (stack.index) {
1491 op = iff_stack_pop(&stack);
1492 iff_setop(iffeat_expr->expr, op, expr_size--);
1493 }
1494
1495 if (++expr_size || ++f_size) {
1496 /* not all expected operators and operands found */
1497 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1498 rc = EXIT_FAILURE;
1499 } else {
1500 rc = EXIT_SUCCESS;
1501 }
1502
1503error:
1504 /* cleanup */
1505 iff_stack_clean(&stack);
1506
1507 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001508}
1509
1510/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001511 * @brief Resolve (find) a data node based on a schema-nodeid.
1512 *
1513 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1514 * module).
1515 *
1516 */
1517struct lyd_node *
1518resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1519{
1520 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001521 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001522 const struct lys_node *schema = NULL;
1523
1524 assert(nodeid && start);
1525
1526 if (nodeid[0] == '/') {
1527 return NULL;
1528 }
1529
1530 str = p = strdup(nodeid);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001531 LY_CHECK_ERR_RETURN(!str, LOGMEM, NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001532
Michal Vasko3edeaf72016-02-11 13:17:43 +01001533 while (p) {
1534 token = p;
1535 p = strchr(p, '/');
1536 if (p) {
1537 *p = '\0';
1538 p++;
1539 }
1540
Radek Krejci5da4eb62016-04-08 14:45:51 +02001541 if (p) {
1542 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001543 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001544 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001545 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001546 result = NULL;
1547 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001548 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001549
Radek Krejci5da4eb62016-04-08 14:45:51 +02001550 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1551 continue;
1552 }
1553 } else {
1554 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001555 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001556 || !schema) {
1557 result = NULL;
1558 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001559 }
1560 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001561 LY_TREE_FOR(result ? result->child : start, iter) {
1562 if (iter->schema == schema) {
1563 /* move in data tree according to returned schema */
1564 result = iter;
1565 break;
1566 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001567 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001568 if (!iter) {
1569 /* instance not found */
1570 result = NULL;
1571 break;
1572 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001573 }
1574 free(str);
1575
1576 return result;
1577}
1578
Radek Krejcibdf92362016-04-08 14:43:34 +02001579/*
1580 * 0 - ok (done)
1581 * 1 - continue
1582 * 2 - break
1583 * -1 - error
1584 */
Radek Krejci1a9c3612017-04-24 14:49:43 +02001585int
Michal Vaskodc300b02017-04-07 14:09:20 +02001586schema_nodeid_siblingcheck(const struct lys_node *sibling, const char *id, const struct lys_module *module,
Michal Vasko45ddfce2017-07-10 15:11:38 +02001587 const char *mod_name, int mod_name_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001588{
1589 const struct lys_module *prefix_mod;
1590
1591 /* module check */
1592 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1593 if (!prefix_mod) {
1594 return -1;
1595 }
1596 if (prefix_mod != lys_node_module(sibling)) {
1597 return 1;
1598 }
1599
Radek Krejcibdf92362016-04-08 14:43:34 +02001600 /* the result node? */
1601 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001602 return 0;
1603 }
1604
Michal Vaskodc300b02017-04-07 14:09:20 +02001605 /* move down the tree, if possible */
1606 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1607 return -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001608 }
1609
1610 return 2;
1611}
1612
Radek Krejcidf46e222016-11-08 11:57:37 +01001613/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1
Radek Krejcidf46e222016-11-08 11:57:37 +01001614 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001615int
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001616resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *cur_module,
Michal Vaskobb520442017-05-23 10:55:18 +02001617 const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001618{
Michal Vaskobb520442017-05-23 10:55:18 +02001619 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001620 const struct lys_node *sibling, *start_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02001621 struct lys_node_augment *last_aug;
1622 int r, nam_len, mod_name_len = 0, is_relative = -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001623 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidf46e222016-11-08 11:57:37 +01001624 const struct lys_module *start_mod, *aux_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001625
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001626 assert(nodeid && (start || cur_module) && !(start && cur_module) && ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001627
1628 id = nodeid;
1629
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001630 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 +01001631 return ((id - nodeid) - r) + 1;
1632 }
1633 id += r;
1634
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001635 if ((is_relative && !start) || (!is_relative && !cur_module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001636 return -1;
1637 }
1638
1639 /* descendant-schema-nodeid */
1640 if (is_relative) {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001641 cur_module = start_mod = start->module;
Michal Vasko24476fa2017-03-08 12:33:48 +01001642 start_parent = lys_parent(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01001643
Michal Vasko3edeaf72016-02-11 13:17:43 +01001644 /* absolute-schema-nodeid */
1645 } else {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001646 start_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001647 if (!start_mod) {
1648 return -1;
1649 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001650 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001651 }
1652
1653 while (1) {
1654 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001655 last_aug = NULL;
1656
1657 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001658 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1659 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001660 /* we are getting into another module (augment) */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02001661 aux_mod = lys_get_import_module(cur_module, NULL, 0, mod_name, mod_name_len);
Michal Vaskobb520442017-05-23 10:55:18 +02001662 if (!aux_mod) {
1663 return -1;
1664 }
1665 } else {
Michal Vasko17315772017-07-10 15:15:39 +02001666 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001667 * because this module may be not implemented and it augments something in another module and
1668 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001669 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001670 }
1671
1672 /* if the module is implemented, all the augments will be connected */
1673 if (!aux_mod->implemented) {
1674get_next_augment:
1675 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1676 }
1677 }
1678
1679 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vasko5b997902017-04-03 14:16:22 +02001680 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001681 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001682 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001683 r = schema_nodeid_siblingcheck(sibling, id, cur_module, mod_name, mod_name_len);
Radek Krejcibdf92362016-04-08 14:43:34 +02001684 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001685 *ret = sibling;
1686 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001687 } else if (r == 1) {
1688 continue;
1689 } else if (r == 2) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001690 start_parent = sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001691 break;
1692 } else {
1693 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001694 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001695 }
1696 }
1697
1698 /* no match */
1699 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02001700 if (last_aug) {
1701 /* it still could be in another augment */
1702 goto get_next_augment;
1703 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01001704 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001705 return EXIT_SUCCESS;
1706 }
1707
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001708 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 +01001709 return ((id - nodeid) - r) + 1;
1710 }
1711 id += r;
1712 }
1713
1714 /* cannot get here */
1715 LOGINT;
1716 return -1;
1717}
1718
Radek Krejcif3c71de2016-04-11 12:45:46 +02001719/* unique, refine,
1720 * >0 - unexpected char on position (ret - 1),
1721 * 0 - ok (but ret can still be NULL),
1722 * -1 - error,
1723 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001724int
1725resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02001726 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001727{
1728 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001729 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001730 int r, nam_len, mod_name_len, is_relative = -1;
1731 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001732 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001733
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001734 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01001735 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01001736
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001737 if (!start) {
1738 /* leaf not found */
1739 return 0;
1740 }
1741
Michal Vasko3edeaf72016-02-11 13:17:43 +01001742 id = nodeid;
1743 module = start->module;
1744
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001745 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 +01001746 return ((id - nodeid) - r) + 1;
1747 }
1748 id += r;
1749
1750 if (!is_relative) {
1751 return -1;
1752 }
1753
Michal Vasko24476fa2017-03-08 12:33:48 +01001754 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02001755 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01001756 start_parent = lys_parent(start_parent);
1757 }
1758
Michal Vasko3edeaf72016-02-11 13:17:43 +01001759 while (1) {
1760 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01001761 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vasko74a991b2017-03-31 09:17:22 +02001762 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001763 /* name match */
1764 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001765 r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len);
Radek Krejcibdf92362016-04-08 14:43:34 +02001766 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001767 if (!(sibling->nodetype & ret_nodetype)) {
1768 /* wrong node type, too bad */
1769 continue;
1770 }
1771 *ret = sibling;
1772 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001773 } else if (r == 1) {
1774 continue;
1775 } else if (r == 2) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001776 start_parent = sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001777 break;
1778 } else {
1779 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001780 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001781 }
1782 }
1783
1784 /* no match */
1785 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001786 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001787 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001788 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1789 *ret = NULL;
1790 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001791 }
1792
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001793 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 +01001794 return ((id - nodeid) - r) + 1;
1795 }
1796 id += r;
1797 }
1798
1799 /* cannot get here */
1800 LOGINT;
1801 return -1;
1802}
1803
1804/* choice default */
1805int
1806resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1807{
1808 /* cannot actually be a path */
1809 if (strchr(nodeid, '/')) {
1810 return -1;
1811 }
1812
Michal Vaskodc300b02017-04-07 14:09:20 +02001813 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001814}
1815
1816/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1817static int
1818resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1819{
1820 const struct lys_module *module;
1821 const char *mod_prefix, *name;
1822 int i, mod_prefix_len, nam_len;
1823
1824 /* parse the identifier, it must be parsed on one call */
1825 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1826 return -i + 1;
1827 }
1828
1829 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1830 if (!module) {
1831 return -1;
1832 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01001833 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001834 start = module->data;
1835 }
1836
1837 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1838
1839 return EXIT_SUCCESS;
1840}
1841
1842int
1843resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1844 const struct lys_node **ret)
1845{
1846 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001847 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001848 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02001849 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001850
1851 assert(nodeid && module && ret);
1852 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1853
1854 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01001855 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001856
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001857 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 +01001858 return ((id - nodeid) - r) + 1;
1859 }
1860 id += r;
1861
1862 if (is_relative) {
1863 return -1;
1864 }
1865
1866 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001867 if (!abs_start_mod) {
1868 return -1;
1869 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001870
1871 while (1) {
1872 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01001873 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vasko3edeaf72016-02-11 13:17:43 +01001874 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1875 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001876 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001877 r = schema_nodeid_siblingcheck(sibling, id, module, mod_name, mod_name_len);
Radek Krejcibdf92362016-04-08 14:43:34 +02001878 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001879 if (!(sibling->nodetype & ret_nodetype)) {
1880 /* wrong node type, too bad */
1881 continue;
1882 }
1883 *ret = sibling;
1884 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001885 } else if (r == 1) {
1886 continue;
1887 } else if (r == 2) {
Michal Vasko45ddfce2017-07-10 15:11:38 +02001888 start_parent = sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001889 break;
1890 } else {
1891 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001892 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001893 }
1894 }
1895
1896 /* no match */
1897 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001898 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001899 return EXIT_SUCCESS;
1900 }
1901
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001902 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 +01001903 return ((id - nodeid) - r) + 1;
1904 }
1905 id += r;
1906 }
1907
1908 /* cannot get here */
1909 LOGINT;
1910 return -1;
1911}
1912
Michal Vaskoe733d682016-03-14 09:08:27 +01001913static int
Michal Vaskof359b022017-07-04 13:50:04 +02001914resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list,
1915 const struct lys_module *cur_module, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001916{
Michal Vaskof359b022017-07-04 13:50:04 +02001917 const char *mod_name, *name;
1918 int mod_name_len, nam_len, has_predicate, i;
1919 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01001920
Michal Vaskof359b022017-07-04 13:50:04 +02001921 if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001922 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001923 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001924 return -1;
1925 }
1926
1927 predicate += i;
1928 *parsed += i;
1929
Michal Vasko58c2aab2017-01-05 10:02:05 +01001930 if (!isdigit(name[0])) {
1931 for (i = 0; i < list->keys_size; ++i) {
Michal Vaskof359b022017-07-04 13:50:04 +02001932 key = (struct lys_node *)list->keys[i];
1933 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
1934 if (mod_name) {
1935 if (!strncmp(lys_node_module(key)->name, mod_name, mod_name_len) && !lys_node_module(key)->name[mod_name_len]) {
1936 break;
1937 }
1938 } else {
1939 if (!strcmp(lys_node_module(key)->name, cur_module->name)) {
1940 break;
1941 }
1942 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001943 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001944 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001945
Michal Vasko58c2aab2017-01-05 10:02:05 +01001946 if (i == list->keys_size) {
1947 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
1948 return -1;
1949 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001950 }
1951
1952 /* more predicates? */
1953 if (has_predicate) {
Michal Vaskof359b022017-07-04 13:50:04 +02001954 return resolve_json_schema_list_predicate(predicate, list, cur_module, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001955 }
1956
1957 return 0;
1958}
1959
Michal Vasko8d26e5c2016-09-08 10:03:49 +02001960/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01001961const struct lys_node *
Michal Vasko8d26e5c2016-09-08 10:03:49 +02001962resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001963{
Michal Vasko10728b52016-04-07 14:26:29 +02001964 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001965 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001966 const struct lys_node *sibling, *start_parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02001967 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001968 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof359b022017-07-04 13:50:04 +02001969 const struct lys_module *prefix_mod, *cur_module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001970
Michal Vasko3547c532016-03-14 09:40:50 +01001971 assert(nodeid && (ctx || start));
1972 if (!ctx) {
1973 ctx = start->module->ctx;
1974 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001975
1976 id = nodeid;
1977
Michal Vaskoe733d682016-03-14 09:08:27 +01001978 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 +01001979 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001980 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001981 }
1982 id += r;
1983
1984 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01001985 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01001986 start_parent = start;
1987 while (start_parent && (start_parent->nodetype == LYS_USES)) {
1988 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01001989 }
Michal Vaskof359b022017-07-04 13:50:04 +02001990 cur_module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01001991 } else {
1992 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02001993 str = strndup(nodeid, (name + nam_len) - nodeid);
1994 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
1995 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01001996 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02001997 } else if (mod_name_len > LY_BUF_SIZE - 1) {
1998 LOGINT;
1999 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002000 }
2001
Michal Vasko971a3ca2016-04-01 13:09:29 +02002002 if (ly_buf_used && module_name[0]) {
2003 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2004 }
2005 ly_buf_used++;
2006
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002007 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002008 module_name[mod_name_len] = '\0';
Michal Vaskof359b022017-07-04 13:50:04 +02002009 cur_module = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002010
2011 if (buf_backup) {
2012 /* return previous internal buffer content */
2013 strcpy(module_name, buf_backup);
2014 free(buf_backup);
2015 buf_backup = NULL;
2016 }
2017 ly_buf_used--;
2018
Michal Vaskof359b022017-07-04 13:50:04 +02002019 if (!cur_module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002020 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2021 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2022 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002023 return NULL;
2024 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002025 start_parent = NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002026
2027 /* now it's as if there was no module name */
2028 mod_name = NULL;
2029 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002030 }
2031
Michal Vasko3edeaf72016-02-11 13:17:43 +01002032 while (1) {
2033 sibling = NULL;
Michal Vaskof359b022017-07-04 13:50:04 +02002034 while ((sibling = lys_getnext(sibling, start_parent, cur_module,
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002035 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002036 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002037 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002038 /* module check */
2039 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002040 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002041 LOGINT;
2042 return NULL;
2043 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002044
2045 if (ly_buf_used && module_name[0]) {
2046 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2047 }
2048 ly_buf_used++;
2049
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002050 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01002051 module_name[mod_name_len] = '\0';
2052 /* will also find an augment module */
2053 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002054
2055 if (buf_backup) {
2056 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002057 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002058 free(buf_backup);
2059 buf_backup = NULL;
2060 }
2061 ly_buf_used--;
2062
Michal Vasko3edeaf72016-02-11 13:17:43 +01002063 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002064 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2065 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2066 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002067 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002068 }
2069 } else {
Michal Vaskof359b022017-07-04 13:50:04 +02002070 prefix_mod = cur_module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002071 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002072 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002073 continue;
2074 }
2075
Michal Vaskoe733d682016-03-14 09:08:27 +01002076 /* do we have some predicates on it? */
2077 if (has_predicate) {
2078 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002079 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vaskof359b022017-07-04 13:50:04 +02002080 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002081 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2082 return NULL;
2083 }
2084 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof359b022017-07-04 13:50:04 +02002085 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, cur_module, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002086 return NULL;
2087 }
2088 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01002089 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002090 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002091 }
2092 id += r;
2093 }
2094
Michal Vasko3edeaf72016-02-11 13:17:43 +01002095 /* the result node? */
2096 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002097 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002098 }
2099
Michal Vaskodc300b02017-04-07 14:09:20 +02002100 /* move down the tree, if possible */
2101 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
2102 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2103 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002104 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002105 start_parent = sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002106 break;
2107 }
2108 }
2109
2110 /* no match */
2111 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002112 str = strndup(nodeid, (name + nam_len) - nodeid);
2113 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2114 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002115 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002116 }
2117
Michal Vaskoe733d682016-03-14 09:08:27 +01002118 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 +01002119 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002120 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002121 }
2122 id += r;
2123 }
2124
2125 /* cannot get here */
2126 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01002127 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002128}
2129
Michal Vasko22448d32016-03-16 13:17:29 +01002130static int
Michal Vasko58c2aab2017-01-05 10:02:05 +01002131resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node,
Michal Vaskof359b022017-07-04 13:50:04 +02002132 int position, const struct lys_module *cur_module, int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002133{
Michal Vaskof359b022017-07-04 13:50:04 +02002134 const char *mod_name, *name, *value, *key_val;
2135 int mod_name_len, nam_len, val_len, has_predicate = 1, r;
Michal Vasko22448d32016-03-16 13:17:29 +01002136 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002137 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01002138
Radek Krejci61a86c62016-03-24 11:06:44 +01002139 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002140 assert(node->schema->nodetype == LYS_LIST);
2141
Michal Vasko53adfc72017-01-06 10:39:10 +01002142 /* is the predicate a number? */
Michal Vaskof359b022017-07-04 13:50:04 +02002143 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko53adfc72017-01-06 10:39:10 +01002144 || !strncmp(name, ".", nam_len)) {
2145 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
2146 return -1;
2147 }
2148
2149 if (isdigit(name[0])) {
2150 if (position == atoi(name)) {
2151 /* match */
2152 *parsed += r;
2153 return 0;
2154 } else {
2155 /* not a match */
2156 return 1;
2157 }
2158 }
2159
2160 if (!((struct lys_node_list *)node->schema)->keys_size) {
2161 /* no keys in schema - causes an error later */
2162 return 0;
2163 }
2164
Michal Vaskof29903d2016-04-18 13:13:10 +02002165 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002166 if (!key) {
2167 /* it is not a position, so we need a key for it to be a match */
2168 return 1;
2169 }
2170
2171 /* go through all the keys */
2172 i = 0;
2173 goto check_parsed_values;
2174
2175 for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
Michal Vasko22448d32016-03-16 13:17:29 +01002176 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002177 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002178 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002179 }
2180
Michal Vaskof359b022017-07-04 13:50:04 +02002181 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002182 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002183 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002184 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002185 }
2186
Michal Vasko53adfc72017-01-06 10:39:10 +01002187check_parsed_values:
Michal Vasko22448d32016-03-16 13:17:29 +01002188 predicate += r;
2189 *parsed += r;
2190
Michal Vaskof29903d2016-04-18 13:13:10 +02002191 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002192 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002193 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002194 }
2195
Michal Vaskof359b022017-07-04 13:50:04 +02002196 if (mod_name) {
2197 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len)
2198 || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) {
2199 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2200 return -1;
2201 }
2202 } else {
2203 if (strcmp(lyd_node_module((struct lyd_node *)key)->name, cur_module->name)) {
2204 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
2205 return -1;
2206 }
2207 }
2208
Michal Vasko9ba34de2016-12-07 12:21:19 +01002209 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002210 if ((key->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002211 && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name))
2212 && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002213 key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1;
2214 } else {
2215 key_val = key->value_str;
2216 }
2217
Michal Vasko22448d32016-03-16 13:17:29 +01002218 /* value does not match */
Michal Vasko9ba34de2016-12-07 12:21:19 +01002219 if (strncmp(key_val, value, val_len) || key_val[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002220 return 1;
2221 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002222
2223 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002224 }
2225
2226 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002227 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002228 return -1;
2229 }
2230
2231 return 0;
2232}
2233
Radek Krejci45826012016-08-24 15:07:57 +02002234/**
2235 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2236 *
2237 * @param[in] nodeid Node data path to find
2238 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2239 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2240 * @param[out] parsed Number of characters processed in \p id
2241 * @return The closes parent (or the node itself) from the path
2242 */
Michal Vasko22448d32016-03-16 13:17:29 +01002243struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002244resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2245 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002246{
Michal Vasko10728b52016-04-07 14:26:29 +02002247 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002248 const char *id, *mod_name, *name, *pred_name, *data_val;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002249 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002250 int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002251 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002252 struct lyd_node_leaf_list *llist;
Michal Vaskof359b022017-07-04 13:50:04 +02002253 const struct lys_module *prefix_mod, *cur_module;
Michal Vasko22448d32016-03-16 13:17:29 +01002254 struct ly_ctx *ctx;
2255
2256 assert(nodeid && start && parsed);
2257
2258 ctx = start->schema->module->ctx;
2259 id = nodeid;
2260
2261 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 +01002262 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002263 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002264 return NULL;
2265 }
2266 id += r;
2267 /* add it to parsed only after the data node was actually found */
2268 last_parsed = r;
2269
2270 if (is_relative) {
Michal Vaskof359b022017-07-04 13:50:04 +02002271 cur_module = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002272 start = start->child;
2273 } else {
2274 for (; start->parent; start = start->parent);
Michal Vaskof359b022017-07-04 13:50:04 +02002275 cur_module = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002276 }
2277
2278 while (1) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002279 list_instance_position = 0;
2280
Michal Vasko22448d32016-03-16 13:17:29 +01002281 LY_TREE_FOR(start, sibling) {
Michal Vasko945b96b2016-10-18 11:49:12 +02002282 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
Michal Vasko2411b942016-03-23 13:50:03 +01002283 if (lys_parent(sibling->schema)) {
2284 if (options & LYD_PATH_OPT_OUTPUT) {
2285 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002286 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002287 *parsed = -1;
2288 return NULL;
2289 }
2290 } else {
2291 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002292 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002293 *parsed = -1;
2294 return NULL;
2295 }
2296 }
2297 }
2298
Michal Vasko22448d32016-03-16 13:17:29 +01002299 /* name match */
2300 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2301
2302 /* module check */
2303 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002304 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01002305 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002306 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002307 return NULL;
2308 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002309
2310 if (ly_buf_used && module_name[0]) {
2311 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2312 }
2313 ly_buf_used++;
2314
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002315 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01002316 module_name[mod_name_len] = '\0';
2317 /* will also find an augment module */
2318 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002319
2320 if (buf_backup) {
2321 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002322 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002323 free(buf_backup);
2324 buf_backup = NULL;
2325 }
2326 ly_buf_used--;
2327
Michal Vasko22448d32016-03-16 13:17:29 +01002328 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002329 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2330 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2331 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002332 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002333 return NULL;
2334 }
2335 } else {
Michal Vaskof359b022017-07-04 13:50:04 +02002336 prefix_mod = cur_module;
Michal Vasko22448d32016-03-16 13:17:29 +01002337 }
Michal Vasko1adc7242016-11-16 11:05:28 +01002338 if (prefix_mod != lyd_node_module(sibling)) {
Michal Vasko22448d32016-03-16 13:17:29 +01002339 continue;
2340 }
2341
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002342 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002343 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002344 llist = (struct lyd_node_leaf_list *)sibling;
2345
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002346 last_has_pred = 0;
Michal Vaskof0a50972016-10-19 11:33:55 +02002347 if (has_predicate) {
Michal Vaskof359b022017-07-04 13:50:04 +02002348 if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value,
2349 &llval_len, &last_has_pred)) < 1) {
Michal Vaskof0a50972016-10-19 11:33:55 +02002350 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2351 *parsed = -1;
2352 return NULL;
2353 }
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002354 if ((pred_name[0] != '.') || (pred_name_len != 1)) {
2355 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1);
2356 *parsed = -1;
2357 return NULL;
2358 }
Michal Vaskof0a50972016-10-19 11:33:55 +02002359 } else {
2360 r = 0;
2361 if (llist_value) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002362 llval_len = strlen(llist_value);
Michal Vaskof0a50972016-10-19 11:33:55 +02002363 }
2364 }
2365
Michal Vasko9ba34de2016-12-07 12:21:19 +01002366 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002367 if ((llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002368 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2369 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002370 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2371 } else {
2372 data_val = llist->value_str;
2373 }
2374
2375 if ((!llist_value && data_val && data_val[0])
2376 || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002377 continue;
2378 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002379
Michal Vaskof0a50972016-10-19 11:33:55 +02002380 id += r;
2381 last_parsed += r;
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002382 has_predicate = last_has_pred;
Michal Vaskof0a50972016-10-19 11:33:55 +02002383
Radek Krejci45826012016-08-24 15:07:57 +02002384 } else if (sibling->schema->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002385 /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */
Michal Vasko22448d32016-03-16 13:17:29 +01002386 if (!has_predicate) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002387 /* none match */
2388 return last_match;
Michal Vasko22448d32016-03-16 13:17:29 +01002389 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01002390
2391 ++list_instance_position;
2392 r = 0;
Michal Vaskof359b022017-07-04 13:50:04 +02002393 ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, cur_module, &r);
Michal Vasko22448d32016-03-16 13:17:29 +01002394 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002395 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002396 return NULL;
2397 } else if (ret == 1) {
2398 /* this list instance does not match */
2399 continue;
2400 }
2401 id += r;
2402 last_parsed += r;
2403 }
2404
2405 *parsed += last_parsed;
2406
2407 /* the result node? */
2408 if (!id[0]) {
2409 return sibling;
2410 }
2411
2412 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002413 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002414 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002415 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002416 return NULL;
2417 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002418 last_match = sibling;
Michal Vasko22448d32016-03-16 13:17:29 +01002419 start = sibling->child;
Michal Vasko22448d32016-03-16 13:17:29 +01002420 break;
2421 }
2422 }
2423
2424 /* no match, return last match */
2425 if (!sibling) {
2426 return last_match;
2427 }
2428
2429 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 +01002430 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002431 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002432 return NULL;
2433 }
2434 id += r;
2435 last_parsed = r;
2436 }
2437
2438 /* cannot get here */
2439 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002440 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002441 return NULL;
2442}
2443
Michal Vasko3edeaf72016-02-11 13:17:43 +01002444/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002445 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002446 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002447 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02002448 * @param[in] str_restr Restriction as a string.
2449 * @param[in] type Type of the restriction.
2450 * @param[out] ret Final interval structure that starts with
2451 * the interval of the initial type, continues with intervals
2452 * of any superior types derived from the initial one, and
2453 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002454 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002455 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002456 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002457int
Michal Vaskoaeb51802016-04-11 10:58:47 +02002458resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002459{
2460 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002461 int kind;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002462 int64_t local_smin, local_smax, local_fmin, local_fmax;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002463 uint64_t local_umin, local_umax;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002464 uint8_t local_fdig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002465 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002466 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002467
2468 switch (type->base) {
2469 case LY_TYPE_BINARY:
2470 kind = 0;
2471 local_umin = 0;
2472 local_umax = 18446744073709551615UL;
2473
2474 if (!str_restr && type->info.binary.length) {
2475 str_restr = type->info.binary.length->expr;
2476 }
2477 break;
2478 case LY_TYPE_DEC64:
2479 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002480 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2481 local_fmax = __INT64_C(9223372036854775807);
2482 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002483
2484 if (!str_restr && type->info.dec64.range) {
2485 str_restr = type->info.dec64.range->expr;
2486 }
2487 break;
2488 case LY_TYPE_INT8:
2489 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002490 local_smin = __INT64_C(-128);
2491 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002492
2493 if (!str_restr && type->info.num.range) {
2494 str_restr = type->info.num.range->expr;
2495 }
2496 break;
2497 case LY_TYPE_INT16:
2498 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002499 local_smin = __INT64_C(-32768);
2500 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002501
2502 if (!str_restr && type->info.num.range) {
2503 str_restr = type->info.num.range->expr;
2504 }
2505 break;
2506 case LY_TYPE_INT32:
2507 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002508 local_smin = __INT64_C(-2147483648);
2509 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002510
2511 if (!str_restr && type->info.num.range) {
2512 str_restr = type->info.num.range->expr;
2513 }
2514 break;
2515 case LY_TYPE_INT64:
2516 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002517 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2518 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002519
2520 if (!str_restr && type->info.num.range) {
2521 str_restr = type->info.num.range->expr;
2522 }
2523 break;
2524 case LY_TYPE_UINT8:
2525 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002526 local_umin = __UINT64_C(0);
2527 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002528
2529 if (!str_restr && type->info.num.range) {
2530 str_restr = type->info.num.range->expr;
2531 }
2532 break;
2533 case LY_TYPE_UINT16:
2534 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002535 local_umin = __UINT64_C(0);
2536 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002537
2538 if (!str_restr && type->info.num.range) {
2539 str_restr = type->info.num.range->expr;
2540 }
2541 break;
2542 case LY_TYPE_UINT32:
2543 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002544 local_umin = __UINT64_C(0);
2545 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002546
2547 if (!str_restr && type->info.num.range) {
2548 str_restr = type->info.num.range->expr;
2549 }
2550 break;
2551 case LY_TYPE_UINT64:
2552 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002553 local_umin = __UINT64_C(0);
2554 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002555
2556 if (!str_restr && type->info.num.range) {
2557 str_restr = type->info.num.range->expr;
2558 }
2559 break;
2560 case LY_TYPE_STRING:
2561 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002562 local_umin = __UINT64_C(0);
2563 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002564
2565 if (!str_restr && type->info.str.length) {
2566 str_restr = type->info.str.length->expr;
2567 }
2568 break;
2569 default:
2570 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002571 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002572 }
2573
2574 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002575 if (type->der) {
2576 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002577 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002578 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002579 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002580 assert(!intv || (intv->kind == kind));
2581 }
2582
2583 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002584 /* we do not have any restriction, return superior ones */
2585 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002586 return EXIT_SUCCESS;
2587 }
2588
2589 /* adjust local min and max */
2590 if (intv) {
2591 tmp_intv = intv;
2592
2593 if (kind == 0) {
2594 local_umin = tmp_intv->value.uval.min;
2595 } else if (kind == 1) {
2596 local_smin = tmp_intv->value.sval.min;
2597 } else if (kind == 2) {
2598 local_fmin = tmp_intv->value.fval.min;
2599 }
2600
2601 while (tmp_intv->next) {
2602 tmp_intv = tmp_intv->next;
2603 }
2604
2605 if (kind == 0) {
2606 local_umax = tmp_intv->value.uval.max;
2607 } else if (kind == 1) {
2608 local_smax = tmp_intv->value.sval.max;
2609 } else if (kind == 2) {
2610 local_fmax = tmp_intv->value.fval.max;
2611 }
2612 }
2613
2614 /* finally parse our restriction */
2615 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002616 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002617 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002618 if (!tmp_local_intv) {
2619 assert(!local_intv);
2620 local_intv = malloc(sizeof *local_intv);
2621 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002622 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002623 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002624 tmp_local_intv = tmp_local_intv->next;
2625 }
Radek Krejcia8d111f2017-05-31 13:57:37 +02002626 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM, error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002627
2628 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002629 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002630 tmp_local_intv->next = NULL;
2631
2632 /* min */
2633 ptr = seg_ptr;
2634 while (isspace(ptr[0])) {
2635 ++ptr;
2636 }
2637 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2638 if (kind == 0) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002639 tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002640 } else if (kind == 1) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002641 tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002642 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002643 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
2644 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2645 goto error;
2646 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002647 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002648 } else if (!strncmp(ptr, "min", 3)) {
2649 if (kind == 0) {
2650 tmp_local_intv->value.uval.min = local_umin;
2651 } else if (kind == 1) {
2652 tmp_local_intv->value.sval.min = local_smin;
2653 } else if (kind == 2) {
2654 tmp_local_intv->value.fval.min = local_fmin;
2655 }
2656
2657 ptr += 3;
2658 } else if (!strncmp(ptr, "max", 3)) {
2659 if (kind == 0) {
2660 tmp_local_intv->value.uval.min = local_umax;
2661 } else if (kind == 1) {
2662 tmp_local_intv->value.sval.min = local_smax;
2663 } else if (kind == 2) {
2664 tmp_local_intv->value.fval.min = local_fmax;
2665 }
2666
2667 ptr += 3;
2668 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002669 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002670 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002671 }
2672
2673 while (isspace(ptr[0])) {
2674 ptr++;
2675 }
2676
2677 /* no interval or interval */
2678 if ((ptr[0] == '|') || !ptr[0]) {
2679 if (kind == 0) {
2680 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2681 } else if (kind == 1) {
2682 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2683 } else if (kind == 2) {
2684 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2685 }
2686 } else if (!strncmp(ptr, "..", 2)) {
2687 /* skip ".." */
2688 ptr += 2;
2689 while (isspace(ptr[0])) {
2690 ++ptr;
2691 }
2692
2693 /* max */
2694 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2695 if (kind == 0) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002696 tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002697 } else if (kind == 1) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002698 tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002699 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002700 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
2701 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2702 goto error;
2703 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002704 }
2705 } else if (!strncmp(ptr, "max", 3)) {
2706 if (kind == 0) {
2707 tmp_local_intv->value.uval.max = local_umax;
2708 } else if (kind == 1) {
2709 tmp_local_intv->value.sval.max = local_smax;
2710 } else if (kind == 2) {
2711 tmp_local_intv->value.fval.max = local_fmax;
2712 }
2713 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002714 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002715 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002716 }
2717 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002718 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002719 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002720 }
2721
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002722 /* check min and max in correct order*/
2723 if (kind == 0) {
2724 /* current segment */
2725 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2726 goto error;
2727 }
2728 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2729 goto error;
2730 }
2731 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02002732 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002733 goto error;
2734 }
2735 } else if (kind == 1) {
2736 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
2737 goto error;
2738 }
2739 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
2740 goto error;
2741 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002742 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002743 goto error;
2744 }
2745 } else if (kind == 2) {
2746 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
2747 goto error;
2748 }
2749 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
2750 goto error;
2751 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002752 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002753 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002754 goto error;
2755 }
2756 }
2757
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002758 /* next segment (next OR) */
2759 seg_ptr = strchr(seg_ptr, '|');
2760 if (!seg_ptr) {
2761 break;
2762 }
2763 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002764 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002765 }
2766
2767 /* check local restrictions against superior ones */
2768 if (intv) {
2769 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002770 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002771
2772 while (tmp_local_intv && tmp_intv) {
2773 /* reuse local variables */
2774 if (kind == 0) {
2775 local_umin = tmp_local_intv->value.uval.min;
2776 local_umax = tmp_local_intv->value.uval.max;
2777
2778 /* it must be in this interval */
2779 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2780 /* this interval is covered, next one */
2781 if (local_umax <= tmp_intv->value.uval.max) {
2782 tmp_local_intv = tmp_local_intv->next;
2783 continue;
2784 /* ascending order of restrictions -> fail */
2785 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002786 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002787 }
2788 }
2789 } else if (kind == 1) {
2790 local_smin = tmp_local_intv->value.sval.min;
2791 local_smax = tmp_local_intv->value.sval.max;
2792
2793 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2794 if (local_smax <= tmp_intv->value.sval.max) {
2795 tmp_local_intv = tmp_local_intv->next;
2796 continue;
2797 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002798 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002799 }
2800 }
2801 } else if (kind == 2) {
2802 local_fmin = tmp_local_intv->value.fval.min;
2803 local_fmax = tmp_local_intv->value.fval.max;
2804
Michal Vasko4d1f0482016-09-19 14:35:06 +02002805 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02002806 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002807 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002808 tmp_local_intv = tmp_local_intv->next;
2809 continue;
2810 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002811 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002812 }
2813 }
2814 }
2815
2816 tmp_intv = tmp_intv->next;
2817 }
2818
2819 /* some interval left uncovered -> fail */
2820 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002821 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002822 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002823 }
2824
Michal Vaskoaeb51802016-04-11 10:58:47 +02002825 /* append the local intervals to all the intervals of the superior types, return it all */
2826 if (intv) {
2827 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2828 tmp_intv->next = local_intv;
2829 } else {
2830 intv = local_intv;
2831 }
2832 *ret = intv;
2833
2834 return EXIT_SUCCESS;
2835
2836error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002837 while (intv) {
2838 tmp_intv = intv->next;
2839 free(intv);
2840 intv = tmp_intv;
2841 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02002842 while (local_intv) {
2843 tmp_local_intv = local_intv->next;
2844 free(local_intv);
2845 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002846 }
2847
Michal Vaskoaeb51802016-04-11 10:58:47 +02002848 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002849}
2850
Michal Vasko730dfdf2015-08-11 14:48:05 +02002851/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02002852 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
2853 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002854 *
2855 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002856 * @param[in] mod_name Typedef name module name.
2857 * @param[in] module Main module.
2858 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002859 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002860 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002861 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002862 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002863int
Michal Vasko1e62a092015-12-01 12:27:20 +01002864resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2865 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002866{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002867 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002868 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002869 int tpdf_size;
2870
Michal Vasko1dca6882015-10-22 14:29:42 +02002871 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002872 /* no prefix, try built-in types */
2873 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01002874 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002875 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01002876 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002877 }
2878 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002879 }
2880 }
2881 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002882 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002883 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002884 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002885 }
2886 }
2887
Michal Vasko1dca6882015-10-22 14:29:42 +02002888 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002889 /* search in local typedefs */
2890 while (parent) {
2891 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002892 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002893 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2894 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002895 break;
2896
Radek Krejci76512572015-08-04 09:47:08 +02002897 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002898 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2899 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002900 break;
2901
Radek Krejci76512572015-08-04 09:47:08 +02002902 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002903 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2904 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002905 break;
2906
Radek Krejci76512572015-08-04 09:47:08 +02002907 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002908 case LYS_ACTION:
2909 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
2910 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002911 break;
2912
Radek Krejci76512572015-08-04 09:47:08 +02002913 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002914 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2915 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002916 break;
2917
Radek Krejci76512572015-08-04 09:47:08 +02002918 case LYS_INPUT:
2919 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02002920 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
2921 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002922 break;
2923
2924 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02002925 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002926 continue;
2927 }
2928
2929 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002930 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002931 match = &tpdf[i];
2932 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002933 }
2934 }
2935
Michal Vaskodcf98e62016-05-05 17:53:53 +02002936 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002937 }
Radek Krejcic071c542016-01-27 14:57:51 +01002938 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002939 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002940 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002941 if (!module) {
2942 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002943 }
2944 }
2945
2946 /* search in top level typedefs */
2947 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002948 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002949 match = &module->tpdf[i];
2950 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002951 }
2952 }
2953
2954 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01002955 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002956 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002957 if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002958 match = &module->inc[i].submodule->tpdf[j];
2959 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002960 }
2961 }
2962 }
2963
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002964 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002965
2966check_leafref:
2967 if (ret) {
2968 *ret = match;
2969 }
2970 if (match->type.base == LY_TYPE_LEAFREF) {
2971 while (!match->type.info.lref.path) {
2972 match = match->type.der;
2973 assert(match);
2974 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02002975 }
2976 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002977}
2978
Michal Vasko1dca6882015-10-22 14:29:42 +02002979/**
2980 * @brief Check the default \p value of the \p type. Logs directly.
2981 *
2982 * @param[in] type Type definition to use.
2983 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01002984 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02002985 *
2986 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
2987 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002988static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01002989check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002990{
Radek Krejcibad2f172016-08-02 11:04:15 +02002991 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02002992 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01002993 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02002994 char *s;
Radek Krejci37b756f2016-01-18 10:15:03 +01002995 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02002996
Radek Krejci51673202016-11-01 17:00:32 +01002997 assert(value);
2998
Radek Krejcic13db382016-08-16 10:52:42 +02002999 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003000 /* the type was not resolved yet, nothing to do for now */
3001 return EXIT_FAILURE;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003002 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003003 /* do not check defaults in not implemented module's data */
3004 return EXIT_SUCCESS;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003005 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003006 /* identityrefs are checked when instantiated in data instead of typedef,
3007 * but in typedef the value has to be modified to include the prefix */
3008 if (*value) {
3009 if (strchr(*value, ':')) {
3010 dflt = transform_schema2json(module, *value);
3011 } else {
3012 /* default prefix of the module where the typedef is defined */
3013 asprintf(&s, "%s:%s", lys_main_module(module)->name, *value);
3014 dflt = lydict_insert_zc(module->ctx, s);
3015 }
3016 lydict_remove(module->ctx, *value);
3017 *value = dflt;
3018 }
3019 return EXIT_SUCCESS;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003020 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3021 /* leafref in typedef cannot be checked */
3022 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003023 }
3024
Radek Krejci51673202016-11-01 17:00:32 +01003025 dflt = *value;
3026 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003027 /* we do not have a new default value, so is there any to check even, in some base type? */
3028 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3029 if (base_tpdf->dflt) {
Radek Krejci51673202016-11-01 17:00:32 +01003030 dflt = base_tpdf->dflt;
Michal Vasko478c4652016-07-21 12:55:01 +02003031 break;
3032 }
3033 }
3034
Radek Krejci51673202016-11-01 17:00:32 +01003035 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003036 /* no default value, nothing to check, all is well */
3037 return EXIT_SUCCESS;
3038 }
3039
3040 /* so there is a default value in a base type, but can the default value be no longer valid (did we define some new restrictions)? */
3041 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003042 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003043 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
3044 return EXIT_SUCCESS;
3045 } else {
3046 /* check the default value from typedef, but use also the typedef's module
3047 * due to possible searching in imported modules which is expected in
3048 * typedef's module instead of module where the typedef is used */
3049 module = base_tpdf->module;
3050 }
3051 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003052 case LY_TYPE_INST:
3053 case LY_TYPE_LEAFREF:
3054 case LY_TYPE_BOOL:
3055 case LY_TYPE_EMPTY:
3056 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
3057 return EXIT_SUCCESS;
Radek Krejcibad2f172016-08-02 11:04:15 +02003058 case LY_TYPE_BITS:
3059 /* the default value must match the restricted list of values, if the type was restricted */
3060 if (type->info.bits.count) {
3061 break;
3062 }
3063 return EXIT_SUCCESS;
3064 case LY_TYPE_ENUM:
3065 /* the default value must match the restricted list of values, if the type was restricted */
3066 if (type->info.enums.count) {
3067 break;
3068 }
3069 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003070 case LY_TYPE_DEC64:
3071 if (type->info.dec64.range) {
3072 break;
3073 }
3074 return EXIT_SUCCESS;
3075 case LY_TYPE_BINARY:
3076 if (type->info.binary.length) {
3077 break;
3078 }
3079 return EXIT_SUCCESS;
3080 case LY_TYPE_INT8:
3081 case LY_TYPE_INT16:
3082 case LY_TYPE_INT32:
3083 case LY_TYPE_INT64:
3084 case LY_TYPE_UINT8:
3085 case LY_TYPE_UINT16:
3086 case LY_TYPE_UINT32:
3087 case LY_TYPE_UINT64:
3088 if (type->info.num.range) {
3089 break;
3090 }
3091 return EXIT_SUCCESS;
3092 case LY_TYPE_STRING:
3093 if (type->info.str.length || type->info.str.patterns) {
3094 break;
3095 }
3096 return EXIT_SUCCESS;
3097 case LY_TYPE_UNION:
3098 /* way too much trouble learning whether we need to check the default again, so just do it */
3099 break;
3100 default:
3101 LOGINT;
3102 return -1;
3103 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003104 } else if (type->base == LY_TYPE_EMPTY) {
3105 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3106 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
3107 return -1;
Michal Vasko478c4652016-07-21 12:55:01 +02003108 }
3109
Michal Vasko1dca6882015-10-22 14:29:42 +02003110 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003111 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003112 node.value_str = dflt;
Michal Vasko1dca6882015-10-22 14:29:42 +02003113 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01003114 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Radek Krejcia8d111f2017-05-31 13:57:37 +02003115 LY_CHECK_ERR_RETURN(!node.schema, LOGMEM, -1);
Radek Krejcibad2f172016-08-02 11:04:15 +02003116 node.schema->name = strdup("fake-default");
Radek Krejcia8d111f2017-05-31 13:57:37 +02003117 LY_CHECK_ERR_RETURN(!node.schema->name, LOGMEM; free(node.schema), -1);
Michal Vasko56826402016-03-02 11:11:37 +01003118 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01003119 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02003120
Radek Krejci37b756f2016-01-18 10:15:03 +01003121 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003122 if (!type->info.lref.target) {
3123 ret = EXIT_FAILURE;
3124 goto finish;
3125 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003126 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003127 if (!ret) {
3128 /* adopt possibly changed default value to its canonical form */
3129 if (*value) {
3130 *value = dflt;
3131 }
3132 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003133 } else {
Radek Krejcia571d942017-02-24 09:26:49 +01003134 if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, NULL, 1, 1)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003135 /* possible forward reference */
3136 ret = 1;
Radek Krejcibad2f172016-08-02 11:04:15 +02003137 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003138 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003139 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3140 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3141 /* we have refined bits/enums */
3142 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
3143 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003144 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003145 }
3146 }
Radek Krejci51673202016-11-01 17:00:32 +01003147 } else {
3148 /* success - adopt canonical form from the node into the default value */
3149 if (dflt != node.value_str) {
3150 /* this can happen only if we have non-inherited default value,
3151 * inherited default values are already in canonical form */
3152 assert(dflt == *value);
3153 *value = node.value_str;
3154 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003155 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003156 }
3157
3158finish:
3159 if (node.value_type == LY_TYPE_BITS) {
3160 free(node.value.bit);
3161 }
3162 free((char *)node.schema->name);
3163 free(node.schema);
3164
3165 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003166}
3167
Michal Vasko730dfdf2015-08-11 14:48:05 +02003168/**
3169 * @brief Check a key for mandatory attributes. Logs directly.
3170 *
3171 * @param[in] key The key to check.
3172 * @param[in] flags What flags to check.
3173 * @param[in] list The list of all the keys.
3174 * @param[in] index Index of the key in the key list.
3175 * @param[in] name The name of the keys.
3176 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003177 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003178 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003179 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003180static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003181check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003182{
Radek Krejciadb57612016-02-16 13:34:34 +01003183 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003184 char *dup = NULL;
3185 int j;
3186
3187 /* existence */
3188 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003189 if (name[len] != '\0') {
3190 dup = strdup(name);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003191 LY_CHECK_ERR_RETURN(!dup, LOGMEM, -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003192 dup[len] = '\0';
3193 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003194 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003195 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003196 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003197 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003198 }
3199
3200 /* uniqueness */
3201 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003202 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003203 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003204 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003205 }
3206 }
3207
3208 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003209 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003210 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003211 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003212 }
3213
3214 /* type of the leaf is not built-in empty */
Radek Krejcic3738db2016-09-15 15:51:21 +02003215 if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003216 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003217 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003218 }
3219
3220 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003221 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003222 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003223 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003224 }
3225
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003226 /* key is not placed from augment */
3227 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003228 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01003229 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003230 return -1;
3231 }
3232
Radek Krejci3f21ada2016-08-01 13:34:31 +02003233 /* key is not when/if-feature -conditional */
3234 j = 0;
3235 if (key->when || (key->iffeature_size && (j = 1))) {
3236 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
Michal Vasko51e5c582017-01-19 14:16:39 +01003237 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003238 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003239 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003240 }
3241
Michal Vasko0b85aa82016-03-07 14:37:43 +01003242 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003243}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003244
3245/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003246 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003247 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003248 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003249 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003250 *
3251 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3252 */
3253int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003254resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003255{
Radek Krejci581ce772015-11-10 17:22:40 +01003256 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003257 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003258
Michal Vaskodc300b02017-04-07 14:09:20 +02003259 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003260 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003261 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003262 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003263 if (rc > 0) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003264 LOGVAL(LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003265 } else if (rc == -2) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003266 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003267 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003268 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003269 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003270 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003271 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003272 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003273 }
Radek Krejci581ce772015-11-10 17:22:40 +01003274 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003275 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003276 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003277 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003278 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003279 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003280 }
3281
Radek Krejcicf509982015-12-15 09:22:44 +01003282 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003283 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3284 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003285 return -1;
3286 }
3287
Radek Krejcid09d1a52016-08-11 14:05:45 +02003288 /* check that all unique's targets are of the same config type */
3289 if (*trg_type) {
3290 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
3291 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003292 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003293 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3294 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3295 return -1;
3296 }
3297 } else {
3298 /* first unique */
3299 if (leaf->flags & LYS_CONFIG_W) {
3300 *trg_type = 1;
3301 } else {
3302 *trg_type = 2;
3303 }
3304 }
3305
Radek Krejcica7efb72016-01-18 13:06:01 +01003306 /* set leaf's unique flag */
3307 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3308
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003309 return EXIT_SUCCESS;
3310
3311error:
3312
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003313 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003314}
3315
Radek Krejci0c0086a2016-03-24 15:20:28 +01003316void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003317unres_data_del(struct unres_data *unres, uint32_t i)
3318{
3319 /* there are items after the one deleted */
3320 if (i+1 < unres->count) {
3321 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003322 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003323
3324 /* deleting the last item */
3325 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003326 free(unres->node);
3327 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003328 }
3329
3330 /* if there are no items after and it is not the last one, just move the counter */
3331 --unres->count;
3332}
3333
Michal Vasko0491ab32015-08-19 14:28:29 +02003334/**
3335 * @brief Resolve (find) a data node from a specific module. Does not log.
3336 *
3337 * @param[in] mod Module to search in.
3338 * @param[in] name Name of the data node.
3339 * @param[in] nam_len Length of the name.
3340 * @param[in] start Data node to start the search from.
3341 * @param[in,out] parents Resolved nodes. If there are some parents,
3342 * they are replaced (!!) with the resolvents.
3343 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003344 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003345 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003346static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003347resolve_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 +02003348{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003349 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003350 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003351 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003352
Michal Vasko23b61ec2015-08-19 11:19:50 +02003353 if (!parents->count) {
3354 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003355 parents->node = malloc(sizeof *parents->node);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003356 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003357 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003358 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003359 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003360 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003361 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003362 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003363 continue;
3364 }
3365 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003366 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003367 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003368 && node->schema->name[nam_len] == '\0') {
3369 /* matching target */
3370 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003371 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003372 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003373 flag = 1;
3374 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003375 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003376 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003377 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003378 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM, EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003379 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003380 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003381 }
3382 }
3383 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003384
3385 if (!flag) {
3386 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003387 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003388 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003389 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003390 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003391 }
3392
Michal Vasko0491ab32015-08-19 14:28:29 +02003393 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003394}
3395
Michal Vaskoe27516a2016-10-10 17:55:31 +00003396static int
Michal Vasko1c007172017-03-10 10:20:44 +01003397resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path)
Michal Vaskoe27516a2016-10-10 17:55:31 +00003398{
3399 int dep1, dep2;
3400 const struct lys_node *node;
3401
3402 if (lys_parent(op_node)) {
3403 /* inner operation (notif/action) */
3404 if (abs_path) {
3405 return 1;
3406 } else {
3407 /* compare depth of both nodes */
3408 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3409 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3410 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3411 return 1;
3412 }
3413 }
3414 } else {
3415 /* top-level operation (notif/rpc) */
3416 if (op_node != first_node) {
3417 return 1;
3418 }
3419 }
3420
3421 return 0;
3422}
3423
Michal Vasko730dfdf2015-08-11 14:48:05 +02003424/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003425 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003426 *
Michal Vaskobb211122015-08-19 14:03:11 +02003427 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003428 * @param[in] context_node Predicate context node (where the predicate is placed).
3429 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003430 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003431 *
Michal Vasko184521f2015-09-24 13:14:26 +02003432 * @return 0 on forward reference, otherwise the number
3433 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003434 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003435 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003436static int
Michal Vasko1c007172017-03-10 10:20:44 +01003437resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3438 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003439{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003440 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003441 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003442 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003443 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3444 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko1f76a282015-08-04 16:16:53 +02003445
3446 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003447 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003448 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003449 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003450 return -parsed+i;
3451 }
3452 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003453 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003454
Michal Vasko58090902015-08-13 14:04:15 +02003455 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003456 if (sour_pref) {
3457 trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len);
3458 } else {
3459 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003460 }
Michal Vaskobb520442017-05-23 10:55:18 +02003461 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003462 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003463 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003464 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003465 }
3466
3467 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003468 dest_parent_times = 0;
3469 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003470 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3471 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003472 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 +02003473 return -parsed;
3474 }
3475 pke_parsed += i;
3476
Radek Krejciadb57612016-02-16 13:34:34 +01003477 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003478 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3479 && !((struct lys_node_augment *)dst_node->parent)->target) {
3480 /* we are in an unresolved augment, cannot evaluate */
3481 LOGVAL(LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
3482 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3483 return 0;
3484 }
3485
Michal Vaskofbaead72016-10-07 10:54:48 +02003486 /* path is supposed to be evaluated in data tree, so we have to skip
3487 * all schema nodes that cannot be instantiated in data tree */
3488 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003489 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003490 dst_node = lys_parent(dst_node));
3491
Michal Vasko1f76a282015-08-04 16:16:53 +02003492 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003493 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003494 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003495 }
3496 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003497 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003498 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003499 if (dest_pref) {
3500 trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len);
3501 } else {
3502 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003503 }
Michal Vaskobb520442017-05-23 10:55:18 +02003504 rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003505 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003506 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003507 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003508 }
3509
Michal Vaskoe27516a2016-10-10 17:55:31 +00003510 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003511 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003512 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003513 }
3514 first_iter = 0;
3515 }
3516
Michal Vasko1f76a282015-08-04 16:16:53 +02003517 if (pke_len == pke_parsed) {
3518 break;
3519 }
3520
Michal Vaskobb520442017-05-23 10:55:18 +02003521 if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
Michal Vasko1f76a282015-08-04 16:16:53 +02003522 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003523 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02003524 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003525 return -parsed;
3526 }
3527 pke_parsed += i;
3528 }
3529
3530 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003531 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskobb520442017-05-23 10:55:18 +02003532 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path - parsed);
Michal Vasko51e5c582017-01-19 14:16:39 +01003533 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003534 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003535 return -parsed;
3536 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003537 } while (has_predicate);
3538
3539 return parsed;
3540}
3541
Michal Vasko730dfdf2015-08-11 14:48:05 +02003542/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003543 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003544 *
Michal Vaskobb211122015-08-19 14:03:11 +02003545 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003546 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003547 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003548 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003549 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003550 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003551static int
Michal Vasko1c007172017-03-10 10:20:44 +01003552resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003553{
Michal Vaskoe27516a2016-10-10 17:55:31 +00003554 const struct lys_node *node, *op_node = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02003555 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003556 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02003557 const char *id, *prefix, *name;
3558 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003559 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003560
Michal Vasko184521f2015-09-24 13:14:26 +02003561 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003562 parent_times = 0;
3563 id = path;
3564
Michal Vasko1c007172017-03-10 10:20:44 +01003565 /* find operation schema we are in */
3566 for (op_node = lys_parent(parent);
3567 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
3568 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02003569
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003570 cur_module = lys_node_module(parent);
Michal Vasko1f76a282015-08-04 16:16:53 +02003571 do {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003572 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Michal Vasko1c007172017-03-10 10:20:44 +01003573 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003574 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003575 }
3576 id += i;
3577
Michal Vaskobb520442017-05-23 10:55:18 +02003578 /* get the current module */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003579 tmp_mod = prefix ? lys_get_import_module(cur_module, NULL, 0, prefix, pref_len) : cur_module;
3580 if (!tmp_mod) {
Michal Vaskobb520442017-05-23 10:55:18 +02003581 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3582 return EXIT_FAILURE;
3583 }
3584 last_aug = NULL;
3585
Michal Vasko184521f2015-09-24 13:14:26 +02003586 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003587 if (parent_times == -1) {
Michal Vaskobb520442017-05-23 10:55:18 +02003588 /* use module data */
3589 node = NULL;
Radek Krejci990af1f2016-11-09 13:53:36 +01003590
Michal Vasko1f76a282015-08-04 16:16:53 +02003591 } else if (parent_times > 0) {
Michal Vaskobb520442017-05-23 10:55:18 +02003592 /* we are looking for the right parent */
3593 for (i = 0, node = parent; i < parent_times; i++) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003594 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
3595 && !((struct lys_node_augment *)node->parent)->target) {
3596 /* we are in an unresolved augment, cannot evaluate */
3597 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node->parent,
3598 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path);
3599 return EXIT_FAILURE;
3600 }
3601
Radek Krejci3a5501d2016-07-18 22:03:34 +02003602 /* path is supposed to be evaluated in data tree, so we have to skip
3603 * all schema nodes that cannot be instantiated in data tree */
3604 for (node = lys_parent(node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003605 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Radek Krejci3a5501d2016-07-18 22:03:34 +02003606 node = lys_parent(node));
3607
Michal Vasko1f76a282015-08-04 16:16:53 +02003608 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003609 if (i == parent_times - 1) {
3610 /* top-level */
3611 break;
3612 }
3613
3614 /* higher than top-level */
Michal Vaskoe9914d12016-10-07 14:32:37 +02003615 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003616 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003617 }
3618 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003619 } else {
3620 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003621 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003622 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003623 }
3624
Michal Vaskobb520442017-05-23 10:55:18 +02003625 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
3626 * - useless to search for that in augments) */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003627 if (!tmp_mod->implemented && node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003628get_next_augment:
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003629 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
Michal Vaskobb520442017-05-23 10:55:18 +02003630 }
3631
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003632 rc = lys_getnext_data(tmp_mod, (last_aug ? (struct lys_node *)last_aug : node), name, nam_len, LYS_LIST
Michal Vaskobb520442017-05-23 10:55:18 +02003633 | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003634 if (rc) {
Michal Vaskobb520442017-05-23 10:55:18 +02003635 if (last_aug) {
3636 goto get_next_augment;
3637 }
Michal Vasko1c007172017-03-10 10:20:44 +01003638 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003639 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003640 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003641
Michal Vaskoe27516a2016-10-10 17:55:31 +00003642 if (first_iter) {
3643 /* set external dependency flag, we can decide based on the first found node */
Michal Vasko1c007172017-03-10 10:20:44 +01003644 if (op_node && parent_times &&
3645 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003646 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003647 }
3648 first_iter = 0;
3649 }
3650
Michal Vasko1f76a282015-08-04 16:16:53 +02003651 if (has_predicate) {
3652 /* we have predicate, so the current result must be list */
3653 if (node->nodetype != LYS_LIST) {
Michal Vasko1c007172017-03-10 10:20:44 +01003654 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003655 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003656 }
3657
Michal Vasko1c007172017-03-10 10:20:44 +01003658 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
Michal Vaskobb520442017-05-23 10:55:18 +02003659 if (!i) {
3660 return EXIT_FAILURE;
3661 } else if (i < 0) {
3662 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003663 }
3664 id += i;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003665 has_predicate = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003666 }
3667 } while (id[0]);
3668
Michal Vaskoca917682016-07-25 11:00:37 +02003669 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
Radek Krejci2a5a9602016-11-04 10:21:13 +01003670 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
Michal Vasko1c007172017-03-10 10:20:44 +01003671 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko51e5c582017-01-19 14:16:39 +01003672 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003673 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003674 }
3675
Radek Krejcicf509982015-12-15 09:22:44 +01003676 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003677 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003678 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003679 return -1;
3680 }
3681
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003682 if (ret) {
3683 *ret = node;
3684 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003685
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003686 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003687}
3688
Michal Vasko730dfdf2015-08-11 14:48:05 +02003689/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003690 * @brief Resolve instance-identifier predicate in JSON data format.
3691 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003692 *
Michal Vaskobb211122015-08-19 14:03:11 +02003693 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003694 * @param[in,out] node_match Nodes matching the restriction without
3695 * the predicate. Nodes not satisfying
3696 * the predicate are removed.
3697 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003698 * @return Number of characters successfully parsed,
3699 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003700 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003701static int
Michal Vasko1c007172017-03-10 10:20:44 +01003702resolve_instid_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003703{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003704 /* ... /node[target = value] ... */
Michal Vaskob2f40be2016-09-08 16:03:48 +02003705 struct lyd_node *target;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003706 const char *model, *name, *value;
Michal Vaskob2f40be2016-09-08 16:03:48 +02003707 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed, pred_iter, k;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003708 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003709
Michal Vasko1f2cc332015-08-19 11:18:32 +02003710 assert(pred && node_match->count);
3711
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003712 idx = -1;
3713 parsed = 0;
3714
Michal Vaskob2f40be2016-09-08 16:03:48 +02003715 pred_iter = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003716 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02003717 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003718 return -parsed+i;
3719 }
3720 parsed += i;
3721 pred += i;
3722
3723 if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02003724 /* pos */
3725 assert(!value);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003726 idx = atoi(name);
Michal Vaskob2f40be2016-09-08 16:03:48 +02003727 } else if (name[0] != '.') {
3728 /* list keys */
3729 if (pred_iter < 0) {
3730 pred_iter = 1;
3731 } else {
3732 ++pred_iter;
3733 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003734 }
3735
Michal Vaskof2f28a12016-09-09 12:43:06 +02003736 for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003737 /* target */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003738 if (name[0] == '.') {
Michal Vaskob2f40be2016-09-08 16:03:48 +02003739 /* leaf-list value */
Michal Vaskocf024702015-10-08 15:01:42 +02003740 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003741 goto remove_instid;
3742 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02003743
3744 target = node_match->node[j];
3745 /* check the value */
3746 if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len)
3747 || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) {
3748 goto remove_instid;
3749 }
3750
3751 } else if (!value) {
3752 /* keyless list position */
3753 if ((node_match->node[j]->schema->nodetype != LYS_LIST)
3754 || ((struct lys_node_list *)node_match->node[j]->schema)->keys) {
3755 goto remove_instid;
3756 }
3757
3758 if (idx != cur_idx) {
3759 goto remove_instid;
3760 }
3761
3762 } else {
3763 /* list key value */
Michal Vaskocf024702015-10-08 15:01:42 +02003764 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003765 goto remove_instid;
3766 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02003767
3768 /* key module must match the list module */
3769 if (strncmp(node_match->node[j]->schema->module->name, model, mod_len)
3770 || node_match->node[j]->schema->module->name[mod_len]) {
3771 goto remove_instid;
3772 }
3773 /* find the key leaf */
Michal Vasko045182c2016-09-09 12:44:07 +02003774 for (k = 1, target = node_match->node[j]->child; target && (k < pred_iter); k++, target = target->next);
Michal Vaskob2f40be2016-09-08 16:03:48 +02003775 if (!target) {
3776 goto remove_instid;
3777 }
3778 if ((struct lys_node_leaf *)target->schema !=
3779 ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) {
3780 goto remove_instid;
3781 }
3782
3783 /* check the value */
3784 if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len)
3785 || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) {
3786 goto remove_instid;
3787 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003788 }
3789
Michal Vaskob2f40be2016-09-08 16:03:48 +02003790 /* instid is ok, continue check with the next one */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003791 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003792 continue;
3793
3794remove_instid:
Michal Vaskob2f40be2016-09-08 16:03:48 +02003795 /* does not fulfill conditions, remove instid record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02003796 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003797 }
3798 } while (has_predicate);
3799
Michal Vaskob2f40be2016-09-08 16:03:48 +02003800 /* check that all list keys were specified */
3801 if ((pred_iter > 0) && node_match->count) {
Michal Vasko045182c2016-09-09 12:44:07 +02003802 j = 0;
3803 while (j < node_match->count) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02003804 assert(node_match->node[j]->schema->nodetype == LYS_LIST);
3805 if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) {
3806 /* not enough predicates, just remove the list instance */
3807 unres_data_del(node_match, j);
Michal Vasko045182c2016-09-09 12:44:07 +02003808 } else {
3809 ++j;
Michal Vaskob2f40be2016-09-08 16:03:48 +02003810 }
3811 }
3812
3813 if (!node_match->count) {
3814 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys.");
3815 }
3816 }
3817
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003818 return parsed;
3819}
3820
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003821int
Michal Vasko769f8032017-01-24 13:11:55 +01003822lys_check_xpath(struct lys_node *node, int check_place, int warn_on_fwd_ref)
Michal Vasko9e635ac2016-10-17 11:44:09 +02003823{
3824 struct lys_node *parent, *elem;
3825 struct lyxp_set set;
3826 uint32_t i;
Michal Vasko769f8032017-01-24 13:11:55 +01003827 int ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003828
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003829 if (check_place) {
3830 parent = node;
3831 while (parent) {
3832 if (parent->nodetype == LYS_GROUPING) {
3833 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02003834 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003835 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003836 if (parent->nodetype == LYS_AUGMENT) {
3837 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02003838 /* unresolved augment, skip for now (will be checked later) */
3839 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003840 } else {
3841 parent = ((struct lys_node_augment *)parent)->target;
3842 continue;
3843 }
3844 }
3845 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003846 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02003847 }
3848
Michal Vasko769f8032017-01-24 13:11:55 +01003849 ret = lyxp_node_atomize(node, &set, warn_on_fwd_ref);
3850 if (ret == -1) {
3851 return -1;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003852 }
3853
3854 for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent));
3855
3856 for (i = 0; i < set.used; ++i) {
3857 /* skip roots'n'stuff */
3858 if (set.val.snodes[i].type == LYXP_NODE_ELEM) {
3859 /* XPath expression cannot reference "lower" status than the node that has the definition */
3860 if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags,
3861 lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) {
3862 return -1;
3863 }
3864
3865 if (parent) {
3866 for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem));
3867 if (!elem) {
3868 /* not in node's RPC or notification subtree, set the flag */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003869 node->flags |= LYS_XPATH_DEP;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003870 break;
3871 }
3872 }
3873 }
3874 }
3875
3876 free(set.val.snodes);
Michal Vasko769f8032017-01-24 13:11:55 +01003877 return ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02003878}
3879
Radek Krejcif71f48f2016-10-25 16:37:24 +02003880static int
3881check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
3882{
3883 int i;
3884
3885 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01003886 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
3887 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Radek Krejcid831dd42017-03-16 12:59:30 +01003888 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.",
Radek Krejcif71f48f2016-10-25 16:37:24 +02003889 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
3890 return -1;
3891 }
3892 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
3893 * of leafref resolving (lys_leaf_add_leafref_target()) */
3894 } else if (type->base == LY_TYPE_UNION) {
3895 for (i = 0; i < type->info.uni.count; i++) {
3896 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
3897 return -1;
3898 }
3899 }
3900 }
3901 return 0;
3902}
3903
Michal Vasko9e635ac2016-10-17 11:44:09 +02003904/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003905 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02003906 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003907 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003908 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02003909 * @param[in] clear Flag to clear all config flags if parent is LYS_NOTIF, LYS_INPUT, LYS_OUTPUT, LYS_RPC.
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003910 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01003911 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02003912 *
3913 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003914 */
Michal Vasko44ab1462017-05-18 13:18:36 +02003915int
3916inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003917{
Radek Krejcif71f48f2016-10-25 16:37:24 +02003918 struct lys_node_leaf *leaf;
3919
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003920 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02003921 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003922 if (clear) {
3923 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02003924 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02003925 } else {
3926 if (node->flags & LYS_CONFIG_SET) {
3927 /* skip nodes with an explicit config value */
3928 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3929 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01003930 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02003931 return -1;
3932 }
3933 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02003934 }
Michal Vaskoe022a562016-09-27 14:24:15 +02003935
3936 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
3937 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
3938 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003939 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
3940 && !((struct lys_node_list *)node)->keys_size) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003941 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
3942 return -1;
3943 }
3944 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02003945 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02003946 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02003947 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02003948 return -1;
3949 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02003950 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3951 leaf = (struct lys_node_leaf *)node;
3952 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02003953 return -1;
3954 }
3955 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003956 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02003957
3958 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003959}
3960
Michal Vasko730dfdf2015-08-11 14:48:05 +02003961/**
Michal Vasko7178e692016-02-12 15:58:05 +01003962 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003963 *
Michal Vaskobb211122015-08-19 14:03:11 +02003964 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01003965 * @param[in] siblings Nodes where to start the search in. If set, uses augment, if not, standalone augment.
Radek Krejcib3142312016-11-09 11:04:12 +01003966 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003967 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003968 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003969 */
Michal Vasko7178e692016-02-12 15:58:05 +01003970static int
Radek Krejcib3142312016-11-09 11:04:12 +01003971resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003972{
Michal Vasko44ab1462017-05-18 13:18:36 +02003973 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02003974 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003975 struct lys_module *mod;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003976
Michal Vasko9bf4aa02017-06-12 09:24:02 +02003977 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01003978 mod = lys_main_module(aug->module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003979
Michal Vaskobb520442017-05-23 10:55:18 +02003980 /* set it as not applied for now */
3981 aug->flags |= LYS_NOTAPPLIED;
3982
Michal Vasko9bf4aa02017-06-12 09:24:02 +02003983 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02003984 if (!aug->target) {
Michal Vasko9bf4aa02017-06-12 09:24:02 +02003985 /* resolve target node */
3986 rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module),
3987 (const struct lys_node **)&aug->target);
3988 if (rc == -1) {
3989 return -1;
3990 } else if (rc > 0) {
3991 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
3992 return -1;
3993 }
3994 if (!aug->target) {
3995 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
3996 return EXIT_FAILURE;
3997 }
Michal Vasko15b36692016-08-26 15:29:54 +02003998 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003999
Michal Vaskod58d5962016-03-02 14:29:41 +01004000 /* check for mandatory nodes - if the target node is in another module
4001 * the added nodes cannot be mandatory
4002 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004003 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4004 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004005 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004006 }
4007
Michal Vasko07e89ef2016-03-03 13:28:57 +01004008 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004009 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004010 LY_TREE_FOR(aug->child, sub) {
4011 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4012 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
4013 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4014 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004015 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004016 return -1;
4017 }
4018 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004019 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004020 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004021 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004022 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004023 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004024 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004025 return -1;
4026 }
4027 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004028 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004029 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004030 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004031 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004032 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004033 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004034 return -1;
4035 }
4036 }
4037 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004038 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
Michal Vasko44ab1462017-05-18 13:18:36 +02004039 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004040 return -1;
4041 }
4042
Radek Krejcic071c542016-01-27 14:57:51 +01004043 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004044 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004045 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004046 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004047 }
4048 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004049
Michal Vasko44ab1462017-05-18 13:18:36 +02004050 if (!aug->child) {
4051 /* empty augment, nothing to connect, but it is techincally applied */
4052 LOGWRN("Augment \"%s\" without children.", aug->target_name);
4053 aug->flags &= ~LYS_NOTAPPLIED;
4054 } else if (mod->implemented && apply_aug(aug, unres)) {
4055 /* we tried to connect it, we failed */
4056 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004057 }
4058
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004059 return EXIT_SUCCESS;
4060}
4061
Radek Krejcie534c132016-11-23 13:32:31 +01004062static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004063resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004064{
4065 enum LY_VLOG_ELEM vlog_type;
4066 void *vlog_node;
4067 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004068 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004069 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004070 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004071 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004072 struct lys_ext_instance *tmp_ext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004073 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004074
4075 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004076 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004077 vlog_node = info->parent;
4078 vlog_type = LY_VLOG_LYS;
4079 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004080 case LYEXT_PAR_MODULE:
4081 case LYEXT_PAR_IMPORT:
4082 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004083 vlog_node = NULL;
4084 vlog_type = LY_VLOG_LYS;
4085 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004086 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004087 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004088 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004089 break;
4090 }
4091
4092 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004093 /* YIN */
4094
Radek Krejcie534c132016-11-23 13:32:31 +01004095 /* get the module where the extension is supposed to be defined */
Radek Krejcia7db9702017-01-20 12:55:14 +01004096 mod = lys_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004097 if (!mod) {
4098 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004099 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004100 }
4101
4102 /* find the extension definition */
4103 e = NULL;
4104 for (i = 0; i < mod->extensions_size; i++) {
4105 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4106 e = &mod->extensions[i];
4107 break;
4108 }
4109 }
4110 /* try submodules */
4111 for (j = 0; !e && j < mod->inc_size; j++) {
4112 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4113 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4114 e = &mod->inc[j].submodule->extensions[i];
4115 break;
4116 }
4117 }
4118 }
4119 if (!e) {
4120 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
4121 return EXIT_FAILURE;
4122 }
4123
4124 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004125
Radek Krejci72b35992017-01-04 16:27:44 +01004126 if (e->plugin && e->plugin->check_position) {
4127 /* common part - we have plugin with position checking function, use it first */
4128 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4129 /* extension is not allowed here */
4130 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name);
4131 return -1;
4132 }
4133 }
4134
Radek Krejci8d6b7422017-02-03 14:42:13 +01004135 /* extension type-specific part - allocation */
4136 if (e->plugin) {
4137 etype = e->plugin->type;
4138 } else {
4139 /* default type */
4140 etype = LYEXT_FLAG;
4141 }
4142 switch (etype) {
4143 case LYEXT_FLAG:
4144 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4145 break;
4146 case LYEXT_COMPLEX:
4147 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4148 break;
4149 case LYEXT_ERR:
4150 /* we never should be here */
4151 LOGINT;
4152 return -1;
4153 }
Radek Krejcia8d111f2017-05-31 13:57:37 +02004154 LY_CHECK_ERR_RETURN(!*ext, LOGMEM, -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004155
4156 /* common part for all extension types */
4157 (*ext)->def = e;
4158 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004159 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004160 (*ext)->insubstmt = info->substmt;
4161 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004162 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004163
4164 if (!(e->flags & LYS_YINELEM) && e->argument) {
4165 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4166 if (!(*ext)->arg_value) {
4167 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
4168 return -1;
4169 }
4170 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4171 }
4172
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004173 (*ext)->nodetype = LYS_EXT;
4174 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004175
Radek Krejci8d6b7422017-02-03 14:42:13 +01004176 /* extension type-specific part - parsing content */
4177 switch (etype) {
4178 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004179 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4180 if (!yin->ns) {
4181 /* garbage */
4182 lyxml_free(mod->ctx, yin);
4183 continue;
4184 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4185 /* standard YANG statements are not expected here */
4186 LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
4187 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004188 } else if (yin->ns == info->data.yin->ns &&
4189 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004190 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004191 if ((*ext)->arg_value) {
Radek Krejci72b35992017-01-04 16:27:44 +01004192 LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004193 return -1;
4194 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004195 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004196 yin->content = NULL;
4197 lyxml_free(mod->ctx, yin);
4198 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004199 /* extension instance */
4200 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4201 LYEXT_SUBSTMT_SELF, 0, unres)) {
4202 return -1;
4203 }
Radek Krejci72b35992017-01-04 16:27:44 +01004204
Radek Krejci72b35992017-01-04 16:27:44 +01004205 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004206 }
Radek Krejci72b35992017-01-04 16:27:44 +01004207 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004208 break;
4209 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004210 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004211 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4212 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004213 return -1;
4214 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004215 break;
4216 default:
4217 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004218 }
Radek Krejci72b35992017-01-04 16:27:44 +01004219
4220 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4221
Radek Krejcie534c132016-11-23 13:32:31 +01004222 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004223 /* YANG */
4224
PavolVicanc1807262017-01-31 18:00:27 +01004225 ext_prefix = (char *)(*ext)->def;
4226 tmp = strchr(ext_prefix, ':');
4227 if (!tmp) {
4228 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004229 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004230 }
4231 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004232
PavolVicanc1807262017-01-31 18:00:27 +01004233 /* get the module where the extension is supposed to be defined */
4234 mod = lys_get_import_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0);
4235 if (!mod) {
4236 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
4237 return EXIT_FAILURE;
4238 }
4239
4240 /* find the extension definition */
4241 e = NULL;
4242 for (i = 0; i < mod->extensions_size; i++) {
4243 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4244 e = &mod->extensions[i];
4245 break;
4246 }
4247 }
4248 /* try submodules */
4249 for (j = 0; !e && j < mod->inc_size; j++) {
4250 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4251 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4252 e = &mod->inc[j].submodule->extensions[i];
4253 break;
4254 }
4255 }
4256 }
4257 if (!e) {
4258 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
4259 return EXIT_FAILURE;
4260 }
4261
4262 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4263
4264 if (e->plugin && e->plugin->check_position) {
4265 /* common part - we have plugin with position checking function, use it first */
4266 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4267 /* extension is not allowed here */
4268 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01004269 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004270 }
4271 }
4272
PavolVican22e88682017-02-14 22:38:18 +01004273 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01004274 (*ext)->flags &= ~LYEXT_OPT_YANG;
PavolVicanc1807262017-01-31 18:00:27 +01004275 (*ext)->def = e;
4276 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01004277 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican22e88682017-02-14 22:38:18 +01004278
PavolVicanb0d84102017-02-15 16:32:42 +01004279 if (e->argument && !(*ext)->arg_value) {
4280 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
4281 goto error;
4282 }
4283
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004284 (*ext)->module = info->mod;
4285 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004286
PavolVican22e88682017-02-14 22:38:18 +01004287 /* extension type-specific part */
4288 if (e->plugin) {
4289 etype = e->plugin->type;
4290 } else {
4291 /* default type */
4292 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01004293 }
PavolVican22e88682017-02-14 22:38:18 +01004294 switch (etype) {
4295 case LYEXT_FLAG:
4296 /* nothing change */
4297 break;
4298 case LYEXT_COMPLEX:
4299 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004300 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM, error);
PavolVican22e88682017-02-14 22:38:18 +01004301 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
4302 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01004303 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01004304 if (info->data.yang) {
4305 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01004306 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
4307 (struct lys_ext_instance_complex*)(*ext))) {
4308 goto error;
4309 }
4310 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
4311 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01004312 goto error;
4313 }
PavolVicana3876672017-02-21 15:49:51 +01004314 }
4315 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
4316 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01004317 }
PavolVican22e88682017-02-14 22:38:18 +01004318 break;
4319 case LYEXT_ERR:
4320 /* we never should be here */
4321 LOGINT;
4322 goto error;
4323 }
4324
PavolVican22e88682017-02-14 22:38:18 +01004325 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
4326 goto error;
4327 }
4328 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01004329 }
4330
4331 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01004332error:
4333 free(ext_prefix);
4334 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01004335}
4336
Michal Vasko730dfdf2015-08-11 14:48:05 +02004337/**
Pavol Vican855ca622016-09-05 13:07:54 +02004338 * @brief Resolve (find) choice default case. Does not log.
4339 *
4340 * @param[in] choic Choice to use.
4341 * @param[in] dflt Name of the default case.
4342 *
4343 * @return Pointer to the default node or NULL.
4344 */
4345static struct lys_node *
4346resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4347{
4348 struct lys_node *child, *ret;
4349
4350 LY_TREE_FOR(choic->child, child) {
4351 if (child->nodetype == LYS_USES) {
4352 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4353 if (ret) {
4354 return ret;
4355 }
4356 }
4357
4358 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02004359 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02004360 return child;
4361 }
4362 }
4363
4364 return NULL;
4365}
4366
4367/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02004368 * @brief Resolve uses, apply augments, refines. Logs directly.
4369 *
Michal Vaskobb211122015-08-19 14:03:11 +02004370 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004371 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004372 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004373 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004374 */
Michal Vasko184521f2015-09-24 13:14:26 +02004375static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004376resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004377{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004378 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02004379 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02004380 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02004381 struct lys_node_leaflist *llist;
4382 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02004383 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004384 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004385 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004386 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004387 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004388 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004389
Michal Vasko71e1aa82015-08-12 12:17:51 +02004390 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01004391
Radek Krejci93def382017-05-24 15:33:48 +02004392 /* check that the grouping is resolved (no unresolved uses inside) */
4393 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02004394
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004395 if (!uses->grp->child) {
4396 /* grouping without children, warning was already displayed */
4397 return EXIT_SUCCESS;
4398 }
4399
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004400 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004401 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01004402 if (node_aux->nodetype & LYS_GROUPING) {
4403 /* do not instantiate groupings from groupings */
4404 continue;
4405 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01004406 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004407 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004408 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
Michal Vasko51e5c582017-01-19 14:16:39 +01004409 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004410 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004411 }
Pavol Vican55abd332016-07-12 15:54:49 +02004412 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01004413 LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02004414 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004415 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004416 }
4417 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004418 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004419
Michal Vaskodef0db12015-10-07 13:22:48 +02004420 /* we managed to copy the grouping, the rest must be possible to resolve */
4421
Pavol Vican855ca622016-09-05 13:07:54 +02004422 if (uses->refine_size) {
4423 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004424 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004425 }
4426
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004427 /* apply refines */
4428 for (i = 0; i < uses->refine_size; i++) {
4429 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01004430 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
4431 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02004432 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004433 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004434 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004435 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004436 }
4437
Radek Krejci1d82ef62015-08-07 14:44:40 +02004438 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004439 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004440 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004441 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004442 }
Pavol Vican855ca622016-09-05 13:07:54 +02004443 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004444
4445 /* description on any nodetype */
4446 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004447 lydict_remove(ctx, node->dsc);
4448 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004449 }
4450
4451 /* reference on any nodetype */
4452 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004453 lydict_remove(ctx, node->ref);
4454 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004455 }
4456
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004457 /* config on any nodetype,
4458 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
4459 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004460 node->flags &= ~LYS_CONFIG_MASK;
4461 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004462 }
4463
4464 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02004465 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004466 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004467 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02004468 leaf = (struct lys_node_leaf *)node;
4469
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004470 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02004471 lydict_remove(ctx, leaf->dflt);
4472 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
4473
4474 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01004475 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
4476 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004477 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004478 }
Radek Krejci200bf712016-08-16 17:11:04 +02004479 } else if (node->nodetype == LYS_LEAFLIST) {
4480 /* leaf-list */
4481 llist = (struct lys_node_leaflist *)node;
4482
4483 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01004484 for (j = 0; j < llist->dflt_size; j++) {
4485 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02004486 }
4487 free(llist->dflt);
4488
4489 /* copy the default set from refine */
Radek Krejcia8d111f2017-05-31 13:57:37 +02004490 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
4491 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, fail);
Radek Krejci200bf712016-08-16 17:11:04 +02004492 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01004493 for (j = 0; j < llist->dflt_size; j++) {
4494 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02004495 }
4496
4497 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01004498 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01004499 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01004500 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02004501 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02004502 }
4503 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004504 }
4505 }
4506
4507 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004508 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01004509 /* remove current value */
4510 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004511
Radek Krejcibf285832017-01-26 16:05:41 +01004512 /* set new value */
4513 node->flags |= (rfn->flags & LYS_MAND_MASK);
4514
Pavol Vican855ca622016-09-05 13:07:54 +02004515 if (rfn->flags & LYS_MAND_TRUE) {
4516 /* check if node has default value */
4517 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004518 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4519 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004520 goto fail;
4521 }
4522 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004523 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses,
4524 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004525 goto fail;
4526 }
4527 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004528 }
4529
4530 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004531 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4532 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4533 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004534 }
4535
4536 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004537 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004538 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004539 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004540 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004541 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004542 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004543 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004544 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004545 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004546 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004547 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004548 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004549 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004550 }
4551 }
4552
4553 /* must in leaf, leaf-list, list, container or anyxml */
4554 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004555 switch (node->nodetype) {
4556 case LYS_LEAF:
4557 old_size = &((struct lys_node_leaf *)node)->must_size;
4558 old_must = &((struct lys_node_leaf *)node)->must;
4559 break;
4560 case LYS_LEAFLIST:
4561 old_size = &((struct lys_node_leaflist *)node)->must_size;
4562 old_must = &((struct lys_node_leaflist *)node)->must;
4563 break;
4564 case LYS_LIST:
4565 old_size = &((struct lys_node_list *)node)->must_size;
4566 old_must = &((struct lys_node_list *)node)->must;
4567 break;
4568 case LYS_CONTAINER:
4569 old_size = &((struct lys_node_container *)node)->must_size;
4570 old_must = &((struct lys_node_container *)node)->must;
4571 break;
4572 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004573 case LYS_ANYDATA:
4574 old_size = &((struct lys_node_anydata *)node)->must_size;
4575 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004576 break;
4577 default:
4578 LOGINT;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004579 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004580 }
4581
4582 size = *old_size + rfn->must_size;
4583 must = realloc(*old_must, size * sizeof *rfn->must);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004584 LY_CHECK_ERR_GOTO(!must, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004585 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01004586 must[j].ext_size = rfn->must[k].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004587 lys_ext_dup(rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004588 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02004589 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
4590 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
4591 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
4592 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
4593 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004594 }
4595
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004596 *old_must = must;
4597 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02004598
4599 /* check XPath dependencies again */
4600 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
4601 goto fail;
4602 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004603 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02004604
4605 /* if-feature in leaf, leaf-list, list, container or anyxml */
4606 if (rfn->iffeature_size) {
4607 old_size = &node->iffeature_size;
4608 old_iff = &node->iffeature;
4609
4610 size = *old_size + rfn->iffeature_size;
4611 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004612 LY_CHECK_ERR_GOTO(!iff, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004613 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
4614 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004615 if (usize1) {
4616 /* there is something to duplicate */
4617 /* duplicate compiled expression */
4618 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
4619 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004620 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004621 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004622
4623 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02004624 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004625 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM, fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004626 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004627 }
4628 }
4629
4630 *old_iff = iff;
4631 *old_size = size;
4632 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004633 }
4634
4635 /* apply augments */
4636 for (i = 0; i < uses->augment_size; i++) {
Radek Krejcib3142312016-11-09 11:04:12 +01004637 rc = resolve_augment(&uses->augment[i], uses->child, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004638 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004639 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004640 }
4641 }
4642
Pavol Vican855ca622016-09-05 13:07:54 +02004643 /* check refines */
4644 for (i = 0; i < uses->refine_size; i++) {
4645 node = refine_nodes[i];
4646 rfn = &uses->refine[i];
4647
4648 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004649 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02004650 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01004651 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02004652 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
4653 (rfn->flags & LYS_CONFIG_W)) {
4654 /* setting config true under config false is prohibited */
4655 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004656 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004657 "changing config from 'false' to 'true' is prohibited while "
4658 "the target's parent is still config 'false'.");
4659 goto fail;
4660 }
4661
4662 /* inherit config change to the target children */
4663 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4664 if (rfn->flags & LYS_CONFIG_W) {
4665 if (iter->flags & LYS_CONFIG_SET) {
4666 /* config is set explicitely, go to next sibling */
4667 next = NULL;
4668 goto nextsibling;
4669 }
4670 } else { /* LYS_CONFIG_R */
4671 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4672 /* error - we would have config data under status data */
4673 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004674 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004675 "changing config from 'true' to 'false' is prohibited while the target "
4676 "has still a children with explicit config 'true'.");
4677 goto fail;
4678 }
4679 }
4680 /* change config */
4681 iter->flags &= ~LYS_CONFIG_MASK;
4682 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4683
4684 /* select next iter - modified LY_TREE_DFS_END */
4685 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4686 next = NULL;
4687 } else {
4688 next = iter->child;
4689 }
4690nextsibling:
4691 if (!next) {
4692 /* try siblings */
4693 next = iter->next;
4694 }
4695 while (!next) {
4696 /* parent is already processed, go to its sibling */
4697 iter = lys_parent(iter);
4698
4699 /* no siblings, go back through parents */
4700 if (iter == node) {
4701 /* we are done, no next element to process */
4702 break;
4703 }
4704 next = iter->next;
4705 }
4706 }
4707 }
4708
4709 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004710 if (rfn->dflt_size) {
4711 if (node->nodetype == LYS_CHOICE) {
4712 /* choice */
4713 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
4714 rfn->dflt[0]);
4715 if (!((struct lys_node_choice *)node)->dflt) {
4716 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
4717 goto fail;
4718 }
4719 if (lyp_check_mandatory_choice(node)) {
4720 goto fail;
4721 }
Pavol Vican855ca622016-09-05 13:07:54 +02004722 }
4723 }
4724
4725 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02004726 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02004727 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004728 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
4729 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004730 goto fail;
4731 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02004732 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02004733 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004734 LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
4735 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004736 goto fail;
4737 }
4738 }
4739
4740 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004741 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02004742 if (node->nodetype == LYS_LEAFLIST) {
4743 llist = (struct lys_node_leaflist *)node;
4744 if (llist->dflt_size && llist->min) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004745 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
4746 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004747 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
4748 goto fail;
4749 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004750 } else if (node->nodetype == LYS_LEAF) {
4751 leaf = (struct lys_node_leaf *)node;
4752 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcibdcaf242017-04-19 10:29:47 +02004753 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
4754 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004755 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
4756 goto fail;
4757 }
Pavol Vican855ca622016-09-05 13:07:54 +02004758 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004759
Pavol Vican855ca622016-09-05 13:07:54 +02004760 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004761 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02004762 for (parent = node->parent;
4763 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
4764 parent = parent->parent) {
4765 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
4766 /* stop also on presence containers */
4767 break;
4768 }
4769 }
4770 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
4771 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
4772 if (lyp_check_mandatory_choice(parent)) {
4773 goto fail;
4774 }
4775 }
4776 }
4777 }
4778 free(refine_nodes);
4779
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004780 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004781
4782fail:
4783 LY_TREE_FOR_SAFE(uses->child, next, iter) {
4784 lys_node_free(iter, NULL, 0);
4785 }
Pavol Vican855ca622016-09-05 13:07:54 +02004786 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02004787 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004788}
4789
Radek Krejci83a4bac2017-02-07 15:53:04 +01004790void
4791resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02004792{
4793 int i;
4794
4795 assert(der && base);
4796
Radek Krejci018f1f52016-08-03 16:01:20 +02004797 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02004798 /* create a set for backlinks if it does not exist */
4799 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02004800 }
Radek Krejci85a54be2016-10-20 12:39:56 +02004801 /* store backlink */
4802 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02004803
Radek Krejci85a54be2016-10-20 12:39:56 +02004804 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02004805 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01004806 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02004807 }
Radek Krejci018f1f52016-08-03 16:01:20 +02004808}
4809
Michal Vasko730dfdf2015-08-11 14:48:05 +02004810/**
4811 * @brief Resolve base identity recursively. Does not log.
4812 *
4813 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004814 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004815 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004816 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004817 *
Radek Krejci219fa612016-08-15 10:36:51 +02004818 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004819 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004820static int
Michal Vasko1e62a092015-12-01 12:27:20 +01004821resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02004822 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004823{
Michal Vaskof02e3742015-08-05 16:27:02 +02004824 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02004825 struct lys_ident *base = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004826
Radek Krejcicf509982015-12-15 09:22:44 +01004827 assert(ret);
4828
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004829 /* search module */
4830 for (i = 0; i < module->ident_size; i++) {
4831 if (!strcmp(basename, module->ident[i].name)) {
4832
4833 if (!ident) {
4834 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004835 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01004836 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004837 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004838 }
4839
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004840 base = &module->ident[i];
4841 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004842 }
4843 }
4844
4845 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004846 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
4847 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
4848 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004849
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004850 if (!ident) {
4851 *ret = &module->inc[j].submodule->ident[i];
4852 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004853 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004854
4855 base = &module->inc[j].submodule->ident[i];
4856 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004857 }
4858 }
4859 }
4860
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01004861matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004862 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01004863 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02004864 /* is it already completely resolved? */
4865 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02004866 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02004867 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
4868
4869 /* simple check for circular reference,
4870 * the complete check is done as a side effect of using only completely
4871 * resolved identities (previous check of unres content) */
4872 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
4873 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
4874 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02004875 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02004876 }
4877
Radek Krejci06f64ed2016-08-15 11:07:44 +02004878 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01004879 }
4880 }
Radek Krejci018f1f52016-08-03 16:01:20 +02004881
Radek Krejcibabbff82016-02-19 13:31:37 +01004882 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02004883 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02004884 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004885 }
4886
Radek Krejci219fa612016-08-15 10:36:51 +02004887 /* base not found (maybe a forward reference) */
4888 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004889}
4890
Michal Vasko730dfdf2015-08-11 14:48:05 +02004891/**
4892 * @brief Resolve base identity. Logs directly.
4893 *
4894 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02004895 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004896 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01004897 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01004898 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004899 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004900 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004901 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004902static int
Michal Vaskof2d43962016-09-02 11:10:16 +02004903resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02004904 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004905{
4906 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02004907 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01004908 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02004909 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01004910 struct lys_module *mod;
4911
4912 assert((ident && !type) || (!ident && type));
4913
4914 if (!type) {
4915 /* have ident to resolve */
4916 ret = &target;
4917 flags = ident->flags;
4918 mod = ident->module;
4919 } else {
4920 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02004921 ++type->info.ident.count;
4922 type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004923 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM, -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02004924
4925 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01004926 flags = type->parent->flags;
4927 mod = type->parent->module;
4928 }
Michal Vaskof2006002016-04-21 16:28:15 +02004929 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004930
4931 /* search for the base identity */
4932 name = strchr(basename, ':');
4933 if (name) {
4934 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02004935 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004936 name++;
4937
Michal Vasko2d851a92015-10-20 16:16:36 +02004938 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004939 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02004940 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004941 }
4942 } else {
4943 name = basename;
4944 }
4945
Radek Krejcic071c542016-01-27 14:57:51 +01004946 /* get module where to search */
4947 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
4948 if (!module) {
4949 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01004950 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01004951 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004952 }
4953
Radek Krejcic071c542016-01-27 14:57:51 +01004954 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02004955 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
4956 if (!rc) {
4957 assert(*ret);
4958
4959 /* check status */
4960 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
4961 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
4962 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01004963 } else if (ident) {
4964 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02004965 if (lys_main_module(mod)->implemented) {
4966 /* in case of the implemented identity, maintain backlinks to it
4967 * from the base identities to make it available when resolving
4968 * data with the identity values (not implemented identity is not
4969 * allowed as an identityref value). */
4970 resolve_identity_backlink_update(ident, *ret);
4971 }
Radek Krejci219fa612016-08-15 10:36:51 +02004972 }
4973 } else if (rc == EXIT_FAILURE) {
4974 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02004975 if (type) {
4976 --type->info.ident.count;
4977 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004978 }
4979
Radek Krejci219fa612016-08-15 10:36:51 +02004980 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004981}
4982
Radek Krejci9e6af732017-04-27 14:40:25 +02004983/*
4984 * 1 - true (der is derived from base)
4985 * 0 - false (der is not derived from base)
4986 */
4987static int
4988search_base_identity(struct lys_ident *der, struct lys_ident *base)
4989{
4990 int i;
4991
4992 if (der == base) {
4993 return 1;
4994 } else {
4995 for(i = 0; i < der->base_size; i++) {
4996 if (search_base_identity(der->base[i], base) == 1) {
4997 return 1;
4998 }
4999 }
5000 }
5001
5002 return 0;
5003}
5004
Michal Vasko730dfdf2015-08-11 14:48:05 +02005005/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005006 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005007 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005008 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005009 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005010 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005011 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005012 *
5013 * @return Pointer to the identity resolvent, NULL on error.
5014 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005015struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005016resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005017{
Radek Krejci9e6af732017-04-27 14:40:25 +02005018 const char *mod_name, *name;
5019 int mod_name_len, rc, i, j;
5020 int make_implemented = 0;
Radek Krejci85a54be2016-10-20 12:39:56 +02005021 unsigned int u;
Michal Vaskof2d43962016-09-02 11:10:16 +02005022 struct lys_ident *der, *cur;
Radek Krejci9e6af732017-04-27 14:40:25 +02005023 struct lys_module *imod = NULL, *m;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005024
Radek Krejci9e6af732017-04-27 14:40:25 +02005025 assert(type && ident_name && node && mod);
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005026
Michal Vaskof2d43962016-09-02 11:10:16 +02005027 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005028 return NULL;
5029 }
5030
Michal Vaskoc633ca02015-08-21 14:03:51 +02005031 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005032 if (rc < 1) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005033 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005034 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005035 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005036 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005037 return NULL;
5038 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005039
5040 m = lys_main_module(mod); /* shortcut */
5041 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5042 /* identity is defined in the same module as node */
5043 imod = m;
5044 } else if (dflt) {
5045 /* solving identityref in default definition in schema -
5046 * find the identity's module in the imported modules list to have a correct revision */
5047 for (i = 0; i < mod->imp_size; i++) {
5048 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5049 imod = mod->imp[i].module;
5050 break;
5051 }
5052 }
5053 } else {
5054 /* solving identityref in data - get the (implemented) module from the context */
5055 u = 0;
5056 while ((imod = (struct lys_module*)ly_ctx_get_module_iter(mod->ctx, &u))) {
5057 if (imod->implemented && !strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5058 break;
5059 }
5060 }
5061 }
5062 if (!imod) {
5063 goto fail;
5064 }
5065
5066 if (dflt && (m != imod || lys_main_module(type->parent->module) != mod)) {
5067 /* we are solving default statement in schema AND the type is not referencing the same schema,
5068 * THEN, we may need to make the module with the identity implemented, but only if it really
5069 * contains the identity */
5070 if (!imod->implemented) {
5071 cur = NULL;
5072 /* get the identity in the module */
5073 for (i = 0; i < imod->ident_size; i++) {
5074 if (!strcmp(name, imod->ident[i].name)) {
5075 cur = &imod->ident[i];
5076 break;
5077 }
5078 }
5079 if (!cur) {
5080 /* go through includes */
5081 for (j = 0; j < imod->inc_size; j++) {
5082 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5083 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5084 cur = &imod->inc[j].submodule->ident[i];
5085 break;
5086 }
5087 }
5088 }
5089 if (!cur) {
5090 goto fail;
5091 }
5092 }
5093
5094 /* check that identity is derived from one of the type's base */
5095 while (type->der) {
5096 for (i = 0; i < type->info.ident.count; i++) {
5097 if (search_base_identity(cur, type->info.ident.ref[i])) {
5098 /* cur's base matches the type's base */
5099 make_implemented = 1;
5100 goto match;
5101 }
5102 }
5103 type = &type->der->type;
5104 }
5105 /* matching base not found */
5106 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
5107 goto fail;
5108 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005109 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005110
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005111 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005112 while (type->der) {
5113 for (i = 0; i < type->info.ident.count; ++i) {
5114 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005115
Radek Krejci85a54be2016-10-20 12:39:56 +02005116 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005117 /* there are some derived identities */
Radek Krejci85a54be2016-10-20 12:39:56 +02005118 for (u = 0; u < cur->der->number; u++) {
5119 der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005120 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005121 /* we have match */
5122 cur = der;
5123 goto match;
5124 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005125 }
5126 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005127 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005128 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005129 }
5130
Radek Krejci9e6af732017-04-27 14:40:25 +02005131fail:
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005132 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005133 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005134
5135match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005136 for (i = 0; i < cur->iffeature_size; i++) {
5137 if (!resolve_iffeature(&cur->iffeature[i])) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005138 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01005139 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005140 return NULL;
5141 }
5142 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005143 if (make_implemented) {
5144 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5145 imod->name, cur->name, mod->name);
5146 if (lys_set_implemented(imod)) {
5147 LOGERR(ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
5148 imod->name, cur->name);
5149 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
5150 goto fail;
5151 }
5152 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005153 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005154}
5155
Michal Vasko730dfdf2015-08-11 14:48:05 +02005156/**
Michal Vaskobb211122015-08-19 14:03:11 +02005157 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005158 *
Michal Vaskobb211122015-08-19 14:03:11 +02005159 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005160 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005161 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005162 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005163 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005164static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005165resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005166{
Radek Krejci93def382017-05-24 15:33:48 +02005167 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005168 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005169
Radek Krejci6ff885d2017-01-03 14:06:22 +01005170 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used
Radek Krejci93def382017-05-24 15:33:48 +02005171 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5172 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5173 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005174 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 +02005175
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005176 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005177 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5178 if (rc == -1) {
Michal Vasko92981a62016-10-14 10:25:16 +02005179 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005180 return -1;
5181 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005182 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005183 return -1;
5184 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005185 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005186 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
5187 LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping.");
5188 return -1;
5189 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005190 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005191 }
Michal Vasko92981a62016-10-14 10:25:16 +02005192 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005193 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005194 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005195 }
5196
Radek Krejci93def382017-05-24 15:33:48 +02005197 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005198 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005199 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
5200 LOGERR(LY_EINT, "Too many unresolved items (uses) inside a grouping.");
5201 return -1;
5202 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005203 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005204 } else {
5205 /* instantiate grouping only when it is completely resolved */
5206 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005207 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005208 return EXIT_FAILURE;
5209 }
5210
Radek Krejci48464ed2016-03-17 15:44:09 +01005211 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005212 if (!rc) {
5213 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005214 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005215 assert(((struct lys_node_grp *)par_grp)->unres_count);
5216 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01005217 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005218 }
Radek Krejcicf509982015-12-15 09:22:44 +01005219
5220 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005221 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005222 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005223 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01005224 return -1;
5225 }
5226
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005227 return EXIT_SUCCESS;
5228 }
5229
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005230 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005231}
5232
Michal Vasko730dfdf2015-08-11 14:48:05 +02005233/**
Michal Vasko9957e592015-08-17 15:04:09 +02005234 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005235 *
Michal Vaskobb211122015-08-19 14:03:11 +02005236 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005237 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005238 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005239 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005240 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005241static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005242resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005243{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005244 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01005245 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02005246 char *s = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005247
5248 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02005249 assert(keys_str);
5250
Radek Krejci5c08a992016-11-02 13:30:04 +01005251 if (!list->child) {
5252 /* no child, possible forward reference */
5253 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
5254 return EXIT_FAILURE;
5255 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005256 /* get the key name */
5257 if ((value = strpbrk(keys_str, " \t\n"))) {
5258 len = value - keys_str;
5259 while (isspace(value[0])) {
5260 value++;
5261 }
5262 } else {
5263 len = strlen(keys_str);
5264 }
5265
Radek Krejcia98048c2017-05-24 16:35:48 +02005266 if (list->keys[i]) {
5267 /* skip already resolved keys */
5268 goto next;
5269 }
5270
Michal Vaskobb520442017-05-23 10:55:18 +02005271 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
5272 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005273 if (rc) {
Radek Krejcia98048c2017-05-24 16:35:48 +02005274 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02005275 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005276 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005277
Radek Krejci48464ed2016-03-17 15:44:09 +01005278 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005279 /* check_key logs */
5280 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005281 }
5282
Radek Krejcicf509982015-12-15 09:22:44 +01005283 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005284 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005285 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
5286 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01005287 return -1;
5288 }
5289
Radek Krejcia98048c2017-05-24 16:35:48 +02005290 /* default value - is ignored, keep it but print a warning */
5291 if (list->keys[i]->dflt) {
5292 /* log is not hidden only in case this resolving fails and in such a case
5293 * we cannot get here
5294 */
5295 assert(*ly_vlog_hide_location());
5296 ly_vlog_hide(0);
5297 LOGWRN("Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
5298 list->keys[i]->name, s = lys_path((struct lys_node*)list));
5299 ly_vlog_hide(1);
5300 free(s);
5301 }
5302
5303next:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005304 /* prepare for next iteration */
5305 while (value && isspace(value[0])) {
5306 value++;
5307 }
5308 keys_str = value;
5309 }
5310
Michal Vaskof02e3742015-08-05 16:27:02 +02005311 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005312}
5313
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005314/**
Michal Vaskobf19d252015-10-08 15:39:17 +02005315 * @brief Resolve (check) all must conditions of \p node.
5316 * Logs directly.
5317 *
5318 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005319 * @param[in] inout_parent If set, must in input or output parent of node->schema will be resolved.
Michal Vaskobf19d252015-10-08 15:39:17 +02005320 *
5321 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
5322 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005323static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005324resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02005325{
Michal Vasko3cfa3182017-01-17 10:00:58 +01005326 int node_flags;
Michal Vaskobf19d252015-10-08 15:39:17 +02005327 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005328 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02005329 struct lys_restr *must;
5330 struct lyxp_set set;
5331
5332 assert(node);
5333 memset(&set, 0, sizeof set);
5334
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005335 if (inout_parent) {
5336 for (schema = lys_parent(node->schema);
5337 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
5338 schema = lys_parent(schema));
5339 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
5340 LOGINT;
5341 return -1;
5342 }
5343 must_size = ((struct lys_node_inout *)schema)->must_size;
5344 must = ((struct lys_node_inout *)schema)->must;
5345
Michal Vasko3cfa3182017-01-17 10:00:58 +01005346 node_flags = schema->flags;
5347
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005348 /* context node is the RPC/action */
5349 node = node->parent;
5350 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
5351 LOGINT;
5352 return -1;
5353 }
5354 } else {
5355 switch (node->schema->nodetype) {
5356 case LYS_CONTAINER:
5357 must_size = ((struct lys_node_container *)node->schema)->must_size;
5358 must = ((struct lys_node_container *)node->schema)->must;
5359 break;
5360 case LYS_LEAF:
5361 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
5362 must = ((struct lys_node_leaf *)node->schema)->must;
5363 break;
5364 case LYS_LEAFLIST:
5365 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
5366 must = ((struct lys_node_leaflist *)node->schema)->must;
5367 break;
5368 case LYS_LIST:
5369 must_size = ((struct lys_node_list *)node->schema)->must_size;
5370 must = ((struct lys_node_list *)node->schema)->must;
5371 break;
5372 case LYS_ANYXML:
5373 case LYS_ANYDATA:
5374 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
5375 must = ((struct lys_node_anydata *)node->schema)->must;
5376 break;
5377 case LYS_NOTIF:
5378 must_size = ((struct lys_node_notif *)node->schema)->must_size;
5379 must = ((struct lys_node_notif *)node->schema)->must;
5380 break;
5381 default:
5382 must_size = 0;
5383 break;
5384 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01005385
5386 node_flags = node->schema->flags;
Michal Vaskobf19d252015-10-08 15:39:17 +02005387 }
5388
5389 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005390 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005391 return -1;
5392 }
5393
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005394 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02005395
Michal Vasko8146d4c2016-05-09 15:50:29 +02005396 if (!set.val.bool) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01005397 if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005398 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
5399 } else {
5400 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
5401 if (must[i].emsg) {
Michal Vasko51e5c582017-01-19 14:16:39 +01005402 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005403 }
5404 if (must[i].eapptag) {
5405 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
5406 }
5407 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02005408 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005409 }
5410 }
5411
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005412 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02005413}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005414
Michal Vaskobf19d252015-10-08 15:39:17 +02005415/**
Michal Vasko508a50d2016-09-07 14:50:33 +02005416 * @brief Resolve (find) when condition schema context node. Does not log.
5417 *
5418 * @param[in] schema Schema node with the when condition.
5419 * @param[out] ctx_snode When schema context node.
5420 * @param[out] ctx_snode_type Schema context node type.
5421 */
5422void
5423resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
5424{
5425 const struct lys_node *sparent;
5426
5427 /* find a not schema-only node */
5428 *ctx_snode_type = LYXP_NODE_ELEM;
5429 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
5430 if (schema->nodetype == LYS_AUGMENT) {
5431 sparent = ((struct lys_node_augment *)schema)->target;
5432 } else {
5433 sparent = schema->parent;
5434 }
5435 if (!sparent) {
5436 /* context node is the document root (fake root in our case) */
5437 if (schema->flags & LYS_CONFIG_W) {
5438 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
5439 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02005440 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02005441 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02005442 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskob94a5e42016-09-08 14:01:56 +02005443 schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0);
Michal Vasko508a50d2016-09-07 14:50:33 +02005444 break;
5445 }
5446 schema = sparent;
5447 }
5448
5449 *ctx_snode = (struct lys_node *)schema;
5450}
5451
5452/**
Michal Vaskocf024702015-10-08 15:01:42 +02005453 * @brief Resolve (find) when condition context node. Does not log.
5454 *
5455 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02005456 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02005457 * @param[out] ctx_node Context node.
5458 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02005459 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02005460 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02005461 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02005462static int
5463resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
5464 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005465{
Michal Vaskocf024702015-10-08 15:01:42 +02005466 struct lyd_node *parent;
5467 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005468 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02005469 uint16_t i, data_depth, schema_depth;
5470
Michal Vasko508a50d2016-09-07 14:50:33 +02005471 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02005472
Michal Vaskofe989752016-09-08 08:47:26 +02005473 if (node_type == LYXP_NODE_ELEM) {
5474 /* standard element context node */
5475 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
5476 for (sparent = schema, schema_depth = 0;
5477 sparent;
5478 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
5479 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
5480 ++schema_depth;
5481 }
Michal Vaskocf024702015-10-08 15:01:42 +02005482 }
Michal Vaskofe989752016-09-08 08:47:26 +02005483 if (data_depth < schema_depth) {
5484 return -1;
5485 }
Michal Vaskocf024702015-10-08 15:01:42 +02005486
Michal Vasko956e8542016-08-26 09:43:35 +02005487 /* find the corresponding data node */
5488 for (i = 0; i < data_depth - schema_depth; ++i) {
5489 node = node->parent;
5490 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02005491 if (node->schema != schema) {
5492 return -1;
5493 }
Michal Vaskofe989752016-09-08 08:47:26 +02005494 } else {
5495 /* root context node */
5496 while (node->parent) {
5497 node = node->parent;
5498 }
5499 while (node->prev->next) {
5500 node = node->prev;
5501 }
Michal Vaskocf024702015-10-08 15:01:42 +02005502 }
5503
Michal Vaskoa59495d2016-08-22 09:18:58 +02005504 *ctx_node = node;
5505 *ctx_node_type = node_type;
5506 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02005507}
5508
Michal Vasko76c3bd32016-08-24 16:02:52 +02005509/**
5510 * @brief Temporarily unlink nodes as per YANG 1.1 RFC section 7.21.5 for when XPath evaluation.
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005511 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02005512 *
5513 * @param[in] snode Schema node, whose children instances need to be unlinked.
5514 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
5515 * it is moved to point to another sibling still in the original tree.
5516 * @param[in,out] ctx_node When context node, adjusted if needed.
5517 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
5518 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
5519 * Ordering may change, but there will be no semantic change.
5520 *
5521 * @return EXIT_SUCCESS on success, -1 on error.
5522 */
5523static int
5524resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
5525 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
5526{
5527 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005528 const struct lys_node *slast;
Michal Vasko76c3bd32016-08-24 16:02:52 +02005529
5530 switch (snode->nodetype) {
5531 case LYS_AUGMENT:
5532 case LYS_USES:
5533 case LYS_CHOICE:
5534 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005535 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01005536 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005537 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
5538 continue;
5539 }
5540
5541 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005542 return -1;
5543 }
5544 }
5545 break;
5546 case LYS_CONTAINER:
5547 case LYS_LIST:
5548 case LYS_LEAF:
5549 case LYS_LEAFLIST:
5550 case LYS_ANYXML:
5551 case LYS_ANYDATA:
5552 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
5553 if (elem->schema == snode) {
5554
5555 if (elem == *ctx_node) {
5556 /* We are going to unlink our context node! This normally cannot happen,
5557 * but we use normal top-level data nodes for faking a document root node,
5558 * so if this is the context node, we just use the next top-level node.
5559 * Additionally, it can even happen that there are no top-level data nodes left,
5560 * all were unlinked, so in this case we pass NULL as the context node/data tree,
5561 * lyxp_eval() can handle this special situation.
5562 */
5563 if (ctx_node_type == LYXP_NODE_ELEM) {
5564 LOGINT;
5565 return -1;
5566 }
5567
5568 if (elem->prev == elem) {
5569 /* unlinking last top-level element, use an empty data tree */
5570 *ctx_node = NULL;
5571 } else {
5572 /* in this case just use the previous/last top-level data node */
5573 *ctx_node = elem->prev;
5574 }
5575 } else if (elem == *node) {
5576 /* We are going to unlink the currently processed node. This does not matter that
5577 * much, but we would lose access to the original data tree, so just move our
5578 * pointer somewhere still inside it.
5579 */
5580 if ((*node)->prev != *node) {
5581 *node = (*node)->prev;
5582 } else {
5583 /* the processed node with sibings were all unlinked, oh well */
5584 *node = NULL;
5585 }
5586 }
5587
5588 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01005589 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005590 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005591 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005592 LOGINT;
5593 return -1;
5594 }
5595 } else {
5596 *unlinked_nodes = elem;
5597 }
5598
5599 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
5600 /* there can be only one instance */
5601 break;
5602 }
5603 }
5604 }
5605 break;
5606 default:
5607 LOGINT;
5608 return -1;
5609 }
5610
5611 return EXIT_SUCCESS;
5612}
5613
5614/**
5615 * @brief Relink the unlinked nodes back.
5616 *
5617 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
5618 * we simply need a sibling from the original data tree.
5619 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
5620 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
5621 * or the sibling of \p unlinked_nodes.
5622 *
5623 * @return EXIT_SUCCESS on success, -1 on error.
5624 */
5625static int
5626resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
5627{
5628 struct lyd_node *elem;
5629
5630 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005631 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005632 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005633 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005634 return -1;
5635 }
5636 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005637 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005638 return -1;
5639 }
5640 }
5641 }
5642
5643 return EXIT_SUCCESS;
5644}
5645
Radek Krejci03b71f72016-03-16 11:10:09 +01005646int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005647resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01005648{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005649 int ret = 0;
5650 uint8_t must_size;
5651 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02005652
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005653 assert(node);
5654
5655 schema = node->schema;
5656
5657 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02005658 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01005659 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005660 must_size = ((struct lys_node_container *)schema)->must_size;
5661 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005662 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005663 must_size = ((struct lys_node_leaf *)schema)->must_size;
5664 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005665 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005666 must_size = ((struct lys_node_leaflist *)schema)->must_size;
5667 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005668 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005669 must_size = ((struct lys_node_list *)schema)->must_size;
5670 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005671 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005672 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005673 must_size = ((struct lys_node_anydata *)schema)->must_size;
5674 break;
5675 case LYS_NOTIF:
5676 must_size = ((struct lys_node_notif *)schema)->must_size;
5677 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005678 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005679 must_size = 0;
5680 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005681 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005682
5683 if (must_size) {
5684 ++ret;
5685 }
5686
5687 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
5688 if (!node->prev->next) {
5689 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
5690 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
5691 ret += 0x2;
5692 }
5693 }
5694
5695 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01005696}
5697
5698int
Radek Krejci46165822016-08-26 14:06:27 +02005699resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01005700{
Radek Krejci46165822016-08-26 14:06:27 +02005701 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01005702
Radek Krejci46165822016-08-26 14:06:27 +02005703 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01005704
Radek Krejci46165822016-08-26 14:06:27 +02005705 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005706 return 1;
5707 }
5708
Radek Krejci46165822016-08-26 14:06:27 +02005709 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01005710 goto check_augment;
5711
Radek Krejci46165822016-08-26 14:06:27 +02005712 while (parent) {
5713 /* stop conditions */
5714 if (!mode) {
5715 /* stop on node that can be instantiated in data tree */
5716 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
5717 break;
5718 }
5719 } else {
5720 /* stop on the specified node */
5721 if (parent == stop) {
5722 break;
5723 }
5724 }
5725
5726 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005727 return 1;
5728 }
5729check_augment:
5730
5731 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02005732 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02005733 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01005734 }
5735 parent = lys_parent(parent);
5736 }
5737
5738 return 0;
5739}
5740
Michal Vaskocf024702015-10-08 15:01:42 +02005741/**
5742 * @brief Resolve (check) all when conditions relevant for \p node.
5743 * Logs directly.
5744 *
5745 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02005746 *
Radek Krejci03b71f72016-03-16 11:10:09 +01005747 * @return
5748 * -1 - error, ly_errno is set
5749 * 0 - true "when" statement
Radek Krejci46165822016-08-26 14:06:27 +02005750 * 0, ly_vecode = LYVE_NOWHEN - false "when" statement
Radek Krejci03b71f72016-03-16 11:10:09 +01005751 * 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 +02005752 */
Radek Krejci46165822016-08-26 14:06:27 +02005753int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005754resolve_when(struct lyd_node *node, int *result, int ignore_fail)
Michal Vaskocf024702015-10-08 15:01:42 +02005755{
Michal Vasko76c3bd32016-08-24 16:02:52 +02005756 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02005757 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02005758 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005759 enum lyxp_node_type ctx_node_type;
Radek Krejci51093642016-03-29 10:14:59 +02005760 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02005761
5762 assert(node);
5763 memset(&set, 0, sizeof set);
5764
Michal Vasko78d97e22017-02-21 09:54:38 +01005765 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005766 /* make the node dummy for the evaluation */
5767 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005768 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
5769 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005770 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01005771 if (rc) {
5772 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005773 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005774 }
Radek Krejci51093642016-03-29 10:14:59 +02005775 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005776 }
5777
Radek Krejci03b71f72016-03-16 11:10:09 +01005778 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005779 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005780 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005781 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko3cfa3182017-01-17 10:00:58 +01005782 if ((ignore_fail == 1) || ((node->schema->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005783 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
5784 ((struct lys_node_container *)node->schema)->when->cond);
5785 } else {
5786 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
5787 goto cleanup;
5788 }
Michal Vaskocf024702015-10-08 15:01:42 +02005789 }
Radek Krejci51093642016-03-29 10:14:59 +02005790
5791 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005792 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005793 }
5794
Michal Vasko90fc2a32016-08-24 15:58:58 +02005795 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02005796 goto check_augment;
5797
5798 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02005799 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
5800 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02005801 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005802 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02005803 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02005804 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02005805 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005806 }
5807 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02005808
5809 unlinked_nodes = NULL;
5810 /* we do not want our node pointer to change */
5811 tmp_node = node;
5812 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
5813 if (rc) {
5814 goto cleanup;
5815 }
5816
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005817 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
5818 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005819
5820 if (unlinked_nodes && ctx_node) {
5821 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
5822 rc = -1;
5823 goto cleanup;
5824 }
5825 }
5826
Radek Krejci03b71f72016-03-16 11:10:09 +01005827 if (rc) {
5828 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005829 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005830 }
Radek Krejci51093642016-03-29 10:14:59 +02005831 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005832 }
5833
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005834 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005835 if (!set.val.bool) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01005836 if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005837 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
5838 ((struct lys_node_uses *)sparent)->when->cond);
5839 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02005840 node->when_status |= LYD_WHEN_FALSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005841 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
5842 goto cleanup;
5843 }
Michal Vaskocf024702015-10-08 15:01:42 +02005844 }
Radek Krejci51093642016-03-29 10:14:59 +02005845
5846 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005847 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005848 }
5849
5850check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02005851 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02005852 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005853 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02005854 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02005855 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02005856 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005857 }
5858 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02005859
5860 unlinked_nodes = NULL;
5861 tmp_node = node;
5862 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
5863 if (rc) {
5864 goto cleanup;
5865 }
5866
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005867 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
5868 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005869
5870 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
5871 * so the tree did not actually change and there is nothing for us to do
5872 */
5873 if (unlinked_nodes && ctx_node) {
5874 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
5875 rc = -1;
5876 goto cleanup;
5877 }
5878 }
5879
Radek Krejci03b71f72016-03-16 11:10:09 +01005880 if (rc) {
5881 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02005882 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005883 }
Radek Krejci51093642016-03-29 10:14:59 +02005884 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005885 }
5886
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005887 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005888 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005889 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko3cfa3182017-01-17 10:00:58 +01005890 if ((ignore_fail == 1) || ((sparent->parent->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005891 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01005892 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005893 } else {
5894 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
5895 goto cleanup;
5896 }
Michal Vaskocf024702015-10-08 15:01:42 +02005897 }
Radek Krejci51093642016-03-29 10:14:59 +02005898
5899 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005900 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02005901 }
5902
Michal Vasko90fc2a32016-08-24 15:58:58 +02005903 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02005904 }
5905
Radek Krejci0b7704f2016-03-18 12:16:14 +01005906 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01005907
Radek Krejci51093642016-03-29 10:14:59 +02005908cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02005909 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005910 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02005911
Radek Krejci46165822016-08-26 14:06:27 +02005912 if (result) {
5913 if (node->when_status & LYD_WHEN_TRUE) {
5914 *result = 1;
5915 } else {
5916 *result = 0;
5917 }
5918 }
5919
Radek Krejci51093642016-03-29 10:14:59 +02005920 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005921}
5922
Radek Krejcicbb473e2016-09-16 14:48:32 +02005923static int
5924check_leafref_features(struct lys_type *type)
5925{
5926 struct lys_node *iter;
5927 struct ly_set *src_parents, *trg_parents, *features;
5928 unsigned int i, j, size, x;
5929 int ret = EXIT_SUCCESS;
5930
5931 assert(type->parent);
5932
5933 src_parents = ly_set_new();
5934 trg_parents = ly_set_new();
5935 features = ly_set_new();
5936
5937 /* get parents chain of source (leafref) */
Radek Krejciecda01a2017-04-05 15:44:27 +02005938 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02005939 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
5940 continue;
5941 }
5942 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
5943 }
5944 /* get parents chain of target */
Radek Krejciecda01a2017-04-05 15:44:27 +02005945 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02005946 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
5947 continue;
5948 }
5949 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
5950 }
5951
5952 /* compare the features used in if-feature statements in the rest of both
5953 * chains of parents. The set of features used for target must be a subset
5954 * of features used for the leafref. This is not a perfect, we should compare
5955 * the truth tables but it could require too much resources, so we simplify that */
5956 for (i = 0; i < src_parents->number; i++) {
5957 iter = src_parents->set.s[i]; /* shortcut */
5958 if (!iter->iffeature_size) {
5959 continue;
5960 }
5961 for (j = 0; j < iter->iffeature_size; j++) {
5962 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
5963 for (; size; size--) {
5964 if (!iter->iffeature[j].features[size - 1]) {
5965 /* not yet resolved feature, postpone this check */
5966 ret = EXIT_FAILURE;
5967 goto cleanup;
5968 }
5969 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
5970 }
5971 }
5972 }
5973 x = features->number;
5974 for (i = 0; i < trg_parents->number; i++) {
5975 iter = trg_parents->set.s[i]; /* shortcut */
5976 if (!iter->iffeature_size) {
5977 continue;
5978 }
5979 for (j = 0; j < iter->iffeature_size; j++) {
5980 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
5981 for (; size; size--) {
5982 if (!iter->iffeature[j].features[size - 1]) {
5983 /* not yet resolved feature, postpone this check */
5984 ret = EXIT_FAILURE;
5985 goto cleanup;
5986 }
5987 if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
5988 /* the feature is not present in features set of target's parents chain */
5989 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
Michal Vasko51e5c582017-01-19 14:16:39 +01005990 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcicbb473e2016-09-16 14:48:32 +02005991 "Leafref is not conditional based on \"%s\" feature as its target.",
5992 iter->iffeature[j].features[size - 1]->name);
5993 ret = -1;
5994 goto cleanup;
5995 }
5996 }
5997 }
5998 }
5999
6000cleanup:
6001 ly_set_free(features);
6002 ly_set_free(src_parents);
6003 ly_set_free(trg_parents);
6004
6005 return ret;
6006}
6007
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02006008static int
6009check_type_union_leafref(struct lys_type *type)
6010{
6011 uint8_t i;
6012
6013 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6014 /* go through unions and look for leafref */
6015 for (i = 0; i < type->info.uni.count; ++i) {
6016 switch (type->info.uni.types[i].base) {
6017 case LY_TYPE_LEAFREF:
6018 return 1;
6019 case LY_TYPE_UNION:
6020 if (check_type_union_leafref(&type->info.uni.types[i])) {
6021 return 1;
6022 }
6023 break;
6024 default:
6025 break;
6026 }
6027 }
6028
6029 return 0;
6030 }
6031
6032 /* just inherit the flag value */
6033 return type->der->has_union_leafref;
6034}
6035
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006036/**
Michal Vaskobb211122015-08-19 14:03:11 +02006037 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006038 *
6039 * @param[in] mod Main module.
6040 * @param[in] item Item to resolve. Type determined by \p type.
6041 * @param[in] type Type of the unresolved item.
6042 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006043 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006044 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006045 *
6046 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6047 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006048static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006049resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vasko769f8032017-01-24 13:11:55 +01006050 struct unres_schema *unres, int final_fail)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006051{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006052 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006053 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006054 unsigned int j;
Radek Krejci80056d52017-01-05 13:13:33 +01006055 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006056 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006057 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006058
Radek Krejcic79c6b12016-07-26 15:11:49 +02006059 struct ly_set *refs, *procs;
6060 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006061 struct lys_ident *ident;
6062 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006063 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006064 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006065 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006066 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006067 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006068 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006069 struct lys_ext_instance *ext, **extlist;
6070 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006071
6072 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006073 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006074 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006075 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006076 ident = item;
6077
Radek Krejci018f1f52016-08-03 16:01:20 +02006078 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006079 break;
6080 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006081 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006082 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006083 stype = item;
6084
Radek Krejci018f1f52016-08-03 16:01:20 +02006085 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006086 break;
6087 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006088 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006089 stype = item;
6090
Michal Vasko1c007172017-03-10 10:20:44 +01006091 rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target);
6092 if (!rc) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02006093 assert(stype->info.lref.target);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006094 /* check if leafref and its target are under a common if-features */
6095 rc = check_leafref_features(stype);
6096 if (rc) {
6097 break;
6098 }
6099
Michal Vaskobb520442017-05-23 10:55:18 +02006100 if (lys_node_module(node)->implemented) {
6101 /* make all the modules on the path implemented */
6102 for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) {
6103 if (!lys_node_module(next)->implemented) {
6104 if (lys_set_implemented(lys_node_module(next))) {
6105 rc = -1;
6106 break;
6107 }
6108 }
6109 }
6110 if (next) {
6111 break;
6112 }
6113
6114 /* store the backlink from leafref target */
6115 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
6116 rc = -1;
6117 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006118 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006119 }
6120
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006121 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006122 case UNRES_TYPE_DER_EXT:
6123 parent_type++;
6124 /* no break */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006125 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006126 parent_type++;
Radek Krejci3a5501d2016-07-18 22:03:34 +02006127 /* no break */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006128 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006129 /* parent */
6130 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006131 stype = item;
6132
Michal Vasko88c29542015-11-27 14:57:53 +01006133 /* HACK type->der is temporarily unparsed type statement */
6134 yin = (struct lyxml_elem *)stype->der;
6135 stype->der = NULL;
6136
Pavol Vicana0e4e672016-02-24 12:20:04 +01006137 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6138 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006139 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006140
6141 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006142 /* may try again later */
6143 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006144 } else {
6145 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02006146 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006147 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006148 }
6149
Michal Vasko88c29542015-11-27 14:57:53 +01006150 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006151 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006152 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006153 /* we need to always be able to free this, it's safe only in this case */
6154 lyxml_free(mod->ctx, yin);
6155 } else {
6156 /* may try again later, put all back how it was */
6157 stype->der = (struct lys_tpdf *)yin;
6158 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006159 }
Radek Krejcic13db382016-08-16 10:52:42 +02006160 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006161 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006162 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006163 LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
6164 }
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02006165
6166 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6167 /* fill typedef union leafref flag */
6168 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6169 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6170 /* copy the type in case it has union leafref flag */
6171 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
6172 LOGERR(LY_EINT, "Failed to duplicate type.");
6173 return -1;
6174 }
6175 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006176 } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +02006177 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6178 * by uses statement until the type is resolved. We do that the same way as uses statements inside
Radek Krejci93def382017-05-24 15:33:48 +02006179 * grouping. The grouping cannot be used unless the unres counter is 0.
6180 * To remember that the grouping already increased the counter, the LY_TYPE_ERR is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006181 * of the type's base member. */
6182 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6183 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006184 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
6185 LOGERR(LY_EINT, "Too many unresolved items (type) inside a grouping.");
6186 return -1;
6187 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006188 stype->base = LY_TYPE_ERR;
Radek Krejcic13db382016-08-16 10:52:42 +02006189 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006190 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006191 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006192 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006193 iff_data = str_snode;
6194 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006195 if (!rc) {
6196 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006197 if (iff_data->infeature) {
6198 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6199 feat = *((struct lys_feature **)item);
6200 if (!feat->depfeatures) {
6201 feat->depfeatures = ly_set_new();
6202 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006203 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006204 }
6205 /* cleanup temporary data */
Radek Krejcicbb473e2016-09-16 14:48:32 +02006206 lydict_remove(mod->ctx, iff_data->fname);
6207 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006208 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006209 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006210 case UNRES_FEATURE:
6211 feat = (struct lys_feature *)item;
6212
6213 if (feat->iffeature_size) {
6214 refs = ly_set_new();
6215 procs = ly_set_new();
6216 ly_set_add(procs, feat, 0);
6217
6218 while (procs->number) {
6219 ref = procs->set.g[procs->number - 1];
6220 ly_set_rm_index(procs, procs->number - 1);
6221
6222 for (i = 0; i < ref->iffeature_size; i++) {
6223 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6224 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006225 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006226 if (ref->iffeature[i].features[j - 1] == feat) {
6227 LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
6228 goto featurecheckdone;
6229 }
6230
6231 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6232 k = refs->number;
6233 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6234 /* not yet seen feature, add it for processing */
6235 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6236 }
6237 }
6238 } else {
6239 /* forward reference */
6240 rc = EXIT_FAILURE;
6241 goto featurecheckdone;
6242 }
6243 }
6244
6245 }
6246 }
6247 rc = EXIT_SUCCESS;
6248
6249featurecheckdone:
6250 ly_set_free(refs);
6251 ly_set_free(procs);
6252 }
6253
6254 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006255 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006256 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006257 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006258 case UNRES_TYPEDEF_DFLT:
6259 parent_type++;
6260 /* no break */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006261 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006262 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006263 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006264 break;
6265 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006266 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006267 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006268 choic = item;
6269
Radek Krejcie00d2312016-08-12 15:27:49 +02006270 if (!choic->dflt) {
6271 choic->dflt = resolve_choice_dflt(choic, expr);
6272 }
Michal Vasko7955b362015-09-04 14:18:15 +02006273 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006274 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006275 } else {
6276 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006277 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006278 break;
6279 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006280 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006281 break;
6282 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006283 unique_info = (struct unres_list_uniq *)item;
6284 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006285 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006286 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006287 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006288 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006289 case UNRES_XPATH:
6290 node = (struct lys_node *)item;
Michal Vasko769f8032017-01-24 13:11:55 +01006291 rc = lys_check_xpath(node, 1, final_fail);
Michal Vasko508a50d2016-09-07 14:50:33 +02006292 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006293 case UNRES_EXT:
6294 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006295 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006296 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006297 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006298 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006299 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006300 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006301 if (eplugin) {
6302 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006303 u = malloc(sizeof *u);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006304 LY_CHECK_ERR_RETURN(!u, LOGMEM, -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006305 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006306 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6307 /* something really bad happend since the extension finalization is not actually
6308 * being resolved while adding into unres, so something more serious with the unres
6309 * list itself must happened */
6310 return -1;
6311 }
Radek Krejci80056d52017-01-05 13:13:33 +01006312 }
6313 }
Radek Krejci79685c92017-02-17 10:49:43 +01006314 }
6315 if (!rc || rc == -1) {
6316 /* cleanup on success or fatal error */
6317 if (ext_data->datatype == LYS_IN_YIN) {
6318 /* YIN */
6319 lyxml_free(mod->ctx, ext_data->data.yin);
6320 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01006321 /* YANG */
6322 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01006323 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006324 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006325 }
6326 break;
Radek Krejci80056d52017-01-05 13:13:33 +01006327 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01006328 u = (uint8_t *)str_snode;
6329 ext = (*(struct lys_ext_instance ***)item)[*u];
6330 free(u);
6331
Radek Krejci80056d52017-01-05 13:13:33 +01006332 eplugin = ext->def->plugin;
6333
6334 /* inherit */
6335 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
6336 root = (struct lys_node *)ext->parent;
6337 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6338 LY_TREE_DFS_BEGIN(root->child, next, node) {
6339 /* first, check if the node already contain instance of the same extension,
6340 * in such a case we won't inherit. In case the node was actually defined as
6341 * augment data, we are supposed to check the same way also the augment node itself */
6342 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
6343 goto inherit_dfs_sibling;
6344 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
6345 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
6346 goto inherit_dfs_sibling;
6347 }
6348
6349 if (eplugin->check_inherit) {
6350 /* we have a callback to check the inheritance, use it */
6351 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
6352 case 0:
6353 /* yes - continue with the inheriting code */
6354 break;
6355 case 1:
6356 /* no - continue with the node's sibling */
6357 goto inherit_dfs_sibling;
6358 case 2:
6359 /* no, but continue with the children, just skip the inheriting code for this node */
6360 goto inherit_dfs_child;
6361 default:
6362 LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
6363 ext->def->module->name, ext->def->name, rc);
6364 }
6365 }
6366
6367 /* inherit the extension */
6368 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006369 LY_CHECK_ERR_RETURN(!extlist, LOGMEM, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006370 extlist[node->ext_size] = malloc(sizeof **extlist);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006371 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM; node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006372 memcpy(extlist[node->ext_size], ext, sizeof *ext);
6373 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
6374
6375 node->ext = extlist;
6376 node->ext_size++;
6377
6378inherit_dfs_child:
6379 /* modification of - select element for the next run - children first */
6380 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
6381 next = NULL;
6382 } else {
6383 next = node->child;
6384 }
6385 if (!next) {
6386inherit_dfs_sibling:
6387 /* no children, try siblings */
6388 next = node->next;
6389 }
6390 while (!next) {
6391 /* go to the parent */
6392 node = lys_parent(node);
6393
6394 /* we are done if we are back in the root (the starter's parent */
6395 if (node == root) {
6396 break;
6397 }
6398
6399 /* parent is already processed, go to its sibling */
6400 next = node->next;
6401 }
6402 }
6403 }
6404 }
6405
6406 /* final check */
6407 if (eplugin->check_result) {
6408 if ((*eplugin->check_result)(ext)) {
Radek Krejci2c121b32017-02-24 10:03:16 +01006409 ly_errno = LY_EEXT;
Radek Krejci80056d52017-01-05 13:13:33 +01006410 return -1;
6411 }
6412 }
6413
6414 rc = 0;
6415 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006416 default:
6417 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006418 break;
6419 }
6420
Radek Krejci54081ce2016-08-12 15:21:47 +02006421 if (has_str && !rc) {
6422 /* the string is no more needed in case of success.
6423 * In case of forward reference, we will try to resolve the string later */
Radek Krejci4f78b532016-02-17 13:43:00 +01006424 lydict_remove(mod->ctx, str_snode);
6425 }
6426
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006427 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006428}
6429
Michal Vaskof02e3742015-08-05 16:27:02 +02006430/* logs directly */
6431static void
Radek Krejci48464ed2016-03-17 15:44:09 +01006432print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006433{
Michal Vaskocb34dc62016-05-20 14:38:37 +02006434 struct lyxml_elem *xml;
6435 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006436 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006437 const char *name = NULL;
6438 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006439
Michal Vaskof02e3742015-08-05 16:27:02 +02006440 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02006441 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006442 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006443 break;
6444 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006445 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006446 break;
6447 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006448 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
6449 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02006450 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006451 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02006452 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02006453 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02006454 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
6455 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01006456 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006457 } else {
6458 LY_TREE_FOR(xml->attr, attr) {
6459 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006460 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006461 break;
6462 }
6463 }
6464 assert(attr);
6465 }
Radek Krejcie534c132016-11-23 13:32:31 +01006466 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006467 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006468 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006469 iff_data = str_node;
6470 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02006471 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006472 case UNRES_FEATURE:
6473 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
6474 ((struct lys_feature *)item)->name);
6475 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006476 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006477 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006478 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006479 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02006480 case UNRES_TYPE_DFLT:
Radek Krejci2e2de832016-10-13 16:12:26 +02006481 if (str_node) {
6482 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
6483 } /* else no default value in the type itself, but we are checking some restrictions against
6484 * possible default value of some base type. The failure is caused by not resolved base type,
6485 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02006486 break;
6487 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006488 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006489 break;
6490 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01006491 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006492 break;
6493 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01006494 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006495 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006496 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006497 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
6498 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01006499 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006500 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01006501 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
6502 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02006503 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006504 case UNRES_EXT:
6505 extinfo = (struct unres_ext *)str_node;
6506 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
6507 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
6508 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006509 default:
6510 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02006511 break;
6512 }
6513}
6514
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006515/**
Michal Vaskobb211122015-08-19 14:03:11 +02006516 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006517 *
6518 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006519 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006520 *
Michal Vasko92b8a382015-08-19 14:03:49 +02006521 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006522 */
Michal Vaskof02e3742015-08-05 16:27:02 +02006523int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006524resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02006525{
Radek Krejci010e54b2016-03-15 09:40:34 +01006526 uint32_t i, resolved = 0, unres_count, res_count;
PavolVicana0fdbf32017-02-15 17:59:02 +01006527 struct lyxml_elem *yin;
6528 struct yang_type *yang;
Michal Vasko74a60c02017-03-08 10:19:48 +01006529 int rc, log_hidden;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006530
6531 assert(unres);
6532
Michal Vaskoe8734262016-09-29 14:12:06 +02006533 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko74a60c02017-03-08 10:19:48 +01006534 if (*ly_vlog_hide_location()) {
6535 log_hidden = 1;
6536 } else {
6537 log_hidden = 0;
6538 ly_vlog_hide(1);
6539 }
Michal Vasko51054ca2015-08-12 12:20:00 +02006540
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006541 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02006542 do {
Michal Vasko88c29542015-11-27 14:57:53 +01006543 unres_count = 0;
6544 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02006545
6546 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006547 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006548 * if-features are resolved here to make sure that we will have all if-features for
6549 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02006550 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006551 continue;
6552 }
Radek Krejci018f1f52016-08-03 16:01:20 +02006553 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejci818b0c52016-11-09 15:10:51 +01006554 * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02006555
Michal Vasko88c29542015-11-27 14:57:53 +01006556 ++unres_count;
Radek Krejcicd9a95a2017-03-25 12:02:35 -05006557 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006558 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006559 unres->type[i] = UNRES_RESOLVED;
6560 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01006561 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02006562 } else if (rc == -1) {
Michal Vasko74a60c02017-03-08 10:19:48 +01006563 if (!log_hidden) {
6564 ly_vlog_hide(0);
6565 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006566 /* print the error */
Radek Krejci791f6c72017-02-22 15:23:39 +01006567 ly_err_repeat();
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006568 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02006569 } else {
6570 /* forward reference, erase ly_errno */
Radek Krejci00a0e712016-10-26 10:24:46 +02006571 ly_err_clean(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02006572 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006573 }
Michal Vasko88c29542015-11-27 14:57:53 +01006574 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02006575
Michal Vasko88c29542015-11-27 14:57:53 +01006576 if (res_count < unres_count) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006577 /* just print the errors */
Michal Vasko74a60c02017-03-08 10:19:48 +01006578 if (!log_hidden) {
6579 ly_vlog_hide(0);
6580 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006581
6582 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006583 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006584 continue;
6585 }
Radek Krejcicd9a95a2017-03-25 12:02:35 -05006586 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1);
Radek Krejci63fc0962017-02-15 13:20:18 +01006587 if (unres->type[i] == UNRES_TYPE_DER_EXT) {
PavolVicana0fdbf32017-02-15 17:59:02 +01006588 yin = (struct lyxml_elem*)((struct lys_type *)unres->item[i])->der;
6589 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6590 yang =(struct yang_type *)yin;
6591 ((struct lys_type *)unres->item[i])->base = yang->base;
6592 if (yang->base == LY_TYPE_UNION) {
6593 yang_free_type_union(mod->ctx, (struct lys_type *)unres->item[i]);
6594 }
6595 lydict_remove(mod->ctx, yang->name);
6596 free(yang);
6597 } else {
6598 lyxml_free(mod->ctx, yin);
6599 }
Radek Krejci63fc0962017-02-15 13:20:18 +01006600 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006601 }
Michal Vasko92b8a382015-08-19 14:03:49 +02006602 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006603 }
6604
Radek Krejci07d0fb92017-01-13 14:11:05 +01006605 /* the rest except finalizing extensions */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006606 for (i = 0; i < unres->count; ++i) {
Radek Krejci80056d52017-01-05 13:13:33 +01006607 if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006608 continue;
6609 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02006610
Radek Krejcicd9a95a2017-03-25 12:02:35 -05006611 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0);
Radek Krejci010e54b2016-03-15 09:40:34 +01006612 if (rc == 0) {
Pavol Vican88e16c92016-09-07 15:41:50 +02006613 if (unres->type[i] == UNRES_LIST_UNIQ) {
6614 /* free the allocated structure */
6615 free(unres->item[i]);
6616 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006617 unres->type[i] = UNRES_RESOLVED;
6618 ++resolved;
6619 } else if (rc == -1) {
Michal Vasko74a60c02017-03-08 10:19:48 +01006620 if (!log_hidden) {
6621 ly_vlog_hide(0);
6622 }
Michal Vasko22af5ca2016-05-20 11:44:02 +02006623 /* print the error */
Radek Krejci791f6c72017-02-22 15:23:39 +01006624 ly_err_repeat();
Michal Vasko22af5ca2016-05-20 11:44:02 +02006625 return -1;
Radek Krejci791f6c72017-02-22 15:23:39 +01006626 } else {
6627 /* forward reference, erase ly_errno */
6628 ly_err_clean(1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006629 }
6630 }
6631
Michal Vasko74a60c02017-03-08 10:19:48 +01006632 if (!log_hidden) {
6633 ly_vlog_hide(0);
6634 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006635
Radek Krejci80056d52017-01-05 13:13:33 +01006636 /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
6637 for (i = 0; i < unres->count; ++i) {
6638 if (unres->type[i] != UNRES_EXT_FINALIZE) {
6639 continue;
6640 }
6641
Radek Krejcicd9a95a2017-03-25 12:02:35 -05006642 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 0);
Radek Krejci791f6c72017-02-22 15:23:39 +01006643 unres->type[i] = UNRES_RESOLVED;
Radek Krejci80056d52017-01-05 13:13:33 +01006644 if (rc == 0) {
Radek Krejci80056d52017-01-05 13:13:33 +01006645 ++resolved;
6646 }
Radek Krejci791f6c72017-02-22 15:23:39 +01006647 /* else error - it was already printed, but resolved was not increased,
6648 so this unres item will not be resolved again in the following code,
6649 but it will cause returning -1 at the end, this way we are able to
6650 print all the issues with unres */
Radek Krejci80056d52017-01-05 13:13:33 +01006651 }
6652
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006653 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01006654 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
6655 * all the validation errors
6656 */
6657 for (i = 0; i < unres->count; ++i) {
6658 if (unres->type[i] == UNRES_RESOLVED) {
6659 continue;
6660 }
Radek Krejcicd9a95a2017-03-25 12:02:35 -05006661 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres, 1);
Radek Krejcib3142312016-11-09 11:04:12 +01006662 if (unres->type[i] == UNRES_XPATH) {
Michal Vasko769f8032017-01-24 13:11:55 +01006663 /* XPath referencing an unknown node is actually supposed to be just a warning */
Radek Krejcib3142312016-11-09 11:04:12 +01006664 unres->type[i] = UNRES_RESOLVED;
6665 resolved++;
Radek Krejcib3142312016-11-09 11:04:12 +01006666 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006667 }
Radek Krejcib3142312016-11-09 11:04:12 +01006668 if (resolved < unres->count) {
6669 return -1;
6670 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006671 }
6672
Michal Vaskoe8734262016-09-29 14:12:06 +02006673 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01006674 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006675 return EXIT_SUCCESS;
6676}
6677
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006678/**
Michal Vaskobb211122015-08-19 14:03:11 +02006679 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006680 *
6681 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006682 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006683 * @param[in] item Item to resolve. Type determined by \p type.
6684 * @param[in] type Type of the unresolved item.
6685 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006686 *
Michal Vasko3767fb22016-07-21 12:10:57 +02006687 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006688 */
6689int
Radek Krejci48464ed2016-03-17 15:44:09 +01006690unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
6691 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006692{
Radek Krejci54081ce2016-08-12 15:21:47 +02006693 int rc;
6694 const char *dictstr;
6695
6696 dictstr = lydict_insert(mod->ctx, str, 0);
6697 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
6698
Radek Krejcid9c0ce22017-01-20 15:20:16 +01006699 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02006700 lydict_remove(mod->ctx, dictstr);
6701 }
6702 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006703}
6704
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006705/**
Michal Vaskobb211122015-08-19 14:03:11 +02006706 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006707 *
6708 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006709 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006710 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01006711 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006712 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006713 *
Radek Krejcid9c0ce22017-01-20 15:20:16 +01006714 * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error, -2 if the unres item
6715 * is already in the unres list.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006716 */
6717int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006718unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01006719 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006720{
Michal Vaskoef486d72016-09-27 12:10:44 +02006721 int rc, log_hidden;
Michal Vasko88c29542015-11-27 14:57:53 +01006722 struct lyxml_elem *yin;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006723
Michal Vasko9bf425b2015-10-22 11:42:03 +02006724 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
6725 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006726
Michal Vasko9e862e82017-03-08 10:20:49 +01006727#ifndef NDEBUG
Radek Krejcidf056df2017-03-09 13:24:45 +01006728 uint32_t u;
6729
Radek Krejci850a5de2016-11-08 14:06:40 +01006730 /* check for duplicities in unres */
6731 for (u = 0; u < unres->count; u++) {
6732 if (unres->type[u] == type && unres->item[u] == item &&
6733 unres->str_snode[u] == snode && unres->module[u] == mod) {
Michal Vasko9e862e82017-03-08 10:20:49 +01006734 /* duplication, should not happen */
6735 assert(0);
Radek Krejci850a5de2016-11-08 14:06:40 +01006736 }
6737 }
Michal Vasko9e862e82017-03-08 10:20:49 +01006738#endif
Radek Krejci850a5de2016-11-08 14:06:40 +01006739
Radek Krejcic293bac2017-02-27 11:25:28 +01006740 if (type == UNRES_EXT_FINALIZE) {
Radek Krejci80056d52017-01-05 13:13:33 +01006741 /* extension finalization is not even tried when adding the item into the inres list */
Radek Krejcic293bac2017-02-27 11:25:28 +01006742 rc = EXIT_FAILURE;
6743 } else {
Radek Krejci80056d52017-01-05 13:13:33 +01006744 if (*ly_vlog_hide_location()) {
6745 log_hidden = 1;
6746 } else {
6747 log_hidden = 0;
6748 ly_vlog_hide(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01006749 }
Radek Krejcicbba57c2017-01-24 13:43:20 +01006750 rc = resolve_unres_schema_item(mod, item, type, snode, unres, 0);
Radek Krejci80056d52017-01-05 13:13:33 +01006751 if (!log_hidden) {
6752 ly_vlog_hide(0);
6753 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006754
Radek Krejci80056d52017-01-05 13:13:33 +01006755 if (rc != EXIT_FAILURE) {
Michal Vaskobb520442017-05-23 10:55:18 +02006756 if (rc == -1) {
Radek Krejci80056d52017-01-05 13:13:33 +01006757 ly_err_repeat();
6758 }
6759 if (type == UNRES_LIST_UNIQ) {
6760 /* free the allocated structure */
6761 free(item);
6762 } else if (rc == -1 && type == UNRES_IFFEAT) {
6763 /* free the allocated resources */
6764 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02006765 }
Radek Krejci80056d52017-01-05 13:13:33 +01006766 return rc;
6767 } else {
6768 /* erase info about validation errors */
6769 ly_err_clean(1);
6770 }
Michal Vaskof02e3742015-08-05 16:27:02 +02006771
Radek Krejci80056d52017-01-05 13:13:33 +01006772 print_unres_schema_item_fail(item, type, snode);
6773
6774 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
6775 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
6776 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
6777 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
6778 lyxml_unlink_elem(mod->ctx, yin, 1);
6779 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
6780 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01006781 }
Michal Vasko88c29542015-11-27 14:57:53 +01006782 }
6783
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006784 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01006785 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006786 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM, -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006787 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01006788 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006789 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006790 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01006791 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006792 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM, -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006793 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01006794 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006795 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM, -1);
Radek Krejcic071c542016-01-27 14:57:51 +01006796 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006797
Michal Vasko3767fb22016-07-21 12:10:57 +02006798 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006799}
6800
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006801/**
Michal Vaskobb211122015-08-19 14:03:11 +02006802 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006803 *
6804 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006805 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006806 * @param[in] item Old item to be resolved.
6807 * @param[in] type Type of the old unresolved item.
6808 * @param[in] new_item New item to use in the duplicate.
6809 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02006810 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006811 */
Michal Vaskodad19402015-08-06 09:51:53 +02006812int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006813unres_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 +02006814{
6815 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006816 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006817 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006818
Michal Vaskocf024702015-10-08 15:01:42 +02006819 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006820
Radek Krejcid09d1a52016-08-11 14:05:45 +02006821 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
6822 if (type == UNRES_LIST_UNIQ) {
6823 aux_uniq.list = item;
6824 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
6825 item = &aux_uniq;
6826 }
Michal Vasko878e38d2016-09-05 12:17:53 +02006827 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006828
6829 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02006830 if (type == UNRES_LIST_UNIQ) {
6831 free(new_item);
6832 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02006833 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006834 }
6835
Radek Krejcic79c6b12016-07-26 15:11:49 +02006836 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02006837 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006838 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006839 LOGINT;
6840 return -1;
6841 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006842 } else if (type == UNRES_IFFEAT) {
6843 /* duplicate unres_iffeature_data */
6844 iff_data = malloc(sizeof *iff_data);
Radek Krejcia8d111f2017-05-31 13:57:37 +02006845 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM, -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006846 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
6847 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
6848 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
6849 LOGINT;
6850 return -1;
6851 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006852 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01006853 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006854 LOGINT;
6855 return -1;
6856 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006857 }
Michal Vaskodad19402015-08-06 09:51:53 +02006858
6859 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006860}
6861
Michal Vaskof02e3742015-08-05 16:27:02 +02006862/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006863int
Michal Vasko878e38d2016-09-05 12:17:53 +02006864unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006865{
Michal Vasko878e38d2016-09-05 12:17:53 +02006866 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006867 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006868
Radek Krejciddddd0d2017-01-20 15:20:46 +01006869 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02006870 i = start_on_backwards;
6871 } else {
6872 i = unres->count - 1;
6873 }
6874 for (; i > -1; i--) {
6875 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02006876 continue;
6877 }
6878 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02006879 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02006880 break;
6881 }
6882 } else {
6883 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
6884 aux_uniq2 = (struct unres_list_uniq *)item;
6885 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02006886 break;
6887 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006888 }
6889 }
6890
Michal Vasko878e38d2016-09-05 12:17:53 +02006891 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006892}
Michal Vasko8bcdf292015-08-19 14:04:43 +02006893
Michal Vaskoede9c472016-06-07 09:38:15 +02006894static void
6895unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
6896{
6897 struct lyxml_elem *yin;
6898 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006899 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02006900
6901 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02006902 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02006903 case UNRES_TYPE_DER:
6904 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
6905 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6906 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01006907 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02006908 lydict_remove(ctx, yang->name);
6909 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01006910 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
6911 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
6912 }
Michal Vaskoede9c472016-06-07 09:38:15 +02006913 } else {
6914 lyxml_free(ctx, yin);
6915 }
6916 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02006917 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006918 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
6919 lydict_remove(ctx, iff_data->fname);
6920 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02006921 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02006922 case UNRES_IDENT:
6923 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02006924 case UNRES_CHOICE_DFLT:
6925 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02006926 lydict_remove(ctx, (const char *)unres->str_snode[i]);
6927 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006928 case UNRES_LIST_UNIQ:
6929 free(unres->item[i]);
6930 break;
PavolVicanc1807262017-01-31 18:00:27 +01006931 case UNRES_EXT:
6932 free(unres->str_snode[i]);
6933 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02006934 default:
6935 break;
6936 }
6937 unres->type[i] = UNRES_RESOLVED;
6938}
6939
Michal Vasko88c29542015-11-27 14:57:53 +01006940void
Michal Vasko44ab1462017-05-18 13:18:36 +02006941unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01006942{
6943 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01006944 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01006945
Radek Krejcic071c542016-01-27 14:57:51 +01006946 if (!unres || !(*unres)) {
6947 return;
Michal Vasko88c29542015-11-27 14:57:53 +01006948 }
6949
Michal Vasko44ab1462017-05-18 13:18:36 +02006950 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01006951
6952 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02006953 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01006954 if ((*unres)->type[i] != UNRES_RESOLVED) {
6955 unresolved++;
6956 }
6957 continue;
6958 }
Michal Vaskoede9c472016-06-07 09:38:15 +02006959
6960 /* free heap memory for the specific item */
6961 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01006962 }
6963
Michal Vaskoede9c472016-06-07 09:38:15 +02006964 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02006965 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01006966 free((*unres)->item);
6967 free((*unres)->type);
6968 free((*unres)->str_snode);
6969 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01006970 free((*unres));
6971 (*unres) = NULL;
6972 }
Michal Vasko88c29542015-11-27 14:57:53 +01006973}
6974
Michal Vasko3cfa3182017-01-17 10:00:58 +01006975static int
6976check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
6977{
6978 struct ly_set *set;
Michal Vasko3c777092017-01-17 14:10:09 +01006979 struct lys_node *op_node, *first_node;
Michal Vasko3cfa3182017-01-17 10:00:58 +01006980 char *buf;
Radek Krejci81c38b82017-06-02 15:04:16 +02006981 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01006982
6983 for (op_node = lys_parent(sleaf);
6984 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
6985 op_node = lys_parent(op_node));
6986
6987 if (op_node && lys_parent(op_node)) {
6988 /* nested operation - any absolute path is external */
6989 return 1;
6990 }
6991
6992 /* get the first node from the instid */
6993 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
6994 if (!buf) {
6995 LOGMEM;
6996 return -1;
6997 }
6998
6999 /* there is a predicate, remove it */
7000 if (buf[strlen(buf) - 1] == ']') {
7001 assert(strchr(buf, '['));
7002 *strchr(buf, '[') = '\0';
7003 }
7004
7005 /* find the first schema node */
Michal Vasko9ff79aa2017-07-03 14:10:32 +02007006 set = lys_find_xpath(sleaf, buf, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007007 if (!set || !set->number) {
7008 free(buf);
Michal Vasko29fd9742017-01-23 09:55:44 +01007009 ly_set_free(set);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007010 return 1;
7011 }
7012 free(buf);
7013
Michal Vasko3c777092017-01-17 14:10:09 +01007014 first_node = set->set.s[0];
Michal Vasko3c777092017-01-17 14:10:09 +01007015
Michal Vasko3cfa3182017-01-17 10:00:58 +01007016 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7017
7018 if (op_node) {
7019 /* it is an operation, so we're good if it points somewhere inside it */
Michal Vasko3c777092017-01-17 14:10:09 +01007020 if (op_node == first_node) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007021 assert(set->number == 1);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007022 } else {
Radek Krejci81c38b82017-06-02 15:04:16 +02007023 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007024 }
7025 }
7026
Radek Krejci81c38b82017-06-02 15:04:16 +02007027 /* cleanup */
7028 ly_set_free(set);
7029
Michal Vasko3cfa3182017-01-17 10:00:58 +01007030 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7031 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007032 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007033}
7034
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007035/**
7036 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7037 *
7038 * @param[in] data Data node where the path is used
7039 * @param[in] path Instance-identifier node value.
7040 * @param[in,out] ret Resolved instance or NULL.
7041 *
7042 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7043 */
7044static int
7045resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7046{
7047 int i = 0, j;
7048 const struct lys_module *mod;
7049 struct ly_ctx *ctx = data->schema->module->ctx;
7050 const char *model, *name;
7051 char *str;
7052 int mod_len, name_len, has_predicate;
7053 struct unres_data node_match;
7054
7055 memset(&node_match, 0, sizeof node_match);
7056 *ret = NULL;
7057
7058 /* we need root to resolve absolute path */
7059 for (; data->parent; data = data->parent);
7060 /* we're still parsing it and the pointer is not correct yet */
7061 if (data->prev) {
7062 for (; data->prev->next; data = data->prev);
7063 }
7064
7065 /* search for the instance node */
7066 while (path[i]) {
7067 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7068 if (j <= 0) {
7069 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
7070 goto error;
7071 }
7072 i += j;
7073
7074 str = strndup(model, mod_len);
7075 if (!str) {
7076 LOGMEM;
7077 goto error;
7078 }
7079 mod = ly_ctx_get_module(ctx, str, NULL);
Michal Vaskof53187d2017-01-13 13:23:14 +01007080 if (ctx->data_clb) {
7081 if (!mod) {
7082 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7083 } else if (!mod->implemented) {
7084 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7085 }
7086 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007087 free(str);
7088
Michal Vaskof53187d2017-01-13 13:23:14 +01007089 if (!mod || !mod->implemented || mod->disabled) {
7090 break;
7091 }
7092
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007093 if (resolve_data(mod, name, name_len, data, &node_match)) {
7094 /* no instance exists */
7095 break;
7096 }
7097
7098 if (has_predicate) {
7099 /* we have predicate, so the current results must be list or leaf-list */
Michal Vasko1c007172017-03-10 10:20:44 +01007100 j = resolve_instid_predicate(&path[i], &node_match);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007101 if (j < 1) {
7102 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
7103 goto error;
7104 }
7105 i += j;
7106
7107 if (!node_match.count) {
7108 /* no instance exists */
7109 break;
7110 }
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007111 } else if (node_match.count) {
7112 /* check that we are not addressing lists */
7113 for (j = 0; (unsigned)j < node_match.count; ++j) {
7114 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7115 unres_data_del(&node_match, j--);
7116 }
7117 }
7118 if (!node_match.count) {
7119 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list keys.");
7120 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007121 }
7122 }
7123
7124 if (!node_match.count) {
7125 /* no instance exists */
7126 if (req_inst > -1) {
7127 LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path);
7128 return EXIT_FAILURE;
7129 }
7130 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7131 return EXIT_SUCCESS;
7132 } else if (node_match.count > 1) {
7133 /* instance identifier must resolve to a single node */
7134 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
7135 goto error;
7136 } else {
7137 /* we have required result, remember it and cleanup */
7138 *ret = node_match.node[0];
7139 free(node_match.node);
7140 return EXIT_SUCCESS;
7141 }
7142
7143error:
7144 /* cleanup */
7145 free(node_match.node);
7146 return -1;
7147}
7148
7149static int
7150resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007151{
Michal Vasko86ae1f22017-07-10 11:50:33 +02007152 struct ly_set *set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007153 uint32_t i;
7154
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007155 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007156
Michal Vasko86ae1f22017-07-10 11:50:33 +02007157 /* syntax was already checked, so just evaluate the path using standard XPath */
7158 set = lyd_find_xpath((struct lyd_node *)leaf, path);
7159 if (!set) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007160 return -1;
7161 }
7162
Michal Vasko86ae1f22017-07-10 11:50:33 +02007163 for (i = 0; i < set->number; ++i) {
7164 if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7165 continue;
7166 }
7167
Radek Krejci1899d6a2016-11-03 13:48:07 +01007168 /* not that the value is already in canonical form since the parsers does the conversion,
7169 * so we can simply compare just the values */
Michal Vasko86ae1f22017-07-10 11:50:33 +02007170 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) {
Radek Krejci1899d6a2016-11-03 13:48:07 +01007171 /* we have the match */
Michal Vasko86ae1f22017-07-10 11:50:33 +02007172 *ret = set->set.d[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007173 break;
7174 }
7175 }
7176
Michal Vasko86ae1f22017-07-10 11:50:33 +02007177 ly_set_free(set);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007178
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007179 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007180 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007181 if (req_inst > -1) {
7182 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007183 return EXIT_FAILURE;
7184 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007185 LOGVRB("There is no leafref \"%s\" with the value \"%s\", but it is not required.", path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007186 }
7187 }
7188
7189 return EXIT_SUCCESS;
7190}
7191
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007192/* ignore fail because we are parsing edit-config, get, or get-config - but only if the union includes leafref or instid */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007193int
7194resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7195 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007196{
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007197 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007198 struct lyd_node *ret;
7199 int found, hidden, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007200 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007201
7202 assert(type->base == LY_TYPE_UNION);
7203
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007204 if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) {
7205 /* either NULL or instid previously converted to JSON */
7206 json_val = leaf->value.string;
7207 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007208
Michal Vaskofd6c6502017-01-06 12:15:41 +01007209 if (store) {
7210 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7211 free(leaf->value.bit);
7212 }
7213 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007214 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007215
7216 /* turn logging off, we are going to try to validate the value with all the types in order */
7217 hidden = *ly_vlog_hide_location();
7218 ly_vlog_hide(1);
7219
7220 t = NULL;
7221 found = 0;
7222 while ((t = lyp_get_next_union_type(type, t, &found))) {
7223 found = 0;
7224
7225 switch (t->base) {
7226 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007227 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7228 req_inst = -1;
7229 } else {
7230 req_inst = t->info.lref.req;
7231 }
7232
7233 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007234 if (store) {
7235 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7236 /* valid resolved */
7237 leaf->value.leafref = ret;
7238 leaf->value_type = LY_TYPE_LEAFREF;
7239 } else {
7240 /* valid unresolved */
Radek Krejcia571d942017-02-24 09:26:49 +01007241 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, 1, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007242 return -1;
7243 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007244 }
7245 }
7246
7247 success = 1;
7248 }
7249 break;
7250 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007251 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7252 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7253 req_inst = -1;
7254 } else {
7255 req_inst = t->info.inst.req;
7256 }
7257
Michal Vaskod3a03112017-01-23 09:56:02 +01007258 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007259 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007260 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007261 /* valid resolved */
7262 leaf->value.instance = ret;
7263 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007264
Michal Vaskofd6c6502017-01-06 12:15:41 +01007265 if (json_val) {
7266 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7267 leaf->value_str = json_val;
7268 json_val = NULL;
7269 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007270 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007271 /* valid unresolved */
7272 if (json_val) {
7273 /* put the JSON val back */
7274 leaf->value.string = json_val;
7275 json_val = NULL;
7276 } else {
7277 leaf->value.instance = NULL;
7278 }
7279 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007280 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007281 }
7282
7283 success = 1;
7284 }
7285 break;
7286 default:
Radek Krejcia571d942017-02-24 09:26:49 +01007287 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, store, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007288 success = 1;
7289 }
7290 break;
7291 }
7292
7293 if (success) {
7294 break;
7295 }
7296
7297 /* erase information about errors - they are false or irrelevant
7298 * and will be replaced by a single error messages */
7299 ly_err_clean(1);
7300
7301 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007302 if (store) {
7303 if (t->base == LY_TYPE_BITS) {
7304 free(leaf->value.bit);
7305 }
7306 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007307 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007308 }
7309
7310 /* turn logging back on */
7311 if (!hidden) {
7312 ly_vlog_hide(0);
7313 }
7314
7315 if (json_val) {
7316 if (!success) {
7317 /* put the value back for now */
7318 assert(leaf->value_type == LY_TYPE_UNION);
7319 leaf->value.string = json_val;
7320 } else {
7321 /* value was ultimately useless, but we could not have known */
7322 lydict_remove(leaf->schema->module->ctx, json_val);
7323 }
7324 }
7325
Michal Vaskofd6c6502017-01-06 12:15:41 +01007326 if (success) {
7327 if (resolved_type) {
7328 *resolved_type = t;
7329 }
7330 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007331 /* not found and it is required */
7332 LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007333 return EXIT_FAILURE;
7334 }
7335
7336 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007337
Radek Krejci9b6aad22016-09-20 15:55:51 +02007338}
7339
Michal Vasko8bcdf292015-08-19 14:04:43 +02007340/**
7341 * @brief Resolve a single unres data item. Logs directly.
7342 *
Michal Vaskocf024702015-10-08 15:01:42 +02007343 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007344 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007345 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007346 *
7347 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7348 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007349int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007350resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007351{
Michal Vasko3cfa3182017-01-17 10:00:58 +01007352 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02007353 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007354 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007355 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007356
Michal Vasko83a6c462015-10-08 16:43:53 +02007357 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02007358 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007359
Michal Vaskocf024702015-10-08 15:01:42 +02007360 switch (type) {
7361 case UNRES_LEAFREF:
7362 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007363 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007364 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7365 req_inst = -1;
7366 } else {
7367 req_inst = sleaf->type.info.lref.req;
7368 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007369 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
7370 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01007371 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007372 /* valid resolved */
Michal Vasko1c8567a2017-01-05 13:42:27 +01007373 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7374 free(leaf->value.bit);
7375 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007376 leaf->value.leafref = ret;
7377 leaf->value_type = LY_TYPE_LEAFREF;
7378 } else {
7379 /* valid unresolved */
7380 if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) {
Radek Krejcia571d942017-02-24 09:26:49 +01007381 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007382 return -1;
7383 }
7384 }
7385 }
7386 leaf->validity &= ~LYD_VAL_LEAFREF;
7387 } else {
7388 return rc;
7389 }
7390 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007391
Michal Vaskocf024702015-10-08 15:01:42 +02007392 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02007393 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007394 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
7395 if (ext_dep == -1) {
7396 return -1;
7397 }
7398
7399 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7400 req_inst = -1;
7401 } else {
7402 req_inst = sleaf->type.info.inst.req;
7403 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007404 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
7405 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007406 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007407 /* valid resolved */
7408 leaf->value.instance = ret;
7409 leaf->value_type = LY_TYPE_INST;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007410 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007411 /* valid unresolved */
7412 leaf->value.instance = NULL;
7413 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007414 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007415 } else {
7416 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007417 }
Michal Vaskocf024702015-10-08 15:01:42 +02007418 break;
7419
Radek Krejci7de36cf2016-09-12 16:18:50 +02007420 case UNRES_UNION:
7421 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007422 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007423
Michal Vaskocf024702015-10-08 15:01:42 +02007424 case UNRES_WHEN:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007425 if ((rc = resolve_when(node, NULL, ignore_fail))) {
Michal Vaskocf024702015-10-08 15:01:42 +02007426 return rc;
7427 }
7428 break;
7429
Michal Vaskobf19d252015-10-08 15:39:17 +02007430 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007431 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02007432 return rc;
7433 }
7434 break;
7435
7436 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007437 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02007438 return rc;
7439 }
7440 break;
7441
Michal Vaskocf024702015-10-08 15:01:42 +02007442 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02007443 LOGINT;
7444 return -1;
7445 }
7446
7447 return EXIT_SUCCESS;
7448}
7449
7450/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01007451 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02007452 *
7453 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02007454 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007455 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01007456 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007457 */
7458int
Radek Krejci0b7704f2016-03-18 12:16:14 +01007459unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007460{
Radek Krejci03b71f72016-03-16 11:10:09 +01007461 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02007462 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02007463 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02007464
Radek Krejci03b71f72016-03-16 11:10:09 +01007465 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007466 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Radek Krejcia8d111f2017-05-31 13:57:37 +02007467 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007468 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01007469 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Radek Krejcia8d111f2017-05-31 13:57:37 +02007470 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM, -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007471 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007472
Radek Krejci0b7704f2016-03-18 12:16:14 +01007473 if (type == UNRES_WHEN) {
7474 /* remove previous result */
7475 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007476 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007477
7478 return EXIT_SUCCESS;
7479}
7480
7481/**
7482 * @brief Resolve every unres data item in the structure. Logs directly.
7483 *
Radek Krejci082c84f2016-10-17 16:33:06 +02007484 * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected,
7485 * unresolved leafrefs/instids are accepted).
7486 *
7487 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
7488 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007489 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02007490 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
7491 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007492 *
7493 * @return EXIT_SUCCESS on success, -1 on error.
7494 */
7495int
Radek Krejci082c84f2016-10-17 16:33:06 +02007496resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007497{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007498 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007499 int rc, progress, ignore_fail;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007500 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007501
Radek Krejci082c84f2016-10-17 16:33:06 +02007502 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01007503 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01007504
7505 if (!unres->count) {
7506 return EXIT_SUCCESS;
7507 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007508
Michal Vaskoad2e44a2017-01-03 10:31:35 +01007509 if (options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007510 ignore_fail = 1;
7511 } else if (options & LYD_OPT_NOEXTDEPS) {
7512 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007513 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007514 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007515 }
7516
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007517 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01007518 ly_vlog_hide(1);
7519
Radek Krejci0b7704f2016-03-18 12:16:14 +01007520 /* when-stmt first */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007521 first = 1;
7522 stmt_count = 0;
7523 resolved = 0;
7524 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01007525 do {
Radek Krejci00a0e712016-10-26 10:24:46 +02007526 ly_err_clean(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007527 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02007528 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007529 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007530 continue;
7531 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007532 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007533 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007534 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007535 }
7536
7537 /* resolve when condition only when all parent when conditions are already resolved */
7538 for (parent = unres->node[i]->parent;
7539 parent && LYD_WHEN_DONE(parent->when_status);
7540 parent = parent->parent) {
7541 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
7542 /* the parent node was already unlinked, do not resolve this node,
7543 * it will be removed anyway, so just mark it as resolved
7544 */
7545 unres->node[i]->when_status |= LYD_WHEN_FALSE;
7546 unres->type[i] = UNRES_RESOLVED;
7547 resolved++;
7548 break;
7549 }
7550 }
7551 if (parent) {
7552 continue;
7553 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007554
Michal Vasko3cfa3182017-01-17 10:00:58 +01007555 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Radek Krejci010e54b2016-03-15 09:40:34 +01007556 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007557 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
Radek Krejci082c84f2016-10-17 16:33:06 +02007558 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01007559 /* false when condition */
7560 ly_vlog_hide(0);
Radek Krejci2467a492016-10-24 15:16:59 +02007561 ly_err_repeat();
Radek Krejci03b71f72016-03-16 11:10:09 +01007562 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007563 } /* follows else */
7564
Michal Vaskoe31d34a2017-03-28 14:50:38 +02007565 /* auto-delete */
7566 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
7567 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
7568
Radek Krejci0c0086a2016-03-24 15:20:28 +01007569 /* only unlink now, the subtree can contain another nodes stored in the unres list */
7570 /* if it has parent non-presence containers that would be empty, we should actually
7571 * remove the container
7572 */
Radek Krejci2537fd32016-09-07 16:22:41 +02007573 for (parent = unres->node[i];
7574 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
7575 parent = parent->parent) {
7576 if (((struct lys_node_container *)parent->parent->schema)->presence) {
7577 /* presence container */
7578 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007579 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007580 if (parent->next || parent->prev != parent) {
7581 /* non empty (the child we are in and we are going to remove is not the only child) */
7582 break;
7583 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007584 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007585 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007586
Radek Krejci0c0086a2016-03-24 15:20:28 +01007587 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007588 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01007589 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007590
Radek Krejci0b7704f2016-03-18 12:16:14 +01007591 lyd_unlink(unres->node[i]);
7592 unres->type[i] = UNRES_DELETE;
7593 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01007594
7595 /* update the rest of unres items */
7596 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01007597 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01007598 continue;
7599 }
7600
7601 /* test if the node is in subtree to be deleted */
7602 for (parent = unres->node[j]; parent; parent = parent->parent) {
7603 if (parent == unres->node[i]) {
7604 /* yes, it is */
7605 unres->type[j] = UNRES_RESOLVED;
7606 resolved++;
7607 break;
7608 }
7609 }
7610 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007611 } else {
7612 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01007613 }
Radek Krejci00a0e712016-10-26 10:24:46 +02007614 ly_err_clean(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007615 resolved++;
7616 progress = 1;
7617 } else if (rc == -1) {
7618 ly_vlog_hide(0);
Michal Vasko76e73402016-08-24 16:00:13 +02007619 /* print only this last error */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007620 resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Radek Krejci010e54b2016-03-15 09:40:34 +01007621 return -1;
Radek Krejci2467a492016-10-24 15:16:59 +02007622 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01007623 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007624 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007625 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01007626
Radek Krejci0b7704f2016-03-18 12:16:14 +01007627 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007628 if (stmt_count > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007629 ly_vlog_hide(0);
Radek Krejci2467a492016-10-24 15:16:59 +02007630 ly_err_repeat();
Radek Krejci0b7704f2016-03-18 12:16:14 +01007631 return -1;
7632 }
7633
7634 for (i = 0; del_items && i < unres->count; i++) {
7635 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
7636 if (unres->type[i] != UNRES_DELETE) {
7637 continue;
7638 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007639 if (!unres->node[i]) {
7640 unres->type[i] = UNRES_RESOLVED;
7641 del_items--;
7642 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007643 }
7644
7645 /* really remove the complete subtree */
7646 lyd_free(unres->node[i]);
7647 unres->type[i] = UNRES_RESOLVED;
7648 del_items--;
7649 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007650
7651 /* now leafrefs */
7652 first = 1;
7653 stmt_count = 0;
7654 resolved = 0;
7655 do {
7656 progress = 0;
7657 for (i = 0; i < unres->count; i++) {
7658 if (unres->type[i] != UNRES_LEAFREF) {
7659 continue;
7660 }
7661 if (first) {
7662 /* count leafref nodes in unres list */
7663 stmt_count++;
7664 }
7665
7666 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
7667 if (!rc) {
7668 unres->type[i] = UNRES_RESOLVED;
7669 ly_err_clean(1);
7670 resolved++;
7671 progress = 1;
7672 } else if (rc == -1) {
7673 ly_vlog_hide(0);
7674 /* print only this last error */
7675 resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
7676 return -1;
7677 } /* else forward reference */
7678 }
7679 first = 0;
7680 } while (progress && resolved < stmt_count);
7681
7682 /* do we have some unresolved leafrefs? */
7683 if (stmt_count > resolved) {
7684 ly_vlog_hide(0);
7685 ly_err_repeat();
7686 return -1;
7687 }
7688
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007689 ly_vlog_hide(0);
Radek Krejci010e54b2016-03-15 09:40:34 +01007690
7691 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007692 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007693 if (unres->type[i] == UNRES_RESOLVED) {
7694 continue;
7695 }
Radek Krejci082c84f2016-10-17 16:33:06 +02007696 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01007697
Michal Vasko3cfa3182017-01-17 10:00:58 +01007698 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007699 if (rc) {
7700 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007701 return -1;
7702 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007703
7704 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007705 }
7706
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007707 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01007708 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007709 return EXIT_SUCCESS;
7710}