blob: 89855f938613c4ac06e0379baaab6e0c09325e90 [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
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200513parse_path_arg(struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
514 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 }
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200570 if (!(*prefix)) {
571 /* 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 Vasko58c2aab2017-01-05 10:02:05 +0100914 * predicate-expr = "." / 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.
920 * @param[out] name Points to the list key name.
921 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100922 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100923 * @param[out] val_len Length of \p value.
924 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
925 */
Michal Vasko22448d32016-03-16 13:17:29 +0100926int
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200927parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len,
Michal Vasko3547c532016-03-14 09:40:50 +0100928 int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100929{
930 const char *ptr;
931 int parsed = 0, ret;
932 char quote;
933
934 assert(id);
935 if (name) {
936 *name = NULL;
937 }
938 if (nam_len) {
939 *nam_len = 0;
940 }
941 if (value) {
942 *value = NULL;
943 }
944 if (val_len) {
945 *val_len = 0;
946 }
947 if (has_predicate) {
948 *has_predicate = 0;
949 }
950
951 if (id[0] != '[') {
952 return -parsed;
953 }
954
955 ++parsed;
956 ++id;
957
958 while (isspace(id[0])) {
959 ++parsed;
960 ++id;
961 }
962
Michal Vasko22448d32016-03-16 13:17:29 +0100963 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200964 if (id[0] == '.') {
965 ret = 1;
Michal Vasko58c2aab2017-01-05 10:02:05 +0100966 } else if (isdigit(id[0])) {
967 if (id[0] == '0') {
968 return -parsed;
969 }
970 ret = 1;
971 while (isdigit(id[ret])) {
972 ++ret;
973 }
Michal Vasko7b54f7e2016-05-03 15:07:31 +0200974 } else if ((ret = parse_identifier(id)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100975 return -parsed + ret;
976 }
977 if (name) {
978 *name = id;
979 }
980 if (nam_len) {
981 *nam_len = ret;
982 }
983
984 parsed += ret;
985 id += ret;
986
987 while (isspace(id[0])) {
988 ++parsed;
989 ++id;
990 }
991
992 /* there is value as well */
993 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +0100994 if (name && isdigit(**name)) {
995 return -parsed;
996 }
997
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100998 ++parsed;
999 ++id;
1000
1001 while (isspace(id[0])) {
1002 ++parsed;
1003 ++id;
1004 }
1005
1006 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1007 if ((id[0] == '\"') || (id[0] == '\'')) {
1008 quote = id[0];
1009
1010 ++parsed;
1011 ++id;
1012
1013 if ((ptr = strchr(id, quote)) == NULL) {
1014 return -parsed;
1015 }
1016 ret = ptr - id;
1017
1018 if (value) {
1019 *value = id;
1020 }
1021 if (val_len) {
1022 *val_len = ret;
1023 }
1024
1025 parsed += ret + 1;
1026 id += ret + 1;
1027 } else {
1028 return -parsed;
1029 }
1030
1031 while (isspace(id[0])) {
1032 ++parsed;
1033 ++id;
1034 }
1035 }
1036
1037 if (id[0] != ']') {
1038 return -parsed;
1039 }
1040
1041 ++parsed;
1042 ++id;
1043
1044 if ((id[0] == '[') && has_predicate) {
1045 *has_predicate = 1;
1046 }
1047
1048 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001049}
1050
1051/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001052 * @brief Resolve (find) a feature definition. Logs directly.
1053 *
1054 * @param[in] feat_name Feature name to resolve.
1055 * @param[in] len Length of \p feat_name.
1056 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001057 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1058 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001059 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001060 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001061 */
1062static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001063resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001064{
1065 char *str;
1066 const char *mod_name, *name;
1067 int mod_name_len, nam_len, i, j;
1068 const struct lys_module *module;
1069
Radek Krejci9ff0a922016-07-14 13:08:05 +02001070 assert(feature);
1071
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001072 /* check prefix */
1073 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) {
1074 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
1075 return -1;
1076 }
1077
1078 module = lys_get_import_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len);
1079 if (!module) {
1080 /* identity refers unknown data model */
1081 LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
1082 return -1;
1083 }
1084
Radek Krejci9ff0a922016-07-14 13:08:05 +02001085 if (module != node->module && module == lys_node_module(node)) {
1086 /* first, try to search directly in submodule where the feature was mentioned */
1087 for (j = 0; j < node->module->features_size; j++) {
1088 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1089 /* check status */
1090 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001091 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001092 return -1;
1093 }
1094 *feature = &node->module->features[j];
1095 return 0;
1096 }
1097 }
1098 }
1099
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001100 /* search in the identified module ... */
1101 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001102 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001103 /* check status */
1104 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001105 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001106 return -1;
1107 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001108 *feature = &module->features[j];
1109 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001110 }
1111 }
1112 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001113 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001114 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001115 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1116 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001117 /* check status */
1118 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1119 module->inc[i].submodule->features[j].flags,
1120 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001121 module->inc[i].submodule->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->inc[i].submodule->features[j];
1125 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001126 }
1127 }
1128 }
1129
1130 /* not found */
1131 str = strndup(feat_name, len);
1132 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
1133 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001134 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001135}
1136
Radek Krejci9ff0a922016-07-14 13:08:05 +02001137/*
1138 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001139 * - 1 if enabled
1140 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001141 * - -1 if not usable by its if-feature expression
1142 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001143static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001144resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001145{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001146 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001147
Radek Krejci9ff0a922016-07-14 13:08:05 +02001148 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001149 if (!resolve_iffeature(&feat->iffeature[i])) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001150 return -1;
1151 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001152 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001153
Radek Krejci69b8d922016-07-27 13:13:41 +02001154 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001155}
1156
1157static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001158resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001159{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001160 uint8_t op;
1161 int rc, a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001162
Radek Krejci9ff0a922016-07-14 13:08:05 +02001163 op = iff_getop(expr->expr, *index_e);
1164 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001165
Radek Krejci9ff0a922016-07-14 13:08:05 +02001166 switch (op) {
1167 case LYS_IFF_F:
1168 /* resolve feature */
1169 return resolve_feature_value(expr->features[(*index_f)++]);
1170 case LYS_IFF_NOT:
1171 rc = resolve_iffeature_recursive(expr, index_e, index_f);
1172 if (rc == -1) {
1173 /* one of the referenced feature is hidden by its if-feature,
1174 * so this if-feature expression is always false */
1175 return -1;
1176 } else {
1177 /* invert result */
1178 return rc ? 0 : 1;
1179 }
1180 case LYS_IFF_AND:
1181 case LYS_IFF_OR:
1182 a = resolve_iffeature_recursive(expr, index_e, index_f);
1183 b = resolve_iffeature_recursive(expr, index_e, index_f);
1184 if (a == -1 || b == -1) {
1185 /* one of the referenced feature is hidden by its if-feature,
1186 * so this if-feature expression is always false */
1187 return -1;
1188 } else if (op == LYS_IFF_AND) {
1189 return a && b;
1190 } else { /* LYS_IFF_OR */
1191 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001192 }
1193 }
1194
Radek Krejci9ff0a922016-07-14 13:08:05 +02001195 return -1;
1196}
1197
1198int
1199resolve_iffeature(struct lys_iffeature *expr)
1200{
1201 int rc = -1;
1202 int index_e = 0, index_f = 0;
1203
1204 if (expr->expr) {
1205 rc = resolve_iffeature_recursive(expr, &index_e, &index_f);
1206 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001207 return (rc == 1) ? 1 : 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001208}
1209
1210struct iff_stack {
1211 int size;
1212 int index; /* first empty item */
1213 uint8_t *stack;
1214};
1215
1216static int
1217iff_stack_push(struct iff_stack *stack, uint8_t value)
1218{
1219 if (stack->index == stack->size) {
1220 stack->size += 4;
1221 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
1222 if (!stack->stack) {
1223 LOGMEM;
1224 stack->size = 0;
1225 return EXIT_FAILURE;
1226 }
1227 }
1228
1229 stack->stack[stack->index++] = value;
1230 return EXIT_SUCCESS;
1231}
1232
1233static uint8_t
1234iff_stack_pop(struct iff_stack *stack)
1235{
1236 stack->index--;
1237 return stack->stack[stack->index];
1238}
1239
1240static void
1241iff_stack_clean(struct iff_stack *stack)
1242{
1243 stack->size = 0;
1244 free(stack->stack);
1245}
1246
1247static void
1248iff_setop(uint8_t *list, uint8_t op, int pos)
1249{
1250 uint8_t *item;
1251 uint8_t mask = 3;
1252
1253 assert(pos >= 0);
1254 assert(op <= 3); /* max 2 bits */
1255
1256 item = &list[pos / 4];
1257 mask = mask << 2 * (pos % 4);
1258 *item = (*item) & ~mask;
1259 *item = (*item) | (op << 2 * (pos % 4));
1260}
1261
1262uint8_t
1263iff_getop(uint8_t *list, int pos)
1264{
1265 uint8_t *item;
1266 uint8_t mask = 3, result;
1267
1268 assert(pos >= 0);
1269
1270 item = &list[pos / 4];
1271 result = (*item) & (mask << 2 * (pos % 4));
1272 return result >> 2 * (pos % 4);
1273}
1274
1275#define LYS_IFF_LP 0x04 /* ( */
1276#define LYS_IFF_RP 0x08 /* ) */
1277
Radek Krejcicbb473e2016-09-16 14:48:32 +02001278/* internal structure for passing data for UNRES_IFFEAT */
1279struct unres_iffeat_data {
1280 struct lys_node *node;
1281 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001282 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001283};
1284
Radek Krejci9ff0a922016-07-14 13:08:05 +02001285void
1286resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1287{
1288 unsigned int e = 0, f = 0, r = 0;
1289 uint8_t op;
1290
1291 assert(iffeat);
1292
1293 if (!iffeat->expr) {
1294 goto result;
1295 }
1296
1297 do {
1298 op = iff_getop(iffeat->expr, e++);
1299 switch (op) {
1300 case LYS_IFF_NOT:
1301 if (!r) {
1302 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001303 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001304 break;
1305 case LYS_IFF_AND:
1306 case LYS_IFF_OR:
1307 if (!r) {
1308 r += 2;
1309 } else {
1310 r += 1;
1311 }
1312 break;
1313 case LYS_IFF_F:
1314 f++;
1315 if (r) {
1316 r--;
1317 }
1318 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001319 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001320 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001321
Radek Krejci9ff0a922016-07-14 13:08:05 +02001322result:
1323 if (expr_size) {
1324 *expr_size = e;
1325 }
1326 if (feat_size) {
1327 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001328 }
1329}
1330
1331int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001332resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001333 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001334{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001335 const char *c = value;
1336 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001337 int i, j, last_not, checkversion = 0;
1338 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001339 uint8_t op;
1340 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001341 struct unres_iffeat_data *iff_data;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001342
Radek Krejci9ff0a922016-07-14 13:08:05 +02001343 assert(c);
1344
1345 if (isspace(c[0])) {
1346 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
1347 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001348 }
1349
Radek Krejci9ff0a922016-07-14 13:08:05 +02001350 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1351 for (i = j = last_not = 0; c[i]; i++) {
1352 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001353 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001354 j++;
1355 continue;
1356 } else if (c[i] == ')') {
1357 j--;
1358 continue;
1359 } else if (isspace(c[i])) {
1360 continue;
1361 }
1362
1363 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1364 if (c[i + r] == '\0') {
Radek Krejcia98da3f2016-07-27 14:05:22 +02001365 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001366 return EXIT_FAILURE;
1367 } else if (!isspace(c[i + r])) {
1368 /* feature name starting with the not/and/or */
1369 last_not = 0;
1370 f_size++;
1371 } else if (c[i] == 'n') { /* not operation */
1372 if (last_not) {
1373 /* double not */
1374 expr_size = expr_size - 2;
1375 last_not = 0;
1376 } else {
1377 last_not = 1;
1378 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001379 } else { /* and, or */
1380 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001381 /* not a not operation */
1382 last_not = 0;
1383 }
1384 i += r;
1385 } else {
1386 f_size++;
1387 last_not = 0;
1388 }
1389 expr_size++;
1390
1391 while (!isspace(c[i])) {
1392 if (!c[i] || c[i] == ')') {
1393 i--;
1394 break;
1395 }
1396 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001397 }
1398 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001399 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001400 /* not matching count of ( and ) */
1401 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1402 return EXIT_FAILURE;
1403 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001404
Radek Krejci69b8d922016-07-27 13:13:41 +02001405 if (checkversion || expr_size > 1) {
1406 /* check that we have 1.1 module */
1407 if (node->module->version != 2) {
1408 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1409 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
1410 return EXIT_FAILURE;
1411 }
1412 }
1413
Radek Krejci9ff0a922016-07-14 13:08:05 +02001414 /* allocate the memory */
1415 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001416 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001417 stack.size = expr_size;
1418 stack.stack = malloc(expr_size * sizeof *stack.stack);
1419 if (!stack.stack || !iffeat_expr->expr || !iffeat_expr->features) {
1420 LOGMEM;
1421 goto error;
1422 }
1423 f_size--; expr_size--; /* used as indexes from now */
1424
1425 for (i--; i >= 0; i--) {
1426 if (c[i] == ')') {
1427 /* push it on stack */
1428 iff_stack_push(&stack, LYS_IFF_RP);
1429 continue;
1430 } else if (c[i] == '(') {
1431 /* pop from the stack into result all operators until ) */
1432 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1433 iff_setop(iffeat_expr->expr, op, expr_size--);
1434 }
1435 continue;
1436 } else if (isspace(c[i])) {
1437 continue;
1438 }
1439
1440 /* end operator or operand -> find beginning and get what is it */
1441 j = i + 1;
1442 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1443 i--;
1444 }
1445 i++; /* get back by one step */
1446
1447 if (!strncmp(&c[i], "not ", 4)) {
1448 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1449 /* double not */
1450 iff_stack_pop(&stack);
1451 } else {
1452 /* not has the highest priority, so do not pop from the stack
1453 * as in case of AND and OR */
1454 iff_stack_push(&stack, LYS_IFF_NOT);
1455 }
1456 } else if (!strncmp(&c[i], "and ", 4)) {
1457 /* as for OR - pop from the stack all operators with the same or higher
1458 * priority and store them to the result, then push the AND to the stack */
1459 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1460 op = iff_stack_pop(&stack);
1461 iff_setop(iffeat_expr->expr, op, expr_size--);
1462 }
1463 iff_stack_push(&stack, LYS_IFF_AND);
1464 } else if (!strncmp(&c[i], "or ", 3)) {
1465 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1466 op = iff_stack_pop(&stack);
1467 iff_setop(iffeat_expr->expr, op, expr_size--);
1468 }
1469 iff_stack_push(&stack, LYS_IFF_OR);
1470 } else {
1471 /* feature name, length is j - i */
1472
1473 /* add it to the result */
1474 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1475
1476 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001477 * forward referenced, we have to keep the feature name in auxiliary
1478 * structure passed into unres */
1479 iff_data = malloc(sizeof *iff_data);
1480 iff_data->node = node;
1481 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001482 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001483 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1484 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001485 f_size--;
1486
1487 if (r == -1) {
Pavol Vican4d084512016-09-29 16:38:12 +02001488 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001489 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001490 }
1491 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001492 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001493 while (stack.index) {
1494 op = iff_stack_pop(&stack);
1495 iff_setop(iffeat_expr->expr, op, expr_size--);
1496 }
1497
1498 if (++expr_size || ++f_size) {
1499 /* not all expected operators and operands found */
1500 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1501 rc = EXIT_FAILURE;
1502 } else {
1503 rc = EXIT_SUCCESS;
1504 }
1505
1506error:
1507 /* cleanup */
1508 iff_stack_clean(&stack);
1509
1510 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001511}
1512
1513/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001514 * @brief Resolve (find) a data node based on a schema-nodeid.
1515 *
1516 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1517 * module).
1518 *
1519 */
1520struct lyd_node *
1521resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1522{
1523 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001524 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001525 const struct lys_node *schema = NULL;
Radek Krejcicc217a62016-04-08 16:58:11 +02001526 int shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001527
1528 assert(nodeid && start);
1529
1530 if (nodeid[0] == '/') {
1531 return NULL;
1532 }
1533
1534 str = p = strdup(nodeid);
1535 if (!str) {
1536 LOGMEM;
1537 return NULL;
1538 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001539
Michal Vasko3edeaf72016-02-11 13:17:43 +01001540 while (p) {
1541 token = p;
1542 p = strchr(p, '/');
1543 if (p) {
1544 *p = '\0';
1545 p++;
1546 }
1547
Radek Krejci5da4eb62016-04-08 14:45:51 +02001548 if (p) {
1549 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001550 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001551 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001552 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001553 result = NULL;
1554 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001555 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001556
Radek Krejci5da4eb62016-04-08 14:45:51 +02001557 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1558 continue;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001559 } else if (lys_parent(schema)->nodetype == LYS_CHOICE) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001560 /* shorthand case */
1561 if (!shorthand) {
1562 shorthand = 1;
Michal Vaskodcf98e62016-05-05 17:53:53 +02001563 schema = lys_parent(schema);
Radek Krejcicc217a62016-04-08 16:58:11 +02001564 continue;
1565 } else {
1566 shorthand = 0;
1567 if (schema->nodetype == LYS_LEAF) {
1568 /* should not be here, since we have leaf, which is not a shorthand nor final node */
1569 result = NULL;
1570 break;
1571 }
1572 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001573 }
1574 } else {
1575 /* final node */
Radek Krejcif3c71de2016-04-11 12:45:46 +02001576 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF,
1577 shorthand ? 0 : 1, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001578 || !schema) {
1579 result = NULL;
1580 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001581 }
1582 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001583 LY_TREE_FOR(result ? result->child : start, iter) {
1584 if (iter->schema == schema) {
1585 /* move in data tree according to returned schema */
1586 result = iter;
1587 break;
1588 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001589 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001590 if (!iter) {
1591 /* instance not found */
1592 result = NULL;
1593 break;
1594 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001595 }
1596 free(str);
1597
1598 return result;
1599}
1600
Radek Krejcibdf92362016-04-08 14:43:34 +02001601/*
1602 * 0 - ok (done)
1603 * 1 - continue
1604 * 2 - break
1605 * -1 - error
1606 */
1607static int
Radek Krejcicc217a62016-04-08 16:58:11 +02001608schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id,
Radek Krejcibdf92362016-04-08 14:43:34 +02001609 const struct lys_module *module, const char *mod_name, int mod_name_len,
Radek Krejci0fa54e92016-09-14 14:01:05 +02001610 int implemented_mod, const struct lys_node **start)
Radek Krejcibdf92362016-04-08 14:43:34 +02001611{
1612 const struct lys_module *prefix_mod;
1613
1614 /* module check */
1615 prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Radek Krejcidf46e222016-11-08 11:57:37 +01001616 if (prefix_mod && implemented_mod) {
Radek Krejci2eee5c02016-12-06 19:18:05 +01001617 prefix_mod = lys_implemented_module(prefix_mod);
Radek Krejci0fa54e92016-09-14 14:01:05 +02001618 }
Radek Krejcibdf92362016-04-08 14:43:34 +02001619 if (!prefix_mod) {
1620 return -1;
1621 }
1622 if (prefix_mod != lys_node_module(sibling)) {
1623 return 1;
1624 }
1625
1626 /* check for shorthand cases - then 'start' does not change */
Michal Vaskodcf98e62016-05-05 17:53:53 +02001627 if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001628 if (*shorthand != -1) {
1629 *shorthand = *shorthand ? 0 : 1;
1630 }
Radek Krejcibdf92362016-04-08 14:43:34 +02001631 }
1632
1633 /* the result node? */
1634 if (!id[0]) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001635 if (*shorthand == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001636 return 1;
1637 }
1638 return 0;
1639 }
1640
Radek Krejci3a130162016-11-07 16:21:20 +01001641 if (!(*shorthand)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001642 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02001643 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001644 return -1;
1645 }
1646 *start = sibling->child;
1647 }
1648
1649 return 2;
1650}
1651
Radek Krejcidf46e222016-11-08 11:57:37 +01001652/* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1
1653 * implement: 0 - do not change the implemented status of the affected modules, 1 - change implemented status of the affected modules
1654 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001655int
1656resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module,
Radek Krejcidf46e222016-11-08 11:57:37 +01001657 int implement, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001658{
Radek Krejcidf46e222016-11-08 11:57:37 +01001659 const char *name, *mod_name, *mod_name_prev, *id;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001660 const struct lys_node *sibling;
1661 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001662 int8_t shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001663 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidf46e222016-11-08 11:57:37 +01001664 const struct lys_module *start_mod, *aux_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001665
1666 assert(nodeid && (start || module) && !(start && module) && ret);
1667
1668 id = nodeid;
1669
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001670 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 +01001671 return ((id - nodeid) - r) + 1;
1672 }
1673 id += r;
1674
1675 if ((is_relative && !start) || (!is_relative && !module)) {
1676 return -1;
1677 }
1678
1679 /* descendant-schema-nodeid */
1680 if (is_relative) {
Michal Vasko4c06fd82016-02-17 13:43:38 +01001681 module = start_mod = start->module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001682
1683 /* absolute-schema-nodeid */
1684 } else {
1685 start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Radek Krejcidf46e222016-11-08 11:57:37 +01001686 if (start_mod != lys_main_module(module) && start_mod && !start_mod->implemented) {
Radek Krejci0fa54e92016-09-14 14:01:05 +02001687 /* if the submodule augments the mainmodule (or in general a module augments
1688 * itself, we don't want to search for the implemented module but augments
1689 * the module anyway. But when augmenting another module, we need the implemented
1690 * revision of the module if any */
Radek Krejci2eee5c02016-12-06 19:18:05 +01001691 aux_mod = lys_implemented_module(start_mod);
Radek Krejcidf46e222016-11-08 11:57:37 +01001692 if (!aux_mod->implemented && implement) {
1693 /* make the found module implemented */
1694 if (lys_set_implemented(aux_mod)) {
1695 return -1;
1696 }
1697 }
1698 start_mod = aux_mod;
1699 implement++;
Radek Krejci0fa54e92016-09-14 14:01:05 +02001700 }
Michal Vaskoe2905632016-02-11 15:42:24 +01001701 if (!start_mod) {
1702 return -1;
1703 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001704 start = start_mod->data;
1705 }
1706
1707 while (1) {
1708 sibling = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01001709 mod_name_prev = mod_name;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001710 while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod,
1711 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
1712 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001713 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejci0fa54e92016-09-14 14:01:05 +02001714 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len,
Radek Krejcidf46e222016-11-08 11:57:37 +01001715 implement, &start);
Radek Krejcibdf92362016-04-08 14:43:34 +02001716 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001717 *ret = sibling;
1718 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001719 } else if (r == 1) {
1720 continue;
1721 } else if (r == 2) {
1722 break;
1723 } else {
1724 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001725 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001726 }
1727 }
1728
1729 /* no match */
1730 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001731 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001732 return EXIT_SUCCESS;
1733 }
1734
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001735 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 +01001736 return ((id - nodeid) - r) + 1;
1737 }
1738 id += r;
Radek Krejcidf46e222016-11-08 11:57:37 +01001739
1740 if ((mod_name && mod_name_prev && strncmp(mod_name, mod_name_prev, mod_name_len + 1)) ||
1741 (mod_name != mod_name_prev && (!mod_name || !mod_name_prev))) {
1742 /* we are getting into another module (augment) */
1743 if (implement) {
1744 /* we have to check that also target modules are implemented, if not, we have to change it */
1745 aux_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
1746 if (!aux_mod) {
1747 return -1;
1748 }
1749 if (!aux_mod->implemented) {
Radek Krejci2eee5c02016-12-06 19:18:05 +01001750 aux_mod = lys_implemented_module(aux_mod);
Radek Krejcidf46e222016-11-08 11:57:37 +01001751 if (!aux_mod->implemented) {
1752 /* make the found module implemented */
1753 if (lys_set_implemented(aux_mod)) {
1754 return -1;
1755 }
1756 }
1757 }
1758 } else {
1759 /* we are not implementing the module itself, so the augments outside the module are ignored */
1760 *ret = NULL;
1761 return EXIT_SUCCESS;
1762 }
1763 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001764 }
1765
1766 /* cannot get here */
1767 LOGINT;
1768 return -1;
1769}
1770
Radek Krejcif3c71de2016-04-11 12:45:46 +02001771/* unique, refine,
1772 * >0 - unexpected char on position (ret - 1),
1773 * 0 - ok (but ret can still be NULL),
1774 * -1 - error,
1775 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001776int
1777resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Radek Krejcif3c71de2016-04-11 12:45:46 +02001778 int check_shorthand, int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001779{
1780 const char *name, *mod_name, *id;
1781 const struct lys_node *sibling;
1782 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001783 int8_t shorthand = check_shorthand ? 0 : -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001784 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001785 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001786
1787 assert(nodeid && start && ret);
1788 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1789
1790 id = nodeid;
1791 module = start->module;
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 if (!is_relative) {
1799 return -1;
1800 }
1801
1802 while (1) {
1803 sibling = NULL;
1804 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
1805 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
1806 /* name match */
1807 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejci0fa54e92016-09-14 14:01:05 +02001808 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start);
Radek Krejcibdf92362016-04-08 14:43:34 +02001809 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001810 if (!(sibling->nodetype & ret_nodetype)) {
1811 /* wrong node type, too bad */
1812 continue;
1813 }
1814 *ret = sibling;
1815 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001816 } else if (r == 1) {
1817 continue;
1818 } else if (r == 2) {
1819 break;
1820 } else {
1821 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001822 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001823 }
1824 }
1825
1826 /* no match */
1827 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001828 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001829 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001830 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1831 *ret = NULL;
1832 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001833 }
1834
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001835 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 +01001836 return ((id - nodeid) - r) + 1;
1837 }
1838 id += r;
1839 }
1840
1841 /* cannot get here */
1842 LOGINT;
1843 return -1;
1844}
1845
1846/* choice default */
1847int
1848resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
1849{
1850 /* cannot actually be a path */
1851 if (strchr(nodeid, '/')) {
1852 return -1;
1853 }
1854
Radek Krejcif3c71de2016-04-11 12:45:46 +02001855 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001856}
1857
1858/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
1859static int
1860resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
1861{
1862 const struct lys_module *module;
1863 const char *mod_prefix, *name;
1864 int i, mod_prefix_len, nam_len;
1865
1866 /* parse the identifier, it must be parsed on one call */
1867 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) {
1868 return -i + 1;
1869 }
1870
1871 module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0);
1872 if (!module) {
1873 return -1;
1874 }
1875 if (module != start->module) {
1876 start = module->data;
1877 }
1878
1879 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
1880
1881 return EXIT_SUCCESS;
1882}
1883
1884int
1885resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
1886 const struct lys_node **ret)
1887{
1888 const char *name, *mod_name, *id;
1889 const struct lys_node *sibling, *start;
1890 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcicc217a62016-04-08 16:58:11 +02001891 int8_t shorthand = 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001892 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001893
1894 assert(nodeid && module && ret);
1895 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
1896
1897 id = nodeid;
1898 start = module->data;
1899
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001900 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 +01001901 return ((id - nodeid) - r) + 1;
1902 }
1903 id += r;
1904
1905 if (is_relative) {
1906 return -1;
1907 }
1908
1909 abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len);
Michal Vaskoe2905632016-02-11 15:42:24 +01001910 if (!abs_start_mod) {
1911 return -1;
1912 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001913
1914 while (1) {
1915 sibling = NULL;
1916 while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE
1917 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) {
1918 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001919 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Radek Krejci0fa54e92016-09-14 14:01:05 +02001920 r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, 0, &start);
Radek Krejcibdf92362016-04-08 14:43:34 +02001921 if (r == 0) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001922 if (!(sibling->nodetype & ret_nodetype)) {
1923 /* wrong node type, too bad */
1924 continue;
1925 }
1926 *ret = sibling;
1927 return EXIT_SUCCESS;
Radek Krejcibdf92362016-04-08 14:43:34 +02001928 } else if (r == 1) {
1929 continue;
1930 } else if (r == 2) {
1931 break;
1932 } else {
1933 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001934 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001935 }
1936 }
1937
1938 /* no match */
1939 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001940 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001941 return EXIT_SUCCESS;
1942 }
1943
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001944 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 +01001945 return ((id - nodeid) - r) + 1;
1946 }
1947 id += r;
1948 }
1949
1950 /* cannot get here */
1951 LOGINT;
1952 return -1;
1953}
1954
Michal Vaskoe733d682016-03-14 09:08:27 +01001955static int
Michal Vasko3547c532016-03-14 09:40:50 +01001956resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01001957{
1958 const char *name;
1959 int nam_len, has_predicate, i;
1960
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001961 if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
1962 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01001963 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01001964 return -1;
1965 }
1966
1967 predicate += i;
1968 *parsed += i;
1969
Michal Vasko58c2aab2017-01-05 10:02:05 +01001970 if (!isdigit(name[0])) {
1971 for (i = 0; i < list->keys_size; ++i) {
1972 if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) {
1973 break;
1974 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001975 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001976
Michal Vasko58c2aab2017-01-05 10:02:05 +01001977 if (i == list->keys_size) {
1978 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
1979 return -1;
1980 }
Michal Vaskoe733d682016-03-14 09:08:27 +01001981 }
1982
1983 /* more predicates? */
1984 if (has_predicate) {
Michal Vasko3547c532016-03-14 09:40:50 +01001985 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01001986 }
1987
1988 return 0;
1989}
1990
Michal Vasko8d26e5c2016-09-08 10:03:49 +02001991/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01001992const struct lys_node *
Michal Vasko8d26e5c2016-09-08 10:03:49 +02001993resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001994{
Michal Vasko10728b52016-04-07 14:26:29 +02001995 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001996 const char *name, *mod_name, *id;
Michal Vasko3547c532016-03-14 09:40:50 +01001997 const struct lys_node *sibling;
Radek Krejcibdf92362016-04-08 14:43:34 +02001998 int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001999 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskoe733d682016-03-14 09:08:27 +01002000 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002001
Michal Vasko3547c532016-03-14 09:40:50 +01002002 assert(nodeid && (ctx || start));
2003 if (!ctx) {
2004 ctx = start->module->ctx;
2005 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002006
2007 id = nodeid;
2008
Michal Vaskoe733d682016-03-14 09:08:27 +01002009 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 +01002010 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002011 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002012 }
2013 id += r;
2014
2015 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002016 assert(start);
2017 start = start->child;
2018 if (!start) {
2019 /* no descendants, fail for sure */
Michal Vasko10728b52016-04-07 14:26:29 +02002020 str = strndup(nodeid, (name + nam_len) - nodeid);
2021 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2022 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002023 return NULL;
2024 }
2025 module = start->module;
2026 } else {
2027 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002028 str = strndup(nodeid, (name + nam_len) - nodeid);
2029 LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
2030 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002031 return NULL;
Michal Vasko971a3ca2016-04-01 13:09:29 +02002032 } else if (mod_name_len > LY_BUF_SIZE - 1) {
2033 LOGINT;
2034 return NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002035 }
2036
Michal Vasko971a3ca2016-04-01 13:09:29 +02002037 if (ly_buf_used && module_name[0]) {
2038 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2039 }
2040 ly_buf_used++;
2041
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002042 memmove(module_name, mod_name, mod_name_len);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002043 module_name[mod_name_len] = '\0';
2044 module = ly_ctx_get_module(ctx, module_name, NULL);
2045
2046 if (buf_backup) {
2047 /* return previous internal buffer content */
2048 strcpy(module_name, buf_backup);
2049 free(buf_backup);
2050 buf_backup = NULL;
2051 }
2052 ly_buf_used--;
2053
Michal Vasko3547c532016-03-14 09:40:50 +01002054 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002055 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2056 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2057 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002058 return NULL;
2059 }
2060 start = module->data;
2061
2062 /* now it's as if there was no module name */
2063 mod_name = NULL;
2064 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002065 }
2066
Michal Vaskoe733d682016-03-14 09:08:27 +01002067 prev_mod = module;
2068
Michal Vasko3edeaf72016-02-11 13:17:43 +01002069 while (1) {
2070 sibling = NULL;
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002071 while ((sibling = lys_getnext(sibling, lys_parent(start), module,
2072 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002073 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002074 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002075 /* module check */
2076 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002077 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002078 LOGINT;
2079 return NULL;
2080 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002081
2082 if (ly_buf_used && module_name[0]) {
2083 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2084 }
2085 ly_buf_used++;
2086
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002087 memmove(module_name, mod_name, mod_name_len);
Michal Vasko8757e7c2016-03-15 10:41:30 +01002088 module_name[mod_name_len] = '\0';
2089 /* will also find an augment module */
2090 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002091
2092 if (buf_backup) {
2093 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002094 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002095 free(buf_backup);
2096 buf_backup = NULL;
2097 }
2098 ly_buf_used--;
2099
Michal Vasko3edeaf72016-02-11 13:17:43 +01002100 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002101 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2102 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2103 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002104 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002105 }
2106 } else {
2107 prefix_mod = prev_mod;
2108 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002109 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002110 continue;
2111 }
2112
Michal Vaskoe733d682016-03-14 09:08:27 +01002113 /* do we have some predicates on it? */
2114 if (has_predicate) {
2115 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002116 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
2117 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
2118 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2119 return NULL;
2120 }
2121 } else if (sibling->nodetype == LYS_LIST) {
2122 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
2123 return NULL;
2124 }
2125 } else {
Michal Vasko43c300e2016-03-22 12:54:27 +01002126 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002127 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002128 }
2129 id += r;
2130 }
2131
Radek Krejcibdf92362016-04-08 14:43:34 +02002132 /* check for shorthand cases - then 'start' does not change */
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002133 if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002134 shorthand = ~shorthand;
2135 }
2136
Michal Vasko3edeaf72016-02-11 13:17:43 +01002137 /* the result node? */
2138 if (!id[0]) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002139 if (shorthand) {
2140 /* wrong path for shorthand */
Michal Vasko025e0452016-05-17 16:14:20 +02002141 str = strndup(nodeid, (name + nam_len) - nodeid);
2142 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko51e5c582017-01-19 14:16:39 +01002143 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Schema shorthand case path must include the virtual case statement.");
Radek Krejci9a5fccc2016-05-18 15:44:58 +02002144 free(str);
Michal Vasko025e0452016-05-17 16:14:20 +02002145 return NULL;
Radek Krejcibdf92362016-04-08 14:43:34 +02002146 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002147 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002148 }
2149
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002150 if (!shorthand) {
Michal Vasko7dc71d02016-03-15 10:42:28 +01002151 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002152 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002153 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko7dc71d02016-03-15 10:42:28 +01002154 return NULL;
2155 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002156 start = sibling->child;
2157 }
2158
2159 /* update prev mod */
2160 prev_mod = start->module;
2161 break;
2162 }
2163 }
2164
2165 /* no match */
2166 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002167 str = strndup(nodeid, (name + nam_len) - nodeid);
2168 LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str);
2169 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002170 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002171 }
2172
Michal Vaskoe733d682016-03-14 09:08:27 +01002173 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 +01002174 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002175 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002176 }
2177 id += r;
2178 }
2179
2180 /* cannot get here */
2181 LOGINT;
Michal Vaskoe733d682016-03-14 09:08:27 +01002182 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002183}
2184
Michal Vasko22448d32016-03-16 13:17:29 +01002185static int
Michal Vasko58c2aab2017-01-05 10:02:05 +01002186resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node,
2187 int position, int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002188{
Michal Vasko9ba34de2016-12-07 12:21:19 +01002189 const char *name, *value, *key_val;
Michal Vasko22448d32016-03-16 13:17:29 +01002190 int nam_len, val_len, has_predicate = 1, r;
2191 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002192 struct lyd_node_leaf_list *key;
Michal Vasko22448d32016-03-16 13:17:29 +01002193
Radek Krejci61a86c62016-03-24 11:06:44 +01002194 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002195 assert(node->schema->nodetype == LYS_LIST);
2196
Michal Vasko53adfc72017-01-06 10:39:10 +01002197 /* is the predicate a number? */
2198 if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
2199 || !strncmp(name, ".", nam_len)) {
2200 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
2201 return -1;
2202 }
2203
2204 if (isdigit(name[0])) {
2205 if (position == atoi(name)) {
2206 /* match */
2207 *parsed += r;
2208 return 0;
2209 } else {
2210 /* not a match */
2211 return 1;
2212 }
2213 }
2214
2215 if (!((struct lys_node_list *)node->schema)->keys_size) {
2216 /* no keys in schema - causes an error later */
2217 return 0;
2218 }
2219
Michal Vaskof29903d2016-04-18 13:13:10 +02002220 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002221 if (!key) {
2222 /* it is not a position, so we need a key for it to be a match */
2223 return 1;
2224 }
2225
2226 /* go through all the keys */
2227 i = 0;
2228 goto check_parsed_values;
2229
2230 for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
Michal Vasko22448d32016-03-16 13:17:29 +01002231 if (!has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002232 LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002233 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002234 }
2235
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002236 if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
2237 || !strncmp(name, ".", nam_len)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002238 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002239 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002240 }
2241
Michal Vasko53adfc72017-01-06 10:39:10 +01002242check_parsed_values:
Michal Vasko22448d32016-03-16 13:17:29 +01002243 predicate += r;
2244 *parsed += r;
2245
Michal Vaskof29903d2016-04-18 13:13:10 +02002246 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002247 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002248 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002249 }
2250
Michal Vasko9ba34de2016-12-07 12:21:19 +01002251 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002252 if ((key->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002253 && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name))
2254 && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002255 key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1;
2256 } else {
2257 key_val = key->value_str;
2258 }
2259
Michal Vasko22448d32016-03-16 13:17:29 +01002260 /* value does not match */
Michal Vasko9ba34de2016-12-07 12:21:19 +01002261 if (strncmp(key_val, value, val_len) || key_val[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002262 return 1;
2263 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002264
2265 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002266 }
2267
2268 if (has_predicate) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002269 LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002270 return -1;
2271 }
2272
2273 return 0;
2274}
2275
Radek Krejci45826012016-08-24 15:07:57 +02002276/**
2277 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2278 *
2279 * @param[in] nodeid Node data path to find
2280 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2281 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2282 * @param[out] parsed Number of characters processed in \p id
2283 * @return The closes parent (or the node itself) from the path
2284 */
Michal Vasko22448d32016-03-16 13:17:29 +01002285struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002286resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2287 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002288{
Michal Vasko10728b52016-04-07 14:26:29 +02002289 char *module_name = ly_buf(), *buf_backup = NULL, *str;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002290 const char *id, *mod_name, *name, *pred_name, *data_val;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002291 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002292 int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002293 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002294 struct lyd_node_leaf_list *llist;
Michal Vasko22448d32016-03-16 13:17:29 +01002295 const struct lys_module *prefix_mod, *prev_mod;
2296 struct ly_ctx *ctx;
2297
2298 assert(nodeid && start && parsed);
2299
2300 ctx = start->schema->module->ctx;
2301 id = nodeid;
2302
2303 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 +01002304 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002305 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002306 return NULL;
2307 }
2308 id += r;
2309 /* add it to parsed only after the data node was actually found */
2310 last_parsed = r;
2311
2312 if (is_relative) {
Michal Vasko1adc7242016-11-16 11:05:28 +01002313 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002314 start = start->child;
2315 } else {
2316 for (; start->parent; start = start->parent);
Michal Vasko1adc7242016-11-16 11:05:28 +01002317 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002318 }
2319
2320 while (1) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002321 list_instance_position = 0;
2322
Michal Vasko22448d32016-03-16 13:17:29 +01002323 LY_TREE_FOR(start, sibling) {
Michal Vasko945b96b2016-10-18 11:49:12 +02002324 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
Michal Vasko2411b942016-03-23 13:50:03 +01002325 if (lys_parent(sibling->schema)) {
2326 if (options & LYD_PATH_OPT_OUTPUT) {
2327 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002328 LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002329 *parsed = -1;
2330 return NULL;
2331 }
2332 } else {
2333 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vaskob7246892016-04-19 10:37:35 +02002334 LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002335 *parsed = -1;
2336 return NULL;
2337 }
2338 }
2339 }
2340
Michal Vasko22448d32016-03-16 13:17:29 +01002341 /* name match */
2342 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2343
2344 /* module check */
2345 if (mod_name) {
Michal Vasko971a3ca2016-04-01 13:09:29 +02002346 if (mod_name_len > LY_BUF_SIZE - 1) {
Michal Vasko22448d32016-03-16 13:17:29 +01002347 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002348 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002349 return NULL;
2350 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002351
2352 if (ly_buf_used && module_name[0]) {
2353 buf_backup = strndup(module_name, LY_BUF_SIZE - 1);
2354 }
2355 ly_buf_used++;
2356
Michal Vaskodf3f1ab2016-04-01 15:07:22 +02002357 memmove(module_name, mod_name, mod_name_len);
Michal Vasko22448d32016-03-16 13:17:29 +01002358 module_name[mod_name_len] = '\0';
2359 /* will also find an augment module */
2360 prefix_mod = ly_ctx_get_module(ctx, module_name, NULL);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002361
2362 if (buf_backup) {
2363 /* return previous internal buffer content */
Michal Vasko888a1292016-04-05 12:08:54 +02002364 strncpy(module_name, buf_backup, LY_BUF_SIZE - 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002365 free(buf_backup);
2366 buf_backup = NULL;
2367 }
2368 ly_buf_used--;
2369
Michal Vasko22448d32016-03-16 13:17:29 +01002370 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002371 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
2372 LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str);
2373 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002374 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002375 return NULL;
2376 }
2377 } else {
2378 prefix_mod = prev_mod;
2379 }
Michal Vasko1adc7242016-11-16 11:05:28 +01002380 if (prefix_mod != lyd_node_module(sibling)) {
Michal Vasko22448d32016-03-16 13:17:29 +01002381 continue;
2382 }
2383
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002384 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002385 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002386 llist = (struct lyd_node_leaf_list *)sibling;
2387
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002388 last_has_pred = 0;
Michal Vaskof0a50972016-10-19 11:33:55 +02002389 if (has_predicate) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002390 if ((r = parse_schema_json_predicate(id, &pred_name, &pred_name_len, &llist_value, &llval_len, &last_has_pred)) < 1) {
Michal Vaskof0a50972016-10-19 11:33:55 +02002391 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2392 *parsed = -1;
2393 return NULL;
2394 }
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002395 if ((pred_name[0] != '.') || (pred_name_len != 1)) {
2396 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1);
2397 *parsed = -1;
2398 return NULL;
2399 }
Michal Vaskof0a50972016-10-19 11:33:55 +02002400 } else {
2401 r = 0;
2402 if (llist_value) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002403 llval_len = strlen(llist_value);
Michal Vaskof0a50972016-10-19 11:33:55 +02002404 }
2405 }
2406
Michal Vasko9ba34de2016-12-07 12:21:19 +01002407 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002408 if ((llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002409 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2410 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002411 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2412 } else {
2413 data_val = llist->value_str;
2414 }
2415
2416 if ((!llist_value && data_val && data_val[0])
2417 || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002418 continue;
2419 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002420
Michal Vaskof0a50972016-10-19 11:33:55 +02002421 id += r;
2422 last_parsed += r;
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002423 has_predicate = last_has_pred;
Michal Vaskof0a50972016-10-19 11:33:55 +02002424
Radek Krejci45826012016-08-24 15:07:57 +02002425 } else if (sibling->schema->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002426 /* 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 +01002427 if (!has_predicate) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002428 /* none match */
2429 return last_match;
Michal Vasko22448d32016-03-16 13:17:29 +01002430 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01002431
2432 ++list_instance_position;
2433 r = 0;
2434 ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r);
Michal Vasko22448d32016-03-16 13:17:29 +01002435 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002436 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002437 return NULL;
2438 } else if (ret == 1) {
2439 /* this list instance does not match */
2440 continue;
2441 }
2442 id += r;
2443 last_parsed += r;
2444 }
2445
2446 *parsed += last_parsed;
2447
2448 /* the result node? */
2449 if (!id[0]) {
2450 return sibling;
2451 }
2452
2453 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002454 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko43c300e2016-03-22 12:54:27 +01002455 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002456 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002457 return NULL;
2458 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002459 last_match = sibling;
Michal Vaskofc11b682016-11-18 09:52:41 +01002460 prev_mod = lyd_node_module(sibling);
Michal Vasko22448d32016-03-16 13:17:29 +01002461 start = sibling->child;
Michal Vasko22448d32016-03-16 13:17:29 +01002462 break;
2463 }
2464 }
2465
2466 /* no match, return last match */
2467 if (!sibling) {
2468 return last_match;
2469 }
2470
2471 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 +01002472 LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002473 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002474 return NULL;
2475 }
2476 id += r;
2477 last_parsed = r;
2478 }
2479
2480 /* cannot get here */
2481 LOGINT;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002482 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002483 return NULL;
2484}
2485
Michal Vasko3edeaf72016-02-11 13:17:43 +01002486/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002487 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002488 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002489 *
Michal Vaskoaeb51802016-04-11 10:58:47 +02002490 * @param[in] str_restr Restriction as a string.
2491 * @param[in] type Type of the restriction.
2492 * @param[out] ret Final interval structure that starts with
2493 * the interval of the initial type, continues with intervals
2494 * of any superior types derived from the initial one, and
2495 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002496 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002497 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002498 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002499int
Michal Vaskoaeb51802016-04-11 10:58:47 +02002500resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002501{
2502 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002503 int kind;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002504 int64_t local_smin, local_smax, local_fmin, local_fmax;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002505 uint64_t local_umin, local_umax;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002506 uint8_t local_fdig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002507 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002508 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002509
2510 switch (type->base) {
2511 case LY_TYPE_BINARY:
2512 kind = 0;
2513 local_umin = 0;
2514 local_umax = 18446744073709551615UL;
2515
2516 if (!str_restr && type->info.binary.length) {
2517 str_restr = type->info.binary.length->expr;
2518 }
2519 break;
2520 case LY_TYPE_DEC64:
2521 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002522 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2523 local_fmax = __INT64_C(9223372036854775807);
2524 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002525
2526 if (!str_restr && type->info.dec64.range) {
2527 str_restr = type->info.dec64.range->expr;
2528 }
2529 break;
2530 case LY_TYPE_INT8:
2531 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002532 local_smin = __INT64_C(-128);
2533 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002534
2535 if (!str_restr && type->info.num.range) {
2536 str_restr = type->info.num.range->expr;
2537 }
2538 break;
2539 case LY_TYPE_INT16:
2540 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002541 local_smin = __INT64_C(-32768);
2542 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002543
2544 if (!str_restr && type->info.num.range) {
2545 str_restr = type->info.num.range->expr;
2546 }
2547 break;
2548 case LY_TYPE_INT32:
2549 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002550 local_smin = __INT64_C(-2147483648);
2551 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002552
2553 if (!str_restr && type->info.num.range) {
2554 str_restr = type->info.num.range->expr;
2555 }
2556 break;
2557 case LY_TYPE_INT64:
2558 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002559 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2560 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002561
2562 if (!str_restr && type->info.num.range) {
2563 str_restr = type->info.num.range->expr;
2564 }
2565 break;
2566 case LY_TYPE_UINT8:
2567 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002568 local_umin = __UINT64_C(0);
2569 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002570
2571 if (!str_restr && type->info.num.range) {
2572 str_restr = type->info.num.range->expr;
2573 }
2574 break;
2575 case LY_TYPE_UINT16:
2576 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002577 local_umin = __UINT64_C(0);
2578 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002579
2580 if (!str_restr && type->info.num.range) {
2581 str_restr = type->info.num.range->expr;
2582 }
2583 break;
2584 case LY_TYPE_UINT32:
2585 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002586 local_umin = __UINT64_C(0);
2587 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002588
2589 if (!str_restr && type->info.num.range) {
2590 str_restr = type->info.num.range->expr;
2591 }
2592 break;
2593 case LY_TYPE_UINT64:
2594 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002595 local_umin = __UINT64_C(0);
2596 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002597
2598 if (!str_restr && type->info.num.range) {
2599 str_restr = type->info.num.range->expr;
2600 }
2601 break;
2602 case LY_TYPE_STRING:
2603 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002604 local_umin = __UINT64_C(0);
2605 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002606
2607 if (!str_restr && type->info.str.length) {
2608 str_restr = type->info.str.length->expr;
2609 }
2610 break;
2611 default:
2612 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002613 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002614 }
2615
2616 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002617 if (type->der) {
2618 if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002619 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002620 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002621 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002622 assert(!intv || (intv->kind == kind));
2623 }
2624
2625 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002626 /* we do not have any restriction, return superior ones */
2627 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002628 return EXIT_SUCCESS;
2629 }
2630
2631 /* adjust local min and max */
2632 if (intv) {
2633 tmp_intv = intv;
2634
2635 if (kind == 0) {
2636 local_umin = tmp_intv->value.uval.min;
2637 } else if (kind == 1) {
2638 local_smin = tmp_intv->value.sval.min;
2639 } else if (kind == 2) {
2640 local_fmin = tmp_intv->value.fval.min;
2641 }
2642
2643 while (tmp_intv->next) {
2644 tmp_intv = tmp_intv->next;
2645 }
2646
2647 if (kind == 0) {
2648 local_umax = tmp_intv->value.uval.max;
2649 } else if (kind == 1) {
2650 local_smax = tmp_intv->value.sval.max;
2651 } else if (kind == 2) {
2652 local_fmax = tmp_intv->value.fval.max;
2653 }
2654 }
2655
2656 /* finally parse our restriction */
2657 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002658 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002659 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002660 if (!tmp_local_intv) {
2661 assert(!local_intv);
2662 local_intv = malloc(sizeof *local_intv);
2663 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002664 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002665 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002666 tmp_local_intv = tmp_local_intv->next;
2667 }
Michal Vasko253035f2015-12-17 16:58:13 +01002668 if (!tmp_local_intv) {
2669 LOGMEM;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002670 goto error;
Michal Vasko253035f2015-12-17 16:58:13 +01002671 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002672
2673 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002674 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002675 tmp_local_intv->next = NULL;
2676
2677 /* min */
2678 ptr = seg_ptr;
2679 while (isspace(ptr[0])) {
2680 ++ptr;
2681 }
2682 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2683 if (kind == 0) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002684 tmp_local_intv->value.uval.min = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002685 } else if (kind == 1) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002686 tmp_local_intv->value.sval.min = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002687 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002688 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
2689 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2690 goto error;
2691 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002692 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002693 } else if (!strncmp(ptr, "min", 3)) {
2694 if (kind == 0) {
2695 tmp_local_intv->value.uval.min = local_umin;
2696 } else if (kind == 1) {
2697 tmp_local_intv->value.sval.min = local_smin;
2698 } else if (kind == 2) {
2699 tmp_local_intv->value.fval.min = local_fmin;
2700 }
2701
2702 ptr += 3;
2703 } else if (!strncmp(ptr, "max", 3)) {
2704 if (kind == 0) {
2705 tmp_local_intv->value.uval.min = local_umax;
2706 } else if (kind == 1) {
2707 tmp_local_intv->value.sval.min = local_smax;
2708 } else if (kind == 2) {
2709 tmp_local_intv->value.fval.min = local_fmax;
2710 }
2711
2712 ptr += 3;
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
2718 while (isspace(ptr[0])) {
2719 ptr++;
2720 }
2721
2722 /* no interval or interval */
2723 if ((ptr[0] == '|') || !ptr[0]) {
2724 if (kind == 0) {
2725 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2726 } else if (kind == 1) {
2727 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2728 } else if (kind == 2) {
2729 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2730 }
2731 } else if (!strncmp(ptr, "..", 2)) {
2732 /* skip ".." */
2733 ptr += 2;
2734 while (isspace(ptr[0])) {
2735 ++ptr;
2736 }
2737
2738 /* max */
2739 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2740 if (kind == 0) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002741 tmp_local_intv->value.uval.max = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002742 } else if (kind == 1) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002743 tmp_local_intv->value.sval.max = strtol(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002744 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002745 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
2746 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
2747 goto error;
2748 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002749 }
2750 } else if (!strncmp(ptr, "max", 3)) {
2751 if (kind == 0) {
2752 tmp_local_intv->value.uval.max = local_umax;
2753 } else if (kind == 1) {
2754 tmp_local_intv->value.sval.max = local_smax;
2755 } else if (kind == 2) {
2756 tmp_local_intv->value.fval.max = local_fmax;
2757 }
2758 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002759 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002760 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002761 }
2762 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02002763 LOGINT;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002764 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002765 }
2766
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002767 /* check min and max in correct order*/
2768 if (kind == 0) {
2769 /* current segment */
2770 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2771 goto error;
2772 }
2773 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2774 goto error;
2775 }
2776 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02002777 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002778 goto error;
2779 }
2780 } else if (kind == 1) {
2781 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
2782 goto error;
2783 }
2784 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
2785 goto error;
2786 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002787 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002788 goto error;
2789 }
2790 } else if (kind == 2) {
2791 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
2792 goto error;
2793 }
2794 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
2795 goto error;
2796 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002797 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002798 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002799 goto error;
2800 }
2801 }
2802
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002803 /* next segment (next OR) */
2804 seg_ptr = strchr(seg_ptr, '|');
2805 if (!seg_ptr) {
2806 break;
2807 }
2808 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002809 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002810 }
2811
2812 /* check local restrictions against superior ones */
2813 if (intv) {
2814 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002815 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002816
2817 while (tmp_local_intv && tmp_intv) {
2818 /* reuse local variables */
2819 if (kind == 0) {
2820 local_umin = tmp_local_intv->value.uval.min;
2821 local_umax = tmp_local_intv->value.uval.max;
2822
2823 /* it must be in this interval */
2824 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2825 /* this interval is covered, next one */
2826 if (local_umax <= tmp_intv->value.uval.max) {
2827 tmp_local_intv = tmp_local_intv->next;
2828 continue;
2829 /* ascending order of restrictions -> fail */
2830 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002831 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002832 }
2833 }
2834 } else if (kind == 1) {
2835 local_smin = tmp_local_intv->value.sval.min;
2836 local_smax = tmp_local_intv->value.sval.max;
2837
2838 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2839 if (local_smax <= tmp_intv->value.sval.max) {
2840 tmp_local_intv = tmp_local_intv->next;
2841 continue;
2842 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002843 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002844 }
2845 }
2846 } else if (kind == 2) {
2847 local_fmin = tmp_local_intv->value.fval.min;
2848 local_fmax = tmp_local_intv->value.fval.max;
2849
Michal Vasko4d1f0482016-09-19 14:35:06 +02002850 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02002851 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002852 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002853 tmp_local_intv = tmp_local_intv->next;
2854 continue;
2855 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002856 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002857 }
2858 }
2859 }
2860
2861 tmp_intv = tmp_intv->next;
2862 }
2863
2864 /* some interval left uncovered -> fail */
2865 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002866 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002867 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002868 }
2869
Michal Vaskoaeb51802016-04-11 10:58:47 +02002870 /* append the local intervals to all the intervals of the superior types, return it all */
2871 if (intv) {
2872 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2873 tmp_intv->next = local_intv;
2874 } else {
2875 intv = local_intv;
2876 }
2877 *ret = intv;
2878
2879 return EXIT_SUCCESS;
2880
2881error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002882 while (intv) {
2883 tmp_intv = intv->next;
2884 free(intv);
2885 intv = tmp_intv;
2886 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02002887 while (local_intv) {
2888 tmp_local_intv = local_intv->next;
2889 free(local_intv);
2890 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002891 }
2892
Michal Vaskoaeb51802016-04-11 10:58:47 +02002893 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002894}
2895
Michal Vasko730dfdf2015-08-11 14:48:05 +02002896/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02002897 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
2898 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002899 *
2900 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02002901 * @param[in] mod_name Typedef name module name.
2902 * @param[in] module Main module.
2903 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002904 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002905 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002906 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002907 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002908int
Michal Vasko1e62a092015-12-01 12:27:20 +01002909resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
2910 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002911{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002912 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02002913 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002914 int tpdf_size;
2915
Michal Vasko1dca6882015-10-22 14:29:42 +02002916 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002917 /* no prefix, try built-in types */
2918 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
2919 if (!strcmp(ly_types[i].def->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002920 if (ret) {
2921 *ret = ly_types[i].def;
2922 }
2923 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002924 }
2925 }
2926 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02002927 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002928 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02002929 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002930 }
2931 }
2932
Michal Vasko1dca6882015-10-22 14:29:42 +02002933 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002934 /* search in local typedefs */
2935 while (parent) {
2936 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002937 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02002938 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
2939 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002940 break;
2941
Radek Krejci76512572015-08-04 09:47:08 +02002942 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02002943 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
2944 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002945 break;
2946
Radek Krejci76512572015-08-04 09:47:08 +02002947 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02002948 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
2949 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002950 break;
2951
Radek Krejci76512572015-08-04 09:47:08 +02002952 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002953 case LYS_ACTION:
2954 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
2955 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002956 break;
2957
Radek Krejci76512572015-08-04 09:47:08 +02002958 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02002959 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
2960 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002961 break;
2962
Radek Krejci76512572015-08-04 09:47:08 +02002963 case LYS_INPUT:
2964 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02002965 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
2966 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002967 break;
2968
2969 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02002970 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002971 continue;
2972 }
2973
2974 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002975 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002976 match = &tpdf[i];
2977 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002978 }
2979 }
2980
Michal Vaskodcf98e62016-05-05 17:53:53 +02002981 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002982 }
Radek Krejcic071c542016-01-27 14:57:51 +01002983 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002984 /* get module where to search */
Michal Vaskob6729c62015-10-21 12:09:47 +02002985 module = lys_get_import_module(module, NULL, 0, mod_name, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02002986 if (!module) {
2987 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002988 }
2989 }
2990
2991 /* search in top level typedefs */
2992 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02002993 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002994 match = &module->tpdf[i];
2995 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002996 }
2997 }
2998
2999 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003000 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003001 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003002 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 +02003003 match = &module->inc[i].submodule->tpdf[j];
3004 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003005 }
3006 }
3007 }
3008
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003009 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003010
3011check_leafref:
3012 if (ret) {
3013 *ret = match;
3014 }
3015 if (match->type.base == LY_TYPE_LEAFREF) {
3016 while (!match->type.info.lref.path) {
3017 match = match->type.der;
3018 assert(match);
3019 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003020 }
3021 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003022}
3023
Michal Vasko1dca6882015-10-22 14:29:42 +02003024/**
3025 * @brief Check the default \p value of the \p type. Logs directly.
3026 *
3027 * @param[in] type Type definition to use.
3028 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003029 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003030 *
3031 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3032 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003033static int
Radek Krejci51673202016-11-01 17:00:32 +01003034check_default(struct lys_type *type, const char **value, struct lys_module *module)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003035{
Radek Krejcibad2f172016-08-02 11:04:15 +02003036 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003037 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003038 const char *dflt = NULL;
Radek Krejci37b756f2016-01-18 10:15:03 +01003039 int ret = EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +02003040
Radek Krejci51673202016-11-01 17:00:32 +01003041 assert(value);
3042
Radek Krejcic13db382016-08-16 10:52:42 +02003043 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003044 /* the type was not resolved yet, nothing to do for now */
3045 return EXIT_FAILURE;
3046 }
3047
Radek Krejci51673202016-11-01 17:00:32 +01003048 dflt = *value;
3049 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003050 /* we do not have a new default value, so is there any to check even, in some base type? */
3051 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3052 if (base_tpdf->dflt) {
Radek Krejci51673202016-11-01 17:00:32 +01003053 dflt = base_tpdf->dflt;
Michal Vasko478c4652016-07-21 12:55:01 +02003054 break;
3055 }
3056 }
3057
Radek Krejci51673202016-11-01 17:00:32 +01003058 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003059 /* no default value, nothing to check, all is well */
3060 return EXIT_SUCCESS;
3061 }
3062
3063 /* 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)? */
3064 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003065 case LY_TYPE_IDENT:
3066 case LY_TYPE_INST:
3067 case LY_TYPE_LEAFREF:
3068 case LY_TYPE_BOOL:
3069 case LY_TYPE_EMPTY:
3070 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
3071 return EXIT_SUCCESS;
Radek Krejcibad2f172016-08-02 11:04:15 +02003072 case LY_TYPE_BITS:
3073 /* the default value must match the restricted list of values, if the type was restricted */
3074 if (type->info.bits.count) {
3075 break;
3076 }
3077 return EXIT_SUCCESS;
3078 case LY_TYPE_ENUM:
3079 /* the default value must match the restricted list of values, if the type was restricted */
3080 if (type->info.enums.count) {
3081 break;
3082 }
3083 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003084 case LY_TYPE_DEC64:
3085 if (type->info.dec64.range) {
3086 break;
3087 }
3088 return EXIT_SUCCESS;
3089 case LY_TYPE_BINARY:
3090 if (type->info.binary.length) {
3091 break;
3092 }
3093 return EXIT_SUCCESS;
3094 case LY_TYPE_INT8:
3095 case LY_TYPE_INT16:
3096 case LY_TYPE_INT32:
3097 case LY_TYPE_INT64:
3098 case LY_TYPE_UINT8:
3099 case LY_TYPE_UINT16:
3100 case LY_TYPE_UINT32:
3101 case LY_TYPE_UINT64:
3102 if (type->info.num.range) {
3103 break;
3104 }
3105 return EXIT_SUCCESS;
3106 case LY_TYPE_STRING:
3107 if (type->info.str.length || type->info.str.patterns) {
3108 break;
3109 }
3110 return EXIT_SUCCESS;
3111 case LY_TYPE_UNION:
3112 /* way too much trouble learning whether we need to check the default again, so just do it */
3113 break;
3114 default:
3115 LOGINT;
3116 return -1;
3117 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003118 } else if (type->base == LY_TYPE_EMPTY) {
3119 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3120 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
3121 return -1;
Michal Vasko478c4652016-07-21 12:55:01 +02003122 }
3123
Michal Vasko1dca6882015-10-22 14:29:42 +02003124 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003125 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003126 node.value_str = dflt;
Michal Vasko1dca6882015-10-22 14:29:42 +02003127 node.value_type = type->base;
Radek Krejci37b756f2016-01-18 10:15:03 +01003128 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko253035f2015-12-17 16:58:13 +01003129 if (!node.schema) {
3130 LOGMEM;
3131 return -1;
3132 }
Radek Krejcibad2f172016-08-02 11:04:15 +02003133 node.schema->name = strdup("fake-default");
Michal Vasko253035f2015-12-17 16:58:13 +01003134 if (!node.schema->name) {
3135 LOGMEM;
Michal Vaskof49eda02016-07-21 12:17:01 +02003136 free(node.schema);
Michal Vasko253035f2015-12-17 16:58:13 +01003137 return -1;
3138 }
Michal Vasko56826402016-03-02 11:11:37 +01003139 node.schema->module = module;
Radek Krejci37b756f2016-01-18 10:15:03 +01003140 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
Michal Vasko1dca6882015-10-22 14:29:42 +02003141
Radek Krejci37b756f2016-01-18 10:15:03 +01003142 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003143 if (!type->info.lref.target) {
3144 ret = EXIT_FAILURE;
3145 goto finish;
3146 }
Radek Krejci51673202016-11-01 17:00:32 +01003147 ret = check_default(&type->info.lref.target->type, &dflt, module);
3148 if (!ret) {
3149 /* adopt possibly changed default value to its canonical form */
3150 if (*value) {
3151 *value = dflt;
3152 }
3153 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003154 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003155 if (!lyp_parse_value(&((struct lys_node_leaf *)node.schema)->type, &node.value_str, NULL, &node, 1, 1)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003156 /* possible forward reference */
3157 ret = 1;
Radek Krejcibad2f172016-08-02 11:04:15 +02003158 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003159 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003160 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3161 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3162 /* we have refined bits/enums */
3163 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
3164 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003165 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003166 }
3167 }
Radek Krejci51673202016-11-01 17:00:32 +01003168 } else {
3169 /* success - adopt canonical form from the node into the default value */
3170 if (dflt != node.value_str) {
3171 /* this can happen only if we have non-inherited default value,
3172 * inherited default values are already in canonical form */
3173 assert(dflt == *value);
3174 *value = node.value_str;
3175 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003176 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003177 }
3178
3179finish:
3180 if (node.value_type == LY_TYPE_BITS) {
3181 free(node.value.bit);
3182 }
3183 free((char *)node.schema->name);
3184 free(node.schema);
3185
3186 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003187}
3188
Michal Vasko730dfdf2015-08-11 14:48:05 +02003189/**
3190 * @brief Check a key for mandatory attributes. Logs directly.
3191 *
3192 * @param[in] key The key to check.
3193 * @param[in] flags What flags to check.
3194 * @param[in] list The list of all the keys.
3195 * @param[in] index Index of the key in the key list.
3196 * @param[in] name The name of the keys.
3197 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003198 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003199 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003200 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003201static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003202check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003203{
Radek Krejciadb57612016-02-16 13:34:34 +01003204 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003205 char *dup = NULL;
3206 int j;
3207
3208 /* existence */
3209 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003210 if (name[len] != '\0') {
3211 dup = strdup(name);
Michal Vasko253035f2015-12-17 16:58:13 +01003212 if (!dup) {
3213 LOGMEM;
3214 return -1;
3215 }
Michal Vaskof02e3742015-08-05 16:27:02 +02003216 dup[len] = '\0';
3217 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003218 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003219 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003220 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003221 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003222 }
3223
3224 /* uniqueness */
3225 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003226 if (key == list->keys[j]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003227 LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003228 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003229 }
3230 }
3231
3232 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003233 if (key->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003234 LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003235 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003236 }
3237
3238 /* type of the leaf is not built-in empty */
Radek Krejcic3738db2016-09-15 15:51:21 +02003239 if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003240 LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003241 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003242 }
3243
3244 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003245 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003246 LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003247 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003248 }
3249
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003250 /* key is not placed from augment */
3251 if (key->parent->nodetype == LYS_AUGMENT) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003252 LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01003253 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003254 return -1;
3255 }
3256
Radek Krejci3f21ada2016-08-01 13:34:31 +02003257 /* key is not when/if-feature -conditional */
3258 j = 0;
3259 if (key->when || (key->iffeature_size && (j = 1))) {
3260 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
Michal Vasko51e5c582017-01-19 14:16:39 +01003261 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003262 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003263 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003264 }
3265
Michal Vasko0b85aa82016-03-07 14:37:43 +01003266 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003267}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003268
3269/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003270 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003271 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003272 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003273 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003274 *
3275 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3276 */
3277int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003278resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003279{
Radek Krejci581ce772015-11-10 17:22:40 +01003280 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003281 const struct lys_node *leaf = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003282
Radek Krejcif3c71de2016-04-11 12:45:46 +02003283 rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003284 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003285 if (rc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003286 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003287 if (rc > 0) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003288 LOGVAL(LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003289 } else if (rc == -2) {
Michal Vasko51e5c582017-01-19 14:16:39 +01003290 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003291 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003292 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003293 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003294 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003295 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003296 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003297 }
Radek Krejci581ce772015-11-10 17:22:40 +01003298 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003299 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003300 if (leaf->nodetype != LYS_LEAF) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003301 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003302 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003303 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003304 }
3305
Radek Krejcicf509982015-12-15 09:22:44 +01003306 /* check status */
Radek Krejci48464ed2016-03-17 15:44:09 +01003307 if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003308 return -1;
3309 }
3310
Radek Krejcid09d1a52016-08-11 14:05:45 +02003311 /* check that all unique's targets are of the same config type */
3312 if (*trg_type) {
3313 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
3314 LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko51e5c582017-01-19 14:16:39 +01003315 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003316 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3317 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3318 return -1;
3319 }
3320 } else {
3321 /* first unique */
3322 if (leaf->flags & LYS_CONFIG_W) {
3323 *trg_type = 1;
3324 } else {
3325 *trg_type = 2;
3326 }
3327 }
3328
Radek Krejcica7efb72016-01-18 13:06:01 +01003329 /* set leaf's unique flag */
3330 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3331
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003332 return EXIT_SUCCESS;
3333
3334error:
3335
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003336 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003337}
3338
Radek Krejci0c0086a2016-03-24 15:20:28 +01003339void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003340unres_data_del(struct unres_data *unres, uint32_t i)
3341{
3342 /* there are items after the one deleted */
3343 if (i+1 < unres->count) {
3344 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003345 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003346
3347 /* deleting the last item */
3348 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003349 free(unres->node);
3350 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003351 }
3352
3353 /* if there are no items after and it is not the last one, just move the counter */
3354 --unres->count;
3355}
3356
Michal Vasko0491ab32015-08-19 14:28:29 +02003357/**
3358 * @brief Resolve (find) a data node from a specific module. Does not log.
3359 *
3360 * @param[in] mod Module to search in.
3361 * @param[in] name Name of the data node.
3362 * @param[in] nam_len Length of the name.
3363 * @param[in] start Data node to start the search from.
3364 * @param[in,out] parents Resolved nodes. If there are some parents,
3365 * they are replaced (!!) with the resolvents.
3366 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003367 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003368 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003369static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003370resolve_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 +02003371{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003372 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003373 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003374 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003375
Michal Vasko23b61ec2015-08-19 11:19:50 +02003376 if (!parents->count) {
3377 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003378 parents->node = malloc(sizeof *parents->node);
Michal Vasko253035f2015-12-17 16:58:13 +01003379 if (!parents->node) {
3380 LOGMEM;
Michal Vasko2471e7f2016-04-11 11:00:15 +02003381 return -1;
Michal Vasko253035f2015-12-17 16:58:13 +01003382 }
Michal Vaskocf024702015-10-08 15:01:42 +02003383 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003384 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003385 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003386 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003387 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003388 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003389 continue;
3390 }
3391 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003392 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003393 if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len)
3394 && node->schema->name[nam_len] == '\0') {
3395 /* matching target */
3396 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003397 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003398 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003399 flag = 1;
3400 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003401 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003402 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003403 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
3404 if (!parents->node) {
3405 return EXIT_FAILURE;
3406 }
Michal Vaskocf024702015-10-08 15:01:42 +02003407 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003408 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003409 }
3410 }
3411 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003412
3413 if (!flag) {
3414 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003415 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003416 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003417 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003418 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003419 }
3420
Michal Vasko0491ab32015-08-19 14:28:29 +02003421 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003422}
3423
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003424/**
3425 * @brief Resolve (find) a data node. Does not log.
3426 *
Radek Krejci581ce772015-11-10 17:22:40 +01003427 * @param[in] mod_name Module name of the data node.
3428 * @param[in] mod_name_len Length of the module name.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003429 * @param[in] name Name of the data node.
3430 * @param[in] nam_len Length of the name.
3431 * @param[in] start Data node to start the search from.
3432 * @param[in,out] parents Resolved nodes. If there are some parents,
3433 * they are replaced (!!) with the resolvents.
3434 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003435 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003436 */
Radek Krejcic5090c32015-08-12 09:46:19 +02003437static int
Radek Krejci581ce772015-11-10 17:22:40 +01003438resolve_data_node(const char *mod_name, int mod_name_len, const char *name, int name_len, struct lyd_node *start,
Michal Vasko23b61ec2015-08-19 11:19:50 +02003439 struct unres_data *parents)
Radek Krejcic5090c32015-08-12 09:46:19 +02003440{
Michal Vasko1e62a092015-12-01 12:27:20 +01003441 const struct lys_module *mod;
Michal Vasko31fc3672015-10-21 12:08:13 +02003442 char *str;
Radek Krejcic5090c32015-08-12 09:46:19 +02003443
Michal Vasko23b61ec2015-08-19 11:19:50 +02003444 assert(start);
3445
Michal Vasko31fc3672015-10-21 12:08:13 +02003446 if (mod_name) {
3447 /* we have mod_name, find appropriate module */
3448 str = strndup(mod_name, mod_name_len);
Michal Vasko253035f2015-12-17 16:58:13 +01003449 if (!str) {
3450 LOGMEM;
3451 return -1;
3452 }
Michal Vasko31fc3672015-10-21 12:08:13 +02003453 mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL);
3454 free(str);
Radek Krejcic5090c32015-08-12 09:46:19 +02003455 if (!mod) {
3456 /* invalid prefix */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003457 return -1;
Radek Krejcic5090c32015-08-12 09:46:19 +02003458 }
3459 } else {
3460 /* no prefix, module is the same as of current node */
3461 mod = start->schema->module;
3462 }
3463
3464 return resolve_data(mod, name, name_len, start, parents);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003465}
3466
Michal Vasko730dfdf2015-08-11 14:48:05 +02003467/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003468 * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly
Radek Krejci48464ed2016-03-17 15:44:09 +01003469 * only specific errors, general no-resolvent error is left to the caller.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003470 *
Michal Vaskobb211122015-08-19 14:03:11 +02003471 * @param[in] pred Predicate to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003472 * @param[in] node Node from which the predicate is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02003473 * @param[in,out] node_match Nodes satisfying the restriction
3474 * without the predicate. Nodes not
3475 * satisfying the predicate are removed.
Michal Vasko0491ab32015-08-19 14:28:29 +02003476 * @param[out] parsed Number of characters parsed, negative on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003477 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003478 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003479 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003480static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003481resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match,
Radek Krejci010e54b2016-03-15 09:40:34 +01003482 int *parsed)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003483{
Michal Vasko730dfdf2015-08-11 14:48:05 +02003484 /* ... /node[source = destination] ... */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003485 struct unres_data source_match, dest_match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003486 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vasko0491ab32015-08-19 14:28:29 +02003487 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0;
3488 int has_predicate, dest_parent_times, i, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003489 uint32_t j;
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003490 struct lyd_node_leaf_list *leaf_dst, *leaf_src;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003491
3492 source_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003493 source_match.node = malloc(sizeof *source_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003494 if (!source_match.node) {
3495 LOGMEM;
3496 return -1;
3497 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003498 dest_match.count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003499 dest_match.node = malloc(sizeof *dest_match.node);
Michal Vasko253035f2015-12-17 16:58:13 +01003500 if (!dest_match.node) {
3501 LOGMEM;
3502 return -1;
3503 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003504
3505 do {
3506 if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
3507 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003508 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003509 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003510 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003511 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003512 parsed_loc += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003513 pred += i;
3514
Michal Vasko23b61ec2015-08-19 11:19:50 +02003515 for (j = 0; j < node_match->count;) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003516 /* source */
Michal Vaskocf024702015-10-08 15:01:42 +02003517 source_match.node[0] = node_match->node[j];
Michal Vasko23b61ec2015-08-19 11:19:50 +02003518
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003519 /* must be leaf (key of a list) */
Radek Krejci581ce772015-11-10 17:22:40 +01003520 if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j],
Michal Vaskocf024702015-10-08 15:01:42 +02003521 &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003522 i = 0;
3523 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003524 }
3525
3526 /* destination */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003527 dest_match.node[0] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003528 dest_parent_times = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003529 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3530 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003531 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003532 rc = -1;
3533 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003534 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003535 pke_parsed = i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003536 for (i = 0; i < dest_parent_times; ++i) {
Michal Vaskocf024702015-10-08 15:01:42 +02003537 dest_match.node[0] = dest_match.node[0]->parent;
3538 if (!dest_match.node[0]) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003539 i = 0;
Michal Vasko0491ab32015-08-19 14:28:29 +02003540 rc = EXIT_FAILURE;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003541 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003542 }
3543 }
3544 while (1) {
Radek Krejci581ce772015-11-10 17:22:40 +01003545 if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0],
Michal Vasko0491ab32015-08-19 14:28:29 +02003546 &dest_match)) || (dest_match.count != 1)) {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003547 i = 0;
3548 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003549 }
3550
3551 if (pke_len == pke_parsed) {
3552 break;
3553 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003554 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003555 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003556 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003557 rc = -1;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003558 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003559 }
3560 pke_parsed += i;
3561 }
3562
3563 /* check match between source and destination nodes */
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003564 leaf_dst = (struct lyd_node_leaf_list *)dest_match.node[0];
3565 while (leaf_dst->value_type == LY_TYPE_LEAFREF) {
3566 leaf_dst = (struct lyd_node_leaf_list *)leaf_dst->value.leafref;
3567 }
3568 leaf_src = (struct lyd_node_leaf_list *)source_match.node[0];
3569 while (leaf_src->value_type == LY_TYPE_LEAFREF) {
3570 leaf_src = (struct lyd_node_leaf_list *)leaf_src->value.leafref;
3571 }
3572 if (leaf_src->value_type != leaf_dst->value_type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003573 goto remove_leafref;
3574 }
3575
Radek Krejci0c2fa3a2016-06-13 15:18:03 +02003576 if (!ly_strequal(leaf_src->value_str, leaf_dst->value_str, 1)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003577 goto remove_leafref;
3578 }
3579
3580 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003581 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003582 continue;
3583
3584remove_leafref:
3585 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003586 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003587 }
3588 } while (has_predicate);
3589
Michal Vaskocf024702015-10-08 15:01:42 +02003590 free(source_match.node);
3591 free(dest_match.node);
Michal Vasko0491ab32015-08-19 14:28:29 +02003592 if (parsed) {
3593 *parsed = parsed_loc;
3594 }
3595 return EXIT_SUCCESS;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003596
3597error:
3598
3599 if (source_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003600 free(source_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003601 }
3602 if (dest_match.count) {
Michal Vaskocf024702015-10-08 15:01:42 +02003603 free(dest_match.node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003604 }
Michal Vasko0491ab32015-08-19 14:28:29 +02003605 if (parsed) {
3606 *parsed = -parsed_loc+i;
3607 }
3608 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003609}
3610
Michal Vasko730dfdf2015-08-11 14:48:05 +02003611/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003612 * @brief Resolve a path (leafref) in JSON data context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003613 *
Michal Vaskocf024702015-10-08 15:01:42 +02003614 * @param[in] node Leafref data node.
Michal Vasko23b61ec2015-08-19 11:19:50 +02003615 * @param[in] path Path of the leafref.
Radek Krejci48464ed2016-03-17 15:44:09 +01003616 * @param[out] ret Matching nodes. Expects an empty, but allocated structure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003617 *
Michal Vasko0491ab32015-08-19 14:28:29 +02003618 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003619 */
Michal Vasko184521f2015-09-24 13:14:26 +02003620static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003621resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003622{
Radek Krejci71b795b2015-08-10 16:20:39 +02003623 struct lyd_node *data = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003624 const char *prefix, *name;
Michal Vasko0491ab32015-08-19 14:28:29 +02003625 int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003626 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003627
Michal Vaskocf024702015-10-08 15:01:42 +02003628 assert(node && path && ret && !ret->count);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003629
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003630 parent_times = 0;
Michal Vaskod9173342015-08-17 14:35:36 +02003631 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003632
3633 /* searching for nodeset */
3634 do {
Radek Krejcif7ed4c32016-10-27 16:20:03 +02003635 if ((i = parse_path_arg(node->schema->module, path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003636 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]);
Michal Vasko0491ab32015-08-19 14:28:29 +02003637 rc = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003638 goto error;
3639 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003640 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003641 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003642
Michal Vasko23b61ec2015-08-19 11:19:50 +02003643 if (!ret->count) {
Michal Vaskobfd98e62016-09-02 09:50:05 +02003644 if (parent_times > 0) {
3645 data = node;
3646 for (i = 1; i < parent_times; ++i) {
3647 data = data->parent;
Michal Vasko253035f2015-12-17 16:58:13 +01003648 }
Michal Vaskobfd98e62016-09-02 09:50:05 +02003649 } else if (!parent_times) {
3650 data = node->child;
3651 } else {
3652 /* absolute path */
3653 for (data = node; data->parent; data = data->parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003654 }
3655
Michal Vaskobfd98e62016-09-02 09:50:05 +02003656 /* we may still be parsing it and the pointer is not correct yet */
3657 if (data->prev) {
3658 while (data->prev->next) {
3659 data = data->prev;
Michal Vasko8bcdf292015-08-19 14:04:43 +02003660 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003661 }
3662 }
3663
3664 /* node identifier */
Radek Krejci581ce772015-11-10 17:22:40 +01003665 if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003666 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003667 LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name);
Michal Vasko184521f2015-09-24 13:14:26 +02003668 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003669 goto error;
3670 }
3671
3672 if (has_predicate) {
3673 /* we have predicate, so the current results must be lists */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003674 for (j = 0; j < ret->count;) {
Michal Vaskocf024702015-10-08 15:01:42 +02003675 if (ret->node[j]->schema->nodetype == LYS_LIST &&
3676 ((struct lys_node_list *)ret->node[0]->schema)->keys) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003677 /* leafref is ok, continue check with next leafref */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003678 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003679 continue;
3680 }
3681
3682 /* does not fulfill conditions, remove leafref record */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003683 unres_data_del(ret, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003684 }
Radek Krejci48464ed2016-03-17 15:44:09 +01003685 if ((rc = resolve_path_predicate_data(path, node, ret, &i))) {
Radek Krejci010e54b2016-03-15 09:40:34 +01003686 if (rc == -1) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003687 LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path);
Michal Vasko184521f2015-09-24 13:14:26 +02003688 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003689 goto error;
3690 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003691 path += i;
Michal Vaskod9173342015-08-17 14:35:36 +02003692 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003693
Michal Vasko23b61ec2015-08-19 11:19:50 +02003694 if (!ret->count) {
Michal Vasko0491ab32015-08-19 14:28:29 +02003695 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003696 goto error;
3697 }
3698 }
3699 } while (path[0] != '\0');
3700
Michal Vaskof02e3742015-08-05 16:27:02 +02003701 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003702
3703error:
3704
Michal Vaskocf024702015-10-08 15:01:42 +02003705 free(ret->node);
3706 ret->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003707 ret->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003708
Michal Vasko0491ab32015-08-19 14:28:29 +02003709 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003710}
3711
Michal Vaskoe27516a2016-10-10 17:55:31 +00003712static int
3713resolve_path_arg_schema_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path)
3714{
3715 int dep1, dep2;
3716 const struct lys_node *node;
3717
3718 if (lys_parent(op_node)) {
3719 /* inner operation (notif/action) */
3720 if (abs_path) {
3721 return 1;
3722 } else {
3723 /* compare depth of both nodes */
3724 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3725 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3726 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3727 return 1;
3728 }
3729 }
3730 } else {
3731 /* top-level operation (notif/rpc) */
3732 if (op_node != first_node) {
3733 return 1;
3734 }
3735 }
3736
3737 return 0;
3738}
3739
Michal Vasko730dfdf2015-08-11 14:48:05 +02003740/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003741 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003742 *
Michal Vaskobb211122015-08-19 14:03:11 +02003743 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003744 * @param[in] context_node Predicate context node (where the predicate is placed).
3745 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003746 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003747 *
Michal Vasko184521f2015-09-24 13:14:26 +02003748 * @return 0 on forward reference, otherwise the number
3749 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003750 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003751 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003752static int
Radek Krejciadb57612016-02-16 13:34:34 +01003753resolve_path_predicate_schema(const char *path, const struct lys_node *context_node,
Michal Vaskoe27516a2016-10-10 17:55:31 +00003754 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003755{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003756 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003757 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003758 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003759 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3760 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko1f76a282015-08-04 16:16:53 +02003761
3762 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003763 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003764 &pke_len, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003765 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003766 return -parsed+i;
3767 }
3768 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003769 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003770
Michal Vasko58090902015-08-13 14:04:15 +02003771 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003772 if (sour_pref) {
3773 trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len);
3774 } else {
3775 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003776 }
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003777 rc = lys_get_data_sibling(trg_mod, context_node->child, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003778 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003779 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003780 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003781 }
3782
3783 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003784 dest_parent_times = 0;
3785 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003786 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3787 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003788 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 +02003789 return -parsed;
3790 }
3791 pke_parsed += i;
3792
Radek Krejciadb57612016-02-16 13:34:34 +01003793 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vaskofbaead72016-10-07 10:54:48 +02003794 /* path is supposed to be evaluated in data tree, so we have to skip
3795 * all schema nodes that cannot be instantiated in data tree */
3796 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003797 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003798 dst_node = lys_parent(dst_node));
3799
Michal Vasko1f76a282015-08-04 16:16:53 +02003800 if (!dst_node) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003801 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003802 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003803 }
3804 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003805 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003806 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003807 if (dest_pref) {
3808 trg_mod = lys_get_import_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len);
3809 } else {
3810 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003811 }
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003812 rc = lys_get_data_sibling(trg_mod, dst_node->child, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003813 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003814 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003815 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003816 }
3817
Michal Vaskoe27516a2016-10-10 17:55:31 +00003818 if (first_iter) {
3819 if (resolve_path_arg_schema_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003820 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003821 }
3822 first_iter = 0;
3823 }
3824
Michal Vasko1f76a282015-08-04 16:16:53 +02003825 if (pke_len == pke_parsed) {
3826 break;
3827 }
3828
3829 if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
3830 &dest_parent_times)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003831 LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Radek Krejciadb57612016-02-16 13:34:34 +01003832 (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003833 return -parsed;
3834 }
3835 pke_parsed += i;
3836 }
3837
3838 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003839 if (dst_node->nodetype != src_node->nodetype) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003840 LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko51e5c582017-01-19 14:16:39 +01003841 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003842 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003843 return -parsed;
3844 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003845 } while (has_predicate);
3846
3847 return parsed;
3848}
3849
Michal Vasko730dfdf2015-08-11 14:48:05 +02003850/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003851 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003852 *
Michal Vaskobb211122015-08-19 14:03:11 +02003853 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003854 * @param[in] parent_node Parent of the leafref.
Radek Krejci2f12f852016-01-08 12:59:57 +01003855 * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path
3856 * has to contain absolute path
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003857 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003858 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003859 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003860 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003861static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003862resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf,
Michal Vasko36cbaa42015-12-14 13:15:48 +01003863 const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003864{
Michal Vaskoe27516a2016-10-10 17:55:31 +00003865 const struct lys_node *node, *op_node = NULL;
Radek Krejci990af1f2016-11-09 13:53:36 +01003866 const struct lys_module *mod;
3867 struct lys_module *mod_start;
Michal Vasko1f76a282015-08-04 16:16:53 +02003868 const char *id, *prefix, *name;
3869 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003870 int i, first_iter, rc;
Michal Vasko1f76a282015-08-04 16:16:53 +02003871
Michal Vasko184521f2015-09-24 13:14:26 +02003872 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003873 parent_times = 0;
3874 id = path;
3875
Michal Vaskoe27516a2016-10-10 17:55:31 +00003876 /* find operation schema we are in, if applicable */
Michal Vaskoe9914d12016-10-07 14:32:37 +02003877 if (!parent_tpdf) {
Michal Vaskoe27516a2016-10-10 17:55:31 +00003878 for (op_node = lys_parent(parent);
3879 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
3880 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02003881 }
3882
Radek Krejci990af1f2016-11-09 13:53:36 +01003883 mod_start = lys_node_module(parent);
Michal Vasko1f76a282015-08-04 16:16:53 +02003884 do {
Radek Krejci990af1f2016-11-09 13:53:36 +01003885 if ((i = parse_path_arg(mod_start, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003886 LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003887 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003888 }
3889 id += i;
3890
Michal Vasko184521f2015-09-24 13:14:26 +02003891 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003892 if (parent_times == -1) {
Radek Krejcic071c542016-01-27 14:57:51 +01003893 /* resolve prefix of the module */
Radek Krejci990af1f2016-11-09 13:53:36 +01003894 mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start;
3895 if (!mod) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003896 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3897 "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003898 return EXIT_FAILURE;
Michal Vasko58090902015-08-13 14:04:15 +02003899 }
Radek Krejci990af1f2016-11-09 13:53:36 +01003900 if (!mod->implemented) {
Radek Krejci2eee5c02016-12-06 19:18:05 +01003901 mod = lys_implemented_module(mod);
Radek Krejci990af1f2016-11-09 13:53:36 +01003902 if (!mod->implemented) {
3903 /* make the found module implemented */
3904 if (lys_set_implemented(mod)) {
3905 return EXIT_FAILURE;
3906 }
3907 }
3908 }
3909 /* get start node */
3910 if (!mod->data) {
3911 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3912 "leafref", path);
3913 return EXIT_FAILURE;
3914 }
3915 node = mod->data;
3916
Michal Vasko1f76a282015-08-04 16:16:53 +02003917 } else if (parent_times > 0) {
Radek Krejci2f12f852016-01-08 12:59:57 +01003918 if (parent_tpdf) {
3919 /* the path is not allowed to contain relative path since we are in top level typedef */
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003920 LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path);
Radek Krejci2f12f852016-01-08 12:59:57 +01003921 return -1;
3922 }
3923
Michal Vasko94458082016-10-07 14:34:36 +02003924 /* we are looking for a sibling of a node, node it's parent (that is why parent_times - 1) */
3925 for (i = 0, node = parent; i < parent_times - 1; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02003926 /* path is supposed to be evaluated in data tree, so we have to skip
3927 * all schema nodes that cannot be instantiated in data tree */
3928 for (node = lys_parent(node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003929 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Radek Krejci3a5501d2016-07-18 22:03:34 +02003930 node = lys_parent(node));
3931
Michal Vasko1f76a282015-08-04 16:16:53 +02003932 if (!node) {
Michal Vaskoe9914d12016-10-07 14:32:37 +02003933 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003934 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003935 }
3936 }
Radek Krejci990af1f2016-11-09 13:53:36 +01003937
3938 /* now we have to check that if we are going into a node from a different module,
3939 * the module is implemented (so its augments are applied) */
3940 mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start;
3941 if (!mod) {
3942 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3943 return EXIT_FAILURE;
3944 }
3945 if (!mod->implemented) {
Radek Krejci2eee5c02016-12-06 19:18:05 +01003946 mod = lys_implemented_module(mod);
Radek Krejci990af1f2016-11-09 13:53:36 +01003947 if (!mod->implemented) {
3948 /* make the found module implemented */
3949 if (lys_set_implemented(mod)) {
3950 return EXIT_FAILURE;
3951 }
3952 }
3953 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003954 } else {
3955 LOGINT;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003956 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003957 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003958 } else {
Radek Krejci990af1f2016-11-09 13:53:36 +01003959 /* we have to first check that the module we are going into is implemented */
3960 mod = prefix ? lys_get_import_module(mod_start, NULL, 0, prefix, pref_len) : mod_start;
3961 if (!mod) {
3962 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3963 return EXIT_FAILURE;
3964 }
3965 if (!mod->implemented) {
Radek Krejci2eee5c02016-12-06 19:18:05 +01003966 mod = lys_implemented_module(mod);
Radek Krejci990af1f2016-11-09 13:53:36 +01003967 if (!mod->implemented) {
3968 /* make the found module implemented */
3969 if (lys_set_implemented(mod)) {
3970 return EXIT_FAILURE;
3971 }
3972 }
3973 }
3974
Michal Vasko7dc71d02016-03-15 10:42:28 +01003975 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02003976 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko2f5aceb2016-03-22 10:24:14 +01003977 LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, name[0], name);
Michal Vasko7dc71d02016-03-15 10:42:28 +01003978 return -1;
3979 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003980 node = node->child;
Radek Krejci43ccc4c2016-10-18 20:40:06 +02003981 if (!node) {
3982 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent,
3983 "leafref", path);
3984 return EXIT_FAILURE;
3985 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003986 }
3987
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003988 rc = lys_get_data_sibling(mod, node, name, nam_len, LYS_LIST | LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF
3989 | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA, &node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003990 if (rc) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02003991 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003992 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003993 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003994
Michal Vaskoe27516a2016-10-10 17:55:31 +00003995 if (first_iter) {
3996 /* set external dependency flag, we can decide based on the first found node */
3997 if (!parent_tpdf && op_node && parent_times &&
3998 resolve_path_arg_schema_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003999 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00004000 }
4001 first_iter = 0;
4002 }
4003
Michal Vasko1f76a282015-08-04 16:16:53 +02004004 if (has_predicate) {
4005 /* we have predicate, so the current result must be list */
4006 if (node->nodetype != LYS_LIST) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02004007 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004008 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004009 }
4010
Michal Vaskoe27516a2016-10-10 17:55:31 +00004011 i = resolve_path_predicate_schema(id, node, parent, op_node);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004012 if (i <= 0) {
4013 if (i == 0) {
4014 return EXIT_FAILURE;
4015 } else { /* i < 0 */
4016 return -1;
4017 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004018 }
4019 id += i;
Michal Vaskof9b35d92016-10-21 15:19:30 +02004020 has_predicate = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02004021 }
4022 } while (id[0]);
4023
Michal Vaskoca917682016-07-25 11:00:37 +02004024 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
Radek Krejci2a5a9602016-11-04 10:21:13 +01004025 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
Michal Vasko3e9f41e2016-05-20 11:41:39 +02004026 LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path);
Michal Vasko51e5c582017-01-19 14:16:39 +01004027 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 +02004028 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02004029 }
4030
Radek Krejcicf509982015-12-15 09:22:44 +01004031 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01004032 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01004033 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01004034 return -1;
4035 }
4036
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004037 if (ret) {
4038 *ret = node;
4039 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004040
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004041 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02004042}
4043
Michal Vasko730dfdf2015-08-11 14:48:05 +02004044/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004045 * @brief Resolve instance-identifier predicate in JSON data format.
4046 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004047 *
Michal Vaskobb211122015-08-19 14:03:11 +02004048 * @param[in] pred Predicate to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004049 * @param[in,out] node_match Nodes matching the restriction without
4050 * the predicate. Nodes not satisfying
4051 * the predicate are removed.
4052 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004053 * @return Number of characters successfully parsed,
4054 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004055 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004056static int
Michal Vaskof39142b2015-10-21 11:40:05 +02004057resolve_predicate(const char *pred, struct unres_data *node_match)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004058{
Michal Vasko730dfdf2015-08-11 14:48:05 +02004059 /* ... /node[target = value] ... */
Michal Vaskob2f40be2016-09-08 16:03:48 +02004060 struct lyd_node *target;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004061 const char *model, *name, *value;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004062 int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed, pred_iter, k;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004063 uint32_t j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004064
Michal Vasko1f2cc332015-08-19 11:18:32 +02004065 assert(pred && node_match->count);
4066
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004067 idx = -1;
4068 parsed = 0;
4069
Michal Vaskob2f40be2016-09-08 16:03:48 +02004070 pred_iter = -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004071 do {
Michal Vaskof39142b2015-10-21 11:40:05 +02004072 if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004073 return -parsed+i;
4074 }
4075 parsed += i;
4076 pred += i;
4077
4078 if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004079 /* pos */
4080 assert(!value);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004081 idx = atoi(name);
Michal Vaskob2f40be2016-09-08 16:03:48 +02004082 } else if (name[0] != '.') {
4083 /* list keys */
4084 if (pred_iter < 0) {
4085 pred_iter = 1;
4086 } else {
4087 ++pred_iter;
4088 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004089 }
4090
Michal Vaskof2f28a12016-09-09 12:43:06 +02004091 for (cur_idx = 1, j = 0; j < node_match->count; ++cur_idx) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004092 /* target */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004093 if (name[0] == '.') {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004094 /* leaf-list value */
Michal Vaskocf024702015-10-08 15:01:42 +02004095 if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004096 goto remove_instid;
4097 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004098
4099 target = node_match->node[j];
4100 /* check the value */
4101 if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len)
4102 || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) {
4103 goto remove_instid;
4104 }
4105
4106 } else if (!value) {
4107 /* keyless list position */
4108 if ((node_match->node[j]->schema->nodetype != LYS_LIST)
4109 || ((struct lys_node_list *)node_match->node[j]->schema)->keys) {
4110 goto remove_instid;
4111 }
4112
4113 if (idx != cur_idx) {
4114 goto remove_instid;
4115 }
4116
4117 } else {
4118 /* list key value */
Michal Vaskocf024702015-10-08 15:01:42 +02004119 if (node_match->node[j]->schema->nodetype != LYS_LIST) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004120 goto remove_instid;
4121 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004122
4123 /* key module must match the list module */
4124 if (strncmp(node_match->node[j]->schema->module->name, model, mod_len)
4125 || node_match->node[j]->schema->module->name[mod_len]) {
4126 goto remove_instid;
4127 }
4128 /* find the key leaf */
Michal Vasko045182c2016-09-09 12:44:07 +02004129 for (k = 1, target = node_match->node[j]->child; target && (k < pred_iter); k++, target = target->next);
Michal Vaskob2f40be2016-09-08 16:03:48 +02004130 if (!target) {
4131 goto remove_instid;
4132 }
4133 if ((struct lys_node_leaf *)target->schema !=
4134 ((struct lys_node_list *)node_match->node[j]->schema)->keys[pred_iter - 1]) {
4135 goto remove_instid;
4136 }
4137
4138 /* check the value */
4139 if (strncmp(((struct lyd_node_leaf_list *)target)->value_str, value, val_len)
4140 || ((struct lyd_node_leaf_list *)target)->value_str[val_len]) {
4141 goto remove_instid;
4142 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004143 }
4144
Michal Vaskob2f40be2016-09-08 16:03:48 +02004145 /* instid is ok, continue check with the next one */
Michal Vasko1f2cc332015-08-19 11:18:32 +02004146 ++j;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004147 continue;
4148
4149remove_instid:
Michal Vaskob2f40be2016-09-08 16:03:48 +02004150 /* does not fulfill conditions, remove instid record */
Michal Vasko1f2cc332015-08-19 11:18:32 +02004151 unres_data_del(node_match, j);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004152 }
4153 } while (has_predicate);
4154
Michal Vaskob2f40be2016-09-08 16:03:48 +02004155 /* check that all list keys were specified */
4156 if ((pred_iter > 0) && node_match->count) {
Michal Vasko045182c2016-09-09 12:44:07 +02004157 j = 0;
4158 while (j < node_match->count) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004159 assert(node_match->node[j]->schema->nodetype == LYS_LIST);
4160 if (pred_iter < ((struct lys_node_list *)node_match->node[j]->schema)->keys_size) {
4161 /* not enough predicates, just remove the list instance */
4162 unres_data_del(node_match, j);
Michal Vasko045182c2016-09-09 12:44:07 +02004163 } else {
4164 ++j;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004165 }
4166 }
4167
4168 if (!node_match->count) {
4169 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing some list keys.");
4170 }
4171 }
4172
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004173 return parsed;
4174}
4175
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004176int
4177lys_check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004178{
4179 struct lys_node *parent, *elem;
4180 struct lyxp_set set;
4181 uint32_t i;
4182 int rc;
4183
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004184 if (check_place) {
4185 parent = node;
4186 while (parent) {
4187 if (parent->nodetype == LYS_GROUPING) {
4188 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004189 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004190 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004191 if (parent->nodetype == LYS_AUGMENT) {
4192 if (!((struct lys_node_augment *)parent)->target) {
Radek Krejcidf46e222016-11-08 11:57:37 +01004193 /* unresolved augment */
4194 if (parent->module->implemented) {
4195 /* skip for now (will be checked later) */
4196 return EXIT_FAILURE;
4197 } else {
4198 /* not implemented augment, skip resolving */
4199 return EXIT_SUCCESS;
4200 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004201 } else {
4202 parent = ((struct lys_node_augment *)parent)->target;
4203 continue;
4204 }
4205 }
4206 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004207 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004208 }
4209
4210 rc = lyxp_node_atomize(node, &set);
4211 if (rc) {
4212 return rc;
4213 }
4214
4215 for (parent = node; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); parent = lys_parent(parent));
4216
4217 for (i = 0; i < set.used; ++i) {
4218 /* skip roots'n'stuff */
4219 if (set.val.snodes[i].type == LYXP_NODE_ELEM) {
4220 /* XPath expression cannot reference "lower" status than the node that has the definition */
4221 if (lyp_check_status(node->flags, lys_node_module(node), node->name, set.val.snodes[i].snode->flags,
4222 lys_node_module(set.val.snodes[i].snode), set.val.snodes[i].snode->name, node)) {
4223 return -1;
4224 }
4225
4226 if (parent) {
4227 for (elem = set.val.snodes[i].snode; elem && (elem != parent); elem = lys_parent(elem));
4228 if (!elem) {
4229 /* not in node's RPC or notification subtree, set the flag */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01004230 node->flags |= LYS_XPATH_DEP;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004231 break;
4232 }
4233 }
4234 }
4235 }
4236
4237 free(set.val.snodes);
4238 return EXIT_SUCCESS;
4239}
4240
Radek Krejcif71f48f2016-10-25 16:37:24 +02004241static int
4242check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4243{
4244 int i;
4245
4246 if (type->base == LY_TYPE_LEAFREF) {
4247 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && (type->info.lref.target->flags & LYS_CONFIG_R)) {
4248 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaf, "The %s is config but refers to a non-config %s.",
4249 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4250 return -1;
4251 }
4252 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4253 * of leafref resolving (lys_leaf_add_leafref_target()) */
4254 } else if (type->base == LY_TYPE_UNION) {
4255 for (i = 0; i < type->info.uni.count; i++) {
4256 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4257 return -1;
4258 }
4259 }
4260 }
4261 return 0;
4262}
4263
Michal Vasko9e635ac2016-10-17 11:44:09 +02004264/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004265 * @brief Passes config flag down to children, skips nodes without config flags.
4266 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004267 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004268 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004269 * @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 +02004270 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004271 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004272 *
4273 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004274 */
Michal Vaskoa86508c2016-08-26 14:30:19 +02004275static int
Radek Krejcib3142312016-11-09 11:04:12 +01004276inherit_config_flag(struct lys_node *node, int flags, int clear, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004277{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004278 struct lys_node_leaf *leaf;
4279
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004280 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Radek Krejci1d82ef62015-08-07 14:44:40 +02004281 LY_TREE_FOR(node, node) {
Radek Krejcib3142312016-11-09 11:04:12 +01004282 if (lys_has_xpath(node) && unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
Michal Vasko9e635ac2016-10-17 11:44:09 +02004283 return -1;
4284 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004285 if (clear) {
4286 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004287 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004288 } else {
4289 if (node->flags & LYS_CONFIG_SET) {
4290 /* skip nodes with an explicit config value */
4291 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4292 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01004293 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004294 return -1;
4295 }
4296 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004297 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004298
4299 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4300 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4301 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004302 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4303 && !((struct lys_node_list *)node)->keys_size) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004304 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
4305 return -1;
4306 }
4307 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004308 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004309 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Radek Krejcib3142312016-11-09 11:04:12 +01004310 if (inherit_config_flag(node->child, flags, clear, unres)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004311 return -1;
4312 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004313 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4314 leaf = (struct lys_node_leaf *)node;
4315 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004316 return -1;
4317 }
4318 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004319 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004320
4321 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004322}
4323
Michal Vasko730dfdf2015-08-11 14:48:05 +02004324/**
Michal Vasko7178e692016-02-12 15:58:05 +01004325 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004326 *
Michal Vaskobb211122015-08-19 14:03:11 +02004327 * @param[in] aug Augment to use.
Michal Vasko3edeaf72016-02-11 13:17:43 +01004328 * @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 +01004329 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004330 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004331 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004332 */
Michal Vasko7178e692016-02-12 15:58:05 +01004333static int
Radek Krejcib3142312016-11-09 11:04:12 +01004334resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004335{
Michal Vaskoe022a562016-09-27 14:24:15 +02004336 int rc, clear_config;
Radek Krejci80056d52017-01-05 13:13:33 +01004337 unsigned int u;
Michal Vasko1d87a922015-08-21 12:57:16 +02004338 struct lys_node *sub;
Pavol Vican47319932016-08-29 09:14:47 +02004339 const struct lys_node *aug_target, *parent;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004340 struct lys_module *mod;
Radek Krejci80056d52017-01-05 13:13:33 +01004341 struct lys_ext_instance *ext;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004342
Michal Vasko15b36692016-08-26 15:29:54 +02004343 assert(aug && !aug->target);
Radek Krejcidf46e222016-11-08 11:57:37 +01004344 mod = lys_main_module(aug->module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004345
Michal Vasko15b36692016-08-26 15:29:54 +02004346 /* resolve target node */
Radek Krejcidf46e222016-11-08 11:57:37 +01004347 rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), mod->implemented, &aug_target);
Michal Vasko15b36692016-08-26 15:29:54 +02004348 if (rc == -1) {
4349 return -1;
Radek Krejci5fb4c382016-11-28 09:21:42 +01004350 } else if (rc > 0) {
Michal Vasko15b36692016-08-26 15:29:54 +02004351 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]);
4352 return -1;
Radek Krejci5fb4c382016-11-28 09:21:42 +01004353 } else if (rc == 0 && aug->target) {
4354 /* augment was resolved as a side effect of setting module implemented when
4355 * resolving augment schema nodeid, so we are done here */
4356 return 0;
Michal Vasko15b36692016-08-26 15:29:54 +02004357 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004358 if (!aug_target && mod->implemented) {
Michal Vasko15b36692016-08-26 15:29:54 +02004359 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
4360 return EXIT_FAILURE;
4361 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004362 /* check that we want to connect augment into its target */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004363 if (!mod->implemented) {
4364 /* it must be augment only to the same module,
4365 * otherwise we do not apply augment in not-implemented
4366 * module. If the module is set to be implemented in future,
4367 * the augment is being resolved and checked again */
Radek Krejcidf46e222016-11-08 11:57:37 +01004368 if (!aug_target) {
4369 /* target was not even resolved */
4370 return EXIT_SUCCESS;
4371 }
4372 /* target was resolved, but it may refer another module */
4373 for (sub = (struct lys_node *)aug_target; sub; sub = lys_parent(sub)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004374 if (lys_node_module(sub) != mod) {
4375 /* this is not an implemented module and the augment
4376 * target some other module, so avoid its connecting
4377 * to the target */
4378 return EXIT_SUCCESS;
4379 }
4380 }
4381 }
4382
Michal Vasko15b36692016-08-26 15:29:54 +02004383 if (!aug->child) {
4384 /* nothing to do */
4385 LOGWRN("Augment \"%s\" without children.", aug->target_name);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004386 goto success;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004387 }
4388
Michal Vaskod58d5962016-03-02 14:29:41 +01004389 /* check for mandatory nodes - if the target node is in another module
4390 * the added nodes cannot be mandatory
4391 */
Michal Vasko15b36692016-08-26 15:29:54 +02004392 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug_target))
Radek Krejcidf24cbe2016-11-08 11:55:51 +01004393 && (rc = lyp_check_mandatory_augment(aug, aug_target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004394 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004395 }
4396
Michal Vasko07e89ef2016-03-03 13:28:57 +01004397 /* check augment target type and then augment nodes type */
Michal Vasko15b36692016-08-26 15:29:54 +02004398 if (aug_target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004399 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004400 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004401 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004402 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko15b36692016-08-26 15:29:54 +02004403 strnodetype(aug_target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004404 return -1;
4405 }
4406 }
Michal Vasko15b36692016-08-26 15:29:54 +02004407 } else if (aug_target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004408 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004409 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004410 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
Michal Vasko51e5c582017-01-19 14:16:39 +01004411 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko15b36692016-08-26 15:29:54 +02004412 strnodetype(aug_target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004413 return -1;
4414 }
4415 }
4416 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004417 LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
Michal Vasko51e5c582017-01-19 14:16:39 +01004418 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug_target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004419 return -1;
4420 }
4421
Radek Krejcic071c542016-01-27 14:57:51 +01004422 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004423 LY_TREE_FOR(aug->child, sub) {
Michal Vasko15b36692016-08-26 15:29:54 +02004424 if (lys_check_id(sub, (struct lys_node *)aug_target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004425 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004426 }
4427 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004428
Michal Vasko15b36692016-08-26 15:29:54 +02004429 /* finally reconnect augmenting data into the target - add them to the target child list,
4430 * by setting aug->target we know the augment is fully resolved now */
4431 aug->target = (struct lys_node *)aug_target;
4432 if (aug->target->child) {
4433 sub = aug->target->child->prev; /* remember current target's last node */
4434 sub->next = aug->child; /* connect augmenting data after target's last node */
4435 aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */
4436 aug->child->prev = sub; /* finish connecting of both child lists */
4437 } else {
4438 aug->target->child = aug->child;
4439 }
4440
Michal Vasko9e635ac2016-10-17 11:44:09 +02004441 /* inherit config information from actual parent */
4442 for(parent = aug_target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
4443 clear_config = (parent) ? 1 : 0;
4444 LY_TREE_FOR(aug->child, sub) {
Radek Krejcib3142312016-11-09 11:04:12 +01004445 if (inherit_config_flag(sub, aug_target->flags & LYS_CONFIG_MASK, clear_config, unres)) {
Michal Vasko9e635ac2016-10-17 11:44:09 +02004446 return -1;
4447 }
4448 }
4449
Radek Krejci80056d52017-01-05 13:13:33 +01004450 /* inherit extensions if any */
4451 for (u = 0; u < aug->target->ext_size; u++) {
4452 ext = aug->target->ext[u]; /* shortcut */
4453 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
4454 unres_schema_add_node(mod, unres, &ext, UNRES_EXT_FINALIZE, NULL);
4455 }
4456 }
4457
Radek Krejci27fe55e2016-09-13 17:13:35 +02004458success:
4459 if (mod->implemented) {
4460 /* make target modules also implemented */
4461 for (sub = aug->target; sub; sub = lys_parent(sub)) {
4462 if (lys_set_implemented(sub->module)) {
4463 return -1;
4464 }
4465 }
4466 }
4467
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004468 return EXIT_SUCCESS;
4469}
4470
Radek Krejcie534c132016-11-23 13:32:31 +01004471static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004472resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004473{
4474 enum LY_VLOG_ELEM vlog_type;
4475 void *vlog_node;
4476 unsigned int i, j;
4477 int c_ext = -1, rc;
4478 struct lys_ext *e;
4479 const char *value;
4480 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004481 const struct lys_module *mod;
Radek Krejcie534c132016-11-23 13:32:31 +01004482
4483 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004484 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004485 vlog_node = info->parent;
4486 vlog_type = LY_VLOG_LYS;
4487 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004488 case LYEXT_PAR_MODULE:
4489 case LYEXT_PAR_IMPORT:
4490 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004491 vlog_node = NULL;
4492 vlog_type = LY_VLOG_LYS;
4493 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004494 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004495 vlog_node = NULL;
4496 vlog_node = LY_VLOG_NONE;
4497 break;
4498 }
4499
4500 if (info->datatype == LYS_IN_YIN) {
4501 /* get the module where the extension is supposed to be defined */
Radek Krejcia7db9702017-01-20 12:55:14 +01004502 mod = lys_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004503 if (!mod) {
4504 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004505 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004506 }
4507
4508 /* find the extension definition */
4509 e = NULL;
4510 for (i = 0; i < mod->extensions_size; i++) {
4511 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4512 e = &mod->extensions[i];
4513 break;
4514 }
4515 }
4516 /* try submodules */
4517 for (j = 0; !e && j < mod->inc_size; j++) {
4518 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4519 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4520 e = &mod->inc[j].submodule->extensions[i];
4521 break;
4522 }
4523 }
4524 }
4525 if (!e) {
4526 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
4527 return EXIT_FAILURE;
4528 }
4529
4530 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004531
Radek Krejci72b35992017-01-04 16:27:44 +01004532 if (e->plugin && e->plugin->check_position) {
4533 /* common part - we have plugin with position checking function, use it first */
4534 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4535 /* extension is not allowed here */
4536 LOGVAL(LYE_INSTMT, vlog_type, vlog_node, e->name);
4537 return -1;
4538 }
4539 }
4540
4541 /* common part, even if there is no plugin */
4542 if (e->flags & LYS_YINELEM) {
4543 value = NULL;
4544 c_ext = 0;
4545 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4546 if (!yin->ns) {
4547 /* garbage */
4548 lyxml_free(mod->ctx, yin);
4549 continue;
4550 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4551 /* standard YANG statements are not expected here */
4552 LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
4553 return -1;
4554 } else if (yin->ns == info->data.yin->ns && ly_strequal(yin->name, e->argument, 1)) {
4555 /* we have the extension's argument */
4556 if (value) {
4557 LOGVAL(LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004558 return -1;
4559 }
Radek Krejci72b35992017-01-04 16:27:44 +01004560 value = yin->content;
4561 yin->content = NULL;
4562 lyxml_free(mod->ctx, yin);
4563 } else {
4564 /* possible extension instance, processed later */
4565 c_ext++;
4566 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004567 }
4568 }
Radek Krejci72b35992017-01-04 16:27:44 +01004569 if (!value) {
4570 LOGVAL(LYE_MISSCHILDSTMT, vlog_type, vlog_node, e->argument, info->data.yin->name);
4571 return -1;
4572 }
4573 } else if (e->argument) {
4574 value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4575 if (!value) {
4576 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
4577 return -1;
4578 }
4579 value = lydict_insert(mod->ctx, value, 0);
4580 } else {
4581 value = NULL;
4582 }
4583 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4584 ((struct lys_ext_instance *)(*ext))->def = e;
4585 ((struct lys_ext_instance *)(*ext))->arg_value = value;
4586 ((struct lys_ext_instance *)(*ext))->parent = info->parent;
4587 ((struct lys_ext_instance *)(*ext))->parent_type = info->parent_type;
4588 ((struct lys_ext_instance *)(*ext))->substmt = info->substmt;
4589 ((struct lys_ext_instance *)(*ext))->substmt_index = info->substmt_index;
4590
4591 /* extension instances in extension */
4592 if (c_ext == -1) {
4593 c_ext = 0;
4594 /* first, we have to count them */
4595 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4596 if (!yin->ns) {
4597 /* garbage */
4598 lyxml_free(mod->ctx, yin);
4599 continue;
4600 } else if (strcmp(yin->ns->value, LY_NSYIN)) {
4601 /* possible extension instance */
4602 c_ext++;
4603 } else {
4604 /* unexpected statement */
4605 LOGVAL(LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004606 return -1;
4607 }
Radek Krejci72b35992017-01-04 16:27:44 +01004608 }
4609 }
4610 if (c_ext) {
4611 (*ext)->ext = calloc(c_ext, sizeof *(*ext)->ext);
4612 if (!(*ext)->ext) {
4613 LOGMEM;
4614 return -1;
4615 }
4616 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01004617 rc = lyp_yin_fill_ext(*ext, LYEXT_PAR_EXTINST, LYEXT_SUBSTMT_SELF, 0, (struct lys_module *)mod, yin,
Radek Krejci2b999ac2017-01-18 16:22:12 +01004618 &(*ext)->ext, (*ext)->ext_size, unres);
Radek Krejci72b35992017-01-04 16:27:44 +01004619 (*ext)->ext_size++;
4620 if (rc == -1) {
4621 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004622 }
4623 }
Radek Krejcie534c132016-11-23 13:32:31 +01004624 }
Radek Krejci72b35992017-01-04 16:27:44 +01004625
4626 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4627
Radek Krejcie534c132016-11-23 13:32:31 +01004628 } else {
4629 /* TODO YANG */
4630
4631 return -1;
4632 }
4633
4634 return EXIT_SUCCESS;
4635}
4636
Michal Vasko730dfdf2015-08-11 14:48:05 +02004637/**
Pavol Vican855ca622016-09-05 13:07:54 +02004638 * @brief Resolve (find) choice default case. Does not log.
4639 *
4640 * @param[in] choic Choice to use.
4641 * @param[in] dflt Name of the default case.
4642 *
4643 * @return Pointer to the default node or NULL.
4644 */
4645static struct lys_node *
4646resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4647{
4648 struct lys_node *child, *ret;
4649
4650 LY_TREE_FOR(choic->child, child) {
4651 if (child->nodetype == LYS_USES) {
4652 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4653 if (ret) {
4654 return ret;
4655 }
4656 }
4657
4658 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02004659 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02004660 return child;
4661 }
4662 }
4663
4664 return NULL;
4665}
4666
4667/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02004668 * @brief Resolve uses, apply augments, refines. Logs directly.
4669 *
Michal Vaskobb211122015-08-19 14:03:11 +02004670 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004671 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004672 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004673 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004674 */
Michal Vasko184521f2015-09-24 13:14:26 +02004675static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004676resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004677{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004678 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02004679 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02004680 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02004681 struct lys_node_leaflist *llist;
4682 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02004683 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004684 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004685 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004686 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004687 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004688 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004689
Michal Vasko71e1aa82015-08-12 12:17:51 +02004690 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01004691
4692 /* HACK just check that the grouping is resolved
4693 * - the higher byte in flags is always empty in grouping (no flags there apply to the groupings)
4694 * so we use it to count unresolved uses inside the grouping */
4695#if __BYTE_ORDER == __LITTLE_ENDIAN
4696 assert(!((uint8_t*)&uses->grp->flags)[1]);
4697#else
4698 assert(!((uint8_t*)&uses->grp->flags)[0]);
4699#endif
Michal Vasko71e1aa82015-08-12 12:17:51 +02004700
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004701 if (!uses->grp->child) {
4702 /* grouping without children, warning was already displayed */
4703 return EXIT_SUCCESS;
4704 }
4705
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004706 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004707 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01004708 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004709 if (!node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004710 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
Michal Vasko51e5c582017-01-19 14:16:39 +01004711 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004712 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004713 }
Pavol Vican55abd332016-07-12 15:54:49 +02004714 /* test the name of siblings */
4715 LY_TREE_FOR((uses->parent) ? uses->parent->child : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02004716 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004717 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004718 }
4719 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004720 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004721
Michal Vaskodef0db12015-10-07 13:22:48 +02004722 /* we managed to copy the grouping, the rest must be possible to resolve */
4723
Pavol Vican855ca622016-09-05 13:07:54 +02004724 if (uses->refine_size) {
4725 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
4726 if (!refine_nodes) {
4727 LOGMEM;
4728 goto fail;
4729 }
4730 }
4731
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004732 /* apply refines */
4733 for (i = 0; i < uses->refine_size; i++) {
4734 rfn = &uses->refine[i];
Michal Vasko3edeaf72016-02-11 13:17:43 +01004735 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE,
Radek Krejcif3c71de2016-04-11 12:45:46 +02004736 1, 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004737 if (rc || !node) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004738 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004739 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004740 }
4741
Radek Krejci1d82ef62015-08-07 14:44:40 +02004742 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004743 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004744 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004745 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004746 }
Pavol Vican855ca622016-09-05 13:07:54 +02004747 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004748
4749 /* description on any nodetype */
4750 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004751 lydict_remove(ctx, node->dsc);
4752 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004753 }
4754
4755 /* reference on any nodetype */
4756 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004757 lydict_remove(ctx, node->ref);
4758 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004759 }
4760
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004761 /* config on any nodetype,
4762 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
4763 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004764 node->flags &= ~LYS_CONFIG_MASK;
4765 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004766 }
4767
4768 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02004769 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004770 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004771 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02004772 leaf = (struct lys_node_leaf *)node;
4773
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004774 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02004775 lydict_remove(ctx, leaf->dflt);
4776 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
4777
4778 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01004779 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
4780 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004781 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004782 }
Radek Krejci200bf712016-08-16 17:11:04 +02004783 } else if (node->nodetype == LYS_LEAFLIST) {
4784 /* leaf-list */
4785 llist = (struct lys_node_leaflist *)node;
4786
4787 /* remove complete set of defaults in target */
4788 for (i = 0; i < llist->dflt_size; i++) {
4789 lydict_remove(ctx, llist->dflt[i]);
4790 }
4791 free(llist->dflt);
4792
4793 /* copy the default set from refine */
4794 llist->dflt_size = rfn->dflt_size;
4795 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
4796 for (i = 0; i < llist->dflt_size; i++) {
4797 llist->dflt[i] = lydict_insert(ctx, rfn->dflt[i], 0);
4798 }
4799
4800 /* check default value */
4801 for (i = 0; i < llist->dflt_size; i++) {
Radek Krejci51673202016-11-01 17:00:32 +01004802 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
4803 (struct lys_node *)(&llist->dflt[i])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02004804 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02004805 }
4806 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004807 }
4808 }
4809
4810 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004811 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004812 if (node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004813 /* remove current value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004814 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004815
4816 /* set new value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004817 node->flags |= (rfn->flags & LYS_MAND_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004818 }
Pavol Vican855ca622016-09-05 13:07:54 +02004819 if (rfn->flags & LYS_MAND_TRUE) {
4820 /* check if node has default value */
4821 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
4822 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\".");
4823 goto fail;
4824 }
4825 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
4826 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\".");
4827 goto fail;
4828 }
4829 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004830 }
4831
4832 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004833 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4834 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4835 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004836 }
4837
4838 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004839 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004840 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004841 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004842 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004843 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004844 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004845 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004846 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004847 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004848 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004849 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004850 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004851 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004852 }
4853 }
4854
4855 /* must in leaf, leaf-list, list, container or anyxml */
4856 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004857 switch (node->nodetype) {
4858 case LYS_LEAF:
4859 old_size = &((struct lys_node_leaf *)node)->must_size;
4860 old_must = &((struct lys_node_leaf *)node)->must;
4861 break;
4862 case LYS_LEAFLIST:
4863 old_size = &((struct lys_node_leaflist *)node)->must_size;
4864 old_must = &((struct lys_node_leaflist *)node)->must;
4865 break;
4866 case LYS_LIST:
4867 old_size = &((struct lys_node_list *)node)->must_size;
4868 old_must = &((struct lys_node_list *)node)->must;
4869 break;
4870 case LYS_CONTAINER:
4871 old_size = &((struct lys_node_container *)node)->must_size;
4872 old_must = &((struct lys_node_container *)node)->must;
4873 break;
4874 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004875 case LYS_ANYDATA:
4876 old_size = &((struct lys_node_anydata *)node)->must_size;
4877 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004878 break;
4879 default:
4880 LOGINT;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004881 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004882 }
4883
4884 size = *old_size + rfn->must_size;
4885 must = realloc(*old_must, size * sizeof *rfn->must);
4886 if (!must) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004887 LOGMEM;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004888 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004889 }
Pavol Vican855ca622016-09-05 13:07:54 +02004890 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01004891 must[j].ext_size = rfn[k].ext_size;
4892 lys_extension_instances_dup(rfn->must[k].ext, rfn->must[k].ext_size, &must[j].ext);
Pavol Vican855ca622016-09-05 13:07:54 +02004893 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
4894 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
4895 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
4896 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
4897 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004898 }
4899
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004900 *old_must = must;
4901 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02004902
4903 /* check XPath dependencies again */
4904 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
4905 goto fail;
4906 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004907 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02004908
4909 /* if-feature in leaf, leaf-list, list, container or anyxml */
4910 if (rfn->iffeature_size) {
4911 old_size = &node->iffeature_size;
4912 old_iff = &node->iffeature;
4913
4914 size = *old_size + rfn->iffeature_size;
4915 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
4916 if (!iff) {
4917 LOGMEM;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004918 goto fail;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004919 }
Pavol Vican855ca622016-09-05 13:07:54 +02004920 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
4921 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004922 if (usize1) {
4923 /* there is something to duplicate */
4924 /* duplicate compiled expression */
4925 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
4926 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Pavol Vican855ca622016-09-05 13:07:54 +02004927 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004928
4929 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02004930 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
4931 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004932 }
4933 }
4934
4935 *old_iff = iff;
4936 *old_size = size;
4937 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004938 }
4939
4940 /* apply augments */
4941 for (i = 0; i < uses->augment_size; i++) {
Radek Krejcib3142312016-11-09 11:04:12 +01004942 rc = resolve_augment(&uses->augment[i], uses->child, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004943 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004944 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004945 }
4946 }
4947
Pavol Vican855ca622016-09-05 13:07:54 +02004948 /* check refines */
4949 for (i = 0; i < uses->refine_size; i++) {
4950 node = refine_nodes[i];
4951 rfn = &uses->refine[i];
4952
4953 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004954 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02004955 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01004956 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02004957 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
4958 (rfn->flags & LYS_CONFIG_W)) {
4959 /* setting config true under config false is prohibited */
4960 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004961 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004962 "changing config from 'false' to 'true' is prohibited while "
4963 "the target's parent is still config 'false'.");
4964 goto fail;
4965 }
4966
4967 /* inherit config change to the target children */
4968 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4969 if (rfn->flags & LYS_CONFIG_W) {
4970 if (iter->flags & LYS_CONFIG_SET) {
4971 /* config is set explicitely, go to next sibling */
4972 next = NULL;
4973 goto nextsibling;
4974 }
4975 } else { /* LYS_CONFIG_R */
4976 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4977 /* error - we would have config data under status data */
4978 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
Michal Vasko51e5c582017-01-19 14:16:39 +01004979 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004980 "changing config from 'true' to 'false' is prohibited while the target "
4981 "has still a children with explicit config 'true'.");
4982 goto fail;
4983 }
4984 }
4985 /* change config */
4986 iter->flags &= ~LYS_CONFIG_MASK;
4987 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4988
4989 /* select next iter - modified LY_TREE_DFS_END */
4990 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4991 next = NULL;
4992 } else {
4993 next = iter->child;
4994 }
4995nextsibling:
4996 if (!next) {
4997 /* try siblings */
4998 next = iter->next;
4999 }
5000 while (!next) {
5001 /* parent is already processed, go to its sibling */
5002 iter = lys_parent(iter);
5003
5004 /* no siblings, go back through parents */
5005 if (iter == node) {
5006 /* we are done, no next element to process */
5007 break;
5008 }
5009 next = iter->next;
5010 }
5011 }
5012 }
5013
5014 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005015 if (rfn->dflt_size) {
5016 if (node->nodetype == LYS_CHOICE) {
5017 /* choice */
5018 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5019 rfn->dflt[0]);
5020 if (!((struct lys_node_choice *)node)->dflt) {
5021 LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
5022 goto fail;
5023 }
5024 if (lyp_check_mandatory_choice(node)) {
5025 goto fail;
5026 }
Pavol Vican855ca622016-09-05 13:07:54 +02005027 }
5028 }
5029
5030 /* min/max-elements on list or leaf-list */
5031 if (node->nodetype == LYS_LIST) {
5032 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
5033 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5034 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
5035 goto fail;
5036 }
5037 } else if (node->nodetype == LYS_LEAFLIST) {
5038 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
5039 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5040 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
5041 goto fail;
5042 }
5043 }
5044
5045 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005046 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005047 if (node->nodetype == LYS_LEAFLIST) {
5048 llist = (struct lys_node_leaflist *)node;
5049 if (llist->dflt_size && llist->min) {
5050 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "min-elements", "refine");
5051 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
5052 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5053 goto fail;
5054 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005055 } else if (node->nodetype == LYS_LEAF) {
5056 leaf = (struct lys_node_leaf *)node;
5057 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
5058 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, rfn->dflt_size ? "default" : "mandatory", "refine");
5059 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
5060 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5061 goto fail;
5062 }
Pavol Vican855ca622016-09-05 13:07:54 +02005063 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005064
Pavol Vican855ca622016-09-05 13:07:54 +02005065 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005066 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005067 for (parent = node->parent;
5068 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5069 parent = parent->parent) {
5070 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5071 /* stop also on presence containers */
5072 break;
5073 }
5074 }
5075 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5076 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5077 if (lyp_check_mandatory_choice(parent)) {
5078 goto fail;
5079 }
5080 }
5081 }
5082 }
5083 free(refine_nodes);
5084
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005085 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005086
5087fail:
5088 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5089 lys_node_free(iter, NULL, 0);
5090 }
Pavol Vican855ca622016-09-05 13:07:54 +02005091 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005092 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005093}
5094
Radek Krejci018f1f52016-08-03 16:01:20 +02005095static int
5096identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
5097{
5098 int i;
5099
5100 assert(der && base);
5101
Radek Krejci018f1f52016-08-03 16:01:20 +02005102 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005103 /* create a set for backlinks if it does not exist */
5104 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005105 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005106 /* store backlink */
5107 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005108
Radek Krejci85a54be2016-10-20 12:39:56 +02005109 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005110 for (i = 0; i < base->base_size; i++) {
5111 if (identity_backlink_update(der, base->base[i])) {
5112 return EXIT_FAILURE;
5113 }
5114 }
5115
5116 return EXIT_SUCCESS;
5117}
5118
Michal Vasko730dfdf2015-08-11 14:48:05 +02005119/**
5120 * @brief Resolve base identity recursively. Does not log.
5121 *
5122 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005123 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005124 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005125 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005126 *
Radek Krejci219fa612016-08-15 10:36:51 +02005127 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005128 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005129static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005130resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005131 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005132{
Michal Vaskof02e3742015-08-05 16:27:02 +02005133 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005134 struct lys_ident *base = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005135
Radek Krejcicf509982015-12-15 09:22:44 +01005136 assert(ret);
5137
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005138 /* search module */
5139 for (i = 0; i < module->ident_size; i++) {
5140 if (!strcmp(basename, module->ident[i].name)) {
5141
5142 if (!ident) {
5143 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005144 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005145 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005146 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005147 }
5148
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005149 base = &module->ident[i];
5150 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005151 }
5152 }
5153
5154 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005155 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5156 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5157 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005158
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005159 if (!ident) {
5160 *ret = &module->inc[j].submodule->ident[i];
5161 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005162 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005163
5164 base = &module->inc[j].submodule->ident[i];
5165 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005166 }
5167 }
5168 }
5169
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005170matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005171 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005172 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005173 /* is it already completely resolved? */
5174 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005175 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005176 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5177
5178 /* simple check for circular reference,
5179 * the complete check is done as a side effect of using only completely
5180 * resolved identities (previous check of unres content) */
5181 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
5182 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5183 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005184 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005185 }
5186
Radek Krejci06f64ed2016-08-15 11:07:44 +02005187 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005188 }
5189 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005190
Radek Krejcibabbff82016-02-19 13:31:37 +01005191 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005192 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005193 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005194 }
5195
Radek Krejci219fa612016-08-15 10:36:51 +02005196 /* base not found (maybe a forward reference) */
5197 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005198}
5199
Michal Vasko730dfdf2015-08-11 14:48:05 +02005200/**
5201 * @brief Resolve base identity. Logs directly.
5202 *
5203 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005204 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005205 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005206 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005207 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005208 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005209 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005210 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005211static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005212resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005213 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005214{
5215 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005216 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005217 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005218 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005219 struct lys_module *mod;
5220
5221 assert((ident && !type) || (!ident && type));
5222
5223 if (!type) {
5224 /* have ident to resolve */
5225 ret = &target;
5226 flags = ident->flags;
5227 mod = ident->module;
5228 } else {
5229 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005230 ++type->info.ident.count;
5231 type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref);
5232 if (!type->info.ident.ref) {
5233 LOGMEM;
5234 return -1;
5235 }
5236
5237 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005238 flags = type->parent->flags;
5239 mod = type->parent->module;
5240 }
Michal Vaskof2006002016-04-21 16:28:15 +02005241 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005242
5243 /* search for the base identity */
5244 name = strchr(basename, ':');
5245 if (name) {
5246 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005247 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005248 name++;
5249
Michal Vasko2d851a92015-10-20 16:16:36 +02005250 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005251 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005252 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005253 }
5254 } else {
5255 name = basename;
5256 }
5257
Radek Krejcic071c542016-01-27 14:57:51 +01005258 /* get module where to search */
5259 module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len);
5260 if (!module) {
5261 /* identity refers unknown data model */
Radek Krejci02a04992016-03-17 16:06:37 +01005262 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005263 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005264 }
5265
Radek Krejcic071c542016-01-27 14:57:51 +01005266 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005267 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5268 if (!rc) {
5269 assert(*ret);
5270
5271 /* check status */
5272 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5273 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5274 rc = -1;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005275 } else {
5276 if (ident) {
5277 ident->base[ident->base_size++] = *ret;
5278
5279 /* maintain backlinks to the derived identities */
5280 rc = identity_backlink_update(ident, *ret) ? -1 : EXIT_SUCCESS;
5281 }
Radek Krejci219fa612016-08-15 10:36:51 +02005282 }
5283 } else if (rc == EXIT_FAILURE) {
5284 LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005285 if (type) {
5286 --type->info.ident.count;
5287 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005288 }
5289
Radek Krejci219fa612016-08-15 10:36:51 +02005290 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005291}
5292
Michal Vasko730dfdf2015-08-11 14:48:05 +02005293/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005294 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005295 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005296 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005297 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005298 * @param[in] node Node where the identityref is being resolved
Michal Vasko730dfdf2015-08-11 14:48:05 +02005299 *
5300 * @return Pointer to the identity resolvent, NULL on error.
5301 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005302struct lys_ident *
Michal Vaskof2d43962016-09-02 11:10:16 +02005303resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005304{
Radek Krejci639491e2016-12-05 13:30:42 +01005305 const char *mod_name, *name, *mod_name_iter;
Radek Krejci85a54be2016-10-20 12:39:56 +02005306 int mod_name_len, rc, i;
5307 unsigned int u;
Michal Vaskof2d43962016-09-02 11:10:16 +02005308 struct lys_ident *der, *cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005309
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005310 assert(type && ident_name && node);
5311
Michal Vaskof2d43962016-09-02 11:10:16 +02005312 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005313 return NULL;
5314 }
5315
Michal Vaskoc633ca02015-08-21 14:03:51 +02005316 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005317 if (rc < 1) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005318 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005319 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005320 } else if (rc < (signed)strlen(ident_name)) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005321 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005322 return NULL;
5323 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005324 if (!mod_name) {
5325 /* no prefix, identity must be defined in the same module as node */
Radek Krejci639491e2016-12-05 13:30:42 +01005326 mod_name = lys_main_module(node->schema->module)->name;
Radek Krejcif32c5f62016-12-05 09:27:38 +01005327 mod_name_len = strlen(mod_name);
5328 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005329
Michal Vaskof2d43962016-09-02 11:10:16 +02005330 /* go through all the bases in all the derived types */
5331 while (type->der) {
5332 for (i = 0; i < type->info.ident.count; ++i) {
5333 cur = type->info.ident.ref[i];
Radek Krejci639491e2016-12-05 13:30:42 +01005334 mod_name_iter = lys_main_module(cur->module)->name;
Radek Krejcif32c5f62016-12-05 09:27:38 +01005335 if (!strcmp(cur->name, name) &&
Radek Krejci639491e2016-12-05 13:30:42 +01005336 !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) {
Michal Vaskof2d43962016-09-02 11:10:16 +02005337 goto match;
5338 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005339
Radek Krejci85a54be2016-10-20 12:39:56 +02005340 if (cur->der) {
5341 /* there are also some derived identities */
5342 for (u = 0; u < cur->der->number; u++) {
5343 der = (struct lys_ident *)cur->der->set.g[u]; /* shortcut */
Radek Krejci639491e2016-12-05 13:30:42 +01005344 mod_name_iter = lys_main_module(der->module)->name;
Radek Krejci85a54be2016-10-20 12:39:56 +02005345 if (!strcmp(der->name, name) &&
Radek Krejci639491e2016-12-05 13:30:42 +01005346 !strncmp(mod_name_iter, mod_name, mod_name_len) && !mod_name_iter[mod_name_len]) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005347 /* we have match */
5348 cur = der;
5349 goto match;
5350 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005351 }
5352 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005353 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005354 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005355 }
5356
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005357 LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005358 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005359
5360match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005361 for (i = 0; i < cur->iffeature_size; i++) {
5362 if (!resolve_iffeature(&cur->iffeature[i])) {
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005363 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01005364 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 +02005365 return NULL;
5366 }
5367 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005368 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005369}
5370
Michal Vasko730dfdf2015-08-11 14:48:05 +02005371/**
Michal Vaskobb211122015-08-19 14:03:11 +02005372 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005373 *
Michal Vaskobb211122015-08-19 14:03:11 +02005374 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005375 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005376 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005377 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005378 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005379static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005380resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005381{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005382 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005383 struct lys_node *par_grp;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005384
Radek Krejci6ff885d2017-01-03 14:06:22 +01005385 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used
5386 * in some uses. When we see such a uses, the grouping's higher byte of the flags member (not used in
5387 * grouping) is used to store number of so far unresolved uses. The grouping cannot be used unless this
5388 * counter is decreased back to 0. To remember that the uses already increased grouping's counter, the
Radek Krejci010e54b2016-03-15 09:40:34 +01005389 * LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005390 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 +02005391
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005392 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005393 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5394 if (rc == -1) {
Michal Vasko92981a62016-10-14 10:25:16 +02005395 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005396 return -1;
5397 } else if (rc > 0) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005398 LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005399 return -1;
5400 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005401 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci4372b4e2016-04-14 17:42:16 +02005402 /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field
5403 * (and smaller flags - it uses only a limited set of flags)
5404 */
Radek Krejci6ff885d2017-01-03 14:06:22 +01005405#if __BYTE_ORDER == __LITTLE_ENDIAN
5406 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++;
5407#else
5408 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++;
5409#endif
Radek Krejci010e54b2016-03-15 09:40:34 +01005410 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005411 }
Michal Vasko92981a62016-10-14 10:25:16 +02005412 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005413 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005414 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005415 }
5416
Radek Krejci6ff885d2017-01-03 14:06:22 +01005417#if __BYTE_ORDER == __LITTLE_ENDIAN
5418 if (((uint8_t*)&uses->grp->flags)[1]) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005419 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01005420 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++;
5421#else
5422 if (((uint8_t*)&uses->grp->flags)[0]) {
5423 if (par_grp && !(uses->flags & LYS_USESGRP)) {
5424 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++;
5425#endif
Radek Krejci010e54b2016-03-15 09:40:34 +01005426 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005427 } else {
5428 /* instantiate grouping only when it is completely resolved */
5429 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005430 }
Michal Vasko92981a62016-10-14 10:25:16 +02005431 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005432 return EXIT_FAILURE;
5433 }
5434
Radek Krejci48464ed2016-03-17 15:44:09 +01005435 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005436 if (!rc) {
5437 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005438 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01005439#if __BYTE_ORDER == __LITTLE_ENDIAN
5440 if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]) {;
5441#else
5442 if (!((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]) {;
5443#endif
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005444 LOGINT;
5445 return -1;
5446 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01005447#if __BYTE_ORDER == __LITTLE_ENDIAN
5448 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]--;
5449#else
5450 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]--;
5451#endif
Radek Krejci010e54b2016-03-15 09:40:34 +01005452 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005453 }
Radek Krejcicf509982015-12-15 09:22:44 +01005454
5455 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005456 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005457 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005458 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01005459 return -1;
5460 }
5461
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005462 return EXIT_SUCCESS;
5463 }
5464
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005465 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005466}
5467
Michal Vasko730dfdf2015-08-11 14:48:05 +02005468/**
Michal Vasko9957e592015-08-17 15:04:09 +02005469 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005470 *
Michal Vaskobb211122015-08-19 14:03:11 +02005471 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005472 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005473 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005474 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005475 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005476static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005477resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005478{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005479 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01005480 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005481
5482 for (i = 0; i < list->keys_size; ++i) {
Radek Krejci5c08a992016-11-02 13:30:04 +01005483 if (!list->child) {
5484 /* no child, possible forward reference */
5485 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
5486 return EXIT_FAILURE;
5487 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005488 /* get the key name */
5489 if ((value = strpbrk(keys_str, " \t\n"))) {
5490 len = value - keys_str;
5491 while (isspace(value[0])) {
5492 value++;
5493 }
5494 } else {
5495 len = strlen(keys_str);
5496 }
5497
Michal Vasko0f99d3e2017-01-10 10:50:40 +01005498 rc = lys_get_data_sibling(lys_node_module((struct lys_node *)list), list->child, keys_str, len, LYS_LEAF,
5499 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005500 if (rc) {
Michal Vasko7a55bea2016-05-02 14:51:20 +02005501 LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
5502 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005503 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005504
Radek Krejci48464ed2016-03-17 15:44:09 +01005505 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005506 /* check_key logs */
5507 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005508 }
5509
Radek Krejcicf509982015-12-15 09:22:44 +01005510 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005511 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005512 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
5513 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01005514 return -1;
5515 }
5516
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005517 /* prepare for next iteration */
5518 while (value && isspace(value[0])) {
5519 value++;
5520 }
5521 keys_str = value;
5522 }
5523
Michal Vaskof02e3742015-08-05 16:27:02 +02005524 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005525}
5526
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005527/**
Michal Vaskobf19d252015-10-08 15:39:17 +02005528 * @brief Resolve (check) all must conditions of \p node.
5529 * Logs directly.
5530 *
5531 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005532 * @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 +02005533 *
5534 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
5535 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005536static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005537resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02005538{
Michal Vasko3cfa3182017-01-17 10:00:58 +01005539 int node_flags;
Michal Vaskobf19d252015-10-08 15:39:17 +02005540 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005541 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02005542 struct lys_restr *must;
5543 struct lyxp_set set;
5544
5545 assert(node);
5546 memset(&set, 0, sizeof set);
5547
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005548 if (inout_parent) {
5549 for (schema = lys_parent(node->schema);
5550 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
5551 schema = lys_parent(schema));
5552 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
5553 LOGINT;
5554 return -1;
5555 }
5556 must_size = ((struct lys_node_inout *)schema)->must_size;
5557 must = ((struct lys_node_inout *)schema)->must;
5558
Michal Vasko3cfa3182017-01-17 10:00:58 +01005559 node_flags = schema->flags;
5560
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005561 /* context node is the RPC/action */
5562 node = node->parent;
5563 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
5564 LOGINT;
5565 return -1;
5566 }
5567 } else {
5568 switch (node->schema->nodetype) {
5569 case LYS_CONTAINER:
5570 must_size = ((struct lys_node_container *)node->schema)->must_size;
5571 must = ((struct lys_node_container *)node->schema)->must;
5572 break;
5573 case LYS_LEAF:
5574 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
5575 must = ((struct lys_node_leaf *)node->schema)->must;
5576 break;
5577 case LYS_LEAFLIST:
5578 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
5579 must = ((struct lys_node_leaflist *)node->schema)->must;
5580 break;
5581 case LYS_LIST:
5582 must_size = ((struct lys_node_list *)node->schema)->must_size;
5583 must = ((struct lys_node_list *)node->schema)->must;
5584 break;
5585 case LYS_ANYXML:
5586 case LYS_ANYDATA:
5587 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
5588 must = ((struct lys_node_anydata *)node->schema)->must;
5589 break;
5590 case LYS_NOTIF:
5591 must_size = ((struct lys_node_notif *)node->schema)->must_size;
5592 must = ((struct lys_node_notif *)node->schema)->must;
5593 break;
5594 default:
5595 must_size = 0;
5596 break;
5597 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01005598
5599 node_flags = node->schema->flags;
Michal Vaskobf19d252015-10-08 15:39:17 +02005600 }
5601
5602 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005603 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005604 return -1;
5605 }
5606
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005607 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02005608
Michal Vasko8146d4c2016-05-09 15:50:29 +02005609 if (!set.val.bool) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01005610 if ((ignore_fail == 1) || ((node_flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005611 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
5612 } else {
5613 LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
5614 if (must[i].emsg) {
Michal Vasko51e5c582017-01-19 14:16:39 +01005615 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005616 }
5617 if (must[i].eapptag) {
5618 strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1);
5619 }
5620 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02005621 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005622 }
5623 }
5624
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005625 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02005626}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005627
Michal Vaskobf19d252015-10-08 15:39:17 +02005628/**
Michal Vasko508a50d2016-09-07 14:50:33 +02005629 * @brief Resolve (find) when condition schema context node. Does not log.
5630 *
5631 * @param[in] schema Schema node with the when condition.
5632 * @param[out] ctx_snode When schema context node.
5633 * @param[out] ctx_snode_type Schema context node type.
5634 */
5635void
5636resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
5637{
5638 const struct lys_node *sparent;
5639
5640 /* find a not schema-only node */
5641 *ctx_snode_type = LYXP_NODE_ELEM;
5642 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
5643 if (schema->nodetype == LYS_AUGMENT) {
5644 sparent = ((struct lys_node_augment *)schema)->target;
5645 } else {
5646 sparent = schema->parent;
5647 }
5648 if (!sparent) {
5649 /* context node is the document root (fake root in our case) */
5650 if (schema->flags & LYS_CONFIG_W) {
5651 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
5652 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02005653 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02005654 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02005655 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskob94a5e42016-09-08 14:01:56 +02005656 schema = lys_getnext(NULL, NULL, lys_node_module(schema), 0);
Michal Vasko508a50d2016-09-07 14:50:33 +02005657 break;
5658 }
5659 schema = sparent;
5660 }
5661
5662 *ctx_snode = (struct lys_node *)schema;
5663}
5664
5665/**
Michal Vaskocf024702015-10-08 15:01:42 +02005666 * @brief Resolve (find) when condition context node. Does not log.
5667 *
5668 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02005669 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02005670 * @param[out] ctx_node Context node.
5671 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02005672 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02005673 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02005674 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02005675static int
5676resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
5677 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005678{
Michal Vaskocf024702015-10-08 15:01:42 +02005679 struct lyd_node *parent;
5680 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005681 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02005682 uint16_t i, data_depth, schema_depth;
5683
Michal Vasko508a50d2016-09-07 14:50:33 +02005684 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02005685
Michal Vaskofe989752016-09-08 08:47:26 +02005686 if (node_type == LYXP_NODE_ELEM) {
5687 /* standard element context node */
5688 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
5689 for (sparent = schema, schema_depth = 0;
5690 sparent;
5691 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
5692 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
5693 ++schema_depth;
5694 }
Michal Vaskocf024702015-10-08 15:01:42 +02005695 }
Michal Vaskofe989752016-09-08 08:47:26 +02005696 if (data_depth < schema_depth) {
5697 return -1;
5698 }
Michal Vaskocf024702015-10-08 15:01:42 +02005699
Michal Vasko956e8542016-08-26 09:43:35 +02005700 /* find the corresponding data node */
5701 for (i = 0; i < data_depth - schema_depth; ++i) {
5702 node = node->parent;
5703 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02005704 if (node->schema != schema) {
5705 return -1;
5706 }
Michal Vaskofe989752016-09-08 08:47:26 +02005707 } else {
5708 /* root context node */
5709 while (node->parent) {
5710 node = node->parent;
5711 }
5712 while (node->prev->next) {
5713 node = node->prev;
5714 }
Michal Vaskocf024702015-10-08 15:01:42 +02005715 }
5716
Michal Vaskoa59495d2016-08-22 09:18:58 +02005717 *ctx_node = node;
5718 *ctx_node_type = node_type;
5719 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02005720}
5721
Michal Vasko76c3bd32016-08-24 16:02:52 +02005722/**
5723 * @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 +01005724 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02005725 *
5726 * @param[in] snode Schema node, whose children instances need to be unlinked.
5727 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
5728 * it is moved to point to another sibling still in the original tree.
5729 * @param[in,out] ctx_node When context node, adjusted if needed.
5730 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
5731 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
5732 * Ordering may change, but there will be no semantic change.
5733 *
5734 * @return EXIT_SUCCESS on success, -1 on error.
5735 */
5736static int
5737resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
5738 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
5739{
5740 struct lyd_node *next, *elem;
5741
5742 switch (snode->nodetype) {
5743 case LYS_AUGMENT:
5744 case LYS_USES:
5745 case LYS_CHOICE:
5746 case LYS_CASE:
5747 LY_TREE_FOR(snode->child, snode) {
5748 if (resolve_when_unlink_nodes(snode, node, ctx_node, ctx_node_type, unlinked_nodes)) {
5749 return -1;
5750 }
5751 }
5752 break;
5753 case LYS_CONTAINER:
5754 case LYS_LIST:
5755 case LYS_LEAF:
5756 case LYS_LEAFLIST:
5757 case LYS_ANYXML:
5758 case LYS_ANYDATA:
5759 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
5760 if (elem->schema == snode) {
5761
5762 if (elem == *ctx_node) {
5763 /* We are going to unlink our context node! This normally cannot happen,
5764 * but we use normal top-level data nodes for faking a document root node,
5765 * so if this is the context node, we just use the next top-level node.
5766 * Additionally, it can even happen that there are no top-level data nodes left,
5767 * all were unlinked, so in this case we pass NULL as the context node/data tree,
5768 * lyxp_eval() can handle this special situation.
5769 */
5770 if (ctx_node_type == LYXP_NODE_ELEM) {
5771 LOGINT;
5772 return -1;
5773 }
5774
5775 if (elem->prev == elem) {
5776 /* unlinking last top-level element, use an empty data tree */
5777 *ctx_node = NULL;
5778 } else {
5779 /* in this case just use the previous/last top-level data node */
5780 *ctx_node = elem->prev;
5781 }
5782 } else if (elem == *node) {
5783 /* We are going to unlink the currently processed node. This does not matter that
5784 * much, but we would lose access to the original data tree, so just move our
5785 * pointer somewhere still inside it.
5786 */
5787 if ((*node)->prev != *node) {
5788 *node = (*node)->prev;
5789 } else {
5790 /* the processed node with sibings were all unlinked, oh well */
5791 *node = NULL;
5792 }
5793 }
5794
5795 /* temporarily unlink the node */
5796 lyd_unlink(elem);
5797 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005798 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005799 LOGINT;
5800 return -1;
5801 }
5802 } else {
5803 *unlinked_nodes = elem;
5804 }
5805
5806 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
5807 /* there can be only one instance */
5808 break;
5809 }
5810 }
5811 }
5812 break;
5813 default:
5814 LOGINT;
5815 return -1;
5816 }
5817
5818 return EXIT_SUCCESS;
5819}
5820
5821/**
5822 * @brief Relink the unlinked nodes back.
5823 *
5824 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
5825 * we simply need a sibling from the original data tree.
5826 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
5827 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
5828 * or the sibling of \p unlinked_nodes.
5829 *
5830 * @return EXIT_SUCCESS on success, -1 on error.
5831 */
5832static int
5833resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
5834{
5835 struct lyd_node *elem;
5836
5837 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vaskode1feeb2016-12-20 14:48:42 +01005838 lyd_unlink(elem);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005839 if (ctx_node_type == LYXP_NODE_ELEM) {
5840 if (lyd_insert(node, elem)) {
5841 return -1;
5842 }
5843 } else {
5844 if (lyd_insert_after(node, elem)) {
5845 return -1;
5846 }
5847 }
5848 }
5849
5850 return EXIT_SUCCESS;
5851}
5852
Radek Krejci03b71f72016-03-16 11:10:09 +01005853int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005854resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01005855{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005856 int ret = 0;
5857 uint8_t must_size;
5858 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02005859
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005860 assert(node);
5861
5862 schema = node->schema;
5863
5864 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02005865 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01005866 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005867 must_size = ((struct lys_node_container *)schema)->must_size;
5868 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005869 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005870 must_size = ((struct lys_node_leaf *)schema)->must_size;
5871 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005872 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005873 must_size = ((struct lys_node_leaflist *)schema)->must_size;
5874 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005875 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005876 must_size = ((struct lys_node_list *)schema)->must_size;
5877 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005878 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005879 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005880 must_size = ((struct lys_node_anydata *)schema)->must_size;
5881 break;
5882 case LYS_NOTIF:
5883 must_size = ((struct lys_node_notif *)schema)->must_size;
5884 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005885 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005886 must_size = 0;
5887 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005888 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005889
5890 if (must_size) {
5891 ++ret;
5892 }
5893
5894 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
5895 if (!node->prev->next) {
5896 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
5897 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
5898 ret += 0x2;
5899 }
5900 }
5901
5902 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01005903}
5904
5905int
Radek Krejci46165822016-08-26 14:06:27 +02005906resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01005907{
Radek Krejci46165822016-08-26 14:06:27 +02005908 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01005909
Radek Krejci46165822016-08-26 14:06:27 +02005910 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01005911
Radek Krejci46165822016-08-26 14:06:27 +02005912 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005913 return 1;
5914 }
5915
Radek Krejci46165822016-08-26 14:06:27 +02005916 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01005917 goto check_augment;
5918
Radek Krejci46165822016-08-26 14:06:27 +02005919 while (parent) {
5920 /* stop conditions */
5921 if (!mode) {
5922 /* stop on node that can be instantiated in data tree */
5923 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
5924 break;
5925 }
5926 } else {
5927 /* stop on the specified node */
5928 if (parent == stop) {
5929 break;
5930 }
5931 }
5932
5933 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01005934 return 1;
5935 }
5936check_augment:
5937
5938 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02005939 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02005940 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01005941 }
5942 parent = lys_parent(parent);
5943 }
5944
5945 return 0;
5946}
5947
Michal Vaskocf024702015-10-08 15:01:42 +02005948/**
5949 * @brief Resolve (check) all when conditions relevant for \p node.
5950 * Logs directly.
5951 *
5952 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskocf024702015-10-08 15:01:42 +02005953 *
Radek Krejci03b71f72016-03-16 11:10:09 +01005954 * @return
5955 * -1 - error, ly_errno is set
5956 * 0 - true "when" statement
Radek Krejci46165822016-08-26 14:06:27 +02005957 * 0, ly_vecode = LYVE_NOWHEN - false "when" statement
Radek Krejci03b71f72016-03-16 11:10:09 +01005958 * 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 +02005959 */
Radek Krejci46165822016-08-26 14:06:27 +02005960int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005961resolve_when(struct lyd_node *node, int *result, int ignore_fail)
Michal Vaskocf024702015-10-08 15:01:42 +02005962{
Michal Vasko76c3bd32016-08-24 16:02:52 +02005963 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02005964 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02005965 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005966 enum lyxp_node_type ctx_node_type;
Radek Krejci51093642016-03-29 10:14:59 +02005967 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02005968
5969 assert(node);
5970 memset(&set, 0, sizeof set);
5971
5972 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005973 /* make the node dummy for the evaluation */
5974 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005975 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
5976 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005977 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01005978 if (rc) {
5979 if (rc == 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005980 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01005981 }
Radek Krejci51093642016-03-29 10:14:59 +02005982 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02005983 }
5984
Radek Krejci03b71f72016-03-16 11:10:09 +01005985 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005986 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02005987 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01005988 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko3cfa3182017-01-17 10:00:58 +01005989 if ((ignore_fail == 1) || ((node->schema->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005990 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
5991 ((struct lys_node_container *)node->schema)->when->cond);
5992 } else {
5993 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
5994 goto cleanup;
5995 }
Michal Vaskocf024702015-10-08 15:01:42 +02005996 }
Radek Krejci51093642016-03-29 10:14:59 +02005997
5998 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005999 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006000 }
6001
Michal Vasko90fc2a32016-08-24 15:58:58 +02006002 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006003 goto check_augment;
6004
6005 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006006 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6007 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006008 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006009 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006010 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02006011 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02006012 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006013 }
6014 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006015
6016 unlinked_nodes = NULL;
6017 /* we do not want our node pointer to change */
6018 tmp_node = node;
6019 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6020 if (rc) {
6021 goto cleanup;
6022 }
6023
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006024 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6025 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006026
6027 if (unlinked_nodes && ctx_node) {
6028 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6029 rc = -1;
6030 goto cleanup;
6031 }
6032 }
6033
Radek Krejci03b71f72016-03-16 11:10:09 +01006034 if (rc) {
6035 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006036 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006037 }
Radek Krejci51093642016-03-29 10:14:59 +02006038 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006039 }
6040
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006041 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006042 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006043 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko3cfa3182017-01-17 10:00:58 +01006044 if ((ignore_fail == 1) || ((sparent->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006045 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6046 ((struct lys_node_uses *)sparent)->when->cond);
6047 } else {
6048 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
6049 goto cleanup;
6050 }
Michal Vaskocf024702015-10-08 15:01:42 +02006051 }
Radek Krejci51093642016-03-29 10:14:59 +02006052
6053 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006054 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006055 }
6056
6057check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006058 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006059 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006060 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006061 if (rc) {
Michal Vaskocf024702015-10-08 15:01:42 +02006062 LOGINT;
Radek Krejci51093642016-03-29 10:14:59 +02006063 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006064 }
6065 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006066
6067 unlinked_nodes = NULL;
6068 tmp_node = node;
6069 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6070 if (rc) {
6071 goto cleanup;
6072 }
6073
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006074 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6075 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006076
6077 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6078 * so the tree did not actually change and there is nothing for us to do
6079 */
6080 if (unlinked_nodes && ctx_node) {
6081 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6082 rc = -1;
6083 goto cleanup;
6084 }
6085 }
6086
Radek Krejci03b71f72016-03-16 11:10:09 +01006087 if (rc) {
6088 if (rc == 1) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006089 LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006090 }
Radek Krejci51093642016-03-29 10:14:59 +02006091 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006092 }
6093
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006094 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006095 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006096 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko3cfa3182017-01-17 10:00:58 +01006097 if ((ignore_fail == 1) || ((sparent->parent->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006098 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006099 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006100 } else {
6101 LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
6102 goto cleanup;
6103 }
Michal Vaskocf024702015-10-08 15:01:42 +02006104 }
Radek Krejci51093642016-03-29 10:14:59 +02006105
6106 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006107 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006108 }
6109
Michal Vasko90fc2a32016-08-24 15:58:58 +02006110 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006111 }
6112
Radek Krejci0b7704f2016-03-18 12:16:14 +01006113 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006114
Radek Krejci51093642016-03-29 10:14:59 +02006115cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006116 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006117 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006118
Radek Krejci46165822016-08-26 14:06:27 +02006119 if (result) {
6120 if (node->when_status & LYD_WHEN_TRUE) {
6121 *result = 1;
6122 } else {
6123 *result = 0;
6124 }
6125 }
6126
Radek Krejci51093642016-03-29 10:14:59 +02006127 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006128}
6129
Radek Krejcicbb473e2016-09-16 14:48:32 +02006130static int
6131check_leafref_features(struct lys_type *type)
6132{
6133 struct lys_node *iter;
6134 struct ly_set *src_parents, *trg_parents, *features;
6135 unsigned int i, j, size, x;
6136 int ret = EXIT_SUCCESS;
6137
6138 assert(type->parent);
6139
6140 src_parents = ly_set_new();
6141 trg_parents = ly_set_new();
6142 features = ly_set_new();
6143
6144 /* get parents chain of source (leafref) */
6145 for (iter = (struct lys_node *)type->parent; iter; iter = iter->parent) {
6146 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6147 continue;
6148 }
6149 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
6150 }
6151 /* get parents chain of target */
6152 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = iter->parent) {
6153 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6154 continue;
6155 }
6156 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
6157 }
6158
6159 /* compare the features used in if-feature statements in the rest of both
6160 * chains of parents. The set of features used for target must be a subset
6161 * of features used for the leafref. This is not a perfect, we should compare
6162 * the truth tables but it could require too much resources, so we simplify that */
6163 for (i = 0; i < src_parents->number; i++) {
6164 iter = src_parents->set.s[i]; /* shortcut */
6165 if (!iter->iffeature_size) {
6166 continue;
6167 }
6168 for (j = 0; j < iter->iffeature_size; j++) {
6169 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6170 for (; size; size--) {
6171 if (!iter->iffeature[j].features[size - 1]) {
6172 /* not yet resolved feature, postpone this check */
6173 ret = EXIT_FAILURE;
6174 goto cleanup;
6175 }
6176 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
6177 }
6178 }
6179 }
6180 x = features->number;
6181 for (i = 0; i < trg_parents->number; i++) {
6182 iter = trg_parents->set.s[i]; /* shortcut */
6183 if (!iter->iffeature_size) {
6184 continue;
6185 }
6186 for (j = 0; j < iter->iffeature_size; j++) {
6187 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6188 for (; size; size--) {
6189 if (!iter->iffeature[j].features[size - 1]) {
6190 /* not yet resolved feature, postpone this check */
6191 ret = EXIT_FAILURE;
6192 goto cleanup;
6193 }
6194 if ((unsigned int)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
6195 /* the feature is not present in features set of target's parents chain */
6196 LOGVAL(LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
Michal Vasko51e5c582017-01-19 14:16:39 +01006197 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcicbb473e2016-09-16 14:48:32 +02006198 "Leafref is not conditional based on \"%s\" feature as its target.",
6199 iter->iffeature[j].features[size - 1]->name);
6200 ret = -1;
6201 goto cleanup;
6202 }
6203 }
6204 }
6205 }
6206
6207cleanup:
6208 ly_set_free(features);
6209 ly_set_free(src_parents);
6210 ly_set_free(trg_parents);
6211
6212 return ret;
6213}
6214
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006215/**
Michal Vaskobb211122015-08-19 14:03:11 +02006216 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006217 *
6218 * @param[in] mod Main module.
6219 * @param[in] item Item to resolve. Type determined by \p type.
6220 * @param[in] type Type of the unresolved item.
6221 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006222 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006223 *
6224 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6225 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006226static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006227resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Radek Krejci48464ed2016-03-17 15:44:09 +01006228 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006229{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006230 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Radek Krejcic79c6b12016-07-26 15:11:49 +02006231 int rc = -1, has_str = 0, tpdf_flag = 0, i, k;
6232 unsigned int j;
Radek Krejci80056d52017-01-05 13:13:33 +01006233 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006234 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006235 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006236
Radek Krejcic79c6b12016-07-26 15:11:49 +02006237 struct ly_set *refs, *procs;
6238 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006239 struct lys_ident *ident;
6240 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006241 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006242 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006243 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006244 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006245 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006246 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006247 struct lys_ext_instance *ext, **extlist;
6248 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006249
6250 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006251 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006252 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006253 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006254 ident = item;
6255
Radek Krejci018f1f52016-08-03 16:01:20 +02006256 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006257 break;
6258 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006259 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006260 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006261 stype = item;
6262
Radek Krejci018f1f52016-08-03 16:01:20 +02006263 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006264 break;
6265 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006266 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006267 stype = item;
6268
Radek Krejci2f12f852016-01-08 12:59:57 +01006269 /* HACK - when there is no parent, we are in top level typedef and in that
6270 * case, the path has to contain absolute path, so we let the resolve_path_arg_schema()
6271 * know it via tpdf_flag */
6272 if (!node) {
Radek Krejci4f78b532016-02-17 13:43:00 +01006273 tpdf_flag = 1;
Radek Krejci2f12f852016-01-08 12:59:57 +01006274 node = (struct lys_node *)stype->parent;
6275 }
6276
Radek Krejci27fe55e2016-09-13 17:13:35 +02006277 if (!lys_node_module(node)->implemented) {
6278 /* not implemented module, don't bother with resolving the leafref
Radek Krejci990af1f2016-11-09 13:53:36 +01006279 * if the module is set to be implemented, the path will be resolved then */
Radek Krejci27fe55e2016-09-13 17:13:35 +02006280 rc = 0;
6281 break;
6282 }
Radek Krejci48464ed2016-03-17 15:44:09 +01006283 rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag,
Michal Vasko1e62a092015-12-01 12:27:20 +01006284 (const struct lys_node **)&stype->info.lref.target);
Michal Vasko01c6fd22016-05-20 11:43:05 +02006285 if (!tpdf_flag && !rc) {
6286 assert(stype->info.lref.target);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006287 /* check if leafref and its target are under a common if-features */
6288 rc = check_leafref_features(stype);
6289 if (rc) {
6290 break;
6291 }
6292
Radek Krejci46c4cd72016-01-21 15:13:52 +01006293 /* store the backlink from leafref target */
Michal Vasko01c6fd22016-05-20 11:43:05 +02006294 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
6295 rc = -1;
Radek Krejci46c4cd72016-01-21 15:13:52 +01006296 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006297 }
6298
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006299 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02006300 case UNRES_TYPE_DER_TPDF:
6301 tpdf_flag = 1;
6302 /* no break */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006303 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006304 /* parent */
6305 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006306 stype = item;
6307
Michal Vasko88c29542015-11-27 14:57:53 +01006308 /* HACK type->der is temporarily unparsed type statement */
6309 yin = (struct lyxml_elem *)stype->der;
6310 stype->der = NULL;
6311
Pavol Vicana0e4e672016-02-24 12:20:04 +01006312 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6313 yang = (struct yang_type *)yin;
Radek Krejci3a5501d2016-07-18 22:03:34 +02006314 rc = yang_check_type(mod, node, yang, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006315
6316 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006317 /* may try again later */
6318 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006319 } else {
6320 /* we need to always be able to free this, it's safe only in this case */
Pavol Vican5f0316a2016-04-05 21:21:11 +02006321 lydict_remove(mod->ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006322 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006323 }
6324
Michal Vasko88c29542015-11-27 14:57:53 +01006325 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02006326 rc = fill_yin_type(mod, node, yin, stype, tpdf_flag, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006327 if (!rc) {
6328 /* we need to always be able to free this, it's safe only in this case */
6329 lyxml_free(mod->ctx, yin);
6330 } else {
6331 /* may try again later, put all back how it was */
6332 stype->der = (struct lys_tpdf *)yin;
6333 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006334 }
Radek Krejcic13db382016-08-16 10:52:42 +02006335 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006336 /* it does not make sense to have leaf-list of empty type */
6337 if (!tpdf_flag && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
6338 LOGWRN("The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
6339 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006340 } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +02006341 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6342 * by uses statement until the type is resolved. We do that the same way as uses statements inside
6343 * grouping - the grouping's nacm member (not used un grouping) is used to increase the number of
6344 * so far unresolved items (uses and types). The grouping cannot be used unless the nacm value is 0.
Radek Krejci9b6aad22016-09-20 15:55:51 +02006345 * To remember that the grouping already increased grouping's nacm, the LY_TYPE_ERR is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006346 * of the type's base member. */
6347 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6348 if (par_grp) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01006349#if __BYTE_ORDER == __LITTLE_ENDIAN
6350 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[1]++;
6351#else
6352 ((uint8_t*)&((struct lys_node_grp *)par_grp)->flags)[0]++;
6353#endif
Radek Krejci9b6aad22016-09-20 15:55:51 +02006354 stype->base = LY_TYPE_ERR;
Radek Krejcic13db382016-08-16 10:52:42 +02006355 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006356 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006357 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006358 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006359 iff_data = str_snode;
6360 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006361 if (!rc) {
6362 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006363 if (iff_data->infeature) {
6364 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6365 feat = *((struct lys_feature **)item);
6366 if (!feat->depfeatures) {
6367 feat->depfeatures = ly_set_new();
6368 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006369 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006370 }
6371 /* cleanup temporary data */
Radek Krejcicbb473e2016-09-16 14:48:32 +02006372 lydict_remove(mod->ctx, iff_data->fname);
6373 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006374 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006375 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006376 case UNRES_FEATURE:
6377 feat = (struct lys_feature *)item;
6378
6379 if (feat->iffeature_size) {
6380 refs = ly_set_new();
6381 procs = ly_set_new();
6382 ly_set_add(procs, feat, 0);
6383
6384 while (procs->number) {
6385 ref = procs->set.g[procs->number - 1];
6386 ly_set_rm_index(procs, procs->number - 1);
6387
6388 for (i = 0; i < ref->iffeature_size; i++) {
6389 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6390 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006391 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006392 if (ref->iffeature[i].features[j - 1] == feat) {
6393 LOGVAL(LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
6394 goto featurecheckdone;
6395 }
6396
6397 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6398 k = refs->number;
6399 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6400 /* not yet seen feature, add it for processing */
6401 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6402 }
6403 }
6404 } else {
6405 /* forward reference */
6406 rc = EXIT_FAILURE;
6407 goto featurecheckdone;
6408 }
6409 }
6410
6411 }
6412 }
6413 rc = EXIT_SUCCESS;
6414
6415featurecheckdone:
6416 ly_set_free(refs);
6417 ly_set_free(procs);
6418 }
6419
6420 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006421 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006422 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006423 break;
6424 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006425 stype = item;
6426
Radek Krejci51673202016-11-01 17:00:32 +01006427 rc = check_default(stype, (const char **)str_snode, mod);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006428 break;
6429 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006430 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006431 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006432 choic = item;
6433
Radek Krejcie00d2312016-08-12 15:27:49 +02006434 if (!choic->dflt) {
6435 choic->dflt = resolve_choice_dflt(choic, expr);
6436 }
Michal Vasko7955b362015-09-04 14:18:15 +02006437 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006438 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006439 } else {
6440 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006441 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006442 break;
6443 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006444 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006445 break;
6446 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006447 unique_info = (struct unres_list_uniq *)item;
6448 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006449 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006450 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006451 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006452 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006453 case UNRES_XPATH:
6454 node = (struct lys_node *)item;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02006455 rc = lys_check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006456 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006457 case UNRES_EXT:
6458 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006459 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006460 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006461 if (!rc) {
6462 if (ext_data->datatype == LYS_IN_YIN) {
6463 /* YIN */
6464 lyxml_free(mod->ctx, ext_data->data.yin);
6465 } else {
6466 /* TODO YANG */
6467 free(ext_data->data.yang);
6468 }
Radek Krejci80056d52017-01-05 13:13:33 +01006469
6470 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006471 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006472 if (eplugin) {
6473 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006474 u = malloc(sizeof *u);
6475 (*u) = ext_data->ext_index;
6476 unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u);
Radek Krejci80056d52017-01-05 13:13:33 +01006477 }
6478 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006479
6480 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006481 }
6482 break;
Radek Krejci80056d52017-01-05 13:13:33 +01006483 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01006484 u = (uint8_t *)str_snode;
6485 ext = (*(struct lys_ext_instance ***)item)[*u];
6486 free(u);
6487
Radek Krejci80056d52017-01-05 13:13:33 +01006488 eplugin = ext->def->plugin;
6489
6490 /* inherit */
6491 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
6492 root = (struct lys_node *)ext->parent;
6493 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6494 LY_TREE_DFS_BEGIN(root->child, next, node) {
6495 /* first, check if the node already contain instance of the same extension,
6496 * in such a case we won't inherit. In case the node was actually defined as
6497 * augment data, we are supposed to check the same way also the augment node itself */
6498 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
6499 goto inherit_dfs_sibling;
6500 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
6501 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
6502 goto inherit_dfs_sibling;
6503 }
6504
6505 if (eplugin->check_inherit) {
6506 /* we have a callback to check the inheritance, use it */
6507 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
6508 case 0:
6509 /* yes - continue with the inheriting code */
6510 break;
6511 case 1:
6512 /* no - continue with the node's sibling */
6513 goto inherit_dfs_sibling;
6514 case 2:
6515 /* no, but continue with the children, just skip the inheriting code for this node */
6516 goto inherit_dfs_child;
6517 default:
6518 LOGERR(LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
6519 ext->def->module->name, ext->def->name, rc);
6520 }
6521 }
6522
6523 /* inherit the extension */
6524 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
6525 if (!extlist) {
6526 LOGMEM;
6527 return -1;
6528 }
6529 extlist[node->ext_size] = malloc(sizeof **extlist);
6530 if (!extlist[node->ext_size]) {
6531 LOGMEM;
6532 node->ext = extlist;
6533 return -1;
6534 }
6535 memcpy(extlist[node->ext_size], ext, sizeof *ext);
6536 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
6537
6538 node->ext = extlist;
6539 node->ext_size++;
6540
6541inherit_dfs_child:
6542 /* modification of - select element for the next run - children first */
6543 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
6544 next = NULL;
6545 } else {
6546 next = node->child;
6547 }
6548 if (!next) {
6549inherit_dfs_sibling:
6550 /* no children, try siblings */
6551 next = node->next;
6552 }
6553 while (!next) {
6554 /* go to the parent */
6555 node = lys_parent(node);
6556
6557 /* we are done if we are back in the root (the starter's parent */
6558 if (node == root) {
6559 break;
6560 }
6561
6562 /* parent is already processed, go to its sibling */
6563 next = node->next;
6564 }
6565 }
6566 }
6567 }
6568
6569 /* final check */
6570 if (eplugin->check_result) {
6571 if ((*eplugin->check_result)(ext)) {
6572 return -1;
6573 }
6574 }
6575
6576 rc = 0;
6577 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006578 default:
6579 LOGINT;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006580 break;
6581 }
6582
Radek Krejci54081ce2016-08-12 15:21:47 +02006583 if (has_str && !rc) {
6584 /* the string is no more needed in case of success.
6585 * In case of forward reference, we will try to resolve the string later */
Radek Krejci4f78b532016-02-17 13:43:00 +01006586 lydict_remove(mod->ctx, str_snode);
6587 }
6588
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006589 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006590}
6591
Michal Vaskof02e3742015-08-05 16:27:02 +02006592/* logs directly */
6593static void
Radek Krejci48464ed2016-03-17 15:44:09 +01006594print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006595{
Michal Vaskocb34dc62016-05-20 14:38:37 +02006596 struct lyxml_elem *xml;
6597 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006598 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006599 const char *name = NULL;
6600 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006601
Michal Vaskof02e3742015-08-05 16:27:02 +02006602 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02006603 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006604 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006605 break;
6606 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006607 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006608 break;
6609 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006610 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
6611 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02006612 break;
Radek Krejci3a5501d2016-07-18 22:03:34 +02006613 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02006614 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02006615 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
6616 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01006617 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006618 } else {
6619 LY_TREE_FOR(xml->attr, attr) {
6620 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006621 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006622 break;
6623 }
6624 }
6625 assert(attr);
6626 }
Radek Krejcie534c132016-11-23 13:32:31 +01006627 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006628 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006629 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006630 iff_data = str_node;
6631 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02006632 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006633 case UNRES_FEATURE:
6634 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
6635 ((struct lys_feature *)item)->name);
6636 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006637 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006638 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006639 break;
6640 case UNRES_TYPE_DFLT:
Radek Krejci2e2de832016-10-13 16:12:26 +02006641 if (str_node) {
6642 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
6643 } /* else no default value in the type itself, but we are checking some restrictions against
6644 * possible default value of some base type. The failure is caused by not resolved base type,
6645 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02006646 break;
6647 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006648 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006649 break;
6650 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01006651 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006652 break;
6653 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01006654 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006655 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006656 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006657 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
6658 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01006659 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006660 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01006661 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
6662 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02006663 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006664 case UNRES_EXT:
6665 extinfo = (struct unres_ext *)str_node;
6666 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
6667 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
6668 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006669 default:
6670 LOGINT;
Michal Vaskof02e3742015-08-05 16:27:02 +02006671 break;
6672 }
6673}
6674
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006675/**
Michal Vaskobb211122015-08-19 14:03:11 +02006676 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006677 *
6678 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006679 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006680 *
Michal Vasko92b8a382015-08-19 14:03:49 +02006681 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006682 */
Michal Vaskof02e3742015-08-05 16:27:02 +02006683int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006684resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02006685{
Radek Krejci010e54b2016-03-15 09:40:34 +01006686 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006687 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006688
6689 assert(unres);
6690
Michal Vaskoe8734262016-09-29 14:12:06 +02006691 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Radek Krejci010e54b2016-03-15 09:40:34 +01006692 ly_vlog_hide(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02006693
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006694 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02006695 do {
Michal Vasko88c29542015-11-27 14:57:53 +01006696 unres_count = 0;
6697 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02006698
6699 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006700 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006701 * if-features are resolved here to make sure that we will have all if-features for
6702 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02006703 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006704 continue;
6705 }
Radek Krejci018f1f52016-08-03 16:01:20 +02006706 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejci818b0c52016-11-09 15:10:51 +01006707 * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02006708
Michal Vasko88c29542015-11-27 14:57:53 +01006709 ++unres_count;
Radek Krejci48464ed2016-03-17 15:44:09 +01006710 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006711 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006712 unres->type[i] = UNRES_RESOLVED;
6713 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01006714 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02006715 } else if (rc == -1) {
Radek Krejci010e54b2016-03-15 09:40:34 +01006716 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02006717 /* print the error */
6718 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006719 return -1;
Radek Krejcic2a180f2016-06-22 13:28:16 +02006720 } else {
6721 /* forward reference, erase ly_errno */
Radek Krejci00a0e712016-10-26 10:24:46 +02006722 ly_err_clean(1);
Michal Vasko51054ca2015-08-12 12:20:00 +02006723 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006724 }
Michal Vasko88c29542015-11-27 14:57:53 +01006725 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02006726
Michal Vasko88c29542015-11-27 14:57:53 +01006727 if (res_count < unres_count) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006728 /* just print the errors */
6729 ly_vlog_hide(0);
6730
6731 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006732 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006733 continue;
6734 }
6735 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
6736 }
Michal Vasko92b8a382015-08-19 14:03:49 +02006737 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006738 }
6739
Radek Krejci07d0fb92017-01-13 14:11:05 +01006740 /* the rest except finalizing extensions */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006741 for (i = 0; i < unres->count; ++i) {
Radek Krejci80056d52017-01-05 13:13:33 +01006742 if (unres->type[i] == UNRES_RESOLVED || unres->type[i] == UNRES_EXT_FINALIZE) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006743 continue;
6744 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02006745
Radek Krejci48464ed2016-03-17 15:44:09 +01006746 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01006747 if (rc == 0) {
Pavol Vican88e16c92016-09-07 15:41:50 +02006748 if (unres->type[i] == UNRES_LIST_UNIQ) {
6749 /* free the allocated structure */
6750 free(unres->item[i]);
6751 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006752 unres->type[i] = UNRES_RESOLVED;
6753 ++resolved;
6754 } else if (rc == -1) {
6755 ly_vlog_hide(0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02006756 /* print the error */
6757 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
6758 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006759 }
6760 }
6761
Radek Krejci010e54b2016-03-15 09:40:34 +01006762 ly_vlog_hide(0);
6763
Radek Krejci80056d52017-01-05 13:13:33 +01006764 /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
6765 for (i = 0; i < unres->count; ++i) {
6766 if (unres->type[i] != UNRES_EXT_FINALIZE) {
6767 continue;
6768 }
6769
6770 rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
6771 if (rc == 0) {
6772 unres->type[i] = UNRES_RESOLVED;
6773 ++resolved;
6774 }
6775 }
6776
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006777 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01006778 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
6779 * all the validation errors
6780 */
6781 for (i = 0; i < unres->count; ++i) {
6782 if (unres->type[i] == UNRES_RESOLVED) {
6783 continue;
6784 }
Radek Krejcib3142312016-11-09 11:04:12 +01006785 if (unres->type[i] == UNRES_XPATH) {
6786 /* unresolvable XPaths are actually supposed to be warnings - they may be
6787 * unresolved due to the not implemented target module so it shouldn't avoid
6788 * parsing the module, but we still want to announce some issue here */
6789 ly_vlog_hide(0xff);
6790 }
Radek Krejci48464ed2016-03-17 15:44:09 +01006791 resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejcib3142312016-11-09 11:04:12 +01006792 if (unres->type[i] == UNRES_XPATH && *ly_vlog_hide_location() == 0xff) {
6793 unres->type[i] = UNRES_RESOLVED;
6794 resolved++;
6795 ly_vlog_hide(0);
6796 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006797 }
Radek Krejcib3142312016-11-09 11:04:12 +01006798 if (resolved < unres->count) {
6799 return -1;
6800 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006801 }
6802
Michal Vaskoe8734262016-09-29 14:12:06 +02006803 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01006804 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006805 return EXIT_SUCCESS;
6806}
6807
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006808/**
Michal Vaskobb211122015-08-19 14:03:11 +02006809 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006810 *
6811 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006812 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006813 * @param[in] item Item to resolve. Type determined by \p type.
6814 * @param[in] type Type of the unresolved item.
6815 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006816 *
Michal Vasko3767fb22016-07-21 12:10:57 +02006817 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006818 */
6819int
Radek Krejci48464ed2016-03-17 15:44:09 +01006820unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
6821 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006822{
Radek Krejci54081ce2016-08-12 15:21:47 +02006823 int rc;
6824 const char *dictstr;
6825
6826 dictstr = lydict_insert(mod->ctx, str, 0);
6827 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
6828
6829 if (rc == -1) {
6830 lydict_remove(mod->ctx, dictstr);
6831 }
6832 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006833}
6834
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006835/**
Michal Vaskobb211122015-08-19 14:03:11 +02006836 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006837 *
6838 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006839 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006840 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01006841 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006842 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006843 *
Michal Vasko3767fb22016-07-21 12:10:57 +02006844 * @return EXIT_SUCCESS on success, EXIT_FIALURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006845 */
6846int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006847unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01006848 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006849{
Michal Vaskoef486d72016-09-27 12:10:44 +02006850 int rc, log_hidden;
Radek Krejci850a5de2016-11-08 14:06:40 +01006851 uint32_t u;
Michal Vasko88c29542015-11-27 14:57:53 +01006852 struct lyxml_elem *yin;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006853
Michal Vasko9bf425b2015-10-22 11:42:03 +02006854 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
6855 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006856
Radek Krejci850a5de2016-11-08 14:06:40 +01006857 /* check for duplicities in unres */
6858 for (u = 0; u < unres->count; u++) {
6859 if (unres->type[u] == type && unres->item[u] == item &&
6860 unres->str_snode[u] == snode && unres->module[u] == mod) {
6861 /* duplication, will be resolved later */
6862 return EXIT_FAILURE;
6863 }
6864 }
6865
Radek Krejci80056d52017-01-05 13:13:33 +01006866 if (type != UNRES_EXT_FINALIZE) {
6867 /* extension finalization is not even tried when adding the item into the inres list */
Michal Vaskoef486d72016-09-27 12:10:44 +02006868
Radek Krejci80056d52017-01-05 13:13:33 +01006869 if (*ly_vlog_hide_location()) {
6870 log_hidden = 1;
6871 } else {
6872 log_hidden = 0;
6873 ly_vlog_hide(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01006874 }
Radek Krejci80056d52017-01-05 13:13:33 +01006875 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
6876 if (!log_hidden) {
6877 ly_vlog_hide(0);
6878 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006879
Radek Krejci80056d52017-01-05 13:13:33 +01006880 if (rc != EXIT_FAILURE) {
6881 if (rc == -1 && ly_errno == LY_EVALID) {
6882 ly_err_repeat();
6883 }
6884 if (type == UNRES_LIST_UNIQ) {
6885 /* free the allocated structure */
6886 free(item);
6887 } else if (rc == -1 && type == UNRES_IFFEAT) {
6888 /* free the allocated resources */
6889 free(*((char **)item));
6890 }
6891 return rc;
6892 } else {
6893 /* erase info about validation errors */
6894 ly_err_clean(1);
6895 }
Michal Vaskof02e3742015-08-05 16:27:02 +02006896
Radek Krejci80056d52017-01-05 13:13:33 +01006897 print_unres_schema_item_fail(item, type, snode);
6898
6899 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
6900 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
6901 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
6902 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
6903 lyxml_unlink_elem(mod->ctx, yin, 1);
6904 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
6905 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01006906 }
Michal Vasko88c29542015-11-27 14:57:53 +01006907 }
6908
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006909 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01006910 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
6911 if (!unres->item) {
6912 LOGMEM;
6913 return -1;
6914 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006915 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01006916 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
6917 if (!unres->type) {
6918 LOGMEM;
6919 return -1;
6920 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006921 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01006922 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
6923 if (!unres->str_snode) {
6924 LOGMEM;
6925 return -1;
6926 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006927 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01006928 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
6929 if (!unres->module) {
6930 LOGMEM;
6931 return -1;
6932 }
6933 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006934
Michal Vasko3767fb22016-07-21 12:10:57 +02006935 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006936}
6937
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006938/**
Michal Vaskobb211122015-08-19 14:03:11 +02006939 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006940 *
6941 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006942 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006943 * @param[in] item Old item to be resolved.
6944 * @param[in] type Type of the old unresolved item.
6945 * @param[in] new_item New item to use in the duplicate.
6946 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02006947 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006948 */
Michal Vaskodad19402015-08-06 09:51:53 +02006949int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006950unres_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 +02006951{
6952 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006953 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006954 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006955
Michal Vaskocf024702015-10-08 15:01:42 +02006956 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006957
Radek Krejcid09d1a52016-08-11 14:05:45 +02006958 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
6959 if (type == UNRES_LIST_UNIQ) {
6960 aux_uniq.list = item;
6961 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
6962 item = &aux_uniq;
6963 }
Michal Vasko878e38d2016-09-05 12:17:53 +02006964 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006965
6966 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02006967 if (type == UNRES_LIST_UNIQ) {
6968 free(new_item);
6969 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02006970 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006971 }
6972
Radek Krejcic79c6b12016-07-26 15:11:49 +02006973 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02006974 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006975 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006976 LOGINT;
6977 return -1;
6978 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006979 } else if (type == UNRES_IFFEAT) {
6980 /* duplicate unres_iffeature_data */
6981 iff_data = malloc(sizeof *iff_data);
6982 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
6983 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
6984 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
6985 LOGINT;
6986 return -1;
6987 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006988 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01006989 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006990 LOGINT;
6991 return -1;
6992 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006993 }
Michal Vaskodad19402015-08-06 09:51:53 +02006994
6995 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006996}
6997
Michal Vaskof02e3742015-08-05 16:27:02 +02006998/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006999int
Michal Vasko878e38d2016-09-05 12:17:53 +02007000unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007001{
Michal Vasko878e38d2016-09-05 12:17:53 +02007002 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007003 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007004
Michal Vasko878e38d2016-09-05 12:17:53 +02007005 if (start_on_backwards > 0) {
7006 i = start_on_backwards;
7007 } else {
7008 i = unres->count - 1;
7009 }
7010 for (; i > -1; i--) {
7011 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007012 continue;
7013 }
7014 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007015 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007016 break;
7017 }
7018 } else {
7019 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7020 aux_uniq2 = (struct unres_list_uniq *)item;
7021 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007022 break;
7023 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007024 }
7025 }
7026
Michal Vasko878e38d2016-09-05 12:17:53 +02007027 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007028}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007029
Michal Vaskoede9c472016-06-07 09:38:15 +02007030static void
7031unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7032{
7033 struct lyxml_elem *yin;
7034 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007035 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007036
7037 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007038 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007039 case UNRES_TYPE_DER:
7040 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7041 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7042 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007043 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007044 lydict_remove(ctx, yang->name);
7045 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007046 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7047 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7048 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007049 } else {
7050 lyxml_free(ctx, yin);
7051 }
7052 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007053 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007054 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7055 lydict_remove(ctx, iff_data->fname);
7056 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007057 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007058 case UNRES_IDENT:
7059 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007060 case UNRES_CHOICE_DFLT:
7061 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007062 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7063 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007064 case UNRES_LIST_UNIQ:
7065 free(unres->item[i]);
7066 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007067 default:
7068 break;
7069 }
7070 unres->type[i] = UNRES_RESOLVED;
7071}
7072
Michal Vasko88c29542015-11-27 14:57:53 +01007073void
Radek Krejcic071c542016-01-27 14:57:51 +01007074unres_schema_free(struct lys_module *module, struct unres_schema **unres)
Michal Vasko88c29542015-11-27 14:57:53 +01007075{
7076 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007077 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007078
Radek Krejcic071c542016-01-27 14:57:51 +01007079 if (!unres || !(*unres)) {
7080 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007081 }
7082
Radek Krejcic071c542016-01-27 14:57:51 +01007083 assert(module || (*unres)->count == 0);
7084
7085 for (i = 0; i < (*unres)->count; ++i) {
7086 if ((*unres)->module[i] != module) {
7087 if ((*unres)->type[i] != UNRES_RESOLVED) {
7088 unresolved++;
7089 }
7090 continue;
7091 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007092
7093 /* free heap memory for the specific item */
7094 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007095 }
7096
Michal Vaskoede9c472016-06-07 09:38:15 +02007097 /* free it all */
Radek Krejcic071c542016-01-27 14:57:51 +01007098 if (!module || (!unresolved && !module->type)) {
7099 free((*unres)->item);
7100 free((*unres)->type);
7101 free((*unres)->str_snode);
7102 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007103 free((*unres));
7104 (*unres) = NULL;
7105 }
Michal Vasko88c29542015-11-27 14:57:53 +01007106}
7107
Michal Vasko3cfa3182017-01-17 10:00:58 +01007108static int
7109check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7110{
7111 struct ly_set *set;
Michal Vasko3c777092017-01-17 14:10:09 +01007112 struct lys_node *op_node, *first_node;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007113 char *buf;
7114
7115 for (op_node = lys_parent(sleaf);
7116 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7117 op_node = lys_parent(op_node));
7118
7119 if (op_node && lys_parent(op_node)) {
7120 /* nested operation - any absolute path is external */
7121 return 1;
7122 }
7123
7124 /* get the first node from the instid */
7125 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7126 if (!buf) {
7127 LOGMEM;
7128 return -1;
7129 }
7130
7131 /* there is a predicate, remove it */
7132 if (buf[strlen(buf) - 1] == ']') {
7133 assert(strchr(buf, '['));
7134 *strchr(buf, '[') = '\0';
7135 }
7136
7137 /* find the first schema node */
7138 set = lys_find_xpath(sleaf, buf, 0);
7139 if (!set || !set->number) {
7140 free(buf);
7141 return 1;
7142 }
7143 free(buf);
7144
Michal Vasko3c777092017-01-17 14:10:09 +01007145 first_node = set->set.s[0];
7146 ly_set_free(set);
7147
Michal Vasko3cfa3182017-01-17 10:00:58 +01007148 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7149
7150 if (op_node) {
7151 /* it is an operation, so we're good if it points somewhere inside it */
Michal Vasko3c777092017-01-17 14:10:09 +01007152 if (op_node == first_node) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007153 assert(set->number == 1);
7154 return 0;
7155 } else {
7156 return 1;
7157 }
7158 }
7159
7160 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7161 * this itself), so we say it's not external */
7162 return 0;
7163}
7164
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007165/**
7166 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7167 *
7168 * @param[in] data Data node where the path is used
7169 * @param[in] path Instance-identifier node value.
7170 * @param[in,out] ret Resolved instance or NULL.
7171 *
7172 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7173 */
7174static int
7175resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7176{
7177 int i = 0, j;
7178 const struct lys_module *mod;
7179 struct ly_ctx *ctx = data->schema->module->ctx;
7180 const char *model, *name;
7181 char *str;
7182 int mod_len, name_len, has_predicate;
7183 struct unres_data node_match;
7184
7185 memset(&node_match, 0, sizeof node_match);
7186 *ret = NULL;
7187
7188 /* we need root to resolve absolute path */
7189 for (; data->parent; data = data->parent);
7190 /* we're still parsing it and the pointer is not correct yet */
7191 if (data->prev) {
7192 for (; data->prev->next; data = data->prev);
7193 }
7194
7195 /* search for the instance node */
7196 while (path[i]) {
7197 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7198 if (j <= 0) {
7199 LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
7200 goto error;
7201 }
7202 i += j;
7203
7204 str = strndup(model, mod_len);
7205 if (!str) {
7206 LOGMEM;
7207 goto error;
7208 }
7209 mod = ly_ctx_get_module(ctx, str, NULL);
Michal Vaskof53187d2017-01-13 13:23:14 +01007210 if (ctx->data_clb) {
7211 if (!mod) {
7212 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7213 } else if (!mod->implemented) {
7214 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7215 }
7216 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007217 free(str);
7218
Michal Vaskof53187d2017-01-13 13:23:14 +01007219 if (!mod || !mod->implemented || mod->disabled) {
7220 break;
7221 }
7222
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007223 if (resolve_data(mod, name, name_len, data, &node_match)) {
7224 /* no instance exists */
7225 break;
7226 }
7227
7228 if (has_predicate) {
7229 /* we have predicate, so the current results must be list or leaf-list */
7230 j = resolve_predicate(&path[i], &node_match);
7231 if (j < 1) {
7232 LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]);
7233 goto error;
7234 }
7235 i += j;
7236
7237 if (!node_match.count) {
7238 /* no instance exists */
7239 break;
7240 }
7241 }
7242 }
7243
7244 if (!node_match.count) {
7245 /* no instance exists */
7246 if (req_inst > -1) {
7247 LOGVAL(LYE_NOREQINS, LY_VLOG_NONE, NULL, path);
7248 return EXIT_FAILURE;
7249 }
7250 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7251 return EXIT_SUCCESS;
7252 } else if (node_match.count > 1) {
7253 /* instance identifier must resolve to a single node */
7254 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
7255 goto error;
7256 } else {
7257 /* we have required result, remember it and cleanup */
7258 *ret = node_match.node[0];
7259 free(node_match.node);
7260 return EXIT_SUCCESS;
7261 }
7262
7263error:
7264 /* cleanup */
7265 free(node_match.node);
7266 return -1;
7267}
7268
7269static int
7270resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007271{
Radek Krejci7de36cf2016-09-12 16:18:50 +02007272 struct unres_data matches;
7273 uint32_t i;
7274
Radek Krejci9b6aad22016-09-20 15:55:51 +02007275 /* init */
Radek Krejci7de36cf2016-09-12 16:18:50 +02007276 memset(&matches, 0, sizeof matches);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007277 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007278
7279 /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007280 if (resolve_path_arg_data((struct lyd_node *)leaf, path, &matches) == -1) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007281 return -1;
7282 }
7283
7284 /* check that value matches */
7285 for (i = 0; i < matches.count; ++i) {
Radek Krejci1899d6a2016-11-03 13:48:07 +01007286 /* not that the value is already in canonical form since the parsers does the conversion,
7287 * so we can simply compare just the values */
Radek Krejci7de36cf2016-09-12 16:18:50 +02007288 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) {
Radek Krejci1899d6a2016-11-03 13:48:07 +01007289 /* we have the match */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007290 *ret = matches.node[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007291 break;
7292 }
7293 }
7294
7295 free(matches.node);
7296
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007297 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007298 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007299 if (req_inst > -1) {
7300 LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007301 return EXIT_FAILURE;
7302 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007303 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 +02007304 }
7305 }
7306
7307 return EXIT_SUCCESS;
7308}
7309
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007310/* 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 +01007311int
7312resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7313 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007314{
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007315 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007316 struct lyd_node *ret;
7317 int found, hidden, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007318 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007319
7320 assert(type->base == LY_TYPE_UNION);
7321
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007322 if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) {
7323 /* either NULL or instid previously converted to JSON */
7324 json_val = leaf->value.string;
7325 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007326
Michal Vaskofd6c6502017-01-06 12:15:41 +01007327 if (store) {
7328 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7329 free(leaf->value.bit);
7330 }
7331 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007332 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007333
7334 /* turn logging off, we are going to try to validate the value with all the types in order */
7335 hidden = *ly_vlog_hide_location();
7336 ly_vlog_hide(1);
7337
7338 t = NULL;
7339 found = 0;
7340 while ((t = lyp_get_next_union_type(type, t, &found))) {
7341 found = 0;
7342
7343 switch (t->base) {
7344 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007345 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7346 req_inst = -1;
7347 } else {
7348 req_inst = t->info.lref.req;
7349 }
7350
7351 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007352 if (store) {
7353 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7354 /* valid resolved */
7355 leaf->value.leafref = ret;
7356 leaf->value_type = LY_TYPE_LEAFREF;
7357 } else {
7358 /* valid unresolved */
7359 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, 1, 0)) {
7360 return -1;
7361 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007362 }
7363 }
7364
7365 success = 1;
7366 }
7367 break;
7368 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007369 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7370 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7371 req_inst = -1;
7372 } else {
7373 req_inst = t->info.inst.req;
7374 }
7375
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007376 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str),
7377 (ignore_fail ? -1 : t->info.inst.req), &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007378 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007379 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007380 /* valid resolved */
7381 leaf->value.instance = ret;
7382 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007383
Michal Vaskofd6c6502017-01-06 12:15:41 +01007384 if (json_val) {
7385 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7386 leaf->value_str = json_val;
7387 json_val = NULL;
7388 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007389 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007390 /* valid unresolved */
7391 if (json_val) {
7392 /* put the JSON val back */
7393 leaf->value.string = json_val;
7394 json_val = NULL;
7395 } else {
7396 leaf->value.instance = NULL;
7397 }
7398 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007399 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007400 }
7401
7402 success = 1;
7403 }
7404 break;
7405 default:
Michal Vaskofd6c6502017-01-06 12:15:41 +01007406 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, store, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007407 success = 1;
7408 }
7409 break;
7410 }
7411
7412 if (success) {
7413 break;
7414 }
7415
7416 /* erase information about errors - they are false or irrelevant
7417 * and will be replaced by a single error messages */
7418 ly_err_clean(1);
7419
7420 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007421 if (store) {
7422 if (t->base == LY_TYPE_BITS) {
7423 free(leaf->value.bit);
7424 }
7425 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007426 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007427 }
7428
7429 /* turn logging back on */
7430 if (!hidden) {
7431 ly_vlog_hide(0);
7432 }
7433
7434 if (json_val) {
7435 if (!success) {
7436 /* put the value back for now */
7437 assert(leaf->value_type == LY_TYPE_UNION);
7438 leaf->value.string = json_val;
7439 } else {
7440 /* value was ultimately useless, but we could not have known */
7441 lydict_remove(leaf->schema->module->ctx, json_val);
7442 }
7443 }
7444
Michal Vaskofd6c6502017-01-06 12:15:41 +01007445 if (success) {
7446 if (resolved_type) {
7447 *resolved_type = t;
7448 }
7449 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007450 /* not found and it is required */
7451 LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007452 return EXIT_FAILURE;
7453 }
7454
7455 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007456
Radek Krejci9b6aad22016-09-20 15:55:51 +02007457}
7458
Michal Vasko8bcdf292015-08-19 14:04:43 +02007459/**
7460 * @brief Resolve a single unres data item. Logs directly.
7461 *
Michal Vaskocf024702015-10-08 15:01:42 +02007462 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007463 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007464 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007465 *
7466 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7467 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007468int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007469resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007470{
Michal Vasko3cfa3182017-01-17 10:00:58 +01007471 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02007472 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007473 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007474 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007475
Michal Vasko83a6c462015-10-08 16:43:53 +02007476 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02007477 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007478
Michal Vaskocf024702015-10-08 15:01:42 +02007479 switch (type) {
7480 case UNRES_LEAFREF:
7481 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007482 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007483 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7484 req_inst = -1;
7485 } else {
7486 req_inst = sleaf->type.info.lref.req;
7487 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007488 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
7489 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01007490 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007491 /* valid resolved */
Michal Vasko1c8567a2017-01-05 13:42:27 +01007492 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7493 free(leaf->value.bit);
7494 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007495 leaf->value.leafref = ret;
7496 leaf->value_type = LY_TYPE_LEAFREF;
7497 } else {
7498 /* valid unresolved */
7499 if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) {
7500 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, 1, 0)) {
7501 return -1;
7502 }
7503 }
7504 }
7505 leaf->validity &= ~LYD_VAL_LEAFREF;
7506 } else {
7507 return rc;
7508 }
7509 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007510
Michal Vaskocf024702015-10-08 15:01:42 +02007511 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02007512 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007513 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
7514 if (ext_dep == -1) {
7515 return -1;
7516 }
7517
7518 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7519 req_inst = -1;
7520 } else {
7521 req_inst = sleaf->type.info.inst.req;
7522 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007523 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
7524 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007525 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007526 /* valid resolved */
7527 leaf->value.instance = ret;
7528 leaf->value_type = LY_TYPE_INST;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007529 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007530 /* valid unresolved */
7531 leaf->value.instance = NULL;
7532 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007533 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007534 } else {
7535 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007536 }
Michal Vaskocf024702015-10-08 15:01:42 +02007537 break;
7538
Radek Krejci7de36cf2016-09-12 16:18:50 +02007539 case UNRES_UNION:
7540 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007541 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007542
Michal Vaskocf024702015-10-08 15:01:42 +02007543 case UNRES_WHEN:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007544 if ((rc = resolve_when(node, NULL, ignore_fail))) {
Michal Vaskocf024702015-10-08 15:01:42 +02007545 return rc;
7546 }
7547 break;
7548
Michal Vaskobf19d252015-10-08 15:39:17 +02007549 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007550 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02007551 return rc;
7552 }
7553 break;
7554
7555 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007556 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02007557 return rc;
7558 }
7559 break;
7560
Michal Vaskocf024702015-10-08 15:01:42 +02007561 default:
Michal Vasko8bcdf292015-08-19 14:04:43 +02007562 LOGINT;
7563 return -1;
7564 }
7565
7566 return EXIT_SUCCESS;
7567}
7568
7569/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01007570 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02007571 *
7572 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02007573 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007574 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01007575 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007576 */
7577int
Radek Krejci0b7704f2016-03-18 12:16:14 +01007578unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007579{
Radek Krejci03b71f72016-03-16 11:10:09 +01007580 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02007581 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02007582 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02007583
Radek Krejci03b71f72016-03-16 11:10:09 +01007584 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007585 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
7586 if (!unres->node) {
7587 LOGMEM;
7588 return -1;
7589 }
Michal Vaskocf024702015-10-08 15:01:42 +02007590 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01007591 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
7592 if (!unres->type) {
7593 LOGMEM;
7594 return -1;
7595 }
Michal Vaskocf024702015-10-08 15:01:42 +02007596 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007597
Radek Krejci0b7704f2016-03-18 12:16:14 +01007598 if (type == UNRES_WHEN) {
7599 /* remove previous result */
7600 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007601 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007602
7603 return EXIT_SUCCESS;
7604}
7605
7606/**
7607 * @brief Resolve every unres data item in the structure. Logs directly.
7608 *
Radek Krejci082c84f2016-10-17 16:33:06 +02007609 * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected,
7610 * unresolved leafrefs/instids are accepted).
7611 *
7612 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
7613 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007614 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02007615 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
7616 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007617 *
7618 * @return EXIT_SUCCESS on success, -1 on error.
7619 */
7620int
Radek Krejci082c84f2016-10-17 16:33:06 +02007621resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007622{
Radek Krejci0c0086a2016-03-24 15:20:28 +01007623 uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007624 int rc, progress, ignore_fail;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007625 struct lyd_node *parent;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007626
Radek Krejci082c84f2016-10-17 16:33:06 +02007627 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01007628 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01007629
7630 if (!unres->count) {
7631 return EXIT_SUCCESS;
7632 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007633
Michal Vaskoad2e44a2017-01-03 10:31:35 +01007634 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 +01007635 ignore_fail = 1;
7636 } else if (options & LYD_OPT_NOEXTDEPS) {
7637 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007638 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007639 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007640 }
7641
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007642 LOGVRB("Resolving unresolved data nodes and their constraints...");
Radek Krejci010e54b2016-03-15 09:40:34 +01007643 ly_vlog_hide(1);
7644
Radek Krejci0b7704f2016-03-18 12:16:14 +01007645 /* when-stmt first */
Radek Krejci010e54b2016-03-15 09:40:34 +01007646 do {
Radek Krejci00a0e712016-10-26 10:24:46 +02007647 ly_err_clean(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007648 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02007649 for (i = 0; i < unres->count; i++) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007650 if (unres->type[i] != UNRES_WHEN) {
7651 continue;
7652 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007653 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007654 /* count when-stmt nodes in unres list */
7655 when_stmt++;
7656 }
7657
7658 /* resolve when condition only when all parent when conditions are already resolved */
7659 for (parent = unres->node[i]->parent;
7660 parent && LYD_WHEN_DONE(parent->when_status);
7661 parent = parent->parent) {
7662 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
7663 /* the parent node was already unlinked, do not resolve this node,
7664 * it will be removed anyway, so just mark it as resolved
7665 */
7666 unres->node[i]->when_status |= LYD_WHEN_FALSE;
7667 unres->type[i] = UNRES_RESOLVED;
7668 resolved++;
7669 break;
7670 }
7671 }
7672 if (parent) {
7673 continue;
7674 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007675
Michal Vasko3cfa3182017-01-17 10:00:58 +01007676 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Radek Krejci010e54b2016-03-15 09:40:34 +01007677 if (!rc) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007678 if (unres->node[i]->when_status & LYD_WHEN_FALSE) {
Radek Krejci082c84f2016-10-17 16:33:06 +02007679 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01007680 /* false when condition */
7681 ly_vlog_hide(0);
Radek Krejci2467a492016-10-24 15:16:59 +02007682 ly_err_repeat();
Radek Krejci03b71f72016-03-16 11:10:09 +01007683 return -1;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007684 } /* follows else */
7685
Radek Krejci0c0086a2016-03-24 15:20:28 +01007686 /* only unlink now, the subtree can contain another nodes stored in the unres list */
7687 /* if it has parent non-presence containers that would be empty, we should actually
7688 * remove the container
7689 */
Radek Krejci2537fd32016-09-07 16:22:41 +02007690 for (parent = unres->node[i];
7691 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
7692 parent = parent->parent) {
7693 if (((struct lys_node_container *)parent->parent->schema)->presence) {
7694 /* presence container */
7695 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007696 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007697 if (parent->next || parent->prev != parent) {
7698 /* non empty (the child we are in and we are going to remove is not the only child) */
7699 break;
7700 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007701 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007702 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007703
Radek Krejci0b7704f2016-03-18 12:16:14 +01007704 /* auto-delete */
7705 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(),
7706 ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond);
Radek Krejci0c0086a2016-03-24 15:20:28 +01007707 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007708 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01007709 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007710
Radek Krejci0b7704f2016-03-18 12:16:14 +01007711 lyd_unlink(unres->node[i]);
7712 unres->type[i] = UNRES_DELETE;
7713 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01007714
7715 /* update the rest of unres items */
7716 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01007717 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01007718 continue;
7719 }
7720
7721 /* test if the node is in subtree to be deleted */
7722 for (parent = unres->node[j]; parent; parent = parent->parent) {
7723 if (parent == unres->node[i]) {
7724 /* yes, it is */
7725 unres->type[j] = UNRES_RESOLVED;
7726 resolved++;
7727 break;
7728 }
7729 }
7730 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007731 } else {
7732 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01007733 }
Radek Krejci00a0e712016-10-26 10:24:46 +02007734 ly_err_clean(1);
Radek Krejci010e54b2016-03-15 09:40:34 +01007735 resolved++;
7736 progress = 1;
7737 } else if (rc == -1) {
7738 ly_vlog_hide(0);
Michal Vasko76e73402016-08-24 16:00:13 +02007739 /* print only this last error */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007740 resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Radek Krejci010e54b2016-03-15 09:40:34 +01007741 return -1;
Radek Krejci2467a492016-10-24 15:16:59 +02007742 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01007743 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007744 first = 0;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007745 } while (progress && resolved < when_stmt);
Radek Krejci010e54b2016-03-15 09:40:34 +01007746
Radek Krejci0b7704f2016-03-18 12:16:14 +01007747 /* do we have some unresolved when-stmt? */
Radek Krejcid940d732016-03-24 16:02:28 +01007748 if (when_stmt > resolved) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007749 ly_vlog_hide(0);
Radek Krejci2467a492016-10-24 15:16:59 +02007750 ly_err_repeat();
Radek Krejci0b7704f2016-03-18 12:16:14 +01007751 return -1;
7752 }
7753
7754 for (i = 0; del_items && i < unres->count; i++) {
7755 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
7756 if (unres->type[i] != UNRES_DELETE) {
7757 continue;
7758 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007759 if (!unres->node[i]) {
7760 unres->type[i] = UNRES_RESOLVED;
7761 del_items--;
7762 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007763 }
7764
7765 /* really remove the complete subtree */
7766 lyd_free(unres->node[i]);
7767 unres->type[i] = UNRES_RESOLVED;
7768 del_items--;
7769 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007770 ly_vlog_hide(0);
Radek Krejci010e54b2016-03-15 09:40:34 +01007771
7772 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007773 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007774 if (unres->type[i] == UNRES_RESOLVED) {
7775 continue;
7776 }
Radek Krejci082c84f2016-10-17 16:33:06 +02007777 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01007778
Michal Vasko3cfa3182017-01-17 10:00:58 +01007779 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007780 if (rc) {
7781 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007782 return -1;
7783 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007784
7785 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007786 }
7787
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007788 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01007789 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007790 return EXIT_SUCCESS;
7791}