blob: bd320bcf90e2ccd735919f43a7bf4c9858a2f9f5 [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"
Michal Vasko6c810702018-03-14 16:23:21 +010030#include "hash_table.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 Vasko185b5272018-09-13 14:26:12 +020033#include "validation.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020034
Michal Vasko2d44ee02018-05-18 09:38:51 +020035/* internal parsed predicate structure */
36struct parsed_pred {
37 const struct lys_node *schema;
38 int len;
39 struct {
40 const char *mod_name;
41 int mod_name_len;
42 const char *name;
43 int nam_len;
44 const char *value;
45 int val_len;
46 } *pred;
47};
48
Michal Vaskod24dd012016-09-30 12:20:22 +020049int
50parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num)
Michal Vasko4d1f0482016-09-19 14:35:06 +020051{
52 const char *ptr;
53 int minus = 0;
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020054 int64_t ret = 0, prev_ret;
Radek Krejcibf47a822016-11-04 10:06:08 +010055 int8_t str_exp, str_dig = -1, trailing_zeros = 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +020056
57 ptr = *str_num;
58
59 if (ptr[0] == '-') {
60 minus = 1;
61 ++ptr;
Radek Krejci51673202016-11-01 17:00:32 +010062 } else if (ptr[0] == '+') {
63 ++ptr;
Michal Vasko4d1f0482016-09-19 14:35:06 +020064 }
65
Michal Vaskod24dd012016-09-30 12:20:22 +020066 if (!isdigit(ptr[0])) {
67 /* there must be at least one */
68 return 1;
69 }
70
Michal Vasko4d1f0482016-09-19 14:35:06 +020071 for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) {
72 if (str_exp > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020073 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020074 }
75
76 if (ptr[0] == '.') {
77 if (ptr[1] == '.') {
78 /* it's the next interval */
79 break;
80 }
81 ++str_dig;
82 } else {
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020083 prev_ret = ret;
84 if (minus) {
85 ret = ret * 10 - (ptr[0] - '0');
86 if (ret > prev_ret) {
87 return 1;
88 }
89 } else {
90 ret = ret * 10 + (ptr[0] - '0');
91 if (ret < prev_ret) {
92 return 1;
93 }
94 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020095 if (str_dig > -1) {
96 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010097 if (ptr[0] == '0') {
98 /* possibly trailing zero */
99 trailing_zeros++;
100 } else {
101 trailing_zeros = 0;
102 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200103 }
104 ++str_exp;
105 }
106 }
Michal Vaskod24dd012016-09-30 12:20:22 +0200107 if (str_dig == 0) {
108 /* no digits after '.' */
109 return 1;
110 } else if (str_dig == -1) {
111 /* there are 0 numbers after the floating point */
Michal Vasko4d1f0482016-09-19 14:35:06 +0200112 str_dig = 0;
113 }
Radek Krejcibf47a822016-11-04 10:06:08 +0100114 /* remove trailing zeros */
115 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +0100116 str_dig -= trailing_zeros;
117 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +0100118 ret = ret / dec_pow(trailing_zeros);
119 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200120
121 /* it's parsed, now adjust the number based on fraction-digits, if needed */
122 if (str_dig < dig) {
123 if ((str_exp - 1) + (dig - str_dig) > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200124 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200125 }
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200126 prev_ret = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200127 ret *= dec_pow(dig - str_dig);
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200128 if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) {
129 return 1;
130 }
131
Michal Vasko4d1f0482016-09-19 14:35:06 +0200132 }
133 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200134 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200135 }
136
Michal Vasko4d1f0482016-09-19 14:35:06 +0200137 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200138 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200139
Michal Vaskod24dd012016-09-30 12:20:22 +0200140 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200141}
142
143/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200144 * @brief Parse an identifier.
145 *
146 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
147 * identifier = (ALPHA / "_")
148 * *(ALPHA / DIGIT / "_" / "-" / ".")
149 *
Michal Vaskobb211122015-08-19 14:03:11 +0200150 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200151 *
152 * @return Number of characters successfully parsed.
153 */
Radek Krejcidce5f972017-09-12 15:47:49 +0200154unsigned int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200155parse_identifier(const char *id)
156{
Radek Krejcidce5f972017-09-12 15:47:49 +0200157 unsigned int parsed = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200158
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100159 assert(id);
160
Radek Krejci6dc53a22015-08-17 13:27:59 +0200161 if (!isalpha(id[0]) && (id[0] != '_')) {
162 return -parsed;
163 }
164
165 ++parsed;
166 ++id;
167
168 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
169 ++parsed;
170 ++id;
171 }
172
173 return parsed;
174}
175
176/**
177 * @brief Parse a node-identifier.
178 *
Michal Vasko723e50c2015-10-20 15:20:29 +0200179 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200180 *
Michal Vaskobb211122015-08-19 14:03:11 +0200181 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200182 * @param[out] mod_name Points to the module name, NULL if there is not any.
183 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200184 * @param[out] name Points to the node name.
185 * @param[out] nam_len Length of the node name.
Michal Vasko50576712017-07-28 12:28:33 +0200186 * @param[out] all_desc Whether the path starts with '/', only supported in extended paths.
PavolVicanb28bbff2018-02-21 00:44:02 +0100187 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200188 *
189 * @return Number of characters successfully parsed,
190 * positive on success, negative on failure.
191 */
192static int
Michal Vasko50576712017-07-28 12:28:33 +0200193parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
194 int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200195{
PavolVican195cf392018-02-23 13:24:45 +0100196 int parsed = 0, ret, all_desc_local = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200197
198 assert(id);
Michal Vasko50576712017-07-28 12:28:33 +0200199 assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len));
200 assert((name && nam_len) || (!name && !nam_len));
Michal Vasko50576712017-07-28 12:28:33 +0200201
Michal Vasko723e50c2015-10-20 15:20:29 +0200202 if (mod_name) {
203 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200204 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200205 }
206 if (name) {
207 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200208 *nam_len = 0;
209 }
210
Michal Vasko50576712017-07-28 12:28:33 +0200211 if (extended) {
212 /* try to parse only the extended expressions */
213 if (id[parsed] == '/') {
PavolVican195cf392018-02-23 13:24:45 +0100214 if (all_desc) {
215 *all_desc = 1;
216 }
217 all_desc_local = 1;
Michal Vasko50576712017-07-28 12:28:33 +0200218 } else {
PavolVican195cf392018-02-23 13:24:45 +0100219 if (all_desc) {
220 *all_desc = 0;
221 }
Michal Vasko50576712017-07-28 12:28:33 +0200222 }
223
224 /* is there a prefix? */
PavolVican195cf392018-02-23 13:24:45 +0100225 ret = parse_identifier(id + all_desc_local);
Michal Vasko50576712017-07-28 12:28:33 +0200226 if (ret > 0) {
PavolVican195cf392018-02-23 13:24:45 +0100227 if (id[all_desc_local + ret] != ':') {
Michal Vasko50576712017-07-28 12:28:33 +0200228 /* this is not a prefix, so not an extended id */
229 goto standard_id;
230 }
231
232 if (mod_name) {
PavolVican195cf392018-02-23 13:24:45 +0100233 *mod_name = id + all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200234 *mod_name_len = ret;
235 }
236
237 /* "/" and ":" */
PavolVican195cf392018-02-23 13:24:45 +0100238 ret += all_desc_local + 1;
Michal Vasko50576712017-07-28 12:28:33 +0200239 } else {
PavolVican195cf392018-02-23 13:24:45 +0100240 ret = all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200241 }
242
243 /* parse either "*" or "." */
PavolVicanb28bbff2018-02-21 00:44:02 +0100244 if (*(id + ret) == '*') {
Michal Vasko50576712017-07-28 12:28:33 +0200245 if (name) {
246 *name = id + ret;
247 *nam_len = 1;
248 }
249 ++ret;
250
251 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100252 } else if (*(id + ret) == '.') {
PavolVican195cf392018-02-23 13:24:45 +0100253 if (!all_desc_local) {
Michal Vasko50576712017-07-28 12:28:33 +0200254 /* /. is redundant expression, we do not accept it */
255 return -ret;
256 }
257
258 if (name) {
259 *name = id + ret;
260 *nam_len = 1;
261 }
262 ++ret;
263
264 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100265 } else if (*(id + ret) == '#') {
PavolVican195cf392018-02-23 13:24:45 +0100266 if (all_desc_local || !ret) {
PavolVicanb28bbff2018-02-21 00:44:02 +0100267 /* no prefix */
268 return 0;
269 }
270 parsed = ret + 1;
271 if ((ret = parse_identifier(id + parsed)) < 1) {
272 return -parsed + ret;
273 }
274 *name = id + parsed - 1;
275 *nam_len = ret + 1;
276 return parsed + ret;
Michal Vasko50576712017-07-28 12:28:33 +0200277 }
278 /* else a standard id, parse it all again */
279 }
280
281standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200282 if ((ret = parse_identifier(id)) < 1) {
283 return ret;
284 }
285
Michal Vasko723e50c2015-10-20 15:20:29 +0200286 if (mod_name) {
287 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200288 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200289 }
290
291 parsed += ret;
292 id += ret;
293
294 /* there is prefix */
295 if (id[0] == ':') {
296 ++parsed;
297 ++id;
298
299 /* there isn't */
300 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200301 if (name && mod_name) {
302 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200303 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200304 if (mod_name) {
305 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200306 }
307
Michal Vasko723e50c2015-10-20 15:20:29 +0200308 if (nam_len && mod_name_len) {
309 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200310 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200311 if (mod_name_len) {
312 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200313 }
314
315 return parsed;
316 }
317
318 /* identifier (node name) */
319 if ((ret = parse_identifier(id)) < 1) {
320 return -parsed+ret;
321 }
322
323 if (name) {
324 *name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200325 *nam_len = ret;
326 }
327
328 return parsed+ret;
329}
330
331/**
332 * @brief Parse a path-predicate (leafref).
333 *
334 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
335 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
336 *
Michal Vaskobb211122015-08-19 14:03:11 +0200337 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200338 * @param[out] prefix Points to the prefix, NULL if there is not any.
339 * @param[out] pref_len Length of the prefix, 0 if there is not any.
340 * @param[out] name Points to the node name.
341 * @param[out] nam_len Length of the node name.
342 * @param[out] path_key_expr Points to the path-key-expr.
343 * @param[out] pke_len Length of the path-key-expr.
344 * @param[out] has_predicate Flag to mark whether there is another predicate following.
345 *
346 * @return Number of characters successfully parsed,
347 * positive on success, negative on failure.
348 */
349static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200350parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
351 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200352{
353 const char *ptr;
354 int parsed = 0, ret;
355
356 assert(id);
357 if (prefix) {
358 *prefix = NULL;
359 }
360 if (pref_len) {
361 *pref_len = 0;
362 }
363 if (name) {
364 *name = NULL;
365 }
366 if (nam_len) {
367 *nam_len = 0;
368 }
369 if (path_key_expr) {
370 *path_key_expr = NULL;
371 }
372 if (pke_len) {
373 *pke_len = 0;
374 }
375 if (has_predicate) {
376 *has_predicate = 0;
377 }
378
379 if (id[0] != '[') {
380 return -parsed;
381 }
382
383 ++parsed;
384 ++id;
385
386 while (isspace(id[0])) {
387 ++parsed;
388 ++id;
389 }
390
Michal Vasko50576712017-07-28 12:28:33 +0200391 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200392 return -parsed+ret;
393 }
394
395 parsed += ret;
396 id += ret;
397
398 while (isspace(id[0])) {
399 ++parsed;
400 ++id;
401 }
402
403 if (id[0] != '=') {
404 return -parsed;
405 }
406
407 ++parsed;
408 ++id;
409
410 while (isspace(id[0])) {
411 ++parsed;
412 ++id;
413 }
414
415 if ((ptr = strchr(id, ']')) == NULL) {
416 return -parsed;
417 }
418
419 --ptr;
420 while (isspace(ptr[0])) {
421 --ptr;
422 }
423 ++ptr;
424
425 ret = ptr-id;
426 if (path_key_expr) {
427 *path_key_expr = id;
428 }
429 if (pke_len) {
430 *pke_len = ret;
431 }
432
433 parsed += ret;
434 id += ret;
435
436 while (isspace(id[0])) {
437 ++parsed;
438 ++id;
439 }
440
441 assert(id[0] == ']');
442
443 if (id[1] == '[') {
444 *has_predicate = 1;
445 }
446
447 return parsed+1;
448}
449
450/**
451 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
452 * the ".." and the first node-identifier, other calls parse a single
453 * node-identifier each.
454 *
455 * path-key-expr = current-function-invocation *WSP "/" *WSP
456 * rel-path-keyexpr
457 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
458 * *(node-identifier *WSP "/" *WSP)
459 * node-identifier
460 *
Michal Vaskobb211122015-08-19 14:03:11 +0200461 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200462 * @param[out] prefix Points to the prefix, NULL if there is not any.
463 * @param[out] pref_len Length of the prefix, 0 if there is not any.
464 * @param[out] name Points to the node name.
465 * @param[out] nam_len Length of the node name.
466 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
467 * must not be changed between consecutive calls.
468 * @return Number of characters successfully parsed,
469 * positive on success, negative on failure.
470 */
471static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200472parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
473 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200474{
475 int parsed = 0, ret, par_times = 0;
476
477 assert(id);
478 assert(parent_times);
479 if (prefix) {
480 *prefix = NULL;
481 }
482 if (pref_len) {
483 *pref_len = 0;
484 }
485 if (name) {
486 *name = NULL;
487 }
488 if (nam_len) {
489 *nam_len = 0;
490 }
491
492 if (!*parent_times) {
493 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
494 if (strncmp(id, "current()", 9)) {
495 return -parsed;
496 }
497
498 parsed += 9;
499 id += 9;
500
501 while (isspace(id[0])) {
502 ++parsed;
503 ++id;
504 }
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 /* rel-path-keyexpr */
519 if (strncmp(id, "..", 2)) {
520 return -parsed;
521 }
522 ++par_times;
523
524 parsed += 2;
525 id += 2;
526
527 while (isspace(id[0])) {
528 ++parsed;
529 ++id;
530 }
531 }
532
533 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
534 *
535 * first parent reference with whitespaces already parsed
536 */
537 if (id[0] != '/') {
538 return -parsed;
539 }
540
541 ++parsed;
542 ++id;
543
544 while (isspace(id[0])) {
545 ++parsed;
546 ++id;
547 }
548
549 while (!strncmp(id, "..", 2) && !*parent_times) {
550 ++par_times;
551
552 parsed += 2;
553 id += 2;
554
555 while (isspace(id[0])) {
556 ++parsed;
557 ++id;
558 }
559
560 if (id[0] != '/') {
561 return -parsed;
562 }
563
564 ++parsed;
565 ++id;
566
567 while (isspace(id[0])) {
568 ++parsed;
569 ++id;
570 }
571 }
572
573 if (!*parent_times) {
574 *parent_times = par_times;
575 }
576
577 /* all parent references must be parsed at this point */
Michal Vasko50576712017-07-28 12:28:33 +0200578 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
579 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200580 }
581
582 parsed += ret;
583 id += ret;
584
585 return parsed;
586}
587
588/**
589 * @brief Parse path-arg (leafref).
590 *
591 * path-arg = absolute-path / relative-path
592 * absolute-path = 1*("/" (node-identifier *path-predicate))
593 * relative-path = 1*(".." "/") descendant-path
594 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200595 * @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 +0200596 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200597 * @param[out] prefix Points to the prefix, NULL if there is not any.
598 * @param[out] pref_len Length of the prefix, 0 if there is not any.
599 * @param[out] name Points to the node name.
600 * @param[out] nam_len Length of the node name.
601 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
602 * must not be changed between consecutive calls. -1 if the
603 * path is relative.
604 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
605 *
606 * @return Number of characters successfully parsed,
607 * positive on success, negative on failure.
608 */
609static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200610parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200611 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200612{
613 int parsed = 0, ret, par_times = 0;
614
615 assert(id);
616 assert(parent_times);
617 if (prefix) {
618 *prefix = NULL;
619 }
620 if (pref_len) {
621 *pref_len = 0;
622 }
623 if (name) {
624 *name = NULL;
625 }
626 if (nam_len) {
627 *nam_len = 0;
628 }
629 if (has_predicate) {
630 *has_predicate = 0;
631 }
632
633 if (!*parent_times && !strncmp(id, "..", 2)) {
634 ++par_times;
635
636 parsed += 2;
637 id += 2;
638
639 while (!strncmp(id, "/..", 3)) {
640 ++par_times;
641
642 parsed += 3;
643 id += 3;
644 }
645 }
646
647 if (!*parent_times) {
648 if (par_times) {
649 *parent_times = par_times;
650 } else {
651 *parent_times = -1;
652 }
653 }
654
655 if (id[0] != '/') {
656 return -parsed;
657 }
658
659 /* skip '/' */
660 ++parsed;
661 ++id;
662
663 /* node-identifier ([prefix:]identifier) */
Michal Vasko50576712017-07-28 12:28:33 +0200664 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
665 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200666 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200667 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200668 /* actually we always need prefix even it is not specified */
669 *prefix = lys_main_module(mod)->name;
670 *pref_len = strlen(*prefix);
671 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200672
673 parsed += ret;
674 id += ret;
675
676 /* there is no predicate */
677 if ((id[0] == '/') || !id[0]) {
678 return parsed;
679 } else if (id[0] != '[') {
680 return -parsed;
681 }
682
683 if (has_predicate) {
684 *has_predicate = 1;
685 }
686
687 return parsed;
688}
689
690/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200691 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200692 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200693 *
694 * instance-identifier = 1*("/" (node-identifier *predicate))
695 *
Michal Vaskobb211122015-08-19 14:03:11 +0200696 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200697 * @param[out] model Points to the model name.
698 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200699 * @param[out] name Points to the node name.
700 * @param[out] nam_len Length of the node name.
701 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
702 *
703 * @return Number of characters successfully parsed,
704 * positive on success, negative on failure.
705 */
706static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200707parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
708 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200709{
710 int parsed = 0, ret;
711
Michal Vasko1b6ca962017-08-03 14:23:09 +0200712 assert(id && model && mod_len && name && nam_len);
713
Radek Krejci6dc53a22015-08-17 13:27:59 +0200714 if (has_predicate) {
715 *has_predicate = 0;
716 }
717
718 if (id[0] != '/') {
719 return -parsed;
720 }
721
722 ++parsed;
723 ++id;
724
Michal Vaskob2f40be2016-09-08 16:03:48 +0200725 if ((ret = parse_identifier(id)) < 1) {
726 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200727 }
728
Michal Vaskob2f40be2016-09-08 16:03:48 +0200729 *name = id;
730 *nam_len = ret;
731
732 parsed += ret;
733 id += ret;
734
Michal Vasko1b6ca962017-08-03 14:23:09 +0200735 if (id[0] == ':') {
736 /* we have prefix */
737 *model = *name;
738 *mod_len = *nam_len;
739
740 ++parsed;
741 ++id;
742
743 if ((ret = parse_identifier(id)) < 1) {
744 return ret;
745 }
746
747 *name = id;
748 *nam_len = ret;
749
750 parsed += ret;
751 id += ret;
752 }
753
Radek Krejci4967cb62016-09-14 16:40:28 +0200754 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200755 *has_predicate = 1;
756 }
757
758 return parsed;
759}
760
761/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200762 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200763 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200764 *
765 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
766 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
767 * ((DQUOTE string DQUOTE) /
768 * (SQUOTE string SQUOTE))
769 * pos = non-negative-integer-value
770 *
Michal Vaskobb211122015-08-19 14:03:11 +0200771 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200772 * @param[out] model Points to the model name.
773 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200774 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
775 * @param[out] nam_len Length of the node name.
776 * @param[out] value Value the node-identifier must have (string from the grammar),
777 * NULL if there is not any.
778 * @param[out] val_len Length of the value, 0 if there is not any.
779 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
780 *
781 * @return Number of characters successfully parsed,
782 * positive on success, negative on failure.
783 */
784static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200785parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
786 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200787{
788 const char *ptr;
789 int parsed = 0, ret;
790 char quote;
791
792 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200793 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200794 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200795 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200796 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200797 }
798 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200799 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200800 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200801 *nam_len = 0;
802 }
803 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200804 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200805 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200806 *val_len = 0;
807 }
808 if (has_predicate) {
809 *has_predicate = 0;
810 }
811
812 if (id[0] != '[') {
813 return -parsed;
814 }
815
816 ++parsed;
817 ++id;
818
819 while (isspace(id[0])) {
820 ++parsed;
821 ++id;
822 }
823
824 /* pos */
825 if (isdigit(id[0])) {
826 if (name) {
827 *name = id;
828 }
829
830 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200831 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200832 }
833
834 while (isdigit(id[0])) {
835 ++parsed;
836 ++id;
837 }
838
839 if (nam_len) {
840 *nam_len = id-(*name);
841 }
842
Michal Vaskof2f28a12016-09-09 12:43:06 +0200843 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200844 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200845 if (id[0] == '.') {
846 if (name) {
847 *name = id;
848 }
849 if (nam_len) {
850 *nam_len = 1;
851 }
852
853 ++parsed;
854 ++id;
855
856 } else {
Michal Vasko50576712017-07-28 12:28:33 +0200857 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200858 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200859 }
860
861 parsed += ret;
862 id += ret;
863 }
864
865 while (isspace(id[0])) {
866 ++parsed;
867 ++id;
868 }
869
870 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200871 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200872 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200873
Radek Krejci6dc53a22015-08-17 13:27:59 +0200874 ++parsed;
875 ++id;
876
Michal Vaskof2f28a12016-09-09 12:43:06 +0200877 while (isspace(id[0])) {
878 ++parsed;
879 ++id;
880 }
881
882 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
883 if ((id[0] == '\"') || (id[0] == '\'')) {
884 quote = id[0];
885
886 ++parsed;
887 ++id;
888
889 if ((ptr = strchr(id, quote)) == NULL) {
890 return -parsed;
891 }
Michal Vasko1b6ca962017-08-03 14:23:09 +0200892 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200893
894 if (value) {
895 *value = id;
896 }
897 if (val_len) {
898 *val_len = ret;
899 }
900
Michal Vasko1b6ca962017-08-03 14:23:09 +0200901 parsed += ret + 1;
902 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200903 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200904 return -parsed;
905 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200906 }
907
908 while (isspace(id[0])) {
909 ++parsed;
910 ++id;
911 }
912
913 if (id[0] != ']') {
914 return -parsed;
915 }
916
917 ++parsed;
918 ++id;
919
920 if ((id[0] == '[') && has_predicate) {
921 *has_predicate = 1;
922 }
923
924 return parsed;
925}
926
927/**
928 * @brief Parse schema-nodeid.
929 *
930 * schema-nodeid = absolute-schema-nodeid /
931 * descendant-schema-nodeid
932 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200933 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200934 * node-identifier
935 * absolute-schema-nodeid
936 *
Michal Vaskobb211122015-08-19 14:03:11 +0200937 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200938 * @param[out] mod_name Points to the module name, NULL if there is not any.
939 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200940 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200941 * @param[out] nam_len Length of the node name.
942 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
943 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100944 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
945 * based on the grammar, in those cases use NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200946 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200947 *
948 * @return Number of characters successfully parsed,
949 * positive on success, negative on failure.
950 */
Michal Vasko22448d32016-03-16 13:17:29 +0100951int
Michal Vasko723e50c2015-10-20 15:20:29 +0200952parse_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 +0200953 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200954{
955 int parsed = 0, ret;
956
957 assert(id);
958 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200959
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100960 if (has_predicate) {
961 *has_predicate = 0;
962 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200963
964 if (id[0] != '/') {
965 if (*is_relative != -1) {
966 return -parsed;
967 } else {
968 *is_relative = 1;
969 }
Michal Vasko48935352016-03-29 11:52:36 +0200970 if (!strncmp(id, "./", 2)) {
971 parsed += 2;
972 id += 2;
973 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200974 } else {
975 if (*is_relative == -1) {
976 *is_relative = 0;
977 }
978 ++parsed;
979 ++id;
980 }
981
Michal Vasko50576712017-07-28 12:28:33 +0200982 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
983 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200984 }
985
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100986 parsed += ret;
987 id += ret;
988
989 if ((id[0] == '[') && has_predicate) {
990 *has_predicate = 1;
991 }
992
993 return parsed;
994}
995
996/**
997 * @brief Parse schema predicate (special format internally used).
998 *
999 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001000 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001001 * key-with-value = identifier *WSP "=" *WSP
1002 * ((DQUOTE string DQUOTE) /
1003 * (SQUOTE string SQUOTE))
1004 *
1005 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001006 * @param[out] mod_name Points to the list key module name.
1007 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001008 * @param[out] name Points to the list key name.
1009 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +01001010 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001011 * @param[out] val_len Length of \p value.
1012 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
1013 */
Michal Vasko22448d32016-03-16 13:17:29 +01001014int
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001015parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
1016 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001017{
1018 const char *ptr;
1019 int parsed = 0, ret;
1020 char quote;
1021
1022 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001023 if (mod_name) {
1024 *mod_name = NULL;
1025 }
1026 if (mod_name_len) {
1027 *mod_name_len = 0;
1028 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001029 if (name) {
1030 *name = NULL;
1031 }
1032 if (nam_len) {
1033 *nam_len = 0;
1034 }
1035 if (value) {
1036 *value = NULL;
1037 }
1038 if (val_len) {
1039 *val_len = 0;
1040 }
1041 if (has_predicate) {
1042 *has_predicate = 0;
1043 }
1044
1045 if (id[0] != '[') {
1046 return -parsed;
1047 }
1048
1049 ++parsed;
1050 ++id;
1051
1052 while (isspace(id[0])) {
1053 ++parsed;
1054 ++id;
1055 }
1056
Michal Vasko22448d32016-03-16 13:17:29 +01001057 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001058 if (id[0] == '.') {
1059 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001060
1061 if (name) {
1062 *name = id;
1063 }
1064 if (nam_len) {
1065 *nam_len = ret;
1066 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001067 } else if (isdigit(id[0])) {
1068 if (id[0] == '0') {
1069 return -parsed;
1070 }
1071 ret = 1;
1072 while (isdigit(id[ret])) {
1073 ++ret;
1074 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001075
1076 if (name) {
1077 *name = id;
1078 }
1079 if (nam_len) {
1080 *nam_len = ret;
1081 }
Michal Vasko50576712017-07-28 12:28:33 +02001082 } 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 +01001083 return -parsed + ret;
1084 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001085
1086 parsed += ret;
1087 id += ret;
1088
1089 while (isspace(id[0])) {
1090 ++parsed;
1091 ++id;
1092 }
1093
1094 /* there is value as well */
1095 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001096 if (name && isdigit(**name)) {
1097 return -parsed;
1098 }
1099
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001100 ++parsed;
1101 ++id;
1102
1103 while (isspace(id[0])) {
1104 ++parsed;
1105 ++id;
1106 }
1107
1108 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1109 if ((id[0] == '\"') || (id[0] == '\'')) {
1110 quote = id[0];
1111
1112 ++parsed;
1113 ++id;
1114
1115 if ((ptr = strchr(id, quote)) == NULL) {
1116 return -parsed;
1117 }
1118 ret = ptr - id;
1119
1120 if (value) {
1121 *value = id;
1122 }
1123 if (val_len) {
1124 *val_len = ret;
1125 }
1126
1127 parsed += ret + 1;
1128 id += ret + 1;
1129 } else {
1130 return -parsed;
1131 }
1132
1133 while (isspace(id[0])) {
1134 ++parsed;
1135 ++id;
1136 }
1137 }
1138
1139 if (id[0] != ']') {
1140 return -parsed;
1141 }
1142
1143 ++parsed;
1144 ++id;
1145
1146 if ((id[0] == '[') && has_predicate) {
1147 *has_predicate = 1;
1148 }
1149
1150 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001151}
1152
Michal Vasko2d44ee02018-05-18 09:38:51 +02001153#ifdef LY_ENABLED_CACHE
1154
1155static int
1156resolve_hash_table_find_equal(void *val1_p, void *val2_p, int mod, void *UNUSED(cb_data))
1157{
1158 struct lyd_node *val2, *elem2;
1159 struct parsed_pred pp;
1160 const char *str;
1161 int i;
1162
1163 assert(!mod);
Michal Vasko87e29a82018-05-21 13:50:43 +02001164 (void)mod;
Michal Vasko2d44ee02018-05-18 09:38:51 +02001165
1166 pp = *((struct parsed_pred *)val1_p);
1167 val2 = *((struct lyd_node **)val2_p);
1168
1169 if (val2->schema != pp.schema) {
1170 return 0;
1171 }
1172
1173 switch (val2->schema->nodetype) {
1174 case LYS_CONTAINER:
1175 case LYS_LEAF:
1176 case LYS_ANYXML:
1177 case LYS_ANYDATA:
1178 return 1;
1179 case LYS_LEAFLIST:
1180 str = ((struct lyd_node_leaf_list *)val2)->value_str;
1181 if (!strncmp(str, pp.pred[0].value, pp.pred[0].val_len) && !str[pp.pred[0].val_len]) {
1182 return 1;
1183 }
1184 return 0;
1185 case LYS_LIST:
1186 assert(((struct lys_node_list *)val2->schema)->keys_size);
1187 assert(((struct lys_node_list *)val2->schema)->keys_size == pp.len);
1188
1189 /* lists with keys, their equivalence is based on their keys */
1190 elem2 = val2->child;
1191 /* the exact data order is guaranteed */
1192 for (i = 0; elem2 && (i < pp.len); ++i) {
1193 /* module check */
1194 if (pp.pred[i].mod_name) {
1195 if (strncmp(lyd_node_module(elem2)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len)
1196 || lyd_node_module(elem2)->name[pp.pred[i].mod_name_len]) {
1197 break;
1198 }
1199 } else {
1200 if (lyd_node_module(elem2) != lys_node_module(pp.schema)) {
1201 break;
1202 }
1203 }
1204
1205 /* name check */
1206 if (strncmp(elem2->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || elem2->schema->name[pp.pred[i].nam_len]) {
1207 break;
1208 }
1209
1210 /* value check */
1211 str = ((struct lyd_node_leaf_list *)elem2)->value_str;
1212 if (strncmp(str, pp.pred[i].value, pp.pred[i].val_len) || str[pp.pred[i].val_len]) {
1213 break;
1214 }
1215
1216 /* next key */
1217 elem2 = elem2->next;
1218 }
1219 if (i == pp.len) {
1220 return 1;
1221 }
1222 return 0;
1223 default:
1224 break;
1225 }
1226
1227 LOGINT(val2->schema->module->ctx);
1228 return 0;
1229}
1230
1231static struct lyd_node *
1232resolve_json_data_node_hash(struct lyd_node *parent, struct parsed_pred pp)
1233{
1234 values_equal_cb prev_cb;
1235 struct lyd_node **ret = NULL;
1236 uint32_t hash;
1237 int i;
1238
1239 assert(parent && parent->hash);
1240
1241 /* set our value equivalence callback that does not require data nodes */
1242 prev_cb = lyht_set_cb(parent->ht, resolve_hash_table_find_equal);
1243
1244 /* get the hash of the searched node */
1245 hash = dict_hash_multi(0, lys_node_module(pp.schema)->name, strlen(lys_node_module(pp.schema)->name));
1246 hash = dict_hash_multi(hash, pp.schema->name, strlen(pp.schema->name));
1247 if (pp.schema->nodetype == LYS_LEAFLIST) {
1248 assert((pp.len == 1) && (pp.pred[0].name[0] == '.') && (pp.pred[0].nam_len == 1));
1249 /* leaf-list value in predicate */
1250 hash = dict_hash_multi(hash, pp.pred[0].value, pp.pred[0].val_len);
1251 } else if (pp.schema->nodetype == LYS_LIST) {
1252 /* list keys in predicates */
1253 for (i = 0; i < pp.len; ++i) {
1254 hash = dict_hash_multi(hash, pp.pred[i].value, pp.pred[i].val_len);
1255 }
1256 }
1257 hash = dict_hash_multi(hash, NULL, 0);
1258
1259 /* try to find the node */
Michal Vaskod60a1a32018-05-23 16:31:22 +02001260 i = lyht_find(parent->ht, &pp, hash, (void **)&ret);
1261 assert(i || *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001262
1263 /* restore the original callback */
1264 lyht_set_cb(parent->ht, prev_cb);
1265
Michal Vaskod60a1a32018-05-23 16:31:22 +02001266 return (i ? NULL : *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001267}
1268
1269#endif
1270
Radek Krejci6dc53a22015-08-17 13:27:59 +02001271/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001272 * @brief Resolve (find) a feature definition. Logs directly.
1273 *
1274 * @param[in] feat_name Feature name to resolve.
1275 * @param[in] len Length of \p feat_name.
1276 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001277 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1278 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001279 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001280 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001281 */
1282static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001283resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001284{
1285 char *str;
1286 const char *mod_name, *name;
1287 int mod_name_len, nam_len, i, j;
1288 const struct lys_module *module;
1289
Radek Krejci9ff0a922016-07-14 13:08:05 +02001290 assert(feature);
1291
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001292 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001293 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 +01001294 LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001295 return -1;
1296 }
1297
Michal Vasko921eb6b2017-10-13 10:01:39 +02001298 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001299 if (!module) {
1300 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01001301 LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001302 return -1;
1303 }
1304
Radek Krejci9ff0a922016-07-14 13:08:05 +02001305 if (module != node->module && module == lys_node_module(node)) {
1306 /* first, try to search directly in submodule where the feature was mentioned */
1307 for (j = 0; j < node->module->features_size; j++) {
1308 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1309 /* check status */
1310 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001311 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001312 return -1;
1313 }
1314 *feature = &node->module->features[j];
1315 return 0;
1316 }
1317 }
1318 }
1319
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001320 /* search in the identified module ... */
1321 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001322 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001323 /* check status */
1324 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001325 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001326 return -1;
1327 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001328 *feature = &module->features[j];
1329 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001330 }
1331 }
1332 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001333 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001334 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001335 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1336 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001337 /* check status */
1338 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1339 module->inc[i].submodule->features[j].flags,
1340 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001341 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001342 return -1;
1343 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001344 *feature = &module->inc[i].submodule->features[j];
1345 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001346 }
1347 }
1348 }
1349
1350 /* not found */
1351 str = strndup(feat_name, len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001352 LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001353 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001354 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001355}
1356
Radek Krejci9ff0a922016-07-14 13:08:05 +02001357/*
1358 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001359 * - 1 if enabled
1360 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001361 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001362static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001363resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001364{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001365 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001366
Radek Krejci9ff0a922016-07-14 13:08:05 +02001367 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001368 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001369 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001370 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001371 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001372
Radek Krejci69b8d922016-07-27 13:13:41 +02001373 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001374}
1375
1376static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001377resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001378{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001379 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001380 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001381
Radek Krejci9ff0a922016-07-14 13:08:05 +02001382 op = iff_getop(expr->expr, *index_e);
1383 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001384
Radek Krejci9ff0a922016-07-14 13:08:05 +02001385 switch (op) {
1386 case LYS_IFF_F:
1387 /* resolve feature */
1388 return resolve_feature_value(expr->features[(*index_f)++]);
1389 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001390 /* invert result */
1391 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001392 case LYS_IFF_AND:
1393 case LYS_IFF_OR:
1394 a = resolve_iffeature_recursive(expr, index_e, index_f);
1395 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001396 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001397 return a && b;
1398 } else { /* LYS_IFF_OR */
1399 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001400 }
1401 }
1402
Radek Krejciaf566332017-02-07 15:56:59 +01001403 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001404}
1405
1406int
1407resolve_iffeature(struct lys_iffeature *expr)
1408{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001409 int index_e = 0, index_f = 0;
1410
1411 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001412 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001413 }
Radek Krejciaf566332017-02-07 15:56:59 +01001414 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001415}
1416
1417struct iff_stack {
1418 int size;
1419 int index; /* first empty item */
1420 uint8_t *stack;
1421};
1422
1423static int
1424iff_stack_push(struct iff_stack *stack, uint8_t value)
1425{
1426 if (stack->index == stack->size) {
1427 stack->size += 4;
1428 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001429 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001430 }
1431
1432 stack->stack[stack->index++] = value;
1433 return EXIT_SUCCESS;
1434}
1435
1436static uint8_t
1437iff_stack_pop(struct iff_stack *stack)
1438{
1439 stack->index--;
1440 return stack->stack[stack->index];
1441}
1442
1443static void
1444iff_stack_clean(struct iff_stack *stack)
1445{
1446 stack->size = 0;
1447 free(stack->stack);
1448}
1449
1450static void
1451iff_setop(uint8_t *list, uint8_t op, int pos)
1452{
1453 uint8_t *item;
1454 uint8_t mask = 3;
1455
1456 assert(pos >= 0);
1457 assert(op <= 3); /* max 2 bits */
1458
1459 item = &list[pos / 4];
1460 mask = mask << 2 * (pos % 4);
1461 *item = (*item) & ~mask;
1462 *item = (*item) | (op << 2 * (pos % 4));
1463}
1464
1465uint8_t
1466iff_getop(uint8_t *list, int pos)
1467{
1468 uint8_t *item;
1469 uint8_t mask = 3, result;
1470
1471 assert(pos >= 0);
1472
1473 item = &list[pos / 4];
1474 result = (*item) & (mask << 2 * (pos % 4));
1475 return result >> 2 * (pos % 4);
1476}
1477
1478#define LYS_IFF_LP 0x04 /* ( */
1479#define LYS_IFF_RP 0x08 /* ) */
1480
Radek Krejcicbb473e2016-09-16 14:48:32 +02001481/* internal structure for passing data for UNRES_IFFEAT */
1482struct unres_iffeat_data {
1483 struct lys_node *node;
1484 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001485 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001486};
1487
Radek Krejci9ff0a922016-07-14 13:08:05 +02001488void
1489resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1490{
1491 unsigned int e = 0, f = 0, r = 0;
1492 uint8_t op;
1493
1494 assert(iffeat);
1495
1496 if (!iffeat->expr) {
1497 goto result;
1498 }
1499
1500 do {
1501 op = iff_getop(iffeat->expr, e++);
1502 switch (op) {
1503 case LYS_IFF_NOT:
1504 if (!r) {
1505 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001506 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001507 break;
1508 case LYS_IFF_AND:
1509 case LYS_IFF_OR:
1510 if (!r) {
1511 r += 2;
1512 } else {
1513 r += 1;
1514 }
1515 break;
1516 case LYS_IFF_F:
1517 f++;
1518 if (r) {
1519 r--;
1520 }
1521 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001522 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001523 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001524
Radek Krejci9ff0a922016-07-14 13:08:05 +02001525result:
1526 if (expr_size) {
1527 *expr_size = e;
1528 }
1529 if (feat_size) {
1530 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001531 }
1532}
1533
1534int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001535resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001536 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001537{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001538 const char *c = value;
1539 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001540 int i, j, last_not, checkversion = 0;
1541 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001542 uint8_t op;
1543 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001544 struct unres_iffeat_data *iff_data;
Michal Vasko53b7da02018-02-13 15:28:42 +01001545 struct ly_ctx *ctx = node->module->ctx;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001546
Radek Krejci9ff0a922016-07-14 13:08:05 +02001547 assert(c);
1548
1549 if (isspace(c[0])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001550 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001551 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001552 }
1553
Radek Krejci9ff0a922016-07-14 13:08:05 +02001554 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1555 for (i = j = last_not = 0; c[i]; i++) {
1556 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001557 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001558 j++;
1559 continue;
1560 } else if (c[i] == ')') {
1561 j--;
1562 continue;
1563 } else if (isspace(c[i])) {
1564 continue;
1565 }
1566
1567 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1568 if (c[i + r] == '\0') {
Michal Vasko53b7da02018-02-13 15:28:42 +01001569 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001570 return EXIT_FAILURE;
1571 } else if (!isspace(c[i + r])) {
1572 /* feature name starting with the not/and/or */
1573 last_not = 0;
1574 f_size++;
1575 } else if (c[i] == 'n') { /* not operation */
1576 if (last_not) {
1577 /* double not */
1578 expr_size = expr_size - 2;
1579 last_not = 0;
1580 } else {
1581 last_not = 1;
1582 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001583 } else { /* and, or */
1584 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001585 /* not a not operation */
1586 last_not = 0;
1587 }
1588 i += r;
1589 } else {
1590 f_size++;
1591 last_not = 0;
1592 }
1593 expr_size++;
1594
1595 while (!isspace(c[i])) {
1596 if (!c[i] || c[i] == ')') {
1597 i--;
1598 break;
1599 }
1600 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001601 }
1602 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001603 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001604 /* not matching count of ( and ) */
Michal Vasko53b7da02018-02-13 15:28:42 +01001605 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001606 return EXIT_FAILURE;
1607 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001608
Radek Krejci69b8d922016-07-27 13:13:41 +02001609 if (checkversion || expr_size > 1) {
1610 /* check that we have 1.1 module */
Radek Krejci13fde922018-05-16 10:45:58 +02001611 if (node->module->version != LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001612 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1613 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 +02001614 return EXIT_FAILURE;
1615 }
1616 }
1617
Radek Krejci9ff0a922016-07-14 13:08:05 +02001618 /* allocate the memory */
1619 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001620 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001621 stack.stack = malloc(expr_size * sizeof *stack.stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001622 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001623 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001624 f_size--; expr_size--; /* used as indexes from now */
1625
1626 for (i--; i >= 0; i--) {
1627 if (c[i] == ')') {
1628 /* push it on stack */
1629 iff_stack_push(&stack, LYS_IFF_RP);
1630 continue;
1631 } else if (c[i] == '(') {
1632 /* pop from the stack into result all operators until ) */
1633 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1634 iff_setop(iffeat_expr->expr, op, expr_size--);
1635 }
1636 continue;
1637 } else if (isspace(c[i])) {
1638 continue;
1639 }
1640
1641 /* end operator or operand -> find beginning and get what is it */
1642 j = i + 1;
1643 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1644 i--;
1645 }
1646 i++; /* get back by one step */
1647
Michal Vasko88011352018-07-12 08:49:07 +02001648 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001649 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1650 /* double not */
1651 iff_stack_pop(&stack);
1652 } else {
1653 /* not has the highest priority, so do not pop from the stack
1654 * as in case of AND and OR */
1655 iff_stack_push(&stack, LYS_IFF_NOT);
1656 }
Michal Vasko88011352018-07-12 08:49:07 +02001657 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001658 /* as for OR - pop from the stack all operators with the same or higher
1659 * priority and store them to the result, then push the AND to the stack */
1660 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1661 op = iff_stack_pop(&stack);
1662 iff_setop(iffeat_expr->expr, op, expr_size--);
1663 }
1664 iff_stack_push(&stack, LYS_IFF_AND);
Michal Vasko88011352018-07-12 08:49:07 +02001665 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001666 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1667 op = iff_stack_pop(&stack);
1668 iff_setop(iffeat_expr->expr, op, expr_size--);
1669 }
1670 iff_stack_push(&stack, LYS_IFF_OR);
1671 } else {
1672 /* feature name, length is j - i */
1673
1674 /* add it to the result */
1675 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1676
1677 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001678 * forward referenced, we have to keep the feature name in auxiliary
1679 * structure passed into unres */
1680 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001681 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001682 iff_data->node = node;
1683 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001684 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001685 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1686 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001687 f_size--;
1688
1689 if (r == -1) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01001690 lydict_remove(node->module->ctx, iff_data->fname);
Pavol Vican4d084512016-09-29 16:38:12 +02001691 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001692 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001693 }
1694 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001695 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001696 while (stack.index) {
1697 op = iff_stack_pop(&stack);
1698 iff_setop(iffeat_expr->expr, op, expr_size--);
1699 }
1700
1701 if (++expr_size || ++f_size) {
1702 /* not all expected operators and operands found */
Michal Vasko53b7da02018-02-13 15:28:42 +01001703 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001704 rc = EXIT_FAILURE;
1705 } else {
1706 rc = EXIT_SUCCESS;
1707 }
1708
1709error:
1710 /* cleanup */
1711 iff_stack_clean(&stack);
1712
1713 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001714}
1715
1716/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001717 * @brief Resolve (find) a data node based on a schema-nodeid.
1718 *
1719 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1720 * module).
1721 *
1722 */
1723struct lyd_node *
1724resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1725{
1726 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001727 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001728 const struct lys_node *schema = NULL;
1729
1730 assert(nodeid && start);
1731
1732 if (nodeid[0] == '/') {
1733 return NULL;
1734 }
1735
1736 str = p = strdup(nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01001737 LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001738
Michal Vasko3edeaf72016-02-11 13:17:43 +01001739 while (p) {
1740 token = p;
1741 p = strchr(p, '/');
1742 if (p) {
1743 *p = '\0';
1744 p++;
1745 }
1746
Radek Krejci5da4eb62016-04-08 14:45:51 +02001747 if (p) {
1748 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001749 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001750 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001751 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001752 result = NULL;
1753 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001754 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001755
Radek Krejci5da4eb62016-04-08 14:45:51 +02001756 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1757 continue;
1758 }
1759 } else {
1760 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001761 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001762 || !schema) {
1763 result = NULL;
1764 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001765 }
1766 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001767 LY_TREE_FOR(result ? result->child : start, iter) {
1768 if (iter->schema == schema) {
1769 /* move in data tree according to returned schema */
1770 result = iter;
1771 break;
1772 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001773 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001774 if (!iter) {
1775 /* instance not found */
1776 result = NULL;
1777 break;
1778 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001779 }
1780 free(str);
1781
1782 return result;
1783}
1784
Radek Krejci1a9c3612017-04-24 14:49:43 +02001785int
Michal Vasko50576712017-07-28 12:28:33 +02001786schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name,
1787 int mod_name_len, const char *name, int nam_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001788{
1789 const struct lys_module *prefix_mod;
1790
Michal Vaskocdb3f062018-02-01 09:55:06 +01001791 /* handle special names */
1792 if (name[0] == '*') {
1793 return 2;
1794 } else if (name[0] == '.') {
1795 return 3;
1796 }
1797
Michal Vasko50576712017-07-28 12:28:33 +02001798 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001799 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001800 return 1;
1801 }
1802
Radek Krejcibdf92362016-04-08 14:43:34 +02001803 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001804 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001805 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001806 if (!prefix_mod) {
1807 return -1;
1808 }
1809 } else {
1810 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001811 }
1812 if (prefix_mod != lys_node_module(sibling)) {
1813 return 1;
1814 }
1815
Michal Vasko50576712017-07-28 12:28:33 +02001816 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001817 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001818}
1819
Michal Vasko50576712017-07-28 12:28:33 +02001820/* keys do not have to be ordered and do not have to be all of them */
1821static int
1822resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node,
1823 const struct lys_module *cur_module, int *nodeid_end)
1824{
1825 int mod_len, nam_len, has_predicate, r, i;
1826 const char *model, *name;
1827 struct lys_node_list *list;
1828
1829 if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1830 return 1;
1831 }
1832
1833 list = (struct lys_node_list *)node;
1834 do {
1835 r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate);
1836 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001837 LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001838 return -1;
1839 }
1840 nodeid += r;
1841
1842 if (node->nodetype == LYS_LEAFLIST) {
1843 /* just check syntax */
1844 if (model || !name || (name[0] != '.') || has_predicate) {
1845 return 1;
1846 }
1847 break;
1848 } else {
1849 /* check the key */
1850 for (i = 0; i < list->keys_size; ++i) {
1851 if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) {
1852 continue;
1853 }
1854 if (model) {
1855 if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len)
1856 || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) {
1857 continue;
1858 }
1859 } else {
1860 if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) {
1861 continue;
1862 }
1863 }
1864
1865 /* match */
1866 break;
1867 }
1868
1869 if (i == list->keys_size) {
1870 return 1;
1871 }
1872 }
1873 } while (has_predicate);
1874
1875 if (!nodeid[0]) {
1876 *nodeid_end = 1;
1877 }
1878 return 0;
1879}
1880
Michal Vasko97234262018-02-01 09:53:01 +01001881/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001882 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001883int
Michal Vasko97234262018-02-01 09:53:01 +01001884resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001885 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001886{
PavolVicanb28bbff2018-02-21 00:44:02 +01001887 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vasko97234262018-02-01 09:53:01 +01001888 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001889 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001890 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
PavolVicanb28bbff2018-02-21 00:44:02 +01001891 int yang_data_name_len, backup_mod_name_len = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001892 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001893 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001894 char *str;
Michal Vasko53b7da02018-02-13 15:28:42 +01001895 struct ly_ctx *ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001896
Michal Vasko97234262018-02-01 09:53:01 +01001897 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001898 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001899
Michal Vasko50576712017-07-28 12:28:33 +02001900 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001901 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001902 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001903 ctx = cur_module->ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001904 id = nodeid;
1905
PavolVican195cf392018-02-23 13:24:45 +01001906 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1);
PavolVicanb28bbff2018-02-21 00:44:02 +01001907 if (r < 1) {
1908 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
1909 return -1;
1910 }
1911
1912 if (name[0] == '#') {
1913 if (is_relative) {
1914 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
1915 return -1;
1916 }
1917 yang_data_name = name + 1;
1918 yang_data_name_len = nam_len - 1;
1919 backup_mod_name = mod_name;
1920 backup_mod_name_len = mod_name_len;
1921 id += r;
1922 } else {
1923 is_relative = -1;
1924 }
1925
Michal Vasko50576712017-07-28 12:28:33 +02001926 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1927 (extended ? &all_desc : NULL), extended);
1928 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001929 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001930 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001931 }
1932 id += r;
1933
PavolVicanb28bbff2018-02-21 00:44:02 +01001934 if (backup_mod_name) {
1935 mod_name = backup_mod_name;
1936 mod_name_len = backup_mod_name_len;
1937 }
1938
Michal Vasko97234262018-02-01 09:53:01 +01001939 if (is_relative && !start_parent) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001940 LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths.");
Michal Vasko3edeaf72016-02-11 13:17:43 +01001941 return -1;
1942 }
1943
1944 /* descendant-schema-nodeid */
1945 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001946 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001947
Michal Vasko3edeaf72016-02-11 13:17:43 +01001948 /* absolute-schema-nodeid */
1949 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001950 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001951 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001952 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001953 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001954 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001955 return -1;
1956 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001957 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01001958 if (yang_data_name) {
1959 start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len);
1960 if (!start_parent) {
1961 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
1962 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
1963 free(str);
1964 return -1;
1965 }
1966 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001967 }
1968
1969 while (1) {
1970 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001971 last_aug = NULL;
1972
1973 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001974 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1975 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001976 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001977 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001978 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001979 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001980 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001981 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001982 return -1;
1983 }
1984 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001985 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001986 * because this module may be not implemented and it augments something in another module and
1987 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001988 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001989 }
1990
Michal Vasko3b2ad462018-07-10 15:40:53 +02001991 /* look into augments */
1992 if (!extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001993get_next_augment:
1994 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1995 }
1996 }
1997
1998 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vaskocb45f472018-02-12 10:47:42 +01001999 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002000 r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len);
2001
2002 /* resolve predicate */
2003 if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) {
2004 r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end);
2005 if (r == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002006 continue;
Michal Vasko50576712017-07-28 12:28:33 +02002007 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002008 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002009 }
Michal Vasko50576712017-07-28 12:28:33 +02002010 } else if (!id[0]) {
2011 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002012 }
Michal Vasko50576712017-07-28 12:28:33 +02002013
2014 if (r == 0) {
2015 /* one matching result */
2016 if (nodeid_end) {
2017 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002018 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002019 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2020 } else {
2021 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
2022 return -1;
2023 }
2024 start_parent = sibling;
2025 }
2026 break;
2027 } else if (r == 1) {
2028 continue;
2029 } else if (r == 2) {
2030 /* "*" */
2031 if (!*ret) {
2032 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002033 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002034 }
2035 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2036 if (all_desc) {
2037 LY_TREE_DFS_BEGIN(sibling, next, elem) {
2038 if (elem != sibling) {
2039 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
2040 }
2041
2042 LY_TREE_DFS_END(sibling, next, elem);
2043 }
2044 }
2045 } else if (r == 3) {
2046 /* "." */
2047 if (!*ret) {
2048 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002049 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002050 ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST);
2051 }
2052 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2053 if (all_desc) {
2054 LY_TREE_DFS_BEGIN(sibling, next, elem) {
2055 if (elem != sibling) {
2056 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
2057 }
2058
2059 LY_TREE_DFS_END(sibling, next, elem);
2060 }
2061 }
2062 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002063 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002064 return -1;
2065 }
2066 }
2067
2068 /* skip predicate */
2069 if (extended && has_predicate) {
2070 while (id[0] == '[') {
2071 id = strchr(id, ']');
2072 if (!id) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002073 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002074 return -1;
2075 }
2076 ++id;
2077 }
2078 }
2079
2080 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
2081 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002082 }
2083
2084 /* no match */
2085 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02002086 if (last_aug) {
2087 /* it still could be in another augment */
2088 goto get_next_augment;
2089 }
Michal Vasko50576712017-07-28 12:28:33 +02002090 if (no_node_error) {
2091 str = strndup(nodeid, (name - nodeid) + nam_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01002092 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02002093 free(str);
2094 return -1;
2095 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01002096 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002097 return EXIT_SUCCESS;
2098 }
2099
Michal Vasko50576712017-07-28 12:28:33 +02002100 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
2101 (extended ? &all_desc : NULL), extended);
2102 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002103 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02002104 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002105 }
2106 id += r;
2107 }
2108
2109 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002110 LOGINT(ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002111 return -1;
2112}
2113
Radek Krejcif3c71de2016-04-11 12:45:46 +02002114/* unique, refine,
2115 * >0 - unexpected char on position (ret - 1),
2116 * 0 - ok (but ret can still be NULL),
2117 * -1 - error,
2118 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01002119int
2120resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02002121 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002122{
2123 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002124 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002125 int r, nam_len, mod_name_len, is_relative = -1;
2126 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02002127 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002128
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002129 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01002130 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01002131
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002132 if (!start) {
2133 /* leaf not found */
2134 return 0;
2135 }
2136
Michal Vasko3edeaf72016-02-11 13:17:43 +01002137 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02002138 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002139
Michal Vasko50576712017-07-28 12:28:33 +02002140 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 +01002141 return ((id - nodeid) - r) + 1;
2142 }
2143 id += r;
2144
2145 if (!is_relative) {
2146 return -1;
2147 }
2148
Michal Vasko24476fa2017-03-08 12:33:48 +01002149 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02002150 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01002151 start_parent = lys_parent(start_parent);
2152 }
2153
Michal Vasko3edeaf72016-02-11 13:17:43 +01002154 while (1) {
2155 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002156 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vaskocb45f472018-02-12 10:47:42 +01002157 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002158 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2159 if (r == 0) {
2160 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002161 if (!(sibling->nodetype & ret_nodetype)) {
2162 /* wrong node type, too bad */
2163 continue;
2164 }
2165 *ret = sibling;
2166 return EXIT_SUCCESS;
2167 }
Michal Vasko50576712017-07-28 12:28:33 +02002168 start_parent = sibling;
2169 break;
2170 } else if (r == 1) {
2171 continue;
2172 } else {
2173 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002174 }
2175 }
2176
2177 /* no match */
2178 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002179 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002180 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02002181 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
2182 *ret = NULL;
2183 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002184 }
2185
Michal Vasko50576712017-07-28 12:28:33 +02002186 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 +01002187 return ((id - nodeid) - r) + 1;
2188 }
2189 id += r;
2190 }
2191
2192 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002193 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002194 return -1;
2195}
2196
2197/* choice default */
2198int
2199resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
2200{
2201 /* cannot actually be a path */
2202 if (strchr(nodeid, '/')) {
2203 return -1;
2204 }
2205
Michal Vaskodc300b02017-04-07 14:09:20 +02002206 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002207}
2208
2209/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2210static int
2211resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
2212{
2213 const struct lys_module *module;
2214 const char *mod_prefix, *name;
2215 int i, mod_prefix_len, nam_len;
2216
2217 /* parse the identifier, it must be parsed on one call */
Michal Vasko50576712017-07-28 12:28:33 +02002218 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 +01002219 return -i + 1;
2220 }
2221
Michal Vasko921eb6b2017-10-13 10:01:39 +02002222 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002223 if (!module) {
2224 return -1;
2225 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002226 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002227 start = module->data;
2228 }
2229
2230 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2231
2232 return EXIT_SUCCESS;
2233}
2234
2235int
2236resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
2237 const struct lys_node **ret)
2238{
2239 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002240 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002241 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002242 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002243
2244 assert(nodeid && module && ret);
2245 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
2246
2247 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01002248 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002249
Michal Vasko50576712017-07-28 12:28:33 +02002250 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 +01002251 return ((id - nodeid) - r) + 1;
2252 }
2253 id += r;
2254
2255 if (is_relative) {
2256 return -1;
2257 }
2258
Michal Vasko921eb6b2017-10-13 10:01:39 +02002259 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002260 if (!abs_start_mod) {
2261 return -1;
2262 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002263
2264 while (1) {
2265 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002266 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vaskocb45f472018-02-12 10:47:42 +01002267 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002268 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2269 if (r == 0) {
2270 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002271 if (!(sibling->nodetype & ret_nodetype)) {
2272 /* wrong node type, too bad */
2273 continue;
2274 }
2275 *ret = sibling;
2276 return EXIT_SUCCESS;
2277 }
Michal Vasko50576712017-07-28 12:28:33 +02002278 start_parent = sibling;
2279 break;
2280 } else if (r == 1) {
2281 continue;
2282 } else {
2283 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002284 }
2285 }
2286
2287 /* no match */
2288 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002289 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002290 return EXIT_SUCCESS;
2291 }
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, NULL, NULL, 0)) < 1) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002294 return ((id - nodeid) - r) + 1;
2295 }
2296 id += r;
2297 }
2298
2299 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002300 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002301 return -1;
2302}
2303
Michal Vaskoe733d682016-03-14 09:08:27 +01002304static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002305resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002306{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002307 const char *mod_name, *name;
2308 int mod_name_len, nam_len, has_predicate, i;
2309 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002310
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002311 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 +02002312 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002313 LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002314 return -1;
2315 }
2316
2317 predicate += i;
2318 *parsed += i;
2319
Michal Vasko58c2aab2017-01-05 10:02:05 +01002320 if (!isdigit(name[0])) {
2321 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002322 key = (struct lys_node *)list->keys[i];
2323 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002324 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002325 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002326 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002327
Michal Vasko58c2aab2017-01-05 10:02:05 +01002328 if (i == list->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002329 LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko58c2aab2017-01-05 10:02:05 +01002330 return -1;
2331 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002332 }
2333
2334 /* more predicates? */
2335 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002336 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002337 }
2338
2339 return 0;
2340}
2341
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002342/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002343const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002344resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002345{
Michal Vasko53b7da02018-02-13 15:28:42 +01002346 char *str;
PavolVicanb28bbff2018-02-21 00:44:02 +01002347 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vaskob3744402017-08-03 14:23:58 +02002348 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002349 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
PavolVicanb28bbff2018-02-21 00:44:02 +01002350 int yang_data_name_len, backup_mod_name_len;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002351 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002352 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002353
Michal Vasko3547c532016-03-14 09:40:50 +01002354 assert(nodeid && (ctx || start));
2355 if (!ctx) {
2356 ctx = start->module->ctx;
2357 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002358
2359 id = nodeid;
2360
PavolVican195cf392018-02-23 13:24:45 +01002361 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) {
PavolVicanb28bbff2018-02-21 00:44:02 +01002362 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2363 return NULL;
2364 }
2365
2366 if (name[0] == '#') {
2367 if (is_relative) {
2368 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
2369 return NULL;
2370 }
2371 yang_data_name = name + 1;
2372 yang_data_name_len = nam_len - 1;
2373 backup_mod_name = mod_name;
2374 backup_mod_name_len = mod_name_len;
2375 id += r;
2376 } else {
2377 is_relative = -1;
2378 }
2379
Michal Vasko50576712017-07-28 12:28:33 +02002380 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 +01002381 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002382 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002383 }
2384 id += r;
2385
PavolVicanb28bbff2018-02-21 00:44:02 +01002386 if (backup_mod_name) {
2387 mod_name = backup_mod_name;
2388 mod_name_len = backup_mod_name_len;
2389 }
2390
Michal Vasko3edeaf72016-02-11 13:17:43 +01002391 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002392 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002393 start_parent = start;
2394 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2395 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002396 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002397 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002398 } else {
2399 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002400 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002401 LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
Michal Vasko10728b52016-04-07 14:26:29 +02002402 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002403 return NULL;
2404 }
2405
Michal Vasko53b7da02018-02-13 15:28:42 +01002406 str = strndup(mod_name, mod_name_len);
2407 module = ly_ctx_get_module(ctx, str, NULL, 1);
2408 free(str);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002409
Michal Vaskof68a49e2017-08-14 13:23:37 +02002410 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002411 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002412 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002413 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002414 return NULL;
2415 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002416 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01002417 if (yang_data_name) {
2418 start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len);
2419 if (!start_parent) {
2420 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
2421 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
2422 free(str);
2423 return NULL;
2424 }
2425 }
Michal Vasko3547c532016-03-14 09:40:50 +01002426
2427 /* now it's as if there was no module name */
2428 mod_name = NULL;
2429 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002430 }
2431
Michal Vaskof68a49e2017-08-14 13:23:37 +02002432 prev_mod = module;
2433
Michal Vasko3edeaf72016-02-11 13:17:43 +01002434 while (1) {
2435 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002436 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002437 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002438 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002439 /* output check */
2440 for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2441 if (parent) {
2442 if (output && (parent->nodetype == LYS_INPUT)) {
2443 continue;
2444 } else if (!output && (parent->nodetype == LYS_OUTPUT)) {
2445 continue;
2446 }
2447 }
2448
Michal Vasko3edeaf72016-02-11 13:17:43 +01002449 /* module check */
2450 if (mod_name) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002451 /* will also find an augment module */
Michal Vasko53b7da02018-02-13 15:28:42 +01002452 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002453
Michal Vasko3edeaf72016-02-11 13:17:43 +01002454 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002455 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002456 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002457 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002458 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002459 }
2460 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002461 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002462 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002463 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002464 continue;
2465 }
2466
Michal Vaskoe733d682016-03-14 09:08:27 +01002467 /* do we have some predicates on it? */
2468 if (has_predicate) {
2469 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002470 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002471 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002472 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002473 return NULL;
2474 }
2475 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002476 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002477 return NULL;
2478 }
2479 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002480 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002481 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002482 }
2483 id += r;
2484 }
2485
Michal Vasko3edeaf72016-02-11 13:17:43 +01002486 /* the result node? */
2487 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002488 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002489 }
2490
Michal Vaskodc300b02017-04-07 14:09:20 +02002491 /* move down the tree, if possible */
2492 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002493 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskodc300b02017-04-07 14:09:20 +02002494 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002495 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002496 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002497
2498 /* update prev mod */
2499 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002500 break;
2501 }
2502 }
2503
2504 /* no match */
2505 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002506 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002507 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002508 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002509 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002510 }
2511
Michal Vasko50576712017-07-28 12:28:33 +02002512 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 +01002513 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002514 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002515 }
2516 id += r;
2517 }
2518
2519 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002520 LOGINT(ctx);
Michal Vaskoe733d682016-03-14 09:08:27 +01002521 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002522}
2523
Michal Vasko22448d32016-03-16 13:17:29 +01002524static int
Michal Vasko2d44ee02018-05-18 09:38:51 +02002525resolve_partial_json_data_list_predicate(struct parsed_pred pp, struct lyd_node *node, int position)
Michal Vasko22448d32016-03-16 13:17:29 +01002526{
Michal Vasko22448d32016-03-16 13:17:29 +01002527 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002528 struct lyd_node_leaf_list *key;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002529 struct lys_node_list *slist;
Michal Vasko53b7da02018-02-13 15:28:42 +01002530 struct ly_ctx *ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002531
Radek Krejci61a86c62016-03-24 11:06:44 +01002532 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002533 assert(node->schema->nodetype == LYS_LIST);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002534 assert(pp.len);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002535
Michal Vasko53b7da02018-02-13 15:28:42 +01002536 ctx = node->schema->module->ctx;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002537 slist = (struct lys_node_list *)node->schema;
Michal Vasko22448d32016-03-16 13:17:29 +01002538
Michal Vasko53adfc72017-01-06 10:39:10 +01002539 /* is the predicate a number? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002540 if (isdigit(pp.pred[0].name[0])) {
2541 if (position == atoi(pp.pred[0].name)) {
Michal Vasko53adfc72017-01-06 10:39:10 +01002542 /* match */
Michal Vasko53adfc72017-01-06 10:39:10 +01002543 return 0;
2544 } else {
2545 /* not a match */
2546 return 1;
2547 }
2548 }
2549
Michal Vaskof29903d2016-04-18 13:13:10 +02002550 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002551 if (!key) {
2552 /* it is not a position, so we need a key for it to be a match */
2553 return 1;
2554 }
2555
2556 /* go through all the keys */
Michal Vaskofebd13d2018-05-17 10:42:24 +02002557 for (i = 0; i < slist->keys_size; ++i) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002558 if (strncmp(key->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || key->schema->name[pp.pred[i].nam_len]) {
2559 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002560 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002561 }
2562
Michal Vasko2d44ee02018-05-18 09:38:51 +02002563 if (pp.pred[i].mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002564 /* specific module, check that the found key is from that module */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002565 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len)
2566 || lyd_node_module((struct lyd_node *)key)->name[pp.pred[i].mod_name_len]) {
2567 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002568 return -1;
2569 }
Michal Vasko50576712017-07-28 12:28:33 +02002570
2571 /* but if the module is the same as the parent, it should have been omitted */
2572 if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002573 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko50576712017-07-28 12:28:33 +02002574 return -1;
2575 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002576 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002577 /* no module, so it must be the same as the list (parent) */
2578 if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002579 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002580 return -1;
2581 }
2582 }
2583
Michal Vasko22448d32016-03-16 13:17:29 +01002584 /* value does not match */
Michal Vasko310bc582018-05-22 10:47:59 +02002585 if (strncmp(key->value_str, pp.pred[i].value, pp.pred[i].val_len) || key->value_str[pp.pred[i].val_len]) {
Michal Vasko22448d32016-03-16 13:17:29 +01002586 return 1;
2587 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002588
2589 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002590 }
2591
Michal Vasko22448d32016-03-16 13:17:29 +01002592 return 0;
2593}
2594
Radek Krejci45826012016-08-24 15:07:57 +02002595/**
2596 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2597 *
2598 * @param[in] nodeid Node data path to find
2599 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2600 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2601 * @param[out] parsed Number of characters processed in \p id
2602 * @return The closes parent (or the node itself) from the path
2603 */
Michal Vasko22448d32016-03-16 13:17:29 +01002604struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002605resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2606 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002607{
Michal Vasko2d44ee02018-05-18 09:38:51 +02002608 const char *id, *mod_name, *name, *data_val, *llval;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002609 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002610 int has_predicate, last_parsed = 0, llval_len;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002611 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002612 struct lyd_node_leaf_list *llist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002613 const struct lys_module *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002614 struct ly_ctx *ctx;
Michal Vasko20964782018-08-01 15:14:30 +02002615 const struct lys_node *ssibling, *sparent;
Michal Vasko310bc582018-05-22 10:47:59 +02002616 struct lys_node_list *slist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002617 struct parsed_pred pp;
Michal Vasko22448d32016-03-16 13:17:29 +01002618
2619 assert(nodeid && start && parsed);
2620
Michal Vasko2d44ee02018-05-18 09:38:51 +02002621 memset(&pp, 0, sizeof pp);
Michal Vasko22448d32016-03-16 13:17:29 +01002622 ctx = start->schema->module->ctx;
2623 id = nodeid;
2624
Michal Vasko2d44ee02018-05-18 09:38:51 +02002625 /* parse first nodeid in case it is yang-data extension */
PavolVican195cf392018-02-23 13:24:45 +01002626 if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1)) < 1) {
PavolVicanb28bbff2018-02-21 00:44:02 +01002627 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002628 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002629 }
2630
2631 if (name[0] == '#') {
2632 if (is_relative) {
2633 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002634 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002635 }
PavolVicanb28bbff2018-02-21 00:44:02 +01002636 id += r;
2637 last_parsed = r;
2638 } else {
2639 is_relative = -1;
2640 }
2641
Michal Vasko2d44ee02018-05-18 09:38:51 +02002642 /* parse first nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002643 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 +01002644 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002645 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002646 }
2647 id += r;
2648 /* add it to parsed only after the data node was actually found */
PavolVicanb28bbff2018-02-21 00:44:02 +01002649 last_parsed += r;
Michal Vasko22448d32016-03-16 13:17:29 +01002650
2651 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002652 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002653 start = start->child;
2654 } else {
2655 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002656 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002657 }
2658
Michal Vaskofebd13d2018-05-17 10:42:24 +02002659 /* do not duplicate code, use predicate parsing from the loop */
2660 goto parse_predicates;
2661
Michal Vasko22448d32016-03-16 13:17:29 +01002662 while (1) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002663 /* find the correct schema node first */
2664 ssibling = NULL;
Michal Vasko20964782018-08-01 15:14:30 +02002665 sparent = (start && start->parent) ? start->parent->schema : NULL;
2666 while ((ssibling = lys_getnext(ssibling, sparent, prev_mod, 0))) {
2667 /* skip invalid input/output nodes */
2668 if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) {
2669 if (options & LYD_PATH_OPT_OUTPUT) {
2670 if (lys_parent(ssibling)->nodetype == LYS_INPUT) {
2671 continue;
2672 }
2673 } else {
2674 if (lys_parent(ssibling)->nodetype == LYS_OUTPUT) {
2675 continue;
2676 }
2677 }
2678 }
2679
Michal Vasko2d44ee02018-05-18 09:38:51 +02002680 if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) {
2681 break;
Michal Vasko2411b942016-03-23 13:50:03 +01002682 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002683 }
2684 if (!ssibling) {
2685 /* there is not even such a schema node */
2686 free(pp.pred);
2687 return last_match;
2688 }
2689 pp.schema = ssibling;
Michal Vasko2411b942016-03-23 13:50:03 +01002690
Michal Vasko2d44ee02018-05-18 09:38:51 +02002691 /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if
2692 * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */
2693 if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) {
2694 pp.len = 1;
2695 pp.pred = calloc(1, sizeof *pp.pred);
2696 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
Michal Vasko22448d32016-03-16 13:17:29 +01002697
Michal Vasko2d44ee02018-05-18 09:38:51 +02002698 pp.pred[0].name = ".";
2699 pp.pred[0].nam_len = 1;
2700 pp.pred[0].value = (llist_value ? llist_value : "");
2701 pp.pred[0].val_len = strlen(pp.pred[0].value);
2702 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002703
Michal Vasko310bc582018-05-22 10:47:59 +02002704 if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) {
2705 /* check leaf/leaf-list predicate */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002706 if (pp.len > 1) {
2707 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2708 goto error;
Michal Vasko310bc582018-05-22 10:47:59 +02002709 } else if (pp.len) {
2710 if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) {
2711 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name);
2712 goto error;
2713 }
2714 if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) {
2715 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value);
2716 goto error;
2717 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002718 }
2719 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko310bc582018-05-22 10:47:59 +02002720 /* list should have predicates for all the keys or position */
2721 slist = (struct lys_node_list *)ssibling;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002722 if (!pp.len) {
2723 /* none match */
2724 return last_match;
Michal Vasko310bc582018-05-22 10:47:59 +02002725 } else if (!isdigit(pp.pred[0].name[0])) {
2726 /* list predicate is not a position, so there must be all the keys */
2727 if (pp.len > slist->keys_size) {
2728 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2729 goto error;
2730 } else if (pp.len < slist->keys_size) {
2731 LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name);
2732 goto error;
2733 }
2734 /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */
2735 for (r = 0; r < pp.len; ++r) {
2736 if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) {
2737 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value);
2738 goto error;
2739 }
2740 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002741 }
2742 } else if (pp.pred) {
2743 /* no other nodes allow predicates */
2744 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2745 goto error;
2746 }
2747
2748#ifdef LY_ENABLED_CACHE
2749 /* we will not be matching keyless lists or state leaf-lists this way */
2750 if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size)
2751 && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) {
2752 sibling = resolve_json_data_node_hash(start->parent, pp);
2753 } else
2754#endif
2755 {
2756 list_instance_position = 0;
2757 LY_TREE_FOR(start, sibling) {
2758 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
2759 if (lys_parent(sibling->schema)) {
2760 if (options & LYD_PATH_OPT_OUTPUT) {
2761 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
2762 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
2763 goto error;
2764 }
2765 } else {
2766 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
2767 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
2768 goto error;
2769 }
Michal Vasko22448d32016-03-16 13:17:29 +01002770 }
Michal Vasko22448d32016-03-16 13:17:29 +01002771 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002772
2773 if (sibling->schema != ssibling) {
2774 /* wrong schema node */
Michal Vasko22448d32016-03-16 13:17:29 +01002775 continue;
2776 }
2777
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002778 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002779 if (ssibling->nodetype == LYS_LEAFLIST) {
2780 if (ssibling->flags & LYS_CONFIG_R) {
Michal Vasko24affa02018-04-03 09:06:06 +02002781 /* state leaf-lists will never match */
2782 continue;
2783 }
2784
Michal Vasko9ba34de2016-12-07 12:21:19 +01002785 llist = (struct lyd_node_leaf_list *)sibling;
2786
Michal Vasko2d44ee02018-05-18 09:38:51 +02002787 /* get the expected leaf-list value */
2788 llval = NULL;
2789 llval_len = 0;
2790 if (pp.pred) {
2791 /* it was already checked that it is correct */
2792 llval = pp.pred[0].value;
2793 llval_len = pp.pred[0].val_len;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002794
Michal Vaskof0a50972016-10-19 11:33:55 +02002795 }
2796
Michal Vasko21b90ce2017-09-19 09:38:27 +02002797 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002798 if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002799 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2800 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002801 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2802 } else {
2803 data_val = llist->value_str;
2804 }
2805
Michal Vasko2d44ee02018-05-18 09:38:51 +02002806 if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len)
2807 || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002808 continue;
2809 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002810
Michal Vasko2d44ee02018-05-18 09:38:51 +02002811 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002812 /* list, we likely need predicates'n'stuff then, but if without a predicate, we are always creating it */
Michal Vasko58c2aab2017-01-05 10:02:05 +01002813 ++list_instance_position;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002814 ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position);
Michal Vasko22448d32016-03-16 13:17:29 +01002815 if (ret == -1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002816 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002817 } else if (ret == 1) {
2818 /* this list instance does not match */
2819 continue;
2820 }
Michal Vasko22448d32016-03-16 13:17:29 +01002821 }
2822
Michal Vasko22448d32016-03-16 13:17:29 +01002823 break;
2824 }
2825 }
2826
2827 /* no match, return last match */
2828 if (!sibling) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002829 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002830 return last_match;
2831 }
2832
Michal Vasko2d44ee02018-05-18 09:38:51 +02002833 /* we found a next matching node */
2834 *parsed += last_parsed;
Michal Vasko586831d2018-05-22 11:13:16 +02002835 last_match = sibling;
2836 prev_mod = lyd_node_module(sibling);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002837
2838 /* the result node? */
2839 if (!id[0]) {
2840 free(pp.pred);
Michal Vasko586831d2018-05-22 11:13:16 +02002841 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002842 }
2843
Michal Vasko586831d2018-05-22 11:13:16 +02002844 /* move down the tree, if possible, and continue */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002845 if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko586831d2018-05-22 11:13:16 +02002846 /* there can be no children even through expected, error */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002847 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2848 goto error;
Michal Vasko586831d2018-05-22 11:13:16 +02002849 } else if (!sibling->child) {
2850 /* there could be some children, but are not, return what we found so far */
2851 free(pp.pred);
2852 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002853 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002854 start = sibling->child;
2855
Michal Vaskofebd13d2018-05-17 10:42:24 +02002856 /* parse nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002857 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 +01002858 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002859 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002860 }
2861 id += r;
2862 last_parsed = r;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002863
2864parse_predicates:
2865 /* parse all the predicates */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002866 free(pp.pred);
2867 pp.schema = NULL;
2868 pp.len = 0;
2869 pp.pred = NULL;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002870 while (has_predicate) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002871 ++pp.len;
2872 pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred);
2873 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
2874 if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len,
2875 &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value,
2876 &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002877 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2878 goto error;
2879 }
2880
2881 id += r;
2882 last_parsed += r;
2883 }
Michal Vasko22448d32016-03-16 13:17:29 +01002884 }
2885
Michal Vaskofebd13d2018-05-17 10:42:24 +02002886error:
Michal Vasko238bd2f2016-03-23 09:39:01 +01002887 *parsed = -1;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002888 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002889 return NULL;
2890}
2891
Michal Vasko3edeaf72016-02-11 13:17:43 +01002892/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002893 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002894 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002895 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002896 * @param[in] ctx Context for errors.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002897 * @param[in] str_restr Restriction as a string.
2898 * @param[in] type Type of the restriction.
2899 * @param[out] ret Final interval structure that starts with
2900 * the interval of the initial type, continues with intervals
2901 * of any superior types derived from the initial one, and
2902 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002903 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002904 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002905 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002906int
Michal Vasko53b7da02018-02-13 15:28:42 +01002907resolve_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 +02002908{
2909 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002910 int kind;
Michal Vaskof75b2772018-03-14 09:55:33 +01002911 int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax;
2912 uint64_t local_umin, local_umax = 0;
2913 uint8_t local_fdig = 0;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002914 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002915 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002916
2917 switch (type->base) {
2918 case LY_TYPE_BINARY:
2919 kind = 0;
2920 local_umin = 0;
2921 local_umax = 18446744073709551615UL;
2922
2923 if (!str_restr && type->info.binary.length) {
2924 str_restr = type->info.binary.length->expr;
2925 }
2926 break;
2927 case LY_TYPE_DEC64:
2928 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002929 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2930 local_fmax = __INT64_C(9223372036854775807);
2931 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002932
2933 if (!str_restr && type->info.dec64.range) {
2934 str_restr = type->info.dec64.range->expr;
2935 }
2936 break;
2937 case LY_TYPE_INT8:
2938 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002939 local_smin = __INT64_C(-128);
2940 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002941
2942 if (!str_restr && type->info.num.range) {
2943 str_restr = type->info.num.range->expr;
2944 }
2945 break;
2946 case LY_TYPE_INT16:
2947 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002948 local_smin = __INT64_C(-32768);
2949 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002950
2951 if (!str_restr && type->info.num.range) {
2952 str_restr = type->info.num.range->expr;
2953 }
2954 break;
2955 case LY_TYPE_INT32:
2956 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002957 local_smin = __INT64_C(-2147483648);
2958 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002959
2960 if (!str_restr && type->info.num.range) {
2961 str_restr = type->info.num.range->expr;
2962 }
2963 break;
2964 case LY_TYPE_INT64:
2965 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002966 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2967 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002968
2969 if (!str_restr && type->info.num.range) {
2970 str_restr = type->info.num.range->expr;
2971 }
2972 break;
2973 case LY_TYPE_UINT8:
2974 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002975 local_umin = __UINT64_C(0);
2976 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002977
2978 if (!str_restr && type->info.num.range) {
2979 str_restr = type->info.num.range->expr;
2980 }
2981 break;
2982 case LY_TYPE_UINT16:
2983 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002984 local_umin = __UINT64_C(0);
2985 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002986
2987 if (!str_restr && type->info.num.range) {
2988 str_restr = type->info.num.range->expr;
2989 }
2990 break;
2991 case LY_TYPE_UINT32:
2992 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002993 local_umin = __UINT64_C(0);
2994 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002995
2996 if (!str_restr && type->info.num.range) {
2997 str_restr = type->info.num.range->expr;
2998 }
2999 break;
3000 case LY_TYPE_UINT64:
3001 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01003002 local_umin = __UINT64_C(0);
3003 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003004
3005 if (!str_restr && type->info.num.range) {
3006 str_restr = type->info.num.range->expr;
3007 }
3008 break;
3009 case LY_TYPE_STRING:
3010 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01003011 local_umin = __UINT64_C(0);
3012 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003013
3014 if (!str_restr && type->info.str.length) {
3015 str_restr = type->info.str.length->expr;
3016 }
3017 break;
3018 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003019 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003020 }
3021
3022 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02003023 if (type->der) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003024 if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003025 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02003026 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003027 assert(!intv || (intv->kind == kind));
3028 }
3029
3030 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003031 /* we do not have any restriction, return superior ones */
3032 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003033 return EXIT_SUCCESS;
3034 }
3035
3036 /* adjust local min and max */
3037 if (intv) {
3038 tmp_intv = intv;
3039
3040 if (kind == 0) {
3041 local_umin = tmp_intv->value.uval.min;
3042 } else if (kind == 1) {
3043 local_smin = tmp_intv->value.sval.min;
3044 } else if (kind == 2) {
3045 local_fmin = tmp_intv->value.fval.min;
3046 }
3047
3048 while (tmp_intv->next) {
3049 tmp_intv = tmp_intv->next;
3050 }
3051
3052 if (kind == 0) {
3053 local_umax = tmp_intv->value.uval.max;
3054 } else if (kind == 1) {
3055 local_smax = tmp_intv->value.sval.max;
3056 } else if (kind == 2) {
3057 local_fmax = tmp_intv->value.fval.max;
3058 }
3059 }
3060
3061 /* finally parse our restriction */
3062 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003063 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003064 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003065 if (!tmp_local_intv) {
3066 assert(!local_intv);
3067 local_intv = malloc(sizeof *local_intv);
3068 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003069 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003070 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003071 tmp_local_intv = tmp_local_intv->next;
3072 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003073 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003074
3075 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003076 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003077 tmp_local_intv->next = NULL;
3078
3079 /* min */
3080 ptr = seg_ptr;
3081 while (isspace(ptr[0])) {
3082 ++ptr;
3083 }
3084 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3085 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003086 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003087 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003088 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003089 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003090 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003091 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003092 goto error;
3093 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003094 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003095 } else if (!strncmp(ptr, "min", 3)) {
3096 if (kind == 0) {
3097 tmp_local_intv->value.uval.min = local_umin;
3098 } else if (kind == 1) {
3099 tmp_local_intv->value.sval.min = local_smin;
3100 } else if (kind == 2) {
3101 tmp_local_intv->value.fval.min = local_fmin;
3102 }
3103
3104 ptr += 3;
3105 } else if (!strncmp(ptr, "max", 3)) {
3106 if (kind == 0) {
3107 tmp_local_intv->value.uval.min = local_umax;
3108 } else if (kind == 1) {
3109 tmp_local_intv->value.sval.min = local_smax;
3110 } else if (kind == 2) {
3111 tmp_local_intv->value.fval.min = local_fmax;
3112 }
3113
3114 ptr += 3;
3115 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003116 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003117 }
3118
3119 while (isspace(ptr[0])) {
3120 ptr++;
3121 }
3122
3123 /* no interval or interval */
3124 if ((ptr[0] == '|') || !ptr[0]) {
3125 if (kind == 0) {
3126 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
3127 } else if (kind == 1) {
3128 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
3129 } else if (kind == 2) {
3130 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
3131 }
3132 } else if (!strncmp(ptr, "..", 2)) {
3133 /* skip ".." */
3134 ptr += 2;
3135 while (isspace(ptr[0])) {
3136 ++ptr;
3137 }
3138
3139 /* max */
3140 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3141 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003142 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003143 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003144 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003145 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003146 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003147 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003148 goto error;
3149 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003150 }
3151 } else if (!strncmp(ptr, "max", 3)) {
3152 if (kind == 0) {
3153 tmp_local_intv->value.uval.max = local_umax;
3154 } else if (kind == 1) {
3155 tmp_local_intv->value.sval.max = local_smax;
3156 } else if (kind == 2) {
3157 tmp_local_intv->value.fval.max = local_fmax;
3158 }
3159 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003160 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003161 }
3162 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003163 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003164 }
3165
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003166 /* check min and max in correct order*/
3167 if (kind == 0) {
3168 /* current segment */
3169 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
3170 goto error;
3171 }
3172 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
3173 goto error;
3174 }
3175 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02003176 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003177 goto error;
3178 }
3179 } else if (kind == 1) {
3180 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
3181 goto error;
3182 }
3183 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
3184 goto error;
3185 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003186 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003187 goto error;
3188 }
3189 } else if (kind == 2) {
3190 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
3191 goto error;
3192 }
3193 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
3194 goto error;
3195 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003196 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003197 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003198 goto error;
3199 }
3200 }
3201
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003202 /* next segment (next OR) */
3203 seg_ptr = strchr(seg_ptr, '|');
3204 if (!seg_ptr) {
3205 break;
3206 }
3207 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003208 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003209 }
3210
3211 /* check local restrictions against superior ones */
3212 if (intv) {
3213 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003214 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003215
3216 while (tmp_local_intv && tmp_intv) {
3217 /* reuse local variables */
3218 if (kind == 0) {
3219 local_umin = tmp_local_intv->value.uval.min;
3220 local_umax = tmp_local_intv->value.uval.max;
3221
3222 /* it must be in this interval */
3223 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
3224 /* this interval is covered, next one */
3225 if (local_umax <= tmp_intv->value.uval.max) {
3226 tmp_local_intv = tmp_local_intv->next;
3227 continue;
3228 /* ascending order of restrictions -> fail */
3229 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003230 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003231 }
3232 }
3233 } else if (kind == 1) {
3234 local_smin = tmp_local_intv->value.sval.min;
3235 local_smax = tmp_local_intv->value.sval.max;
3236
3237 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
3238 if (local_smax <= tmp_intv->value.sval.max) {
3239 tmp_local_intv = tmp_local_intv->next;
3240 continue;
3241 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003242 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003243 }
3244 }
3245 } else if (kind == 2) {
3246 local_fmin = tmp_local_intv->value.fval.min;
3247 local_fmax = tmp_local_intv->value.fval.max;
3248
Michal Vasko4d1f0482016-09-19 14:35:06 +02003249 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02003250 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003251 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003252 tmp_local_intv = tmp_local_intv->next;
3253 continue;
3254 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003255 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003256 }
3257 }
3258 }
3259
3260 tmp_intv = tmp_intv->next;
3261 }
3262
3263 /* some interval left uncovered -> fail */
3264 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003265 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003266 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003267 }
3268
Michal Vaskoaeb51802016-04-11 10:58:47 +02003269 /* append the local intervals to all the intervals of the superior types, return it all */
3270 if (intv) {
3271 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
3272 tmp_intv->next = local_intv;
3273 } else {
3274 intv = local_intv;
3275 }
3276 *ret = intv;
3277
3278 return EXIT_SUCCESS;
3279
3280error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003281 while (intv) {
3282 tmp_intv = intv->next;
3283 free(intv);
3284 intv = tmp_intv;
3285 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003286 while (local_intv) {
3287 tmp_local_intv = local_intv->next;
3288 free(local_intv);
3289 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003290 }
3291
Michal Vaskoaeb51802016-04-11 10:58:47 +02003292 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003293}
3294
Michal Vasko730dfdf2015-08-11 14:48:05 +02003295/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003296 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3297 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003298 *
3299 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003300 * @param[in] mod_name Typedef name module name.
3301 * @param[in] module Main module.
3302 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003303 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003304 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003305 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003306 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003307int
Michal Vasko1e62a092015-12-01 12:27:20 +01003308resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3309 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003310{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003311 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003312 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003313 int tpdf_size;
3314
Michal Vasko1dca6882015-10-22 14:29:42 +02003315 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003316 /* no prefix, try built-in types */
3317 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003318 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003319 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003320 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003321 }
3322 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003323 }
3324 }
3325 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003326 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003327 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003328 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003329 }
3330 }
3331
Michal Vasko1dca6882015-10-22 14:29:42 +02003332 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003333 /* search in local typedefs */
3334 while (parent) {
3335 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003336 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003337 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3338 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003339 break;
3340
Radek Krejci76512572015-08-04 09:47:08 +02003341 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003342 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3343 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003344 break;
3345
Radek Krejci76512572015-08-04 09:47:08 +02003346 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003347 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3348 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003349 break;
3350
Radek Krejci76512572015-08-04 09:47:08 +02003351 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003352 case LYS_ACTION:
3353 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3354 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003355 break;
3356
Radek Krejci76512572015-08-04 09:47:08 +02003357 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003358 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3359 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003360 break;
3361
Radek Krejci76512572015-08-04 09:47:08 +02003362 case LYS_INPUT:
3363 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003364 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3365 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003366 break;
3367
3368 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003369 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003370 continue;
3371 }
3372
3373 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003374 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003375 match = &tpdf[i];
3376 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003377 }
3378 }
3379
Michal Vaskodcf98e62016-05-05 17:53:53 +02003380 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003381 }
Radek Krejcic071c542016-01-27 14:57:51 +01003382 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003383 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003384 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003385 if (!module) {
3386 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003387 }
3388 }
3389
3390 /* search in top level typedefs */
3391 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003392 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003393 match = &module->tpdf[i];
3394 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003395 }
3396 }
3397
3398 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003399 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003400 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003401 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 +02003402 match = &module->inc[i].submodule->tpdf[j];
3403 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003404 }
3405 }
3406 }
3407
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003408 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003409
3410check_leafref:
3411 if (ret) {
3412 *ret = match;
3413 }
3414 if (match->type.base == LY_TYPE_LEAFREF) {
3415 while (!match->type.info.lref.path) {
3416 match = match->type.der;
3417 assert(match);
3418 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003419 }
3420 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003421}
3422
Michal Vasko1dca6882015-10-22 14:29:42 +02003423/**
3424 * @brief Check the default \p value of the \p type. Logs directly.
3425 *
3426 * @param[in] type Type definition to use.
3427 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003428 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003429 *
3430 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3431 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003432static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003433check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003434{
Radek Krejcibad2f172016-08-02 11:04:15 +02003435 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003436 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003437 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003438 char *s;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003439 int ret = EXIT_SUCCESS, r;
Michal Vasko53b7da02018-02-13 15:28:42 +01003440 struct ly_ctx *ctx = module->ctx;
Michal Vasko1dca6882015-10-22 14:29:42 +02003441
Radek Krejci51673202016-11-01 17:00:32 +01003442 assert(value);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003443 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003444
Radek Krejcic13db382016-08-16 10:52:42 +02003445 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003446 /* the type was not resolved yet, nothing to do for now */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003447 ret = EXIT_FAILURE;
3448 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003449 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003450 /* do not check defaults in not implemented module's data */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003451 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003452 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003453 /* identityrefs are checked when instantiated in data instead of typedef,
3454 * but in typedef the value has to be modified to include the prefix */
3455 if (*value) {
3456 if (strchr(*value, ':')) {
3457 dflt = transform_schema2json(module, *value);
3458 } else {
3459 /* default prefix of the module where the typedef is defined */
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003460 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
3461 LOGMEM(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003462 ret = -1;
3463 goto cleanup;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003464 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003465 dflt = lydict_insert_zc(ctx, s);
Radek Krejci9e6af732017-04-27 14:40:25 +02003466 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003467 lydict_remove(ctx, *value);
Radek Krejci9e6af732017-04-27 14:40:25 +02003468 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003469 dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003470 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003471 goto cleanup;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003472 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3473 /* leafref in typedef cannot be checked */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003474 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003475 }
3476
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003477 dflt = lydict_insert(ctx, *value, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003478 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003479 /* we do not have a new default value, so is there any to check even, in some base type? */
3480 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3481 if (base_tpdf->dflt) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003482 dflt = lydict_insert(ctx, base_tpdf->dflt, 0);
Michal Vasko478c4652016-07-21 12:55:01 +02003483 break;
3484 }
3485 }
3486
Radek Krejci51673202016-11-01 17:00:32 +01003487 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003488 /* no default value, nothing to check, all is well */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003489 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003490 }
3491
3492 /* 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)? */
3493 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003494 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003495 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003496 goto cleanup;
Radek Krejci9e6af732017-04-27 14:40:25 +02003497 } else {
3498 /* check the default value from typedef, but use also the typedef's module
3499 * due to possible searching in imported modules which is expected in
3500 * typedef's module instead of module where the typedef is used */
3501 module = base_tpdf->module;
3502 }
3503 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003504 case LY_TYPE_INST:
3505 case LY_TYPE_LEAFREF:
3506 case LY_TYPE_BOOL:
3507 case LY_TYPE_EMPTY:
3508 /* these have no restrictions, so we would do the exact same work as the unres in the base typedef */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003509 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003510 case LY_TYPE_BITS:
3511 /* the default value must match the restricted list of values, if the type was restricted */
3512 if (type->info.bits.count) {
3513 break;
3514 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003515 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003516 case LY_TYPE_ENUM:
3517 /* the default value must match the restricted list of values, if the type was restricted */
3518 if (type->info.enums.count) {
3519 break;
3520 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003521 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003522 case LY_TYPE_DEC64:
3523 if (type->info.dec64.range) {
3524 break;
3525 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003526 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003527 case LY_TYPE_BINARY:
3528 if (type->info.binary.length) {
3529 break;
3530 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003531 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003532 case LY_TYPE_INT8:
3533 case LY_TYPE_INT16:
3534 case LY_TYPE_INT32:
3535 case LY_TYPE_INT64:
3536 case LY_TYPE_UINT8:
3537 case LY_TYPE_UINT16:
3538 case LY_TYPE_UINT32:
3539 case LY_TYPE_UINT64:
3540 if (type->info.num.range) {
3541 break;
3542 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003543 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003544 case LY_TYPE_STRING:
3545 if (type->info.str.length || type->info.str.patterns) {
3546 break;
3547 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003548 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003549 case LY_TYPE_UNION:
3550 /* way too much trouble learning whether we need to check the default again, so just do it */
3551 break;
3552 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01003553 LOGINT(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003554 ret = -1;
3555 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003556 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003557 } else if (type->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003558 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3559 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003560 ret = -1;
3561 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003562 }
3563
Michal Vasko1dca6882015-10-22 14:29:42 +02003564 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003565 memset(&node, 0, sizeof node);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003566 node.value_str = lydict_insert(ctx, dflt, 0);
Michal Vasko1dca6882015-10-22 14:29:42 +02003567 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003568
3569 if (tpdf) {
3570 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003571 if (!node.schema) {
3572 LOGMEM(ctx);
3573 ret = -1;
3574 goto cleanup;
3575 }
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003576 r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003577 if (r == -1) {
3578 LOGMEM(ctx);
3579 ret = -1;
3580 goto cleanup;
3581 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003582 node.schema->module = module;
3583 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3584 } else {
3585 node.schema = (struct lys_node *)type->parent;
3586 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003587
Radek Krejci37b756f2016-01-18 10:15:03 +01003588 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003589 if (!type->info.lref.target) {
3590 ret = EXIT_FAILURE;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003591 goto cleanup;
Michal Vasko1dca6882015-10-22 14:29:42 +02003592 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003593 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003594 if (!ret) {
3595 /* adopt possibly changed default value to its canonical form */
3596 if (*value) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003597 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003598 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003599 dflt = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003600 }
3601 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003602 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02003603 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1, 0)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003604 /* possible forward reference */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003605 ret = EXIT_FAILURE;
Radek Krejcibad2f172016-08-02 11:04:15 +02003606 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003607 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003608 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3609 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3610 /* we have refined bits/enums */
Michal Vasko53b7da02018-02-13 15:28:42 +01003611 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
Radek Krejcibad2f172016-08-02 11:04:15 +02003612 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003613 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003614 }
3615 }
Radek Krejci51673202016-11-01 17:00:32 +01003616 } else {
3617 /* success - adopt canonical form from the node into the default value */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003618 if (!ly_strequal(dflt, node.value_str, 1)) {
Radek Krejci51673202016-11-01 17:00:32 +01003619 /* this can happen only if we have non-inherited default value,
3620 * inherited default values are already in canonical form */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003621 assert(ly_strequal(dflt, *value, 1));
3622
3623 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003624 *value = node.value_str;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003625 node.value_str = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003626 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003627 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003628 }
3629
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003630cleanup:
Michal Vasko7ba37442018-11-06 08:59:27 +01003631 lyd_free_value(node.value, node.value_type, node.value_flags, type, NULL, NULL, NULL);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003632 lydict_remove(ctx, node.value_str);
3633 if (tpdf && node.schema) {
Michal Vasko31a2d322018-01-12 13:36:12 +01003634 free((char *)node.schema->name);
3635 free(node.schema);
3636 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003637 lydict_remove(ctx, dflt);
Michal Vasko1dca6882015-10-22 14:29:42 +02003638
3639 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003640}
3641
Michal Vasko730dfdf2015-08-11 14:48:05 +02003642/**
3643 * @brief Check a key for mandatory attributes. Logs directly.
3644 *
3645 * @param[in] key The key to check.
3646 * @param[in] flags What flags to check.
3647 * @param[in] list The list of all the keys.
3648 * @param[in] index Index of the key in the key list.
3649 * @param[in] name The name of the keys.
3650 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003651 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003652 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003653 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003654static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003655check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003656{
Radek Krejciadb57612016-02-16 13:34:34 +01003657 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003658 char *dup = NULL;
3659 int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01003660 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003661
3662 /* existence */
3663 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003664 if (name[len] != '\0') {
3665 dup = strdup(name);
Michal Vasko53b7da02018-02-13 15:28:42 +01003666 LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003667 dup[len] = '\0';
3668 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003669 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003670 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003671 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003672 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003673 }
3674
3675 /* uniqueness */
3676 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003677 if (key == list->keys[j]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003678 LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003679 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003680 }
3681 }
3682
3683 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003684 if (key->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003685 LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003686 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003687 }
3688
3689 /* type of the leaf is not built-in empty */
Radek Krejci13fde922018-05-16 10:45:58 +02003690 if (key->type.base == LY_TYPE_EMPTY && key->module->version < LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003691 LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003692 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003693 }
3694
3695 /* config attribute is the same as of the list */
Michal Vasko627016e2018-10-24 09:25:55 +02003696 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK)
3697 && ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003698 LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003699 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003700 }
3701
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003702 /* key is not placed from augment */
3703 if (key->parent->nodetype == LYS_AUGMENT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003704 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3705 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003706 return -1;
3707 }
3708
Radek Krejci3f21ada2016-08-01 13:34:31 +02003709 /* key is not when/if-feature -conditional */
3710 j = 0;
3711 if (key->when || (key->iffeature_size && (j = 1))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003712 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3713 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003714 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003715 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003716 }
3717
Michal Vasko0b85aa82016-03-07 14:37:43 +01003718 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003719}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003720
3721/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003722 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003723 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003724 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003725 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003726 *
3727 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3728 */
3729int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003730resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003731{
Radek Krejci581ce772015-11-10 17:22:40 +01003732 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003733 const struct lys_node *leaf = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01003734 struct ly_ctx *ctx = parent->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003735
Michal Vaskodc300b02017-04-07 14:09:20 +02003736 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003737 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003738 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003739 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003740 if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003741 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 +02003742 } else if (rc == -2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003743 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003744 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003745 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003746 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003747 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3748 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003749 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003750 }
Radek Krejci581ce772015-11-10 17:22:40 +01003751 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003752 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003753 if (leaf->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003754 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3755 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003756 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003757 }
3758
Radek Krejcicf509982015-12-15 09:22:44 +01003759 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003760 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3761 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003762 return -1;
3763 }
3764
Radek Krejcid09d1a52016-08-11 14:05:45 +02003765 /* check that all unique's targets are of the same config type */
3766 if (*trg_type) {
3767 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003768 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3769 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003770 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3771 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3772 return -1;
3773 }
3774 } else {
3775 /* first unique */
3776 if (leaf->flags & LYS_CONFIG_W) {
3777 *trg_type = 1;
3778 } else {
3779 *trg_type = 2;
3780 }
3781 }
3782
Radek Krejcica7efb72016-01-18 13:06:01 +01003783 /* set leaf's unique flag */
3784 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3785
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003786 return EXIT_SUCCESS;
3787
3788error:
3789
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003790 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003791}
3792
Radek Krejci0c0086a2016-03-24 15:20:28 +01003793void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003794unres_data_del(struct unres_data *unres, uint32_t i)
3795{
3796 /* there are items after the one deleted */
3797 if (i+1 < unres->count) {
3798 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003799 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003800
3801 /* deleting the last item */
3802 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003803 free(unres->node);
3804 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003805 }
3806
3807 /* if there are no items after and it is not the last one, just move the counter */
3808 --unres->count;
3809}
3810
Michal Vasko0491ab32015-08-19 14:28:29 +02003811/**
3812 * @brief Resolve (find) a data node from a specific module. Does not log.
3813 *
3814 * @param[in] mod Module to search in.
3815 * @param[in] name Name of the data node.
3816 * @param[in] nam_len Length of the name.
3817 * @param[in] start Data node to start the search from.
3818 * @param[in,out] parents Resolved nodes. If there are some parents,
3819 * they are replaced (!!) with the resolvents.
3820 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003821 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003822 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003823static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003824resolve_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 +02003825{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003826 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003827 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003828 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003829
Michal Vasko23b61ec2015-08-19 11:19:50 +02003830 if (!parents->count) {
3831 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003832 parents->node = malloc(sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003833 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003834 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003835 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003836 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003837 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003838 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003839 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003840 continue;
3841 }
3842 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003843 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003844 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003845 && node->schema->name[nam_len] == '\0') {
3846 /* matching target */
3847 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003848 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003849 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003850 flag = 1;
3851 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003852 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003853 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003854 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003855 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003856 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003857 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003858 }
3859 }
3860 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003861
3862 if (!flag) {
3863 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003864 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003865 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003866 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003867 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003868 }
3869
Michal Vasko0491ab32015-08-19 14:28:29 +02003870 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003871}
3872
Michal Vaskoe27516a2016-10-10 17:55:31 +00003873static int
Michal Vasko1c007172017-03-10 10:20:44 +01003874resolve_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 +00003875{
3876 int dep1, dep2;
3877 const struct lys_node *node;
3878
3879 if (lys_parent(op_node)) {
3880 /* inner operation (notif/action) */
3881 if (abs_path) {
3882 return 1;
3883 } else {
3884 /* compare depth of both nodes */
3885 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3886 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3887 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3888 return 1;
3889 }
3890 }
3891 } else {
3892 /* top-level operation (notif/rpc) */
3893 if (op_node != first_node) {
3894 return 1;
3895 }
3896 }
3897
3898 return 0;
3899}
3900
Michal Vasko730dfdf2015-08-11 14:48:05 +02003901/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003902 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003903 *
Michal Vaskobb211122015-08-19 14:03:11 +02003904 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003905 * @param[in] context_node Predicate context node (where the predicate is placed).
3906 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003907 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003908 *
Michal Vasko184521f2015-09-24 13:14:26 +02003909 * @return 0 on forward reference, otherwise the number
3910 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003911 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003912 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003913static int
Michal Vasko1c007172017-03-10 10:20:44 +01003914resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3915 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003916{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003917 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003918 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003919 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003920 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3921 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003922 struct ly_ctx *ctx = context_node->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003923
3924 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003925 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003926 &pke_len, &has_predicate)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003927 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003928 return -parsed+i;
3929 }
3930 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003931 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003932
Michal Vasko58090902015-08-13 14:04:15 +02003933 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003934 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003935 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003936 } else {
Michal Vaskoa50e60c2018-08-22 12:07:21 +02003937 trg_mod = lys_node_module(parent);
Michal Vasko36cbaa42015-12-14 13:15:48 +01003938 }
Michal Vaskobb520442017-05-23 10:55:18 +02003939 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003940 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003941 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003942 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003943 }
3944
3945 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003946 dest_parent_times = 0;
3947 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003948 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3949 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003950 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003951 return -parsed;
3952 }
3953 pke_parsed += i;
3954
Radek Krejciadb57612016-02-16 13:34:34 +01003955 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003956 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3957 && !((struct lys_node_augment *)dst_node->parent)->target) {
3958 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003959 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003960 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3961 return 0;
3962 }
3963
Michal Vaskofbaead72016-10-07 10:54:48 +02003964 /* path is supposed to be evaluated in data tree, so we have to skip
3965 * all schema nodes that cannot be instantiated in data tree */
3966 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003967 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003968 dst_node = lys_parent(dst_node));
3969
Michal Vasko1f76a282015-08-04 16:16:53 +02003970 if (!dst_node) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003971 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003972 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003973 }
3974 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003975 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003976 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003977 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003978 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003979 } else {
Michal Vaskoa50e60c2018-08-22 12:07:21 +02003980 trg_mod = lys_node_module(parent);
Michal Vasko36cbaa42015-12-14 13:15:48 +01003981 }
Michal Vaskobb520442017-05-23 10:55:18 +02003982 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 +02003983 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003984 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003985 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003986 }
3987
Michal Vaskoe27516a2016-10-10 17:55:31 +00003988 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003989 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003990 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003991 }
3992 first_iter = 0;
3993 }
3994
Michal Vasko1f76a282015-08-04 16:16:53 +02003995 if (pke_len == pke_parsed) {
3996 break;
3997 }
3998
Michal Vaskobb520442017-05-23 10:55:18 +02003999 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 +02004000 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02004001 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02004002 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02004003 return -parsed;
4004 }
4005 pke_parsed += i;
4006 }
4007
4008 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02004009 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02004010 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed);
Michal Vasko53b7da02018-02-13 15:28:42 +01004011 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02004012 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02004013 return -parsed;
4014 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004015 } while (has_predicate);
4016
4017 return parsed;
4018}
4019
Michal Vasko74083ec2018-06-15 10:00:12 +02004020static int
4021check_leafref_features(struct lys_type *type)
4022{
4023 struct lys_node *iter;
4024 struct ly_set *src_parents, *trg_parents, *features;
4025 struct lys_node_augment *aug;
4026 struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx;
4027 unsigned int i, j, size, x;
4028 int ret = EXIT_SUCCESS;
4029
4030 assert(type->parent);
4031
4032 src_parents = ly_set_new();
4033 trg_parents = ly_set_new();
4034 features = ly_set_new();
4035
4036 /* get parents chain of source (leafref) */
4037 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
4038 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4039 continue;
4040 }
4041 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4042 aug = (struct lys_node_augment *)iter->parent;
4043 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4044 /* unresolved augment, wait until it's resolved */
4045 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4046 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4047 ret = EXIT_FAILURE;
4048 goto cleanup;
4049 }
Michal Vaskod42871e2018-07-12 09:06:53 +02004050 /* also add this augment */
4051 ly_set_add(src_parents, aug, LY_SET_OPT_USEASLIST);
Michal Vasko74083ec2018-06-15 10:00:12 +02004052 }
4053 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
4054 }
4055 /* get parents chain of target */
4056 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
4057 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4058 continue;
4059 }
4060 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4061 aug = (struct lys_node_augment *)iter->parent;
4062 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4063 /* unresolved augment, wait until it's resolved */
4064 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4065 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4066 ret = EXIT_FAILURE;
4067 goto cleanup;
4068 }
4069 }
4070 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
4071 }
4072
4073 /* compare the features used in if-feature statements in the rest of both
4074 * chains of parents. The set of features used for target must be a subset
4075 * of features used for the leafref. This is not a perfect, we should compare
4076 * the truth tables but it could require too much resources, so we simplify that */
4077 for (i = 0; i < src_parents->number; i++) {
4078 iter = src_parents->set.s[i]; /* shortcut */
4079 if (!iter->iffeature_size) {
4080 continue;
4081 }
4082 for (j = 0; j < iter->iffeature_size; j++) {
4083 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4084 for (; size; size--) {
4085 if (!iter->iffeature[j].features[size - 1]) {
4086 /* not yet resolved feature, postpone this check */
4087 ret = EXIT_FAILURE;
4088 goto cleanup;
4089 }
4090 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
4091 }
4092 }
4093 }
4094 x = features->number;
4095 for (i = 0; i < trg_parents->number; i++) {
4096 iter = trg_parents->set.s[i]; /* shortcut */
4097 if (!iter->iffeature_size) {
4098 continue;
4099 }
4100 for (j = 0; j < iter->iffeature_size; j++) {
4101 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4102 for (; size; size--) {
4103 if (!iter->iffeature[j].features[size - 1]) {
4104 /* not yet resolved feature, postpone this check */
4105 ret = EXIT_FAILURE;
4106 goto cleanup;
4107 }
4108 if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
4109 /* the feature is not present in features set of target's parents chain */
4110 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
4111 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
4112 "Leafref is not conditional based on \"%s\" feature as its target.",
4113 iter->iffeature[j].features[size - 1]->name);
4114 ret = -1;
4115 goto cleanup;
4116 }
4117 }
4118 }
4119 }
4120
4121cleanup:
4122 ly_set_free(features);
4123 ly_set_free(src_parents);
4124 ly_set_free(trg_parents);
4125
4126 return ret;
4127}
4128
Michal Vasko730dfdf2015-08-11 14:48:05 +02004129/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004130 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004131 *
Michal Vaskobb211122015-08-19 14:03:11 +02004132 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004133 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004134 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004135 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004136 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004137 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004138static int
Michal Vasko74083ec2018-06-15 10:00:12 +02004139resolve_schema_leafref(struct lys_type *type, struct lys_node *parent, struct unres_schema *unres)
Michal Vasko1f76a282015-08-04 16:16:53 +02004140{
Michal Vaskocb45f472018-02-12 10:47:42 +01004141 const struct lys_node *node, *op_node = NULL, *tmp_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02004142 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02004143 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02004144 const char *id, *prefix, *name;
4145 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskocb45f472018-02-12 10:47:42 +01004146 int i, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01004147 struct ly_ctx *ctx = parent->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02004148
Michal Vasko74083ec2018-06-15 10:00:12 +02004149 if (!type->info.lref.target) {
4150 first_iter = 1;
4151 parent_times = 0;
4152 id = type->info.lref.path;
Michal Vasko1f76a282015-08-04 16:16:53 +02004153
Michal Vasko74083ec2018-06-15 10:00:12 +02004154 /* find operation schema we are in */
4155 for (op_node = lys_parent(parent);
4156 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
4157 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02004158
Michal Vasko74083ec2018-06-15 10:00:12 +02004159 cur_module = lys_node_module(parent);
4160 do {
4161 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
4162 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vaskobb520442017-05-23 10:55:18 +02004163 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004164 }
4165 id += i;
Michal Vasko74083ec2018-06-15 10:00:12 +02004166
4167 /* get the current module */
4168 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
4169 if (!tmp_mod) {
4170 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4171 return EXIT_FAILURE;
4172 }
4173 last_aug = NULL;
4174
4175 if (first_iter) {
4176 if (parent_times == -1) {
4177 /* use module data */
4178 node = NULL;
4179
4180 } else if (parent_times > 0) {
4181 /* we are looking for the right parent */
4182 for (i = 0, node = parent; i < parent_times; i++) {
4183 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
4184 && !((struct lys_node_augment *)node->parent)->target) {
4185 /* we are in an unresolved augment, cannot evaluate */
4186 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent,
4187 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path);
4188 return EXIT_FAILURE;
4189 }
4190
4191 /* path is supposed to be evaluated in data tree, so we have to skip
4192 * all schema nodes that cannot be instantiated in data tree */
4193 for (node = lys_parent(node);
4194 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
4195 node = lys_parent(node));
4196
4197 if (!node) {
4198 if (i == parent_times - 1) {
4199 /* top-level */
4200 break;
4201 }
4202
4203 /* higher than top-level */
4204 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4205 return EXIT_FAILURE;
4206 }
4207 }
4208 } else {
4209 LOGINT(ctx);
4210 return -1;
4211 }
4212 }
4213
4214 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
4215 * - useless to search for that in augments) */
4216 if (!tmp_mod->implemented && node) {
4217 get_next_augment:
4218 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
4219 }
4220
4221 tmp_parent = (last_aug ? (struct lys_node *)last_aug : node);
4222 node = NULL;
4223 while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) {
4224 if (lys_node_module(node) != lys_main_module(tmp_mod)) {
4225 continue;
4226 }
4227 if (strncmp(node->name, name, nam_len) || node->name[nam_len]) {
4228 continue;
4229 }
4230 /* match */
4231 break;
4232 }
4233 if (!node) {
4234 if (last_aug) {
4235 /* restore the correct augment target */
4236 node = last_aug->target;
4237 goto get_next_augment;
4238 }
4239 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4240 return EXIT_FAILURE;
4241 }
4242
4243 if (first_iter) {
4244 /* set external dependency flag, we can decide based on the first found node */
4245 if (op_node && parent_times &&
4246 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
4247 parent->flags |= LYS_LEAFREF_DEP;
4248 }
4249 first_iter = 0;
4250 }
4251
4252 if (has_predicate) {
4253 /* we have predicate, so the current result must be list */
4254 if (node->nodetype != LYS_LIST) {
4255 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4256 return -1;
4257 }
4258
4259 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
4260 if (!i) {
4261 return EXIT_FAILURE;
4262 } else if (i < 0) {
4263 return -1;
4264 }
4265 id += i;
4266 has_predicate = 0;
4267 }
4268 } while (id[0]);
4269
4270 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
4271 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
4272 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4273 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path);
4274 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004275 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004276
Michal Vasko74083ec2018-06-15 10:00:12 +02004277 /* check status */
4278 if (lyp_check_status(parent->flags, parent->module, parent->name,
4279 node->flags, node->module, node->name, node)) {
4280 return -1;
4281 }
4282
4283 /* assign */
4284 type->info.lref.target = (struct lys_node_leaf *)node;
Radek Krejcib1c12512015-08-11 11:22:04 +02004285 }
4286
Michal Vasko74083ec2018-06-15 10:00:12 +02004287 /* as the last thing traverse this leafref and make targets on the path implemented */
4288 if (lys_node_module(parent)->implemented) {
4289 /* make all the modules in the path implemented */
4290 for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) {
4291 if (!lys_node_module(node)->implemented) {
4292 lys_node_module(node)->implemented = 1;
4293 if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4294 return -1;
4295 }
4296 }
4297 }
4298
4299 /* store the backlink from leafref target */
4300 if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
4301 return -1;
4302 }
Radek Krejcicf509982015-12-15 09:22:44 +01004303 }
4304
Michal Vasko74083ec2018-06-15 10:00:12 +02004305 /* check if leafref and its target are under common if-features */
4306 return check_leafref_features(type);
Michal Vasko1f76a282015-08-04 16:16:53 +02004307}
4308
Michal Vasko730dfdf2015-08-11 14:48:05 +02004309/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02004310 * @brief Compare 2 data node values.
4311 *
4312 * Comparison performed on canonical forms, the first value
4313 * is first transformed into canonical form.
4314 *
4315 * @param[in] node Leaf/leaf-list with these values.
4316 * @param[in] noncan_val Non-canonical value.
4317 * @param[in] noncan_val_len Length of \p noncal_val.
4318 * @param[in] can_val Canonical value.
4319 * @return 1 if equal, 0 if not, -1 on error (logged).
4320 */
4321static int
4322valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
4323{
4324 int ret;
4325 struct lyd_node_leaf_list leaf;
4326 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
4327
4328 /* dummy leaf */
4329 memset(&leaf, 0, sizeof leaf);
4330 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
4331
4332repeat:
4333 leaf.value_type = sleaf->type.base;
4334 leaf.schema = node;
4335
4336 if (leaf.value_type == LY_TYPE_LEAFREF) {
4337 if (!sleaf->type.info.lref.target) {
4338 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01004339 LOGINT(node->module->ctx);
Michal Vasko718ecdd2017-10-03 14:12:39 +02004340 ret = -1;
4341 goto finish;
4342 }
4343 sleaf = sleaf->type.info.lref.target;
4344 goto repeat;
4345 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02004346 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02004347 ret = -1;
4348 goto finish;
4349 }
4350 }
4351
4352 if (!strcmp(leaf.value_str, can_val)) {
4353 ret = 1;
4354 } else {
4355 ret = 0;
4356 }
4357
4358finish:
4359 lydict_remove(node->module->ctx, leaf.value_str);
4360 return ret;
4361}
4362
4363/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004364 * @brief Resolve instance-identifier predicate in JSON data format.
4365 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004366 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02004367 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02004368 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004369 * @param[in,out] node Node matching the restriction without
4370 * the predicate. If it does not satisfy the predicate,
4371 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004372 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004373 * @return Number of characters successfully parsed,
4374 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004375 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004376static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004377resolve_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 +02004378{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004379 /* ... /node[key=value] ... */
4380 struct lyd_node_leaf_list *key;
4381 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02004382 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004383 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004384 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vasko53b7da02018-02-13 15:28:42 +01004385 struct ly_ctx *ctx = prev_mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004386
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004387 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02004388
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004389 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004390 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004391 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
4392 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004393 }
4394 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004395
Michal Vasko88850b72017-10-02 13:13:21 +02004396 if (!(*node)) {
4397 /* just parse it all */
4398 continue;
4399 }
4400
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004401 /* target */
4402 if (name[0] == '.') {
4403 /* leaf-list value */
4404 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004405 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004406 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4407 parsed = -1;
4408 goto cleanup;
4409 }
4410
4411 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004412 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004413 *node = NULL;
4414 goto cleanup;
4415 }
4416
4417 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004418 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004419
4420 /* keyless list position */
4421 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004422 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004423 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4424 parsed = -1;
4425 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004426 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004427
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004428 if (((struct lys_node_list *)(*node)->schema)->keys) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004429 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 +02004430 (*node)->schema->name);
4431 parsed = -1;
4432 goto cleanup;
4433 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004434
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004435 /* check the index */
4436 if (atoi(name) != cur_idx) {
4437 *node = NULL;
4438 goto cleanup;
4439 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004440
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004441 } else {
4442 /* list key value */
4443 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004444 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004445 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4446 parsed = -1;
4447 goto cleanup;
4448 }
4449 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004450
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004451 /* prepare key array */
4452 if (!list_keys) {
4453 list_keys = malloc(slist->keys_size * sizeof *list_keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01004454 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004455 for (i = 0; i < slist->keys_size; ++i) {
4456 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004457 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004458 }
4459
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004460 /* find the schema key leaf */
4461 for (i = 0; i < slist->keys_size; ++i) {
4462 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4463 break;
4464 }
4465 }
4466 if (i == slist->keys_size) {
4467 /* this list has no such key */
Michal Vasko53b7da02018-02-13 15:28:42 +01004468 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004469 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4470 parsed = -1;
4471 goto cleanup;
4472 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004473
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004474 /* check module */
4475 if (model) {
4476 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004477 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 +02004478 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4479 parsed = -1;
4480 goto cleanup;
4481 }
4482 } else {
4483 if (list_keys[i]->module != prev_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004484 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 +02004485 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4486 parsed = -1;
4487 goto cleanup;
4488 }
4489 }
4490
4491 /* find the actual data key */
4492 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4493 if (key->schema == (struct lys_node *)list_keys[i]) {
4494 break;
4495 }
4496 }
4497 if (!key) {
4498 /* list instance is missing a key? definitely should not happen */
Michal Vasko53b7da02018-02-13 15:28:42 +01004499 LOGINT(ctx);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004500 parsed = -1;
4501 goto cleanup;
4502 }
4503
4504 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004505 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004506 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004507 /* we still want to parse the whole predicate */
4508 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004509 }
4510
4511 /* everything is fine, mark this key as resolved */
4512 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004513 }
4514 } while (has_predicate);
4515
Michal Vaskob2f40be2016-09-08 16:03:48 +02004516 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004517 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004518 for (i = 0; i < slist->keys_size; ++i) {
4519 if (list_keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004520 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 +02004521 parsed = -1;
4522 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004523 }
4524 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004525 }
4526
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004527cleanup:
4528 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004529 return parsed;
4530}
4531
Michal Vasko895c11f2018-03-12 11:35:58 +01004532static int
4533check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004534{
Michal Vasko0b963112017-08-11 12:45:36 +02004535 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004536 struct lyxp_set set;
Michal Vasko895c11f2018-03-12 11:35:58 +01004537 enum int_log_opts prev_ilo;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004538
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004539 if (check_place) {
4540 parent = node;
4541 while (parent) {
4542 if (parent->nodetype == LYS_GROUPING) {
4543 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004544 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004545 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004546 if (parent->nodetype == LYS_AUGMENT) {
4547 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004548 /* unresolved augment, skip for now (will be checked later) */
4549 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004550 } else {
4551 parent = ((struct lys_node_augment *)parent)->target;
4552 continue;
4553 }
4554 }
4555 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004556 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004557 }
4558
Michal Vasko895c11f2018-03-12 11:35:58 +01004559 memset(&set, 0, sizeof set);
Michal Vasko9e635ac2016-10-17 11:44:09 +02004560
Michal Vasko895c11f2018-03-12 11:35:58 +01004561 /* produce just warnings */
4562 ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL);
4563 lyxp_node_atomize(node, &set, 1);
4564 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
4565
4566 if (set.val.snodes) {
4567 free(set.val.snodes);
4568 }
4569 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004570}
4571
Radek Krejcif71f48f2016-10-25 16:37:24 +02004572static int
4573check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4574{
Radek Krejcidce5f972017-09-12 15:47:49 +02004575 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004576
4577 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004578 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4579 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004580 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 +02004581 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4582 return -1;
4583 }
4584 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4585 * of leafref resolving (lys_leaf_add_leafref_target()) */
4586 } else if (type->base == LY_TYPE_UNION) {
4587 for (i = 0; i < type->info.uni.count; i++) {
4588 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4589 return -1;
4590 }
4591 }
4592 }
4593 return 0;
4594}
4595
Michal Vasko9e635ac2016-10-17 11:44:09 +02004596/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004597 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004598 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004599 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004600 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004601 * @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 +02004602 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004603 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004604 *
4605 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004606 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004607int
4608inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004609{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004610 struct lys_node_leaf *leaf;
Michal Vasko53b7da02018-02-13 15:28:42 +01004611 struct ly_ctx *ctx;
4612
4613 if (!node) {
4614 return 0;
4615 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004616
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004617 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Michal Vasko53b7da02018-02-13 15:28:42 +01004618 ctx = node->module->ctx;
4619
Radek Krejci1d82ef62015-08-07 14:44:40 +02004620 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004621 if (clear) {
4622 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004623 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004624 } else {
4625 if (node->flags & LYS_CONFIG_SET) {
4626 /* skip nodes with an explicit config value */
4627 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004628 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config");
4629 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004630 return -1;
4631 }
4632 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004633 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004634
4635 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4636 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4637 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004638 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4639 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004640 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
Michal Vaskoe022a562016-09-27 14:24:15 +02004641 return -1;
4642 }
4643 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004644 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004645 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004646 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004647 return -1;
4648 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004649 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4650 leaf = (struct lys_node_leaf *)node;
4651 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004652 return -1;
4653 }
4654 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004655 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004656
4657 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004658}
4659
Michal Vasko730dfdf2015-08-11 14:48:05 +02004660/**
Michal Vasko7178e692016-02-12 15:58:05 +01004661 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004662 *
Michal Vaskobb211122015-08-19 14:03:11 +02004663 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004664 * @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 +01004665 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004666 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004667 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004668 */
Michal Vasko7178e692016-02-12 15:58:05 +01004669static int
Michal Vasko97234262018-02-01 09:53:01 +01004670resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004671{
Michal Vasko44ab1462017-05-18 13:18:36 +02004672 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004673 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004674 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004675 struct ly_set *set;
Michal Vasko53b7da02018-02-13 15:28:42 +01004676 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004677
Michal Vasko2ef7db62017-06-12 09:24:02 +02004678 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004679 mod = lys_main_module(aug->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01004680 ctx = mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004681
Michal Vaskobb520442017-05-23 10:55:18 +02004682 /* set it as not applied for now */
4683 aug->flags |= LYS_NOTAPPLIED;
4684
Michal Vasko2ef7db62017-06-12 09:24:02 +02004685 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004686 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004687 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004688 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 +02004689 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004690 LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004691 return -1;
4692 }
Michal Vasko50576712017-07-28 12:28:33 +02004693 if (!set) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004694 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004695 return EXIT_FAILURE;
4696 }
Michal Vasko50576712017-07-28 12:28:33 +02004697 aug->target = set->set.s[0];
4698 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004699 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004700
Michal Vasko74083ec2018-06-15 10:00:12 +02004701 /* make this module implemented if the target module is (if the target is in an unimplemented module,
4702 * it is fine because when we will be making that module implemented, its augment will be applied
4703 * and that augment target module made implemented, recursively) */
4704 if (mod->implemented && !lys_node_module(aug->target)->implemented) {
4705 lys_node_module(aug->target)->implemented = 1;
4706 if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4707 return -1;
4708 }
4709 }
4710
Michal Vaskod58d5962016-03-02 14:29:41 +01004711 /* check for mandatory nodes - if the target node is in another module
4712 * the added nodes cannot be mandatory
4713 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004714 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4715 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004716 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004717 }
4718
Michal Vasko07e89ef2016-03-03 13:28:57 +01004719 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004720 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004721 LY_TREE_FOR(aug->child, sub) {
4722 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4723 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004724 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4725 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004726 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004727 return -1;
4728 }
4729 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004730 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004731 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004732 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004733 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4734 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004735 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004736 return -1;
4737 }
4738 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004739 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004740 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004741 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004742 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4743 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004744 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004745 return -1;
4746 }
4747 }
4748 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004749 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
4750 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 +01004751 return -1;
4752 }
4753
Radek Krejcic071c542016-01-27 14:57:51 +01004754 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004755 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004756 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004757 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004758 }
4759 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004760
Michal Vasko44ab1462017-05-18 13:18:36 +02004761 if (!aug->child) {
4762 /* empty augment, nothing to connect, but it is techincally applied */
Michal Vasko53b7da02018-02-13 15:28:42 +01004763 LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name);
Michal Vasko44ab1462017-05-18 13:18:36 +02004764 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004765 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4766 /* we try to connect the augment only in case the module is implemented or
4767 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004768 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004769 }
4770
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004771 return EXIT_SUCCESS;
4772}
4773
Radek Krejcie534c132016-11-23 13:32:31 +01004774static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004775resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004776{
4777 enum LY_VLOG_ELEM vlog_type;
4778 void *vlog_node;
4779 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004780 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004781 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004782 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004783 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004784 struct lys_ext_instance *tmp_ext;
Michal Vasko53b7da02018-02-13 15:28:42 +01004785 struct ly_ctx *ctx = NULL;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004786 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004787
4788 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004789 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004790 vlog_node = info->parent;
4791 vlog_type = LY_VLOG_LYS;
4792 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004793 case LYEXT_PAR_MODULE:
4794 case LYEXT_PAR_IMPORT:
4795 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004796 vlog_node = NULL;
4797 vlog_type = LY_VLOG_LYS;
4798 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004799 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004800 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004801 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004802 break;
4803 }
4804
4805 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004806 /* YIN */
4807
Radek Krejcie534c132016-11-23 13:32:31 +01004808 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004809 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004810 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004811 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004812 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004813 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004814 ctx = mod->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01004815
4816 /* find the extension definition */
4817 e = NULL;
4818 for (i = 0; i < mod->extensions_size; i++) {
4819 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4820 e = &mod->extensions[i];
4821 break;
4822 }
4823 }
4824 /* try submodules */
4825 for (j = 0; !e && j < mod->inc_size; j++) {
4826 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4827 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4828 e = &mod->inc[j].submodule->extensions[i];
4829 break;
4830 }
4831 }
4832 }
4833 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004834 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004835 return EXIT_FAILURE;
4836 }
4837
4838 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004839
Radek Krejci72b35992017-01-04 16:27:44 +01004840 if (e->plugin && e->plugin->check_position) {
4841 /* common part - we have plugin with position checking function, use it first */
4842 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4843 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004844 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004845 return -1;
4846 }
4847 }
4848
Radek Krejci8d6b7422017-02-03 14:42:13 +01004849 /* extension type-specific part - allocation */
4850 if (e->plugin) {
4851 etype = e->plugin->type;
4852 } else {
4853 /* default type */
4854 etype = LYEXT_FLAG;
4855 }
4856 switch (etype) {
4857 case LYEXT_FLAG:
4858 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4859 break;
4860 case LYEXT_COMPLEX:
4861 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4862 break;
4863 case LYEXT_ERR:
4864 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004865 LOGINT(ctx);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004866 return -1;
4867 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004868 LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004869
4870 /* common part for all extension types */
4871 (*ext)->def = e;
4872 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004873 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004874 (*ext)->insubstmt = info->substmt;
4875 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004876 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004877 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004878
PavolVicand86b0d62017-09-01 11:01:39 +02004879 if (e->argument) {
4880 if (!(e->flags & LYS_YINELEM)) {
4881 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4882 if (!(*ext)->arg_value) {
PavolVicane7a1fc62018-02-19 16:50:27 +01004883 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
PavolVicand86b0d62017-09-01 11:01:39 +02004884 return -1;
4885 }
4886
4887 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4888 } else {
4889 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4890 if (ly_strequal(yin->name, e->argument, 1)) {
4891 (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0);
4892 lyxml_free(mod->ctx, yin);
4893 break;
4894 }
4895 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004896 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004897 }
4898
PavolVican92f23622017-12-12 13:35:56 +01004899 if ((*ext)->flags & LYEXT_OPT_VALID &&
4900 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01004901 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004902 }
4903
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004904 (*ext)->nodetype = LYS_EXT;
4905 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004906
Radek Krejci8d6b7422017-02-03 14:42:13 +01004907 /* extension type-specific part - parsing content */
4908 switch (etype) {
4909 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004910 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4911 if (!yin->ns) {
4912 /* garbage */
4913 lyxml_free(mod->ctx, yin);
4914 continue;
4915 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4916 /* standard YANG statements are not expected here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004917 LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004918 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004919 } else if (yin->ns == info->data.yin->ns &&
4920 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004921 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004922 if ((*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004923 LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004924 return -1;
4925 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004926 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004927 yin->content = NULL;
4928 lyxml_free(mod->ctx, yin);
4929 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004930 /* extension instance */
4931 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4932 LYEXT_SUBSTMT_SELF, 0, unres)) {
4933 return -1;
4934 }
Radek Krejci72b35992017-01-04 16:27:44 +01004935
Radek Krejci72b35992017-01-04 16:27:44 +01004936 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004937 }
Radek Krejci72b35992017-01-04 16:27:44 +01004938 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004939 break;
4940 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004941 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004942 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4943 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004944 return -1;
4945 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004946 break;
4947 default:
4948 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004949 }
Radek Krejci72b35992017-01-04 16:27:44 +01004950
4951 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4952
Radek Krejcie534c132016-11-23 13:32:31 +01004953 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004954 /* YANG */
4955
PavolVicanc1807262017-01-31 18:00:27 +01004956 ext_prefix = (char *)(*ext)->def;
4957 tmp = strchr(ext_prefix, ':');
4958 if (!tmp) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004959 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004960 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004961 }
4962 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004963
PavolVicanc1807262017-01-31 18:00:27 +01004964 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004965 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004966 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004967 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004968 return EXIT_FAILURE;
4969 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004970 ctx = mod->ctx;
PavolVicanc1807262017-01-31 18:00:27 +01004971
4972 /* find the extension definition */
4973 e = NULL;
4974 for (i = 0; i < mod->extensions_size; i++) {
4975 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4976 e = &mod->extensions[i];
4977 break;
4978 }
4979 }
4980 /* try submodules */
4981 for (j = 0; !e && j < mod->inc_size; j++) {
4982 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4983 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4984 e = &mod->inc[j].submodule->extensions[i];
4985 break;
4986 }
4987 }
4988 }
4989 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004990 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004991 return EXIT_FAILURE;
4992 }
4993
PavolVicanfcc98762017-09-01 15:51:39 +02004994 (*ext)->flags &= ~LYEXT_OPT_YANG;
4995 (*ext)->def = NULL;
4996
PavolVicanc1807262017-01-31 18:00:27 +01004997 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4998
4999 if (e->plugin && e->plugin->check_position) {
5000 /* common part - we have plugin with position checking function, use it first */
5001 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
5002 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01005003 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01005004 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01005005 }
5006 }
5007
PavolVican22e88682017-02-14 22:38:18 +01005008 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01005009 (*ext)->def = e;
5010 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01005011 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01005012 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
PavolVican22e88682017-02-14 22:38:18 +01005013
PavolVicanb0d84102017-02-15 16:32:42 +01005014 if (e->argument && !(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005015 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
PavolVicanb0d84102017-02-15 16:32:42 +01005016 goto error;
5017 }
5018
PavolVican92f23622017-12-12 13:35:56 +01005019 if ((*ext)->flags & LYEXT_OPT_VALID &&
5020 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01005021 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
PavolVican92f23622017-12-12 13:35:56 +01005022 }
5023
Radek Krejci7f1d47e2017-04-12 15:29:02 +02005024 (*ext)->module = info->mod;
5025 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02005026
PavolVican22e88682017-02-14 22:38:18 +01005027 /* extension type-specific part */
5028 if (e->plugin) {
5029 etype = e->plugin->type;
5030 } else {
5031 /* default type */
5032 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01005033 }
PavolVican22e88682017-02-14 22:38:18 +01005034 switch (etype) {
5035 case LYEXT_FLAG:
5036 /* nothing change */
5037 break;
5038 case LYEXT_COMPLEX:
5039 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Michal Vasko53b7da02018-02-13 15:28:42 +01005040 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error);
PavolVican22e88682017-02-14 22:38:18 +01005041 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
5042 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01005043 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01005044 if (info->data.yang) {
5045 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01005046 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
5047 (struct lys_ext_instance_complex*)(*ext))) {
5048 goto error;
5049 }
5050 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
5051 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01005052 goto error;
5053 }
PavolVicana3876672017-02-21 15:49:51 +01005054 }
5055 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
5056 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01005057 }
PavolVican22e88682017-02-14 22:38:18 +01005058 break;
5059 case LYEXT_ERR:
5060 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01005061 LOGINT(ctx);
PavolVican22e88682017-02-14 22:38:18 +01005062 goto error;
5063 }
5064
PavolVican22e88682017-02-14 22:38:18 +01005065 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
5066 goto error;
5067 }
5068 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01005069 }
5070
5071 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01005072error:
5073 free(ext_prefix);
5074 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01005075}
5076
Michal Vasko730dfdf2015-08-11 14:48:05 +02005077/**
Pavol Vican855ca622016-09-05 13:07:54 +02005078 * @brief Resolve (find) choice default case. Does not log.
5079 *
5080 * @param[in] choic Choice to use.
5081 * @param[in] dflt Name of the default case.
5082 *
5083 * @return Pointer to the default node or NULL.
5084 */
5085static struct lys_node *
5086resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
5087{
5088 struct lys_node *child, *ret;
5089
5090 LY_TREE_FOR(choic->child, child) {
5091 if (child->nodetype == LYS_USES) {
5092 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
5093 if (ret) {
5094 return ret;
5095 }
5096 }
5097
5098 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02005099 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02005100 return child;
5101 }
5102 }
5103
5104 return NULL;
5105}
5106
5107/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02005108 * @brief Resolve uses, apply augments, refines. Logs directly.
5109 *
Michal Vaskobb211122015-08-19 14:03:11 +02005110 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005111 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005112 *
Michal Vaskodef0db12015-10-07 13:22:48 +02005113 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005114 */
Michal Vasko184521f2015-09-24 13:14:26 +02005115static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005116resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005117{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005118 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02005119 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02005120 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02005121 struct lys_node_leaflist *llist;
5122 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02005123 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005124 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005125 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005126 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005127 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005128 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005129
Michal Vasko71e1aa82015-08-12 12:17:51 +02005130 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01005131
Radek Krejci93def382017-05-24 15:33:48 +02005132 /* check that the grouping is resolved (no unresolved uses inside) */
5133 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02005134
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005135 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01005136 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01005137 if (node_aux->nodetype & LYS_GROUPING) {
5138 /* do not instantiate groupings from groupings */
5139 continue;
5140 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01005141 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01005142 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005143 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
5144 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005145 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005146 }
Pavol Vican55abd332016-07-12 15:54:49 +02005147 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01005148 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 +02005149 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005150 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02005151 }
5152 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005153 }
Michal Vaskoe022a562016-09-27 14:24:15 +02005154
Michal Vaskodef0db12015-10-07 13:22:48 +02005155 /* we managed to copy the grouping, the rest must be possible to resolve */
5156
Pavol Vican855ca622016-09-05 13:07:54 +02005157 if (uses->refine_size) {
5158 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01005159 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005160 }
5161
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005162 /* apply refines */
5163 for (i = 0; i < uses->refine_size; i++) {
5164 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01005165 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
5166 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02005167 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01005168 if (rc || !node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005169 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005170 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005171 }
5172
Radek Krejci1d82ef62015-08-07 14:44:40 +02005173 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005174 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
5175 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005176 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005177 }
Pavol Vican855ca622016-09-05 13:07:54 +02005178 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005179
5180 /* description on any nodetype */
5181 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005182 lydict_remove(ctx, node->dsc);
5183 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005184 }
5185
5186 /* reference on any nodetype */
5187 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005188 lydict_remove(ctx, node->ref);
5189 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005190 }
5191
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005192 /* config on any nodetype,
5193 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
5194 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005195 node->flags &= ~LYS_CONFIG_MASK;
5196 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005197 }
5198
5199 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02005200 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005201 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005202 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02005203 leaf = (struct lys_node_leaf *)node;
5204
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005205 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02005206 lydict_remove(ctx, leaf->dflt);
5207 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
5208
5209 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01005210 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
5211 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005212 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005213 }
Radek Krejci200bf712016-08-16 17:11:04 +02005214 } else if (node->nodetype == LYS_LEAFLIST) {
5215 /* leaf-list */
5216 llist = (struct lys_node_leaflist *)node;
5217
5218 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01005219 for (j = 0; j < llist->dflt_size; j++) {
5220 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02005221 }
5222 free(llist->dflt);
5223
5224 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02005225 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01005226 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail);
Radek Krejci200bf712016-08-16 17:11:04 +02005227 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01005228 for (j = 0; j < llist->dflt_size; j++) {
5229 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02005230 }
5231
5232 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01005233 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01005234 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01005235 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02005236 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02005237 }
5238 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005239 }
5240 }
5241
5242 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02005243 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01005244 /* remove current value */
5245 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005246
Radek Krejcibf285832017-01-26 16:05:41 +01005247 /* set new value */
5248 node->flags |= (rfn->flags & LYS_MAND_MASK);
5249
Pavol Vican855ca622016-09-05 13:07:54 +02005250 if (rfn->flags & LYS_MAND_TRUE) {
5251 /* check if node has default value */
5252 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005253 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005254 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005255 goto fail;
5256 }
5257 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005258 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005259 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005260 goto fail;
5261 }
5262 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005263 }
5264
5265 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005266 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
5267 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
5268 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005269 }
5270
5271 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005272 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005273 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005274 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005275 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005276 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005277 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005278 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005279 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005280 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005281 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005282 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005283 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005284 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005285 }
5286 }
5287
5288 /* must in leaf, leaf-list, list, container or anyxml */
5289 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005290 switch (node->nodetype) {
5291 case LYS_LEAF:
5292 old_size = &((struct lys_node_leaf *)node)->must_size;
5293 old_must = &((struct lys_node_leaf *)node)->must;
5294 break;
5295 case LYS_LEAFLIST:
5296 old_size = &((struct lys_node_leaflist *)node)->must_size;
5297 old_must = &((struct lys_node_leaflist *)node)->must;
5298 break;
5299 case LYS_LIST:
5300 old_size = &((struct lys_node_list *)node)->must_size;
5301 old_must = &((struct lys_node_list *)node)->must;
5302 break;
5303 case LYS_CONTAINER:
5304 old_size = &((struct lys_node_container *)node)->must_size;
5305 old_must = &((struct lys_node_container *)node)->must;
5306 break;
5307 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005308 case LYS_ANYDATA:
5309 old_size = &((struct lys_node_anydata *)node)->must_size;
5310 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005311 break;
5312 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01005313 LOGINT(ctx);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005314 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005315 }
5316
5317 size = *old_size + rfn->must_size;
5318 must = realloc(*old_must, size * sizeof *rfn->must);
Michal Vasko53b7da02018-02-13 15:28:42 +01005319 LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005320 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01005321 must[j].ext_size = rfn->must[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005322 lys_ext_dup(ctx, rfn->module, rfn->must[k].ext, rfn->must[k].ext_size, &rfn->must[k], LYEXT_PAR_RESTR,
Radek Krejci5138e9f2017-04-12 13:10:46 +02005323 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02005324 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
5325 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
5326 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
5327 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
5328 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02005329 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005330 }
5331
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005332 *old_must = must;
5333 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02005334
5335 /* check XPath dependencies again */
5336 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
5337 goto fail;
5338 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005339 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02005340
5341 /* if-feature in leaf, leaf-list, list, container or anyxml */
5342 if (rfn->iffeature_size) {
5343 old_size = &node->iffeature_size;
5344 old_iff = &node->iffeature;
5345
5346 size = *old_size + rfn->iffeature_size;
5347 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01005348 LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02005349 *old_iff = iff;
5350
Pavol Vican855ca622016-09-05 13:07:54 +02005351 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
5352 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005353 if (usize1) {
5354 /* there is something to duplicate */
5355 /* duplicate compiled expression */
5356 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
5357 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01005358 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005359 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005360
5361 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02005362 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01005363 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005364 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005365
Radek Krejci3a3b2002017-09-13 16:39:02 +02005366 /* duplicate extensions */
5367 iff[j].ext_size = rfn->iffeature[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005368 lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
Radek Krejci3a3b2002017-09-13 16:39:02 +02005369 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
5370 }
5371 (*old_size)++;
5372 }
5373 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005374 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005375 }
5376
5377 /* apply augments */
5378 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01005379 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005380 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005381 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005382 }
5383 }
5384
Pavol Vican855ca622016-09-05 13:07:54 +02005385 /* check refines */
5386 for (i = 0; i < uses->refine_size; i++) {
5387 node = refine_nodes[i];
5388 rfn = &uses->refine[i];
5389
5390 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005391 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02005392 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01005393 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02005394 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
5395 (rfn->flags & LYS_CONFIG_W)) {
5396 /* setting config true under config false is prohibited */
Michal Vasko53b7da02018-02-13 15:28:42 +01005397 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5398 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005399 "changing config from 'false' to 'true' is prohibited while "
5400 "the target's parent is still config 'false'.");
5401 goto fail;
5402 }
5403
5404 /* inherit config change to the target children */
5405 LY_TREE_DFS_BEGIN(node->child, next, iter) {
5406 if (rfn->flags & LYS_CONFIG_W) {
5407 if (iter->flags & LYS_CONFIG_SET) {
5408 /* config is set explicitely, go to next sibling */
5409 next = NULL;
5410 goto nextsibling;
5411 }
5412 } else { /* LYS_CONFIG_R */
5413 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
5414 /* error - we would have config data under status data */
Michal Vasko53b7da02018-02-13 15:28:42 +01005415 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5416 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005417 "changing config from 'true' to 'false' is prohibited while the target "
5418 "has still a children with explicit config 'true'.");
5419 goto fail;
5420 }
5421 }
5422 /* change config */
5423 iter->flags &= ~LYS_CONFIG_MASK;
5424 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
5425
5426 /* select next iter - modified LY_TREE_DFS_END */
5427 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5428 next = NULL;
5429 } else {
5430 next = iter->child;
5431 }
5432nextsibling:
5433 if (!next) {
5434 /* try siblings */
5435 next = iter->next;
5436 }
5437 while (!next) {
5438 /* parent is already processed, go to its sibling */
5439 iter = lys_parent(iter);
5440
5441 /* no siblings, go back through parents */
5442 if (iter == node) {
5443 /* we are done, no next element to process */
5444 break;
5445 }
5446 next = iter->next;
5447 }
5448 }
5449 }
5450
5451 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005452 if (rfn->dflt_size) {
5453 if (node->nodetype == LYS_CHOICE) {
5454 /* choice */
5455 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5456 rfn->dflt[0]);
5457 if (!((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005458 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005459 goto fail;
5460 }
5461 if (lyp_check_mandatory_choice(node)) {
5462 goto fail;
5463 }
Pavol Vican855ca622016-09-05 13:07:54 +02005464 }
5465 }
5466
5467 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02005468 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005469 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005470 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5471 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005472 goto fail;
5473 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005474 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005475 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005476 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5477 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005478 goto fail;
5479 }
5480 }
5481
5482 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005483 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005484 if (node->nodetype == LYS_LEAFLIST) {
5485 llist = (struct lys_node_leaflist *)node;
5486 if (llist->dflt_size && llist->min) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005487 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5488 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005489 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5490 goto fail;
5491 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005492 } else if (node->nodetype == LYS_LEAF) {
5493 leaf = (struct lys_node_leaf *)node;
5494 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005495 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5496 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005497 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5498 goto fail;
5499 }
Pavol Vican855ca622016-09-05 13:07:54 +02005500 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005501
Pavol Vican855ca622016-09-05 13:07:54 +02005502 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005503 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005504 for (parent = node->parent;
5505 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5506 parent = parent->parent) {
5507 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5508 /* stop also on presence containers */
5509 break;
5510 }
5511 }
5512 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5513 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5514 if (lyp_check_mandatory_choice(parent)) {
5515 goto fail;
5516 }
5517 }
5518 }
5519 }
5520 free(refine_nodes);
5521
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005522 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005523
5524fail:
5525 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5526 lys_node_free(iter, NULL, 0);
5527 }
Pavol Vican855ca622016-09-05 13:07:54 +02005528 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005529 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005530}
5531
Radek Krejci83a4bac2017-02-07 15:53:04 +01005532void
5533resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005534{
5535 int i;
5536
5537 assert(der && base);
5538
Radek Krejci018f1f52016-08-03 16:01:20 +02005539 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005540 /* create a set for backlinks if it does not exist */
5541 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005542 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005543 /* store backlink */
5544 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005545
Radek Krejci85a54be2016-10-20 12:39:56 +02005546 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005547 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005548 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005549 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005550}
5551
Michal Vasko730dfdf2015-08-11 14:48:05 +02005552/**
5553 * @brief Resolve base identity recursively. Does not log.
5554 *
5555 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005556 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005557 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005558 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005559 *
Radek Krejci219fa612016-08-15 10:36:51 +02005560 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005561 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005562static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005563resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005564 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005565{
Michal Vaskof02e3742015-08-05 16:27:02 +02005566 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005567 struct lys_ident *base = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005568 struct ly_ctx *ctx = module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005569
Radek Krejcicf509982015-12-15 09:22:44 +01005570 assert(ret);
5571
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005572 /* search module */
5573 for (i = 0; i < module->ident_size; i++) {
5574 if (!strcmp(basename, module->ident[i].name)) {
5575
5576 if (!ident) {
5577 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005578 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005579 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005580 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005581 }
5582
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005583 base = &module->ident[i];
5584 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005585 }
5586 }
5587
5588 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005589 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5590 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5591 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005592
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005593 if (!ident) {
5594 *ret = &module->inc[j].submodule->ident[i];
5595 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005596 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005597
5598 base = &module->inc[j].submodule->ident[i];
5599 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005600 }
5601 }
5602 }
5603
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005604matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005605 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005606 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005607 /* is it already completely resolved? */
5608 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005609 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005610 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5611
5612 /* simple check for circular reference,
5613 * the complete check is done as a side effect of using only completely
5614 * resolved identities (previous check of unres content) */
5615 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005616 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5617 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005618 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005619 }
5620
Radek Krejci06f64ed2016-08-15 11:07:44 +02005621 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005622 }
5623 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005624
Radek Krejcibabbff82016-02-19 13:31:37 +01005625 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005626 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005627 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005628 }
5629
Radek Krejci219fa612016-08-15 10:36:51 +02005630 /* base not found (maybe a forward reference) */
5631 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005632}
5633
Michal Vasko730dfdf2015-08-11 14:48:05 +02005634/**
5635 * @brief Resolve base identity. Logs directly.
5636 *
5637 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005638 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005639 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005640 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005641 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005642 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005643 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005644 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005645static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005646resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005647 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005648{
5649 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005650 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005651 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005652 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005653 struct lys_module *mod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005654 struct ly_ctx *ctx = module->ctx;
Radek Krejcicf509982015-12-15 09:22:44 +01005655
5656 assert((ident && !type) || (!ident && type));
5657
5658 if (!type) {
5659 /* have ident to resolve */
5660 ret = &target;
5661 flags = ident->flags;
5662 mod = ident->module;
5663 } else {
5664 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005665 ++type->info.ident.count;
5666 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 +01005667 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005668
5669 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005670 flags = type->parent->flags;
5671 mod = type->parent->module;
5672 }
Michal Vaskof2006002016-04-21 16:28:15 +02005673 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005674
5675 /* search for the base identity */
5676 name = strchr(basename, ':');
5677 if (name) {
5678 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005679 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005680 name++;
5681
Michal Vasko2d851a92015-10-20 16:16:36 +02005682 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005683 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005684 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005685 }
5686 } else {
5687 name = basename;
5688 }
5689
Radek Krejcic071c542016-01-27 14:57:51 +01005690 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005691 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005692 if (!module) {
5693 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01005694 LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005695 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005696 }
5697
Radek Krejcic071c542016-01-27 14:57:51 +01005698 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005699 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5700 if (!rc) {
5701 assert(*ret);
5702
5703 /* check status */
5704 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5705 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5706 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005707 } else if (ident) {
5708 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005709 if (lys_main_module(mod)->implemented) {
5710 /* in case of the implemented identity, maintain backlinks to it
5711 * from the base identities to make it available when resolving
5712 * data with the identity values (not implemented identity is not
5713 * allowed as an identityref value). */
5714 resolve_identity_backlink_update(ident, *ret);
5715 }
Radek Krejci219fa612016-08-15 10:36:51 +02005716 }
5717 } else if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005718 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005719 if (type) {
5720 --type->info.ident.count;
5721 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005722 }
5723
Radek Krejci219fa612016-08-15 10:36:51 +02005724 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005725}
5726
Radek Krejci9e6af732017-04-27 14:40:25 +02005727/*
5728 * 1 - true (der is derived from base)
5729 * 0 - false (der is not derived from base)
5730 */
5731static int
5732search_base_identity(struct lys_ident *der, struct lys_ident *base)
5733{
5734 int i;
5735
5736 if (der == base) {
5737 return 1;
5738 } else {
5739 for(i = 0; i < der->base_size; i++) {
5740 if (search_base_identity(der->base[i], base) == 1) {
5741 return 1;
5742 }
5743 }
5744 }
5745
5746 return 0;
5747}
5748
Michal Vasko730dfdf2015-08-11 14:48:05 +02005749/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005750 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005751 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005752 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005753 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005754 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005755 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005756 *
5757 * @return Pointer to the identity resolvent, NULL on error.
5758 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005759struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005760resolve_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 +02005761{
Radek Krejci9e6af732017-04-27 14:40:25 +02005762 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005763 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005764 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005765 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005766 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005767 struct lys_ident *der, *cur;
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005768 struct lys_module *imod = NULL, *m, *tmod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005769 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005770
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005771 assert(type && ident_name && mod);
Michal Vasko53b7da02018-02-13 15:28:42 +01005772 ctx = mod->ctx;
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005773
Michal Vaskof2d43962016-09-02 11:10:16 +02005774 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005775 return NULL;
5776 }
5777
Michal Vasko50576712017-07-28 12:28:33 +02005778 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005779 if (rc < 1) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005780 LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[-rc], &ident_name[-rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005781 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005782 } else if (rc < (signed)strlen(ident_name)) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005783 LOGVAL(ctx, LYE_INCHAR, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, ident_name[rc], &ident_name[rc]);
Michal Vaskofb0873c2015-08-21 09:00:07 +02005784 return NULL;
5785 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005786
5787 m = lys_main_module(mod); /* shortcut */
5788 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5789 /* identity is defined in the same module as node */
5790 imod = m;
5791 } else if (dflt) {
5792 /* solving identityref in default definition in schema -
5793 * find the identity's module in the imported modules list to have a correct revision */
5794 for (i = 0; i < mod->imp_size; i++) {
5795 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5796 imod = mod->imp[i].module;
5797 break;
5798 }
5799 }
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005800
5801 /* We may need to pull it from the module that the typedef came from */
5802 if (!imod && type && type->der) {
5803 tmod = type->der->module;
5804 for (i = 0; i < tmod->imp_size; i++) {
5805 if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) {
5806 imod = tmod->imp[i].module;
5807 break;
5808 }
5809 }
5810 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005811 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005812 /* solving identityref in data - get the module from the context */
5813 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5814 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005815 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005816 break;
5817 }
Michal Vasko08767f72017-10-06 14:38:08 +02005818 imod = NULL;
5819 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005820 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5821 /* we are currently parsing some module and checking XPath or a default value,
5822 * so take this module into account */
5823 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5824 imod = mod->ctx->models.parsing_sub_modules[i];
5825 if (imod->type) {
5826 /* skip submodules */
5827 continue;
5828 }
5829 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5830 break;
5831 }
5832 imod = NULL;
5833 }
5834 }
Michal Vasko08767f72017-10-06 14:38:08 +02005835 }
5836
Michal Vasko53b7da02018-02-13 15:28:42 +01005837 if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) {
Michal Vasko08767f72017-10-06 14:38:08 +02005838 /* the needed module was not found, but it may have been expected so call the data callback */
5839 if (imod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005840 ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005841 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005842 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01005843 imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
Michal Vasko08767f72017-10-06 14:38:08 +02005844 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005845 }
5846 }
5847 if (!imod) {
5848 goto fail;
5849 }
5850
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005851 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005852 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005853 * THEN, we may need to make the module with the identity implemented, but only if it really
5854 * contains the identity */
5855 if (!imod->implemented) {
5856 cur = NULL;
5857 /* get the identity in the module */
5858 for (i = 0; i < imod->ident_size; i++) {
5859 if (!strcmp(name, imod->ident[i].name)) {
5860 cur = &imod->ident[i];
5861 break;
5862 }
5863 }
5864 if (!cur) {
5865 /* go through includes */
5866 for (j = 0; j < imod->inc_size; j++) {
5867 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5868 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5869 cur = &imod->inc[j].submodule->ident[i];
5870 break;
5871 }
5872 }
5873 }
5874 if (!cur) {
5875 goto fail;
5876 }
5877 }
5878
5879 /* check that identity is derived from one of the type's base */
5880 while (type->der) {
5881 for (i = 0; i < type->info.ident.count; i++) {
5882 if (search_base_identity(cur, type->info.ident.ref[i])) {
5883 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005884 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005885 goto match;
5886 }
5887 }
5888 type = &type->der->type;
5889 }
5890 /* matching base not found */
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005891 LOGVAL(ctx, LYE_SPEC, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "Identity used as identityref value is not implemented.");
Radek Krejci9e6af732017-04-27 14:40:25 +02005892 goto fail;
5893 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005894 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005895
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005896 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005897 while (type->der) {
5898 for (i = 0; i < type->info.ident.count; ++i) {
5899 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005900
Radek Krejci85a54be2016-10-20 12:39:56 +02005901 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005902 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005903 for (j = 0; j < cur->der->number; j++) {
5904 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005905 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005906 /* we have match */
5907 cur = der;
5908 goto match;
5909 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005910 }
5911 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005912 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005913 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005914 }
5915
Radek Krejci9e6af732017-04-27 14:40:25 +02005916fail:
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005917 LOGVAL(ctx, LYE_INRESOLV, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005918 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005919
5920match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005921 for (i = 0; i < cur->iffeature_size; i++) {
5922 if (!resolve_iffeature(&cur->iffeature[i])) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005923 if (node) {
5924 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
5925 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005926 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 +02005927 return NULL;
5928 }
5929 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005930 if (need_implemented) {
5931 if (dflt) {
Michal Vasko0f437062018-06-08 15:52:39 +02005932 /* later try to make the module implemented */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005933 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5934 imod->name, cur->name, mod->name);
Michal Vasko0f437062018-06-08 15:52:39 +02005935 /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of
5936 * several functions with little gain */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005937 if (lys_set_implemented(imod)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005938 LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005939 imod->name, cur->name);
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005940 goto fail;
5941 }
5942 } else {
5943 /* just say that it was found, but in a non-implemented module */
Michal Vasko53b7da02018-02-13 15:28:42 +01005944 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005945 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005946 goto fail;
5947 }
5948 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005949 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005950}
5951
Michal Vasko730dfdf2015-08-11 14:48:05 +02005952/**
Michal Vaskobb211122015-08-19 14:03:11 +02005953 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005954 *
Michal Vaskobb211122015-08-19 14:03:11 +02005955 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005956 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005957 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005958 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005959 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005960static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005961resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005962{
Radek Krejci93def382017-05-24 15:33:48 +02005963 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005964 struct lys_node *par_grp;
Michal Vasko53b7da02018-02-13 15:28:42 +01005965 struct ly_ctx *ctx = uses->module->ctx;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005966
Radek Krejci6ff885d2017-01-03 14:06:22 +01005967 /* 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 +02005968 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5969 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5970 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005971 for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
Michal Vaskoa9792722018-06-28 09:01:02 +02005972 if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) {
5973 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
5974 return -1;
5975 }
Michal Vaskoe91afce2015-08-12 12:21:00 +02005976
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005977 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005978 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5979 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005980 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005981 return -1;
5982 } else if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005983 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005984 return -1;
5985 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005986 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005987 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005988 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005989 return -1;
5990 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005991 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005992 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005993 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005994 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005995 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005996 }
5997
Radek Krejci93def382017-05-24 15:33:48 +02005998 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005999 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02006000 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006001 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006002 return -1;
6003 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006004 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02006005 } else {
6006 /* instantiate grouping only when it is completely resolved */
6007 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02006008 }
Michal Vaskoa9792722018-06-28 09:01:02 +02006009 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006010 return EXIT_FAILURE;
6011 }
6012
Radek Krejci48464ed2016-03-17 15:44:09 +01006013 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006014 if (!rc) {
6015 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01006016 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02006017 assert(((struct lys_node_grp *)par_grp)->unres_count);
6018 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01006019 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006020 }
Radek Krejcicf509982015-12-15 09:22:44 +01006021
6022 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01006023 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01006024 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006025 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01006026 return -1;
6027 }
6028
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006029 return EXIT_SUCCESS;
6030 }
6031
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006032 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006033}
6034
Michal Vasko730dfdf2015-08-11 14:48:05 +02006035/**
Michal Vasko9957e592015-08-17 15:04:09 +02006036 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006037 *
Michal Vaskobb211122015-08-19 14:03:11 +02006038 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006039 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006040 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006041 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006042 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006043static int
Radek Krejci48464ed2016-03-17 15:44:09 +01006044resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006045{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006046 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01006047 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02006048 char *s = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01006049 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006050
6051 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02006052 assert(keys_str);
6053
Radek Krejci5c08a992016-11-02 13:30:04 +01006054 if (!list->child) {
6055 /* no child, possible forward reference */
Michal Vasko53b7da02018-02-13 15:28:42 +01006056 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Radek Krejci5c08a992016-11-02 13:30:04 +01006057 return EXIT_FAILURE;
6058 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006059 /* get the key name */
6060 if ((value = strpbrk(keys_str, " \t\n"))) {
6061 len = value - keys_str;
6062 while (isspace(value[0])) {
6063 value++;
6064 }
6065 } else {
6066 len = strlen(keys_str);
6067 }
6068
Michal Vaskobb520442017-05-23 10:55:18 +02006069 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
6070 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006071 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006072 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02006073 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006074 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006075
Radek Krejci48464ed2016-03-17 15:44:09 +01006076 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006077 /* check_key logs */
6078 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006079 }
6080
Radek Krejcicf509982015-12-15 09:22:44 +01006081 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01006082 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006083 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
6084 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01006085 return -1;
6086 }
6087
Radek Krejcia98048c2017-05-24 16:35:48 +02006088 /* default value - is ignored, keep it but print a warning */
6089 if (list->keys[i]->dflt) {
6090 /* log is not hidden only in case this resolving fails and in such a case
6091 * we cannot get here
6092 */
Michal Vasko53b7da02018-02-13 15:28:42 +01006093 assert(log_opt == ILO_STORE);
6094 log_opt = ILO_LOG;
6095 LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01006096 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Michal Vasko53b7da02018-02-13 15:28:42 +01006097 log_opt = ILO_STORE;
Radek Krejcia98048c2017-05-24 16:35:48 +02006098 free(s);
6099 }
6100
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006101 /* prepare for next iteration */
6102 while (value && isspace(value[0])) {
6103 value++;
6104 }
6105 keys_str = value;
6106 }
6107
Michal Vaskof02e3742015-08-05 16:27:02 +02006108 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006109}
6110
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006111/**
Michal Vaskobf19d252015-10-08 15:39:17 +02006112 * @brief Resolve (check) all must conditions of \p node.
6113 * Logs directly.
6114 *
6115 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006116 * @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 +02006117 *
6118 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
6119 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006120static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006121resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02006122{
Michal Vaskobf19d252015-10-08 15:39:17 +02006123 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006124 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02006125 struct lys_restr *must;
6126 struct lyxp_set set;
Michal Vasko53b7da02018-02-13 15:28:42 +01006127 struct ly_ctx *ctx = node->schema->module->ctx;
Michal Vaskobf19d252015-10-08 15:39:17 +02006128
6129 assert(node);
6130 memset(&set, 0, sizeof set);
6131
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006132 if (inout_parent) {
6133 for (schema = lys_parent(node->schema);
6134 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
6135 schema = lys_parent(schema));
6136 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006137 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006138 return -1;
6139 }
6140 must_size = ((struct lys_node_inout *)schema)->must_size;
6141 must = ((struct lys_node_inout *)schema)->must;
6142
6143 /* context node is the RPC/action */
6144 node = node->parent;
6145 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006146 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006147 return -1;
6148 }
6149 } else {
6150 switch (node->schema->nodetype) {
6151 case LYS_CONTAINER:
6152 must_size = ((struct lys_node_container *)node->schema)->must_size;
6153 must = ((struct lys_node_container *)node->schema)->must;
6154 break;
6155 case LYS_LEAF:
6156 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
6157 must = ((struct lys_node_leaf *)node->schema)->must;
6158 break;
6159 case LYS_LEAFLIST:
6160 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
6161 must = ((struct lys_node_leaflist *)node->schema)->must;
6162 break;
6163 case LYS_LIST:
6164 must_size = ((struct lys_node_list *)node->schema)->must_size;
6165 must = ((struct lys_node_list *)node->schema)->must;
6166 break;
6167 case LYS_ANYXML:
6168 case LYS_ANYDATA:
6169 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
6170 must = ((struct lys_node_anydata *)node->schema)->must;
6171 break;
6172 case LYS_NOTIF:
6173 must_size = ((struct lys_node_notif *)node->schema)->must_size;
6174 must = ((struct lys_node_notif *)node->schema)->must;
6175 break;
6176 default:
6177 must_size = 0;
6178 break;
6179 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006180 }
6181
6182 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006183 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02006184 return -1;
6185 }
6186
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006187 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02006188
Michal Vasko8146d4c2016-05-09 15:50:29 +02006189 if (!set.val.bool) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01006190 if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006191 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
6192 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006193 LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006194 if (must[i].emsg) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006195 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006196 }
6197 if (must[i].eapptag) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006198 ly_err_last_set_apptag(ctx, must[i].eapptag);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006199 }
6200 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02006201 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006202 }
6203 }
6204
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006205 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02006206}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006207
Michal Vaskobf19d252015-10-08 15:39:17 +02006208/**
Michal Vasko508a50d2016-09-07 14:50:33 +02006209 * @brief Resolve (find) when condition schema context node. Does not log.
6210 *
6211 * @param[in] schema Schema node with the when condition.
6212 * @param[out] ctx_snode When schema context node.
6213 * @param[out] ctx_snode_type Schema context node type.
6214 */
6215void
6216resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
6217{
6218 const struct lys_node *sparent;
6219
6220 /* find a not schema-only node */
6221 *ctx_snode_type = LYXP_NODE_ELEM;
6222 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
6223 if (schema->nodetype == LYS_AUGMENT) {
6224 sparent = ((struct lys_node_augment *)schema)->target;
6225 } else {
6226 sparent = schema->parent;
6227 }
6228 if (!sparent) {
6229 /* context node is the document root (fake root in our case) */
6230 if (schema->flags & LYS_CONFIG_W) {
6231 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
6232 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02006233 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02006234 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02006235 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskocb45f472018-02-12 10:47:42 +01006236 schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko508a50d2016-09-07 14:50:33 +02006237 break;
6238 }
6239 schema = sparent;
6240 }
6241
6242 *ctx_snode = (struct lys_node *)schema;
6243}
6244
6245/**
Michal Vaskocf024702015-10-08 15:01:42 +02006246 * @brief Resolve (find) when condition context node. Does not log.
6247 *
6248 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02006249 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02006250 * @param[out] ctx_node Context node.
6251 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02006252 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02006253 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02006254 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02006255static int
6256resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
6257 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006258{
Michal Vaskocf024702015-10-08 15:01:42 +02006259 struct lyd_node *parent;
6260 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006261 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02006262 uint16_t i, data_depth, schema_depth;
6263
Michal Vasko508a50d2016-09-07 14:50:33 +02006264 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02006265
Michal Vaskofe989752016-09-08 08:47:26 +02006266 if (node_type == LYXP_NODE_ELEM) {
6267 /* standard element context node */
6268 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
6269 for (sparent = schema, schema_depth = 0;
6270 sparent;
6271 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
6272 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
6273 ++schema_depth;
6274 }
Michal Vaskocf024702015-10-08 15:01:42 +02006275 }
Michal Vaskofe989752016-09-08 08:47:26 +02006276 if (data_depth < schema_depth) {
6277 return -1;
6278 }
Michal Vaskocf024702015-10-08 15:01:42 +02006279
Michal Vasko956e8542016-08-26 09:43:35 +02006280 /* find the corresponding data node */
6281 for (i = 0; i < data_depth - schema_depth; ++i) {
6282 node = node->parent;
6283 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02006284 if (node->schema != schema) {
6285 return -1;
6286 }
Michal Vaskofe989752016-09-08 08:47:26 +02006287 } else {
6288 /* root context node */
6289 while (node->parent) {
6290 node = node->parent;
6291 }
6292 while (node->prev->next) {
6293 node = node->prev;
6294 }
Michal Vaskocf024702015-10-08 15:01:42 +02006295 }
6296
Michal Vaskoa59495d2016-08-22 09:18:58 +02006297 *ctx_node = node;
6298 *ctx_node_type = node_type;
6299 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02006300}
6301
Michal Vasko76c3bd32016-08-24 16:02:52 +02006302/**
6303 * @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 +01006304 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02006305 *
6306 * @param[in] snode Schema node, whose children instances need to be unlinked.
6307 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
6308 * it is moved to point to another sibling still in the original tree.
6309 * @param[in,out] ctx_node When context node, adjusted if needed.
6310 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
6311 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
6312 * Ordering may change, but there will be no semantic change.
6313 *
6314 * @return EXIT_SUCCESS on success, -1 on error.
6315 */
6316static int
6317resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
6318 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
6319{
6320 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006321 const struct lys_node *slast;
Michal Vasko53b7da02018-02-13 15:28:42 +01006322 struct ly_ctx *ctx = snode->module->ctx;
Michal Vasko76c3bd32016-08-24 16:02:52 +02006323
6324 switch (snode->nodetype) {
6325 case LYS_AUGMENT:
6326 case LYS_USES:
6327 case LYS_CHOICE:
6328 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006329 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01006330 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006331 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
6332 continue;
6333 }
6334
6335 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006336 return -1;
6337 }
6338 }
6339 break;
6340 case LYS_CONTAINER:
6341 case LYS_LIST:
6342 case LYS_LEAF:
6343 case LYS_LEAFLIST:
6344 case LYS_ANYXML:
6345 case LYS_ANYDATA:
6346 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
6347 if (elem->schema == snode) {
6348
6349 if (elem == *ctx_node) {
6350 /* We are going to unlink our context node! This normally cannot happen,
6351 * but we use normal top-level data nodes for faking a document root node,
6352 * so if this is the context node, we just use the next top-level node.
6353 * Additionally, it can even happen that there are no top-level data nodes left,
6354 * all were unlinked, so in this case we pass NULL as the context node/data tree,
6355 * lyxp_eval() can handle this special situation.
6356 */
6357 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006358 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006359 return -1;
6360 }
6361
6362 if (elem->prev == elem) {
6363 /* unlinking last top-level element, use an empty data tree */
6364 *ctx_node = NULL;
6365 } else {
6366 /* in this case just use the previous/last top-level data node */
6367 *ctx_node = elem->prev;
6368 }
6369 } else if (elem == *node) {
6370 /* We are going to unlink the currently processed node. This does not matter that
6371 * much, but we would lose access to the original data tree, so just move our
6372 * pointer somewhere still inside it.
6373 */
6374 if ((*node)->prev != *node) {
6375 *node = (*node)->prev;
6376 } else {
6377 /* the processed node with sibings were all unlinked, oh well */
6378 *node = NULL;
6379 }
6380 }
6381
6382 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01006383 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006384 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006385 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006386 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006387 return -1;
6388 }
6389 } else {
6390 *unlinked_nodes = elem;
6391 }
6392
6393 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
6394 /* there can be only one instance */
6395 break;
6396 }
6397 }
6398 }
6399 break;
6400 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006401 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006402 return -1;
6403 }
6404
6405 return EXIT_SUCCESS;
6406}
6407
6408/**
6409 * @brief Relink the unlinked nodes back.
6410 *
6411 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
6412 * we simply need a sibling from the original data tree.
6413 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
6414 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
6415 * or the sibling of \p unlinked_nodes.
6416 *
6417 * @return EXIT_SUCCESS on success, -1 on error.
6418 */
6419static int
6420resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
6421{
6422 struct lyd_node *elem;
6423
6424 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006425 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006426 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006427 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006428 return -1;
6429 }
6430 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006431 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006432 return -1;
6433 }
6434 }
6435 }
6436
6437 return EXIT_SUCCESS;
6438}
6439
Radek Krejci03b71f72016-03-16 11:10:09 +01006440int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006441resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01006442{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006443 int ret = 0;
6444 uint8_t must_size;
6445 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02006446
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006447 assert(node);
6448
6449 schema = node->schema;
6450
6451 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02006452 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01006453 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006454 must_size = ((struct lys_node_container *)schema)->must_size;
6455 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006456 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006457 must_size = ((struct lys_node_leaf *)schema)->must_size;
6458 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006459 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006460 must_size = ((struct lys_node_leaflist *)schema)->must_size;
6461 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006462 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006463 must_size = ((struct lys_node_list *)schema)->must_size;
6464 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006465 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02006466 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006467 must_size = ((struct lys_node_anydata *)schema)->must_size;
6468 break;
6469 case LYS_NOTIF:
6470 must_size = ((struct lys_node_notif *)schema)->must_size;
6471 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006472 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006473 must_size = 0;
6474 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006475 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006476
6477 if (must_size) {
6478 ++ret;
6479 }
6480
6481 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6482 if (!node->prev->next) {
6483 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6484 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6485 ret += 0x2;
6486 }
6487 }
6488
6489 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006490}
6491
Michal Vaskodd962292018-09-10 08:53:01 +02006492static struct lys_when *
6493snode_get_when(const struct lys_node *schema)
6494{
6495 switch (schema->nodetype) {
6496 case LYS_CONTAINER:
6497 return ((struct lys_node_container *)schema)->when;
6498 case LYS_CHOICE:
6499 return ((struct lys_node_choice *)schema)->when;
6500 case LYS_LEAF:
6501 return ((struct lys_node_leaf *)schema)->when;
6502 case LYS_LEAFLIST:
6503 return ((struct lys_node_leaflist *)schema)->when;
6504 case LYS_LIST:
6505 return ((struct lys_node_list *)schema)->when;
6506 case LYS_ANYDATA:
6507 case LYS_ANYXML:
6508 return ((struct lys_node_anydata *)schema)->when;
6509 case LYS_CASE:
6510 return ((struct lys_node_case *)schema)->when;
6511 case LYS_USES:
6512 return ((struct lys_node_uses *)schema)->when;
6513 case LYS_AUGMENT:
6514 return ((struct lys_node_augment *)schema)->when;
6515 default:
6516 return NULL;
6517 }
6518}
6519
Radek Krejci01696bf2016-03-18 13:19:36 +01006520int
Radek Krejci46165822016-08-26 14:06:27 +02006521resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006522{
Radek Krejci46165822016-08-26 14:06:27 +02006523 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006524
Radek Krejci46165822016-08-26 14:06:27 +02006525 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006526
Michal Vaskodd962292018-09-10 08:53:01 +02006527 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && snode_get_when(schema)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006528 return 1;
6529 }
6530
Radek Krejci46165822016-08-26 14:06:27 +02006531 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006532 goto check_augment;
6533
Radek Krejci46165822016-08-26 14:06:27 +02006534 while (parent) {
6535 /* stop conditions */
6536 if (!mode) {
6537 /* stop on node that can be instantiated in data tree */
6538 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6539 break;
6540 }
6541 } else {
6542 /* stop on the specified node */
6543 if (parent == stop) {
6544 break;
6545 }
6546 }
6547
Michal Vaskodd962292018-09-10 08:53:01 +02006548 if (snode_get_when(parent)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006549 return 1;
6550 }
6551check_augment:
6552
Michal Vaskodd962292018-09-10 08:53:01 +02006553 if (parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && snode_get_when(parent->parent)) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006554 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006555 }
6556 parent = lys_parent(parent);
6557 }
6558
6559 return 0;
6560}
6561
Michal Vaskocf024702015-10-08 15:01:42 +02006562/**
6563 * @brief Resolve (check) all when conditions relevant for \p node.
6564 * Logs directly.
6565 *
6566 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006567 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6568 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006569 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006570 * @return
6571 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006572 * 0 - all "when" statements true
6573 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006574 * 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 +02006575 */
Radek Krejci46165822016-08-26 14:06:27 +02006576int
Michal Vasko0b963112017-08-11 12:45:36 +02006577resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006578{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006579 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006580 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006581 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006582 enum lyxp_node_type ctx_node_type;
Michal Vasko53b7da02018-02-13 15:28:42 +01006583 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci51093642016-03-29 10:14:59 +02006584 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006585
6586 assert(node);
6587 memset(&set, 0, sizeof set);
6588
Michal Vaskodd962292018-09-10 08:53:01 +02006589 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && snode_get_when(node->schema)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006590 /* make the node dummy for the evaluation */
6591 node->validity |= LYD_VAL_INUSE;
Michal Vaskodd962292018-09-10 08:53:01 +02006592 rc = lyxp_eval(snode_get_when(node->schema)->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006593 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006594 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006595 if (rc) {
6596 if (rc == 1) {
Michal Vaskodd962292018-09-10 08:53:01 +02006597 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006598 }
Radek Krejci51093642016-03-29 10:14:59 +02006599 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006600 }
6601
Radek Krejci03b71f72016-03-16 11:10:09 +01006602 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006603 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006604 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006605 node->when_status |= LYD_WHEN_FALSE;
Michal Vaskodd962292018-09-10 08:53:01 +02006606 if ((ignore_fail == 1) || ((snode_get_when(node->schema)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
Michal Vaskoc04173b2018-03-09 10:43:22 +01006607 && (ignore_fail == 2))) {
Michal Vaskodd962292018-09-10 08:53:01 +02006608 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(node->schema)->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006609 } else {
Michal Vaskodd962292018-09-10 08:53:01 +02006610 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(node->schema)->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006611 if (failed_when) {
Michal Vaskodd962292018-09-10 08:53:01 +02006612 *failed_when = snode_get_when(node->schema);
Michal Vasko0b963112017-08-11 12:45:36 +02006613 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006614 goto cleanup;
6615 }
Michal Vaskocf024702015-10-08 15:01:42 +02006616 }
Radek Krejci51093642016-03-29 10:14:59 +02006617
6618 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006619 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006620 }
6621
Michal Vasko90fc2a32016-08-24 15:58:58 +02006622 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006623 goto check_augment;
6624
6625 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006626 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
Michal Vaskodd962292018-09-10 08:53:01 +02006627 if (snode_get_when(sparent)) {
Michal Vaskocf024702015-10-08 15:01:42 +02006628 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006629 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006630 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006631 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006632 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006633 }
6634 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006635
6636 unlinked_nodes = NULL;
6637 /* we do not want our node pointer to change */
6638 tmp_node = node;
6639 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6640 if (rc) {
6641 goto cleanup;
6642 }
6643
Michal Vaskodd962292018-09-10 08:53:01 +02006644 rc = lyxp_eval(snode_get_when(sparent)->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006645 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006646
6647 if (unlinked_nodes && ctx_node) {
6648 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6649 rc = -1;
6650 goto cleanup;
6651 }
6652 }
6653
Radek Krejci03b71f72016-03-16 11:10:09 +01006654 if (rc) {
6655 if (rc == 1) {
Michal Vaskodd962292018-09-10 08:53:01 +02006656 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006657 }
Radek Krejci51093642016-03-29 10:14:59 +02006658 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006659 }
6660
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006661 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006662 if (!set.val.bool) {
Michal Vaskodd962292018-09-10 08:53:01 +02006663 if ((ignore_fail == 1) || ((snode_get_when(sparent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6664 && (ignore_fail == 2))) {
6665 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.", snode_get_when(sparent)->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006666 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006667 node->when_status |= LYD_WHEN_FALSE;
Michal Vaskodd962292018-09-10 08:53:01 +02006668 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent)->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006669 if (failed_when) {
Michal Vaskodd962292018-09-10 08:53:01 +02006670 *failed_when = snode_get_when(sparent);
Michal Vasko0b963112017-08-11 12:45:36 +02006671 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006672 goto cleanup;
6673 }
Michal Vaskocf024702015-10-08 15:01:42 +02006674 }
Radek Krejci51093642016-03-29 10:14:59 +02006675
6676 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006677 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006678 }
6679
6680check_augment:
Michal Vaskodd962292018-09-10 08:53:01 +02006681 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && snode_get_when(sparent->parent))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006682 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006683 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006684 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006685 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006686 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006687 }
6688 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006689
6690 unlinked_nodes = NULL;
6691 tmp_node = node;
6692 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6693 if (rc) {
6694 goto cleanup;
6695 }
6696
Michal Vaskodd962292018-09-10 08:53:01 +02006697 rc = lyxp_eval(snode_get_when(sparent->parent)->cond, ctx_node, ctx_node_type,
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006698 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006699
6700 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6701 * so the tree did not actually change and there is nothing for us to do
6702 */
6703 if (unlinked_nodes && ctx_node) {
6704 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6705 rc = -1;
6706 goto cleanup;
6707 }
6708 }
6709
Radek Krejci03b71f72016-03-16 11:10:09 +01006710 if (rc) {
6711 if (rc == 1) {
Michal Vaskodd962292018-09-10 08:53:01 +02006712 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006713 }
Radek Krejci51093642016-03-29 10:14:59 +02006714 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006715 }
6716
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006717 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006718 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006719 node->when_status |= LYD_WHEN_FALSE;
Michal Vaskodd962292018-09-10 08:53:01 +02006720 if ((ignore_fail == 1) || ((snode_get_when(sparent->parent)->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
Michal Vaskoc04173b2018-03-09 10:43:22 +01006721 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006722 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vaskodd962292018-09-10 08:53:01 +02006723 snode_get_when(sparent->parent)->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006724 } else {
Michal Vaskodd962292018-09-10 08:53:01 +02006725 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, snode_get_when(sparent->parent)->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006726 if (failed_when) {
Michal Vaskodd962292018-09-10 08:53:01 +02006727 *failed_when = snode_get_when(sparent->parent);
Michal Vasko0b963112017-08-11 12:45:36 +02006728 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006729 goto cleanup;
6730 }
Michal Vaskocf024702015-10-08 15:01:42 +02006731 }
Radek Krejci51093642016-03-29 10:14:59 +02006732
6733 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006734 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006735 }
6736
Michal Vasko90fc2a32016-08-24 15:58:58 +02006737 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006738 }
6739
Radek Krejci0b7704f2016-03-18 12:16:14 +01006740 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006741
Radek Krejci51093642016-03-29 10:14:59 +02006742cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006743 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006744 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006745 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006746}
6747
Radek Krejcicbb473e2016-09-16 14:48:32 +02006748static int
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006749check_type_union_leafref(struct lys_type *type)
6750{
6751 uint8_t i;
6752
6753 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6754 /* go through unions and look for leafref */
6755 for (i = 0; i < type->info.uni.count; ++i) {
6756 switch (type->info.uni.types[i].base) {
6757 case LY_TYPE_LEAFREF:
6758 return 1;
6759 case LY_TYPE_UNION:
6760 if (check_type_union_leafref(&type->info.uni.types[i])) {
6761 return 1;
6762 }
6763 break;
6764 default:
6765 break;
6766 }
6767 }
6768
6769 return 0;
6770 }
6771
6772 /* just inherit the flag value */
6773 return type->der->has_union_leafref;
6774}
6775
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006776/**
Michal Vaskobb211122015-08-19 14:03:11 +02006777 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006778 *
6779 * @param[in] mod Main module.
6780 * @param[in] item Item to resolve. Type determined by \p type.
6781 * @param[in] type Type of the unresolved item.
6782 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006783 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006784 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006785 *
6786 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6787 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006788static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006789resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006790 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006791{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006792 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vasko53b7da02018-02-13 15:28:42 +01006793 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006794 unsigned int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01006795 struct ly_ctx * ctx = mod->ctx;
Radek Krejci80056d52017-01-05 13:13:33 +01006796 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006797 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006798 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006799
Radek Krejcic79c6b12016-07-26 15:11:49 +02006800 struct ly_set *refs, *procs;
6801 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006802 struct lys_ident *ident;
6803 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006804 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006805 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006806 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006807 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006808 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006809 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006810 struct lys_ext_instance *ext, **extlist;
6811 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006812
6813 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006814 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006815 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006816 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006817 ident = item;
6818
Radek Krejci018f1f52016-08-03 16:01:20 +02006819 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006820 break;
6821 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006822 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006823 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006824 stype = item;
6825
Radek Krejci018f1f52016-08-03 16:01:20 +02006826 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006827 break;
6828 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006829 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006830 stype = item;
6831
Michal Vasko74083ec2018-06-15 10:00:12 +02006832 rc = resolve_schema_leafref(stype, node, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006833 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006834 case UNRES_TYPE_DER_EXT:
6835 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006836 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006837 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006838 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006839 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006840 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006841 /* parent */
6842 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006843 stype = item;
6844
Michal Vasko88c29542015-11-27 14:57:53 +01006845 /* HACK type->der is temporarily unparsed type statement */
6846 yin = (struct lyxml_elem *)stype->der;
6847 stype->der = NULL;
6848
Pavol Vicana0e4e672016-02-24 12:20:04 +01006849 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6850 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006851 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006852
6853 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006854 /* may try again later */
6855 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006856 } else {
6857 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006858 lydict_remove(ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006859 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006860 }
6861
Michal Vasko88c29542015-11-27 14:57:53 +01006862 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006863 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006864 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006865 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006866 lyxml_free(ctx, yin);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006867 } else {
6868 /* may try again later, put all back how it was */
6869 stype->der = (struct lys_tpdf *)yin;
6870 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006871 }
Radek Krejcic13db382016-08-16 10:52:42 +02006872 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006873 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006874 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006875 LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
Radek Krejci2c2fce82016-08-01 13:52:26 +02006876 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006877
6878 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6879 /* fill typedef union leafref flag */
6880 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6881 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6882 /* copy the type in case it has union leafref flag */
6883 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006884 LOGERR(ctx, LY_EINT, "Failed to duplicate type.");
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006885 return -1;
6886 }
6887 }
Michal Vasko101658e2018-06-05 15:05:54 +02006888 } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) {
Radek Krejcic13db382016-08-16 10:52:42 +02006889 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6890 * 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 +02006891 * grouping. The grouping cannot be used unless the unres counter is 0.
Michal Vasko70bf8e52018-03-26 11:32:33 +02006892 * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006893 * of the type's base member. */
6894 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6895 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006896 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006897 LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006898 return -1;
6899 }
Michal Vasko101658e2018-06-05 15:05:54 +02006900 stype->value_flags |= LY_VALUE_UNRESGRP;
Radek Krejcic13db382016-08-16 10:52:42 +02006901 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006902 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006903 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006904 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006905 iff_data = str_snode;
6906 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006907 if (!rc) {
6908 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006909 if (iff_data->infeature) {
6910 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6911 feat = *((struct lys_feature **)item);
6912 if (!feat->depfeatures) {
6913 feat->depfeatures = ly_set_new();
6914 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006915 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006916 }
6917 /* cleanup temporary data */
Michal Vasko53b7da02018-02-13 15:28:42 +01006918 lydict_remove(ctx, iff_data->fname);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006919 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006920 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006921 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006922 case UNRES_FEATURE:
6923 feat = (struct lys_feature *)item;
6924
6925 if (feat->iffeature_size) {
6926 refs = ly_set_new();
6927 procs = ly_set_new();
6928 ly_set_add(procs, feat, 0);
6929
6930 while (procs->number) {
6931 ref = procs->set.g[procs->number - 1];
6932 ly_set_rm_index(procs, procs->number - 1);
6933
6934 for (i = 0; i < ref->iffeature_size; i++) {
6935 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6936 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006937 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006938 if (ref->iffeature[i].features[j - 1] == feat) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006939 LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006940 goto featurecheckdone;
6941 }
6942
6943 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6944 k = refs->number;
6945 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6946 /* not yet seen feature, add it for processing */
6947 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6948 }
6949 }
6950 } else {
6951 /* forward reference */
6952 rc = EXIT_FAILURE;
6953 goto featurecheckdone;
6954 }
6955 }
6956
6957 }
6958 }
6959 rc = EXIT_SUCCESS;
6960
6961featurecheckdone:
6962 ly_set_free(refs);
6963 ly_set_free(procs);
6964 }
6965
6966 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006967 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006968 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006969 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006970 case UNRES_TYPEDEF_DFLT:
6971 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006972 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006973 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006974 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006975 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006976 break;
6977 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006978 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006979 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006980 choic = item;
6981
Radek Krejcie00d2312016-08-12 15:27:49 +02006982 if (!choic->dflt) {
6983 choic->dflt = resolve_choice_dflt(choic, expr);
6984 }
Michal Vasko7955b362015-09-04 14:18:15 +02006985 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006986 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006987 } else {
6988 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006989 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006990 break;
6991 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006992 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006993 break;
6994 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006995 unique_info = (struct unres_list_uniq *)item;
6996 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006997 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006998 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006999 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01007000 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02007001 case UNRES_XPATH:
7002 node = (struct lys_node *)item;
Michal Vasko895c11f2018-03-12 11:35:58 +01007003 rc = check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02007004 break;
Michal Vasko0f437062018-06-08 15:52:39 +02007005 case UNRES_MOD_IMPLEMENT:
7006 rc = lys_make_implemented_r(mod, unres);
7007 break;
Radek Krejcie534c132016-11-23 13:32:31 +01007008 case UNRES_EXT:
7009 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01007010 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01007011 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01007012 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01007013 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01007014 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01007015 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01007016 if (eplugin) {
7017 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01007018 u = malloc(sizeof *u);
Michal Vasko53b7da02018-02-13 15:28:42 +01007019 LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01007020 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01007021 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
7022 /* something really bad happend since the extension finalization is not actually
7023 * being resolved while adding into unres, so something more serious with the unres
7024 * list itself must happened */
7025 return -1;
7026 }
Radek Krejci80056d52017-01-05 13:13:33 +01007027 }
7028 }
Radek Krejci79685c92017-02-17 10:49:43 +01007029 }
7030 if (!rc || rc == -1) {
7031 /* cleanup on success or fatal error */
7032 if (ext_data->datatype == LYS_IN_YIN) {
7033 /* YIN */
Michal Vasko53b7da02018-02-13 15:28:42 +01007034 lyxml_free(ctx, ext_data->data.yin);
Radek Krejci79685c92017-02-17 10:49:43 +01007035 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01007036 /* YANG */
7037 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01007038 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01007039 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01007040 }
7041 break;
Radek Krejci80056d52017-01-05 13:13:33 +01007042 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01007043 u = (uint8_t *)str_snode;
7044 ext = (*(struct lys_ext_instance ***)item)[*u];
7045 free(u);
7046
Radek Krejci80056d52017-01-05 13:13:33 +01007047 eplugin = ext->def->plugin;
7048
7049 /* inherit */
7050 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
7051 root = (struct lys_node *)ext->parent;
7052 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
7053 LY_TREE_DFS_BEGIN(root->child, next, node) {
7054 /* first, check if the node already contain instance of the same extension,
7055 * in such a case we won't inherit. In case the node was actually defined as
7056 * augment data, we are supposed to check the same way also the augment node itself */
7057 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
7058 goto inherit_dfs_sibling;
7059 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
7060 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
7061 goto inherit_dfs_sibling;
7062 }
7063
7064 if (eplugin->check_inherit) {
7065 /* we have a callback to check the inheritance, use it */
7066 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
7067 case 0:
7068 /* yes - continue with the inheriting code */
7069 break;
7070 case 1:
7071 /* no - continue with the node's sibling */
7072 goto inherit_dfs_sibling;
7073 case 2:
7074 /* no, but continue with the children, just skip the inheriting code for this node */
7075 goto inherit_dfs_child;
7076 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007077 LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
Radek Krejci80056d52017-01-05 13:13:33 +01007078 ext->def->module->name, ext->def->name, rc);
7079 }
7080 }
7081
7082 /* inherit the extension */
7083 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01007084 LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007085 extlist[node->ext_size] = malloc(sizeof **extlist);
Michal Vasko53b7da02018-02-13 15:28:42 +01007086 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007087 memcpy(extlist[node->ext_size], ext, sizeof *ext);
7088 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
7089
7090 node->ext = extlist;
7091 node->ext_size++;
7092
7093inherit_dfs_child:
7094 /* modification of - select element for the next run - children first */
7095 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
7096 next = NULL;
7097 } else {
7098 next = node->child;
7099 }
7100 if (!next) {
7101inherit_dfs_sibling:
7102 /* no children, try siblings */
7103 next = node->next;
7104 }
7105 while (!next) {
7106 /* go to the parent */
7107 node = lys_parent(node);
7108
7109 /* we are done if we are back in the root (the starter's parent */
7110 if (node == root) {
7111 break;
7112 }
7113
7114 /* parent is already processed, go to its sibling */
7115 next = node->next;
7116 }
7117 }
7118 }
7119 }
7120
7121 /* final check */
7122 if (eplugin->check_result) {
7123 if ((*eplugin->check_result)(ext)) {
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007124 LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed.");
Radek Krejci80056d52017-01-05 13:13:33 +01007125 return -1;
7126 }
7127 }
7128
7129 rc = 0;
7130 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007131 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007132 LOGINT(ctx);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007133 break;
7134 }
7135
Radek Krejci54081ce2016-08-12 15:21:47 +02007136 if (has_str && !rc) {
7137 /* the string is no more needed in case of success.
7138 * In case of forward reference, we will try to resolve the string later */
Michal Vasko53b7da02018-02-13 15:28:42 +01007139 lydict_remove(ctx, str_snode);
Radek Krejci4f78b532016-02-17 13:43:00 +01007140 }
7141
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007142 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007143}
7144
Michal Vaskof02e3742015-08-05 16:27:02 +02007145/* logs directly */
7146static void
Radek Krejci48464ed2016-03-17 15:44:09 +01007147print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007148{
Michal Vaskocb34dc62016-05-20 14:38:37 +02007149 struct lyxml_elem *xml;
7150 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007151 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01007152 const char *name = NULL;
7153 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007154
Michal Vaskof02e3742015-08-05 16:27:02 +02007155 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02007156 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007157 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007158 break;
7159 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007160 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007161 break;
7162 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007163 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
7164 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02007165 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01007166 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02007167 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02007168 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02007169 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
7170 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01007171 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007172 } else {
7173 LY_TREE_FOR(xml->attr, attr) {
7174 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01007175 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007176 break;
7177 }
7178 }
7179 assert(attr);
7180 }
Radek Krejcie534c132016-11-23 13:32:31 +01007181 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007182 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007183 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007184 iff_data = str_node;
7185 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02007186 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02007187 case UNRES_FEATURE:
7188 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
7189 ((struct lys_feature *)item)->name);
7190 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007191 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01007192 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007193 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01007194 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02007195 case UNRES_TYPE_DFLT:
Michal Vaskoba835cd2018-02-23 09:25:48 +01007196 if (*(char **)str_node) {
7197 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node);
Radek Krejci2e2de832016-10-13 16:12:26 +02007198 } /* else no default value in the type itself, but we are checking some restrictions against
7199 * possible default value of some base type. The failure is caused by not resolved base type,
7200 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02007201 break;
7202 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007203 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007204 break;
7205 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01007206 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007207 break;
7208 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01007209 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007210 break;
Michal Vasko7178e692016-02-12 15:58:05 +01007211 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007212 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
7213 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01007214 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02007215 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01007216 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
7217 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02007218 break;
Radek Krejcie534c132016-11-23 13:32:31 +01007219 case UNRES_EXT:
7220 extinfo = (struct unres_ext *)str_node;
7221 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
7222 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
7223 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007224 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007225 LOGINT(NULL);
Michal Vaskof02e3742015-08-05 16:27:02 +02007226 break;
7227 }
7228}
7229
Michal Vasko0f437062018-06-08 15:52:39 +02007230static int
7231resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref,
7232 int print_all_errors, uint32_t *resolved)
7233{
7234 uint32_t i, unres_count, res_count;
7235 int ret = 0, rc;
7236 struct ly_err_item *prev_eitem;
7237 enum int_log_opts prev_ilo;
7238 LY_ERR prev_ly_errno;
7239
7240 /* if there can be no forward references, every failure is final, so we can print it directly */
7241 if (forward_ref) {
7242 prev_ly_errno = ly_errno;
7243 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
7244 }
7245
7246 do {
7247 unres_count = 0;
7248 res_count = 0;
7249
7250 for (i = 0; i < unres->count; ++i) {
7251 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7252 * if-features are resolved here to make sure that we will have all if-features for
7253 * later check of feature circular dependency */
7254 if (unres->type[i] & types) {
7255 ++unres_count;
7256 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7257 if (unres->type[i] == UNRES_EXT_FINALIZE) {
7258 /* to avoid double free */
7259 unres->type[i] = UNRES_RESOLVED;
7260 }
7261 if (!rc || (unres->type[i] == UNRES_XPATH)) {
7262 /* invalid XPath can never cause an error, only a warning */
7263 if (unres->type[i] == UNRES_LIST_UNIQ) {
7264 /* free the allocated structure */
7265 free(unres->item[i]);
7266 }
7267
7268 unres->type[i] = UNRES_RESOLVED;
7269 ++(*resolved);
7270 ++res_count;
7271 } else if ((rc == EXIT_FAILURE) && forward_ref) {
7272 /* forward reference, erase errors */
7273 ly_err_free_next(ctx, prev_eitem);
7274 } else if (print_all_errors) {
7275 /* just so that we quit the loop */
7276 ++res_count;
7277 ret = -1;
7278 } else {
Michal Vaskoffdf4ce2018-08-17 10:31:49 +02007279 if (forward_ref) {
7280 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
7281 }
Michal Vasko0f437062018-06-08 15:52:39 +02007282 return -1;
7283 }
7284 }
7285 }
7286 } while (res_count && (res_count < unres_count));
7287
7288 if (res_count < unres_count) {
7289 assert(forward_ref);
7290 /* just print the errors (but we must free the ones we have and get them again :-/ ) */
7291 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7292
7293 for (i = 0; i < unres->count; ++i) {
7294 if (unres->type[i] & types) {
7295 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7296 }
7297 }
7298 return -1;
7299 }
7300
7301 if (forward_ref) {
7302 /* restore log */
7303 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7304 ly_errno = prev_ly_errno;
7305 }
7306
7307 return ret;
7308}
7309
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007310/**
Michal Vaskobb211122015-08-19 14:03:11 +02007311 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007312 *
7313 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007314 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007315 *
Michal Vasko92b8a382015-08-19 14:03:49 +02007316 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007317 */
Michal Vaskof02e3742015-08-05 16:27:02 +02007318int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007319resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02007320{
Michal Vasko0f437062018-06-08 15:52:39 +02007321 uint32_t resolved = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007322
7323 assert(unres);
7324
Michal Vaskoe8734262016-09-29 14:12:06 +02007325 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko51054ca2015-08-12 12:20:00 +02007326
Michal Vasko0f437062018-06-08 15:52:39 +02007327 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7328 * if-features are resolved here to make sure that we will have all if-features for
7329 * later check of feature circular dependency */
7330 if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF
7331 | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT,
7332 mod->ctx, 1, 0, &resolved)) {
Michal Vasko92b8a382015-08-19 14:03:49 +02007333 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007334 }
7335
Michal Vasko0f437062018-06-08 15:52:39 +02007336 /* another batch of resolved items */
7337 if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT
7338 | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) {
7339 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007340 }
7341
Michal Vasko0f437062018-06-08 15:52:39 +02007342 /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
7343 if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) {
7344 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007345 }
7346
Michal Vaskoe8734262016-09-29 14:12:06 +02007347 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01007348 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007349 return EXIT_SUCCESS;
7350}
7351
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007352/**
Michal Vaskobb211122015-08-19 14:03:11 +02007353 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007354 *
7355 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007356 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007357 * @param[in] item Item to resolve. Type determined by \p type.
7358 * @param[in] type Type of the unresolved item.
7359 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007360 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007361 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007362 */
7363int
Radek Krejci48464ed2016-03-17 15:44:09 +01007364unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7365 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007366{
Radek Krejci54081ce2016-08-12 15:21:47 +02007367 int rc;
7368 const char *dictstr;
7369
7370 dictstr = lydict_insert(mod->ctx, str, 0);
7371 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7372
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007373 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007374 lydict_remove(mod->ctx, dictstr);
7375 }
7376 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007377}
7378
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007379/**
Michal Vaskobb211122015-08-19 14:03:11 +02007380 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007381 *
7382 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007383 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007384 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007385 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007386 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007387 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007388 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007389 */
7390int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007391unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007392 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007393{
Michal Vasko53b7da02018-02-13 15:28:42 +01007394 int rc;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007395 uint32_t u;
Michal Vasko53b7da02018-02-13 15:28:42 +01007396 enum int_log_opts prev_ilo;
7397 struct ly_err_item *prev_eitem;
7398 LY_ERR prev_ly_errno;
Michal Vasko88c29542015-11-27 14:57:53 +01007399 struct lyxml_elem *yin;
Michal Vasko53b7da02018-02-13 15:28:42 +01007400 struct ly_ctx *ctx = mod->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007401
Michal Vasko0f437062018-06-08 15:52:39 +02007402 assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID)
7403 && (type != UNRES_WHEN) && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007404
Radek Krejci850a5de2016-11-08 14:06:40 +01007405 /* check for duplicities in unres */
7406 for (u = 0; u < unres->count; u++) {
7407 if (unres->type[u] == type && unres->item[u] == item &&
7408 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007409 /* duplication can happen when the node contains multiple statements of the same type to check,
7410 * this can happen for example when refinement is being applied, so we just postpone the processing
7411 * and do not duplicate the information */
7412 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007413 }
7414 }
7415
Michal Vasko0f437062018-06-08 15:52:39 +02007416 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02007417 /* extension finalization is not even tried when adding the item into the inres list,
Michal Vasko0f437062018-06-08 15:52:39 +02007418 * xpath is not tried because it would hide some potential warnings,
7419 * implementing module must be deferred because some other nodes can be added that will need to be traversed
7420 * and their targets made implemented */
Radek Krejcic293bac2017-02-27 11:25:28 +01007421 rc = EXIT_FAILURE;
7422 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007423 prev_ly_errno = ly_errno;
7424 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007425
Michal Vasko53b7da02018-02-13 15:28:42 +01007426 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007427 if (rc != EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007428 ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0);
7429 if (rc != -1) {
7430 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007431 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007432
Radek Krejci80056d52017-01-05 13:13:33 +01007433 if (type == UNRES_LIST_UNIQ) {
7434 /* free the allocated structure */
7435 free(item);
7436 } else if (rc == -1 && type == UNRES_IFFEAT) {
7437 /* free the allocated resources */
7438 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007439 }
Radek Krejci80056d52017-01-05 13:13:33 +01007440 return rc;
7441 } else {
7442 /* erase info about validation errors */
Michal Vasko53b7da02018-02-13 15:28:42 +01007443 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7444 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007445 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007446
Radek Krejci80056d52017-01-05 13:13:33 +01007447 print_unres_schema_item_fail(item, type, snode);
7448
7449 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7450 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7451 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7452 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7453 lyxml_unlink_elem(mod->ctx, yin, 1);
7454 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7455 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007456 }
Michal Vasko88c29542015-11-27 14:57:53 +01007457 }
7458
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007459 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007460 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Michal Vasko53b7da02018-02-13 15:28:42 +01007461 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007462 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007463 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007464 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007465 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007466 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Michal Vasko53b7da02018-02-13 15:28:42 +01007467 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007468 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007469 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01007470 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007471 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007472
Michal Vasko3767fb22016-07-21 12:10:57 +02007473 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007474}
7475
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007476/**
Michal Vaskobb211122015-08-19 14:03:11 +02007477 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007478 *
7479 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007480 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007481 * @param[in] item Old item to be resolved.
7482 * @param[in] type Type of the old unresolved item.
7483 * @param[in] new_item New item to use in the duplicate.
7484 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007485 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007486 */
Michal Vaskodad19402015-08-06 09:51:53 +02007487int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007488unres_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 +02007489{
7490 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007491 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007492 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007493
Michal Vaskocf024702015-10-08 15:01:42 +02007494 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007495
Radek Krejcid09d1a52016-08-11 14:05:45 +02007496 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7497 if (type == UNRES_LIST_UNIQ) {
7498 aux_uniq.list = item;
7499 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7500 item = &aux_uniq;
7501 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007502 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007503
7504 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007505 if (type == UNRES_LIST_UNIQ) {
7506 free(new_item);
7507 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007508 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007509 }
7510
Radek Krejcic79c6b12016-07-26 15:11:49 +02007511 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007512 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007513 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007514 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007515 return -1;
7516 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007517 } else if (type == UNRES_IFFEAT) {
7518 /* duplicate unres_iffeature_data */
7519 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01007520 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007521 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7522 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7523 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007524 LOGINT(mod->ctx);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007525 return -1;
7526 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007527 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007528 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007529 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007530 return -1;
7531 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007532 }
Michal Vaskodad19402015-08-06 09:51:53 +02007533
7534 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007535}
7536
Michal Vaskof02e3742015-08-05 16:27:02 +02007537/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007538int
Michal Vasko878e38d2016-09-05 12:17:53 +02007539unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007540{
Michal Vasko878e38d2016-09-05 12:17:53 +02007541 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007542 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007543
Michal Vaskoc643f712018-09-14 08:26:13 +02007544 if (!unres->count) {
7545 return -1;
7546 }
7547
Radek Krejciddddd0d2017-01-20 15:20:46 +01007548 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007549 i = start_on_backwards;
7550 } else {
7551 i = unres->count - 1;
7552 }
7553 for (; i > -1; i--) {
7554 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007555 continue;
7556 }
7557 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007558 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007559 break;
7560 }
7561 } else {
7562 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7563 aux_uniq2 = (struct unres_list_uniq *)item;
7564 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007565 break;
7566 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007567 }
7568 }
7569
Michal Vasko878e38d2016-09-05 12:17:53 +02007570 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007571}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007572
Michal Vaskoede9c472016-06-07 09:38:15 +02007573static void
7574unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7575{
7576 struct lyxml_elem *yin;
7577 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007578 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007579
7580 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007581 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007582 case UNRES_TYPE_DER:
7583 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7584 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7585 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007586 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007587 lydict_remove(ctx, yang->name);
7588 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007589 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7590 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7591 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007592 } else {
7593 lyxml_free(ctx, yin);
7594 }
7595 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007596 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007597 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7598 lydict_remove(ctx, iff_data->fname);
7599 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007600 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007601 case UNRES_IDENT:
7602 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007603 case UNRES_CHOICE_DFLT:
7604 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007605 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7606 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007607 case UNRES_LIST_UNIQ:
7608 free(unres->item[i]);
7609 break;
PavolVicanc1807262017-01-31 18:00:27 +01007610 case UNRES_EXT:
7611 free(unres->str_snode[i]);
7612 break;
PavolVicanfcc98762017-09-01 15:51:39 +02007613 case UNRES_EXT_FINALIZE:
7614 free(unres->str_snode[i]);
Michal Vaskoede9c472016-06-07 09:38:15 +02007615 default:
7616 break;
7617 }
7618 unres->type[i] = UNRES_RESOLVED;
7619}
7620
Michal Vasko88c29542015-11-27 14:57:53 +01007621void
Michal Vasko44ab1462017-05-18 13:18:36 +02007622unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007623{
7624 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007625 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007626
Radek Krejcic071c542016-01-27 14:57:51 +01007627 if (!unres || !(*unres)) {
7628 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007629 }
7630
Michal Vasko44ab1462017-05-18 13:18:36 +02007631 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007632
7633 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007634 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007635 if ((*unres)->type[i] != UNRES_RESOLVED) {
7636 unresolved++;
7637 }
7638 continue;
7639 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007640
7641 /* free heap memory for the specific item */
7642 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007643 }
7644
Michal Vaskoede9c472016-06-07 09:38:15 +02007645 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007646 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007647 free((*unres)->item);
7648 free((*unres)->type);
7649 free((*unres)->str_snode);
7650 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007651 free((*unres));
7652 (*unres) = NULL;
7653 }
Michal Vasko88c29542015-11-27 14:57:53 +01007654}
7655
Michal Vaskoff690e72017-08-03 14:25:07 +02007656/* check whether instance-identifier points outside its data subtree (for operation it is any node
7657 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007658static int
7659check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7660{
Michal Vaskoff690e72017-08-03 14:25:07 +02007661 const struct lys_node *op_node, *first_node;
Michal Vasko53b7da02018-02-13 15:28:42 +01007662 enum int_log_opts prev_ilo;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007663 char *buf;
Michal Vasko53b7da02018-02-13 15:28:42 +01007664 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007665
Radek Krejci034cb102017-08-01 15:45:13 +02007666 if (!json_instid || !json_instid[0]) {
7667 /* no/empty value */
7668 return 0;
7669 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007670
7671 for (op_node = lys_parent(sleaf);
7672 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7673 op_node = lys_parent(op_node));
7674
7675 if (op_node && lys_parent(op_node)) {
7676 /* nested operation - any absolute path is external */
7677 return 1;
7678 }
7679
7680 /* get the first node from the instid */
7681 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7682 if (!buf) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007683 /* so that we do not have to bother with logging, say it is not external */
7684 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007685 }
7686
Michal Vaskoff690e72017-08-03 14:25:07 +02007687 /* find the first schema node, do not log */
Michal Vasko53b7da02018-02-13 15:28:42 +01007688 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoff690e72017-08-03 14:25:07 +02007689 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
Michal Vasko53b7da02018-02-13 15:28:42 +01007690 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007691
Michal Vasko53b7da02018-02-13 15:28:42 +01007692 free(buf);
Michal Vaskoff690e72017-08-03 14:25:07 +02007693 if (!first_node) {
7694 /* unknown path, say it is not external */
Michal Vaskoff690e72017-08-03 14:25:07 +02007695 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007696 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007697
7698 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7699
Michal Vaskoff690e72017-08-03 14:25:07 +02007700 if (op_node && (op_node != first_node)) {
7701 /* it is a top-level operation, so we're good if it points somewhere inside it */
7702 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007703 }
7704
7705 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7706 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007707 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007708}
7709
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007710/**
7711 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7712 *
7713 * @param[in] data Data node where the path is used
7714 * @param[in] path Instance-identifier node value.
7715 * @param[in,out] ret Resolved instance or NULL.
7716 *
7717 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7718 */
7719static int
7720resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7721{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007722 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007723 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007724 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007725 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007726 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007727 char *str;
7728 int mod_len, name_len, has_predicate;
7729 struct unres_data node_match;
7730
7731 memset(&node_match, 0, sizeof node_match);
7732 *ret = NULL;
7733
7734 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007735 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007736 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007737 if (root->prev) {
7738 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007739 }
7740
7741 /* search for the instance node */
7742 while (path[i]) {
7743 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7744 if (j <= 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007745 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007746 goto error;
7747 }
7748 i += j;
7749
Michal Vasko1b6ca962017-08-03 14:23:09 +02007750 if (model) {
7751 str = strndup(model, mod_len);
7752 if (!str) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007753 LOGMEM(ctx);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007754 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007755 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007756 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007757 if (ctx->data_clb) {
7758 if (!mod) {
7759 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7760 } else if (!mod->implemented) {
7761 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7762 }
7763 }
7764 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007765
Michal Vasko1b6ca962017-08-03 14:23:09 +02007766 if (!mod || !mod->implemented || mod->disabled) {
7767 break;
7768 }
7769 } else if (!prev_mod) {
7770 /* first iteration and we are missing module name */
Michal Vaskoaf8ec362018-03-28 09:08:09 +02007771 LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name);
7772 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node.");
Michal Vasko1b6ca962017-08-03 14:23:09 +02007773 goto error;
7774 } else {
7775 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007776 }
7777
Radek Krejci2c822ed2017-08-03 14:23:36 +02007778 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007779 /* no instance exists */
7780 break;
7781 }
7782
7783 if (has_predicate) {
7784 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007785 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007786 /* index of the current node (for lists with position predicates) */
7787 cur_idx = 1;
7788 while (j < (signed)node_match.count) {
7789 node = node_match.node[j];
7790 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7791 if (parsed < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007792 LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007793 goto error;
7794 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007795
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007796 if (!node) {
7797 /* current node does not satisfy the predicate */
7798 unres_data_del(&node_match, j);
7799 } else {
7800 ++j;
7801 }
7802 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007803 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007804
7805 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007806 } else if (node_match.count) {
7807 /* check that we are not addressing lists */
7808 for (j = 0; (unsigned)j < node_match.count; ++j) {
7809 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7810 unres_data_del(&node_match, j--);
7811 }
7812 }
7813 if (!node_match.count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007814 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007815 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007816 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007817
7818 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007819 }
7820
7821 if (!node_match.count) {
7822 /* no instance exists */
7823 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007824 LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007825 return EXIT_FAILURE;
7826 }
7827 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7828 return EXIT_SUCCESS;
7829 } else if (node_match.count > 1) {
7830 /* instance identifier must resolve to a single node */
Michal Vasko53b7da02018-02-13 15:28:42 +01007831 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007832 goto error;
7833 } else {
7834 /* we have required result, remember it and cleanup */
7835 *ret = node_match.node[0];
7836 free(node_match.node);
7837 return EXIT_SUCCESS;
7838 }
7839
7840error:
7841 /* cleanup */
7842 free(node_match.node);
7843 return -1;
7844}
7845
7846static int
7847resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007848{
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007849 struct lyxp_set xp_set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007850 uint32_t i;
7851
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007852 memset(&xp_set, 0, sizeof xp_set);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007853 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007854
Michal Vaskoca16cb32017-07-10 11:50:33 +02007855 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007856 if (lyxp_eval(path, (struct lyd_node *)leaf, LYXP_NODE_ELEM, lyd_node_module((struct lyd_node *)leaf), &xp_set, 0) != EXIT_SUCCESS) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007857 return -1;
7858 }
7859
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007860 if (xp_set.type == LYXP_SET_NODE_SET) {
7861 for (i = 0; i < xp_set.used; ++i) {
7862 if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7863 continue;
7864 }
Michal Vaskoca16cb32017-07-10 11:50:33 +02007865
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007866 /* not that the value is already in canonical form since the parsers does the conversion,
7867 * so we can simply compare just the values */
7868 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) {
7869 /* we have the match */
7870 *ret = xp_set.val.nodes[i].node;
7871 break;
7872 }
Radek Krejci7de36cf2016-09-12 16:18:50 +02007873 }
7874 }
7875
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007876 lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, (struct lyd_node *)leaf, NULL, 0);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007877
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007878 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007879 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007880 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007881 LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007882 return EXIT_FAILURE;
7883 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007884 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 +02007885 }
7886 }
7887
7888 return EXIT_SUCCESS;
7889}
7890
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007891/* 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 +01007892int
7893resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7894 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007895{
Michal Vasko53b7da02018-02-13 15:28:42 +01007896 struct ly_ctx *ctx = leaf->schema->module->ctx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007897 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007898 struct lyd_node *ret;
Michal Vasko53b7da02018-02-13 15:28:42 +01007899 enum int_log_opts prev_ilo;
7900 int found, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007901 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007902
7903 assert(type->base == LY_TYPE_UNION);
7904
Michal Vasko101658e2018-06-05 15:05:54 +02007905 if ((leaf->value_type == LY_TYPE_UNION) || ((leaf->value_type == LY_TYPE_INST) && (leaf->value_flags & LY_VALUE_UNRES))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007906 /* either NULL or instid previously converted to JSON */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007907 json_val = lydict_insert(ctx, leaf->value.string, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007908 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007909
Michal Vaskofd6c6502017-01-06 12:15:41 +01007910 if (store) {
Michal Vasko7ba37442018-11-06 08:59:27 +01007911 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type,
7912 NULL, NULL, NULL);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007913 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007914 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007915
7916 /* 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 +01007917 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007918
7919 t = NULL;
7920 found = 0;
7921 while ((t = lyp_get_next_union_type(type, t, &found))) {
7922 found = 0;
7923
7924 switch (t->base) {
7925 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007926 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7927 req_inst = -1;
7928 } else {
7929 req_inst = t->info.lref.req;
7930 }
7931
7932 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007933 if (store) {
7934 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7935 /* valid resolved */
7936 leaf->value.leafref = ret;
7937 leaf->value_type = LY_TYPE_LEAFREF;
7938 } else {
7939 /* valid unresolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01007940 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko35f46a82018-05-30 10:44:11 +02007941 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007942 return -1;
7943 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007944 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007945 }
7946 }
7947
7948 success = 1;
7949 }
7950 break;
7951 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007952 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7953 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7954 req_inst = -1;
7955 } else {
7956 req_inst = t->info.inst.req;
7957 }
7958
Michal Vaskod3a03112017-01-23 09:56:02 +01007959 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007960 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007961 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007962 /* valid resolved */
7963 leaf->value.instance = ret;
7964 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007965
Michal Vaskofd6c6502017-01-06 12:15:41 +01007966 if (json_val) {
7967 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7968 leaf->value_str = json_val;
7969 json_val = NULL;
7970 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007971 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007972 /* valid unresolved */
7973 if (json_val) {
7974 /* put the JSON val back */
7975 leaf->value.string = json_val;
7976 json_val = NULL;
7977 } else {
7978 leaf->value.instance = NULL;
7979 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02007980 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02007981 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007982 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007983 }
7984
7985 success = 1;
7986 }
7987 break;
7988 default:
Michal Vasko35f46a82018-05-30 10:44:11 +02007989 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007990 success = 1;
7991 }
7992 break;
7993 }
7994
7995 if (success) {
7996 break;
7997 }
7998
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007999 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01008000 if (store) {
Michal Vasko7ba37442018-11-06 08:59:27 +01008001 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t, NULL, NULL, NULL);
Michal Vaskofd6c6502017-01-06 12:15:41 +01008002 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008003 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008004 }
8005
8006 /* turn logging back on */
Michal Vasko53b7da02018-02-13 15:28:42 +01008007 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008008
8009 if (json_val) {
8010 if (!success) {
8011 /* put the value back for now */
8012 assert(leaf->value_type == LY_TYPE_UNION);
8013 leaf->value.string = json_val;
8014 } else {
8015 /* value was ultimately useless, but we could not have known */
8016 lydict_remove(leaf->schema->module->ctx, json_val);
8017 }
8018 }
8019
Michal Vaskofd6c6502017-01-06 12:15:41 +01008020 if (success) {
8021 if (resolved_type) {
8022 *resolved_type = t;
8023 }
8024 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008025 /* not found and it is required */
Michal Vasko53b7da02018-02-13 15:28:42 +01008026 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02008027 return EXIT_FAILURE;
8028 }
8029
8030 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008031
Radek Krejci9b6aad22016-09-20 15:55:51 +02008032}
8033
Michal Vasko8bcdf292015-08-19 14:04:43 +02008034/**
8035 * @brief Resolve a single unres data item. Logs directly.
8036 *
Michal Vaskocf024702015-10-08 15:01:42 +02008037 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02008038 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01008039 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008040 *
8041 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
8042 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02008043int
Michal Vasko0b963112017-08-11 12:45:36 +02008044resolve_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 +02008045{
Michal Vasko3cfa3182017-01-17 10:00:58 +01008046 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02008047 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008048 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008049 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008050
Michal Vasko83a6c462015-10-08 16:43:53 +02008051 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02008052 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008053
Michal Vaskocf024702015-10-08 15:01:42 +02008054 switch (type) {
8055 case UNRES_LEAFREF:
8056 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008057 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008058 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
8059 req_inst = -1;
8060 } else {
8061 req_inst = sleaf->type.info.lref.req;
8062 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008063 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
8064 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01008065 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008066 /* valid resolved */
Michal Vasko70bf8e52018-03-26 11:32:33 +02008067 if (leaf->value_type == LY_TYPE_BITS) {
Michal Vasko1c8567a2017-01-05 13:42:27 +01008068 free(leaf->value.bit);
8069 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008070 leaf->value.leafref = ret;
8071 leaf->value_type = LY_TYPE_LEAFREF;
Michal Vasko101658e2018-06-05 15:05:54 +02008072 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008073 } else {
8074 /* valid unresolved */
Michal Vasko101658e2018-06-05 15:05:54 +02008075 if (!(leaf->value_flags & LY_VALUE_UNRES)) {
Michal Vasko35f46a82018-05-30 10:44:11 +02008076 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008077 return -1;
8078 }
8079 }
8080 }
8081 leaf->validity &= ~LYD_VAL_LEAFREF;
8082 } else {
8083 return rc;
8084 }
8085 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008086
Michal Vaskocf024702015-10-08 15:01:42 +02008087 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02008088 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008089 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
8090 if (ext_dep == -1) {
8091 return -1;
8092 }
8093
8094 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
8095 req_inst = -1;
8096 } else {
8097 req_inst = sleaf->type.info.inst.req;
8098 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008099 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
8100 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008101 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008102 /* valid resolved */
8103 leaf->value.instance = ret;
8104 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008105 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008106 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008107 /* valid unresolved */
8108 leaf->value.instance = NULL;
Michal Vasko70bf8e52018-03-26 11:32:33 +02008109 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008110 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008111 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008112 } else {
8113 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008114 }
Michal Vaskocf024702015-10-08 15:01:42 +02008115 break;
8116
Radek Krejci7de36cf2016-09-12 16:18:50 +02008117 case UNRES_UNION:
8118 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01008119 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02008120
Michal Vaskocf024702015-10-08 15:01:42 +02008121 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02008122 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02008123 return rc;
8124 }
8125 break;
8126
Michal Vaskobf19d252015-10-08 15:39:17 +02008127 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008128 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02008129 return rc;
8130 }
8131 break;
8132
8133 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008134 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02008135 return rc;
8136 }
8137 break;
8138
Michal Vasko185b5272018-09-13 14:26:12 +02008139 case UNRES_UNIQ_LEAVES:
8140 if (lyv_data_unique(node)) {
8141 return -1;
8142 }
8143 break;
8144
Michal Vaskocf024702015-10-08 15:01:42 +02008145 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01008146 LOGINT(NULL);
Michal Vasko8bcdf292015-08-19 14:04:43 +02008147 return -1;
8148 }
8149
8150 return EXIT_SUCCESS;
8151}
8152
8153/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01008154 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02008155 *
8156 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02008157 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008158 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01008159 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008160 */
8161int
Radek Krejci0b7704f2016-03-18 12:16:14 +01008162unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02008163{
Radek Krejci03b71f72016-03-16 11:10:09 +01008164 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02008165 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Michal Vasko185b5272018-09-13 14:26:12 +02008166 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION) || (type == UNRES_UNIQ_LEAVES));
Michal Vasko8bcdf292015-08-19 14:04:43 +02008167
Radek Krejci03b71f72016-03-16 11:10:09 +01008168 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01008169 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01008170 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008171 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01008172 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01008173 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008174 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008175
Radek Krejci0b7704f2016-03-18 12:16:14 +01008176 if (type == UNRES_WHEN) {
8177 /* remove previous result */
8178 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008179 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008180
Michal Vasko53b7da02018-02-13 15:28:42 +01008181 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008182}
8183
8184/**
8185 * @brief Resolve every unres data item in the structure. Logs directly.
8186 *
Michal Vasko660582a2018-03-19 10:10:08 +01008187 * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected,
8188 * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit
8189 * non-presence containers may need to be deleted).
Radek Krejci082c84f2016-10-17 16:33:06 +02008190 *
Michal Vaskobbc43b12018-10-12 09:22:00 +02008191 * If options includes #LYD_OPT_WHENAUTODEL, the non-default nodes with false when conditions are auto-deleted.
Radek Krejci082c84f2016-10-17 16:33:06 +02008192 *
Michal Vasko53b7da02018-02-13 15:28:42 +01008193 * @param[in] ctx Context used.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008194 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02008195 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
8196 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008197 *
8198 * @return EXIT_SUCCESS on success, -1 on error.
8199 */
8200int
Michal Vasko53b7da02018-02-13 15:28:42 +01008201resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008202{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008203 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008204 int rc, progress, ignore_fail;
Michal Vasko53b7da02018-02-13 15:28:42 +01008205 enum int_log_opts prev_ilo;
8206 struct ly_err_item *prev_eitem;
mohitarora2489837dc2018-05-01 15:09:36 +05308207 LY_ERR prev_ly_errno = ly_errno;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008208 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02008209 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008210
Radek Krejci082c84f2016-10-17 16:33:06 +02008211 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01008212 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01008213
8214 if (!unres->count) {
8215 return EXIT_SUCCESS;
8216 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008217
Michal Vasko7fa93142018-03-19 09:59:10 +01008218 if (options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008219 ignore_fail = 1;
8220 } else if (options & LYD_OPT_NOEXTDEPS) {
8221 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008222 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008223 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008224 }
8225
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008226 LOGVRB("Resolving unresolved data nodes and their constraints...");
Michal Vasko7fa93142018-03-19 09:59:10 +01008227 if (!ignore_fail) {
8228 /* remember logging state only if errors are generated and valid */
Michal Vasko7fa93142018-03-19 09:59:10 +01008229 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
8230 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008231
Michal Vasko7fa93142018-03-19 09:59:10 +01008232 /*
8233 * when-stmt first
8234 */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008235 first = 1;
8236 stmt_count = 0;
8237 resolved = 0;
8238 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01008239 do {
Michal Vasko7fa93142018-03-19 09:59:10 +01008240 if (!ignore_fail) {
8241 ly_err_free_next(ctx, prev_eitem);
8242 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008243 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02008244 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008245 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008246 continue;
8247 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008248 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008249 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008250 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008251 }
8252
8253 /* resolve when condition only when all parent when conditions are already resolved */
8254 for (parent = unres->node[i]->parent;
8255 parent && LYD_WHEN_DONE(parent->when_status);
8256 parent = parent->parent) {
8257 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
8258 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02008259 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01008260 */
8261 unres->node[i]->when_status |= LYD_WHEN_FALSE;
8262 unres->type[i] = UNRES_RESOLVED;
8263 resolved++;
8264 break;
8265 }
8266 }
8267 if (parent) {
8268 continue;
8269 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008270
Michal Vasko0b963112017-08-11 12:45:36 +02008271 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01008272 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02008273 /* finish with error/delete the node only if when was false, an external dependency was not required,
8274 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02008275 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vaskoc04173b2018-03-09 10:43:22 +01008276 && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) {
Michal Vaskobbc43b12018-10-12 09:22:00 +02008277 if (!(options & LYD_OPT_WHENAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01008278 /* false when condition */
Michal Vasko53b7da02018-02-13 15:28:42 +01008279 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008280 } /* follows else */
8281
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008282 /* auto-delete */
Michal Vasko53b7da02018-02-13 15:28:42 +01008283 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond);
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008284
Radek Krejci0c0086a2016-03-24 15:20:28 +01008285 /* only unlink now, the subtree can contain another nodes stored in the unres list */
8286 /* if it has parent non-presence containers that would be empty, we should actually
8287 * remove the container
8288 */
Radek Krejci2537fd32016-09-07 16:22:41 +02008289 for (parent = unres->node[i];
8290 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
8291 parent = parent->parent) {
8292 if (((struct lys_node_container *)parent->parent->schema)->presence) {
8293 /* presence container */
8294 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008295 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008296 if (parent->next || parent->prev != parent) {
8297 /* non empty (the child we are in and we are going to remove is not the only child) */
8298 break;
8299 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008300 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008301 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008302
Radek Krejci0c0086a2016-03-24 15:20:28 +01008303 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008304 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01008305 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008306
Radek Krejci0b7704f2016-03-18 12:16:14 +01008307 lyd_unlink(unres->node[i]);
8308 unres->type[i] = UNRES_DELETE;
8309 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01008310
8311 /* update the rest of unres items */
8312 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01008313 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01008314 continue;
8315 }
8316
8317 /* test if the node is in subtree to be deleted */
8318 for (parent = unres->node[j]; parent; parent = parent->parent) {
8319 if (parent == unres->node[i]) {
8320 /* yes, it is */
8321 unres->type[j] = UNRES_RESOLVED;
8322 resolved++;
8323 break;
8324 }
8325 }
8326 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008327 } else {
8328 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01008329 }
Michal Vasko7fa93142018-03-19 09:59:10 +01008330 if (!ignore_fail) {
8331 ly_err_free_next(ctx, prev_eitem);
8332 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008333 resolved++;
8334 progress = 1;
8335 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008336 goto error;
Radek Krejci2467a492016-10-24 15:16:59 +02008337 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01008338 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008339 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008340 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01008341
Radek Krejci0b7704f2016-03-18 12:16:14 +01008342 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008343 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008344 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008345 }
8346
8347 for (i = 0; del_items && i < unres->count; i++) {
8348 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
8349 if (unres->type[i] != UNRES_DELETE) {
8350 continue;
8351 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008352 if (!unres->node[i]) {
8353 unres->type[i] = UNRES_RESOLVED;
8354 del_items--;
8355 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008356 }
8357
8358 /* really remove the complete subtree */
8359 lyd_free(unres->node[i]);
8360 unres->type[i] = UNRES_RESOLVED;
8361 del_items--;
8362 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008363
Michal Vasko7fa93142018-03-19 09:59:10 +01008364 /*
8365 * now leafrefs
8366 */
8367 if (options & LYD_OPT_TRUSTED) {
8368 /* we want to attempt to resolve leafrefs */
8369 assert(!ignore_fail);
8370 ignore_fail = 1;
8371
8372 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8373 ly_errno = prev_ly_errno;
8374 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008375 first = 1;
8376 stmt_count = 0;
8377 resolved = 0;
8378 do {
8379 progress = 0;
8380 for (i = 0; i < unres->count; i++) {
8381 if (unres->type[i] != UNRES_LEAFREF) {
8382 continue;
8383 }
8384 if (first) {
8385 /* count leafref nodes in unres list */
8386 stmt_count++;
8387 }
8388
Michal Vasko0b963112017-08-11 12:45:36 +02008389 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008390 if (!rc) {
8391 unres->type[i] = UNRES_RESOLVED;
Michal Vasko7fa93142018-03-19 09:59:10 +01008392 if (!ignore_fail) {
8393 ly_err_free_next(ctx, prev_eitem);
8394 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008395 resolved++;
8396 progress = 1;
8397 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008398 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008399 } /* else forward reference */
8400 }
8401 first = 0;
8402 } while (progress && resolved < stmt_count);
8403
8404 /* do we have some unresolved leafrefs? */
8405 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008406 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008407 }
8408
Michal Vasko7fa93142018-03-19 09:59:10 +01008409 if (!ignore_fail) {
8410 /* log normally now, throw away irrelevant errors */
8411 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8412 ly_errno = prev_ly_errno;
8413 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008414
Michal Vasko7fa93142018-03-19 09:59:10 +01008415 /*
8416 * rest
8417 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008418 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008419 if (unres->type[i] == UNRES_RESOLVED) {
8420 continue;
8421 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008422 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008423
Michal Vasko0b963112017-08-11 12:45:36 +02008424 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008425 if (rc) {
8426 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008427 return -1;
8428 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008429
8430 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008431 }
8432
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008433 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008434 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008435 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01008436
8437error:
Michal Vasko7fa93142018-03-19 09:59:10 +01008438 if (!ignore_fail) {
8439 /* print all the new errors */
8440 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
8441 /* do not restore ly_errno, it was udpated properly */
8442 }
Michal Vasko53b7da02018-02-13 15:28:42 +01008443 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008444}