blob: aeb3a88f69d50cfa0089b24c10874e3c58f2040e [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 *
Michal Vasko53b7da02018-02-13 15:28:42 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Michal Vasko730dfdf2015-08-11 14:48:05 +02007 *
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;
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020039 int64_t ret = 0, prev_ret;
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 {
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020068 prev_ret = ret;
69 if (minus) {
70 ret = ret * 10 - (ptr[0] - '0');
71 if (ret > prev_ret) {
72 return 1;
73 }
74 } else {
75 ret = ret * 10 + (ptr[0] - '0');
76 if (ret < prev_ret) {
77 return 1;
78 }
79 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020080 if (str_dig > -1) {
81 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010082 if (ptr[0] == '0') {
83 /* possibly trailing zero */
84 trailing_zeros++;
85 } else {
86 trailing_zeros = 0;
87 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020088 }
89 ++str_exp;
90 }
91 }
Michal Vaskod24dd012016-09-30 12:20:22 +020092 if (str_dig == 0) {
93 /* no digits after '.' */
94 return 1;
95 } else if (str_dig == -1) {
96 /* there are 0 numbers after the floating point */
Michal Vasko4d1f0482016-09-19 14:35:06 +020097 str_dig = 0;
98 }
Radek Krejcibf47a822016-11-04 10:06:08 +010099 /* remove trailing zeros */
100 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +0100101 str_dig -= trailing_zeros;
102 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +0100103 ret = ret / dec_pow(trailing_zeros);
104 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200105
106 /* it's parsed, now adjust the number based on fraction-digits, if needed */
107 if (str_dig < dig) {
108 if ((str_exp - 1) + (dig - str_dig) > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200109 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200110 }
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200111 prev_ret = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200112 ret *= dec_pow(dig - str_dig);
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200113 if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) {
114 return 1;
115 }
116
Michal Vasko4d1f0482016-09-19 14:35:06 +0200117 }
118 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200119 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200120 }
121
Michal Vasko4d1f0482016-09-19 14:35:06 +0200122 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200123 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200124
Michal Vaskod24dd012016-09-30 12:20:22 +0200125 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200126}
127
128/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200129 * @brief Parse an identifier.
130 *
131 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
132 * identifier = (ALPHA / "_")
133 * *(ALPHA / DIGIT / "_" / "-" / ".")
134 *
Michal Vaskobb211122015-08-19 14:03:11 +0200135 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200136 *
137 * @return Number of characters successfully parsed.
138 */
Radek Krejcidce5f972017-09-12 15:47:49 +0200139unsigned int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200140parse_identifier(const char *id)
141{
Radek Krejcidce5f972017-09-12 15:47:49 +0200142 unsigned int parsed = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200143
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100144 assert(id);
145
Radek Krejci6dc53a22015-08-17 13:27:59 +0200146 if (!isalpha(id[0]) && (id[0] != '_')) {
147 return -parsed;
148 }
149
150 ++parsed;
151 ++id;
152
153 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
154 ++parsed;
155 ++id;
156 }
157
158 return parsed;
159}
160
161/**
162 * @brief Parse a node-identifier.
163 *
Michal Vasko723e50c2015-10-20 15:20:29 +0200164 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200165 *
Michal Vaskobb211122015-08-19 14:03:11 +0200166 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200167 * @param[out] mod_name Points to the module name, NULL if there is not any.
168 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200169 * @param[out] name Points to the node name.
170 * @param[out] nam_len Length of the node name.
Michal Vasko50576712017-07-28 12:28:33 +0200171 * @param[out] all_desc Whether the path starts with '/', only supported in extended paths.
172 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200173 *
174 * @return Number of characters successfully parsed,
175 * positive on success, negative on failure.
176 */
177static int
Michal Vasko50576712017-07-28 12:28:33 +0200178parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
179 int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200180{
181 int parsed = 0, ret;
182
183 assert(id);
Michal Vasko50576712017-07-28 12:28:33 +0200184 assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len));
185 assert((name && nam_len) || (!name && !nam_len));
186 assert(!extended || all_desc);
187
Michal Vasko723e50c2015-10-20 15:20:29 +0200188 if (mod_name) {
189 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200190 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200191 }
192 if (name) {
193 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200194 *nam_len = 0;
195 }
196
Michal Vasko50576712017-07-28 12:28:33 +0200197 if (extended) {
198 /* try to parse only the extended expressions */
199 if (id[parsed] == '/') {
200 *all_desc = 1;
201 } else {
202 *all_desc = 0;
203 }
204
205 /* is there a prefix? */
206 ret = parse_identifier(id + *all_desc);
207 if (ret > 0) {
208 if (id[*all_desc + ret] != ':') {
209 /* this is not a prefix, so not an extended id */
210 goto standard_id;
211 }
212
213 if (mod_name) {
214 *mod_name = id + *all_desc;
215 *mod_name_len = ret;
216 }
217
218 /* "/" and ":" */
219 ret += *all_desc + 1;
220 } else {
221 ret = *all_desc;
222 }
223
224 /* parse either "*" or "." */
225 if (!strcmp(id + ret, "*")) {
226 if (name) {
227 *name = id + ret;
228 *nam_len = 1;
229 }
230 ++ret;
231
232 return ret;
233 } else if (!strcmp(id + ret, ".")) {
234 if (!*all_desc) {
235 /* /. is redundant expression, we do not accept it */
236 return -ret;
237 }
238
239 if (name) {
240 *name = id + ret;
241 *nam_len = 1;
242 }
243 ++ret;
244
245 return ret;
246 }
247 /* else a standard id, parse it all again */
248 }
249
250standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200251 if ((ret = parse_identifier(id)) < 1) {
252 return ret;
253 }
254
Michal Vasko723e50c2015-10-20 15:20:29 +0200255 if (mod_name) {
256 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200257 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200258 }
259
260 parsed += ret;
261 id += ret;
262
263 /* there is prefix */
264 if (id[0] == ':') {
265 ++parsed;
266 ++id;
267
268 /* there isn't */
269 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200270 if (name && mod_name) {
271 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200272 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200273 if (mod_name) {
274 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200275 }
276
Michal Vasko723e50c2015-10-20 15:20:29 +0200277 if (nam_len && mod_name_len) {
278 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200279 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200280 if (mod_name_len) {
281 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200282 }
283
284 return parsed;
285 }
286
287 /* identifier (node name) */
288 if ((ret = parse_identifier(id)) < 1) {
289 return -parsed+ret;
290 }
291
292 if (name) {
293 *name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200294 *nam_len = ret;
295 }
296
297 return parsed+ret;
298}
299
300/**
301 * @brief Parse a path-predicate (leafref).
302 *
303 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
304 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
305 *
Michal Vaskobb211122015-08-19 14:03:11 +0200306 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200307 * @param[out] prefix Points to the prefix, NULL if there is not any.
308 * @param[out] pref_len Length of the prefix, 0 if there is not any.
309 * @param[out] name Points to the node name.
310 * @param[out] nam_len Length of the node name.
311 * @param[out] path_key_expr Points to the path-key-expr.
312 * @param[out] pke_len Length of the path-key-expr.
313 * @param[out] has_predicate Flag to mark whether there is another predicate following.
314 *
315 * @return Number of characters successfully parsed,
316 * positive on success, negative on failure.
317 */
318static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200319parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
320 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200321{
322 const char *ptr;
323 int parsed = 0, ret;
324
325 assert(id);
326 if (prefix) {
327 *prefix = NULL;
328 }
329 if (pref_len) {
330 *pref_len = 0;
331 }
332 if (name) {
333 *name = NULL;
334 }
335 if (nam_len) {
336 *nam_len = 0;
337 }
338 if (path_key_expr) {
339 *path_key_expr = NULL;
340 }
341 if (pke_len) {
342 *pke_len = 0;
343 }
344 if (has_predicate) {
345 *has_predicate = 0;
346 }
347
348 if (id[0] != '[') {
349 return -parsed;
350 }
351
352 ++parsed;
353 ++id;
354
355 while (isspace(id[0])) {
356 ++parsed;
357 ++id;
358 }
359
Michal Vasko50576712017-07-28 12:28:33 +0200360 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200361 return -parsed+ret;
362 }
363
364 parsed += ret;
365 id += ret;
366
367 while (isspace(id[0])) {
368 ++parsed;
369 ++id;
370 }
371
372 if (id[0] != '=') {
373 return -parsed;
374 }
375
376 ++parsed;
377 ++id;
378
379 while (isspace(id[0])) {
380 ++parsed;
381 ++id;
382 }
383
384 if ((ptr = strchr(id, ']')) == NULL) {
385 return -parsed;
386 }
387
388 --ptr;
389 while (isspace(ptr[0])) {
390 --ptr;
391 }
392 ++ptr;
393
394 ret = ptr-id;
395 if (path_key_expr) {
396 *path_key_expr = id;
397 }
398 if (pke_len) {
399 *pke_len = ret;
400 }
401
402 parsed += ret;
403 id += ret;
404
405 while (isspace(id[0])) {
406 ++parsed;
407 ++id;
408 }
409
410 assert(id[0] == ']');
411
412 if (id[1] == '[') {
413 *has_predicate = 1;
414 }
415
416 return parsed+1;
417}
418
419/**
420 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
421 * the ".." and the first node-identifier, other calls parse a single
422 * node-identifier each.
423 *
424 * path-key-expr = current-function-invocation *WSP "/" *WSP
425 * rel-path-keyexpr
426 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
427 * *(node-identifier *WSP "/" *WSP)
428 * node-identifier
429 *
Michal Vaskobb211122015-08-19 14:03:11 +0200430 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200431 * @param[out] prefix Points to the prefix, NULL if there is not any.
432 * @param[out] pref_len Length of the prefix, 0 if there is not any.
433 * @param[out] name Points to the node name.
434 * @param[out] nam_len Length of the node name.
435 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
436 * must not be changed between consecutive calls.
437 * @return Number of characters successfully parsed,
438 * positive on success, negative on failure.
439 */
440static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200441parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
442 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200443{
444 int parsed = 0, ret, par_times = 0;
445
446 assert(id);
447 assert(parent_times);
448 if (prefix) {
449 *prefix = NULL;
450 }
451 if (pref_len) {
452 *pref_len = 0;
453 }
454 if (name) {
455 *name = NULL;
456 }
457 if (nam_len) {
458 *nam_len = 0;
459 }
460
461 if (!*parent_times) {
462 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
463 if (strncmp(id, "current()", 9)) {
464 return -parsed;
465 }
466
467 parsed += 9;
468 id += 9;
469
470 while (isspace(id[0])) {
471 ++parsed;
472 ++id;
473 }
474
475 if (id[0] != '/') {
476 return -parsed;
477 }
478
479 ++parsed;
480 ++id;
481
482 while (isspace(id[0])) {
483 ++parsed;
484 ++id;
485 }
486
487 /* rel-path-keyexpr */
488 if (strncmp(id, "..", 2)) {
489 return -parsed;
490 }
491 ++par_times;
492
493 parsed += 2;
494 id += 2;
495
496 while (isspace(id[0])) {
497 ++parsed;
498 ++id;
499 }
500 }
501
502 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
503 *
504 * first parent reference with whitespaces already parsed
505 */
506 if (id[0] != '/') {
507 return -parsed;
508 }
509
510 ++parsed;
511 ++id;
512
513 while (isspace(id[0])) {
514 ++parsed;
515 ++id;
516 }
517
518 while (!strncmp(id, "..", 2) && !*parent_times) {
519 ++par_times;
520
521 parsed += 2;
522 id += 2;
523
524 while (isspace(id[0])) {
525 ++parsed;
526 ++id;
527 }
528
529 if (id[0] != '/') {
530 return -parsed;
531 }
532
533 ++parsed;
534 ++id;
535
536 while (isspace(id[0])) {
537 ++parsed;
538 ++id;
539 }
540 }
541
542 if (!*parent_times) {
543 *parent_times = par_times;
544 }
545
546 /* all parent references must be parsed at this point */
Michal Vasko50576712017-07-28 12:28:33 +0200547 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
548 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200549 }
550
551 parsed += ret;
552 id += ret;
553
554 return parsed;
555}
556
557/**
558 * @brief Parse path-arg (leafref).
559 *
560 * path-arg = absolute-path / relative-path
561 * absolute-path = 1*("/" (node-identifier *path-predicate))
562 * relative-path = 1*(".." "/") descendant-path
563 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200564 * @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 +0200565 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200566 * @param[out] prefix Points to the prefix, NULL if there is not any.
567 * @param[out] pref_len Length of the prefix, 0 if there is not any.
568 * @param[out] name Points to the node name.
569 * @param[out] nam_len Length of the node name.
570 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
571 * must not be changed between consecutive calls. -1 if the
572 * path is relative.
573 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
574 *
575 * @return Number of characters successfully parsed,
576 * positive on success, negative on failure.
577 */
578static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200579parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200580 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200581{
582 int parsed = 0, ret, par_times = 0;
583
584 assert(id);
585 assert(parent_times);
586 if (prefix) {
587 *prefix = NULL;
588 }
589 if (pref_len) {
590 *pref_len = 0;
591 }
592 if (name) {
593 *name = NULL;
594 }
595 if (nam_len) {
596 *nam_len = 0;
597 }
598 if (has_predicate) {
599 *has_predicate = 0;
600 }
601
602 if (!*parent_times && !strncmp(id, "..", 2)) {
603 ++par_times;
604
605 parsed += 2;
606 id += 2;
607
608 while (!strncmp(id, "/..", 3)) {
609 ++par_times;
610
611 parsed += 3;
612 id += 3;
613 }
614 }
615
616 if (!*parent_times) {
617 if (par_times) {
618 *parent_times = par_times;
619 } else {
620 *parent_times = -1;
621 }
622 }
623
624 if (id[0] != '/') {
625 return -parsed;
626 }
627
628 /* skip '/' */
629 ++parsed;
630 ++id;
631
632 /* node-identifier ([prefix:]identifier) */
Michal Vasko50576712017-07-28 12:28:33 +0200633 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
634 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200635 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200636 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200637 /* actually we always need prefix even it is not specified */
638 *prefix = lys_main_module(mod)->name;
639 *pref_len = strlen(*prefix);
640 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200641
642 parsed += ret;
643 id += ret;
644
645 /* there is no predicate */
646 if ((id[0] == '/') || !id[0]) {
647 return parsed;
648 } else if (id[0] != '[') {
649 return -parsed;
650 }
651
652 if (has_predicate) {
653 *has_predicate = 1;
654 }
655
656 return parsed;
657}
658
659/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200660 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200661 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200662 *
663 * instance-identifier = 1*("/" (node-identifier *predicate))
664 *
Michal Vaskobb211122015-08-19 14:03:11 +0200665 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200666 * @param[out] model Points to the model name.
667 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200668 * @param[out] name Points to the node name.
669 * @param[out] nam_len Length of the node name.
670 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
671 *
672 * @return Number of characters successfully parsed,
673 * positive on success, negative on failure.
674 */
675static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200676parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
677 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200678{
679 int parsed = 0, ret;
680
Michal Vasko1b6ca962017-08-03 14:23:09 +0200681 assert(id && model && mod_len && name && nam_len);
682
Radek Krejci6dc53a22015-08-17 13:27:59 +0200683 if (has_predicate) {
684 *has_predicate = 0;
685 }
686
687 if (id[0] != '/') {
688 return -parsed;
689 }
690
691 ++parsed;
692 ++id;
693
Michal Vaskob2f40be2016-09-08 16:03:48 +0200694 if ((ret = parse_identifier(id)) < 1) {
695 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200696 }
697
Michal Vaskob2f40be2016-09-08 16:03:48 +0200698 *name = id;
699 *nam_len = ret;
700
701 parsed += ret;
702 id += ret;
703
Michal Vasko1b6ca962017-08-03 14:23:09 +0200704 if (id[0] == ':') {
705 /* we have prefix */
706 *model = *name;
707 *mod_len = *nam_len;
708
709 ++parsed;
710 ++id;
711
712 if ((ret = parse_identifier(id)) < 1) {
713 return ret;
714 }
715
716 *name = id;
717 *nam_len = ret;
718
719 parsed += ret;
720 id += ret;
721 }
722
Radek Krejci4967cb62016-09-14 16:40:28 +0200723 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200724 *has_predicate = 1;
725 }
726
727 return parsed;
728}
729
730/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200731 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200732 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200733 *
734 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
735 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
736 * ((DQUOTE string DQUOTE) /
737 * (SQUOTE string SQUOTE))
738 * pos = non-negative-integer-value
739 *
Michal Vaskobb211122015-08-19 14:03:11 +0200740 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200741 * @param[out] model Points to the model name.
742 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200743 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
744 * @param[out] nam_len Length of the node name.
745 * @param[out] value Value the node-identifier must have (string from the grammar),
746 * NULL if there is not any.
747 * @param[out] val_len Length of the value, 0 if there is not any.
748 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
749 *
750 * @return Number of characters successfully parsed,
751 * positive on success, negative on failure.
752 */
753static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200754parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
755 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200756{
757 const char *ptr;
758 int parsed = 0, ret;
759 char quote;
760
761 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200762 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200763 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200764 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200765 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200766 }
767 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200768 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200769 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200770 *nam_len = 0;
771 }
772 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200773 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200774 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200775 *val_len = 0;
776 }
777 if (has_predicate) {
778 *has_predicate = 0;
779 }
780
781 if (id[0] != '[') {
782 return -parsed;
783 }
784
785 ++parsed;
786 ++id;
787
788 while (isspace(id[0])) {
789 ++parsed;
790 ++id;
791 }
792
793 /* pos */
794 if (isdigit(id[0])) {
795 if (name) {
796 *name = id;
797 }
798
799 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200800 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200801 }
802
803 while (isdigit(id[0])) {
804 ++parsed;
805 ++id;
806 }
807
808 if (nam_len) {
809 *nam_len = id-(*name);
810 }
811
Michal Vaskof2f28a12016-09-09 12:43:06 +0200812 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200813 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200814 if (id[0] == '.') {
815 if (name) {
816 *name = id;
817 }
818 if (nam_len) {
819 *nam_len = 1;
820 }
821
822 ++parsed;
823 ++id;
824
825 } else {
Michal Vasko50576712017-07-28 12:28:33 +0200826 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200827 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200828 }
829
830 parsed += ret;
831 id += ret;
832 }
833
834 while (isspace(id[0])) {
835 ++parsed;
836 ++id;
837 }
838
839 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200840 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200841 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200842
Radek Krejci6dc53a22015-08-17 13:27:59 +0200843 ++parsed;
844 ++id;
845
Michal Vaskof2f28a12016-09-09 12:43:06 +0200846 while (isspace(id[0])) {
847 ++parsed;
848 ++id;
849 }
850
851 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
852 if ((id[0] == '\"') || (id[0] == '\'')) {
853 quote = id[0];
854
855 ++parsed;
856 ++id;
857
858 if ((ptr = strchr(id, quote)) == NULL) {
859 return -parsed;
860 }
Michal Vasko1b6ca962017-08-03 14:23:09 +0200861 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200862
863 if (value) {
864 *value = id;
865 }
866 if (val_len) {
867 *val_len = ret;
868 }
869
Michal Vasko1b6ca962017-08-03 14:23:09 +0200870 parsed += ret + 1;
871 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200872 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200873 return -parsed;
874 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200875 }
876
877 while (isspace(id[0])) {
878 ++parsed;
879 ++id;
880 }
881
882 if (id[0] != ']') {
883 return -parsed;
884 }
885
886 ++parsed;
887 ++id;
888
889 if ((id[0] == '[') && has_predicate) {
890 *has_predicate = 1;
891 }
892
893 return parsed;
894}
895
896/**
897 * @brief Parse schema-nodeid.
898 *
899 * schema-nodeid = absolute-schema-nodeid /
900 * descendant-schema-nodeid
901 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200902 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200903 * node-identifier
904 * absolute-schema-nodeid
905 *
Michal Vaskobb211122015-08-19 14:03:11 +0200906 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200907 * @param[out] mod_name Points to the module name, NULL if there is not any.
908 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200909 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200910 * @param[out] nam_len Length of the node name.
911 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
912 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100913 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
914 * based on the grammar, in those cases use NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200915 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200916 *
917 * @return Number of characters successfully parsed,
918 * positive on success, negative on failure.
919 */
Michal Vasko22448d32016-03-16 13:17:29 +0100920int
Michal Vasko723e50c2015-10-20 15:20:29 +0200921parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
Michal Vasko50576712017-07-28 12:28:33 +0200922 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200923{
924 int parsed = 0, ret;
925
926 assert(id);
927 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200928
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100929 if (has_predicate) {
930 *has_predicate = 0;
931 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200932
933 if (id[0] != '/') {
934 if (*is_relative != -1) {
935 return -parsed;
936 } else {
937 *is_relative = 1;
938 }
Michal Vasko48935352016-03-29 11:52:36 +0200939 if (!strncmp(id, "./", 2)) {
940 parsed += 2;
941 id += 2;
942 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200943 } else {
944 if (*is_relative == -1) {
945 *is_relative = 0;
946 }
947 ++parsed;
948 ++id;
949 }
950
Michal Vasko50576712017-07-28 12:28:33 +0200951 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
952 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200953 }
954
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100955 parsed += ret;
956 id += ret;
957
958 if ((id[0] == '[') && has_predicate) {
959 *has_predicate = 1;
960 }
961
962 return parsed;
963}
964
965/**
966 * @brief Parse schema predicate (special format internally used).
967 *
968 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200969 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100970 * key-with-value = identifier *WSP "=" *WSP
971 * ((DQUOTE string DQUOTE) /
972 * (SQUOTE string SQUOTE))
973 *
974 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200975 * @param[out] mod_name Points to the list key module name.
976 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100977 * @param[out] name Points to the list key name.
978 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +0100979 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100980 * @param[out] val_len Length of \p value.
981 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
982 */
Michal Vasko22448d32016-03-16 13:17:29 +0100983int
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200984parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
985 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100986{
987 const char *ptr;
988 int parsed = 0, ret;
989 char quote;
990
991 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200992 if (mod_name) {
993 *mod_name = NULL;
994 }
995 if (mod_name_len) {
996 *mod_name_len = 0;
997 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100998 if (name) {
999 *name = NULL;
1000 }
1001 if (nam_len) {
1002 *nam_len = 0;
1003 }
1004 if (value) {
1005 *value = NULL;
1006 }
1007 if (val_len) {
1008 *val_len = 0;
1009 }
1010 if (has_predicate) {
1011 *has_predicate = 0;
1012 }
1013
1014 if (id[0] != '[') {
1015 return -parsed;
1016 }
1017
1018 ++parsed;
1019 ++id;
1020
1021 while (isspace(id[0])) {
1022 ++parsed;
1023 ++id;
1024 }
1025
Michal Vasko22448d32016-03-16 13:17:29 +01001026 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001027 if (id[0] == '.') {
1028 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001029
1030 if (name) {
1031 *name = id;
1032 }
1033 if (nam_len) {
1034 *nam_len = ret;
1035 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001036 } else if (isdigit(id[0])) {
1037 if (id[0] == '0') {
1038 return -parsed;
1039 }
1040 ret = 1;
1041 while (isdigit(id[ret])) {
1042 ++ret;
1043 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001044
1045 if (name) {
1046 *name = id;
1047 }
1048 if (nam_len) {
1049 *nam_len = ret;
1050 }
Michal Vasko50576712017-07-28 12:28:33 +02001051 } else if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001052 return -parsed + ret;
1053 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001054
1055 parsed += ret;
1056 id += ret;
1057
1058 while (isspace(id[0])) {
1059 ++parsed;
1060 ++id;
1061 }
1062
1063 /* there is value as well */
1064 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001065 if (name && isdigit(**name)) {
1066 return -parsed;
1067 }
1068
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001069 ++parsed;
1070 ++id;
1071
1072 while (isspace(id[0])) {
1073 ++parsed;
1074 ++id;
1075 }
1076
1077 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1078 if ((id[0] == '\"') || (id[0] == '\'')) {
1079 quote = id[0];
1080
1081 ++parsed;
1082 ++id;
1083
1084 if ((ptr = strchr(id, quote)) == NULL) {
1085 return -parsed;
1086 }
1087 ret = ptr - id;
1088
1089 if (value) {
1090 *value = id;
1091 }
1092 if (val_len) {
1093 *val_len = ret;
1094 }
1095
1096 parsed += ret + 1;
1097 id += ret + 1;
1098 } else {
1099 return -parsed;
1100 }
1101
1102 while (isspace(id[0])) {
1103 ++parsed;
1104 ++id;
1105 }
1106 }
1107
1108 if (id[0] != ']') {
1109 return -parsed;
1110 }
1111
1112 ++parsed;
1113 ++id;
1114
1115 if ((id[0] == '[') && has_predicate) {
1116 *has_predicate = 1;
1117 }
1118
1119 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001120}
1121
1122/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001123 * @brief Resolve (find) a feature definition. Logs directly.
1124 *
1125 * @param[in] feat_name Feature name to resolve.
1126 * @param[in] len Length of \p feat_name.
1127 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001128 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1129 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001130 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001131 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001132 */
1133static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001134resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001135{
1136 char *str;
1137 const char *mod_name, *name;
1138 int mod_name_len, nam_len, i, j;
1139 const struct lys_module *module;
1140
Radek Krejci9ff0a922016-07-14 13:08:05 +02001141 assert(feature);
1142
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001143 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001144 if ((i = parse_node_identifier(feat_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001145 LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001146 return -1;
1147 }
1148
Michal Vasko921eb6b2017-10-13 10:01:39 +02001149 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001150 if (!module) {
1151 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01001152 LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001153 return -1;
1154 }
1155
Radek Krejci9ff0a922016-07-14 13:08:05 +02001156 if (module != node->module && module == lys_node_module(node)) {
1157 /* first, try to search directly in submodule where the feature was mentioned */
1158 for (j = 0; j < node->module->features_size; j++) {
1159 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1160 /* check status */
1161 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001162 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001163 return -1;
1164 }
1165 *feature = &node->module->features[j];
1166 return 0;
1167 }
1168 }
1169 }
1170
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001171 /* search in the identified module ... */
1172 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001173 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001174 /* check status */
1175 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001176 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001177 return -1;
1178 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001179 *feature = &module->features[j];
1180 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001181 }
1182 }
1183 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001184 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001185 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001186 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1187 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001188 /* check status */
1189 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1190 module->inc[i].submodule->features[j].flags,
1191 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001192 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001193 return -1;
1194 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001195 *feature = &module->inc[i].submodule->features[j];
1196 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001197 }
1198 }
1199 }
1200
1201 /* not found */
1202 str = strndup(feat_name, len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001203 LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001204 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001205 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001206}
1207
Radek Krejci9ff0a922016-07-14 13:08:05 +02001208/*
1209 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001210 * - 1 if enabled
1211 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001212 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001213static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001214resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001215{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001216 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001217
Radek Krejci9ff0a922016-07-14 13:08:05 +02001218 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001219 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001220 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001221 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001222 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001223
Radek Krejci69b8d922016-07-27 13:13:41 +02001224 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001225}
1226
1227static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001228resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001229{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001230 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001231 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001232
Radek Krejci9ff0a922016-07-14 13:08:05 +02001233 op = iff_getop(expr->expr, *index_e);
1234 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001235
Radek Krejci9ff0a922016-07-14 13:08:05 +02001236 switch (op) {
1237 case LYS_IFF_F:
1238 /* resolve feature */
1239 return resolve_feature_value(expr->features[(*index_f)++]);
1240 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001241 /* invert result */
1242 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001243 case LYS_IFF_AND:
1244 case LYS_IFF_OR:
1245 a = resolve_iffeature_recursive(expr, index_e, index_f);
1246 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001247 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001248 return a && b;
1249 } else { /* LYS_IFF_OR */
1250 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001251 }
1252 }
1253
Radek Krejciaf566332017-02-07 15:56:59 +01001254 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001255}
1256
1257int
1258resolve_iffeature(struct lys_iffeature *expr)
1259{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001260 int index_e = 0, index_f = 0;
1261
1262 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001263 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001264 }
Radek Krejciaf566332017-02-07 15:56:59 +01001265 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001266}
1267
1268struct iff_stack {
1269 int size;
1270 int index; /* first empty item */
1271 uint8_t *stack;
1272};
1273
1274static int
1275iff_stack_push(struct iff_stack *stack, uint8_t value)
1276{
1277 if (stack->index == stack->size) {
1278 stack->size += 4;
1279 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001280 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001281 }
1282
1283 stack->stack[stack->index++] = value;
1284 return EXIT_SUCCESS;
1285}
1286
1287static uint8_t
1288iff_stack_pop(struct iff_stack *stack)
1289{
1290 stack->index--;
1291 return stack->stack[stack->index];
1292}
1293
1294static void
1295iff_stack_clean(struct iff_stack *stack)
1296{
1297 stack->size = 0;
1298 free(stack->stack);
1299}
1300
1301static void
1302iff_setop(uint8_t *list, uint8_t op, int pos)
1303{
1304 uint8_t *item;
1305 uint8_t mask = 3;
1306
1307 assert(pos >= 0);
1308 assert(op <= 3); /* max 2 bits */
1309
1310 item = &list[pos / 4];
1311 mask = mask << 2 * (pos % 4);
1312 *item = (*item) & ~mask;
1313 *item = (*item) | (op << 2 * (pos % 4));
1314}
1315
1316uint8_t
1317iff_getop(uint8_t *list, int pos)
1318{
1319 uint8_t *item;
1320 uint8_t mask = 3, result;
1321
1322 assert(pos >= 0);
1323
1324 item = &list[pos / 4];
1325 result = (*item) & (mask << 2 * (pos % 4));
1326 return result >> 2 * (pos % 4);
1327}
1328
1329#define LYS_IFF_LP 0x04 /* ( */
1330#define LYS_IFF_RP 0x08 /* ) */
1331
Radek Krejcicbb473e2016-09-16 14:48:32 +02001332/* internal structure for passing data for UNRES_IFFEAT */
1333struct unres_iffeat_data {
1334 struct lys_node *node;
1335 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001336 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001337};
1338
Radek Krejci9ff0a922016-07-14 13:08:05 +02001339void
1340resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1341{
1342 unsigned int e = 0, f = 0, r = 0;
1343 uint8_t op;
1344
1345 assert(iffeat);
1346
1347 if (!iffeat->expr) {
1348 goto result;
1349 }
1350
1351 do {
1352 op = iff_getop(iffeat->expr, e++);
1353 switch (op) {
1354 case LYS_IFF_NOT:
1355 if (!r) {
1356 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001357 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001358 break;
1359 case LYS_IFF_AND:
1360 case LYS_IFF_OR:
1361 if (!r) {
1362 r += 2;
1363 } else {
1364 r += 1;
1365 }
1366 break;
1367 case LYS_IFF_F:
1368 f++;
1369 if (r) {
1370 r--;
1371 }
1372 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001373 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001374 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001375
Radek Krejci9ff0a922016-07-14 13:08:05 +02001376result:
1377 if (expr_size) {
1378 *expr_size = e;
1379 }
1380 if (feat_size) {
1381 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001382 }
1383}
1384
1385int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001386resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001387 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001388{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001389 const char *c = value;
1390 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001391 int i, j, last_not, checkversion = 0;
1392 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001393 uint8_t op;
1394 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001395 struct unres_iffeat_data *iff_data;
Michal Vasko53b7da02018-02-13 15:28:42 +01001396 struct ly_ctx *ctx = node->module->ctx;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001397
Radek Krejci9ff0a922016-07-14 13:08:05 +02001398 assert(c);
1399
1400 if (isspace(c[0])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001401 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001402 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001403 }
1404
Radek Krejci9ff0a922016-07-14 13:08:05 +02001405 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1406 for (i = j = last_not = 0; c[i]; i++) {
1407 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001408 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001409 j++;
1410 continue;
1411 } else if (c[i] == ')') {
1412 j--;
1413 continue;
1414 } else if (isspace(c[i])) {
1415 continue;
1416 }
1417
1418 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1419 if (c[i + r] == '\0') {
Michal Vasko53b7da02018-02-13 15:28:42 +01001420 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001421 return EXIT_FAILURE;
1422 } else if (!isspace(c[i + r])) {
1423 /* feature name starting with the not/and/or */
1424 last_not = 0;
1425 f_size++;
1426 } else if (c[i] == 'n') { /* not operation */
1427 if (last_not) {
1428 /* double not */
1429 expr_size = expr_size - 2;
1430 last_not = 0;
1431 } else {
1432 last_not = 1;
1433 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001434 } else { /* and, or */
1435 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001436 /* not a not operation */
1437 last_not = 0;
1438 }
1439 i += r;
1440 } else {
1441 f_size++;
1442 last_not = 0;
1443 }
1444 expr_size++;
1445
1446 while (!isspace(c[i])) {
1447 if (!c[i] || c[i] == ')') {
1448 i--;
1449 break;
1450 }
1451 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001452 }
1453 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001454 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001455 /* not matching count of ( and ) */
Michal Vasko53b7da02018-02-13 15:28:42 +01001456 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001457 return EXIT_FAILURE;
1458 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001459
Radek Krejci69b8d922016-07-27 13:13:41 +02001460 if (checkversion || expr_size > 1) {
1461 /* check that we have 1.1 module */
1462 if (node->module->version != 2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001463 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1464 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "YANG 1.1 if-feature expression found in 1.0 module.");
Radek Krejci69b8d922016-07-27 13:13:41 +02001465 return EXIT_FAILURE;
1466 }
1467 }
1468
Radek Krejci9ff0a922016-07-14 13:08:05 +02001469 /* allocate the memory */
1470 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001471 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001472 stack.stack = malloc(expr_size * sizeof *stack.stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001473 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001474 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001475 f_size--; expr_size--; /* used as indexes from now */
1476
1477 for (i--; i >= 0; i--) {
1478 if (c[i] == ')') {
1479 /* push it on stack */
1480 iff_stack_push(&stack, LYS_IFF_RP);
1481 continue;
1482 } else if (c[i] == '(') {
1483 /* pop from the stack into result all operators until ) */
1484 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1485 iff_setop(iffeat_expr->expr, op, expr_size--);
1486 }
1487 continue;
1488 } else if (isspace(c[i])) {
1489 continue;
1490 }
1491
1492 /* end operator or operand -> find beginning and get what is it */
1493 j = i + 1;
1494 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1495 i--;
1496 }
1497 i++; /* get back by one step */
1498
1499 if (!strncmp(&c[i], "not ", 4)) {
1500 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1501 /* double not */
1502 iff_stack_pop(&stack);
1503 } else {
1504 /* not has the highest priority, so do not pop from the stack
1505 * as in case of AND and OR */
1506 iff_stack_push(&stack, LYS_IFF_NOT);
1507 }
1508 } else if (!strncmp(&c[i], "and ", 4)) {
1509 /* as for OR - pop from the stack all operators with the same or higher
1510 * priority and store them to the result, then push the AND to the stack */
1511 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1512 op = iff_stack_pop(&stack);
1513 iff_setop(iffeat_expr->expr, op, expr_size--);
1514 }
1515 iff_stack_push(&stack, LYS_IFF_AND);
1516 } else if (!strncmp(&c[i], "or ", 3)) {
1517 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1518 op = iff_stack_pop(&stack);
1519 iff_setop(iffeat_expr->expr, op, expr_size--);
1520 }
1521 iff_stack_push(&stack, LYS_IFF_OR);
1522 } else {
1523 /* feature name, length is j - i */
1524
1525 /* add it to the result */
1526 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1527
1528 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001529 * forward referenced, we have to keep the feature name in auxiliary
1530 * structure passed into unres */
1531 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001532 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001533 iff_data->node = node;
1534 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001535 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001536 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1537 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001538 f_size--;
1539
1540 if (r == -1) {
Pavol Vican4d084512016-09-29 16:38:12 +02001541 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001542 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001543 }
1544 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001545 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001546 while (stack.index) {
1547 op = iff_stack_pop(&stack);
1548 iff_setop(iffeat_expr->expr, op, expr_size--);
1549 }
1550
1551 if (++expr_size || ++f_size) {
1552 /* not all expected operators and operands found */
Michal Vasko53b7da02018-02-13 15:28:42 +01001553 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001554 rc = EXIT_FAILURE;
1555 } else {
1556 rc = EXIT_SUCCESS;
1557 }
1558
1559error:
1560 /* cleanup */
1561 iff_stack_clean(&stack);
1562
1563 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001564}
1565
1566/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001567 * @brief Resolve (find) a data node based on a schema-nodeid.
1568 *
1569 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1570 * module).
1571 *
1572 */
1573struct lyd_node *
1574resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1575{
1576 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001577 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001578 const struct lys_node *schema = NULL;
1579
1580 assert(nodeid && start);
1581
1582 if (nodeid[0] == '/') {
1583 return NULL;
1584 }
1585
1586 str = p = strdup(nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01001587 LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001588
Michal Vasko3edeaf72016-02-11 13:17:43 +01001589 while (p) {
1590 token = p;
1591 p = strchr(p, '/');
1592 if (p) {
1593 *p = '\0';
1594 p++;
1595 }
1596
Radek Krejci5da4eb62016-04-08 14:45:51 +02001597 if (p) {
1598 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001599 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001600 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001601 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001602 result = NULL;
1603 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001604 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001605
Radek Krejci5da4eb62016-04-08 14:45:51 +02001606 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1607 continue;
1608 }
1609 } else {
1610 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001611 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001612 || !schema) {
1613 result = NULL;
1614 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001615 }
1616 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001617 LY_TREE_FOR(result ? result->child : start, iter) {
1618 if (iter->schema == schema) {
1619 /* move in data tree according to returned schema */
1620 result = iter;
1621 break;
1622 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001623 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001624 if (!iter) {
1625 /* instance not found */
1626 result = NULL;
1627 break;
1628 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001629 }
1630 free(str);
1631
1632 return result;
1633}
1634
Radek Krejci1a9c3612017-04-24 14:49:43 +02001635int
Michal Vasko50576712017-07-28 12:28:33 +02001636schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name,
1637 int mod_name_len, const char *name, int nam_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001638{
1639 const struct lys_module *prefix_mod;
1640
Michal Vaskocdb3f062018-02-01 09:55:06 +01001641 /* handle special names */
1642 if (name[0] == '*') {
1643 return 2;
1644 } else if (name[0] == '.') {
1645 return 3;
1646 }
1647
Michal Vasko50576712017-07-28 12:28:33 +02001648 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001649 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001650 return 1;
1651 }
1652
Radek Krejcibdf92362016-04-08 14:43:34 +02001653 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001654 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001655 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001656 if (!prefix_mod) {
1657 return -1;
1658 }
1659 } else {
1660 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001661 }
1662 if (prefix_mod != lys_node_module(sibling)) {
1663 return 1;
1664 }
1665
Michal Vasko50576712017-07-28 12:28:33 +02001666 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001667 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001668}
1669
Michal Vasko50576712017-07-28 12:28:33 +02001670/* keys do not have to be ordered and do not have to be all of them */
1671static int
1672resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node,
1673 const struct lys_module *cur_module, int *nodeid_end)
1674{
1675 int mod_len, nam_len, has_predicate, r, i;
1676 const char *model, *name;
1677 struct lys_node_list *list;
1678
1679 if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1680 return 1;
1681 }
1682
1683 list = (struct lys_node_list *)node;
1684 do {
1685 r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate);
1686 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001687 LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001688 return -1;
1689 }
1690 nodeid += r;
1691
1692 if (node->nodetype == LYS_LEAFLIST) {
1693 /* just check syntax */
1694 if (model || !name || (name[0] != '.') || has_predicate) {
1695 return 1;
1696 }
1697 break;
1698 } else {
1699 /* check the key */
1700 for (i = 0; i < list->keys_size; ++i) {
1701 if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) {
1702 continue;
1703 }
1704 if (model) {
1705 if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len)
1706 || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) {
1707 continue;
1708 }
1709 } else {
1710 if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) {
1711 continue;
1712 }
1713 }
1714
1715 /* match */
1716 break;
1717 }
1718
1719 if (i == list->keys_size) {
1720 return 1;
1721 }
1722 }
1723 } while (has_predicate);
1724
1725 if (!nodeid[0]) {
1726 *nodeid_end = 1;
1727 }
1728 return 0;
1729}
1730
Michal Vasko97234262018-02-01 09:53:01 +01001731/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001732 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001733int
Michal Vasko97234262018-02-01 09:53:01 +01001734resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001735 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001736{
Michal Vaskobb520442017-05-23 10:55:18 +02001737 const char *name, *mod_name, *id;
Michal Vasko97234262018-02-01 09:53:01 +01001738 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001739 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001740 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001741 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001742 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001743 char *str;
Michal Vasko53b7da02018-02-13 15:28:42 +01001744 struct ly_ctx *ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001745
Michal Vasko97234262018-02-01 09:53:01 +01001746 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001747 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001748
Michal Vasko50576712017-07-28 12:28:33 +02001749 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001750 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001751 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001752 ctx = cur_module->ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001753 id = nodeid;
1754
Michal Vasko50576712017-07-28 12:28:33 +02001755 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1756 (extended ? &all_desc : NULL), extended);
1757 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001758 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001759 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001760 }
1761 id += r;
1762
Michal Vasko97234262018-02-01 09:53:01 +01001763 if (is_relative && !start_parent) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001764 LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths.");
Michal Vasko3edeaf72016-02-11 13:17:43 +01001765 return -1;
1766 }
1767
1768 /* descendant-schema-nodeid */
1769 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001770 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001771
Michal Vasko3edeaf72016-02-11 13:17:43 +01001772 /* absolute-schema-nodeid */
1773 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001774 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001775 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001776 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001777 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001778 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001779 return -1;
1780 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001781 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001782 }
1783
1784 while (1) {
1785 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001786 last_aug = NULL;
1787
1788 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001789 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1790 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001791 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001792 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001793 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001794 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001795 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001796 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001797 return -1;
1798 }
1799 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001800 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001801 * because this module may be not implemented and it augments something in another module and
1802 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001803 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001804 }
1805
1806 /* if the module is implemented, all the augments will be connected */
Michal Vasko50576712017-07-28 12:28:33 +02001807 if (!aux_mod->implemented && !extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001808get_next_augment:
1809 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1810 }
1811 }
1812
1813 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vaskocb45f472018-02-12 10:47:42 +01001814 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02001815 r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len);
1816
1817 /* resolve predicate */
1818 if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) {
1819 r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end);
1820 if (r == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001821 continue;
Michal Vasko50576712017-07-28 12:28:33 +02001822 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02001823 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001824 }
Michal Vasko50576712017-07-28 12:28:33 +02001825 } else if (!id[0]) {
1826 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001827 }
Michal Vasko50576712017-07-28 12:28:33 +02001828
1829 if (r == 0) {
1830 /* one matching result */
1831 if (nodeid_end) {
1832 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001833 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001834 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1835 } else {
1836 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1837 return -1;
1838 }
1839 start_parent = sibling;
1840 }
1841 break;
1842 } else if (r == 1) {
1843 continue;
1844 } else if (r == 2) {
1845 /* "*" */
1846 if (!*ret) {
1847 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001848 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001849 }
1850 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1851 if (all_desc) {
1852 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1853 if (elem != sibling) {
1854 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1855 }
1856
1857 LY_TREE_DFS_END(sibling, next, elem);
1858 }
1859 }
1860 } else if (r == 3) {
1861 /* "." */
1862 if (!*ret) {
1863 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01001864 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02001865 ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST);
1866 }
1867 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
1868 if (all_desc) {
1869 LY_TREE_DFS_BEGIN(sibling, next, elem) {
1870 if (elem != sibling) {
1871 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
1872 }
1873
1874 LY_TREE_DFS_END(sibling, next, elem);
1875 }
1876 }
1877 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01001878 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02001879 return -1;
1880 }
1881 }
1882
1883 /* skip predicate */
1884 if (extended && has_predicate) {
1885 while (id[0] == '[') {
1886 id = strchr(id, ']');
1887 if (!id) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001888 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02001889 return -1;
1890 }
1891 ++id;
1892 }
1893 }
1894
1895 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
1896 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001897 }
1898
1899 /* no match */
1900 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02001901 if (last_aug) {
1902 /* it still could be in another augment */
1903 goto get_next_augment;
1904 }
Michal Vasko50576712017-07-28 12:28:33 +02001905 if (no_node_error) {
1906 str = strndup(nodeid, (name - nodeid) + nam_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001907 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001908 free(str);
1909 return -1;
1910 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01001911 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001912 return EXIT_SUCCESS;
1913 }
1914
Michal Vasko50576712017-07-28 12:28:33 +02001915 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1916 (extended ? &all_desc : NULL), extended);
1917 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001918 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001919 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001920 }
1921 id += r;
1922 }
1923
1924 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01001925 LOGINT(ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001926 return -1;
1927}
1928
Radek Krejcif3c71de2016-04-11 12:45:46 +02001929/* unique, refine,
1930 * >0 - unexpected char on position (ret - 1),
1931 * 0 - ok (but ret can still be NULL),
1932 * -1 - error,
1933 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001934int
1935resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02001936 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001937{
1938 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01001939 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001940 int r, nam_len, mod_name_len, is_relative = -1;
1941 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02001942 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001943
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001944 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01001945 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01001946
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01001947 if (!start) {
1948 /* leaf not found */
1949 return 0;
1950 }
1951
Michal Vasko3edeaf72016-02-11 13:17:43 +01001952 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02001953 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01001954
Michal Vasko50576712017-07-28 12:28:33 +02001955 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001956 return ((id - nodeid) - r) + 1;
1957 }
1958 id += r;
1959
1960 if (!is_relative) {
1961 return -1;
1962 }
1963
Michal Vasko24476fa2017-03-08 12:33:48 +01001964 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02001965 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01001966 start_parent = lys_parent(start_parent);
1967 }
1968
Michal Vasko3edeaf72016-02-11 13:17:43 +01001969 while (1) {
1970 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01001971 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vaskocb45f472018-02-12 10:47:42 +01001972 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02001973 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
1974 if (r == 0) {
1975 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001976 if (!(sibling->nodetype & ret_nodetype)) {
1977 /* wrong node type, too bad */
1978 continue;
1979 }
1980 *ret = sibling;
1981 return EXIT_SUCCESS;
1982 }
Michal Vasko50576712017-07-28 12:28:33 +02001983 start_parent = sibling;
1984 break;
1985 } else if (r == 1) {
1986 continue;
1987 } else {
1988 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001989 }
1990 }
1991
1992 /* no match */
1993 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01001994 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001995 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02001996 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
1997 *ret = NULL;
1998 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001999 }
2000
Michal Vasko50576712017-07-28 12:28:33 +02002001 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002002 return ((id - nodeid) - r) + 1;
2003 }
2004 id += r;
2005 }
2006
2007 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002008 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002009 return -1;
2010}
2011
2012/* choice default */
2013int
2014resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
2015{
2016 /* cannot actually be a path */
2017 if (strchr(nodeid, '/')) {
2018 return -1;
2019 }
2020
Michal Vaskodc300b02017-04-07 14:09:20 +02002021 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002022}
2023
2024/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2025static int
2026resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
2027{
2028 const struct lys_module *module;
2029 const char *mod_prefix, *name;
2030 int i, mod_prefix_len, nam_len;
2031
2032 /* parse the identifier, it must be parsed on one call */
Michal Vasko50576712017-07-28 12:28:33 +02002033 if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len, NULL, 0)) < 1) || nodeid[i]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002034 return -i + 1;
2035 }
2036
Michal Vasko921eb6b2017-10-13 10:01:39 +02002037 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002038 if (!module) {
2039 return -1;
2040 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002041 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002042 start = module->data;
2043 }
2044
2045 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2046
2047 return EXIT_SUCCESS;
2048}
2049
2050int
2051resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
2052 const struct lys_node **ret)
2053{
2054 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002055 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002056 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002057 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002058
2059 assert(nodeid && module && ret);
2060 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
2061
2062 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01002063 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002064
Michal Vasko50576712017-07-28 12:28:33 +02002065 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002066 return ((id - nodeid) - r) + 1;
2067 }
2068 id += r;
2069
2070 if (is_relative) {
2071 return -1;
2072 }
2073
Michal Vasko921eb6b2017-10-13 10:01:39 +02002074 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002075 if (!abs_start_mod) {
2076 return -1;
2077 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002078
2079 while (1) {
2080 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002081 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vaskocb45f472018-02-12 10:47:42 +01002082 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002083 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2084 if (r == 0) {
2085 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002086 if (!(sibling->nodetype & ret_nodetype)) {
2087 /* wrong node type, too bad */
2088 continue;
2089 }
2090 *ret = sibling;
2091 return EXIT_SUCCESS;
2092 }
Michal Vasko50576712017-07-28 12:28:33 +02002093 start_parent = sibling;
2094 break;
2095 } else if (r == 1) {
2096 continue;
2097 } else {
2098 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002099 }
2100 }
2101
2102 /* no match */
2103 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002104 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002105 return EXIT_SUCCESS;
2106 }
2107
Michal Vasko50576712017-07-28 12:28:33 +02002108 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002109 return ((id - nodeid) - r) + 1;
2110 }
2111 id += r;
2112 }
2113
2114 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002115 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002116 return -1;
2117}
2118
Michal Vaskoe733d682016-03-14 09:08:27 +01002119static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002120resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002121{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002122 const char *mod_name, *name;
2123 int mod_name_len, nam_len, has_predicate, i;
2124 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002125
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002126 if (((i = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, NULL, NULL, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002127 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002128 LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002129 return -1;
2130 }
2131
2132 predicate += i;
2133 *parsed += i;
2134
Michal Vasko58c2aab2017-01-05 10:02:05 +01002135 if (!isdigit(name[0])) {
2136 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002137 key = (struct lys_node *)list->keys[i];
2138 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002139 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002140 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002141 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002142
Michal Vasko58c2aab2017-01-05 10:02:05 +01002143 if (i == list->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002144 LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko58c2aab2017-01-05 10:02:05 +01002145 return -1;
2146 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002147 }
2148
2149 /* more predicates? */
2150 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002151 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002152 }
2153
2154 return 0;
2155}
2156
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002157/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002158const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002159resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002160{
Michal Vasko53b7da02018-02-13 15:28:42 +01002161 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002162 const char *name, *mod_name, *id;
Michal Vaskob3744402017-08-03 14:23:58 +02002163 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002164 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002165 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002166 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002167
Michal Vasko3547c532016-03-14 09:40:50 +01002168 assert(nodeid && (ctx || start));
2169 if (!ctx) {
2170 ctx = start->module->ctx;
2171 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002172
2173 id = nodeid;
2174
Michal Vasko50576712017-07-28 12:28:33 +02002175 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002176 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002177 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002178 }
2179 id += r;
2180
2181 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002182 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002183 start_parent = start;
2184 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2185 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002186 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002187 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002188 } else {
2189 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002190 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002191 LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
Michal Vasko10728b52016-04-07 14:26:29 +02002192 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002193 return NULL;
2194 }
2195
Michal Vasko53b7da02018-02-13 15:28:42 +01002196 str = strndup(mod_name, mod_name_len);
2197 module = ly_ctx_get_module(ctx, str, NULL, 1);
2198 free(str);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002199
Michal Vaskof68a49e2017-08-14 13:23:37 +02002200 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002201 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002202 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002203 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002204 return NULL;
2205 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002206 start_parent = NULL;
Michal Vasko3547c532016-03-14 09:40:50 +01002207
2208 /* now it's as if there was no module name */
2209 mod_name = NULL;
2210 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002211 }
2212
Michal Vaskof68a49e2017-08-14 13:23:37 +02002213 prev_mod = module;
2214
Michal Vasko3edeaf72016-02-11 13:17:43 +01002215 while (1) {
2216 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002217 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002218 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002219 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002220 /* output check */
2221 for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2222 if (parent) {
2223 if (output && (parent->nodetype == LYS_INPUT)) {
2224 continue;
2225 } else if (!output && (parent->nodetype == LYS_OUTPUT)) {
2226 continue;
2227 }
2228 }
2229
Michal Vasko3edeaf72016-02-11 13:17:43 +01002230 /* module check */
2231 if (mod_name) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002232 /* will also find an augment module */
Michal Vasko53b7da02018-02-13 15:28:42 +01002233 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002234
Michal Vasko3edeaf72016-02-11 13:17:43 +01002235 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002236 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002237 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002238 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002239 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002240 }
2241 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002242 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002243 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002244 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002245 continue;
2246 }
2247
Michal Vaskoe733d682016-03-14 09:08:27 +01002248 /* do we have some predicates on it? */
2249 if (has_predicate) {
2250 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002251 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002252 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002253 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002254 return NULL;
2255 }
2256 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002257 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002258 return NULL;
2259 }
2260 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002261 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002262 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002263 }
2264 id += r;
2265 }
2266
Michal Vasko3edeaf72016-02-11 13:17:43 +01002267 /* the result node? */
2268 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002269 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002270 }
2271
Michal Vaskodc300b02017-04-07 14:09:20 +02002272 /* move down the tree, if possible */
2273 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002274 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskodc300b02017-04-07 14:09:20 +02002275 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002276 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002277 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002278
2279 /* update prev mod */
2280 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002281 break;
2282 }
2283 }
2284
2285 /* no match */
2286 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002287 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002288 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002289 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002290 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002291 }
2292
Michal Vasko50576712017-07-28 12:28:33 +02002293 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002294 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002295 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002296 }
2297 id += r;
2298 }
2299
2300 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002301 LOGINT(ctx);
Michal Vaskoe733d682016-03-14 09:08:27 +01002302 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002303}
2304
Michal Vasko22448d32016-03-16 13:17:29 +01002305static int
Michal Vasko58c2aab2017-01-05 10:02:05 +01002306resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node,
Michal Vasko50576712017-07-28 12:28:33 +02002307 int position, int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002308{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002309 const char *mod_name, *name, *value, *key_val;
2310 int mod_name_len, nam_len, val_len, has_predicate = 1, r;
Michal Vasko22448d32016-03-16 13:17:29 +01002311 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002312 struct lyd_node_leaf_list *key;
Michal Vasko53b7da02018-02-13 15:28:42 +01002313 struct ly_ctx *ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002314
Radek Krejci61a86c62016-03-24 11:06:44 +01002315 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002316 assert(node->schema->nodetype == LYS_LIST);
Michal Vasko53b7da02018-02-13 15:28:42 +01002317 ctx = node->schema->module->ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002318
Michal Vasko53adfc72017-01-06 10:39:10 +01002319 /* is the predicate a number? */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002320 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko53adfc72017-01-06 10:39:10 +01002321 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002322 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vasko53adfc72017-01-06 10:39:10 +01002323 return -1;
2324 }
2325
2326 if (isdigit(name[0])) {
2327 if (position == atoi(name)) {
2328 /* match */
2329 *parsed += r;
2330 return 0;
2331 } else {
2332 /* not a match */
2333 return 1;
2334 }
2335 }
2336
2337 if (!((struct lys_node_list *)node->schema)->keys_size) {
2338 /* no keys in schema - causes an error later */
2339 return 0;
2340 }
2341
Michal Vaskof29903d2016-04-18 13:13:10 +02002342 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002343 if (!key) {
2344 /* it is not a position, so we need a key for it to be a match */
2345 return 1;
2346 }
2347
2348 /* go through all the keys */
2349 i = 0;
2350 goto check_parsed_values;
2351
2352 for (; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) {
Michal Vasko22448d32016-03-16 13:17:29 +01002353 if (!has_predicate) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002354 LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002355 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002356 }
2357
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002358 if (((r = parse_schema_json_predicate(predicate, &mod_name, &mod_name_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1)
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002359 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002360 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]);
Michal Vaskof29903d2016-04-18 13:13:10 +02002361 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002362 }
2363
Michal Vasko53adfc72017-01-06 10:39:10 +01002364check_parsed_values:
Michal Vasko22448d32016-03-16 13:17:29 +01002365 predicate += r;
2366 *parsed += r;
2367
Michal Vaskof29903d2016-04-18 13:13:10 +02002368 if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002369 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002370 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002371 }
2372
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002373 if (mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002374 /* specific module, check that the found key is from that module */
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002375 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, mod_name, mod_name_len)
2376 || lyd_node_module((struct lyd_node *)key)->name[mod_name_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002377 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002378 return -1;
2379 }
Michal Vasko50576712017-07-28 12:28:33 +02002380
2381 /* but if the module is the same as the parent, it should have been omitted */
2382 if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002383 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko50576712017-07-28 12:28:33 +02002384 return -1;
2385 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002386 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002387 /* no module, so it must be the same as the list (parent) */
2388 if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002389 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002390 return -1;
2391 }
2392 }
2393
Michal Vasko9ba34de2016-12-07 12:21:19 +01002394 /* make value canonical */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002395 if ((key->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002396 && !strncmp(key->value_str, lyd_node_module(node)->name, strlen(lyd_node_module(node)->name))
2397 && (key->value_str[strlen(lyd_node_module(node)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002398 key_val = key->value_str + strlen(lyd_node_module(node)->name) + 1;
2399 } else {
2400 key_val = key->value_str;
2401 }
2402
Michal Vasko22448d32016-03-16 13:17:29 +01002403 /* value does not match */
Michal Vasko9ba34de2016-12-07 12:21:19 +01002404 if (strncmp(key_val, value, val_len) || key_val[val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002405 return 1;
2406 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002407
2408 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002409 }
2410
2411 if (has_predicate) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002412 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko22448d32016-03-16 13:17:29 +01002413 return -1;
2414 }
2415
2416 return 0;
2417}
2418
Radek Krejci45826012016-08-24 15:07:57 +02002419/**
2420 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2421 *
2422 * @param[in] nodeid Node data path to find
2423 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2424 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2425 * @param[out] parsed Number of characters processed in \p id
2426 * @return The closes parent (or the node itself) from the path
2427 */
Michal Vasko22448d32016-03-16 13:17:29 +01002428struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002429resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2430 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002431{
Michal Vasko53b7da02018-02-13 15:28:42 +01002432 char *str;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002433 const char *id, *mod_name, *name, *pred_name, *data_val;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002434 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vasko9ba34de2016-12-07 12:21:19 +01002435 int has_predicate, last_parsed, llval_len, pred_name_len, last_has_pred;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002436 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002437 struct lyd_node_leaf_list *llist;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002438 const struct lys_module *prefix_mod, *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002439 struct ly_ctx *ctx;
2440
2441 assert(nodeid && start && parsed);
2442
2443 ctx = start->schema->module->ctx;
2444 id = nodeid;
2445
Michal Vasko50576712017-07-28 12:28:33 +02002446 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002447 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002448 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002449 return NULL;
2450 }
2451 id += r;
2452 /* add it to parsed only after the data node was actually found */
2453 last_parsed = r;
2454
2455 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002456 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002457 start = start->child;
2458 } else {
2459 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002460 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002461 }
2462
2463 while (1) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002464 list_instance_position = 0;
2465
Michal Vasko22448d32016-03-16 13:17:29 +01002466 LY_TREE_FOR(start, sibling) {
Michal Vasko945b96b2016-10-18 11:49:12 +02002467 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
Michal Vasko2411b942016-03-23 13:50:03 +01002468 if (lys_parent(sibling->schema)) {
2469 if (options & LYD_PATH_OPT_OUTPUT) {
2470 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002471 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002472 *parsed = -1;
2473 return NULL;
2474 }
2475 } else {
2476 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002477 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
Michal Vasko2411b942016-03-23 13:50:03 +01002478 *parsed = -1;
2479 return NULL;
2480 }
2481 }
2482 }
2483
Michal Vasko22448d32016-03-16 13:17:29 +01002484 /* name match */
2485 if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) {
2486
2487 /* module check */
2488 if (mod_name) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002489 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002490
Michal Vasko22448d32016-03-16 13:17:29 +01002491 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002492 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002493 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002494 free(str);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002495 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002496 return NULL;
2497 }
2498 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002499 prefix_mod = prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002500 }
Michal Vasko1adc7242016-11-16 11:05:28 +01002501 if (prefix_mod != lyd_node_module(sibling)) {
Michal Vasko22448d32016-03-16 13:17:29 +01002502 continue;
2503 }
2504
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002505 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko22448d32016-03-16 13:17:29 +01002506 if (sibling->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002507 llist = (struct lyd_node_leaf_list *)sibling;
2508
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002509 last_has_pred = 0;
Michal Vaskof0a50972016-10-19 11:33:55 +02002510 if (has_predicate) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002511 if ((r = parse_schema_json_predicate(id, NULL, NULL, &pred_name, &pred_name_len, &llist_value,
2512 &llval_len, &last_has_pred)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002513 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskof0a50972016-10-19 11:33:55 +02002514 *parsed = -1;
2515 return NULL;
2516 }
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002517 if ((pred_name[0] != '.') || (pred_name_len != 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002518 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[1], id + 1);
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002519 *parsed = -1;
2520 return NULL;
2521 }
Michal Vaskof0a50972016-10-19 11:33:55 +02002522 } else {
2523 r = 0;
2524 if (llist_value) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002525 llval_len = strlen(llist_value);
Michal Vaskof0a50972016-10-19 11:33:55 +02002526 }
2527 }
2528
Michal Vasko21b90ce2017-09-19 09:38:27 +02002529 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vaskod6d51292017-09-22 14:30:48 +02002530 if (llist_value && !strchr(llist_value, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002531 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2532 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002533 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2534 } else {
2535 data_val = llist->value_str;
2536 }
2537
2538 if ((!llist_value && data_val && data_val[0])
2539 || (llist_value && (strncmp(llist_value, data_val, llval_len) || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002540 continue;
2541 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002542
Michal Vaskof0a50972016-10-19 11:33:55 +02002543 id += r;
2544 last_parsed += r;
Michal Vasko0a8d17f2016-10-19 15:51:36 +02002545 has_predicate = last_has_pred;
Michal Vaskof0a50972016-10-19 11:33:55 +02002546
Radek Krejci45826012016-08-24 15:07:57 +02002547 } else if (sibling->schema->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002548 /* 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 +01002549 if (!has_predicate) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002550 /* none match */
2551 return last_match;
Michal Vasko22448d32016-03-16 13:17:29 +01002552 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01002553
2554 ++list_instance_position;
2555 r = 0;
Michal Vasko50576712017-07-28 12:28:33 +02002556 ret = resolve_partial_json_data_list_predicate(id, name, sibling, list_instance_position, &r);
Michal Vasko22448d32016-03-16 13:17:29 +01002557 if (ret == -1) {
Michal Vasko238bd2f2016-03-23 09:39:01 +01002558 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002559 return NULL;
2560 } else if (ret == 1) {
2561 /* this list instance does not match */
2562 continue;
2563 }
2564 id += r;
2565 last_parsed += r;
2566 }
2567
2568 *parsed += last_parsed;
2569
2570 /* the result node? */
2571 if (!id[0]) {
2572 return sibling;
2573 }
2574
2575 /* move down the tree, if possible */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002576 if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002577 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002578 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002579 return NULL;
2580 }
Michal Vaskoc6c52cf2016-03-29 11:53:04 +02002581 last_match = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002582 prev_mod = lyd_node_module(sibling);
Michal Vasko22448d32016-03-16 13:17:29 +01002583 start = sibling->child;
Michal Vasko22448d32016-03-16 13:17:29 +01002584 break;
2585 }
2586 }
2587
2588 /* no match, return last match */
2589 if (!sibling) {
2590 return last_match;
2591 }
2592
Michal Vasko50576712017-07-28 12:28:33 +02002593 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate, NULL, 0)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002594 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002595 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002596 return NULL;
2597 }
2598 id += r;
2599 last_parsed = r;
2600 }
2601
2602 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002603 LOGINT(ctx);
Michal Vasko238bd2f2016-03-23 09:39:01 +01002604 *parsed = -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002605 return NULL;
2606}
2607
Michal Vasko3edeaf72016-02-11 13:17:43 +01002608/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002609 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002610 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002611 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002612 * @param[in] ctx Context for errors.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002613 * @param[in] str_restr Restriction as a string.
2614 * @param[in] type Type of the restriction.
2615 * @param[out] ret Final interval structure that starts with
2616 * the interval of the initial type, continues with intervals
2617 * of any superior types derived from the initial one, and
2618 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002619 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002620 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002621 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002622int
Michal Vasko53b7da02018-02-13 15:28:42 +01002623resolve_len_ran_interval(struct ly_ctx *ctx, const char *str_restr, struct lys_type *type, struct len_ran_intv **ret)
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002624{
2625 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002626 int kind;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002627 int64_t local_smin, local_smax, local_fmin, local_fmax;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002628 uint64_t local_umin, local_umax;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002629 uint8_t local_fdig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002630 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002631 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002632
2633 switch (type->base) {
2634 case LY_TYPE_BINARY:
2635 kind = 0;
2636 local_umin = 0;
2637 local_umax = 18446744073709551615UL;
2638
2639 if (!str_restr && type->info.binary.length) {
2640 str_restr = type->info.binary.length->expr;
2641 }
2642 break;
2643 case LY_TYPE_DEC64:
2644 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002645 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2646 local_fmax = __INT64_C(9223372036854775807);
2647 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002648
2649 if (!str_restr && type->info.dec64.range) {
2650 str_restr = type->info.dec64.range->expr;
2651 }
2652 break;
2653 case LY_TYPE_INT8:
2654 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002655 local_smin = __INT64_C(-128);
2656 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002657
2658 if (!str_restr && type->info.num.range) {
2659 str_restr = type->info.num.range->expr;
2660 }
2661 break;
2662 case LY_TYPE_INT16:
2663 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002664 local_smin = __INT64_C(-32768);
2665 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002666
2667 if (!str_restr && type->info.num.range) {
2668 str_restr = type->info.num.range->expr;
2669 }
2670 break;
2671 case LY_TYPE_INT32:
2672 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002673 local_smin = __INT64_C(-2147483648);
2674 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002675
2676 if (!str_restr && type->info.num.range) {
2677 str_restr = type->info.num.range->expr;
2678 }
2679 break;
2680 case LY_TYPE_INT64:
2681 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002682 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2683 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002684
2685 if (!str_restr && type->info.num.range) {
2686 str_restr = type->info.num.range->expr;
2687 }
2688 break;
2689 case LY_TYPE_UINT8:
2690 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002691 local_umin = __UINT64_C(0);
2692 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002693
2694 if (!str_restr && type->info.num.range) {
2695 str_restr = type->info.num.range->expr;
2696 }
2697 break;
2698 case LY_TYPE_UINT16:
2699 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002700 local_umin = __UINT64_C(0);
2701 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002702
2703 if (!str_restr && type->info.num.range) {
2704 str_restr = type->info.num.range->expr;
2705 }
2706 break;
2707 case LY_TYPE_UINT32:
2708 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002709 local_umin = __UINT64_C(0);
2710 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002711
2712 if (!str_restr && type->info.num.range) {
2713 str_restr = type->info.num.range->expr;
2714 }
2715 break;
2716 case LY_TYPE_UINT64:
2717 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002718 local_umin = __UINT64_C(0);
2719 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002720
2721 if (!str_restr && type->info.num.range) {
2722 str_restr = type->info.num.range->expr;
2723 }
2724 break;
2725 case LY_TYPE_STRING:
2726 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002727 local_umin = __UINT64_C(0);
2728 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002729
2730 if (!str_restr && type->info.str.length) {
2731 str_restr = type->info.str.length->expr;
2732 }
2733 break;
2734 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002735 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002736 }
2737
2738 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002739 if (type->der) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002740 if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002741 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02002742 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002743 assert(!intv || (intv->kind == kind));
2744 }
2745
2746 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002747 /* we do not have any restriction, return superior ones */
2748 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002749 return EXIT_SUCCESS;
2750 }
2751
2752 /* adjust local min and max */
2753 if (intv) {
2754 tmp_intv = intv;
2755
2756 if (kind == 0) {
2757 local_umin = tmp_intv->value.uval.min;
2758 } else if (kind == 1) {
2759 local_smin = tmp_intv->value.sval.min;
2760 } else if (kind == 2) {
2761 local_fmin = tmp_intv->value.fval.min;
2762 }
2763
2764 while (tmp_intv->next) {
2765 tmp_intv = tmp_intv->next;
2766 }
2767
2768 if (kind == 0) {
2769 local_umax = tmp_intv->value.uval.max;
2770 } else if (kind == 1) {
2771 local_smax = tmp_intv->value.sval.max;
2772 } else if (kind == 2) {
2773 local_fmax = tmp_intv->value.fval.max;
2774 }
2775 }
2776
2777 /* finally parse our restriction */
2778 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002779 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002780 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002781 if (!tmp_local_intv) {
2782 assert(!local_intv);
2783 local_intv = malloc(sizeof *local_intv);
2784 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002785 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002786 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002787 tmp_local_intv = tmp_local_intv->next;
2788 }
Michal Vasko53b7da02018-02-13 15:28:42 +01002789 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002790
2791 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002792 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002793 tmp_local_intv->next = NULL;
2794
2795 /* min */
2796 ptr = seg_ptr;
2797 while (isspace(ptr[0])) {
2798 ++ptr;
2799 }
2800 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2801 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002802 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002803 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002804 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002805 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002806 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002807 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02002808 goto error;
2809 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002810 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002811 } else if (!strncmp(ptr, "min", 3)) {
2812 if (kind == 0) {
2813 tmp_local_intv->value.uval.min = local_umin;
2814 } else if (kind == 1) {
2815 tmp_local_intv->value.sval.min = local_smin;
2816 } else if (kind == 2) {
2817 tmp_local_intv->value.fval.min = local_fmin;
2818 }
2819
2820 ptr += 3;
2821 } else if (!strncmp(ptr, "max", 3)) {
2822 if (kind == 0) {
2823 tmp_local_intv->value.uval.min = local_umax;
2824 } else if (kind == 1) {
2825 tmp_local_intv->value.sval.min = local_smax;
2826 } else if (kind == 2) {
2827 tmp_local_intv->value.fval.min = local_fmax;
2828 }
2829
2830 ptr += 3;
2831 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002832 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002833 }
2834
2835 while (isspace(ptr[0])) {
2836 ptr++;
2837 }
2838
2839 /* no interval or interval */
2840 if ((ptr[0] == '|') || !ptr[0]) {
2841 if (kind == 0) {
2842 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
2843 } else if (kind == 1) {
2844 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
2845 } else if (kind == 2) {
2846 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
2847 }
2848 } else if (!strncmp(ptr, "..", 2)) {
2849 /* skip ".." */
2850 ptr += 2;
2851 while (isspace(ptr[0])) {
2852 ++ptr;
2853 }
2854
2855 /* max */
2856 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
2857 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02002858 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002859 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02002860 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002861 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02002862 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002863 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02002864 goto error;
2865 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002866 }
2867 } else if (!strncmp(ptr, "max", 3)) {
2868 if (kind == 0) {
2869 tmp_local_intv->value.uval.max = local_umax;
2870 } else if (kind == 1) {
2871 tmp_local_intv->value.sval.max = local_smax;
2872 } else if (kind == 2) {
2873 tmp_local_intv->value.fval.max = local_fmax;
2874 }
2875 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002876 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002877 }
2878 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002879 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002880 }
2881
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002882 /* check min and max in correct order*/
2883 if (kind == 0) {
2884 /* current segment */
2885 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
2886 goto error;
2887 }
2888 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
2889 goto error;
2890 }
2891 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02002892 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002893 goto error;
2894 }
2895 } else if (kind == 1) {
2896 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
2897 goto error;
2898 }
2899 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
2900 goto error;
2901 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002902 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002903 goto error;
2904 }
2905 } else if (kind == 2) {
2906 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
2907 goto error;
2908 }
2909 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
2910 goto error;
2911 }
Pavol Vican69f62c92016-08-30 09:06:25 +02002912 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002913 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002914 goto error;
2915 }
2916 }
2917
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002918 /* next segment (next OR) */
2919 seg_ptr = strchr(seg_ptr, '|');
2920 if (!seg_ptr) {
2921 break;
2922 }
2923 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02002924 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002925 }
2926
2927 /* check local restrictions against superior ones */
2928 if (intv) {
2929 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002930 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002931
2932 while (tmp_local_intv && tmp_intv) {
2933 /* reuse local variables */
2934 if (kind == 0) {
2935 local_umin = tmp_local_intv->value.uval.min;
2936 local_umax = tmp_local_intv->value.uval.max;
2937
2938 /* it must be in this interval */
2939 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
2940 /* this interval is covered, next one */
2941 if (local_umax <= tmp_intv->value.uval.max) {
2942 tmp_local_intv = tmp_local_intv->next;
2943 continue;
2944 /* ascending order of restrictions -> fail */
2945 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002946 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002947 }
2948 }
2949 } else if (kind == 1) {
2950 local_smin = tmp_local_intv->value.sval.min;
2951 local_smax = tmp_local_intv->value.sval.max;
2952
2953 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
2954 if (local_smax <= tmp_intv->value.sval.max) {
2955 tmp_local_intv = tmp_local_intv->next;
2956 continue;
2957 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002958 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002959 }
2960 }
2961 } else if (kind == 2) {
2962 local_fmin = tmp_local_intv->value.fval.min;
2963 local_fmax = tmp_local_intv->value.fval.max;
2964
Michal Vasko4d1f0482016-09-19 14:35:06 +02002965 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02002966 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02002967 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002968 tmp_local_intv = tmp_local_intv->next;
2969 continue;
2970 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002971 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002972 }
2973 }
2974 }
2975
2976 tmp_intv = tmp_intv->next;
2977 }
2978
2979 /* some interval left uncovered -> fail */
2980 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02002981 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002982 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002983 }
2984
Michal Vaskoaeb51802016-04-11 10:58:47 +02002985 /* append the local intervals to all the intervals of the superior types, return it all */
2986 if (intv) {
2987 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
2988 tmp_intv->next = local_intv;
2989 } else {
2990 intv = local_intv;
2991 }
2992 *ret = intv;
2993
2994 return EXIT_SUCCESS;
2995
2996error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002997 while (intv) {
2998 tmp_intv = intv->next;
2999 free(intv);
3000 intv = tmp_intv;
3001 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003002 while (local_intv) {
3003 tmp_local_intv = local_intv->next;
3004 free(local_intv);
3005 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003006 }
3007
Michal Vaskoaeb51802016-04-11 10:58:47 +02003008 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003009}
3010
Michal Vasko730dfdf2015-08-11 14:48:05 +02003011/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003012 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3013 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003014 *
3015 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003016 * @param[in] mod_name Typedef name module name.
3017 * @param[in] module Main module.
3018 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003019 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003020 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003021 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003022 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003023int
Michal Vasko1e62a092015-12-01 12:27:20 +01003024resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3025 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003026{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003027 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003028 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003029 int tpdf_size;
3030
Michal Vasko1dca6882015-10-22 14:29:42 +02003031 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003032 /* no prefix, try built-in types */
3033 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003034 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003035 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003036 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003037 }
3038 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003039 }
3040 }
3041 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003042 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003043 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003044 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003045 }
3046 }
3047
Michal Vasko1dca6882015-10-22 14:29:42 +02003048 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003049 /* search in local typedefs */
3050 while (parent) {
3051 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003052 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003053 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3054 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003055 break;
3056
Radek Krejci76512572015-08-04 09:47:08 +02003057 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003058 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3059 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003060 break;
3061
Radek Krejci76512572015-08-04 09:47:08 +02003062 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003063 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3064 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003065 break;
3066
Radek Krejci76512572015-08-04 09:47:08 +02003067 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003068 case LYS_ACTION:
3069 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3070 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003071 break;
3072
Radek Krejci76512572015-08-04 09:47:08 +02003073 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003074 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3075 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003076 break;
3077
Radek Krejci76512572015-08-04 09:47:08 +02003078 case LYS_INPUT:
3079 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003080 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3081 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003082 break;
3083
3084 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003085 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003086 continue;
3087 }
3088
3089 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003090 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003091 match = &tpdf[i];
3092 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003093 }
3094 }
3095
Michal Vaskodcf98e62016-05-05 17:53:53 +02003096 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003097 }
Radek Krejcic071c542016-01-27 14:57:51 +01003098 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003099 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003100 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003101 if (!module) {
3102 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003103 }
3104 }
3105
3106 /* search in top level typedefs */
3107 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003108 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003109 match = &module->tpdf[i];
3110 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003111 }
3112 }
3113
3114 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003115 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003116 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003117 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 +02003118 match = &module->inc[i].submodule->tpdf[j];
3119 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003120 }
3121 }
3122 }
3123
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003124 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003125
3126check_leafref:
3127 if (ret) {
3128 *ret = match;
3129 }
3130 if (match->type.base == LY_TYPE_LEAFREF) {
3131 while (!match->type.info.lref.path) {
3132 match = match->type.der;
3133 assert(match);
3134 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003135 }
3136 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003137}
3138
Michal Vasko1dca6882015-10-22 14:29:42 +02003139/**
3140 * @brief Check the default \p value of the \p type. Logs directly.
3141 *
3142 * @param[in] type Type definition to use.
3143 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003144 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003145 *
3146 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3147 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003148static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003149check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003150{
Radek Krejcibad2f172016-08-02 11:04:15 +02003151 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003152 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003153 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003154 char *s;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003155 int ret = EXIT_SUCCESS, r;
Michal Vasko53b7da02018-02-13 15:28:42 +01003156 struct ly_ctx *ctx = module->ctx;
Michal Vasko1dca6882015-10-22 14:29:42 +02003157
Radek Krejci51673202016-11-01 17:00:32 +01003158 assert(value);
3159
Radek Krejcic13db382016-08-16 10:52:42 +02003160 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003161 /* the type was not resolved yet, nothing to do for now */
3162 return EXIT_FAILURE;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003163 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003164 /* do not check defaults in not implemented module's data */
3165 return EXIT_SUCCESS;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003166 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003167 /* identityrefs are checked when instantiated in data instead of typedef,
3168 * but in typedef the value has to be modified to include the prefix */
3169 if (*value) {
3170 if (strchr(*value, ':')) {
3171 dflt = transform_schema2json(module, *value);
3172 } else {
3173 /* default prefix of the module where the typedef is defined */
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003174 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
3175 LOGMEM(ctx);
3176 return -1;
3177 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003178 dflt = lydict_insert_zc(ctx, s);
Radek Krejci9e6af732017-04-27 14:40:25 +02003179 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003180 lydict_remove(ctx, *value);
Radek Krejci9e6af732017-04-27 14:40:25 +02003181 *value = dflt;
3182 }
3183 return EXIT_SUCCESS;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003184 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3185 /* leafref in typedef cannot be checked */
3186 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003187 }
3188
Radek Krejci51673202016-11-01 17:00:32 +01003189 dflt = *value;
3190 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003191 /* we do not have a new default value, so is there any to check even, in some base type? */
3192 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3193 if (base_tpdf->dflt) {
Radek Krejci51673202016-11-01 17:00:32 +01003194 dflt = base_tpdf->dflt;
Michal Vasko478c4652016-07-21 12:55:01 +02003195 break;
3196 }
3197 }
3198
Radek Krejci51673202016-11-01 17:00:32 +01003199 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003200 /* no default value, nothing to check, all is well */
3201 return EXIT_SUCCESS;
3202 }
3203
3204 /* 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)? */
3205 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003206 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003207 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
3208 return EXIT_SUCCESS;
3209 } else {
3210 /* check the default value from typedef, but use also the typedef's module
3211 * due to possible searching in imported modules which is expected in
3212 * typedef's module instead of module where the typedef is used */
3213 module = base_tpdf->module;
3214 }
3215 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003216 case LY_TYPE_INST:
3217 case LY_TYPE_LEAFREF:
3218 case LY_TYPE_BOOL:
3219 case LY_TYPE_EMPTY:
3220 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
3221 return EXIT_SUCCESS;
Radek Krejcibad2f172016-08-02 11:04:15 +02003222 case LY_TYPE_BITS:
3223 /* the default value must match the restricted list of values, if the type was restricted */
3224 if (type->info.bits.count) {
3225 break;
3226 }
3227 return EXIT_SUCCESS;
3228 case LY_TYPE_ENUM:
3229 /* the default value must match the restricted list of values, if the type was restricted */
3230 if (type->info.enums.count) {
3231 break;
3232 }
3233 return EXIT_SUCCESS;
Michal Vasko478c4652016-07-21 12:55:01 +02003234 case LY_TYPE_DEC64:
3235 if (type->info.dec64.range) {
3236 break;
3237 }
3238 return EXIT_SUCCESS;
3239 case LY_TYPE_BINARY:
3240 if (type->info.binary.length) {
3241 break;
3242 }
3243 return EXIT_SUCCESS;
3244 case LY_TYPE_INT8:
3245 case LY_TYPE_INT16:
3246 case LY_TYPE_INT32:
3247 case LY_TYPE_INT64:
3248 case LY_TYPE_UINT8:
3249 case LY_TYPE_UINT16:
3250 case LY_TYPE_UINT32:
3251 case LY_TYPE_UINT64:
3252 if (type->info.num.range) {
3253 break;
3254 }
3255 return EXIT_SUCCESS;
3256 case LY_TYPE_STRING:
3257 if (type->info.str.length || type->info.str.patterns) {
3258 break;
3259 }
3260 return EXIT_SUCCESS;
3261 case LY_TYPE_UNION:
3262 /* way too much trouble learning whether we need to check the default again, so just do it */
3263 break;
3264 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01003265 LOGINT(ctx);
Michal Vasko478c4652016-07-21 12:55:01 +02003266 return -1;
3267 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003268 } else if (type->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003269 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3270 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
Radek Krejci55a161c2016-09-05 17:13:25 +02003271 return -1;
Michal Vasko478c4652016-07-21 12:55:01 +02003272 }
3273
Michal Vasko1dca6882015-10-22 14:29:42 +02003274 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003275 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003276 node.value_str = dflt;
Michal Vasko1dca6882015-10-22 14:29:42 +02003277 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003278
3279 if (tpdf) {
3280 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko53b7da02018-02-13 15:28:42 +01003281 LY_CHECK_ERR_RETURN(!node.schema, LOGMEM(ctx), -1);
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003282 r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
3283 LY_CHECK_ERR_RETURN(r == -1, LOGMEM(ctx); free(node.schema), -1);
Michal Vasko31a2d322018-01-12 13:36:12 +01003284 node.schema->module = module;
3285 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3286 } else {
3287 node.schema = (struct lys_node *)type->parent;
3288 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003289
Radek Krejci37b756f2016-01-18 10:15:03 +01003290 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003291 if (!type->info.lref.target) {
3292 ret = EXIT_FAILURE;
3293 goto finish;
3294 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003295 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003296 if (!ret) {
3297 /* adopt possibly changed default value to its canonical form */
3298 if (*value) {
3299 *value = dflt;
3300 }
3301 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003302 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01003303 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003304 /* possible forward reference */
3305 ret = 1;
Radek Krejcibad2f172016-08-02 11:04:15 +02003306 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003307 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003308 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3309 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3310 /* we have refined bits/enums */
Michal Vasko53b7da02018-02-13 15:28:42 +01003311 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
Radek Krejcibad2f172016-08-02 11:04:15 +02003312 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003313 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003314 }
3315 }
Radek Krejci51673202016-11-01 17:00:32 +01003316 } else {
3317 /* success - adopt canonical form from the node into the default value */
3318 if (dflt != node.value_str) {
3319 /* this can happen only if we have non-inherited default value,
3320 * inherited default values are already in canonical form */
3321 assert(dflt == *value);
3322 *value = node.value_str;
3323 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003324 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003325 }
3326
3327finish:
3328 if (node.value_type == LY_TYPE_BITS) {
3329 free(node.value.bit);
3330 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003331 if (tpdf) {
3332 free((char *)node.schema->name);
3333 free(node.schema);
3334 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003335
3336 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003337}
3338
Michal Vasko730dfdf2015-08-11 14:48:05 +02003339/**
3340 * @brief Check a key for mandatory attributes. Logs directly.
3341 *
3342 * @param[in] key The key to check.
3343 * @param[in] flags What flags to check.
3344 * @param[in] list The list of all the keys.
3345 * @param[in] index Index of the key in the key list.
3346 * @param[in] name The name of the keys.
3347 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003348 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003349 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003350 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003351static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003352check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003353{
Radek Krejciadb57612016-02-16 13:34:34 +01003354 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003355 char *dup = NULL;
3356 int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01003357 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003358
3359 /* existence */
3360 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003361 if (name[len] != '\0') {
3362 dup = strdup(name);
Michal Vasko53b7da02018-02-13 15:28:42 +01003363 LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003364 dup[len] = '\0';
3365 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003366 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003367 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003368 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003369 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003370 }
3371
3372 /* uniqueness */
3373 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003374 if (key == list->keys[j]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003375 LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003376 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003377 }
3378 }
3379
3380 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003381 if (key->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003382 LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003383 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003384 }
3385
3386 /* type of the leaf is not built-in empty */
Radek Krejcic3738db2016-09-15 15:51:21 +02003387 if (key->type.base == LY_TYPE_EMPTY && key->module->version < 2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003388 LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003389 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003390 }
3391
3392 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003393 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003394 LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003395 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003396 }
3397
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003398 /* key is not placed from augment */
3399 if (key->parent->nodetype == LYS_AUGMENT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003400 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3401 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003402 return -1;
3403 }
3404
Radek Krejci3f21ada2016-08-01 13:34:31 +02003405 /* key is not when/if-feature -conditional */
3406 j = 0;
3407 if (key->when || (key->iffeature_size && (j = 1))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003408 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3409 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003410 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003411 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003412 }
3413
Michal Vasko0b85aa82016-03-07 14:37:43 +01003414 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003415}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003416
3417/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003418 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003419 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003420 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003421 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003422 *
3423 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3424 */
3425int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003426resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003427{
Radek Krejci581ce772015-11-10 17:22:40 +01003428 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003429 const struct lys_node *leaf = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01003430 struct ly_ctx *ctx = parent->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003431
Michal Vaskodc300b02017-04-07 14:09:20 +02003432 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003433 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003434 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003435 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003436 if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003437 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_PREV, NULL, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]);
Radek Krejcif3c71de2016-04-11 12:45:46 +02003438 } else if (rc == -2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003439 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003440 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003441 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003442 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003443 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3444 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003445 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003446 }
Radek Krejci581ce772015-11-10 17:22:40 +01003447 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003448 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003449 if (leaf->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003450 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3451 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003452 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003453 }
3454
Radek Krejcicf509982015-12-15 09:22:44 +01003455 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003456 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3457 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003458 return -1;
3459 }
3460
Radek Krejcid09d1a52016-08-11 14:05:45 +02003461 /* check that all unique's targets are of the same config type */
3462 if (*trg_type) {
3463 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003464 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3465 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003466 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3467 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3468 return -1;
3469 }
3470 } else {
3471 /* first unique */
3472 if (leaf->flags & LYS_CONFIG_W) {
3473 *trg_type = 1;
3474 } else {
3475 *trg_type = 2;
3476 }
3477 }
3478
Radek Krejcica7efb72016-01-18 13:06:01 +01003479 /* set leaf's unique flag */
3480 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3481
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003482 return EXIT_SUCCESS;
3483
3484error:
3485
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003486 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003487}
3488
Radek Krejci0c0086a2016-03-24 15:20:28 +01003489void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003490unres_data_del(struct unres_data *unres, uint32_t i)
3491{
3492 /* there are items after the one deleted */
3493 if (i+1 < unres->count) {
3494 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003495 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003496
3497 /* deleting the last item */
3498 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003499 free(unres->node);
3500 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003501 }
3502
3503 /* if there are no items after and it is not the last one, just move the counter */
3504 --unres->count;
3505}
3506
Michal Vasko0491ab32015-08-19 14:28:29 +02003507/**
3508 * @brief Resolve (find) a data node from a specific module. Does not log.
3509 *
3510 * @param[in] mod Module to search in.
3511 * @param[in] name Name of the data node.
3512 * @param[in] nam_len Length of the name.
3513 * @param[in] start Data node to start the search from.
3514 * @param[in,out] parents Resolved nodes. If there are some parents,
3515 * they are replaced (!!) with the resolvents.
3516 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003517 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003518 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003519static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003520resolve_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 +02003521{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003522 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003523 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003524 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003525
Michal Vasko23b61ec2015-08-19 11:19:50 +02003526 if (!parents->count) {
3527 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003528 parents->node = malloc(sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003529 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003530 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003531 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003532 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003533 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003534 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003535 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003536 continue;
3537 }
3538 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003539 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003540 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003541 && node->schema->name[nam_len] == '\0') {
3542 /* matching target */
3543 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003544 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003545 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003546 flag = 1;
3547 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003548 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003549 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003550 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003551 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003552 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003553 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003554 }
3555 }
3556 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003557
3558 if (!flag) {
3559 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003560 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003561 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003562 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003563 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003564 }
3565
Michal Vasko0491ab32015-08-19 14:28:29 +02003566 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003567}
3568
Michal Vaskoe27516a2016-10-10 17:55:31 +00003569static int
Michal Vasko1c007172017-03-10 10:20:44 +01003570resolve_schema_leafref_valid_dep_flag(const struct lys_node *op_node, const struct lys_node *first_node, int abs_path)
Michal Vaskoe27516a2016-10-10 17:55:31 +00003571{
3572 int dep1, dep2;
3573 const struct lys_node *node;
3574
3575 if (lys_parent(op_node)) {
3576 /* inner operation (notif/action) */
3577 if (abs_path) {
3578 return 1;
3579 } else {
3580 /* compare depth of both nodes */
3581 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3582 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3583 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3584 return 1;
3585 }
3586 }
3587 } else {
3588 /* top-level operation (notif/rpc) */
3589 if (op_node != first_node) {
3590 return 1;
3591 }
3592 }
3593
3594 return 0;
3595}
3596
Michal Vasko730dfdf2015-08-11 14:48:05 +02003597/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003598 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003599 *
Michal Vaskobb211122015-08-19 14:03:11 +02003600 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003601 * @param[in] context_node Predicate context node (where the predicate is placed).
3602 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003603 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003604 *
Michal Vasko184521f2015-09-24 13:14:26 +02003605 * @return 0 on forward reference, otherwise the number
3606 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003607 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003608 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003609static int
Michal Vasko1c007172017-03-10 10:20:44 +01003610resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3611 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003612{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003613 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003614 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003615 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003616 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3617 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003618 struct ly_ctx *ctx = context_node->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003619
3620 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003621 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003622 &pke_len, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003623 LOGVAL(ctx, LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003624 return -parsed+i;
3625 }
3626 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003627 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003628
Michal Vasko58090902015-08-13 14:04:15 +02003629 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003630 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003631 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003632 } else {
3633 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003634 }
Michal Vaskobb520442017-05-23 10:55:18 +02003635 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003636 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003637 LOGVAL(ctx, LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003638 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003639 }
3640
3641 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003642 dest_parent_times = 0;
3643 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003644 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3645 &dest_parent_times)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003646 LOGVAL(ctx, 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 +02003647 return -parsed;
3648 }
3649 pke_parsed += i;
3650
Radek Krejciadb57612016-02-16 13:34:34 +01003651 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003652 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3653 && !((struct lys_node_augment *)dst_node->parent)->target) {
3654 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003655 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003656 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3657 return 0;
3658 }
3659
Michal Vaskofbaead72016-10-07 10:54:48 +02003660 /* path is supposed to be evaluated in data tree, so we have to skip
3661 * all schema nodes that cannot be instantiated in data tree */
3662 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003663 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003664 dst_node = lys_parent(dst_node));
3665
Michal Vasko1f76a282015-08-04 16:16:53 +02003666 if (!dst_node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003667 LOGVAL(ctx, LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003668 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003669 }
3670 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003671 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003672 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003673 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003674 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003675 } else {
3676 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003677 }
Michal Vaskobb520442017-05-23 10:55:18 +02003678 rc = lys_getnext_data(trg_mod, dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003679 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003680 LOGVAL(ctx, LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003681 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003682 }
3683
Michal Vaskoe27516a2016-10-10 17:55:31 +00003684 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003685 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003686 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003687 }
3688 first_iter = 0;
3689 }
3690
Michal Vasko1f76a282015-08-04 16:16:53 +02003691 if (pke_len == pke_parsed) {
3692 break;
3693 }
3694
Michal Vaskobb520442017-05-23 10:55:18 +02003695 if ((i = parse_path_key_expr(path_key_expr + pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len,
Michal Vasko1f76a282015-08-04 16:16:53 +02003696 &dest_parent_times)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003697 LOGVAL(ctx, LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02003698 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003699 return -parsed;
3700 }
3701 pke_parsed += i;
3702 }
3703
3704 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02003705 if (dst_node->nodetype != src_node->nodetype) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003706 LOGVAL(ctx, LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path - parsed);
3707 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02003708 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02003709 return -parsed;
3710 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003711 } while (has_predicate);
3712
3713 return parsed;
3714}
3715
Michal Vasko730dfdf2015-08-11 14:48:05 +02003716/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003717 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003718 *
Michal Vaskobb211122015-08-19 14:03:11 +02003719 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003720 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003721 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003722 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003723 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003724 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003725static int
Michal Vasko1c007172017-03-10 10:20:44 +01003726resolve_schema_leafref(const char *path, struct lys_node *parent, const struct lys_node **ret)
Michal Vasko1f76a282015-08-04 16:16:53 +02003727{
Michal Vaskocb45f472018-02-12 10:47:42 +01003728 const struct lys_node *node, *op_node = NULL, *tmp_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02003729 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003730 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02003731 const char *id, *prefix, *name;
3732 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskocb45f472018-02-12 10:47:42 +01003733 int i, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003734 struct ly_ctx *ctx = parent->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003735
Michal Vasko184521f2015-09-24 13:14:26 +02003736 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003737 parent_times = 0;
3738 id = path;
3739
Michal Vasko1c007172017-03-10 10:20:44 +01003740 /* find operation schema we are in */
3741 for (op_node = lys_parent(parent);
3742 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
3743 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02003744
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003745 cur_module = lys_node_module(parent);
Michal Vasko1f76a282015-08-04 16:16:53 +02003746 do {
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003747 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003748 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003749 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003750 }
3751 id += i;
3752
Michal Vaskobb520442017-05-23 10:55:18 +02003753 /* get the current module */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003754 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003755 if (!tmp_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003756 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vaskobb520442017-05-23 10:55:18 +02003757 return EXIT_FAILURE;
3758 }
3759 last_aug = NULL;
3760
Michal Vasko184521f2015-09-24 13:14:26 +02003761 if (first_iter) {
Michal Vasko1f76a282015-08-04 16:16:53 +02003762 if (parent_times == -1) {
Michal Vaskobb520442017-05-23 10:55:18 +02003763 /* use module data */
3764 node = NULL;
Radek Krejci990af1f2016-11-09 13:53:36 +01003765
Michal Vasko1f76a282015-08-04 16:16:53 +02003766 } else if (parent_times > 0) {
Michal Vaskobb520442017-05-23 10:55:18 +02003767 /* we are looking for the right parent */
3768 for (i = 0, node = parent; i < parent_times; i++) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003769 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
3770 && !((struct lys_node_augment *)node->parent)->target) {
3771 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003772 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003773 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", path);
3774 return EXIT_FAILURE;
3775 }
3776
Radek Krejci3a5501d2016-07-18 22:03:34 +02003777 /* path is supposed to be evaluated in data tree, so we have to skip
3778 * all schema nodes that cannot be instantiated in data tree */
3779 for (node = lys_parent(node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003780 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Radek Krejci3a5501d2016-07-18 22:03:34 +02003781 node = lys_parent(node));
3782
Michal Vasko1f76a282015-08-04 16:16:53 +02003783 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003784 if (i == parent_times - 1) {
3785 /* top-level */
3786 break;
3787 }
3788
3789 /* higher than top-level */
Michal Vasko53b7da02018-02-13 15:28:42 +01003790 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003791 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003792 }
3793 }
Michal Vaskoe01eca52015-08-13 14:42:02 +02003794 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003795 LOGINT(ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003796 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003797 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003798 }
3799
Michal Vaskobb520442017-05-23 10:55:18 +02003800 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
3801 * - useless to search for that in augments) */
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003802 if (!tmp_mod->implemented && node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003803get_next_augment:
Michal Vasko3c60cbb2017-07-10 11:50:03 +02003804 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
Michal Vaskobb520442017-05-23 10:55:18 +02003805 }
3806
Michal Vaskocb45f472018-02-12 10:47:42 +01003807 tmp_parent = (last_aug ? (struct lys_node *)last_aug : node);
3808 node = NULL;
3809 while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) {
3810 if (lys_node_module(node) != lys_main_module(tmp_mod)) {
3811 continue;
3812 }
3813 if (strncmp(node->name, name, nam_len) || node->name[nam_len]) {
3814 continue;
3815 }
3816 /* match */
3817 break;
3818 }
3819 if (!node) {
Michal Vaskobb520442017-05-23 10:55:18 +02003820 if (last_aug) {
3821 goto get_next_augment;
3822 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003823 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko7a55bea2016-05-02 14:51:20 +02003824 return EXIT_FAILURE;
Michal Vasko1f76a282015-08-04 16:16:53 +02003825 }
Michal Vasko1f76a282015-08-04 16:16:53 +02003826
Michal Vaskoe27516a2016-10-10 17:55:31 +00003827 if (first_iter) {
3828 /* set external dependency flag, we can decide based on the first found node */
Michal Vasko1c007172017-03-10 10:20:44 +01003829 if (op_node && parent_times &&
3830 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003831 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003832 }
3833 first_iter = 0;
3834 }
3835
Michal Vasko1f76a282015-08-04 16:16:53 +02003836 if (has_predicate) {
3837 /* we have predicate, so the current result must be list */
3838 if (node->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003839 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003840 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003841 }
3842
Michal Vasko1c007172017-03-10 10:20:44 +01003843 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
Michal Vaskobb520442017-05-23 10:55:18 +02003844 if (!i) {
3845 return EXIT_FAILURE;
3846 } else if (i < 0) {
3847 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003848 }
3849 id += i;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003850 has_predicate = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003851 }
3852 } while (id[0]);
3853
Michal Vaskoca917682016-07-25 11:00:37 +02003854 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
Radek Krejci2a5a9602016-11-04 10:21:13 +01003855 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003856 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", path);
3857 LOGVAL(ctx, 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 +02003858 return -1;
Radek Krejcib1c12512015-08-11 11:22:04 +02003859 }
3860
Radek Krejcicf509982015-12-15 09:22:44 +01003861 /* check status */
Radek Krejciadb57612016-02-16 13:34:34 +01003862 if (lyp_check_status(parent->flags, parent->module, parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01003863 node->flags, node->module, node->name, node)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003864 return -1;
3865 }
3866
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003867 if (ret) {
3868 *ret = node;
3869 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003870
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003871 return EXIT_SUCCESS;
Michal Vasko1f76a282015-08-04 16:16:53 +02003872}
3873
Michal Vasko730dfdf2015-08-11 14:48:05 +02003874/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02003875 * @brief Compare 2 data node values.
3876 *
3877 * Comparison performed on canonical forms, the first value
3878 * is first transformed into canonical form.
3879 *
3880 * @param[in] node Leaf/leaf-list with these values.
3881 * @param[in] noncan_val Non-canonical value.
3882 * @param[in] noncan_val_len Length of \p noncal_val.
3883 * @param[in] can_val Canonical value.
3884 * @return 1 if equal, 0 if not, -1 on error (logged).
3885 */
3886static int
3887valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
3888{
3889 int ret;
3890 struct lyd_node_leaf_list leaf;
3891 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
3892
3893 /* dummy leaf */
3894 memset(&leaf, 0, sizeof leaf);
3895 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
3896
3897repeat:
3898 leaf.value_type = sleaf->type.base;
3899 leaf.schema = node;
3900
3901 if (leaf.value_type == LY_TYPE_LEAFREF) {
3902 if (!sleaf->type.info.lref.target) {
3903 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01003904 LOGINT(node->module->ctx);
Michal Vasko718ecdd2017-10-03 14:12:39 +02003905 ret = -1;
3906 goto finish;
3907 }
3908 sleaf = sleaf->type.info.lref.target;
3909 goto repeat;
3910 } else {
Michal Vasko31a2d322018-01-12 13:36:12 +01003911 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02003912 ret = -1;
3913 goto finish;
3914 }
3915 }
3916
3917 if (!strcmp(leaf.value_str, can_val)) {
3918 ret = 1;
3919 } else {
3920 ret = 0;
3921 }
3922
3923finish:
3924 lydict_remove(node->module->ctx, leaf.value_str);
3925 return ret;
3926}
3927
3928/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003929 * @brief Resolve instance-identifier predicate in JSON data format.
3930 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003931 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02003932 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02003933 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003934 * @param[in,out] node Node matching the restriction without
3935 * the predicate. If it does not satisfy the predicate,
3936 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003937 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003938 * @return Number of characters successfully parsed,
3939 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003940 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003941static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003942resolve_instid_predicate(const struct lys_module *prev_mod, const char *pred, struct lyd_node **node, int cur_idx)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003943{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003944 /* ... /node[key=value] ... */
3945 struct lyd_node_leaf_list *key;
3946 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02003947 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02003948 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003949 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vasko53b7da02018-02-13 15:28:42 +01003950 struct ly_ctx *ctx = prev_mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003951
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003952 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02003953
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003954 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003955 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003956 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
3957 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003958 }
3959 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003960
Michal Vasko88850b72017-10-02 13:13:21 +02003961 if (!(*node)) {
3962 /* just parse it all */
3963 continue;
3964 }
3965
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003966 /* target */
3967 if (name[0] == '.') {
3968 /* leaf-list value */
3969 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003970 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003971 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
3972 parsed = -1;
3973 goto cleanup;
3974 }
3975
3976 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02003977 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003978 *node = NULL;
3979 goto cleanup;
3980 }
3981
3982 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02003983 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003984
3985 /* keyless list position */
3986 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003987 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003988 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
3989 parsed = -1;
3990 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02003991 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003992
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003993 if (((struct lys_node_list *)(*node)->schema)->keys) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003994 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list without keys, but have list \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02003995 (*node)->schema->name);
3996 parsed = -1;
3997 goto cleanup;
3998 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02003999
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004000 /* check the index */
4001 if (atoi(name) != cur_idx) {
4002 *node = NULL;
4003 goto cleanup;
4004 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004005
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004006 } else {
4007 /* list key value */
4008 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004009 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004010 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4011 parsed = -1;
4012 goto cleanup;
4013 }
4014 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004015
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004016 /* prepare key array */
4017 if (!list_keys) {
4018 list_keys = malloc(slist->keys_size * sizeof *list_keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01004019 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004020 for (i = 0; i < slist->keys_size; ++i) {
4021 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004022 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004023 }
4024
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004025 /* find the schema key leaf */
4026 for (i = 0; i < slist->keys_size; ++i) {
4027 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4028 break;
4029 }
4030 }
4031 if (i == slist->keys_size) {
4032 /* this list has no such key */
Michal Vasko53b7da02018-02-13 15:28:42 +01004033 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004034 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4035 parsed = -1;
4036 goto cleanup;
4037 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004038
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004039 /* check module */
4040 if (model) {
4041 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004042 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%.*s\", not \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004043 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4044 parsed = -1;
4045 goto cleanup;
4046 }
4047 } else {
4048 if (list_keys[i]->module != prev_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004049 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects key \"%s\" from module \"%s\", not \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004050 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4051 parsed = -1;
4052 goto cleanup;
4053 }
4054 }
4055
4056 /* find the actual data key */
4057 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4058 if (key->schema == (struct lys_node *)list_keys[i]) {
4059 break;
4060 }
4061 }
4062 if (!key) {
4063 /* list instance is missing a key? definitely should not happen */
Michal Vasko53b7da02018-02-13 15:28:42 +01004064 LOGINT(ctx);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004065 parsed = -1;
4066 goto cleanup;
4067 }
4068
4069 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004070 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004071 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004072 /* we still want to parse the whole predicate */
4073 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004074 }
4075
4076 /* everything is fine, mark this key as resolved */
4077 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004078 }
4079 } while (has_predicate);
4080
Michal Vaskob2f40be2016-09-08 16:03:48 +02004081 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004082 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004083 for (i = 0; i < slist->keys_size; ++i) {
4084 if (list_keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004085 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier is missing list key \"%s\".", list_keys[i]->name);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004086 parsed = -1;
4087 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004088 }
4089 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004090 }
4091
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004092cleanup:
4093 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004094 return parsed;
4095}
4096
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004097int
Michal Vaskof96dfb62017-08-17 12:23:49 +02004098lys_check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004099{
Michal Vasko0b963112017-08-11 12:45:36 +02004100 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004101 struct lyxp_set set;
Michal Vasko769f8032017-01-24 13:11:55 +01004102 int ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004103
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004104 if (check_place) {
4105 parent = node;
4106 while (parent) {
4107 if (parent->nodetype == LYS_GROUPING) {
4108 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004109 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004110 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004111 if (parent->nodetype == LYS_AUGMENT) {
4112 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004113 /* unresolved augment, skip for now (will be checked later) */
4114 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004115 } else {
4116 parent = ((struct lys_node_augment *)parent)->target;
4117 continue;
4118 }
4119 }
4120 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004121 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004122 }
4123
Michal Vaskof96dfb62017-08-17 12:23:49 +02004124 ret = lyxp_node_atomize(node, &set, 1);
Michal Vasko769f8032017-01-24 13:11:55 +01004125 if (ret == -1) {
4126 return -1;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004127 }
4128
Michal Vasko9e635ac2016-10-17 11:44:09 +02004129 free(set.val.snodes);
Michal Vasko769f8032017-01-24 13:11:55 +01004130 return ret;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004131}
4132
Radek Krejcif71f48f2016-10-25 16:37:24 +02004133static int
4134check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4135{
Radek Krejcidce5f972017-09-12 15:47:49 +02004136 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004137
4138 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004139 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4140 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004141 LOGVAL(leaf->module->ctx, LYE_SPEC, LY_VLOG_LYS, leaf, "The leafref %s is config but refers to a non-config %s.",
Radek Krejcif71f48f2016-10-25 16:37:24 +02004142 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4143 return -1;
4144 }
4145 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4146 * of leafref resolving (lys_leaf_add_leafref_target()) */
4147 } else if (type->base == LY_TYPE_UNION) {
4148 for (i = 0; i < type->info.uni.count; i++) {
4149 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4150 return -1;
4151 }
4152 }
4153 }
4154 return 0;
4155}
4156
Michal Vasko9e635ac2016-10-17 11:44:09 +02004157/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004158 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004159 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004160 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004161 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004162 * @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 +02004163 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004164 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004165 *
4166 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004167 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004168int
4169inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004170{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004171 struct lys_node_leaf *leaf;
Michal Vasko53b7da02018-02-13 15:28:42 +01004172 struct ly_ctx *ctx;
4173
4174 if (!node) {
4175 return 0;
4176 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004177
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004178 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Michal Vasko53b7da02018-02-13 15:28:42 +01004179 ctx = node->module->ctx;
4180
Radek Krejci1d82ef62015-08-07 14:44:40 +02004181 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004182 if (clear) {
4183 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004184 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004185 } else {
4186 if (node->flags & LYS_CONFIG_SET) {
4187 /* skip nodes with an explicit config value */
4188 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004189 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config");
4190 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004191 return -1;
4192 }
4193 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004194 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004195
4196 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4197 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4198 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004199 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4200 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004201 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
Michal Vaskoe022a562016-09-27 14:24:15 +02004202 return -1;
4203 }
4204 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004205 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004206 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004207 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004208 return -1;
4209 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004210 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4211 leaf = (struct lys_node_leaf *)node;
4212 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004213 return -1;
4214 }
4215 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004216 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004217
4218 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004219}
4220
Michal Vasko730dfdf2015-08-11 14:48:05 +02004221/**
Michal Vasko7178e692016-02-12 15:58:05 +01004222 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004223 *
Michal Vaskobb211122015-08-19 14:03:11 +02004224 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004225 * @param[in] uses Parent where to start the search in. If set, uses augment, if not, standalone augment.
Radek Krejcib3142312016-11-09 11:04:12 +01004226 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004227 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004228 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004229 */
Michal Vasko7178e692016-02-12 15:58:05 +01004230static int
Michal Vasko97234262018-02-01 09:53:01 +01004231resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004232{
Michal Vasko44ab1462017-05-18 13:18:36 +02004233 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004234 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004235 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004236 struct ly_set *set;
Michal Vasko53b7da02018-02-13 15:28:42 +01004237 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004238
Michal Vasko2ef7db62017-06-12 09:24:02 +02004239 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004240 mod = lys_main_module(aug->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01004241 ctx = mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004242
Michal Vaskobb520442017-05-23 10:55:18 +02004243 /* set it as not applied for now */
4244 aug->flags |= LYS_NOTAPPLIED;
4245
Michal Vasko2ef7db62017-06-12 09:24:02 +02004246 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004247 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004248 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004249 rc = resolve_schema_nodeid(aug->target_name, uses, (uses ? NULL : lys_node_module((struct lys_node *)aug)), &set, 0, 0);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004250 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004251 LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004252 return -1;
4253 }
Michal Vasko50576712017-07-28 12:28:33 +02004254 if (!set) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004255 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004256 return EXIT_FAILURE;
4257 }
Michal Vasko50576712017-07-28 12:28:33 +02004258 aug->target = set->set.s[0];
4259 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004260 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004261
Michal Vaskod58d5962016-03-02 14:29:41 +01004262 /* check for mandatory nodes - if the target node is in another module
4263 * the added nodes cannot be mandatory
4264 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004265 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4266 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004267 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004268 }
4269
Michal Vasko07e89ef2016-03-03 13:28:57 +01004270 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004271 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004272 LY_TREE_FOR(aug->child, sub) {
4273 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4274 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004275 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4276 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004277 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004278 return -1;
4279 }
4280 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004281 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004282 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004283 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004284 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4285 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004286 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004287 return -1;
4288 }
4289 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004290 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004291 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004292 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004293 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4294 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004295 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004296 return -1;
4297 }
4298 }
4299 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004300 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
4301 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004302 return -1;
4303 }
4304
Radek Krejcic071c542016-01-27 14:57:51 +01004305 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004306 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004307 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004308 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004309 }
4310 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004311
Michal Vasko44ab1462017-05-18 13:18:36 +02004312 if (!aug->child) {
4313 /* empty augment, nothing to connect, but it is techincally applied */
Michal Vasko53b7da02018-02-13 15:28:42 +01004314 LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name);
Michal Vasko44ab1462017-05-18 13:18:36 +02004315 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004316 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4317 /* we try to connect the augment only in case the module is implemented or
4318 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004319 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004320 }
4321
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004322 return EXIT_SUCCESS;
4323}
4324
Radek Krejcie534c132016-11-23 13:32:31 +01004325static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004326resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004327{
4328 enum LY_VLOG_ELEM vlog_type;
4329 void *vlog_node;
4330 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004331 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004332 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004333 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004334 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004335 struct lys_ext_instance *tmp_ext;
Michal Vasko53b7da02018-02-13 15:28:42 +01004336 struct ly_ctx *ctx = NULL;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004337 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004338
4339 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004340 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004341 vlog_node = info->parent;
4342 vlog_type = LY_VLOG_LYS;
4343 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004344 case LYEXT_PAR_MODULE:
4345 case LYEXT_PAR_IMPORT:
4346 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004347 vlog_node = NULL;
4348 vlog_type = LY_VLOG_LYS;
4349 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004350 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004351 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004352 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004353 break;
4354 }
4355
4356 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004357 /* YIN */
4358
Radek Krejcie534c132016-11-23 13:32:31 +01004359 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004360 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004361 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004362 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004363 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004364 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004365 ctx = mod->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01004366
4367 /* find the extension definition */
4368 e = NULL;
4369 for (i = 0; i < mod->extensions_size; i++) {
4370 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4371 e = &mod->extensions[i];
4372 break;
4373 }
4374 }
4375 /* try submodules */
4376 for (j = 0; !e && j < mod->inc_size; j++) {
4377 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4378 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4379 e = &mod->inc[j].submodule->extensions[i];
4380 break;
4381 }
4382 }
4383 }
4384 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004385 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004386 return EXIT_FAILURE;
4387 }
4388
4389 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004390
Radek Krejci72b35992017-01-04 16:27:44 +01004391 if (e->plugin && e->plugin->check_position) {
4392 /* common part - we have plugin with position checking function, use it first */
4393 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4394 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004395 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004396 return -1;
4397 }
4398 }
4399
Radek Krejci8d6b7422017-02-03 14:42:13 +01004400 /* extension type-specific part - allocation */
4401 if (e->plugin) {
4402 etype = e->plugin->type;
4403 } else {
4404 /* default type */
4405 etype = LYEXT_FLAG;
4406 }
4407 switch (etype) {
4408 case LYEXT_FLAG:
4409 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4410 break;
4411 case LYEXT_COMPLEX:
4412 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4413 break;
4414 case LYEXT_ERR:
4415 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004416 LOGINT(ctx);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004417 return -1;
4418 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004419 LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004420
4421 /* common part for all extension types */
4422 (*ext)->def = e;
4423 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004424 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004425 (*ext)->insubstmt = info->substmt;
4426 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004427 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004428
4429 if (!(e->flags & LYS_YINELEM) && e->argument) {
4430 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4431 if (!(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004432 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004433 return -1;
4434 }
4435 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4436 }
4437
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004438 (*ext)->nodetype = LYS_EXT;
4439 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004440
Radek Krejci8d6b7422017-02-03 14:42:13 +01004441 /* extension type-specific part - parsing content */
4442 switch (etype) {
4443 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004444 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4445 if (!yin->ns) {
4446 /* garbage */
4447 lyxml_free(mod->ctx, yin);
4448 continue;
4449 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4450 /* standard YANG statements are not expected here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004451 LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004452 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004453 } else if (yin->ns == info->data.yin->ns &&
4454 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004455 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004456 if ((*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004457 LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004458 return -1;
4459 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004460 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004461 yin->content = NULL;
4462 lyxml_free(mod->ctx, yin);
4463 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004464 /* extension instance */
4465 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4466 LYEXT_SUBSTMT_SELF, 0, unres)) {
4467 return -1;
4468 }
Radek Krejci72b35992017-01-04 16:27:44 +01004469
Radek Krejci72b35992017-01-04 16:27:44 +01004470 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004471 }
Radek Krejci72b35992017-01-04 16:27:44 +01004472 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004473 break;
4474 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004475 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004476 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4477 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004478 return -1;
4479 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004480 break;
4481 default:
4482 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004483 }
Radek Krejci72b35992017-01-04 16:27:44 +01004484
4485 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4486
Radek Krejcie534c132016-11-23 13:32:31 +01004487 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004488 /* YANG */
4489
PavolVicanc1807262017-01-31 18:00:27 +01004490 ext_prefix = (char *)(*ext)->def;
4491 tmp = strchr(ext_prefix, ':');
4492 if (!tmp) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004493 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004494 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004495 }
4496 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004497
PavolVicanc1807262017-01-31 18:00:27 +01004498 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004499 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004500 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004501 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004502 return EXIT_FAILURE;
4503 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004504 ctx = mod->ctx;
PavolVicanc1807262017-01-31 18:00:27 +01004505
4506 /* find the extension definition */
4507 e = NULL;
4508 for (i = 0; i < mod->extensions_size; i++) {
4509 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4510 e = &mod->extensions[i];
4511 break;
4512 }
4513 }
4514 /* try submodules */
4515 for (j = 0; !e && j < mod->inc_size; j++) {
4516 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4517 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4518 e = &mod->inc[j].submodule->extensions[i];
4519 break;
4520 }
4521 }
4522 }
4523 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004524 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004525 return EXIT_FAILURE;
4526 }
4527
4528 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4529
4530 if (e->plugin && e->plugin->check_position) {
4531 /* common part - we have plugin with position checking function, use it first */
4532 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4533 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004534 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01004535 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004536 }
4537 }
4538
PavolVican22e88682017-02-14 22:38:18 +01004539 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01004540 (*ext)->flags &= ~LYEXT_OPT_YANG;
PavolVicanc1807262017-01-31 18:00:27 +01004541 (*ext)->def = e;
4542 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01004543 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican22e88682017-02-14 22:38:18 +01004544
PavolVicanb0d84102017-02-15 16:32:42 +01004545 if (e->argument && !(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004546 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
PavolVicanb0d84102017-02-15 16:32:42 +01004547 goto error;
4548 }
4549
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004550 (*ext)->module = info->mod;
4551 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004552
PavolVican22e88682017-02-14 22:38:18 +01004553 /* extension type-specific part */
4554 if (e->plugin) {
4555 etype = e->plugin->type;
4556 } else {
4557 /* default type */
4558 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01004559 }
PavolVican22e88682017-02-14 22:38:18 +01004560 switch (etype) {
4561 case LYEXT_FLAG:
4562 /* nothing change */
4563 break;
4564 case LYEXT_COMPLEX:
4565 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Michal Vasko53b7da02018-02-13 15:28:42 +01004566 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error);
PavolVican22e88682017-02-14 22:38:18 +01004567 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
4568 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01004569 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01004570 if (info->data.yang) {
4571 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01004572 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
4573 (struct lys_ext_instance_complex*)(*ext))) {
4574 goto error;
4575 }
4576 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
4577 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01004578 goto error;
4579 }
PavolVicana3876672017-02-21 15:49:51 +01004580 }
4581 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
4582 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01004583 }
PavolVican22e88682017-02-14 22:38:18 +01004584 break;
4585 case LYEXT_ERR:
4586 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004587 LOGINT(ctx);
PavolVican22e88682017-02-14 22:38:18 +01004588 goto error;
4589 }
4590
PavolVican22e88682017-02-14 22:38:18 +01004591 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
4592 goto error;
4593 }
4594 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01004595 }
4596
4597 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01004598error:
4599 free(ext_prefix);
4600 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01004601}
4602
Michal Vasko730dfdf2015-08-11 14:48:05 +02004603/**
Pavol Vican855ca622016-09-05 13:07:54 +02004604 * @brief Resolve (find) choice default case. Does not log.
4605 *
4606 * @param[in] choic Choice to use.
4607 * @param[in] dflt Name of the default case.
4608 *
4609 * @return Pointer to the default node or NULL.
4610 */
4611static struct lys_node *
4612resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
4613{
4614 struct lys_node *child, *ret;
4615
4616 LY_TREE_FOR(choic->child, child) {
4617 if (child->nodetype == LYS_USES) {
4618 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
4619 if (ret) {
4620 return ret;
4621 }
4622 }
4623
4624 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02004625 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02004626 return child;
4627 }
4628 }
4629
4630 return NULL;
4631}
4632
4633/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02004634 * @brief Resolve uses, apply augments, refines. Logs directly.
4635 *
Michal Vaskobb211122015-08-19 14:03:11 +02004636 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004637 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004638 *
Michal Vaskodef0db12015-10-07 13:22:48 +02004639 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004640 */
Michal Vasko184521f2015-09-24 13:14:26 +02004641static int
Radek Krejci48464ed2016-03-17 15:44:09 +01004642resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004643{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004644 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02004645 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02004646 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02004647 struct lys_node_leaflist *llist;
4648 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02004649 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004650 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004651 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004652 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004653 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02004654 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004655
Michal Vasko71e1aa82015-08-12 12:17:51 +02004656 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01004657
Radek Krejci93def382017-05-24 15:33:48 +02004658 /* check that the grouping is resolved (no unresolved uses inside) */
4659 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02004660
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004661 if (!uses->grp->child) {
4662 /* grouping without children, warning was already displayed */
4663 return EXIT_SUCCESS;
4664 }
4665
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004666 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01004667 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01004668 if (node_aux->nodetype & LYS_GROUPING) {
4669 /* do not instantiate groupings from groupings */
4670 continue;
4671 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01004672 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01004673 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004674 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
4675 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004676 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004677 }
Pavol Vican55abd332016-07-12 15:54:49 +02004678 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01004679 LY_TREE_FOR((uses->parent) ? *lys_child(uses->parent, LYS_USES) : lys_main_module(uses->module)->data, tmp) {
Pavol Vican2510ddc2016-07-18 16:23:44 +02004680 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004681 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02004682 }
4683 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004684 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004685
Michal Vaskodef0db12015-10-07 13:22:48 +02004686 /* we managed to copy the grouping, the rest must be possible to resolve */
4687
Pavol Vican855ca622016-09-05 13:07:54 +02004688 if (uses->refine_size) {
4689 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01004690 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004691 }
4692
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004693 /* apply refines */
4694 for (i = 0; i < uses->refine_size; i++) {
4695 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01004696 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
4697 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02004698 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01004699 if (rc || !node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004700 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004701 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004702 }
4703
Radek Krejci1d82ef62015-08-07 14:44:40 +02004704 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004705 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
4706 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02004707 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004708 }
Pavol Vican855ca622016-09-05 13:07:54 +02004709 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004710
4711 /* description on any nodetype */
4712 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004713 lydict_remove(ctx, node->dsc);
4714 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004715 }
4716
4717 /* reference on any nodetype */
4718 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004719 lydict_remove(ctx, node->ref);
4720 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004721 }
4722
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004723 /* config on any nodetype,
4724 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
4725 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004726 node->flags &= ~LYS_CONFIG_MASK;
4727 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004728 }
4729
4730 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02004731 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004732 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004733 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02004734 leaf = (struct lys_node_leaf *)node;
4735
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004736 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02004737 lydict_remove(ctx, leaf->dflt);
4738 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
4739
4740 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01004741 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
4742 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004743 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004744 }
Radek Krejci200bf712016-08-16 17:11:04 +02004745 } else if (node->nodetype == LYS_LEAFLIST) {
4746 /* leaf-list */
4747 llist = (struct lys_node_leaflist *)node;
4748
4749 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01004750 for (j = 0; j < llist->dflt_size; j++) {
4751 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02004752 }
4753 free(llist->dflt);
4754
4755 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02004756 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01004757 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail);
Radek Krejci200bf712016-08-16 17:11:04 +02004758 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01004759 for (j = 0; j < llist->dflt_size; j++) {
4760 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02004761 }
4762
4763 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01004764 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01004765 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01004766 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02004767 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02004768 }
4769 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004770 }
4771 }
4772
4773 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004774 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01004775 /* remove current value */
4776 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004777
Radek Krejcibf285832017-01-26 16:05:41 +01004778 /* set new value */
4779 node->flags |= (rfn->flags & LYS_MAND_MASK);
4780
Pavol Vican855ca622016-09-05 13:07:54 +02004781 if (rfn->flags & LYS_MAND_TRUE) {
4782 /* check if node has default value */
4783 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004784 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02004785 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004786 goto fail;
4787 }
4788 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004789 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02004790 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02004791 goto fail;
4792 }
4793 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004794 }
4795
4796 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004797 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
4798 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
4799 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004800 }
4801
4802 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004803 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004804 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004805 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004806 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004807 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004808 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004809 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004810 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004811 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004812 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004813 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02004814 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004815 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004816 }
4817 }
4818
4819 /* must in leaf, leaf-list, list, container or anyxml */
4820 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004821 switch (node->nodetype) {
4822 case LYS_LEAF:
4823 old_size = &((struct lys_node_leaf *)node)->must_size;
4824 old_must = &((struct lys_node_leaf *)node)->must;
4825 break;
4826 case LYS_LEAFLIST:
4827 old_size = &((struct lys_node_leaflist *)node)->must_size;
4828 old_must = &((struct lys_node_leaflist *)node)->must;
4829 break;
4830 case LYS_LIST:
4831 old_size = &((struct lys_node_list *)node)->must_size;
4832 old_must = &((struct lys_node_list *)node)->must;
4833 break;
4834 case LYS_CONTAINER:
4835 old_size = &((struct lys_node_container *)node)->must_size;
4836 old_must = &((struct lys_node_container *)node)->must;
4837 break;
4838 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02004839 case LYS_ANYDATA:
4840 old_size = &((struct lys_node_anydata *)node)->must_size;
4841 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004842 break;
4843 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01004844 LOGINT(ctx);
Michal Vaskoa86508c2016-08-26 14:30:19 +02004845 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004846 }
4847
4848 size = *old_size + rfn->must_size;
4849 must = realloc(*old_must, size * sizeof *rfn->must);
Michal Vasko53b7da02018-02-13 15:28:42 +01004850 LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004851 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01004852 must[j].ext_size = rfn->must[k].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004853 lys_ext_dup(rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004854 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02004855 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
4856 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
4857 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
4858 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
4859 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02004860 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004861 }
4862
Michal Vaskoef2fdc82015-09-24 09:54:42 +02004863 *old_must = must;
4864 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02004865
4866 /* check XPath dependencies again */
4867 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
4868 goto fail;
4869 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004870 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02004871
4872 /* if-feature in leaf, leaf-list, list, container or anyxml */
4873 if (rfn->iffeature_size) {
4874 old_size = &node->iffeature_size;
4875 old_iff = &node->iffeature;
4876
4877 size = *old_size + rfn->iffeature_size;
4878 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01004879 LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02004880 *old_iff = iff;
4881
Pavol Vican855ca622016-09-05 13:07:54 +02004882 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
4883 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004884 if (usize1) {
4885 /* there is something to duplicate */
4886 /* duplicate compiled expression */
4887 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
4888 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01004889 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004890 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004891
4892 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02004893 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01004894 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02004895 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004896
Radek Krejci3a3b2002017-09-13 16:39:02 +02004897 /* duplicate extensions */
4898 iff[j].ext_size = rfn->iffeature[k].ext_size;
4899 lys_ext_dup(rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
4900 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
4901 }
4902 (*old_size)++;
4903 }
4904 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02004905 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004906 }
4907
4908 /* apply augments */
4909 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01004910 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004911 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004912 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004913 }
4914 }
4915
Pavol Vican855ca622016-09-05 13:07:54 +02004916 /* check refines */
4917 for (i = 0; i < uses->refine_size; i++) {
4918 node = refine_nodes[i];
4919 rfn = &uses->refine[i];
4920
4921 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004922 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02004923 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01004924 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02004925 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
4926 (rfn->flags & LYS_CONFIG_W)) {
4927 /* setting config true under config false is prohibited */
Michal Vasko53b7da02018-02-13 15:28:42 +01004928 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4929 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004930 "changing config from 'false' to 'true' is prohibited while "
4931 "the target's parent is still config 'false'.");
4932 goto fail;
4933 }
4934
4935 /* inherit config change to the target children */
4936 LY_TREE_DFS_BEGIN(node->child, next, iter) {
4937 if (rfn->flags & LYS_CONFIG_W) {
4938 if (iter->flags & LYS_CONFIG_SET) {
4939 /* config is set explicitely, go to next sibling */
4940 next = NULL;
4941 goto nextsibling;
4942 }
4943 } else { /* LYS_CONFIG_R */
4944 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
4945 /* error - we would have config data under status data */
Michal Vasko53b7da02018-02-13 15:28:42 +01004946 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
4947 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02004948 "changing config from 'true' to 'false' is prohibited while the target "
4949 "has still a children with explicit config 'true'.");
4950 goto fail;
4951 }
4952 }
4953 /* change config */
4954 iter->flags &= ~LYS_CONFIG_MASK;
4955 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
4956
4957 /* select next iter - modified LY_TREE_DFS_END */
4958 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4959 next = NULL;
4960 } else {
4961 next = iter->child;
4962 }
4963nextsibling:
4964 if (!next) {
4965 /* try siblings */
4966 next = iter->next;
4967 }
4968 while (!next) {
4969 /* parent is already processed, go to its sibling */
4970 iter = lys_parent(iter);
4971
4972 /* no siblings, go back through parents */
4973 if (iter == node) {
4974 /* we are done, no next element to process */
4975 break;
4976 }
4977 next = iter->next;
4978 }
4979 }
4980 }
4981
4982 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004983 if (rfn->dflt_size) {
4984 if (node->nodetype == LYS_CHOICE) {
4985 /* choice */
4986 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
4987 rfn->dflt[0]);
4988 if (!((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004989 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
Radek Krejci4c5fbd32016-10-25 15:14:23 +02004990 goto fail;
4991 }
4992 if (lyp_check_mandatory_choice(node)) {
4993 goto fail;
4994 }
Pavol Vican855ca622016-09-05 13:07:54 +02004995 }
4996 }
4997
4998 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02004999 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005000 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005001 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5002 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005003 goto fail;
5004 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005005 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005006 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005007 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5008 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005009 goto fail;
5010 }
5011 }
5012
5013 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005014 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005015 if (node->nodetype == LYS_LEAFLIST) {
5016 llist = (struct lys_node_leaflist *)node;
5017 if (llist->dflt_size && llist->min) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005018 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5019 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005020 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5021 goto fail;
5022 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005023 } else if (node->nodetype == LYS_LEAF) {
5024 leaf = (struct lys_node_leaf *)node;
5025 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005026 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5027 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005028 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5029 goto fail;
5030 }
Pavol Vican855ca622016-09-05 13:07:54 +02005031 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005032
Pavol Vican855ca622016-09-05 13:07:54 +02005033 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005034 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005035 for (parent = node->parent;
5036 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5037 parent = parent->parent) {
5038 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5039 /* stop also on presence containers */
5040 break;
5041 }
5042 }
5043 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5044 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5045 if (lyp_check_mandatory_choice(parent)) {
5046 goto fail;
5047 }
5048 }
5049 }
5050 }
5051 free(refine_nodes);
5052
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005053 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005054
5055fail:
5056 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5057 lys_node_free(iter, NULL, 0);
5058 }
Pavol Vican855ca622016-09-05 13:07:54 +02005059 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005060 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005061}
5062
Radek Krejci83a4bac2017-02-07 15:53:04 +01005063void
5064resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005065{
5066 int i;
5067
5068 assert(der && base);
5069
Radek Krejci018f1f52016-08-03 16:01:20 +02005070 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005071 /* create a set for backlinks if it does not exist */
5072 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005073 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005074 /* store backlink */
5075 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005076
Radek Krejci85a54be2016-10-20 12:39:56 +02005077 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005078 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005079 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005080 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005081}
5082
Michal Vasko730dfdf2015-08-11 14:48:05 +02005083/**
5084 * @brief Resolve base identity recursively. Does not log.
5085 *
5086 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005087 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005088 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005089 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005090 *
Radek Krejci219fa612016-08-15 10:36:51 +02005091 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005092 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005093static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005094resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005095 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005096{
Michal Vaskof02e3742015-08-05 16:27:02 +02005097 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005098 struct lys_ident *base = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005099 struct ly_ctx *ctx = module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005100
Radek Krejcicf509982015-12-15 09:22:44 +01005101 assert(ret);
5102
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005103 /* search module */
5104 for (i = 0; i < module->ident_size; i++) {
5105 if (!strcmp(basename, module->ident[i].name)) {
5106
5107 if (!ident) {
5108 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005109 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005110 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005111 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005112 }
5113
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005114 base = &module->ident[i];
5115 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005116 }
5117 }
5118
5119 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005120 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5121 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5122 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005123
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005124 if (!ident) {
5125 *ret = &module->inc[j].submodule->ident[i];
5126 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005127 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005128
5129 base = &module->inc[j].submodule->ident[i];
5130 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005131 }
5132 }
5133 }
5134
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005135matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005136 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005137 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005138 /* is it already completely resolved? */
5139 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005140 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005141 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5142
5143 /* simple check for circular reference,
5144 * the complete check is done as a side effect of using only completely
5145 * resolved identities (previous check of unres content) */
5146 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005147 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5148 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005149 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005150 }
5151
Radek Krejci06f64ed2016-08-15 11:07:44 +02005152 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005153 }
5154 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005155
Radek Krejcibabbff82016-02-19 13:31:37 +01005156 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005157 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005158 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005159 }
5160
Radek Krejci219fa612016-08-15 10:36:51 +02005161 /* base not found (maybe a forward reference) */
5162 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005163}
5164
Michal Vasko730dfdf2015-08-11 14:48:05 +02005165/**
5166 * @brief Resolve base identity. Logs directly.
5167 *
5168 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005169 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005170 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005171 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005172 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005173 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005174 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005175 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005176static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005177resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005178 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005179{
5180 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005181 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005182 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005183 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005184 struct lys_module *mod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005185 struct ly_ctx *ctx = module->ctx;
Radek Krejcicf509982015-12-15 09:22:44 +01005186
5187 assert((ident && !type) || (!ident && type));
5188
5189 if (!type) {
5190 /* have ident to resolve */
5191 ret = &target;
5192 flags = ident->flags;
5193 mod = ident->module;
5194 } else {
5195 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005196 ++type->info.ident.count;
5197 type->info.ident.ref = ly_realloc(type->info.ident.ref, type->info.ident.count * sizeof *type->info.ident.ref);
Michal Vasko53b7da02018-02-13 15:28:42 +01005198 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005199
5200 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005201 flags = type->parent->flags;
5202 mod = type->parent->module;
5203 }
Michal Vaskof2006002016-04-21 16:28:15 +02005204 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005205
5206 /* search for the base identity */
5207 name = strchr(basename, ':');
5208 if (name) {
5209 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005210 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005211 name++;
5212
Michal Vasko2d851a92015-10-20 16:16:36 +02005213 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005214 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005215 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005216 }
5217 } else {
5218 name = basename;
5219 }
5220
Radek Krejcic071c542016-01-27 14:57:51 +01005221 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005222 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005223 if (!module) {
5224 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01005225 LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005226 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005227 }
5228
Radek Krejcic071c542016-01-27 14:57:51 +01005229 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005230 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5231 if (!rc) {
5232 assert(*ret);
5233
5234 /* check status */
5235 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5236 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5237 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005238 } else if (ident) {
5239 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005240 if (lys_main_module(mod)->implemented) {
5241 /* in case of the implemented identity, maintain backlinks to it
5242 * from the base identities to make it available when resolving
5243 * data with the identity values (not implemented identity is not
5244 * allowed as an identityref value). */
5245 resolve_identity_backlink_update(ident, *ret);
5246 }
Radek Krejci219fa612016-08-15 10:36:51 +02005247 }
5248 } else if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005249 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005250 if (type) {
5251 --type->info.ident.count;
5252 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005253 }
5254
Radek Krejci219fa612016-08-15 10:36:51 +02005255 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005256}
5257
Radek Krejci9e6af732017-04-27 14:40:25 +02005258/*
5259 * 1 - true (der is derived from base)
5260 * 0 - false (der is not derived from base)
5261 */
5262static int
5263search_base_identity(struct lys_ident *der, struct lys_ident *base)
5264{
5265 int i;
5266
5267 if (der == base) {
5268 return 1;
5269 } else {
5270 for(i = 0; i < der->base_size; i++) {
5271 if (search_base_identity(der->base[i], base) == 1) {
5272 return 1;
5273 }
5274 }
5275 }
5276
5277 return 0;
5278}
5279
Michal Vasko730dfdf2015-08-11 14:48:05 +02005280/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005281 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005282 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005283 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005284 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005285 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005286 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005287 *
5288 * @return Pointer to the identity resolvent, NULL on error.
5289 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005290struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005291resolve_identref(struct lys_type *type, const char *ident_name, struct lyd_node *node, struct lys_module *mod, int dflt)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005292{
Radek Krejci9e6af732017-04-27 14:40:25 +02005293 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005294 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005295 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005296 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005297 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005298 struct lys_ident *der, *cur;
Radek Krejci9e6af732017-04-27 14:40:25 +02005299 struct lys_module *imod = NULL, *m;
Michal Vasko53b7da02018-02-13 15:28:42 +01005300 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005301
Radek Krejci9e6af732017-04-27 14:40:25 +02005302 assert(type && ident_name && node && mod);
Michal Vasko53b7da02018-02-13 15:28:42 +01005303 ctx = mod->ctx;
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005304
Michal Vaskof2d43962016-09-02 11:10:16 +02005305 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005306 return NULL;
5307 }
5308
Michal Vasko50576712017-07-28 12:28:33 +02005309 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005310 if (rc < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005311 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005312 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005313 } else if (rc < (signed)strlen(ident_name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005314 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005315 return NULL;
5316 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005317
5318 m = lys_main_module(mod); /* shortcut */
5319 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5320 /* identity is defined in the same module as node */
5321 imod = m;
5322 } else if (dflt) {
5323 /* solving identityref in default definition in schema -
5324 * find the identity's module in the imported modules list to have a correct revision */
5325 for (i = 0; i < mod->imp_size; i++) {
5326 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5327 imod = mod->imp[i].module;
5328 break;
5329 }
5330 }
5331 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005332 /* solving identityref in data - get the module from the context */
5333 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5334 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005335 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005336 break;
5337 }
Michal Vasko08767f72017-10-06 14:38:08 +02005338 imod = NULL;
5339 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005340 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5341 /* we are currently parsing some module and checking XPath or a default value,
5342 * so take this module into account */
5343 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5344 imod = mod->ctx->models.parsing_sub_modules[i];
5345 if (imod->type) {
5346 /* skip submodules */
5347 continue;
5348 }
5349 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5350 break;
5351 }
5352 imod = NULL;
5353 }
5354 }
Michal Vasko08767f72017-10-06 14:38:08 +02005355 }
5356
Michal Vasko53b7da02018-02-13 15:28:42 +01005357 if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) {
Michal Vasko08767f72017-10-06 14:38:08 +02005358 /* the needed module was not found, but it may have been expected so call the data callback */
5359 if (imod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005360 ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005361 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005362 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01005363 imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
Michal Vasko08767f72017-10-06 14:38:08 +02005364 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005365 }
5366 }
5367 if (!imod) {
5368 goto fail;
5369 }
5370
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005371 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005372 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005373 * THEN, we may need to make the module with the identity implemented, but only if it really
5374 * contains the identity */
5375 if (!imod->implemented) {
5376 cur = NULL;
5377 /* get the identity in the module */
5378 for (i = 0; i < imod->ident_size; i++) {
5379 if (!strcmp(name, imod->ident[i].name)) {
5380 cur = &imod->ident[i];
5381 break;
5382 }
5383 }
5384 if (!cur) {
5385 /* go through includes */
5386 for (j = 0; j < imod->inc_size; j++) {
5387 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5388 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5389 cur = &imod->inc[j].submodule->ident[i];
5390 break;
5391 }
5392 }
5393 }
5394 if (!cur) {
5395 goto fail;
5396 }
5397 }
5398
5399 /* check that identity is derived from one of the type's base */
5400 while (type->der) {
5401 for (i = 0; i < type->info.ident.count; i++) {
5402 if (search_base_identity(cur, type->info.ident.ref[i])) {
5403 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005404 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005405 goto match;
5406 }
5407 }
5408 type = &type->der->type;
5409 }
5410 /* matching base not found */
Michal Vasko53b7da02018-02-13 15:28:42 +01005411 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
Radek Krejci9e6af732017-04-27 14:40:25 +02005412 goto fail;
5413 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005414 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005415
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005416 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005417 while (type->der) {
5418 for (i = 0; i < type->info.ident.count; ++i) {
5419 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005420
Radek Krejci85a54be2016-10-20 12:39:56 +02005421 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005422 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005423 for (j = 0; j < cur->der->number; j++) {
5424 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005425 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005426 /* we have match */
5427 cur = der;
5428 goto match;
5429 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005430 }
5431 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005432 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005433 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005434 }
5435
Radek Krejci9e6af732017-04-27 14:40:25 +02005436fail:
Michal Vasko53b7da02018-02-13 15:28:42 +01005437 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005438 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005439
5440match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005441 for (i = 0; i < cur->iffeature_size; i++) {
5442 if (!resolve_iffeature(&cur->iffeature[i])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005443 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
5444 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Identity \"%s\" is disabled by its if-feature condition.", cur->name);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005445 return NULL;
5446 }
5447 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005448 if (need_implemented) {
5449 if (dflt) {
5450 /* try to make the module implemented */
5451 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5452 imod->name, cur->name, mod->name);
5453 if (lys_set_implemented(imod)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005454 LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005455 imod->name, cur->name);
Michal Vasko53b7da02018-02-13 15:28:42 +01005456 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, node, "Identity used as identityref value is not implemented.");
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005457 goto fail;
5458 }
5459 } else {
5460 /* just say that it was found, but in a non-implemented module */
Michal Vasko53b7da02018-02-13 15:28:42 +01005461 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005462 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005463 goto fail;
5464 }
5465 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005466 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005467}
5468
Michal Vasko730dfdf2015-08-11 14:48:05 +02005469/**
Michal Vaskobb211122015-08-19 14:03:11 +02005470 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005471 *
Michal Vaskobb211122015-08-19 14:03:11 +02005472 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005473 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005474 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005475 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005476 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005477static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005478resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005479{
Radek Krejci93def382017-05-24 15:33:48 +02005480 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005481 struct lys_node *par_grp;
Michal Vasko53b7da02018-02-13 15:28:42 +01005482 struct ly_ctx *ctx = uses->module->ctx;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005483
Radek Krejci6ff885d2017-01-03 14:06:22 +01005484 /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself is used
Radek Krejci93def382017-05-24 15:33:48 +02005485 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5486 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5487 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005488 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 +02005489
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005490 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005491 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5492 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005493 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005494 return -1;
5495 } else if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005496 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005497 return -1;
5498 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005499 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005500 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005501 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005502 return -1;
5503 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005504 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005505 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005506 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005507 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005508 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005509 }
5510
Radek Krejci93def382017-05-24 15:33:48 +02005511 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005512 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005513 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005514 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005515 return -1;
5516 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005517 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02005518 } else {
5519 /* instantiate grouping only when it is completely resolved */
5520 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005521 }
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005522 return EXIT_FAILURE;
5523 }
5524
Radek Krejci48464ed2016-03-17 15:44:09 +01005525 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005526 if (!rc) {
5527 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01005528 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005529 assert(((struct lys_node_grp *)par_grp)->unres_count);
5530 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01005531 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005532 }
Radek Krejcicf509982015-12-15 09:22:44 +01005533
5534 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005535 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01005536 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005537 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01005538 return -1;
5539 }
5540
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005541 return EXIT_SUCCESS;
5542 }
5543
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005544 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005545}
5546
Michal Vasko730dfdf2015-08-11 14:48:05 +02005547/**
Michal Vasko9957e592015-08-17 15:04:09 +02005548 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005549 *
Michal Vaskobb211122015-08-19 14:03:11 +02005550 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005551 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005552 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005553 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005554 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005555static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005556resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005557{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005558 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01005559 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02005560 char *s = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005561 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005562
5563 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02005564 assert(keys_str);
5565
Radek Krejci5c08a992016-11-02 13:30:04 +01005566 if (!list->child) {
5567 /* no child, possible forward reference */
Michal Vasko53b7da02018-02-13 15:28:42 +01005568 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Radek Krejci5c08a992016-11-02 13:30:04 +01005569 return EXIT_FAILURE;
5570 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005571 /* get the key name */
5572 if ((value = strpbrk(keys_str, " \t\n"))) {
5573 len = value - keys_str;
5574 while (isspace(value[0])) {
5575 value++;
5576 }
5577 } else {
5578 len = strlen(keys_str);
5579 }
5580
Radek Krejcia98048c2017-05-24 16:35:48 +02005581 if (list->keys[i]) {
5582 /* skip already resolved keys */
5583 goto next;
5584 }
5585
Michal Vaskobb520442017-05-23 10:55:18 +02005586 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
5587 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005588 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005589 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02005590 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005591 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005592
Radek Krejci48464ed2016-03-17 15:44:09 +01005593 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005594 /* check_key logs */
5595 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005596 }
5597
Radek Krejcicf509982015-12-15 09:22:44 +01005598 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01005599 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01005600 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
5601 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01005602 return -1;
5603 }
5604
Radek Krejcia98048c2017-05-24 16:35:48 +02005605 /* default value - is ignored, keep it but print a warning */
5606 if (list->keys[i]->dflt) {
5607 /* log is not hidden only in case this resolving fails and in such a case
5608 * we cannot get here
5609 */
Michal Vasko53b7da02018-02-13 15:28:42 +01005610 assert(log_opt == ILO_STORE);
5611 log_opt = ILO_LOG;
5612 LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01005613 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Michal Vasko53b7da02018-02-13 15:28:42 +01005614 log_opt = ILO_STORE;
Radek Krejcia98048c2017-05-24 16:35:48 +02005615 free(s);
5616 }
5617
5618next:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005619 /* prepare for next iteration */
5620 while (value && isspace(value[0])) {
5621 value++;
5622 }
5623 keys_str = value;
5624 }
5625
Michal Vaskof02e3742015-08-05 16:27:02 +02005626 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005627}
5628
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005629/**
Michal Vaskobf19d252015-10-08 15:39:17 +02005630 * @brief Resolve (check) all must conditions of \p node.
5631 * Logs directly.
5632 *
5633 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005634 * @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 +02005635 *
5636 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
5637 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005638static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005639resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02005640{
Michal Vaskobf19d252015-10-08 15:39:17 +02005641 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005642 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02005643 struct lys_restr *must;
5644 struct lyxp_set set;
Michal Vasko53b7da02018-02-13 15:28:42 +01005645 struct ly_ctx *ctx = node->schema->module->ctx;
Michal Vaskobf19d252015-10-08 15:39:17 +02005646
5647 assert(node);
5648 memset(&set, 0, sizeof set);
5649
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005650 if (inout_parent) {
5651 for (schema = lys_parent(node->schema);
5652 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
5653 schema = lys_parent(schema));
5654 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005655 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005656 return -1;
5657 }
5658 must_size = ((struct lys_node_inout *)schema)->must_size;
5659 must = ((struct lys_node_inout *)schema)->must;
5660
5661 /* context node is the RPC/action */
5662 node = node->parent;
5663 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005664 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005665 return -1;
5666 }
5667 } else {
5668 switch (node->schema->nodetype) {
5669 case LYS_CONTAINER:
5670 must_size = ((struct lys_node_container *)node->schema)->must_size;
5671 must = ((struct lys_node_container *)node->schema)->must;
5672 break;
5673 case LYS_LEAF:
5674 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
5675 must = ((struct lys_node_leaf *)node->schema)->must;
5676 break;
5677 case LYS_LEAFLIST:
5678 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
5679 must = ((struct lys_node_leaflist *)node->schema)->must;
5680 break;
5681 case LYS_LIST:
5682 must_size = ((struct lys_node_list *)node->schema)->must_size;
5683 must = ((struct lys_node_list *)node->schema)->must;
5684 break;
5685 case LYS_ANYXML:
5686 case LYS_ANYDATA:
5687 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
5688 must = ((struct lys_node_anydata *)node->schema)->must;
5689 break;
5690 case LYS_NOTIF:
5691 must_size = ((struct lys_node_notif *)node->schema)->must_size;
5692 must = ((struct lys_node_notif *)node->schema)->must;
5693 break;
5694 default:
5695 must_size = 0;
5696 break;
5697 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005698 }
5699
5700 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005701 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02005702 return -1;
5703 }
5704
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005705 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02005706
Michal Vasko8146d4c2016-05-09 15:50:29 +02005707 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02005708 if ((ignore_fail == 1) || ((must[i].flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005709 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
5710 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01005711 LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005712 if (must[i].emsg) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005713 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005714 }
5715 if (must[i].eapptag) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005716 ly_err_last_set_apptag(ctx, must[i].eapptag);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005717 }
5718 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02005719 }
Michal Vaskobf19d252015-10-08 15:39:17 +02005720 }
5721 }
5722
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005723 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02005724}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005725
Michal Vaskobf19d252015-10-08 15:39:17 +02005726/**
Michal Vasko508a50d2016-09-07 14:50:33 +02005727 * @brief Resolve (find) when condition schema context node. Does not log.
5728 *
5729 * @param[in] schema Schema node with the when condition.
5730 * @param[out] ctx_snode When schema context node.
5731 * @param[out] ctx_snode_type Schema context node type.
5732 */
5733void
5734resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
5735{
5736 const struct lys_node *sparent;
5737
5738 /* find a not schema-only node */
5739 *ctx_snode_type = LYXP_NODE_ELEM;
5740 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
5741 if (schema->nodetype == LYS_AUGMENT) {
5742 sparent = ((struct lys_node_augment *)schema)->target;
5743 } else {
5744 sparent = schema->parent;
5745 }
5746 if (!sparent) {
5747 /* context node is the document root (fake root in our case) */
5748 if (schema->flags & LYS_CONFIG_W) {
5749 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
5750 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02005751 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02005752 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02005753 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskocb45f472018-02-12 10:47:42 +01005754 schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko508a50d2016-09-07 14:50:33 +02005755 break;
5756 }
5757 schema = sparent;
5758 }
5759
5760 *ctx_snode = (struct lys_node *)schema;
5761}
5762
5763/**
Michal Vaskocf024702015-10-08 15:01:42 +02005764 * @brief Resolve (find) when condition context node. Does not log.
5765 *
5766 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02005767 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02005768 * @param[out] ctx_node Context node.
5769 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02005770 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02005771 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02005772 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02005773static int
5774resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
5775 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005776{
Michal Vaskocf024702015-10-08 15:01:42 +02005777 struct lyd_node *parent;
5778 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02005779 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02005780 uint16_t i, data_depth, schema_depth;
5781
Michal Vasko508a50d2016-09-07 14:50:33 +02005782 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02005783
Michal Vaskofe989752016-09-08 08:47:26 +02005784 if (node_type == LYXP_NODE_ELEM) {
5785 /* standard element context node */
5786 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
5787 for (sparent = schema, schema_depth = 0;
5788 sparent;
5789 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
5790 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
5791 ++schema_depth;
5792 }
Michal Vaskocf024702015-10-08 15:01:42 +02005793 }
Michal Vaskofe989752016-09-08 08:47:26 +02005794 if (data_depth < schema_depth) {
5795 return -1;
5796 }
Michal Vaskocf024702015-10-08 15:01:42 +02005797
Michal Vasko956e8542016-08-26 09:43:35 +02005798 /* find the corresponding data node */
5799 for (i = 0; i < data_depth - schema_depth; ++i) {
5800 node = node->parent;
5801 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02005802 if (node->schema != schema) {
5803 return -1;
5804 }
Michal Vaskofe989752016-09-08 08:47:26 +02005805 } else {
5806 /* root context node */
5807 while (node->parent) {
5808 node = node->parent;
5809 }
5810 while (node->prev->next) {
5811 node = node->prev;
5812 }
Michal Vaskocf024702015-10-08 15:01:42 +02005813 }
5814
Michal Vaskoa59495d2016-08-22 09:18:58 +02005815 *ctx_node = node;
5816 *ctx_node_type = node_type;
5817 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02005818}
5819
Michal Vasko76c3bd32016-08-24 16:02:52 +02005820/**
5821 * @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 +01005822 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02005823 *
5824 * @param[in] snode Schema node, whose children instances need to be unlinked.
5825 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
5826 * it is moved to point to another sibling still in the original tree.
5827 * @param[in,out] ctx_node When context node, adjusted if needed.
5828 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
5829 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
5830 * Ordering may change, but there will be no semantic change.
5831 *
5832 * @return EXIT_SUCCESS on success, -1 on error.
5833 */
5834static int
5835resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
5836 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
5837{
5838 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005839 const struct lys_node *slast;
Michal Vasko53b7da02018-02-13 15:28:42 +01005840 struct ly_ctx *ctx = snode->module->ctx;
Michal Vasko76c3bd32016-08-24 16:02:52 +02005841
5842 switch (snode->nodetype) {
5843 case LYS_AUGMENT:
5844 case LYS_USES:
5845 case LYS_CHOICE:
5846 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005847 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01005848 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01005849 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
5850 continue;
5851 }
5852
5853 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005854 return -1;
5855 }
5856 }
5857 break;
5858 case LYS_CONTAINER:
5859 case LYS_LIST:
5860 case LYS_LEAF:
5861 case LYS_LEAFLIST:
5862 case LYS_ANYXML:
5863 case LYS_ANYDATA:
5864 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
5865 if (elem->schema == snode) {
5866
5867 if (elem == *ctx_node) {
5868 /* We are going to unlink our context node! This normally cannot happen,
5869 * but we use normal top-level data nodes for faking a document root node,
5870 * so if this is the context node, we just use the next top-level node.
5871 * Additionally, it can even happen that there are no top-level data nodes left,
5872 * all were unlinked, so in this case we pass NULL as the context node/data tree,
5873 * lyxp_eval() can handle this special situation.
5874 */
5875 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005876 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005877 return -1;
5878 }
5879
5880 if (elem->prev == elem) {
5881 /* unlinking last top-level element, use an empty data tree */
5882 *ctx_node = NULL;
5883 } else {
5884 /* in this case just use the previous/last top-level data node */
5885 *ctx_node = elem->prev;
5886 }
5887 } else if (elem == *node) {
5888 /* We are going to unlink the currently processed node. This does not matter that
5889 * much, but we would lose access to the original data tree, so just move our
5890 * pointer somewhere still inside it.
5891 */
5892 if ((*node)->prev != *node) {
5893 *node = (*node)->prev;
5894 } else {
5895 /* the processed node with sibings were all unlinked, oh well */
5896 *node = NULL;
5897 }
5898 }
5899
5900 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01005901 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005902 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01005903 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005904 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005905 return -1;
5906 }
5907 } else {
5908 *unlinked_nodes = elem;
5909 }
5910
5911 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
5912 /* there can be only one instance */
5913 break;
5914 }
5915 }
5916 }
5917 break;
5918 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01005919 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005920 return -1;
5921 }
5922
5923 return EXIT_SUCCESS;
5924}
5925
5926/**
5927 * @brief Relink the unlinked nodes back.
5928 *
5929 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
5930 * we simply need a sibling from the original data tree.
5931 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
5932 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
5933 * or the sibling of \p unlinked_nodes.
5934 *
5935 * @return EXIT_SUCCESS on success, -1 on error.
5936 */
5937static int
5938resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
5939{
5940 struct lyd_node *elem;
5941
5942 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005943 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02005944 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005945 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005946 return -1;
5947 }
5948 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01005949 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02005950 return -1;
5951 }
5952 }
5953 }
5954
5955 return EXIT_SUCCESS;
5956}
5957
Radek Krejci03b71f72016-03-16 11:10:09 +01005958int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005959resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01005960{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005961 int ret = 0;
5962 uint8_t must_size;
5963 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02005964
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005965 assert(node);
5966
5967 schema = node->schema;
5968
5969 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02005970 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01005971 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005972 must_size = ((struct lys_node_container *)schema)->must_size;
5973 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005974 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005975 must_size = ((struct lys_node_leaf *)schema)->must_size;
5976 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005977 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005978 must_size = ((struct lys_node_leaflist *)schema)->must_size;
5979 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005980 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005981 must_size = ((struct lys_node_list *)schema)->must_size;
5982 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005983 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005984 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005985 must_size = ((struct lys_node_anydata *)schema)->must_size;
5986 break;
5987 case LYS_NOTIF:
5988 must_size = ((struct lys_node_notif *)schema)->must_size;
5989 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005990 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005991 must_size = 0;
5992 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01005993 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02005994
5995 if (must_size) {
5996 ++ret;
5997 }
5998
5999 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6000 if (!node->prev->next) {
6001 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6002 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6003 ret += 0x2;
6004 }
6005 }
6006
6007 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006008}
6009
6010int
Radek Krejci46165822016-08-26 14:06:27 +02006011resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006012{
Radek Krejci46165822016-08-26 14:06:27 +02006013 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006014
Radek Krejci46165822016-08-26 14:06:27 +02006015 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006016
Radek Krejci46165822016-08-26 14:06:27 +02006017 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006018 return 1;
6019 }
6020
Radek Krejci46165822016-08-26 14:06:27 +02006021 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006022 goto check_augment;
6023
Radek Krejci46165822016-08-26 14:06:27 +02006024 while (parent) {
6025 /* stop conditions */
6026 if (!mode) {
6027 /* stop on node that can be instantiated in data tree */
6028 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6029 break;
6030 }
6031 } else {
6032 /* stop on the specified node */
6033 if (parent == stop) {
6034 break;
6035 }
6036 }
6037
6038 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006039 return 1;
6040 }
6041check_augment:
6042
6043 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02006044 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006045 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006046 }
6047 parent = lys_parent(parent);
6048 }
6049
6050 return 0;
6051}
6052
Michal Vaskocf024702015-10-08 15:01:42 +02006053/**
6054 * @brief Resolve (check) all when conditions relevant for \p node.
6055 * Logs directly.
6056 *
6057 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006058 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6059 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006060 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006061 * @return
6062 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006063 * 0 - all "when" statements true
6064 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006065 * 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 +02006066 */
Radek Krejci46165822016-08-26 14:06:27 +02006067int
Michal Vasko0b963112017-08-11 12:45:36 +02006068resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006069{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006070 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006071 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006072 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006073 enum lyxp_node_type ctx_node_type;
Michal Vasko53b7da02018-02-13 15:28:42 +01006074 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci51093642016-03-29 10:14:59 +02006075 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006076
6077 assert(node);
6078 memset(&set, 0, sizeof set);
6079
Michal Vasko78d97e22017-02-21 09:54:38 +01006080 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006081 /* make the node dummy for the evaluation */
6082 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006083 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
6084 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006085 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006086 if (rc) {
6087 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006088 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006089 }
Radek Krejci51093642016-03-29 10:14:59 +02006090 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006091 }
6092
Radek Krejci03b71f72016-03-16 11:10:09 +01006093 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006094 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), 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 Vasko0b963112017-08-11 12:45:36 +02006097 if ((ignore_fail == 1)
6098 || ((((struct lys_node_container *)node->schema)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006099 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6100 ((struct lys_node_container *)node->schema)->when->cond);
6101 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006102 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006103 if (failed_when) {
6104 *failed_when = ((struct lys_node_container *)node->schema)->when;
6105 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006106 goto cleanup;
6107 }
Michal Vaskocf024702015-10-08 15:01:42 +02006108 }
Radek Krejci51093642016-03-29 10:14:59 +02006109
6110 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006111 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006112 }
6113
Michal Vasko90fc2a32016-08-24 15:58:58 +02006114 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006115 goto check_augment;
6116
6117 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006118 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6119 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006120 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006121 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006122 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006123 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006124 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006125 }
6126 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006127
6128 unlinked_nodes = NULL;
6129 /* we do not want our node pointer to change */
6130 tmp_node = node;
6131 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6132 if (rc) {
6133 goto cleanup;
6134 }
6135
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006136 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6137 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006138
6139 if (unlinked_nodes && ctx_node) {
6140 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6141 rc = -1;
6142 goto cleanup;
6143 }
6144 }
6145
Radek Krejci03b71f72016-03-16 11:10:09 +01006146 if (rc) {
6147 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006148 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006149 }
Radek Krejci51093642016-03-29 10:14:59 +02006150 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006151 }
6152
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006153 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006154 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02006155 if ((ignore_fail == 1)
6156 || ((((struct lys_node_uses *)sparent)->when->flags & LYS_XPATH_DEP) || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006157 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6158 ((struct lys_node_uses *)sparent)->when->cond);
6159 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006160 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko53b7da02018-02-13 15:28:42 +01006161 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006162 if (failed_when) {
6163 *failed_when = ((struct lys_node_uses *)sparent)->when;
6164 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006165 goto cleanup;
6166 }
Michal Vaskocf024702015-10-08 15:01:42 +02006167 }
Radek Krejci51093642016-03-29 10:14:59 +02006168
6169 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006170 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006171 }
6172
6173check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006174 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006175 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006176 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006177 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006178 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006179 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006180 }
6181 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006182
6183 unlinked_nodes = NULL;
6184 tmp_node = node;
6185 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6186 if (rc) {
6187 goto cleanup;
6188 }
6189
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006190 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6191 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006192
6193 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6194 * so the tree did not actually change and there is nothing for us to do
6195 */
6196 if (unlinked_nodes && ctx_node) {
6197 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6198 rc = -1;
6199 goto cleanup;
6200 }
6201 }
6202
Radek Krejci03b71f72016-03-16 11:10:09 +01006203 if (rc) {
6204 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006205 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006206 }
Radek Krejci51093642016-03-29 10:14:59 +02006207 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006208 }
6209
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006210 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006211 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006212 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006213 if ((ignore_fail == 1)
6214 || ((((struct lys_node_augment *)sparent->parent)->when->flags & LYS_XPATH_DEP) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006215 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006216 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006217 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006218 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006219 if (failed_when) {
6220 *failed_when = ((struct lys_node_augment *)sparent->parent)->when;
6221 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006222 goto cleanup;
6223 }
Michal Vaskocf024702015-10-08 15:01:42 +02006224 }
Radek Krejci51093642016-03-29 10:14:59 +02006225
6226 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006227 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006228 }
6229
Michal Vasko90fc2a32016-08-24 15:58:58 +02006230 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006231 }
6232
Radek Krejci0b7704f2016-03-18 12:16:14 +01006233 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006234
Radek Krejci51093642016-03-29 10:14:59 +02006235cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006236 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006237 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006238 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006239}
6240
Radek Krejcicbb473e2016-09-16 14:48:32 +02006241static int
6242check_leafref_features(struct lys_type *type)
6243{
6244 struct lys_node *iter;
6245 struct ly_set *src_parents, *trg_parents, *features;
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006246 struct lys_node_augment *aug;
Michal Vasko53b7da02018-02-13 15:28:42 +01006247 struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006248 unsigned int i, j, size, x;
6249 int ret = EXIT_SUCCESS;
6250
6251 assert(type->parent);
6252
6253 src_parents = ly_set_new();
6254 trg_parents = ly_set_new();
6255 features = ly_set_new();
6256
6257 /* get parents chain of source (leafref) */
Radek Krejciecda01a2017-04-05 15:44:27 +02006258 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006259 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6260 continue;
6261 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006262 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006263 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006264 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006265 /* unresolved augment, wait until it's resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01006266 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
Michal Vaskobd026102017-11-21 13:43:01 +01006267 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006268 ret = EXIT_FAILURE;
6269 goto cleanup;
6270 }
6271 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006272 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
6273 }
6274 /* get parents chain of target */
Radek Krejciecda01a2017-04-05 15:44:27 +02006275 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006276 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6277 continue;
6278 }
Michal Vaskoe9212852017-11-21 13:41:43 +01006279 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006280 aug = (struct lys_node_augment *)iter->parent;
Michal Vaskoe9212852017-11-21 13:41:43 +01006281 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006282 /* unresolved augment, wait until it's resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01006283 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
Michal Vaskobd026102017-11-21 13:43:01 +01006284 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006285 ret = EXIT_FAILURE;
6286 goto cleanup;
6287 }
6288 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02006289 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
6290 }
6291
6292 /* compare the features used in if-feature statements in the rest of both
6293 * chains of parents. The set of features used for target must be a subset
6294 * of features used for the leafref. This is not a perfect, we should compare
6295 * the truth tables but it could require too much resources, so we simplify that */
6296 for (i = 0; i < src_parents->number; i++) {
6297 iter = src_parents->set.s[i]; /* shortcut */
6298 if (!iter->iffeature_size) {
6299 continue;
6300 }
6301 for (j = 0; j < iter->iffeature_size; j++) {
6302 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6303 for (; size; size--) {
6304 if (!iter->iffeature[j].features[size - 1]) {
6305 /* not yet resolved feature, postpone this check */
6306 ret = EXIT_FAILURE;
6307 goto cleanup;
6308 }
6309 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
6310 }
6311 }
6312 }
6313 x = features->number;
6314 for (i = 0; i < trg_parents->number; i++) {
6315 iter = trg_parents->set.s[i]; /* shortcut */
6316 if (!iter->iffeature_size) {
6317 continue;
6318 }
6319 for (j = 0; j < iter->iffeature_size; j++) {
6320 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
6321 for (; size; size--) {
6322 if (!iter->iffeature[j].features[size - 1]) {
6323 /* not yet resolved feature, postpone this check */
6324 ret = EXIT_FAILURE;
6325 goto cleanup;
6326 }
Michal Vasko53b7da02018-02-13 15:28:42 +01006327 if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006328 /* the feature is not present in features set of target's parents chain */
Michal Vasko53b7da02018-02-13 15:28:42 +01006329 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
6330 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcicbb473e2016-09-16 14:48:32 +02006331 "Leafref is not conditional based on \"%s\" feature as its target.",
6332 iter->iffeature[j].features[size - 1]->name);
6333 ret = -1;
6334 goto cleanup;
6335 }
6336 }
6337 }
6338 }
6339
6340cleanup:
6341 ly_set_free(features);
6342 ly_set_free(src_parents);
6343 ly_set_free(trg_parents);
6344
6345 return ret;
6346}
6347
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006348static int
6349check_type_union_leafref(struct lys_type *type)
6350{
6351 uint8_t i;
6352
6353 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6354 /* go through unions and look for leafref */
6355 for (i = 0; i < type->info.uni.count; ++i) {
6356 switch (type->info.uni.types[i].base) {
6357 case LY_TYPE_LEAFREF:
6358 return 1;
6359 case LY_TYPE_UNION:
6360 if (check_type_union_leafref(&type->info.uni.types[i])) {
6361 return 1;
6362 }
6363 break;
6364 default:
6365 break;
6366 }
6367 }
6368
6369 return 0;
6370 }
6371
6372 /* just inherit the flag value */
6373 return type->der->has_union_leafref;
6374}
6375
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006376/**
Michal Vaskobb211122015-08-19 14:03:11 +02006377 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006378 *
6379 * @param[in] mod Main module.
6380 * @param[in] item Item to resolve. Type determined by \p type.
6381 * @param[in] type Type of the unresolved item.
6382 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006383 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006384 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006385 *
6386 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6387 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006388static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006389resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006390 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006391{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006392 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vasko53b7da02018-02-13 15:28:42 +01006393 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006394 unsigned int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01006395 struct ly_ctx * ctx = mod->ctx;
Radek Krejci80056d52017-01-05 13:13:33 +01006396 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006397 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006398 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006399
Radek Krejcic79c6b12016-07-26 15:11:49 +02006400 struct ly_set *refs, *procs;
6401 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006402 struct lys_ident *ident;
6403 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006404 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006405 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006406 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006407 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006408 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006409 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006410 struct lys_ext_instance *ext, **extlist;
6411 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006412
6413 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006414 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006415 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006416 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006417 ident = item;
6418
Radek Krejci018f1f52016-08-03 16:01:20 +02006419 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006420 break;
6421 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006422 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006423 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006424 stype = item;
6425
Radek Krejci018f1f52016-08-03 16:01:20 +02006426 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006427 break;
6428 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006429 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006430 stype = item;
6431
Michal Vasko1c007172017-03-10 10:20:44 +01006432 rc = resolve_schema_leafref(stype->info.lref.path, node, (const struct lys_node **)&stype->info.lref.target);
6433 if (!rc) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02006434 assert(stype->info.lref.target);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006435
Michal Vaskobb520442017-05-23 10:55:18 +02006436 if (lys_node_module(node)->implemented) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006437 /* make all the modules in the path implemented */
Michal Vaskobb520442017-05-23 10:55:18 +02006438 for (next = (struct lys_node *)stype->info.lref.target; next; next = lys_parent(next)) {
6439 if (!lys_node_module(next)->implemented) {
6440 if (lys_set_implemented(lys_node_module(next))) {
6441 rc = -1;
6442 break;
6443 }
6444 }
6445 }
6446 if (next) {
6447 break;
6448 }
6449
6450 /* store the backlink from leafref target */
6451 if (lys_leaf_add_leafref_target(stype->info.lref.target, (struct lys_node *)stype->parent)) {
6452 rc = -1;
Michal Vaskobe136f62017-09-21 12:08:39 +02006453 break;
6454 }
Radek Krejci46c4cd72016-01-21 15:13:52 +01006455 }
Michal Vaskof1aa47d2017-09-21 12:09:29 +02006456
6457 /* check if leafref and its target are under common if-features */
6458 rc = check_leafref_features(stype);
Radek Krejci46c4cd72016-01-21 15:13:52 +01006459 }
6460
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006461 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006462 case UNRES_TYPE_DER_EXT:
6463 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006464 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006465 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006466 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006467 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006468 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006469 /* parent */
6470 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006471 stype = item;
6472
Michal Vasko88c29542015-11-27 14:57:53 +01006473 /* HACK type->der is temporarily unparsed type statement */
6474 yin = (struct lyxml_elem *)stype->der;
6475 stype->der = NULL;
6476
Pavol Vicana0e4e672016-02-24 12:20:04 +01006477 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6478 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006479 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006480
6481 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006482 /* may try again later */
6483 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006484 } else {
6485 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006486 lydict_remove(ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006487 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006488 }
6489
Michal Vasko88c29542015-11-27 14:57:53 +01006490 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006491 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006492 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006493 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006494 lyxml_free(ctx, yin);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006495 } else {
6496 /* may try again later, put all back how it was */
6497 stype->der = (struct lys_tpdf *)yin;
6498 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006499 }
Radek Krejcic13db382016-08-16 10:52:42 +02006500 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006501 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006502 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006503 LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
Radek Krejci2c2fce82016-08-01 13:52:26 +02006504 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006505
6506 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6507 /* fill typedef union leafref flag */
6508 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6509 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6510 /* copy the type in case it has union leafref flag */
6511 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006512 LOGERR(ctx, LY_EINT, "Failed to duplicate type.");
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006513 return -1;
6514 }
6515 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006516 } else if (rc == EXIT_FAILURE && stype->base != LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +02006517 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6518 * by uses statement until the type is resolved. We do that the same way as uses statements inside
Radek Krejci93def382017-05-24 15:33:48 +02006519 * grouping. The grouping cannot be used unless the unres counter is 0.
6520 * To remember that the grouping already increased the counter, the LY_TYPE_ERR is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006521 * of the type's base member. */
6522 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6523 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006524 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006525 LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006526 return -1;
6527 }
Radek Krejci9b6aad22016-09-20 15:55:51 +02006528 stype->base = LY_TYPE_ERR;
Radek Krejcic13db382016-08-16 10:52:42 +02006529 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006530 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006531 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006532 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006533 iff_data = str_snode;
6534 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006535 if (!rc) {
6536 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006537 if (iff_data->infeature) {
6538 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6539 feat = *((struct lys_feature **)item);
6540 if (!feat->depfeatures) {
6541 feat->depfeatures = ly_set_new();
6542 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006543 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006544 }
6545 /* cleanup temporary data */
Michal Vasko53b7da02018-02-13 15:28:42 +01006546 lydict_remove(ctx, iff_data->fname);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006547 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006548 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006549 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006550 case UNRES_FEATURE:
6551 feat = (struct lys_feature *)item;
6552
6553 if (feat->iffeature_size) {
6554 refs = ly_set_new();
6555 procs = ly_set_new();
6556 ly_set_add(procs, feat, 0);
6557
6558 while (procs->number) {
6559 ref = procs->set.g[procs->number - 1];
6560 ly_set_rm_index(procs, procs->number - 1);
6561
6562 for (i = 0; i < ref->iffeature_size; i++) {
6563 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6564 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006565 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006566 if (ref->iffeature[i].features[j - 1] == feat) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006567 LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006568 goto featurecheckdone;
6569 }
6570
6571 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6572 k = refs->number;
6573 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6574 /* not yet seen feature, add it for processing */
6575 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6576 }
6577 }
6578 } else {
6579 /* forward reference */
6580 rc = EXIT_FAILURE;
6581 goto featurecheckdone;
6582 }
6583 }
6584
6585 }
6586 }
6587 rc = EXIT_SUCCESS;
6588
6589featurecheckdone:
6590 ly_set_free(refs);
6591 ly_set_free(procs);
6592 }
6593
6594 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006595 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006596 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006597 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006598 case UNRES_TYPEDEF_DFLT:
6599 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006600 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006601 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006602 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006603 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006604 break;
6605 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006606 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006607 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006608 choic = item;
6609
Radek Krejcie00d2312016-08-12 15:27:49 +02006610 if (!choic->dflt) {
6611 choic->dflt = resolve_choice_dflt(choic, expr);
6612 }
Michal Vasko7955b362015-09-04 14:18:15 +02006613 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006614 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006615 } else {
6616 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006617 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006618 break;
6619 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006620 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006621 break;
6622 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006623 unique_info = (struct unres_list_uniq *)item;
6624 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006625 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006626 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006627 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006628 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006629 case UNRES_XPATH:
6630 node = (struct lys_node *)item;
Michal Vaskof96dfb62017-08-17 12:23:49 +02006631 rc = lys_check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006632 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006633 case UNRES_EXT:
6634 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006635 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006636 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006637 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006638 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006639 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006640 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006641 if (eplugin) {
6642 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006643 u = malloc(sizeof *u);
Michal Vasko53b7da02018-02-13 15:28:42 +01006644 LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006645 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006646 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6647 /* something really bad happend since the extension finalization is not actually
6648 * being resolved while adding into unres, so something more serious with the unres
6649 * list itself must happened */
6650 return -1;
6651 }
Radek Krejci80056d52017-01-05 13:13:33 +01006652 }
6653 }
Radek Krejci79685c92017-02-17 10:49:43 +01006654 }
6655 if (!rc || rc == -1) {
6656 /* cleanup on success or fatal error */
6657 if (ext_data->datatype == LYS_IN_YIN) {
6658 /* YIN */
Michal Vasko53b7da02018-02-13 15:28:42 +01006659 lyxml_free(ctx, ext_data->data.yin);
Radek Krejci79685c92017-02-17 10:49:43 +01006660 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01006661 /* YANG */
6662 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01006663 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01006664 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01006665 }
6666 break;
Radek Krejci80056d52017-01-05 13:13:33 +01006667 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01006668 u = (uint8_t *)str_snode;
6669 ext = (*(struct lys_ext_instance ***)item)[*u];
6670 free(u);
6671
Radek Krejci80056d52017-01-05 13:13:33 +01006672 eplugin = ext->def->plugin;
6673
6674 /* inherit */
6675 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
6676 root = (struct lys_node *)ext->parent;
6677 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6678 LY_TREE_DFS_BEGIN(root->child, next, node) {
6679 /* first, check if the node already contain instance of the same extension,
6680 * in such a case we won't inherit. In case the node was actually defined as
6681 * augment data, we are supposed to check the same way also the augment node itself */
6682 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
6683 goto inherit_dfs_sibling;
6684 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
6685 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
6686 goto inherit_dfs_sibling;
6687 }
6688
6689 if (eplugin->check_inherit) {
6690 /* we have a callback to check the inheritance, use it */
6691 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
6692 case 0:
6693 /* yes - continue with the inheriting code */
6694 break;
6695 case 1:
6696 /* no - continue with the node's sibling */
6697 goto inherit_dfs_sibling;
6698 case 2:
6699 /* no, but continue with the children, just skip the inheriting code for this node */
6700 goto inherit_dfs_child;
6701 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006702 LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
Radek Krejci80056d52017-01-05 13:13:33 +01006703 ext->def->module->name, ext->def->name, rc);
6704 }
6705 }
6706
6707 /* inherit the extension */
6708 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01006709 LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006710 extlist[node->ext_size] = malloc(sizeof **extlist);
Michal Vasko53b7da02018-02-13 15:28:42 +01006711 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01006712 memcpy(extlist[node->ext_size], ext, sizeof *ext);
6713 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
6714
6715 node->ext = extlist;
6716 node->ext_size++;
6717
6718inherit_dfs_child:
6719 /* modification of - select element for the next run - children first */
6720 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
6721 next = NULL;
6722 } else {
6723 next = node->child;
6724 }
6725 if (!next) {
6726inherit_dfs_sibling:
6727 /* no children, try siblings */
6728 next = node->next;
6729 }
6730 while (!next) {
6731 /* go to the parent */
6732 node = lys_parent(node);
6733
6734 /* we are done if we are back in the root (the starter's parent */
6735 if (node == root) {
6736 break;
6737 }
6738
6739 /* parent is already processed, go to its sibling */
6740 next = node->next;
6741 }
6742 }
6743 }
6744 }
6745
6746 /* final check */
6747 if (eplugin->check_result) {
6748 if ((*eplugin->check_result)(ext)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006749 LOGERR(ctx, LY_EEXT, "Resolving extension failed.");
Radek Krejci80056d52017-01-05 13:13:33 +01006750 return -1;
6751 }
6752 }
6753
6754 rc = 0;
6755 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006756 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006757 LOGINT(ctx);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006758 break;
6759 }
6760
Radek Krejci54081ce2016-08-12 15:21:47 +02006761 if (has_str && !rc) {
6762 /* the string is no more needed in case of success.
6763 * In case of forward reference, we will try to resolve the string later */
Michal Vasko53b7da02018-02-13 15:28:42 +01006764 lydict_remove(ctx, str_snode);
Radek Krejci4f78b532016-02-17 13:43:00 +01006765 }
6766
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006767 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006768}
6769
Michal Vaskof02e3742015-08-05 16:27:02 +02006770/* logs directly */
6771static void
Radek Krejci48464ed2016-03-17 15:44:09 +01006772print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006773{
Michal Vaskocb34dc62016-05-20 14:38:37 +02006774 struct lyxml_elem *xml;
6775 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006776 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006777 const char *name = NULL;
6778 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006779
Michal Vaskof02e3742015-08-05 16:27:02 +02006780 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02006781 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006782 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006783 break;
6784 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006785 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006786 break;
6787 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01006788 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
6789 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02006790 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006791 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02006792 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02006793 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02006794 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
6795 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01006796 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006797 } else {
6798 LY_TREE_FOR(xml->attr, attr) {
6799 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006800 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02006801 break;
6802 }
6803 }
6804 assert(attr);
6805 }
Radek Krejcie534c132016-11-23 13:32:31 +01006806 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006807 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006808 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006809 iff_data = str_node;
6810 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02006811 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006812 case UNRES_FEATURE:
6813 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
6814 ((struct lys_feature *)item)->name);
6815 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02006816 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006817 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02006818 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006819 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02006820 case UNRES_TYPE_DFLT:
Radek Krejci2e2de832016-10-13 16:12:26 +02006821 if (str_node) {
6822 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);
6823 } /* else no default value in the type itself, but we are checking some restrictions against
6824 * possible default value of some base type. The failure is caused by not resolved base type,
6825 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02006826 break;
6827 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006828 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006829 break;
6830 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01006831 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006832 break;
6833 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01006834 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02006835 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006836 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01006837 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
6838 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01006839 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006840 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01006841 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
6842 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02006843 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006844 case UNRES_EXT:
6845 extinfo = (struct unres_ext *)str_node;
6846 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
6847 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
6848 break;
Michal Vaskocf024702015-10-08 15:01:42 +02006849 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006850 LOGINT(NULL);
Michal Vaskof02e3742015-08-05 16:27:02 +02006851 break;
6852 }
6853}
6854
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006855/**
Michal Vaskobb211122015-08-19 14:03:11 +02006856 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006857 *
6858 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006859 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006860 *
Michal Vasko92b8a382015-08-19 14:03:49 +02006861 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006862 */
Michal Vaskof02e3742015-08-05 16:27:02 +02006863int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006864resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02006865{
Radek Krejci010e54b2016-03-15 09:40:34 +01006866 uint32_t i, resolved = 0, unres_count, res_count;
Michal Vasko53b7da02018-02-13 15:28:42 +01006867 struct ly_err_item *prev_eitem;
6868 enum int_log_opts prev_ilo;
6869 LY_ERR prev_ly_errno;
6870 int rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006871
6872 assert(unres);
6873
Michal Vaskoe8734262016-09-29 14:12:06 +02006874 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko53b7da02018-02-13 15:28:42 +01006875 prev_ly_errno = ly_errno;
6876 ly_ilo_change(mod->ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vasko51054ca2015-08-12 12:20:00 +02006877
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006878 /* uses */
Michal Vasko51054ca2015-08-12 12:20:00 +02006879 do {
Michal Vasko88c29542015-11-27 14:57:53 +01006880 unres_count = 0;
6881 res_count = 0;
Michal Vasko51054ca2015-08-12 12:20:00 +02006882
6883 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006884 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006885 * if-features are resolved here to make sure that we will have all if-features for
6886 * later check of feature circular dependency */
Radek Krejci018f1f52016-08-03 16:01:20 +02006887 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006888 continue;
6889 }
Radek Krejci018f1f52016-08-03 16:01:20 +02006890 /* processes UNRES_USES, UNRES_IFFEAT, UNRES_TYPE_DER, UNRES_TYPE_DER_TPDF, UNRES_TYPE_LEAFREF,
Radek Krejci818b0c52016-11-09 15:10:51 +01006891 * UNRES_AUGMENT, UNRES_CHOICE_DFLT and UNRES_IDENT */
Michal Vasko51054ca2015-08-12 12:20:00 +02006892
Michal Vasko88c29542015-11-27 14:57:53 +01006893 ++unres_count;
Michal Vaskof96dfb62017-08-17 12:23:49 +02006894 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006895 if (!rc) {
Michal Vasko51054ca2015-08-12 12:20:00 +02006896 unres->type[i] = UNRES_RESOLVED;
6897 ++resolved;
Michal Vasko88c29542015-11-27 14:57:53 +01006898 ++res_count;
Michal Vasko89e15322015-08-17 15:46:55 +02006899 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006900 goto error;
Radek Krejcic2a180f2016-06-22 13:28:16 +02006901 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006902 /* forward reference, erase errors */
6903 ly_err_free_next(mod->ctx, prev_eitem);
Michal Vasko51054ca2015-08-12 12:20:00 +02006904 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006905 }
Michal Vasko88c29542015-11-27 14:57:53 +01006906 } while (res_count && (res_count < unres_count));
Michal Vasko51054ca2015-08-12 12:20:00 +02006907
Michal Vasko88c29542015-11-27 14:57:53 +01006908 if (res_count < unres_count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006909 /* just print the errors (but we must free the ones we have and get them again :-/ ) */
6910 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 0);
Michal Vasko22af5ca2016-05-20 11:44:02 +02006911
6912 for (i = 0; i < unres->count; ++i) {
Radek Krejci018f1f52016-08-03 16:01:20 +02006913 if (unres->type[i] > UNRES_IDENT) {
Michal Vasko22af5ca2016-05-20 11:44:02 +02006914 continue;
6915 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02006916 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Michal Vasko22af5ca2016-05-20 11:44:02 +02006917 }
Michal Vasko92b8a382015-08-19 14:03:49 +02006918 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006919 }
6920
Michal Vaskof96dfb62017-08-17 12:23:49 +02006921 /* the rest except finalizing extensions and xpath */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006922 for (i = 0; i < unres->count; ++i) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02006923 if ((unres->type[i] == UNRES_RESOLVED) || (unres->type[i] == UNRES_EXT_FINALIZE) || (unres->type[i] == UNRES_XPATH)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006924 continue;
6925 }
Michal Vaskoc07187d2015-08-13 15:20:57 +02006926
Michal Vaskof96dfb62017-08-17 12:23:49 +02006927 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci010e54b2016-03-15 09:40:34 +01006928 if (rc == 0) {
Pavol Vican88e16c92016-09-07 15:41:50 +02006929 if (unres->type[i] == UNRES_LIST_UNIQ) {
6930 /* free the allocated structure */
6931 free(unres->item[i]);
6932 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006933 unres->type[i] = UNRES_RESOLVED;
6934 ++resolved;
6935 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006936 goto error;
Radek Krejci791f6c72017-02-22 15:23:39 +01006937 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006938 /* forward reference, erase errors */
6939 ly_err_free_next(mod->ctx, prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006940 }
6941 }
6942
Michal Vasko53b7da02018-02-13 15:28:42 +01006943 /* log normally now */
6944 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 0);
6945 ly_errno = prev_ly_errno;
Radek Krejci010e54b2016-03-15 09:40:34 +01006946
Radek Krejci80056d52017-01-05 13:13:33 +01006947 /* finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
6948 for (i = 0; i < unres->count; ++i) {
6949 if (unres->type[i] != UNRES_EXT_FINALIZE) {
6950 continue;
6951 }
6952
Michal Vaskof96dfb62017-08-17 12:23:49 +02006953 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejci791f6c72017-02-22 15:23:39 +01006954 unres->type[i] = UNRES_RESOLVED;
Radek Krejci80056d52017-01-05 13:13:33 +01006955 if (rc == 0) {
Radek Krejci80056d52017-01-05 13:13:33 +01006956 ++resolved;
6957 }
Radek Krejci791f6c72017-02-22 15:23:39 +01006958 /* else error - it was already printed, but resolved was not increased,
6959 so this unres item will not be resolved again in the following code,
6960 but it will cause returning -1 at the end, this way we are able to
6961 print all the issues with unres */
Radek Krejci80056d52017-01-05 13:13:33 +01006962 }
6963
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006964 if (resolved < unres->count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01006965 /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print
Michal Vaskof96dfb62017-08-17 12:23:49 +02006966 * all the validation errors, xpath is resolved only here to properly print all the messages
Radek Krejci010e54b2016-03-15 09:40:34 +01006967 */
6968 for (i = 0; i < unres->count; ++i) {
6969 if (unres->type[i] == UNRES_RESOLVED) {
6970 continue;
6971 }
Michal Vaskof96dfb62017-08-17 12:23:49 +02006972 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
Radek Krejcib3142312016-11-09 11:04:12 +01006973 if (unres->type[i] == UNRES_XPATH) {
Michal Vasko769f8032017-01-24 13:11:55 +01006974 /* XPath referencing an unknown node is actually supposed to be just a warning */
Radek Krejcib3142312016-11-09 11:04:12 +01006975 unres->type[i] = UNRES_RESOLVED;
6976 resolved++;
Radek Krejcib3142312016-11-09 11:04:12 +01006977 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006978 }
Radek Krejcib3142312016-11-09 11:04:12 +01006979 if (resolved < unres->count) {
6980 return -1;
6981 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006982 }
6983
Michal Vaskoe8734262016-09-29 14:12:06 +02006984 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01006985 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006986 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01006987
6988error:
6989 ly_ilo_restore(mod->ctx, prev_ilo, prev_eitem, 1);
6990 /* ly_errno will be set accordingly, we do not want to keep the previous value */
6991 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006992}
6993
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006994/**
Michal Vaskobb211122015-08-19 14:03:11 +02006995 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006996 *
6997 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02006998 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006999 * @param[in] item Item to resolve. Type determined by \p type.
7000 * @param[in] type Type of the unresolved item.
7001 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007002 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007003 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007004 */
7005int
Radek Krejci48464ed2016-03-17 15:44:09 +01007006unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7007 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007008{
Radek Krejci54081ce2016-08-12 15:21:47 +02007009 int rc;
7010 const char *dictstr;
7011
7012 dictstr = lydict_insert(mod->ctx, str, 0);
7013 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7014
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007015 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007016 lydict_remove(mod->ctx, dictstr);
7017 }
7018 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007019}
7020
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007021/**
Michal Vaskobb211122015-08-19 14:03:11 +02007022 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007023 *
7024 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007025 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007026 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007027 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007028 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007029 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007030 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007031 */
7032int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007033unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007034 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007035{
Michal Vasko53b7da02018-02-13 15:28:42 +01007036 int rc;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007037 uint32_t u;
Michal Vasko53b7da02018-02-13 15:28:42 +01007038 enum int_log_opts prev_ilo;
7039 struct ly_err_item *prev_eitem;
7040 LY_ERR prev_ly_errno;
Michal Vasko88c29542015-11-27 14:57:53 +01007041 struct lyxml_elem *yin;
Michal Vasko53b7da02018-02-13 15:28:42 +01007042 struct ly_ctx *ctx = mod->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007043
Michal Vasko9bf425b2015-10-22 11:42:03 +02007044 assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)
7045 && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007046
Radek Krejci850a5de2016-11-08 14:06:40 +01007047 /* check for duplicities in unres */
7048 for (u = 0; u < unres->count; u++) {
7049 if (unres->type[u] == type && unres->item[u] == item &&
7050 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007051 /* duplication can happen when the node contains multiple statements of the same type to check,
7052 * this can happen for example when refinement is being applied, so we just postpone the processing
7053 * and do not duplicate the information */
7054 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007055 }
7056 }
7057
Michal Vaskof96dfb62017-08-17 12:23:49 +02007058 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH)) {
7059 /* extension finalization is not even tried when adding the item into the inres list,
7060 * xpath is not tried because it would hide some potential warnings */
Radek Krejcic293bac2017-02-27 11:25:28 +01007061 rc = EXIT_FAILURE;
7062 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007063 prev_ly_errno = ly_errno;
7064 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007065
Michal Vasko53b7da02018-02-13 15:28:42 +01007066 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007067 if (rc != EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007068 ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0);
7069 if (rc != -1) {
7070 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007071 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007072
Radek Krejci80056d52017-01-05 13:13:33 +01007073 if (type == UNRES_LIST_UNIQ) {
7074 /* free the allocated structure */
7075 free(item);
7076 } else if (rc == -1 && type == UNRES_IFFEAT) {
7077 /* free the allocated resources */
7078 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007079 }
Radek Krejci80056d52017-01-05 13:13:33 +01007080 return rc;
7081 } else {
7082 /* erase info about validation errors */
Michal Vasko53b7da02018-02-13 15:28:42 +01007083 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7084 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007085 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007086
Radek Krejci80056d52017-01-05 13:13:33 +01007087 print_unres_schema_item_fail(item, type, snode);
7088
7089 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7090 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7091 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7092 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7093 lyxml_unlink_elem(mod->ctx, yin, 1);
7094 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7095 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007096 }
Michal Vasko88c29542015-11-27 14:57:53 +01007097 }
7098
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007099 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007100 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Michal Vasko53b7da02018-02-13 15:28:42 +01007101 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007102 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007103 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007104 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007105 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007106 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Michal Vasko53b7da02018-02-13 15:28:42 +01007107 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007108 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007109 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01007110 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007111 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007112
Michal Vasko3767fb22016-07-21 12:10:57 +02007113 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007114}
7115
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007116/**
Michal Vaskobb211122015-08-19 14:03:11 +02007117 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007118 *
7119 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007120 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007121 * @param[in] item Old item to be resolved.
7122 * @param[in] type Type of the old unresolved item.
7123 * @param[in] new_item New item to use in the duplicate.
7124 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007125 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007126 */
Michal Vaskodad19402015-08-06 09:51:53 +02007127int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007128unres_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 +02007129{
7130 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007131 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007132 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007133
Michal Vaskocf024702015-10-08 15:01:42 +02007134 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007135
Radek Krejcid09d1a52016-08-11 14:05:45 +02007136 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7137 if (type == UNRES_LIST_UNIQ) {
7138 aux_uniq.list = item;
7139 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7140 item = &aux_uniq;
7141 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007142 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007143
7144 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007145 if (type == UNRES_LIST_UNIQ) {
7146 free(new_item);
7147 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007148 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007149 }
7150
Radek Krejcic79c6b12016-07-26 15:11:49 +02007151 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007152 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007153 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007154 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007155 return -1;
7156 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007157 } else if (type == UNRES_IFFEAT) {
7158 /* duplicate unres_iffeature_data */
7159 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01007160 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007161 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7162 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7163 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007164 LOGINT(mod->ctx);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007165 return -1;
7166 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007167 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007168 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007169 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007170 return -1;
7171 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007172 }
Michal Vaskodad19402015-08-06 09:51:53 +02007173
7174 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007175}
7176
Michal Vaskof02e3742015-08-05 16:27:02 +02007177/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007178int
Michal Vasko878e38d2016-09-05 12:17:53 +02007179unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007180{
Michal Vasko878e38d2016-09-05 12:17:53 +02007181 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007182 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007183
Radek Krejciddddd0d2017-01-20 15:20:46 +01007184 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007185 i = start_on_backwards;
7186 } else {
7187 i = unres->count - 1;
7188 }
7189 for (; i > -1; i--) {
7190 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007191 continue;
7192 }
7193 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007194 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007195 break;
7196 }
7197 } else {
7198 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7199 aux_uniq2 = (struct unres_list_uniq *)item;
7200 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007201 break;
7202 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007203 }
7204 }
7205
Michal Vasko878e38d2016-09-05 12:17:53 +02007206 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007207}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007208
Michal Vaskoede9c472016-06-07 09:38:15 +02007209static void
7210unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7211{
7212 struct lyxml_elem *yin;
7213 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007214 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007215
7216 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007217 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007218 case UNRES_TYPE_DER:
7219 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7220 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7221 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007222 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007223 lydict_remove(ctx, yang->name);
7224 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007225 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7226 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7227 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007228 } else {
7229 lyxml_free(ctx, yin);
7230 }
7231 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007232 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007233 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7234 lydict_remove(ctx, iff_data->fname);
7235 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007236 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007237 case UNRES_IDENT:
7238 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007239 case UNRES_CHOICE_DFLT:
7240 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007241 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7242 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007243 case UNRES_LIST_UNIQ:
7244 free(unres->item[i]);
7245 break;
PavolVicanc1807262017-01-31 18:00:27 +01007246 case UNRES_EXT:
7247 free(unres->str_snode[i]);
7248 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007249 default:
7250 break;
7251 }
7252 unres->type[i] = UNRES_RESOLVED;
7253}
7254
Michal Vasko88c29542015-11-27 14:57:53 +01007255void
Michal Vasko44ab1462017-05-18 13:18:36 +02007256unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007257{
7258 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007259 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007260
Radek Krejcic071c542016-01-27 14:57:51 +01007261 if (!unres || !(*unres)) {
7262 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007263 }
7264
Michal Vasko44ab1462017-05-18 13:18:36 +02007265 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007266
7267 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007268 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007269 if ((*unres)->type[i] != UNRES_RESOLVED) {
7270 unresolved++;
7271 }
7272 continue;
7273 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007274
7275 /* free heap memory for the specific item */
7276 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007277 }
7278
Michal Vaskoede9c472016-06-07 09:38:15 +02007279 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007280 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007281 free((*unres)->item);
7282 free((*unres)->type);
7283 free((*unres)->str_snode);
7284 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007285 free((*unres));
7286 (*unres) = NULL;
7287 }
Michal Vasko88c29542015-11-27 14:57:53 +01007288}
7289
Michal Vaskoff690e72017-08-03 14:25:07 +02007290/* check whether instance-identifier points outside its data subtree (for operation it is any node
7291 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007292static int
7293check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7294{
Michal Vaskoff690e72017-08-03 14:25:07 +02007295 const struct lys_node *op_node, *first_node;
Michal Vasko53b7da02018-02-13 15:28:42 +01007296 enum int_log_opts prev_ilo;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007297 char *buf;
Michal Vasko53b7da02018-02-13 15:28:42 +01007298 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007299
Radek Krejci034cb102017-08-01 15:45:13 +02007300 if (!json_instid || !json_instid[0]) {
7301 /* no/empty value */
7302 return 0;
7303 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007304
7305 for (op_node = lys_parent(sleaf);
7306 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7307 op_node = lys_parent(op_node));
7308
7309 if (op_node && lys_parent(op_node)) {
7310 /* nested operation - any absolute path is external */
7311 return 1;
7312 }
7313
7314 /* get the first node from the instid */
7315 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7316 if (!buf) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007317 /* so that we do not have to bother with logging, say it is not external */
7318 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007319 }
7320
Michal Vaskoff690e72017-08-03 14:25:07 +02007321 /* find the first schema node, do not log */
Michal Vasko53b7da02018-02-13 15:28:42 +01007322 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoff690e72017-08-03 14:25:07 +02007323 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
Michal Vasko53b7da02018-02-13 15:28:42 +01007324 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007325
Michal Vasko53b7da02018-02-13 15:28:42 +01007326 free(buf);
Michal Vaskoff690e72017-08-03 14:25:07 +02007327 if (!first_node) {
7328 /* unknown path, say it is not external */
Michal Vaskoff690e72017-08-03 14:25:07 +02007329 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007330 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007331
7332 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7333
Michal Vaskoff690e72017-08-03 14:25:07 +02007334 if (op_node && (op_node != first_node)) {
7335 /* it is a top-level operation, so we're good if it points somewhere inside it */
7336 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007337 }
7338
7339 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7340 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007341 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007342}
7343
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007344/**
7345 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7346 *
7347 * @param[in] data Data node where the path is used
7348 * @param[in] path Instance-identifier node value.
7349 * @param[in,out] ret Resolved instance or NULL.
7350 *
7351 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7352 */
7353static int
7354resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7355{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007356 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007357 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007358 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007359 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007360 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007361 char *str;
7362 int mod_len, name_len, has_predicate;
7363 struct unres_data node_match;
7364
7365 memset(&node_match, 0, sizeof node_match);
7366 *ret = NULL;
7367
7368 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007369 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007370 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007371 if (root->prev) {
7372 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007373 }
7374
7375 /* search for the instance node */
7376 while (path[i]) {
7377 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7378 if (j <= 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007379 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007380 goto error;
7381 }
7382 i += j;
7383
Michal Vasko1b6ca962017-08-03 14:23:09 +02007384 if (model) {
7385 str = strndup(model, mod_len);
7386 if (!str) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007387 LOGMEM(ctx);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007388 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007389 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007390 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007391 if (ctx->data_clb) {
7392 if (!mod) {
7393 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7394 } else if (!mod->implemented) {
7395 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7396 }
7397 }
7398 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007399
Michal Vasko1b6ca962017-08-03 14:23:09 +02007400 if (!mod || !mod->implemented || mod->disabled) {
7401 break;
7402 }
7403 } else if (!prev_mod) {
7404 /* first iteration and we are missing module name */
Michal Vasko53b7da02018-02-13 15:28:42 +01007405 LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_NONE, NULL, name_len, name);
7406 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instane-identifier is missing prefix in the first node.");
Michal Vasko1b6ca962017-08-03 14:23:09 +02007407 goto error;
7408 } else {
7409 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007410 }
7411
Radek Krejci2c822ed2017-08-03 14:23:36 +02007412 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007413 /* no instance exists */
7414 break;
7415 }
7416
7417 if (has_predicate) {
7418 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007419 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007420 /* index of the current node (for lists with position predicates) */
7421 cur_idx = 1;
7422 while (j < (signed)node_match.count) {
7423 node = node_match.node[j];
7424 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7425 if (parsed < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007426 LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007427 goto error;
7428 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007429
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007430 if (!node) {
7431 /* current node does not satisfy the predicate */
7432 unres_data_del(&node_match, j);
7433 } else {
7434 ++j;
7435 }
7436 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007437 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007438
7439 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007440 } else if (node_match.count) {
7441 /* check that we are not addressing lists */
7442 for (j = 0; (unsigned)j < node_match.count; ++j) {
7443 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7444 unres_data_del(&node_match, j--);
7445 }
7446 }
7447 if (!node_match.count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007448 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007449 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007450 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007451
7452 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007453 }
7454
7455 if (!node_match.count) {
7456 /* no instance exists */
7457 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007458 LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007459 return EXIT_FAILURE;
7460 }
7461 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7462 return EXIT_SUCCESS;
7463 } else if (node_match.count > 1) {
7464 /* instance identifier must resolve to a single node */
Michal Vasko53b7da02018-02-13 15:28:42 +01007465 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007466 goto error;
7467 } else {
7468 /* we have required result, remember it and cleanup */
7469 *ret = node_match.node[0];
7470 free(node_match.node);
7471 return EXIT_SUCCESS;
7472 }
7473
7474error:
7475 /* cleanup */
7476 free(node_match.node);
7477 return -1;
7478}
7479
7480static int
7481resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007482{
Michal Vaskoca16cb32017-07-10 11:50:33 +02007483 struct ly_set *set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007484 uint32_t i;
7485
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007486 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007487
Michal Vaskoca16cb32017-07-10 11:50:33 +02007488 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vasko50576712017-07-28 12:28:33 +02007489 set = lyd_find_path((struct lyd_node *)leaf, path);
Michal Vaskoca16cb32017-07-10 11:50:33 +02007490 if (!set) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007491 return -1;
7492 }
7493
Michal Vaskoca16cb32017-07-10 11:50:33 +02007494 for (i = 0; i < set->number; ++i) {
7495 if (!(set->set.d[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7496 continue;
7497 }
7498
Radek Krejci1899d6a2016-11-03 13:48:07 +01007499 /* not that the value is already in canonical form since the parsers does the conversion,
7500 * so we can simply compare just the values */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007501 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)set->set.d[i])->value_str, 1)) {
Radek Krejci1899d6a2016-11-03 13:48:07 +01007502 /* we have the match */
Michal Vaskoca16cb32017-07-10 11:50:33 +02007503 *ret = set->set.d[i];
Radek Krejci7de36cf2016-09-12 16:18:50 +02007504 break;
7505 }
7506 }
7507
Michal Vaskoca16cb32017-07-10 11:50:33 +02007508 ly_set_free(set);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007509
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007510 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007511 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007512 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007513 LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007514 return EXIT_FAILURE;
7515 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007516 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 +02007517 }
7518 }
7519
7520 return EXIT_SUCCESS;
7521}
7522
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007523/* 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 +01007524int
7525resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7526 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007527{
Michal Vasko53b7da02018-02-13 15:28:42 +01007528 struct ly_ctx *ctx = leaf->schema->module->ctx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007529 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007530 struct lyd_node *ret;
Michal Vasko53b7da02018-02-13 15:28:42 +01007531 enum int_log_opts prev_ilo;
7532 int found, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007533 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007534
7535 assert(type->base == LY_TYPE_UNION);
7536
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007537 if ((leaf->value_type == LY_TYPE_UNION) || (leaf->value_type == (LY_TYPE_INST | LY_TYPE_INST_UNRES))) {
7538 /* either NULL or instid previously converted to JSON */
7539 json_val = leaf->value.string;
7540 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007541
Michal Vaskofd6c6502017-01-06 12:15:41 +01007542 if (store) {
7543 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7544 free(leaf->value.bit);
7545 }
7546 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007547 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007548
7549 /* turn logging off, we are going to try to validate the value with all the types in order */
Michal Vasko53b7da02018-02-13 15:28:42 +01007550 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007551
7552 t = NULL;
7553 found = 0;
7554 while ((t = lyp_get_next_union_type(type, t, &found))) {
7555 found = 0;
7556
7557 switch (t->base) {
7558 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007559 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7560 req_inst = -1;
7561 } else {
7562 req_inst = t->info.lref.req;
7563 }
7564
7565 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007566 if (store) {
7567 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7568 /* valid resolved */
7569 leaf->value.leafref = ret;
7570 leaf->value_type = LY_TYPE_LEAFREF;
7571 } else {
7572 /* valid unresolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01007573 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko31a2d322018-01-12 13:36:12 +01007574 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007575 return -1;
7576 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007577 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007578 }
7579 }
7580
7581 success = 1;
7582 }
7583 break;
7584 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007585 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7586 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7587 req_inst = -1;
7588 } else {
7589 req_inst = t->info.inst.req;
7590 }
7591
Michal Vaskod3a03112017-01-23 09:56:02 +01007592 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007593 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007594 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007595 /* valid resolved */
7596 leaf->value.instance = ret;
7597 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007598
Michal Vaskofd6c6502017-01-06 12:15:41 +01007599 if (json_val) {
7600 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7601 leaf->value_str = json_val;
7602 json_val = NULL;
7603 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007604 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007605 /* valid unresolved */
7606 if (json_val) {
7607 /* put the JSON val back */
7608 leaf->value.string = json_val;
7609 json_val = NULL;
7610 } else {
7611 leaf->value.instance = NULL;
7612 }
7613 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007614 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007615 }
7616
7617 success = 1;
7618 }
7619 break;
7620 default:
Michal Vasko31a2d322018-01-12 13:36:12 +01007621 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007622 success = 1;
7623 }
7624 break;
7625 }
7626
7627 if (success) {
7628 break;
7629 }
7630
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007631 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007632 if (store) {
7633 if (t->base == LY_TYPE_BITS) {
7634 free(leaf->value.bit);
7635 }
7636 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007637 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007638 }
7639
7640 /* turn logging back on */
Michal Vasko53b7da02018-02-13 15:28:42 +01007641 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007642
7643 if (json_val) {
7644 if (!success) {
7645 /* put the value back for now */
7646 assert(leaf->value_type == LY_TYPE_UNION);
7647 leaf->value.string = json_val;
7648 } else {
7649 /* value was ultimately useless, but we could not have known */
7650 lydict_remove(leaf->schema->module->ctx, json_val);
7651 }
7652 }
7653
Michal Vaskofd6c6502017-01-06 12:15:41 +01007654 if (success) {
7655 if (resolved_type) {
7656 *resolved_type = t;
7657 }
7658 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007659 /* not found and it is required */
Michal Vasko53b7da02018-02-13 15:28:42 +01007660 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007661 return EXIT_FAILURE;
7662 }
7663
7664 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007665
Radek Krejci9b6aad22016-09-20 15:55:51 +02007666}
7667
Michal Vasko8bcdf292015-08-19 14:04:43 +02007668/**
7669 * @brief Resolve a single unres data item. Logs directly.
7670 *
Michal Vaskocf024702015-10-08 15:01:42 +02007671 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02007672 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01007673 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007674 *
7675 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
7676 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02007677int
Michal Vasko0b963112017-08-11 12:45:36 +02007678resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int ignore_fail, struct lys_when **failed_when)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007679{
Michal Vasko3cfa3182017-01-17 10:00:58 +01007680 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02007681 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007682 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007683 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007684
Michal Vasko83a6c462015-10-08 16:43:53 +02007685 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02007686 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007687
Michal Vaskocf024702015-10-08 15:01:42 +02007688 switch (type) {
7689 case UNRES_LEAFREF:
7690 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007691 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007692 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7693 req_inst = -1;
7694 } else {
7695 req_inst = sleaf->type.info.lref.req;
7696 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007697 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
7698 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01007699 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007700 /* valid resolved */
Michal Vasko1c8567a2017-01-05 13:42:27 +01007701 if ((leaf->value_type & LY_DATA_TYPE_MASK) == LY_TYPE_BITS) {
7702 free(leaf->value.bit);
7703 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007704 leaf->value.leafref = ret;
7705 leaf->value_type = LY_TYPE_LEAFREF;
7706 } else {
7707 /* valid unresolved */
7708 if (!(leaf->value_type & LY_TYPE_LEAFREF_UNRES)) {
Michal Vasko31a2d322018-01-12 13:36:12 +01007709 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007710 return -1;
7711 }
7712 }
7713 }
7714 leaf->validity &= ~LYD_VAL_LEAFREF;
7715 } else {
7716 return rc;
7717 }
7718 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007719
Michal Vaskocf024702015-10-08 15:01:42 +02007720 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02007721 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007722 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
7723 if (ext_dep == -1) {
7724 return -1;
7725 }
7726
7727 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7728 req_inst = -1;
7729 } else {
7730 req_inst = sleaf->type.info.inst.req;
7731 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007732 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
7733 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007734 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007735 /* valid resolved */
7736 leaf->value.instance = ret;
7737 leaf->value_type = LY_TYPE_INST;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007738 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007739 /* valid unresolved */
7740 leaf->value.instance = NULL;
7741 leaf->value_type = LY_TYPE_INST | LY_TYPE_INST_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007742 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007743 } else {
7744 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007745 }
Michal Vaskocf024702015-10-08 15:01:42 +02007746 break;
7747
Radek Krejci7de36cf2016-09-12 16:18:50 +02007748 case UNRES_UNION:
7749 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007750 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007751
Michal Vaskocf024702015-10-08 15:01:42 +02007752 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02007753 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02007754 return rc;
7755 }
7756 break;
7757
Michal Vaskobf19d252015-10-08 15:39:17 +02007758 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007759 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02007760 return rc;
7761 }
7762 break;
7763
7764 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007765 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02007766 return rc;
7767 }
7768 break;
7769
Michal Vaskocf024702015-10-08 15:01:42 +02007770 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007771 LOGINT(NULL);
Michal Vasko8bcdf292015-08-19 14:04:43 +02007772 return -1;
7773 }
7774
7775 return EXIT_SUCCESS;
7776}
7777
7778/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01007779 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02007780 *
7781 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02007782 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007783 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01007784 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02007785 */
7786int
Radek Krejci0b7704f2016-03-18 12:16:14 +01007787unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02007788{
Radek Krejci03b71f72016-03-16 11:10:09 +01007789 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02007790 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02007791 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02007792
Radek Krejci03b71f72016-03-16 11:10:09 +01007793 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007794 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01007795 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007796 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01007797 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007798 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02007799 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02007800
Radek Krejci0b7704f2016-03-18 12:16:14 +01007801 if (type == UNRES_WHEN) {
7802 /* remove previous result */
7803 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007804 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007805
Michal Vasko53b7da02018-02-13 15:28:42 +01007806 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007807}
7808
7809/**
7810 * @brief Resolve every unres data item in the structure. Logs directly.
7811 *
Radek Krejci082c84f2016-10-17 16:33:06 +02007812 * If options includes LYD_OPT_TRUSTED, the data are considered trusted (when, must conditions are not expected,
7813 * unresolved leafrefs/instids are accepted).
7814 *
7815 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
7816 *
Michal Vasko53b7da02018-02-13 15:28:42 +01007817 * @param[in] ctx Context used.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007818 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02007819 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
7820 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007821 *
7822 * @return EXIT_SUCCESS on success, -1 on error.
7823 */
7824int
Michal Vasko53b7da02018-02-13 15:28:42 +01007825resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007826{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007827 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007828 int rc, progress, ignore_fail;
Michal Vasko53b7da02018-02-13 15:28:42 +01007829 enum int_log_opts prev_ilo;
7830 struct ly_err_item *prev_eitem;
7831 LY_ERR prev_ly_errno;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007832 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02007833 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007834
Radek Krejci082c84f2016-10-17 16:33:06 +02007835 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01007836 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01007837
7838 if (!unres->count) {
7839 return EXIT_SUCCESS;
7840 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007841
Michal Vaskoad2e44a2017-01-03 10:31:35 +01007842 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 +01007843 ignore_fail = 1;
7844 } else if (options & LYD_OPT_NOEXTDEPS) {
7845 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007846 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007847 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007848 }
7849
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02007850 LOGVRB("Resolving unresolved data nodes and their constraints...");
Michal Vasko53b7da02018-02-13 15:28:42 +01007851 /* remember logging state */
7852 prev_ly_errno = ly_errno;
7853 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Radek Krejci010e54b2016-03-15 09:40:34 +01007854
Radek Krejci0b7704f2016-03-18 12:16:14 +01007855 /* when-stmt first */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007856 first = 1;
7857 stmt_count = 0;
7858 resolved = 0;
7859 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01007860 do {
Michal Vasko53b7da02018-02-13 15:28:42 +01007861 ly_err_free_next(ctx, prev_eitem);
Radek Krejci010e54b2016-03-15 09:40:34 +01007862 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02007863 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007864 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01007865 continue;
7866 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007867 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007868 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007869 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007870 }
7871
7872 /* resolve when condition only when all parent when conditions are already resolved */
7873 for (parent = unres->node[i]->parent;
7874 parent && LYD_WHEN_DONE(parent->when_status);
7875 parent = parent->parent) {
7876 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
7877 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02007878 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01007879 */
7880 unres->node[i]->when_status |= LYD_WHEN_FALSE;
7881 unres->type[i] = UNRES_RESOLVED;
7882 resolved++;
7883 break;
7884 }
7885 }
7886 if (parent) {
7887 continue;
7888 }
Radek Krejci010e54b2016-03-15 09:40:34 +01007889
Michal Vasko0b963112017-08-11 12:45:36 +02007890 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01007891 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02007892 /* finish with error/delete the node only if when was false, an external dependency was not required,
7893 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02007894 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vasko0b963112017-08-11 12:45:36 +02007895 && (!(when->flags & LYS_XPATH_DEP) || !(options & LYD_OPT_NOEXTDEPS))) {
Radek Krejci082c84f2016-10-17 16:33:06 +02007896 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01007897 /* false when condition */
Michal Vasko53b7da02018-02-13 15:28:42 +01007898 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007899 } /* follows else */
7900
Michal Vaskoe31d34a2017-03-28 14:50:38 +02007901 /* auto-delete */
Michal Vasko53b7da02018-02-13 15:28:42 +01007902 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond);
Michal Vaskoe31d34a2017-03-28 14:50:38 +02007903
Radek Krejci0c0086a2016-03-24 15:20:28 +01007904 /* only unlink now, the subtree can contain another nodes stored in the unres list */
7905 /* if it has parent non-presence containers that would be empty, we should actually
7906 * remove the container
7907 */
Radek Krejci2537fd32016-09-07 16:22:41 +02007908 for (parent = unres->node[i];
7909 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
7910 parent = parent->parent) {
7911 if (((struct lys_node_container *)parent->parent->schema)->presence) {
7912 /* presence container */
7913 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007914 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007915 if (parent->next || parent->prev != parent) {
7916 /* non empty (the child we are in and we are going to remove is not the only child) */
7917 break;
7918 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007919 }
Radek Krejci2537fd32016-09-07 16:22:41 +02007920 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01007921
Radek Krejci0c0086a2016-03-24 15:20:28 +01007922 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01007923 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01007924 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007925
Radek Krejci0b7704f2016-03-18 12:16:14 +01007926 lyd_unlink(unres->node[i]);
7927 unres->type[i] = UNRES_DELETE;
7928 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01007929
7930 /* update the rest of unres items */
7931 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01007932 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01007933 continue;
7934 }
7935
7936 /* test if the node is in subtree to be deleted */
7937 for (parent = unres->node[j]; parent; parent = parent->parent) {
7938 if (parent == unres->node[i]) {
7939 /* yes, it is */
7940 unres->type[j] = UNRES_RESOLVED;
7941 resolved++;
7942 break;
7943 }
7944 }
7945 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01007946 } else {
7947 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01007948 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007949 ly_err_free_next(ctx, prev_eitem);
Radek Krejci010e54b2016-03-15 09:40:34 +01007950 resolved++;
7951 progress = 1;
7952 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007953 goto error;
Radek Krejci2467a492016-10-24 15:16:59 +02007954 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01007955 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007956 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007957 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01007958
Radek Krejci0b7704f2016-03-18 12:16:14 +01007959 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007960 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007961 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007962 }
7963
7964 for (i = 0; del_items && i < unres->count; i++) {
7965 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
7966 if (unres->type[i] != UNRES_DELETE) {
7967 continue;
7968 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01007969 if (!unres->node[i]) {
7970 unres->type[i] = UNRES_RESOLVED;
7971 del_items--;
7972 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01007973 }
7974
7975 /* really remove the complete subtree */
7976 lyd_free(unres->node[i]);
7977 unres->type[i] = UNRES_RESOLVED;
7978 del_items--;
7979 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007980
7981 /* now leafrefs */
7982 first = 1;
7983 stmt_count = 0;
7984 resolved = 0;
7985 do {
7986 progress = 0;
7987 for (i = 0; i < unres->count; i++) {
7988 if (unres->type[i] != UNRES_LEAFREF) {
7989 continue;
7990 }
7991 if (first) {
7992 /* count leafref nodes in unres list */
7993 stmt_count++;
7994 }
7995
Michal Vasko0b963112017-08-11 12:45:36 +02007996 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02007997 if (!rc) {
7998 unres->type[i] = UNRES_RESOLVED;
Michal Vasko53b7da02018-02-13 15:28:42 +01007999 ly_err_free_next(ctx, prev_eitem);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008000 resolved++;
8001 progress = 1;
8002 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008003 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008004 } /* else forward reference */
8005 }
8006 first = 0;
8007 } while (progress && resolved < stmt_count);
8008
8009 /* do we have some unresolved leafrefs? */
8010 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008011 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008012 }
8013
Michal Vasko53b7da02018-02-13 15:28:42 +01008014 /* log normally now, throw away irrelevant errors */
8015 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8016 ly_errno = prev_ly_errno;
Radek Krejci010e54b2016-03-15 09:40:34 +01008017
8018 /* rest */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008019 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008020 if (unres->type[i] == UNRES_RESOLVED) {
8021 continue;
8022 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008023 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008024
Michal Vasko0b963112017-08-11 12:45:36 +02008025 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008026 if (rc) {
8027 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008028 return -1;
8029 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008030
8031 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008032 }
8033
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008034 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008035 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008036 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01008037
8038error:
8039 /* print all the new errors */
8040 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
8041 /* do not restore ly_errno, it was udpated properly */
8042 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008043}