blob: e482611510b5655ad5f942b211e1a80ab3d8b6da [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 Vaskoc3d9f8c2015-07-31 14:37:24 +020033
Michal Vasko2d44ee02018-05-18 09:38:51 +020034/* internal parsed predicate structure */
35struct parsed_pred {
36 const struct lys_node *schema;
37 int len;
38 struct {
39 const char *mod_name;
40 int mod_name_len;
41 const char *name;
42 int nam_len;
43 const char *value;
44 int val_len;
45 } *pred;
46};
47
Michal Vaskod24dd012016-09-30 12:20:22 +020048int
49parse_range_dec64(const char **str_num, uint8_t dig, int64_t *num)
Michal Vasko4d1f0482016-09-19 14:35:06 +020050{
51 const char *ptr;
52 int minus = 0;
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020053 int64_t ret = 0, prev_ret;
Radek Krejcibf47a822016-11-04 10:06:08 +010054 int8_t str_exp, str_dig = -1, trailing_zeros = 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +020055
56 ptr = *str_num;
57
58 if (ptr[0] == '-') {
59 minus = 1;
60 ++ptr;
Radek Krejci51673202016-11-01 17:00:32 +010061 } else if (ptr[0] == '+') {
62 ++ptr;
Michal Vasko4d1f0482016-09-19 14:35:06 +020063 }
64
Michal Vaskod24dd012016-09-30 12:20:22 +020065 if (!isdigit(ptr[0])) {
66 /* there must be at least one */
67 return 1;
68 }
69
Michal Vasko4d1f0482016-09-19 14:35:06 +020070 for (str_exp = 0; isdigit(ptr[0]) || ((ptr[0] == '.') && (str_dig < 0)); ++ptr) {
71 if (str_exp > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +020072 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +020073 }
74
75 if (ptr[0] == '.') {
76 if (ptr[1] == '.') {
77 /* it's the next interval */
78 break;
79 }
80 ++str_dig;
81 } else {
Michal Vaskoe2ea45a2017-08-07 13:15:07 +020082 prev_ret = ret;
83 if (minus) {
84 ret = ret * 10 - (ptr[0] - '0');
85 if (ret > prev_ret) {
86 return 1;
87 }
88 } else {
89 ret = ret * 10 + (ptr[0] - '0');
90 if (ret < prev_ret) {
91 return 1;
92 }
93 }
Michal Vasko4d1f0482016-09-19 14:35:06 +020094 if (str_dig > -1) {
95 ++str_dig;
Radek Krejcibf47a822016-11-04 10:06:08 +010096 if (ptr[0] == '0') {
97 /* possibly trailing zero */
98 trailing_zeros++;
99 } else {
100 trailing_zeros = 0;
101 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200102 }
103 ++str_exp;
104 }
105 }
Michal Vaskod24dd012016-09-30 12:20:22 +0200106 if (str_dig == 0) {
107 /* no digits after '.' */
108 return 1;
109 } else if (str_dig == -1) {
110 /* there are 0 numbers after the floating point */
Michal Vasko4d1f0482016-09-19 14:35:06 +0200111 str_dig = 0;
112 }
Radek Krejcibf47a822016-11-04 10:06:08 +0100113 /* remove trailing zeros */
114 if (trailing_zeros) {
Michal Vasko6ca5ca72016-11-28 09:21:51 +0100115 str_dig -= trailing_zeros;
116 str_exp -= trailing_zeros;
Radek Krejcibf47a822016-11-04 10:06:08 +0100117 ret = ret / dec_pow(trailing_zeros);
118 }
Michal Vasko4d1f0482016-09-19 14:35:06 +0200119
120 /* it's parsed, now adjust the number based on fraction-digits, if needed */
121 if (str_dig < dig) {
122 if ((str_exp - 1) + (dig - str_dig) > 18) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200123 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200124 }
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200125 prev_ret = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200126 ret *= dec_pow(dig - str_dig);
Michal Vaskoe2ea45a2017-08-07 13:15:07 +0200127 if ((minus && (ret > prev_ret)) || (!minus && (ret < prev_ret))) {
128 return 1;
129 }
130
Michal Vasko4d1f0482016-09-19 14:35:06 +0200131 }
132 if (str_dig > dig) {
Michal Vaskod24dd012016-09-30 12:20:22 +0200133 return 1;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200134 }
135
Michal Vasko4d1f0482016-09-19 14:35:06 +0200136 *str_num = ptr;
Michal Vaskod24dd012016-09-30 12:20:22 +0200137 *num = ret;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200138
Michal Vaskod24dd012016-09-30 12:20:22 +0200139 return 0;
Michal Vasko4d1f0482016-09-19 14:35:06 +0200140}
141
142/**
Radek Krejci6dc53a22015-08-17 13:27:59 +0200143 * @brief Parse an identifier.
144 *
145 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
146 * identifier = (ALPHA / "_")
147 * *(ALPHA / DIGIT / "_" / "-" / ".")
148 *
Michal Vaskobb211122015-08-19 14:03:11 +0200149 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200150 *
151 * @return Number of characters successfully parsed.
152 */
Radek Krejcidce5f972017-09-12 15:47:49 +0200153unsigned int
Radek Krejci6dc53a22015-08-17 13:27:59 +0200154parse_identifier(const char *id)
155{
Radek Krejcidce5f972017-09-12 15:47:49 +0200156 unsigned int parsed = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200157
Michal Vasko1ab90bc2016-03-15 10:40:22 +0100158 assert(id);
159
Radek Krejci6dc53a22015-08-17 13:27:59 +0200160 if (!isalpha(id[0]) && (id[0] != '_')) {
161 return -parsed;
162 }
163
164 ++parsed;
165 ++id;
166
167 while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) {
168 ++parsed;
169 ++id;
170 }
171
172 return parsed;
173}
174
175/**
176 * @brief Parse a node-identifier.
177 *
Michal Vasko723e50c2015-10-20 15:20:29 +0200178 * node-identifier = [module-name ":"] identifier
Radek Krejci6dc53a22015-08-17 13:27:59 +0200179 *
Michal Vaskobb211122015-08-19 14:03:11 +0200180 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200181 * @param[out] mod_name Points to the module name, NULL if there is not any.
182 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200183 * @param[out] name Points to the node name.
184 * @param[out] nam_len Length of the node name.
Michal Vasko50576712017-07-28 12:28:33 +0200185 * @param[out] all_desc Whether the path starts with '/', only supported in extended paths.
PavolVicanb28bbff2018-02-21 00:44:02 +0100186 * @param[in] extended Whether to accept an extended path (support for [prefix:]*, /[prefix:]*, /[prefix:]., prefix:#identifier).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200187 *
188 * @return Number of characters successfully parsed,
189 * positive on success, negative on failure.
190 */
191static int
Michal Vasko50576712017-07-28 12:28:33 +0200192parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
193 int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200194{
PavolVican195cf392018-02-23 13:24:45 +0100195 int parsed = 0, ret, all_desc_local = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200196
197 assert(id);
Michal Vasko50576712017-07-28 12:28:33 +0200198 assert((mod_name && mod_name_len) || (!mod_name && !mod_name_len));
199 assert((name && nam_len) || (!name && !nam_len));
Michal Vasko50576712017-07-28 12:28:33 +0200200
Michal Vasko723e50c2015-10-20 15:20:29 +0200201 if (mod_name) {
202 *mod_name = NULL;
Michal Vasko723e50c2015-10-20 15:20:29 +0200203 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200204 }
205 if (name) {
206 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200207 *nam_len = 0;
208 }
209
Michal Vasko50576712017-07-28 12:28:33 +0200210 if (extended) {
211 /* try to parse only the extended expressions */
212 if (id[parsed] == '/') {
PavolVican195cf392018-02-23 13:24:45 +0100213 if (all_desc) {
214 *all_desc = 1;
215 }
216 all_desc_local = 1;
Michal Vasko50576712017-07-28 12:28:33 +0200217 } else {
PavolVican195cf392018-02-23 13:24:45 +0100218 if (all_desc) {
219 *all_desc = 0;
220 }
Michal Vasko50576712017-07-28 12:28:33 +0200221 }
222
223 /* is there a prefix? */
PavolVican195cf392018-02-23 13:24:45 +0100224 ret = parse_identifier(id + all_desc_local);
Michal Vasko50576712017-07-28 12:28:33 +0200225 if (ret > 0) {
PavolVican195cf392018-02-23 13:24:45 +0100226 if (id[all_desc_local + ret] != ':') {
Michal Vasko50576712017-07-28 12:28:33 +0200227 /* this is not a prefix, so not an extended id */
228 goto standard_id;
229 }
230
231 if (mod_name) {
PavolVican195cf392018-02-23 13:24:45 +0100232 *mod_name = id + all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200233 *mod_name_len = ret;
234 }
235
236 /* "/" and ":" */
PavolVican195cf392018-02-23 13:24:45 +0100237 ret += all_desc_local + 1;
Michal Vasko50576712017-07-28 12:28:33 +0200238 } else {
PavolVican195cf392018-02-23 13:24:45 +0100239 ret = all_desc_local;
Michal Vasko50576712017-07-28 12:28:33 +0200240 }
241
242 /* parse either "*" or "." */
PavolVicanb28bbff2018-02-21 00:44:02 +0100243 if (*(id + ret) == '*') {
Michal Vasko50576712017-07-28 12:28:33 +0200244 if (name) {
245 *name = id + ret;
246 *nam_len = 1;
247 }
248 ++ret;
249
250 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100251 } else if (*(id + ret) == '.') {
PavolVican195cf392018-02-23 13:24:45 +0100252 if (!all_desc_local) {
Michal Vasko50576712017-07-28 12:28:33 +0200253 /* /. is redundant expression, we do not accept it */
254 return -ret;
255 }
256
257 if (name) {
258 *name = id + ret;
259 *nam_len = 1;
260 }
261 ++ret;
262
263 return ret;
PavolVicanb28bbff2018-02-21 00:44:02 +0100264 } else if (*(id + ret) == '#') {
PavolVican195cf392018-02-23 13:24:45 +0100265 if (all_desc_local || !ret) {
PavolVicanb28bbff2018-02-21 00:44:02 +0100266 /* no prefix */
267 return 0;
268 }
269 parsed = ret + 1;
270 if ((ret = parse_identifier(id + parsed)) < 1) {
271 return -parsed + ret;
272 }
273 *name = id + parsed - 1;
274 *nam_len = ret + 1;
275 return parsed + ret;
Michal Vasko50576712017-07-28 12:28:33 +0200276 }
277 /* else a standard id, parse it all again */
278 }
279
280standard_id:
Radek Krejci6dc53a22015-08-17 13:27:59 +0200281 if ((ret = parse_identifier(id)) < 1) {
282 return ret;
283 }
284
Michal Vasko723e50c2015-10-20 15:20:29 +0200285 if (mod_name) {
286 *mod_name = id;
Michal Vasko723e50c2015-10-20 15:20:29 +0200287 *mod_name_len = ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200288 }
289
290 parsed += ret;
291 id += ret;
292
293 /* there is prefix */
294 if (id[0] == ':') {
295 ++parsed;
296 ++id;
297
298 /* there isn't */
299 } else {
Michal Vasko723e50c2015-10-20 15:20:29 +0200300 if (name && mod_name) {
301 *name = *mod_name;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200302 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200303 if (mod_name) {
304 *mod_name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200305 }
306
Michal Vasko723e50c2015-10-20 15:20:29 +0200307 if (nam_len && mod_name_len) {
308 *nam_len = *mod_name_len;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200309 }
Michal Vasko723e50c2015-10-20 15:20:29 +0200310 if (mod_name_len) {
311 *mod_name_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200312 }
313
314 return parsed;
315 }
316
317 /* identifier (node name) */
318 if ((ret = parse_identifier(id)) < 1) {
319 return -parsed+ret;
320 }
321
322 if (name) {
323 *name = id;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200324 *nam_len = ret;
325 }
326
327 return parsed+ret;
328}
329
330/**
331 * @brief Parse a path-predicate (leafref).
332 *
333 * path-predicate = "[" *WSP path-equality-expr *WSP "]"
334 * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr
335 *
Michal Vaskobb211122015-08-19 14:03:11 +0200336 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200337 * @param[out] prefix Points to the prefix, NULL if there is not any.
338 * @param[out] pref_len Length of the prefix, 0 if there is not any.
339 * @param[out] name Points to the node name.
340 * @param[out] nam_len Length of the node name.
341 * @param[out] path_key_expr Points to the path-key-expr.
342 * @param[out] pke_len Length of the path-key-expr.
343 * @param[out] has_predicate Flag to mark whether there is another predicate following.
344 *
345 * @return Number of characters successfully parsed,
346 * positive on success, negative on failure.
347 */
348static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200349parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
350 const char **path_key_expr, int *pke_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200351{
352 const char *ptr;
353 int parsed = 0, ret;
354
355 assert(id);
356 if (prefix) {
357 *prefix = NULL;
358 }
359 if (pref_len) {
360 *pref_len = 0;
361 }
362 if (name) {
363 *name = NULL;
364 }
365 if (nam_len) {
366 *nam_len = 0;
367 }
368 if (path_key_expr) {
369 *path_key_expr = NULL;
370 }
371 if (pke_len) {
372 *pke_len = 0;
373 }
374 if (has_predicate) {
375 *has_predicate = 0;
376 }
377
378 if (id[0] != '[') {
379 return -parsed;
380 }
381
382 ++parsed;
383 ++id;
384
385 while (isspace(id[0])) {
386 ++parsed;
387 ++id;
388 }
389
Michal Vasko50576712017-07-28 12:28:33 +0200390 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200391 return -parsed+ret;
392 }
393
394 parsed += ret;
395 id += ret;
396
397 while (isspace(id[0])) {
398 ++parsed;
399 ++id;
400 }
401
402 if (id[0] != '=') {
403 return -parsed;
404 }
405
406 ++parsed;
407 ++id;
408
409 while (isspace(id[0])) {
410 ++parsed;
411 ++id;
412 }
413
414 if ((ptr = strchr(id, ']')) == NULL) {
415 return -parsed;
416 }
417
418 --ptr;
419 while (isspace(ptr[0])) {
420 --ptr;
421 }
422 ++ptr;
423
424 ret = ptr-id;
425 if (path_key_expr) {
426 *path_key_expr = id;
427 }
428 if (pke_len) {
429 *pke_len = ret;
430 }
431
432 parsed += ret;
433 id += ret;
434
435 while (isspace(id[0])) {
436 ++parsed;
437 ++id;
438 }
439
440 assert(id[0] == ']');
441
442 if (id[1] == '[') {
443 *has_predicate = 1;
444 }
445
446 return parsed+1;
447}
448
449/**
450 * @brief Parse a path-key-expr (leafref). First call parses "current()", all
451 * the ".." and the first node-identifier, other calls parse a single
452 * node-identifier each.
453 *
454 * path-key-expr = current-function-invocation *WSP "/" *WSP
455 * rel-path-keyexpr
456 * rel-path-keyexpr = 1*(".." *WSP "/" *WSP)
457 * *(node-identifier *WSP "/" *WSP)
458 * node-identifier
459 *
Michal Vaskobb211122015-08-19 14:03:11 +0200460 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200461 * @param[out] prefix Points to the prefix, NULL if there is not any.
462 * @param[out] pref_len Length of the prefix, 0 if there is not any.
463 * @param[out] name Points to the node name.
464 * @param[out] nam_len Length of the node name.
465 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
466 * must not be changed between consecutive calls.
467 * @return Number of characters successfully parsed,
468 * positive on success, negative on failure.
469 */
470static int
Michal Vasko23b61ec2015-08-19 11:19:50 +0200471parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len,
472 int *parent_times)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200473{
474 int parsed = 0, ret, par_times = 0;
475
476 assert(id);
477 assert(parent_times);
478 if (prefix) {
479 *prefix = NULL;
480 }
481 if (pref_len) {
482 *pref_len = 0;
483 }
484 if (name) {
485 *name = NULL;
486 }
487 if (nam_len) {
488 *nam_len = 0;
489 }
490
491 if (!*parent_times) {
492 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
493 if (strncmp(id, "current()", 9)) {
494 return -parsed;
495 }
496
497 parsed += 9;
498 id += 9;
499
500 while (isspace(id[0])) {
501 ++parsed;
502 ++id;
503 }
504
505 if (id[0] != '/') {
506 return -parsed;
507 }
508
509 ++parsed;
510 ++id;
511
512 while (isspace(id[0])) {
513 ++parsed;
514 ++id;
515 }
516
517 /* rel-path-keyexpr */
518 if (strncmp(id, "..", 2)) {
519 return -parsed;
520 }
521 ++par_times;
522
523 parsed += 2;
524 id += 2;
525
526 while (isspace(id[0])) {
527 ++parsed;
528 ++id;
529 }
530 }
531
532 /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier
533 *
534 * first parent reference with whitespaces already parsed
535 */
536 if (id[0] != '/') {
537 return -parsed;
538 }
539
540 ++parsed;
541 ++id;
542
543 while (isspace(id[0])) {
544 ++parsed;
545 ++id;
546 }
547
548 while (!strncmp(id, "..", 2) && !*parent_times) {
549 ++par_times;
550
551 parsed += 2;
552 id += 2;
553
554 while (isspace(id[0])) {
555 ++parsed;
556 ++id;
557 }
558
559 if (id[0] != '/') {
560 return -parsed;
561 }
562
563 ++parsed;
564 ++id;
565
566 while (isspace(id[0])) {
567 ++parsed;
568 ++id;
569 }
570 }
571
572 if (!*parent_times) {
573 *parent_times = par_times;
574 }
575
576 /* all parent references must be parsed at this point */
Michal Vasko50576712017-07-28 12:28:33 +0200577 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
578 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200579 }
580
581 parsed += ret;
582 id += ret;
583
584 return parsed;
585}
586
587/**
588 * @brief Parse path-arg (leafref).
589 *
590 * path-arg = absolute-path / relative-path
591 * absolute-path = 1*("/" (node-identifier *path-predicate))
592 * relative-path = 1*(".." "/") descendant-path
593 *
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200594 * @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 +0200595 * @param[in] id Identifier to use.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200596 * @param[out] prefix Points to the prefix, NULL if there is not any.
597 * @param[out] pref_len Length of the prefix, 0 if there is not any.
598 * @param[out] name Points to the node name.
599 * @param[out] nam_len Length of the node name.
600 * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call,
601 * must not be changed between consecutive calls. -1 if the
602 * path is relative.
603 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
604 *
605 * @return Number of characters successfully parsed,
606 * positive on success, negative on failure.
607 */
608static int
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200609parse_path_arg(const struct lys_module *mod, const char *id, const char **prefix, int *pref_len,
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200610 const char **name, int *nam_len, int *parent_times, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200611{
612 int parsed = 0, ret, par_times = 0;
613
614 assert(id);
615 assert(parent_times);
616 if (prefix) {
617 *prefix = NULL;
618 }
619 if (pref_len) {
620 *pref_len = 0;
621 }
622 if (name) {
623 *name = NULL;
624 }
625 if (nam_len) {
626 *nam_len = 0;
627 }
628 if (has_predicate) {
629 *has_predicate = 0;
630 }
631
632 if (!*parent_times && !strncmp(id, "..", 2)) {
633 ++par_times;
634
635 parsed += 2;
636 id += 2;
637
638 while (!strncmp(id, "/..", 3)) {
639 ++par_times;
640
641 parsed += 3;
642 id += 3;
643 }
644 }
645
646 if (!*parent_times) {
647 if (par_times) {
648 *parent_times = par_times;
649 } else {
650 *parent_times = -1;
651 }
652 }
653
654 if (id[0] != '/') {
655 return -parsed;
656 }
657
658 /* skip '/' */
659 ++parsed;
660 ++id;
661
662 /* node-identifier ([prefix:]identifier) */
Michal Vasko50576712017-07-28 12:28:33 +0200663 if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len, NULL, 0)) < 1) {
664 return -parsed - ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200665 }
Michal Vasko3c60cbb2017-07-10 11:50:03 +0200666 if (prefix && !(*prefix)) {
Radek Krejcif7ed4c32016-10-27 16:20:03 +0200667 /* actually we always need prefix even it is not specified */
668 *prefix = lys_main_module(mod)->name;
669 *pref_len = strlen(*prefix);
670 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200671
672 parsed += ret;
673 id += ret;
674
675 /* there is no predicate */
676 if ((id[0] == '/') || !id[0]) {
677 return parsed;
678 } else if (id[0] != '[') {
679 return -parsed;
680 }
681
682 if (has_predicate) {
683 *has_predicate = 1;
684 }
685
686 return parsed;
687}
688
689/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200690 * @brief Parse instance-identifier in JSON data format. That means that prefixes
Michal Vasko1b6ca962017-08-03 14:23:09 +0200691 * are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200692 *
693 * instance-identifier = 1*("/" (node-identifier *predicate))
694 *
Michal Vaskobb211122015-08-19 14:03:11 +0200695 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200696 * @param[out] model Points to the model name.
697 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200698 * @param[out] name Points to the node name.
699 * @param[out] nam_len Length of the node name.
700 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
701 *
702 * @return Number of characters successfully parsed,
703 * positive on success, negative on failure.
704 */
705static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200706parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
707 int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200708{
709 int parsed = 0, ret;
710
Michal Vasko1b6ca962017-08-03 14:23:09 +0200711 assert(id && model && mod_len && name && nam_len);
712
Radek Krejci6dc53a22015-08-17 13:27:59 +0200713 if (has_predicate) {
714 *has_predicate = 0;
715 }
716
717 if (id[0] != '/') {
718 return -parsed;
719 }
720
721 ++parsed;
722 ++id;
723
Michal Vaskob2f40be2016-09-08 16:03:48 +0200724 if ((ret = parse_identifier(id)) < 1) {
725 return ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200726 }
727
Michal Vaskob2f40be2016-09-08 16:03:48 +0200728 *name = id;
729 *nam_len = ret;
730
731 parsed += ret;
732 id += ret;
733
Michal Vasko1b6ca962017-08-03 14:23:09 +0200734 if (id[0] == ':') {
735 /* we have prefix */
736 *model = *name;
737 *mod_len = *nam_len;
738
739 ++parsed;
740 ++id;
741
742 if ((ret = parse_identifier(id)) < 1) {
743 return ret;
744 }
745
746 *name = id;
747 *nam_len = ret;
748
749 parsed += ret;
750 id += ret;
751 }
752
Radek Krejci4967cb62016-09-14 16:40:28 +0200753 if (id[0] == '[' && has_predicate) {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200754 *has_predicate = 1;
755 }
756
757 return parsed;
758}
759
760/**
Michal Vaskof39142b2015-10-21 11:40:05 +0200761 * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes
Michal Vasko1f2cc332015-08-19 11:18:32 +0200762 * (which are mandatory) are actually model names.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200763 *
764 * predicate = "[" *WSP (predicate-expr / pos) *WSP "]"
765 * predicate-expr = (node-identifier / ".") *WSP "=" *WSP
766 * ((DQUOTE string DQUOTE) /
767 * (SQUOTE string SQUOTE))
768 * pos = non-negative-integer-value
769 *
Michal Vaskobb211122015-08-19 14:03:11 +0200770 * @param[in] id Identifier to use.
Michal Vasko1f2cc332015-08-19 11:18:32 +0200771 * @param[out] model Points to the model name.
772 * @param[out] mod_len Length of the model name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200773 * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos.
774 * @param[out] nam_len Length of the node name.
775 * @param[out] value Value the node-identifier must have (string from the grammar),
776 * NULL if there is not any.
777 * @param[out] val_len Length of the value, 0 if there is not any.
778 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
779 *
780 * @return Number of characters successfully parsed,
781 * positive on success, negative on failure.
782 */
783static int
Michal Vaskof39142b2015-10-21 11:40:05 +0200784parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len,
785 const char **value, int *val_len, int *has_predicate)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200786{
787 const char *ptr;
788 int parsed = 0, ret;
789 char quote;
790
791 assert(id);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200792 if (model) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200793 assert(mod_len);
Michal Vasko1f2cc332015-08-19 11:18:32 +0200794 *model = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +0200795 *mod_len = 0;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200796 }
797 if (name) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200798 assert(nam_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200799 *name = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200800 *nam_len = 0;
801 }
802 if (value) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200803 assert(val_len);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200804 *value = NULL;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200805 *val_len = 0;
806 }
807 if (has_predicate) {
808 *has_predicate = 0;
809 }
810
811 if (id[0] != '[') {
812 return -parsed;
813 }
814
815 ++parsed;
816 ++id;
817
818 while (isspace(id[0])) {
819 ++parsed;
820 ++id;
821 }
822
823 /* pos */
824 if (isdigit(id[0])) {
825 if (name) {
826 *name = id;
827 }
828
829 if (id[0] == '0') {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200830 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200831 }
832
833 while (isdigit(id[0])) {
834 ++parsed;
835 ++id;
836 }
837
838 if (nam_len) {
839 *nam_len = id-(*name);
840 }
841
Michal Vaskof2f28a12016-09-09 12:43:06 +0200842 /* "." or node-identifier */
Radek Krejci6dc53a22015-08-17 13:27:59 +0200843 } else {
Michal Vaskof2f28a12016-09-09 12:43:06 +0200844 if (id[0] == '.') {
845 if (name) {
846 *name = id;
847 }
848 if (nam_len) {
849 *nam_len = 1;
850 }
851
852 ++parsed;
853 ++id;
854
855 } else {
Michal Vasko50576712017-07-28 12:28:33 +0200856 if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len, NULL, 0)) < 1) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200857 return -parsed + ret;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200858 }
859
860 parsed += ret;
861 id += ret;
862 }
863
864 while (isspace(id[0])) {
865 ++parsed;
866 ++id;
867 }
868
869 if (id[0] != '=') {
Michal Vasko1f2cc332015-08-19 11:18:32 +0200870 return -parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200871 }
Michal Vasko1f2cc332015-08-19 11:18:32 +0200872
Radek Krejci6dc53a22015-08-17 13:27:59 +0200873 ++parsed;
874 ++id;
875
Michal Vaskof2f28a12016-09-09 12:43:06 +0200876 while (isspace(id[0])) {
877 ++parsed;
878 ++id;
879 }
880
881 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
882 if ((id[0] == '\"') || (id[0] == '\'')) {
883 quote = id[0];
884
885 ++parsed;
886 ++id;
887
888 if ((ptr = strchr(id, quote)) == NULL) {
889 return -parsed;
890 }
Michal Vasko1b6ca962017-08-03 14:23:09 +0200891 ret = ptr - id;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200892
893 if (value) {
894 *value = id;
895 }
896 if (val_len) {
897 *val_len = ret;
898 }
899
Michal Vasko1b6ca962017-08-03 14:23:09 +0200900 parsed += ret + 1;
901 id += ret + 1;
Michal Vaskof2f28a12016-09-09 12:43:06 +0200902 } else {
Radek Krejci6dc53a22015-08-17 13:27:59 +0200903 return -parsed;
904 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200905 }
906
907 while (isspace(id[0])) {
908 ++parsed;
909 ++id;
910 }
911
912 if (id[0] != ']') {
913 return -parsed;
914 }
915
916 ++parsed;
917 ++id;
918
919 if ((id[0] == '[') && has_predicate) {
920 *has_predicate = 1;
921 }
922
923 return parsed;
924}
925
926/**
927 * @brief Parse schema-nodeid.
928 *
929 * schema-nodeid = absolute-schema-nodeid /
930 * descendant-schema-nodeid
931 * absolute-schema-nodeid = 1*("/" node-identifier)
Michal Vasko48935352016-03-29 11:52:36 +0200932 * descendant-schema-nodeid = ["." "/"]
Radek Krejci6dc53a22015-08-17 13:27:59 +0200933 * node-identifier
934 * absolute-schema-nodeid
935 *
Michal Vaskobb211122015-08-19 14:03:11 +0200936 * @param[in] id Identifier to use.
Michal Vasko723e50c2015-10-20 15:20:29 +0200937 * @param[out] mod_name Points to the module name, NULL if there is not any.
938 * @param[out] mod_name_len Length of the module name, 0 if there is not any.
Michal Vasko48935352016-03-29 11:52:36 +0200939 * @param[out] name Points to the node name.
Radek Krejci6dc53a22015-08-17 13:27:59 +0200940 * @param[out] nam_len Length of the node name.
941 * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1
942 * on the first call, must not be changed between consecutive calls.
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100943 * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be
944 * based on the grammar, in those cases use NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200945 * @param[in] extended Whether to accept an extended path (support for /[prefix:]*, //[prefix:]*, //[prefix:].).
Radek Krejci6dc53a22015-08-17 13:27:59 +0200946 *
947 * @return Number of characters successfully parsed,
948 * positive on success, negative on failure.
949 */
Michal Vasko22448d32016-03-16 13:17:29 +0100950int
Michal Vasko723e50c2015-10-20 15:20:29 +0200951parse_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 +0200952 int *is_relative, int *has_predicate, int *all_desc, int extended)
Radek Krejci6dc53a22015-08-17 13:27:59 +0200953{
954 int parsed = 0, ret;
955
956 assert(id);
957 assert(is_relative);
Michal Vasko50576712017-07-28 12:28:33 +0200958
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100959 if (has_predicate) {
960 *has_predicate = 0;
961 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200962
963 if (id[0] != '/') {
964 if (*is_relative != -1) {
965 return -parsed;
966 } else {
967 *is_relative = 1;
968 }
Michal Vasko48935352016-03-29 11:52:36 +0200969 if (!strncmp(id, "./", 2)) {
970 parsed += 2;
971 id += 2;
972 }
Radek Krejci6dc53a22015-08-17 13:27:59 +0200973 } else {
974 if (*is_relative == -1) {
975 *is_relative = 0;
976 }
977 ++parsed;
978 ++id;
979 }
980
Michal Vasko50576712017-07-28 12:28:33 +0200981 if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len, all_desc, extended)) < 1) {
982 return -parsed + ret;
Radek Krejci6dc53a22015-08-17 13:27:59 +0200983 }
984
Michal Vasko83e8e5b2016-03-11 14:29:10 +0100985 parsed += ret;
986 id += ret;
987
988 if ((id[0] == '[') && has_predicate) {
989 *has_predicate = 1;
990 }
991
992 return parsed;
993}
994
995/**
996 * @brief Parse schema predicate (special format internally used).
997 *
998 * predicate = "[" *WSP predicate-expr *WSP "]"
Michal Vasko9fbb6e82017-07-04 13:50:04 +0200999 * predicate-expr = "." / [prefix:]identifier / positive-integer / key-with-value
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001000 * key-with-value = identifier *WSP "=" *WSP
1001 * ((DQUOTE string DQUOTE) /
1002 * (SQUOTE string SQUOTE))
1003 *
1004 * @param[in] id Identifier to use.
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001005 * @param[out] mod_name Points to the list key module name.
1006 * @param[out] mod_name_len Length of \p mod_name.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001007 * @param[out] name Points to the list key name.
1008 * @param[out] nam_len Length of \p name.
Michal Vasko22448d32016-03-16 13:17:29 +01001009 * @param[out] value Points to the key value. If specified, key-with-value is expected.
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001010 * @param[out] val_len Length of \p value.
1011 * @param[out] has_predicate Flag to mark whether there is another predicate specified.
1012 */
Michal Vasko22448d32016-03-16 13:17:29 +01001013int
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001014parse_schema_json_predicate(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len,
1015 const char **value, int *val_len, int *has_predicate)
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001016{
1017 const char *ptr;
1018 int parsed = 0, ret;
1019 char quote;
1020
1021 assert(id);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001022 if (mod_name) {
1023 *mod_name = NULL;
1024 }
1025 if (mod_name_len) {
1026 *mod_name_len = 0;
1027 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001028 if (name) {
1029 *name = NULL;
1030 }
1031 if (nam_len) {
1032 *nam_len = 0;
1033 }
1034 if (value) {
1035 *value = NULL;
1036 }
1037 if (val_len) {
1038 *val_len = 0;
1039 }
1040 if (has_predicate) {
1041 *has_predicate = 0;
1042 }
1043
1044 if (id[0] != '[') {
1045 return -parsed;
1046 }
1047
1048 ++parsed;
1049 ++id;
1050
1051 while (isspace(id[0])) {
1052 ++parsed;
1053 ++id;
1054 }
1055
Michal Vasko22448d32016-03-16 13:17:29 +01001056 /* identifier */
Michal Vasko7b54f7e2016-05-03 15:07:31 +02001057 if (id[0] == '.') {
1058 ret = 1;
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001059
1060 if (name) {
1061 *name = id;
1062 }
1063 if (nam_len) {
1064 *nam_len = ret;
1065 }
Michal Vasko58c2aab2017-01-05 10:02:05 +01001066 } else if (isdigit(id[0])) {
1067 if (id[0] == '0') {
1068 return -parsed;
1069 }
1070 ret = 1;
1071 while (isdigit(id[ret])) {
1072 ++ret;
1073 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02001074
1075 if (name) {
1076 *name = id;
1077 }
1078 if (nam_len) {
1079 *nam_len = ret;
1080 }
Michal Vasko50576712017-07-28 12:28:33 +02001081 } 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 +01001082 return -parsed + ret;
1083 }
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001084
1085 parsed += ret;
1086 id += ret;
1087
1088 while (isspace(id[0])) {
1089 ++parsed;
1090 ++id;
1091 }
1092
1093 /* there is value as well */
1094 if (id[0] == '=') {
Michal Vasko58c2aab2017-01-05 10:02:05 +01001095 if (name && isdigit(**name)) {
1096 return -parsed;
1097 }
1098
Michal Vasko83e8e5b2016-03-11 14:29:10 +01001099 ++parsed;
1100 ++id;
1101
1102 while (isspace(id[0])) {
1103 ++parsed;
1104 ++id;
1105 }
1106
1107 /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */
1108 if ((id[0] == '\"') || (id[0] == '\'')) {
1109 quote = id[0];
1110
1111 ++parsed;
1112 ++id;
1113
1114 if ((ptr = strchr(id, quote)) == NULL) {
1115 return -parsed;
1116 }
1117 ret = ptr - id;
1118
1119 if (value) {
1120 *value = id;
1121 }
1122 if (val_len) {
1123 *val_len = ret;
1124 }
1125
1126 parsed += ret + 1;
1127 id += ret + 1;
1128 } else {
1129 return -parsed;
1130 }
1131
1132 while (isspace(id[0])) {
1133 ++parsed;
1134 ++id;
1135 }
1136 }
1137
1138 if (id[0] != ']') {
1139 return -parsed;
1140 }
1141
1142 ++parsed;
1143 ++id;
1144
1145 if ((id[0] == '[') && has_predicate) {
1146 *has_predicate = 1;
1147 }
1148
1149 return parsed;
Radek Krejci6dc53a22015-08-17 13:27:59 +02001150}
1151
Michal Vasko2d44ee02018-05-18 09:38:51 +02001152#ifdef LY_ENABLED_CACHE
1153
1154static int
1155resolve_hash_table_find_equal(void *val1_p, void *val2_p, int mod, void *UNUSED(cb_data))
1156{
1157 struct lyd_node *val2, *elem2;
1158 struct parsed_pred pp;
1159 const char *str;
1160 int i;
1161
1162 assert(!mod);
Michal Vasko87e29a82018-05-21 13:50:43 +02001163 (void)mod;
Michal Vasko2d44ee02018-05-18 09:38:51 +02001164
1165 pp = *((struct parsed_pred *)val1_p);
1166 val2 = *((struct lyd_node **)val2_p);
1167
1168 if (val2->schema != pp.schema) {
1169 return 0;
1170 }
1171
1172 switch (val2->schema->nodetype) {
1173 case LYS_CONTAINER:
1174 case LYS_LEAF:
1175 case LYS_ANYXML:
1176 case LYS_ANYDATA:
1177 return 1;
1178 case LYS_LEAFLIST:
1179 str = ((struct lyd_node_leaf_list *)val2)->value_str;
1180 if (!strncmp(str, pp.pred[0].value, pp.pred[0].val_len) && !str[pp.pred[0].val_len]) {
1181 return 1;
1182 }
1183 return 0;
1184 case LYS_LIST:
1185 assert(((struct lys_node_list *)val2->schema)->keys_size);
1186 assert(((struct lys_node_list *)val2->schema)->keys_size == pp.len);
1187
1188 /* lists with keys, their equivalence is based on their keys */
1189 elem2 = val2->child;
1190 /* the exact data order is guaranteed */
1191 for (i = 0; elem2 && (i < pp.len); ++i) {
1192 /* module check */
1193 if (pp.pred[i].mod_name) {
1194 if (strncmp(lyd_node_module(elem2)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len)
1195 || lyd_node_module(elem2)->name[pp.pred[i].mod_name_len]) {
1196 break;
1197 }
1198 } else {
1199 if (lyd_node_module(elem2) != lys_node_module(pp.schema)) {
1200 break;
1201 }
1202 }
1203
1204 /* name check */
1205 if (strncmp(elem2->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || elem2->schema->name[pp.pred[i].nam_len]) {
1206 break;
1207 }
1208
1209 /* value check */
1210 str = ((struct lyd_node_leaf_list *)elem2)->value_str;
1211 if (strncmp(str, pp.pred[i].value, pp.pred[i].val_len) || str[pp.pred[i].val_len]) {
1212 break;
1213 }
1214
1215 /* next key */
1216 elem2 = elem2->next;
1217 }
1218 if (i == pp.len) {
1219 return 1;
1220 }
1221 return 0;
1222 default:
1223 break;
1224 }
1225
1226 LOGINT(val2->schema->module->ctx);
1227 return 0;
1228}
1229
1230static struct lyd_node *
1231resolve_json_data_node_hash(struct lyd_node *parent, struct parsed_pred pp)
1232{
1233 values_equal_cb prev_cb;
1234 struct lyd_node **ret = NULL;
1235 uint32_t hash;
1236 int i;
1237
1238 assert(parent && parent->hash);
1239
1240 /* set our value equivalence callback that does not require data nodes */
1241 prev_cb = lyht_set_cb(parent->ht, resolve_hash_table_find_equal);
1242
1243 /* get the hash of the searched node */
1244 hash = dict_hash_multi(0, lys_node_module(pp.schema)->name, strlen(lys_node_module(pp.schema)->name));
1245 hash = dict_hash_multi(hash, pp.schema->name, strlen(pp.schema->name));
1246 if (pp.schema->nodetype == LYS_LEAFLIST) {
1247 assert((pp.len == 1) && (pp.pred[0].name[0] == '.') && (pp.pred[0].nam_len == 1));
1248 /* leaf-list value in predicate */
1249 hash = dict_hash_multi(hash, pp.pred[0].value, pp.pred[0].val_len);
1250 } else if (pp.schema->nodetype == LYS_LIST) {
1251 /* list keys in predicates */
1252 for (i = 0; i < pp.len; ++i) {
1253 hash = dict_hash_multi(hash, pp.pred[i].value, pp.pred[i].val_len);
1254 }
1255 }
1256 hash = dict_hash_multi(hash, NULL, 0);
1257
1258 /* try to find the node */
Michal Vaskod60a1a32018-05-23 16:31:22 +02001259 i = lyht_find(parent->ht, &pp, hash, (void **)&ret);
1260 assert(i || *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001261
1262 /* restore the original callback */
1263 lyht_set_cb(parent->ht, prev_cb);
1264
Michal Vaskod60a1a32018-05-23 16:31:22 +02001265 return (i ? NULL : *ret);
Michal Vasko2d44ee02018-05-18 09:38:51 +02001266}
1267
1268#endif
1269
Radek Krejci6dc53a22015-08-17 13:27:59 +02001270/**
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001271 * @brief Resolve (find) a feature definition. Logs directly.
1272 *
1273 * @param[in] feat_name Feature name to resolve.
1274 * @param[in] len Length of \p feat_name.
1275 * @param[in] node Node with the if-feature expression.
Radek Krejci9ff0a922016-07-14 13:08:05 +02001276 * @param[out] feature Pointer to be set to point to the feature definition, if feature not found
1277 * (return code 1), the pointer is untouched.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001278 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02001279 * @return 0 on success, 1 on forward reference, -1 on error.
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001280 */
1281static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001282resolve_feature(const char *feat_name, uint16_t len, const struct lys_node *node, struct lys_feature **feature)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001283{
1284 char *str;
1285 const char *mod_name, *name;
1286 int mod_name_len, nam_len, i, j;
1287 const struct lys_module *module;
1288
Radek Krejci9ff0a922016-07-14 13:08:05 +02001289 assert(feature);
1290
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001291 /* check prefix */
Michal Vasko50576712017-07-28 12:28:33 +02001292 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 +01001293 LOGVAL(node->module->ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, feat_name[-i], &feat_name[-i]);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001294 return -1;
1295 }
1296
Michal Vasko921eb6b2017-10-13 10:01:39 +02001297 module = lyp_get_module(lys_node_module(node), NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001298 if (!module) {
1299 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01001300 LOGVAL(node->module->ctx, LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001301 return -1;
1302 }
1303
Radek Krejci9ff0a922016-07-14 13:08:05 +02001304 if (module != node->module && module == lys_node_module(node)) {
1305 /* first, try to search directly in submodule where the feature was mentioned */
1306 for (j = 0; j < node->module->features_size; j++) {
1307 if (!strncmp(name, node->module->features[j].name, nam_len) && !node->module->features[j].name[nam_len]) {
1308 /* check status */
1309 if (lyp_check_status(node->flags, lys_node_module(node), node->name, node->module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001310 node->module->features[j].module, node->module->features[j].name, NULL)) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001311 return -1;
1312 }
1313 *feature = &node->module->features[j];
1314 return 0;
1315 }
1316 }
1317 }
1318
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001319 /* search in the identified module ... */
1320 for (j = 0; j < module->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001321 if (!strncmp(name, module->features[j].name, nam_len) && !module->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001322 /* check status */
1323 if (lyp_check_status(node->flags, lys_node_module(node), node->name, module->features[j].flags,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001324 module->features[j].module, module->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001325 return -1;
1326 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001327 *feature = &module->features[j];
1328 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001329 }
1330 }
1331 /* ... and all its submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01001332 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001333 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
Michal Vasko3def8672016-07-01 11:43:09 +02001334 if (!strncmp(name, module->inc[i].submodule->features[j].name, nam_len)
1335 && !module->inc[i].submodule->features[j].name[nam_len]) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001336 /* check status */
1337 if (lyp_check_status(node->flags, lys_node_module(node), node->name,
1338 module->inc[i].submodule->features[j].flags,
1339 module->inc[i].submodule->features[j].module,
Pavol Vicanfdab9f92016-09-07 15:23:27 +02001340 module->inc[i].submodule->features[j].name, NULL)) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001341 return -1;
1342 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001343 *feature = &module->inc[i].submodule->features[j];
1344 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001345 }
1346 }
1347 }
1348
1349 /* not found */
1350 str = strndup(feat_name, len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001351 LOGVAL(node->module->ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", str);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001352 free(str);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001353 return 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001354}
1355
Radek Krejci9ff0a922016-07-14 13:08:05 +02001356/*
1357 * @return
Radek Krejci69b8d922016-07-27 13:13:41 +02001358 * - 1 if enabled
1359 * - 0 if disabled
Radek Krejci9ff0a922016-07-14 13:08:05 +02001360 */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001361static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001362resolve_feature_value(const struct lys_feature *feat)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001363{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001364 int i;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001365
Radek Krejci9ff0a922016-07-14 13:08:05 +02001366 for (i = 0; i < feat->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02001367 if (!resolve_iffeature(&feat->iffeature[i])) {
Radek Krejciaf566332017-02-07 15:56:59 +01001368 return 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001369 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001370 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001371
Radek Krejci69b8d922016-07-27 13:13:41 +02001372 return feat->flags & LYS_FENABLED ? 1 : 0;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001373}
1374
1375static int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001376resolve_iffeature_recursive(struct lys_iffeature *expr, int *index_e, int *index_f)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001377{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001378 uint8_t op;
Radek Krejciaf566332017-02-07 15:56:59 +01001379 int a, b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001380
Radek Krejci9ff0a922016-07-14 13:08:05 +02001381 op = iff_getop(expr->expr, *index_e);
1382 (*index_e)++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001383
Radek Krejci9ff0a922016-07-14 13:08:05 +02001384 switch (op) {
1385 case LYS_IFF_F:
1386 /* resolve feature */
1387 return resolve_feature_value(expr->features[(*index_f)++]);
1388 case LYS_IFF_NOT:
Radek Krejciaf566332017-02-07 15:56:59 +01001389 /* invert result */
1390 return resolve_iffeature_recursive(expr, index_e, index_f) ? 0 : 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001391 case LYS_IFF_AND:
1392 case LYS_IFF_OR:
1393 a = resolve_iffeature_recursive(expr, index_e, index_f);
1394 b = resolve_iffeature_recursive(expr, index_e, index_f);
Radek Krejciaf566332017-02-07 15:56:59 +01001395 if (op == LYS_IFF_AND) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001396 return a && b;
1397 } else { /* LYS_IFF_OR */
1398 return a || b;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001399 }
1400 }
1401
Radek Krejciaf566332017-02-07 15:56:59 +01001402 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001403}
1404
1405int
1406resolve_iffeature(struct lys_iffeature *expr)
1407{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001408 int index_e = 0, index_f = 0;
1409
1410 if (expr->expr) {
Radek Krejciaf566332017-02-07 15:56:59 +01001411 return resolve_iffeature_recursive(expr, &index_e, &index_f);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001412 }
Radek Krejciaf566332017-02-07 15:56:59 +01001413 return 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001414}
1415
1416struct iff_stack {
1417 int size;
1418 int index; /* first empty item */
1419 uint8_t *stack;
1420};
1421
1422static int
1423iff_stack_push(struct iff_stack *stack, uint8_t value)
1424{
1425 if (stack->index == stack->size) {
1426 stack->size += 4;
1427 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001428 LY_CHECK_ERR_RETURN(!stack->stack, LOGMEM(NULL); stack->size = 0, EXIT_FAILURE);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001429 }
1430
1431 stack->stack[stack->index++] = value;
1432 return EXIT_SUCCESS;
1433}
1434
1435static uint8_t
1436iff_stack_pop(struct iff_stack *stack)
1437{
1438 stack->index--;
1439 return stack->stack[stack->index];
1440}
1441
1442static void
1443iff_stack_clean(struct iff_stack *stack)
1444{
1445 stack->size = 0;
1446 free(stack->stack);
1447}
1448
1449static void
1450iff_setop(uint8_t *list, uint8_t op, int pos)
1451{
1452 uint8_t *item;
1453 uint8_t mask = 3;
1454
1455 assert(pos >= 0);
1456 assert(op <= 3); /* max 2 bits */
1457
1458 item = &list[pos / 4];
1459 mask = mask << 2 * (pos % 4);
1460 *item = (*item) & ~mask;
1461 *item = (*item) | (op << 2 * (pos % 4));
1462}
1463
1464uint8_t
1465iff_getop(uint8_t *list, int pos)
1466{
1467 uint8_t *item;
1468 uint8_t mask = 3, result;
1469
1470 assert(pos >= 0);
1471
1472 item = &list[pos / 4];
1473 result = (*item) & (mask << 2 * (pos % 4));
1474 return result >> 2 * (pos % 4);
1475}
1476
1477#define LYS_IFF_LP 0x04 /* ( */
1478#define LYS_IFF_RP 0x08 /* ) */
1479
Radek Krejcicbb473e2016-09-16 14:48:32 +02001480/* internal structure for passing data for UNRES_IFFEAT */
1481struct unres_iffeat_data {
1482 struct lys_node *node;
1483 const char *fname;
Radek Krejci9de2c042016-10-19 16:53:06 +02001484 int infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001485};
1486
Radek Krejci9ff0a922016-07-14 13:08:05 +02001487void
1488resolve_iffeature_getsizes(struct lys_iffeature *iffeat, unsigned int *expr_size, unsigned int *feat_size)
1489{
1490 unsigned int e = 0, f = 0, r = 0;
1491 uint8_t op;
1492
1493 assert(iffeat);
1494
1495 if (!iffeat->expr) {
1496 goto result;
1497 }
1498
1499 do {
1500 op = iff_getop(iffeat->expr, e++);
1501 switch (op) {
1502 case LYS_IFF_NOT:
1503 if (!r) {
1504 r += 1;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001505 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001506 break;
1507 case LYS_IFF_AND:
1508 case LYS_IFF_OR:
1509 if (!r) {
1510 r += 2;
1511 } else {
1512 r += 1;
1513 }
1514 break;
1515 case LYS_IFF_F:
1516 f++;
1517 if (r) {
1518 r--;
1519 }
1520 break;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001521 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001522 } while(r);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001523
Radek Krejci9ff0a922016-07-14 13:08:05 +02001524result:
1525 if (expr_size) {
1526 *expr_size = e;
1527 }
1528 if (feat_size) {
1529 *feat_size = f;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001530 }
1531}
1532
1533int
Radek Krejci9ff0a922016-07-14 13:08:05 +02001534resolve_iffeature_compile(struct lys_iffeature *iffeat_expr, const char *value, struct lys_node *node,
Radek Krejci9de2c042016-10-19 16:53:06 +02001535 int infeature, struct unres_schema *unres)
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001536{
Radek Krejci9ff0a922016-07-14 13:08:05 +02001537 const char *c = value;
1538 int r, rc = EXIT_FAILURE;
Radek Krejci69b8d922016-07-27 13:13:41 +02001539 int i, j, last_not, checkversion = 0;
1540 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001541 uint8_t op;
1542 struct iff_stack stack = {0, 0, NULL};
Radek Krejcicbb473e2016-09-16 14:48:32 +02001543 struct unres_iffeat_data *iff_data;
Michal Vasko53b7da02018-02-13 15:28:42 +01001544 struct ly_ctx *ctx = node->module->ctx;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001545
Radek Krejci9ff0a922016-07-14 13:08:05 +02001546 assert(c);
1547
1548 if (isspace(c[0])) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001549 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_NONE, NULL, c[0], c);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001550 return EXIT_FAILURE;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001551 }
1552
Radek Krejci9ff0a922016-07-14 13:08:05 +02001553 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
1554 for (i = j = last_not = 0; c[i]; i++) {
1555 if (c[i] == '(') {
Radek Krejci69b8d922016-07-27 13:13:41 +02001556 checkversion = 1;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001557 j++;
1558 continue;
1559 } else if (c[i] == ')') {
1560 j--;
1561 continue;
1562 } else if (isspace(c[i])) {
1563 continue;
1564 }
1565
1566 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
1567 if (c[i + r] == '\0') {
Michal Vasko53b7da02018-02-13 15:28:42 +01001568 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001569 return EXIT_FAILURE;
1570 } else if (!isspace(c[i + r])) {
1571 /* feature name starting with the not/and/or */
1572 last_not = 0;
1573 f_size++;
1574 } else if (c[i] == 'n') { /* not operation */
1575 if (last_not) {
1576 /* double not */
1577 expr_size = expr_size - 2;
1578 last_not = 0;
1579 } else {
1580 last_not = 1;
1581 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001582 } else { /* and, or */
1583 f_exp++;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001584 /* not a not operation */
1585 last_not = 0;
1586 }
1587 i += r;
1588 } else {
1589 f_size++;
1590 last_not = 0;
1591 }
1592 expr_size++;
1593
1594 while (!isspace(c[i])) {
1595 if (!c[i] || c[i] == ')') {
1596 i--;
1597 break;
1598 }
1599 i++;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001600 }
1601 }
Radek Krejci69b8d922016-07-27 13:13:41 +02001602 if (j || f_exp != f_size) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001603 /* not matching count of ( and ) */
Michal Vasko53b7da02018-02-13 15:28:42 +01001604 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001605 return EXIT_FAILURE;
1606 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001607
Radek Krejci69b8d922016-07-27 13:13:41 +02001608 if (checkversion || expr_size > 1) {
1609 /* check that we have 1.1 module */
Radek Krejci13fde922018-05-16 10:45:58 +02001610 if (node->module->version != LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001611 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
1612 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 +02001613 return EXIT_FAILURE;
1614 }
1615 }
1616
Radek Krejci9ff0a922016-07-14 13:08:05 +02001617 /* allocate the memory */
1618 iffeat_expr->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iffeat_expr->expr);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001619 iffeat_expr->features = calloc(f_size, sizeof *iffeat_expr->features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001620 stack.stack = malloc(expr_size * sizeof *stack.stack);
Michal Vasko53b7da02018-02-13 15:28:42 +01001621 LY_CHECK_ERR_GOTO(!stack.stack || !iffeat_expr->expr || !iffeat_expr->features, LOGMEM(ctx), error);
Radek Krejciaa1303c2017-05-31 13:57:37 +02001622 stack.size = expr_size;
Radek Krejci9ff0a922016-07-14 13:08:05 +02001623 f_size--; expr_size--; /* used as indexes from now */
1624
1625 for (i--; i >= 0; i--) {
1626 if (c[i] == ')') {
1627 /* push it on stack */
1628 iff_stack_push(&stack, LYS_IFF_RP);
1629 continue;
1630 } else if (c[i] == '(') {
1631 /* pop from the stack into result all operators until ) */
1632 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
1633 iff_setop(iffeat_expr->expr, op, expr_size--);
1634 }
1635 continue;
1636 } else if (isspace(c[i])) {
1637 continue;
1638 }
1639
1640 /* end operator or operand -> find beginning and get what is it */
1641 j = i + 1;
1642 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
1643 i--;
1644 }
1645 i++; /* get back by one step */
1646
Michal Vasko88011352018-07-12 08:49:07 +02001647 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001648 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
1649 /* double not */
1650 iff_stack_pop(&stack);
1651 } else {
1652 /* not has the highest priority, so do not pop from the stack
1653 * as in case of AND and OR */
1654 iff_stack_push(&stack, LYS_IFF_NOT);
1655 }
Michal Vasko88011352018-07-12 08:49:07 +02001656 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001657 /* as for OR - pop from the stack all operators with the same or higher
1658 * priority and store them to the result, then push the AND to the stack */
1659 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
1660 op = iff_stack_pop(&stack);
1661 iff_setop(iffeat_expr->expr, op, expr_size--);
1662 }
1663 iff_stack_push(&stack, LYS_IFF_AND);
Michal Vasko88011352018-07-12 08:49:07 +02001664 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001665 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
1666 op = iff_stack_pop(&stack);
1667 iff_setop(iffeat_expr->expr, op, expr_size--);
1668 }
1669 iff_stack_push(&stack, LYS_IFF_OR);
1670 } else {
1671 /* feature name, length is j - i */
1672
1673 /* add it to the result */
1674 iff_setop(iffeat_expr->expr, LYS_IFF_F, expr_size--);
1675
1676 /* now get the link to the feature definition. Since it can be
Radek Krejcicbb473e2016-09-16 14:48:32 +02001677 * forward referenced, we have to keep the feature name in auxiliary
1678 * structure passed into unres */
1679 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001680 LY_CHECK_ERR_GOTO(!iff_data, LOGMEM(ctx), error);
Radek Krejcicbb473e2016-09-16 14:48:32 +02001681 iff_data->node = node;
1682 iff_data->fname = lydict_insert(node->module->ctx, &c[i], j - i);
Radek Krejci9de2c042016-10-19 16:53:06 +02001683 iff_data->infeature = infeature;
Radek Krejcicbb473e2016-09-16 14:48:32 +02001684 r = unres_schema_add_node(node->module, unres, &iffeat_expr->features[f_size], UNRES_IFFEAT,
1685 (struct lys_node *)iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001686 f_size--;
1687
1688 if (r == -1) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01001689 lydict_remove(node->module->ctx, iff_data->fname);
Pavol Vican4d084512016-09-29 16:38:12 +02001690 free(iff_data);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001691 goto error;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001692 }
1693 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001694 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02001695 while (stack.index) {
1696 op = iff_stack_pop(&stack);
1697 iff_setop(iffeat_expr->expr, op, expr_size--);
1698 }
1699
1700 if (++expr_size || ++f_size) {
1701 /* not all expected operators and operands found */
Michal Vasko53b7da02018-02-13 15:28:42 +01001702 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +02001703 rc = EXIT_FAILURE;
1704 } else {
1705 rc = EXIT_SUCCESS;
1706 }
1707
1708error:
1709 /* cleanup */
1710 iff_stack_clean(&stack);
1711
1712 return rc;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001713}
1714
1715/**
Michal Vasko3edeaf72016-02-11 13:17:43 +01001716 * @brief Resolve (find) a data node based on a schema-nodeid.
1717 *
1718 * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different
1719 * module).
1720 *
1721 */
1722struct lyd_node *
1723resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start)
1724{
1725 char *str, *token, *p;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001726 struct lyd_node *result = NULL, *iter;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001727 const struct lys_node *schema = NULL;
1728
1729 assert(nodeid && start);
1730
1731 if (nodeid[0] == '/') {
1732 return NULL;
1733 }
1734
1735 str = p = strdup(nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01001736 LY_CHECK_ERR_RETURN(!str, LOGMEM(start->schema->module->ctx), NULL);
Radek Krejci5da4eb62016-04-08 14:45:51 +02001737
Michal Vasko3edeaf72016-02-11 13:17:43 +01001738 while (p) {
1739 token = p;
1740 p = strchr(p, '/');
1741 if (p) {
1742 *p = '\0';
1743 p++;
1744 }
1745
Radek Krejci5da4eb62016-04-08 14:45:51 +02001746 if (p) {
1747 /* inner node */
Radek Krejcicc217a62016-04-08 16:58:11 +02001748 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema,
Michal Vaskodc300b02017-04-07 14:09:20 +02001749 LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, &schema)
Radek Krejci5da4eb62016-04-08 14:45:51 +02001750 || !schema) {
Radek Krejcicc217a62016-04-08 16:58:11 +02001751 result = NULL;
1752 break;
Radek Krejci5da4eb62016-04-08 14:45:51 +02001753 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001754
Radek Krejci5da4eb62016-04-08 14:45:51 +02001755 if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
1756 continue;
1757 }
1758 } else {
1759 /* final node */
Michal Vaskodc300b02017-04-07 14:09:20 +02001760 if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, 0, &schema)
Radek Krejcicc217a62016-04-08 16:58:11 +02001761 || !schema) {
1762 result = NULL;
1763 break;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001764 }
1765 }
Radek Krejci5da4eb62016-04-08 14:45:51 +02001766 LY_TREE_FOR(result ? result->child : start, iter) {
1767 if (iter->schema == schema) {
1768 /* move in data tree according to returned schema */
1769 result = iter;
1770 break;
1771 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001772 }
Radek Krejcicc217a62016-04-08 16:58:11 +02001773 if (!iter) {
1774 /* instance not found */
1775 result = NULL;
1776 break;
1777 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001778 }
1779 free(str);
1780
1781 return result;
1782}
1783
Radek Krejci1a9c3612017-04-24 14:49:43 +02001784int
Michal Vasko50576712017-07-28 12:28:33 +02001785schema_nodeid_siblingcheck(const struct lys_node *sibling, const struct lys_module *cur_module, const char *mod_name,
1786 int mod_name_len, const char *name, int nam_len)
Radek Krejcibdf92362016-04-08 14:43:34 +02001787{
1788 const struct lys_module *prefix_mod;
1789
Michal Vaskocdb3f062018-02-01 09:55:06 +01001790 /* handle special names */
1791 if (name[0] == '*') {
1792 return 2;
1793 } else if (name[0] == '.') {
1794 return 3;
1795 }
1796
Michal Vasko50576712017-07-28 12:28:33 +02001797 /* name check */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001798 if (strncmp(name, sibling->name, nam_len) || sibling->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02001799 return 1;
1800 }
1801
Radek Krejcibdf92362016-04-08 14:43:34 +02001802 /* module check */
Michal Vasko50576712017-07-28 12:28:33 +02001803 if (mod_name) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001804 prefix_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vasko50576712017-07-28 12:28:33 +02001805 if (!prefix_mod) {
1806 return -1;
1807 }
1808 } else {
1809 prefix_mod = cur_module;
Radek Krejcibdf92362016-04-08 14:43:34 +02001810 }
1811 if (prefix_mod != lys_node_module(sibling)) {
1812 return 1;
1813 }
1814
Michal Vasko50576712017-07-28 12:28:33 +02001815 /* match */
Michal Vaskocdb3f062018-02-01 09:55:06 +01001816 return 0;
Radek Krejcibdf92362016-04-08 14:43:34 +02001817}
1818
Michal Vasko50576712017-07-28 12:28:33 +02001819/* keys do not have to be ordered and do not have to be all of them */
1820static int
1821resolve_extended_schema_nodeid_predicate(const char *nodeid, const struct lys_node *node,
1822 const struct lys_module *cur_module, int *nodeid_end)
1823{
1824 int mod_len, nam_len, has_predicate, r, i;
1825 const char *model, *name;
1826 struct lys_node_list *list;
1827
1828 if (!(node->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1829 return 1;
1830 }
1831
1832 list = (struct lys_node_list *)node;
1833 do {
1834 r = parse_schema_json_predicate(nodeid, &model, &mod_len, &name, &nam_len, NULL, NULL, &has_predicate);
1835 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001836 LOGVAL(cur_module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, nodeid[r], &nodeid[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001837 return -1;
1838 }
1839 nodeid += r;
1840
1841 if (node->nodetype == LYS_LEAFLIST) {
1842 /* just check syntax */
1843 if (model || !name || (name[0] != '.') || has_predicate) {
1844 return 1;
1845 }
1846 break;
1847 } else {
1848 /* check the key */
1849 for (i = 0; i < list->keys_size; ++i) {
1850 if (strncmp(list->keys[i]->name, name, nam_len) || list->keys[i]->name[nam_len]) {
1851 continue;
1852 }
1853 if (model) {
1854 if (strncmp(lys_node_module((struct lys_node *)list->keys[i])->name, model, mod_len)
1855 || lys_node_module((struct lys_node *)list->keys[i])->name[mod_len]) {
1856 continue;
1857 }
1858 } else {
1859 if (lys_node_module((struct lys_node *)list->keys[i]) != cur_module) {
1860 continue;
1861 }
1862 }
1863
1864 /* match */
1865 break;
1866 }
1867
1868 if (i == list->keys_size) {
1869 return 1;
1870 }
1871 }
1872 } while (has_predicate);
1873
1874 if (!nodeid[0]) {
1875 *nodeid_end = 1;
1876 }
1877 return 0;
1878}
1879
Michal Vasko97234262018-02-01 09:53:01 +01001880/* start_parent - relative, module - absolute, -1 error (logged), EXIT_SUCCESS ok
Radek Krejcidf46e222016-11-08 11:57:37 +01001881 */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001882int
Michal Vasko97234262018-02-01 09:53:01 +01001883resolve_schema_nodeid(const char *nodeid, const struct lys_node *start_parent, const struct lys_module *cur_module,
Michal Vasko50576712017-07-28 12:28:33 +02001884 struct ly_set **ret, int extended, int no_node_error)
Michal Vasko3edeaf72016-02-11 13:17:43 +01001885{
PavolVicanb28bbff2018-02-21 00:44:02 +01001886 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vasko97234262018-02-01 09:53:01 +01001887 const struct lys_node *sibling, *next, *elem;
Michal Vaskobb520442017-05-23 10:55:18 +02001888 struct lys_node_augment *last_aug;
Michal Vasko50576712017-07-28 12:28:33 +02001889 int r, nam_len, mod_name_len = 0, is_relative = -1, all_desc, has_predicate, nodeid_end = 0;
PavolVicanb28bbff2018-02-21 00:44:02 +01001890 int yang_data_name_len, backup_mod_name_len = 0;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001891 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcidaa547a2017-09-22 15:56:27 +02001892 const struct lys_module *start_mod, *aux_mod = NULL;
Michal Vasko50576712017-07-28 12:28:33 +02001893 char *str;
Michal Vasko53b7da02018-02-13 15:28:42 +01001894 struct ly_ctx *ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001895
Michal Vasko97234262018-02-01 09:53:01 +01001896 assert(nodeid && (start_parent || cur_module) && ret);
Michal Vasko50576712017-07-28 12:28:33 +02001897 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001898
Michal Vasko50576712017-07-28 12:28:33 +02001899 if (!cur_module) {
Michal Vasko97234262018-02-01 09:53:01 +01001900 cur_module = lys_node_module(start_parent);
Michal Vasko50576712017-07-28 12:28:33 +02001901 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001902 ctx = cur_module->ctx;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001903 id = nodeid;
1904
PavolVican195cf392018-02-23 13:24:45 +01001905 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL, NULL, 1);
PavolVicanb28bbff2018-02-21 00:44:02 +01001906 if (r < 1) {
1907 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
1908 return -1;
1909 }
1910
1911 if (name[0] == '#') {
1912 if (is_relative) {
1913 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
1914 return -1;
1915 }
1916 yang_data_name = name + 1;
1917 yang_data_name_len = nam_len - 1;
1918 backup_mod_name = mod_name;
1919 backup_mod_name_len = mod_name_len;
1920 id += r;
1921 } else {
1922 is_relative = -1;
1923 }
1924
Michal Vasko50576712017-07-28 12:28:33 +02001925 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
1926 (extended ? &all_desc : NULL), extended);
1927 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001928 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02001929 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001930 }
1931 id += r;
1932
PavolVicanb28bbff2018-02-21 00:44:02 +01001933 if (backup_mod_name) {
1934 mod_name = backup_mod_name;
1935 mod_name_len = backup_mod_name_len;
1936 }
1937
Michal Vasko97234262018-02-01 09:53:01 +01001938 if (is_relative && !start_parent) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001939 LOGVAL(ctx, LYE_SPEC, LY_VLOG_STR, nodeid, "Starting node must be provided for relative paths.");
Michal Vasko3edeaf72016-02-11 13:17:43 +01001940 return -1;
1941 }
1942
1943 /* descendant-schema-nodeid */
1944 if (is_relative) {
Michal Vasko97234262018-02-01 09:53:01 +01001945 cur_module = start_mod = lys_node_module(start_parent);
Michal Vasko24476fa2017-03-08 12:33:48 +01001946
Michal Vasko3edeaf72016-02-11 13:17:43 +01001947 /* absolute-schema-nodeid */
1948 } else {
Michal Vasko921eb6b2017-10-13 10:01:39 +02001949 start_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01001950 if (!start_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001951 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001952 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001953 free(str);
Michal Vaskoe2905632016-02-11 15:42:24 +01001954 return -1;
1955 }
Michal Vasko24476fa2017-03-08 12:33:48 +01001956 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01001957 if (yang_data_name) {
1958 start_parent = lyp_get_yang_data_template(start_mod, yang_data_name, yang_data_name_len);
1959 if (!start_parent) {
1960 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
1961 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
1962 free(str);
1963 return -1;
1964 }
1965 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01001966 }
1967
1968 while (1) {
1969 sibling = NULL;
Michal Vaskobb520442017-05-23 10:55:18 +02001970 last_aug = NULL;
1971
1972 if (start_parent) {
Michal Vasko17315772017-07-10 15:15:39 +02001973 if (mod_name && (strncmp(mod_name, cur_module->name, mod_name_len)
1974 || (mod_name_len != (signed)strlen(cur_module->name)))) {
Michal Vaskobb520442017-05-23 10:55:18 +02001975 /* we are getting into another module (augment) */
Michal Vasko921eb6b2017-10-13 10:01:39 +02001976 aux_mod = lyp_get_module(cur_module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskobb520442017-05-23 10:55:18 +02001977 if (!aux_mod) {
Michal Vasko50576712017-07-28 12:28:33 +02001978 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01001979 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02001980 free(str);
Michal Vaskobb520442017-05-23 10:55:18 +02001981 return -1;
1982 }
1983 } else {
Michal Vasko201c3392017-07-10 15:15:39 +02001984 /* there is no mod_name, so why are we checking augments again?
Michal Vaskobb520442017-05-23 10:55:18 +02001985 * because this module may be not implemented and it augments something in another module and
1986 * there is another augment augmenting that previous one */
Michal Vasko17315772017-07-10 15:15:39 +02001987 aux_mod = cur_module;
Michal Vaskobb520442017-05-23 10:55:18 +02001988 }
1989
Michal Vasko3b2ad462018-07-10 15:40:53 +02001990 /* look into augments */
1991 if (!extended) {
Michal Vaskobb520442017-05-23 10:55:18 +02001992get_next_augment:
1993 last_aug = lys_getnext_target_aug(last_aug, aux_mod, start_parent);
1994 }
1995 }
1996
1997 while ((sibling = lys_getnext(sibling, (last_aug ? (struct lys_node *)last_aug : start_parent), start_mod,
Michal Vaskocb45f472018-02-12 10:47:42 +01001998 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02001999 r = schema_nodeid_siblingcheck(sibling, cur_module, mod_name, mod_name_len, name, nam_len);
2000
2001 /* resolve predicate */
2002 if (extended && ((r == 0) || (r == 2) || (r == 3)) && has_predicate) {
2003 r = resolve_extended_schema_nodeid_predicate(id, sibling, cur_module, &nodeid_end);
2004 if (r == 1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002005 continue;
Michal Vasko50576712017-07-28 12:28:33 +02002006 } else if (r == -1) {
Radek Krejcibdf92362016-04-08 14:43:34 +02002007 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002008 }
Michal Vasko50576712017-07-28 12:28:33 +02002009 } else if (!id[0]) {
2010 nodeid_end = 1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002011 }
Michal Vasko50576712017-07-28 12:28:33 +02002012
2013 if (r == 0) {
2014 /* one matching result */
2015 if (nodeid_end) {
2016 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002017 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002018 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2019 } else {
2020 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
2021 return -1;
2022 }
2023 start_parent = sibling;
2024 }
2025 break;
2026 } else if (r == 1) {
2027 continue;
2028 } else if (r == 2) {
2029 /* "*" */
2030 if (!*ret) {
2031 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002032 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002033 }
2034 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2035 if (all_desc) {
2036 LY_TREE_DFS_BEGIN(sibling, next, elem) {
2037 if (elem != sibling) {
2038 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
2039 }
2040
2041 LY_TREE_DFS_END(sibling, next, elem);
2042 }
2043 }
2044 } else if (r == 3) {
2045 /* "." */
2046 if (!*ret) {
2047 *ret = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01002048 LY_CHECK_ERR_RETURN(!*ret, LOGMEM(ctx), -1);
Michal Vasko50576712017-07-28 12:28:33 +02002049 ly_set_add(*ret, (void *)start_parent, LY_SET_OPT_USEASLIST);
2050 }
2051 ly_set_add(*ret, (void *)sibling, LY_SET_OPT_USEASLIST);
2052 if (all_desc) {
2053 LY_TREE_DFS_BEGIN(sibling, next, elem) {
2054 if (elem != sibling) {
2055 ly_set_add(*ret, (void *)elem, LY_SET_OPT_USEASLIST);
2056 }
2057
2058 LY_TREE_DFS_END(sibling, next, elem);
2059 }
2060 }
2061 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002062 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002063 return -1;
2064 }
2065 }
2066
2067 /* skip predicate */
2068 if (extended && has_predicate) {
2069 while (id[0] == '[') {
2070 id = strchr(id, ']');
2071 if (!id) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002072 LOGINT(ctx);
Michal Vasko50576712017-07-28 12:28:33 +02002073 return -1;
2074 }
2075 ++id;
2076 }
2077 }
2078
2079 if (nodeid_end && ((r == 0) || (r == 2) || (r == 3))) {
2080 return EXIT_SUCCESS;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002081 }
2082
2083 /* no match */
2084 if (!sibling) {
Michal Vaskobb520442017-05-23 10:55:18 +02002085 if (last_aug) {
2086 /* it still could be in another augment */
2087 goto get_next_augment;
2088 }
Michal Vasko50576712017-07-28 12:28:33 +02002089 if (no_node_error) {
2090 str = strndup(nodeid, (name - nodeid) + nam_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01002091 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko50576712017-07-28 12:28:33 +02002092 free(str);
2093 return -1;
2094 }
Michal Vaskoa426fef2016-03-07 10:47:31 +01002095 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002096 return EXIT_SUCCESS;
2097 }
2098
Michal Vasko50576712017-07-28 12:28:33 +02002099 r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate,
2100 (extended ? &all_desc : NULL), extended);
2101 if (r < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002102 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[r], &id[r]);
Michal Vasko50576712017-07-28 12:28:33 +02002103 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002104 }
2105 id += r;
2106 }
2107
2108 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002109 LOGINT(ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002110 return -1;
2111}
2112
Radek Krejcif3c71de2016-04-11 12:45:46 +02002113/* unique, refine,
2114 * >0 - unexpected char on position (ret - 1),
2115 * 0 - ok (but ret can still be NULL),
2116 * -1 - error,
2117 * -2 - violated no_innerlist */
Michal Vasko3edeaf72016-02-11 13:17:43 +01002118int
2119resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype,
Michal Vaskodc300b02017-04-07 14:09:20 +02002120 int no_innerlist, const struct lys_node **ret)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002121{
2122 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002123 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002124 int r, nam_len, mod_name_len, is_relative = -1;
2125 /* resolved import module from the start module, it must match the next node-name-match sibling */
Radek Krejcibdf92362016-04-08 14:43:34 +02002126 const struct lys_module *module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002127
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002128 assert(nodeid && ret);
Radek Krejcie2077412017-01-26 16:03:39 +01002129 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT | LYS_GROUPING)));
Michal Vasko3edeaf72016-02-11 13:17:43 +01002130
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01002131 if (!start) {
2132 /* leaf not found */
2133 return 0;
2134 }
2135
Michal Vasko3edeaf72016-02-11 13:17:43 +01002136 id = nodeid;
Michal Vasko50576712017-07-28 12:28:33 +02002137 module = lys_node_module(start);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002138
Michal Vasko50576712017-07-28 12:28:33 +02002139 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 +01002140 return ((id - nodeid) - r) + 1;
2141 }
2142 id += r;
2143
2144 if (!is_relative) {
2145 return -1;
2146 }
2147
Michal Vasko24476fa2017-03-08 12:33:48 +01002148 start_parent = lys_parent(start);
Michal Vasko74a991b2017-03-31 09:17:22 +02002149 while ((start_parent->nodetype == LYS_USES) && lys_parent(start_parent)) {
Michal Vasko24476fa2017-03-08 12:33:48 +01002150 start_parent = lys_parent(start_parent);
2151 }
2152
Michal Vasko3edeaf72016-02-11 13:17:43 +01002153 while (1) {
2154 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002155 while ((sibling = lys_getnext(sibling, start_parent, module,
Michal Vaskocb45f472018-02-12 10:47:42 +01002156 LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_PARENTUSES | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002157 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2158 if (r == 0) {
2159 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002160 if (!(sibling->nodetype & ret_nodetype)) {
2161 /* wrong node type, too bad */
2162 continue;
2163 }
2164 *ret = sibling;
2165 return EXIT_SUCCESS;
2166 }
Michal Vasko50576712017-07-28 12:28:33 +02002167 start_parent = sibling;
2168 break;
2169 } else if (r == 1) {
2170 continue;
2171 } else {
2172 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002173 }
2174 }
2175
2176 /* no match */
2177 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002178 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002179 return EXIT_SUCCESS;
Radek Krejcif3c71de2016-04-11 12:45:46 +02002180 } else if (no_innerlist && sibling->nodetype == LYS_LIST) {
2181 *ret = NULL;
2182 return -2;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002183 }
2184
Michal Vasko50576712017-07-28 12:28:33 +02002185 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 +01002186 return ((id - nodeid) - r) + 1;
2187 }
2188 id += r;
2189 }
2190
2191 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002192 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002193 return -1;
2194}
2195
2196/* choice default */
2197int
2198resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret)
2199{
2200 /* cannot actually be a path */
2201 if (strchr(nodeid, '/')) {
2202 return -1;
2203 }
2204
Michal Vaskodc300b02017-04-07 14:09:20 +02002205 return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 0, ret);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002206}
2207
2208/* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */
2209static int
2210resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret)
2211{
2212 const struct lys_module *module;
2213 const char *mod_prefix, *name;
2214 int i, mod_prefix_len, nam_len;
2215
2216 /* parse the identifier, it must be parsed on one call */
Michal Vasko50576712017-07-28 12:28:33 +02002217 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 +01002218 return -i + 1;
2219 }
2220
Michal Vasko921eb6b2017-10-13 10:01:39 +02002221 module = lyp_get_module(start->module, mod_prefix, mod_prefix_len, NULL, 0, 0);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002222 if (!module) {
2223 return -1;
2224 }
Radek Krejci0a8205d2017-03-01 16:25:29 +01002225 if (module != lys_main_module(start->module)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002226 start = module->data;
2227 }
2228
2229 *ret = lys_find_grouping_up(name, (struct lys_node *)start);
2230
2231 return EXIT_SUCCESS;
2232}
2233
2234int
2235resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype,
2236 const struct lys_node **ret)
2237{
2238 const char *name, *mod_name, *id;
Michal Vasko24476fa2017-03-08 12:33:48 +01002239 const struct lys_node *sibling, *start_parent;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002240 int r, nam_len, mod_name_len, is_relative = -1;
Radek Krejcibdf92362016-04-08 14:43:34 +02002241 const struct lys_module *abs_start_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002242
2243 assert(nodeid && module && ret);
2244 assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING)));
2245
2246 id = nodeid;
Michal Vasko24476fa2017-03-08 12:33:48 +01002247 start_parent = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002248
Michal Vasko50576712017-07-28 12:28:33 +02002249 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 +01002250 return ((id - nodeid) - r) + 1;
2251 }
2252 id += r;
2253
2254 if (is_relative) {
2255 return -1;
2256 }
2257
Michal Vasko921eb6b2017-10-13 10:01:39 +02002258 abs_start_mod = lyp_get_module(module, NULL, 0, mod_name, mod_name_len, 0);
Michal Vaskoe2905632016-02-11 15:42:24 +01002259 if (!abs_start_mod) {
2260 return -1;
2261 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002262
2263 while (1) {
2264 sibling = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01002265 while ((sibling = lys_getnext(sibling, start_parent, abs_start_mod, LYS_GETNEXT_WITHCHOICE
Michal Vaskocb45f472018-02-12 10:47:42 +01002266 | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING | LYS_GETNEXT_NOSTATECHECK))) {
Michal Vasko50576712017-07-28 12:28:33 +02002267 r = schema_nodeid_siblingcheck(sibling, module, mod_name, mod_name_len, name, nam_len);
2268 if (r == 0) {
2269 if (!id[0]) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002270 if (!(sibling->nodetype & ret_nodetype)) {
2271 /* wrong node type, too bad */
2272 continue;
2273 }
2274 *ret = sibling;
2275 return EXIT_SUCCESS;
2276 }
Michal Vasko50576712017-07-28 12:28:33 +02002277 start_parent = sibling;
2278 break;
2279 } else if (r == 1) {
2280 continue;
2281 } else {
2282 return -1;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002283 }
2284 }
2285
2286 /* no match */
2287 if (!sibling) {
Michal Vaskoa426fef2016-03-07 10:47:31 +01002288 *ret = NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002289 return EXIT_SUCCESS;
2290 }
2291
Michal Vasko50576712017-07-28 12:28:33 +02002292 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 +01002293 return ((id - nodeid) - r) + 1;
2294 }
2295 id += r;
2296 }
2297
2298 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002299 LOGINT(module->ctx);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002300 return -1;
2301}
2302
Michal Vaskoe733d682016-03-14 09:08:27 +01002303static int
Michal Vaskof68a49e2017-08-14 13:23:37 +02002304resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed)
Michal Vaskoe733d682016-03-14 09:08:27 +01002305{
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002306 const char *mod_name, *name;
2307 int mod_name_len, nam_len, has_predicate, i;
2308 struct lys_node *key;
Michal Vaskoe733d682016-03-14 09:08:27 +01002309
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002310 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 +02002311 || !strncmp(name, ".", nam_len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002312 LOGVAL(list->module->ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002313 return -1;
2314 }
2315
2316 predicate += i;
2317 *parsed += i;
2318
Michal Vasko58c2aab2017-01-05 10:02:05 +01002319 if (!isdigit(name[0])) {
2320 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002321 key = (struct lys_node *)list->keys[i];
2322 if (!strncmp(key->name, name, nam_len) && !key->name[nam_len]) {
Michal Vasko50576712017-07-28 12:28:33 +02002323 break;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002324 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002325 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002326
Michal Vasko58c2aab2017-01-05 10:02:05 +01002327 if (i == list->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002328 LOGVAL(list->module->ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name);
Michal Vasko58c2aab2017-01-05 10:02:05 +01002329 return -1;
2330 }
Michal Vaskoe733d682016-03-14 09:08:27 +01002331 }
2332
2333 /* more predicates? */
2334 if (has_predicate) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002335 return resolve_json_schema_list_predicate(predicate, list, parsed);
Michal Vaskoe733d682016-03-14 09:08:27 +01002336 }
2337
2338 return 0;
2339}
2340
Michal Vasko8d26e5c2016-09-08 10:03:49 +02002341/* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly */
Michal Vaskoe733d682016-03-14 09:08:27 +01002342const struct lys_node *
Michal Vaskob3744402017-08-03 14:23:58 +02002343resolve_json_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int output)
Michal Vasko3edeaf72016-02-11 13:17:43 +01002344{
Michal Vasko53b7da02018-02-13 15:28:42 +01002345 char *str;
PavolVicanb28bbff2018-02-21 00:44:02 +01002346 const char *name, *mod_name, *id, *backup_mod_name = NULL, *yang_data_name = NULL;
Michal Vaskob3744402017-08-03 14:23:58 +02002347 const struct lys_node *sibling, *start_parent, *parent;
Michal Vaskodc300b02017-04-07 14:09:20 +02002348 int r, nam_len, mod_name_len, is_relative = -1, has_predicate;
PavolVicanb28bbff2018-02-21 00:44:02 +01002349 int yang_data_name_len, backup_mod_name_len;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002350 /* resolved import module from the start module, it must match the next node-name-match sibling */
Michal Vaskof68a49e2017-08-14 13:23:37 +02002351 const struct lys_module *prefix_mod, *module, *prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002352
Michal Vasko3547c532016-03-14 09:40:50 +01002353 assert(nodeid && (ctx || start));
2354 if (!ctx) {
2355 ctx = start->module->ctx;
2356 }
Michal Vasko3edeaf72016-02-11 13:17:43 +01002357
2358 id = nodeid;
2359
PavolVican195cf392018-02-23 13:24:45 +01002360 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 +01002361 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
2362 return NULL;
2363 }
2364
2365 if (name[0] == '#') {
2366 if (is_relative) {
2367 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
2368 return NULL;
2369 }
2370 yang_data_name = name + 1;
2371 yang_data_name_len = nam_len - 1;
2372 backup_mod_name = mod_name;
2373 backup_mod_name_len = mod_name_len;
2374 id += r;
2375 } else {
2376 is_relative = -1;
2377 }
2378
Michal Vasko50576712017-07-28 12:28:33 +02002379 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 +01002380 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002381 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002382 }
2383 id += r;
2384
PavolVicanb28bbff2018-02-21 00:44:02 +01002385 if (backup_mod_name) {
2386 mod_name = backup_mod_name;
2387 mod_name_len = backup_mod_name_len;
2388 }
2389
Michal Vasko3edeaf72016-02-11 13:17:43 +01002390 if (is_relative) {
Michal Vasko3547c532016-03-14 09:40:50 +01002391 assert(start);
Michal Vasko24476fa2017-03-08 12:33:48 +01002392 start_parent = start;
2393 while (start_parent && (start_parent->nodetype == LYS_USES)) {
2394 start_parent = lys_parent(start_parent);
Michal Vasko3547c532016-03-14 09:40:50 +01002395 }
Michal Vaskof68a49e2017-08-14 13:23:37 +02002396 module = start->module;
Michal Vasko3547c532016-03-14 09:40:50 +01002397 } else {
2398 if (!mod_name) {
Michal Vasko10728b52016-04-07 14:26:29 +02002399 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002400 LOGVAL(ctx, LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid);
Michal Vasko10728b52016-04-07 14:26:29 +02002401 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002402 return NULL;
2403 }
2404
Michal Vasko53b7da02018-02-13 15:28:42 +01002405 str = strndup(mod_name, mod_name_len);
2406 module = ly_ctx_get_module(ctx, str, NULL, 1);
2407 free(str);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002408
Michal Vaskof68a49e2017-08-14 13:23:37 +02002409 if (!module) {
Michal Vasko10728b52016-04-07 14:26:29 +02002410 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002411 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002412 free(str);
Michal Vasko3547c532016-03-14 09:40:50 +01002413 return NULL;
2414 }
Michal Vasko24476fa2017-03-08 12:33:48 +01002415 start_parent = NULL;
PavolVicanb28bbff2018-02-21 00:44:02 +01002416 if (yang_data_name) {
2417 start_parent = lyp_get_yang_data_template(module, yang_data_name, yang_data_name_len);
2418 if (!start_parent) {
2419 str = strndup(nodeid, (yang_data_name + yang_data_name_len) - nodeid);
2420 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
2421 free(str);
2422 return NULL;
2423 }
2424 }
Michal Vasko3547c532016-03-14 09:40:50 +01002425
2426 /* now it's as if there was no module name */
2427 mod_name = NULL;
2428 mod_name_len = 0;
Michal Vaskoe733d682016-03-14 09:08:27 +01002429 }
2430
Michal Vaskof68a49e2017-08-14 13:23:37 +02002431 prev_mod = module;
2432
Michal Vasko3edeaf72016-02-11 13:17:43 +01002433 while (1) {
2434 sibling = NULL;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002435 while ((sibling = lys_getnext(sibling, start_parent, module, 0))) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002436 /* name match */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002437 if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) {
Michal Vaskob3744402017-08-03 14:23:58 +02002438 /* output check */
2439 for (parent = lys_parent(sibling); parent && !(parent->nodetype & (LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2440 if (parent) {
2441 if (output && (parent->nodetype == LYS_INPUT)) {
2442 continue;
2443 } else if (!output && (parent->nodetype == LYS_OUTPUT)) {
2444 continue;
2445 }
2446 }
2447
Michal Vasko3edeaf72016-02-11 13:17:43 +01002448 /* module check */
2449 if (mod_name) {
Michal Vasko8757e7c2016-03-15 10:41:30 +01002450 /* will also find an augment module */
Michal Vasko53b7da02018-02-13 15:28:42 +01002451 prefix_mod = ly_ctx_nget_module(ctx, mod_name, mod_name_len, NULL, 1);
Michal Vasko971a3ca2016-04-01 13:09:29 +02002452
Michal Vasko3edeaf72016-02-11 13:17:43 +01002453 if (!prefix_mod) {
Michal Vasko10728b52016-04-07 14:26:29 +02002454 str = strndup(nodeid, (mod_name + mod_name_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002455 LOGVAL(ctx, LYE_PATH_INMOD, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002456 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002457 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002458 }
2459 } else {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002460 prefix_mod = prev_mod;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002461 }
Michal Vasko4f0dad02016-02-15 14:08:23 +01002462 if (prefix_mod != lys_node_module(sibling)) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002463 continue;
2464 }
2465
Michal Vaskoe733d682016-03-14 09:08:27 +01002466 /* do we have some predicates on it? */
2467 if (has_predicate) {
2468 r = 0;
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002469 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002470 if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, NULL, NULL, &has_predicate)) < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002471 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002472 return NULL;
2473 }
2474 } else if (sibling->nodetype == LYS_LIST) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002475 if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) {
Michal Vasko7b54f7e2016-05-03 15:07:31 +02002476 return NULL;
2477 }
2478 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01002479 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskoe733d682016-03-14 09:08:27 +01002480 return NULL;
Michal Vaskoe733d682016-03-14 09:08:27 +01002481 }
2482 id += r;
2483 }
2484
Michal Vasko3edeaf72016-02-11 13:17:43 +01002485 /* the result node? */
2486 if (!id[0]) {
Michal Vaskoe733d682016-03-14 09:08:27 +01002487 return sibling;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002488 }
2489
Michal Vaskodc300b02017-04-07 14:09:20 +02002490 /* move down the tree, if possible */
2491 if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002492 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
Michal Vaskodc300b02017-04-07 14:09:20 +02002493 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002494 }
Michal Vaskodc300b02017-04-07 14:09:20 +02002495 start_parent = sibling;
Michal Vaskof68a49e2017-08-14 13:23:37 +02002496
2497 /* update prev mod */
2498 prev_mod = (start_parent->child ? lys_node_module(start_parent->child) : module);
Michal Vasko3edeaf72016-02-11 13:17:43 +01002499 break;
2500 }
2501 }
2502
2503 /* no match */
2504 if (!sibling) {
Michal Vasko10728b52016-04-07 14:26:29 +02002505 str = strndup(nodeid, (name + nam_len) - nodeid);
Michal Vasko53b7da02018-02-13 15:28:42 +01002506 LOGVAL(ctx, LYE_PATH_INNODE, LY_VLOG_STR, str);
Michal Vasko10728b52016-04-07 14:26:29 +02002507 free(str);
Michal Vaskoe733d682016-03-14 09:08:27 +01002508 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002509 }
2510
Michal Vasko50576712017-07-28 12:28:33 +02002511 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 +01002512 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskoe733d682016-03-14 09:08:27 +01002513 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002514 }
2515 id += r;
2516 }
2517
2518 /* cannot get here */
Michal Vasko53b7da02018-02-13 15:28:42 +01002519 LOGINT(ctx);
Michal Vaskoe733d682016-03-14 09:08:27 +01002520 return NULL;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002521}
2522
Michal Vasko22448d32016-03-16 13:17:29 +01002523static int
Michal Vasko2d44ee02018-05-18 09:38:51 +02002524resolve_partial_json_data_list_predicate(struct parsed_pred pp, struct lyd_node *node, int position)
Michal Vasko22448d32016-03-16 13:17:29 +01002525{
Michal Vasko22448d32016-03-16 13:17:29 +01002526 uint16_t i;
Michal Vaskof29903d2016-04-18 13:13:10 +02002527 struct lyd_node_leaf_list *key;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002528 struct lys_node_list *slist;
Michal Vasko53b7da02018-02-13 15:28:42 +01002529 struct ly_ctx *ctx;
Michal Vasko22448d32016-03-16 13:17:29 +01002530
Radek Krejci61a86c62016-03-24 11:06:44 +01002531 assert(node);
Michal Vasko22448d32016-03-16 13:17:29 +01002532 assert(node->schema->nodetype == LYS_LIST);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002533 assert(pp.len);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002534
Michal Vasko53b7da02018-02-13 15:28:42 +01002535 ctx = node->schema->module->ctx;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002536 slist = (struct lys_node_list *)node->schema;
Michal Vasko22448d32016-03-16 13:17:29 +01002537
Michal Vasko53adfc72017-01-06 10:39:10 +01002538 /* is the predicate a number? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002539 if (isdigit(pp.pred[0].name[0])) {
2540 if (position == atoi(pp.pred[0].name)) {
Michal Vasko53adfc72017-01-06 10:39:10 +01002541 /* match */
Michal Vasko53adfc72017-01-06 10:39:10 +01002542 return 0;
2543 } else {
2544 /* not a match */
2545 return 1;
2546 }
2547 }
2548
Michal Vaskof29903d2016-04-18 13:13:10 +02002549 key = (struct lyd_node_leaf_list *)node->child;
Michal Vasko53adfc72017-01-06 10:39:10 +01002550 if (!key) {
2551 /* it is not a position, so we need a key for it to be a match */
2552 return 1;
2553 }
2554
2555 /* go through all the keys */
Michal Vaskofebd13d2018-05-17 10:42:24 +02002556 for (i = 0; i < slist->keys_size; ++i) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002557 if (strncmp(key->schema->name, pp.pred[i].name, pp.pred[i].nam_len) || key->schema->name[pp.pred[i].nam_len]) {
2558 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vaskof29903d2016-04-18 13:13:10 +02002559 return -1;
Michal Vasko22448d32016-03-16 13:17:29 +01002560 }
2561
Michal Vasko2d44ee02018-05-18 09:38:51 +02002562 if (pp.pred[i].mod_name) {
Michal Vasko50576712017-07-28 12:28:33 +02002563 /* specific module, check that the found key is from that module */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002564 if (strncmp(lyd_node_module((struct lyd_node *)key)->name, pp.pred[i].mod_name, pp.pred[i].mod_name_len)
2565 || lyd_node_module((struct lyd_node *)key)->name[pp.pred[i].mod_name_len]) {
2566 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002567 return -1;
2568 }
Michal Vasko50576712017-07-28 12:28:33 +02002569
2570 /* but if the module is the same as the parent, it should have been omitted */
2571 if (lyd_node_module((struct lyd_node *)key) == lyd_node_module(node)) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002572 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko50576712017-07-28 12:28:33 +02002573 return -1;
2574 }
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002575 } else {
Michal Vasko50576712017-07-28 12:28:33 +02002576 /* no module, so it must be the same as the list (parent) */
2577 if (lyd_node_module((struct lyd_node *)key) != lyd_node_module(node)) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002578 LOGVAL(ctx, LYE_PATH_INKEY, LY_VLOG_NONE, NULL, pp.pred[i].name);
Michal Vasko9fbb6e82017-07-04 13:50:04 +02002579 return -1;
2580 }
2581 }
2582
Michal Vasko22448d32016-03-16 13:17:29 +01002583 /* value does not match */
Michal Vasko310bc582018-05-22 10:47:59 +02002584 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 +01002585 return 1;
2586 }
Michal Vaskof29903d2016-04-18 13:13:10 +02002587
2588 key = (struct lyd_node_leaf_list *)key->next;
Michal Vasko22448d32016-03-16 13:17:29 +01002589 }
2590
Michal Vasko22448d32016-03-16 13:17:29 +01002591 return 0;
2592}
2593
Radek Krejci45826012016-08-24 15:07:57 +02002594/**
2595 * @brief get the closest parent of the node (or the node itself) identified by the nodeid (path)
2596 *
2597 * @param[in] nodeid Node data path to find
2598 * @param[in] llist_value If the \p nodeid identifies leaf-list, this is expected value of the leaf-list instance.
2599 * @param[in] options Bitmask of options flags, see @ref pathoptions.
2600 * @param[out] parsed Number of characters processed in \p id
2601 * @return The closes parent (or the node itself) from the path
2602 */
Michal Vasko22448d32016-03-16 13:17:29 +01002603struct lyd_node *
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002604resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options,
2605 int *parsed)
Michal Vasko22448d32016-03-16 13:17:29 +01002606{
Michal Vasko2d44ee02018-05-18 09:38:51 +02002607 const char *id, *mod_name, *name, *data_val, *llval;
Michal Vasko58c2aab2017-01-05 10:02:05 +01002608 int r, ret, mod_name_len, nam_len, is_relative = -1, list_instance_position;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002609 int has_predicate, last_parsed = 0, llval_len;
Michal Vasko238bd2f2016-03-23 09:39:01 +01002610 struct lyd_node *sibling, *last_match = NULL;
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002611 struct lyd_node_leaf_list *llist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002612 const struct lys_module *prev_mod;
Michal Vasko22448d32016-03-16 13:17:29 +01002613 struct ly_ctx *ctx;
Michal Vasko20964782018-08-01 15:14:30 +02002614 const struct lys_node *ssibling, *sparent;
Michal Vasko310bc582018-05-22 10:47:59 +02002615 struct lys_node_list *slist;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002616 struct parsed_pred pp;
Michal Vasko22448d32016-03-16 13:17:29 +01002617
2618 assert(nodeid && start && parsed);
2619
Michal Vasko2d44ee02018-05-18 09:38:51 +02002620 memset(&pp, 0, sizeof pp);
Michal Vasko22448d32016-03-16 13:17:29 +01002621 ctx = start->schema->module->ctx;
2622 id = nodeid;
2623
Michal Vasko2d44ee02018-05-18 09:38:51 +02002624 /* parse first nodeid in case it is yang-data extension */
PavolVican195cf392018-02-23 13:24:45 +01002625 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 +01002626 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002627 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002628 }
2629
2630 if (name[0] == '#') {
2631 if (is_relative) {
2632 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, '#', name);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002633 goto error;
PavolVicanb28bbff2018-02-21 00:44:02 +01002634 }
PavolVicanb28bbff2018-02-21 00:44:02 +01002635 id += r;
2636 last_parsed = r;
2637 } else {
2638 is_relative = -1;
2639 }
2640
Michal Vasko2d44ee02018-05-18 09:38:51 +02002641 /* parse first nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002642 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 +01002643 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002644 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002645 }
2646 id += r;
2647 /* add it to parsed only after the data node was actually found */
PavolVicanb28bbff2018-02-21 00:44:02 +01002648 last_parsed += r;
Michal Vasko22448d32016-03-16 13:17:29 +01002649
2650 if (is_relative) {
Michal Vaskof68a49e2017-08-14 13:23:37 +02002651 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002652 start = start->child;
2653 } else {
2654 for (; start->parent; start = start->parent);
Michal Vaskof68a49e2017-08-14 13:23:37 +02002655 prev_mod = lyd_node_module(start);
Michal Vasko22448d32016-03-16 13:17:29 +01002656 }
2657
Michal Vaskofebd13d2018-05-17 10:42:24 +02002658 /* do not duplicate code, use predicate parsing from the loop */
2659 goto parse_predicates;
2660
Michal Vasko22448d32016-03-16 13:17:29 +01002661 while (1) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002662 /* find the correct schema node first */
2663 ssibling = NULL;
Michal Vasko20964782018-08-01 15:14:30 +02002664 sparent = (start && start->parent) ? start->parent->schema : NULL;
2665 while ((ssibling = lys_getnext(ssibling, sparent, prev_mod, 0))) {
2666 /* skip invalid input/output nodes */
2667 if (sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) {
2668 if (options & LYD_PATH_OPT_OUTPUT) {
2669 if (lys_parent(ssibling)->nodetype == LYS_INPUT) {
2670 continue;
2671 }
2672 } else {
2673 if (lys_parent(ssibling)->nodetype == LYS_OUTPUT) {
2674 continue;
2675 }
2676 }
2677 }
2678
Michal Vasko2d44ee02018-05-18 09:38:51 +02002679 if (!schema_nodeid_siblingcheck(ssibling, prev_mod, mod_name, mod_name_len, name, nam_len)) {
2680 break;
Michal Vasko2411b942016-03-23 13:50:03 +01002681 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002682 }
2683 if (!ssibling) {
2684 /* there is not even such a schema node */
2685 free(pp.pred);
2686 return last_match;
2687 }
2688 pp.schema = ssibling;
Michal Vasko2411b942016-03-23 13:50:03 +01002689
Michal Vasko2d44ee02018-05-18 09:38:51 +02002690 /* unify leaf-list value - it is possible to specify last-node value as both a predicate or parameter if
2691 * is a leaf-list, unify both cases and the value will in both cases be in the predicate structure */
2692 if (!id[0] && !pp.len && (ssibling->nodetype == LYS_LEAFLIST)) {
2693 pp.len = 1;
2694 pp.pred = calloc(1, sizeof *pp.pred);
2695 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
Michal Vasko22448d32016-03-16 13:17:29 +01002696
Michal Vasko2d44ee02018-05-18 09:38:51 +02002697 pp.pred[0].name = ".";
2698 pp.pred[0].nam_len = 1;
2699 pp.pred[0].value = (llist_value ? llist_value : "");
2700 pp.pred[0].val_len = strlen(pp.pred[0].value);
2701 }
Michal Vasko971a3ca2016-04-01 13:09:29 +02002702
Michal Vasko310bc582018-05-22 10:47:59 +02002703 if (ssibling->nodetype & (LYS_LEAFLIST | LYS_LEAF)) {
2704 /* check leaf/leaf-list predicate */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002705 if (pp.len > 1) {
2706 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2707 goto error;
Michal Vasko310bc582018-05-22 10:47:59 +02002708 } else if (pp.len) {
2709 if ((pp.pred[0].name[0] != '.') || (pp.pred[0].nam_len != 1)) {
2710 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, pp.pred[0].name[0], pp.pred[0].name);
2711 goto error;
2712 }
2713 if ((((struct lys_node_leaf *)ssibling)->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[0].value, ':', pp.pred[0].val_len)) {
2714 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, ssibling, pp.pred[0].val_len, pp.pred[0].value);
2715 goto error;
2716 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002717 }
2718 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko310bc582018-05-22 10:47:59 +02002719 /* list should have predicates for all the keys or position */
2720 slist = (struct lys_node_list *)ssibling;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002721 if (!pp.len) {
2722 /* none match */
2723 return last_match;
Michal Vasko310bc582018-05-22 10:47:59 +02002724 } else if (!isdigit(pp.pred[0].name[0])) {
2725 /* list predicate is not a position, so there must be all the keys */
2726 if (pp.len > slist->keys_size) {
2727 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2728 goto error;
2729 } else if (pp.len < slist->keys_size) {
2730 LOGVAL(ctx, LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, slist->keys[pp.len]->name);
2731 goto error;
2732 }
2733 /* check that all identityrefs have module name, otherwise the hash of the list instance will never match!! */
2734 for (r = 0; r < pp.len; ++r) {
2735 if ((slist->keys[r]->type.base == LY_TYPE_IDENT) && !strnchr(pp.pred[r].value, ':', pp.pred[r].val_len)) {
2736 LOGVAL(ctx, LYE_PATH_INIDENTREF, LY_VLOG_LYS, slist->keys[r], pp.pred[r].val_len, pp.pred[r].value);
2737 goto error;
2738 }
2739 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002740 }
2741 } else if (pp.pred) {
2742 /* no other nodes allow predicates */
2743 LOGVAL(ctx, LYE_PATH_PREDTOOMANY, LY_VLOG_NONE, NULL);
2744 goto error;
2745 }
2746
2747#ifdef LY_ENABLED_CACHE
2748 /* we will not be matching keyless lists or state leaf-lists this way */
2749 if (start->parent && start->parent->ht && ((pp.schema->nodetype != LYS_LIST) || ((struct lys_node_list *)pp.schema)->keys_size)
2750 && ((pp.schema->nodetype != LYS_LEAFLIST) || (pp.schema->flags & LYS_CONFIG_W))) {
2751 sibling = resolve_json_data_node_hash(start->parent, pp);
2752 } else
2753#endif
2754 {
2755 list_instance_position = 0;
2756 LY_TREE_FOR(start, sibling) {
2757 /* RPC/action data check, return simply invalid argument, because the data tree is invalid */
2758 if (lys_parent(sibling->schema)) {
2759 if (options & LYD_PATH_OPT_OUTPUT) {
2760 if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) {
2761 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name);
2762 goto error;
2763 }
2764 } else {
2765 if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) {
2766 LOGERR(ctx, LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name);
2767 goto error;
2768 }
Michal Vasko22448d32016-03-16 13:17:29 +01002769 }
Michal Vasko22448d32016-03-16 13:17:29 +01002770 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002771
2772 if (sibling->schema != ssibling) {
2773 /* wrong schema node */
Michal Vasko22448d32016-03-16 13:17:29 +01002774 continue;
2775 }
2776
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002777 /* leaf-list, did we find it with the correct value or not? */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002778 if (ssibling->nodetype == LYS_LEAFLIST) {
2779 if (ssibling->flags & LYS_CONFIG_R) {
Michal Vasko24affa02018-04-03 09:06:06 +02002780 /* state leaf-lists will never match */
2781 continue;
2782 }
2783
Michal Vasko9ba34de2016-12-07 12:21:19 +01002784 llist = (struct lyd_node_leaf_list *)sibling;
2785
Michal Vasko2d44ee02018-05-18 09:38:51 +02002786 /* get the expected leaf-list value */
2787 llval = NULL;
2788 llval_len = 0;
2789 if (pp.pred) {
2790 /* it was already checked that it is correct */
2791 llval = pp.pred[0].value;
2792 llval_len = pp.pred[0].val_len;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002793
Michal Vaskof0a50972016-10-19 11:33:55 +02002794 }
2795
Michal Vasko21b90ce2017-09-19 09:38:27 +02002796 /* make value canonical (remove module name prefix) unless it was specified with it */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002797 if (llval && !strchr(llval, ':') && (llist->value_type & LY_TYPE_IDENT)
Michal Vasko6a938d62016-12-21 09:21:30 +01002798 && !strncmp(llist->value_str, lyd_node_module(sibling)->name, strlen(lyd_node_module(sibling)->name))
2799 && (llist->value_str[strlen(lyd_node_module(sibling)->name)] == ':')) {
Michal Vasko9ba34de2016-12-07 12:21:19 +01002800 data_val = llist->value_str + strlen(lyd_node_module(sibling)->name) + 1;
2801 } else {
2802 data_val = llist->value_str;
2803 }
2804
Michal Vasko2d44ee02018-05-18 09:38:51 +02002805 if ((!llval && data_val && data_val[0]) || (llval && (strncmp(llval, data_val, llval_len)
2806 || data_val[llval_len]))) {
Michal Vasko13eb4ac2016-04-06 12:19:37 +02002807 continue;
2808 }
Michal Vasko9ba34de2016-12-07 12:21:19 +01002809
Michal Vasko2d44ee02018-05-18 09:38:51 +02002810 } else if (ssibling->nodetype == LYS_LIST) {
Michal Vasko58c2aab2017-01-05 10:02:05 +01002811 /* 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 +01002812 ++list_instance_position;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002813 ret = resolve_partial_json_data_list_predicate(pp, sibling, list_instance_position);
Michal Vasko22448d32016-03-16 13:17:29 +01002814 if (ret == -1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002815 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002816 } else if (ret == 1) {
2817 /* this list instance does not match */
2818 continue;
2819 }
Michal Vasko22448d32016-03-16 13:17:29 +01002820 }
2821
Michal Vasko22448d32016-03-16 13:17:29 +01002822 break;
2823 }
2824 }
2825
2826 /* no match, return last match */
2827 if (!sibling) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002828 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002829 return last_match;
2830 }
2831
Michal Vasko2d44ee02018-05-18 09:38:51 +02002832 /* we found a next matching node */
2833 *parsed += last_parsed;
Michal Vasko586831d2018-05-22 11:13:16 +02002834 last_match = sibling;
2835 prev_mod = lyd_node_module(sibling);
Michal Vasko2d44ee02018-05-18 09:38:51 +02002836
2837 /* the result node? */
2838 if (!id[0]) {
2839 free(pp.pred);
Michal Vasko586831d2018-05-22 11:13:16 +02002840 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002841 }
2842
Michal Vasko586831d2018-05-22 11:13:16 +02002843 /* move down the tree, if possible, and continue */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002844 if (ssibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko586831d2018-05-22 11:13:16 +02002845 /* there can be no children even through expected, error */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002846 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2847 goto error;
Michal Vasko586831d2018-05-22 11:13:16 +02002848 } else if (!sibling->child) {
2849 /* there could be some children, but are not, return what we found so far */
2850 free(pp.pred);
2851 return last_match;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002852 }
Michal Vasko2d44ee02018-05-18 09:38:51 +02002853 start = sibling->child;
2854
Michal Vaskofebd13d2018-05-17 10:42:24 +02002855 /* parse nodeid */
Michal Vasko50576712017-07-28 12:28:33 +02002856 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 +01002857 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]);
Michal Vaskofebd13d2018-05-17 10:42:24 +02002858 goto error;
Michal Vasko22448d32016-03-16 13:17:29 +01002859 }
2860 id += r;
2861 last_parsed = r;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002862
2863parse_predicates:
2864 /* parse all the predicates */
Michal Vasko2d44ee02018-05-18 09:38:51 +02002865 free(pp.pred);
2866 pp.schema = NULL;
2867 pp.len = 0;
2868 pp.pred = NULL;
Michal Vaskofebd13d2018-05-17 10:42:24 +02002869 while (has_predicate) {
Michal Vasko2d44ee02018-05-18 09:38:51 +02002870 ++pp.len;
2871 pp.pred = ly_realloc(pp.pred, pp.len * sizeof *pp.pred);
2872 LY_CHECK_ERR_GOTO(!pp.pred, LOGMEM(ctx), error);
2873 if ((r = parse_schema_json_predicate(id, &pp.pred[pp.len - 1].mod_name, &pp.pred[pp.len - 1].mod_name_len,
2874 &pp.pred[pp.len - 1].name, &pp.pred[pp.len - 1].nam_len, &pp.pred[pp.len - 1].value,
2875 &pp.pred[pp.len - 1].val_len, &has_predicate)) < 1) {
Michal Vaskofebd13d2018-05-17 10:42:24 +02002876 LOGVAL(ctx, LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id);
2877 goto error;
2878 }
2879
2880 id += r;
2881 last_parsed += r;
2882 }
Michal Vasko22448d32016-03-16 13:17:29 +01002883 }
2884
Michal Vaskofebd13d2018-05-17 10:42:24 +02002885error:
Michal Vasko238bd2f2016-03-23 09:39:01 +01002886 *parsed = -1;
Michal Vasko2d44ee02018-05-18 09:38:51 +02002887 free(pp.pred);
Michal Vasko22448d32016-03-16 13:17:29 +01002888 return NULL;
2889}
2890
Michal Vasko3edeaf72016-02-11 13:17:43 +01002891/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02002892 * @brief Resolves length or range intervals. Does not log.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002893 * Syntax is assumed to be correct, *ret MUST be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002894 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002895 * @param[in] ctx Context for errors.
Michal Vaskoaeb51802016-04-11 10:58:47 +02002896 * @param[in] str_restr Restriction as a string.
2897 * @param[in] type Type of the restriction.
2898 * @param[out] ret Final interval structure that starts with
2899 * the interval of the initial type, continues with intervals
2900 * of any superior types derived from the initial one, and
2901 * finishes with intervals from our \p type.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002902 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002903 * @return EXIT_SUCCESS on succes, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02002904 */
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002905int
Michal Vasko53b7da02018-02-13 15:28:42 +01002906resolve_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 +02002907{
2908 /* 0 - unsigned, 1 - signed, 2 - floating point */
Michal Vaskoaeb51802016-04-11 10:58:47 +02002909 int kind;
Michal Vaskof75b2772018-03-14 09:55:33 +01002910 int64_t local_smin = 0, local_smax = 0, local_fmin, local_fmax;
2911 uint64_t local_umin, local_umax = 0;
2912 uint8_t local_fdig = 0;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002913 const char *seg_ptr, *ptr;
Michal Vaskoaeb51802016-04-11 10:58:47 +02002914 struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002915
2916 switch (type->base) {
2917 case LY_TYPE_BINARY:
2918 kind = 0;
2919 local_umin = 0;
2920 local_umax = 18446744073709551615UL;
2921
2922 if (!str_restr && type->info.binary.length) {
2923 str_restr = type->info.binary.length->expr;
2924 }
2925 break;
2926 case LY_TYPE_DEC64:
2927 kind = 2;
Michal Vasko4d1f0482016-09-19 14:35:06 +02002928 local_fmin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2929 local_fmax = __INT64_C(9223372036854775807);
2930 local_fdig = type->info.dec64.dig;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002931
2932 if (!str_restr && type->info.dec64.range) {
2933 str_restr = type->info.dec64.range->expr;
2934 }
2935 break;
2936 case LY_TYPE_INT8:
2937 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002938 local_smin = __INT64_C(-128);
2939 local_smax = __INT64_C(127);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002940
2941 if (!str_restr && type->info.num.range) {
2942 str_restr = type->info.num.range->expr;
2943 }
2944 break;
2945 case LY_TYPE_INT16:
2946 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002947 local_smin = __INT64_C(-32768);
2948 local_smax = __INT64_C(32767);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002949
2950 if (!str_restr && type->info.num.range) {
2951 str_restr = type->info.num.range->expr;
2952 }
2953 break;
2954 case LY_TYPE_INT32:
2955 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002956 local_smin = __INT64_C(-2147483648);
2957 local_smax = __INT64_C(2147483647);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002958
2959 if (!str_restr && type->info.num.range) {
2960 str_restr = type->info.num.range->expr;
2961 }
2962 break;
2963 case LY_TYPE_INT64:
2964 kind = 1;
Radek Krejcif56577b2015-10-29 14:05:25 +01002965 local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1);
2966 local_smax = __INT64_C(9223372036854775807);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002967
2968 if (!str_restr && type->info.num.range) {
2969 str_restr = type->info.num.range->expr;
2970 }
2971 break;
2972 case LY_TYPE_UINT8:
2973 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002974 local_umin = __UINT64_C(0);
2975 local_umax = __UINT64_C(255);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002976
2977 if (!str_restr && type->info.num.range) {
2978 str_restr = type->info.num.range->expr;
2979 }
2980 break;
2981 case LY_TYPE_UINT16:
2982 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002983 local_umin = __UINT64_C(0);
2984 local_umax = __UINT64_C(65535);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002985
2986 if (!str_restr && type->info.num.range) {
2987 str_restr = type->info.num.range->expr;
2988 }
2989 break;
2990 case LY_TYPE_UINT32:
2991 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01002992 local_umin = __UINT64_C(0);
2993 local_umax = __UINT64_C(4294967295);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02002994
2995 if (!str_restr && type->info.num.range) {
2996 str_restr = type->info.num.range->expr;
2997 }
2998 break;
2999 case LY_TYPE_UINT64:
3000 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01003001 local_umin = __UINT64_C(0);
3002 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003003
3004 if (!str_restr && type->info.num.range) {
3005 str_restr = type->info.num.range->expr;
3006 }
3007 break;
3008 case LY_TYPE_STRING:
3009 kind = 0;
Radek Krejcif56577b2015-10-29 14:05:25 +01003010 local_umin = __UINT64_C(0);
3011 local_umax = __UINT64_C(18446744073709551615);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003012
3013 if (!str_restr && type->info.str.length) {
3014 str_restr = type->info.str.length->expr;
3015 }
3016 break;
3017 default:
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003018 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003019 }
3020
3021 /* process superior types */
Michal Vaskoaeb51802016-04-11 10:58:47 +02003022 if (type->der) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003023 if (resolve_len_ran_interval(ctx, NULL, &type->der->type, &intv)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003024 return -1;
Michal Vasko0c888fd2015-08-11 15:54:08 +02003025 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003026 assert(!intv || (intv->kind == kind));
3027 }
3028
3029 if (!str_restr) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003030 /* we do not have any restriction, return superior ones */
3031 *ret = intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003032 return EXIT_SUCCESS;
3033 }
3034
3035 /* adjust local min and max */
3036 if (intv) {
3037 tmp_intv = intv;
3038
3039 if (kind == 0) {
3040 local_umin = tmp_intv->value.uval.min;
3041 } else if (kind == 1) {
3042 local_smin = tmp_intv->value.sval.min;
3043 } else if (kind == 2) {
3044 local_fmin = tmp_intv->value.fval.min;
3045 }
3046
3047 while (tmp_intv->next) {
3048 tmp_intv = tmp_intv->next;
3049 }
3050
3051 if (kind == 0) {
3052 local_umax = tmp_intv->value.uval.max;
3053 } else if (kind == 1) {
3054 local_smax = tmp_intv->value.sval.max;
3055 } else if (kind == 2) {
3056 local_fmax = tmp_intv->value.fval.max;
3057 }
3058 }
3059
3060 /* finally parse our restriction */
3061 seg_ptr = str_restr;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003062 tmp_intv = NULL;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003063 while (1) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003064 if (!tmp_local_intv) {
3065 assert(!local_intv);
3066 local_intv = malloc(sizeof *local_intv);
3067 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003068 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003069 tmp_local_intv->next = malloc(sizeof *tmp_local_intv);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003070 tmp_local_intv = tmp_local_intv->next;
3071 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003072 LY_CHECK_ERR_GOTO(!tmp_local_intv, LOGMEM(ctx), error);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003073
3074 tmp_local_intv->kind = kind;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003075 tmp_local_intv->type = type;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003076 tmp_local_intv->next = NULL;
3077
3078 /* min */
3079 ptr = seg_ptr;
3080 while (isspace(ptr[0])) {
3081 ++ptr;
3082 }
3083 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3084 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003085 tmp_local_intv->value.uval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003086 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003087 tmp_local_intv->value.sval.min = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003088 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003089 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.min)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003090 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003091 goto error;
3092 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003093 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003094 } else if (!strncmp(ptr, "min", 3)) {
3095 if (kind == 0) {
3096 tmp_local_intv->value.uval.min = local_umin;
3097 } else if (kind == 1) {
3098 tmp_local_intv->value.sval.min = local_smin;
3099 } else if (kind == 2) {
3100 tmp_local_intv->value.fval.min = local_fmin;
3101 }
3102
3103 ptr += 3;
3104 } else if (!strncmp(ptr, "max", 3)) {
3105 if (kind == 0) {
3106 tmp_local_intv->value.uval.min = local_umax;
3107 } else if (kind == 1) {
3108 tmp_local_intv->value.sval.min = local_smax;
3109 } else if (kind == 2) {
3110 tmp_local_intv->value.fval.min = local_fmax;
3111 }
3112
3113 ptr += 3;
3114 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003115 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003116 }
3117
3118 while (isspace(ptr[0])) {
3119 ptr++;
3120 }
3121
3122 /* no interval or interval */
3123 if ((ptr[0] == '|') || !ptr[0]) {
3124 if (kind == 0) {
3125 tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min;
3126 } else if (kind == 1) {
3127 tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min;
3128 } else if (kind == 2) {
3129 tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min;
3130 }
3131 } else if (!strncmp(ptr, "..", 2)) {
3132 /* skip ".." */
3133 ptr += 2;
3134 while (isspace(ptr[0])) {
3135 ++ptr;
3136 }
3137
3138 /* max */
3139 if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) {
3140 if (kind == 0) {
Radek Krejci25894412017-07-11 10:53:16 +02003141 tmp_local_intv->value.uval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003142 } else if (kind == 1) {
Radek Krejci25894412017-07-11 10:53:16 +02003143 tmp_local_intv->value.sval.max = strtoll(ptr, (char **)&ptr, 10);
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003144 } else if (kind == 2) {
Michal Vaskod24dd012016-09-30 12:20:22 +02003145 if (parse_range_dec64(&ptr, local_fdig, &tmp_local_intv->value.fval.max)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003146 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, ptr, "range");
Michal Vaskod24dd012016-09-30 12:20:22 +02003147 goto error;
3148 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003149 }
3150 } else if (!strncmp(ptr, "max", 3)) {
3151 if (kind == 0) {
3152 tmp_local_intv->value.uval.max = local_umax;
3153 } else if (kind == 1) {
3154 tmp_local_intv->value.sval.max = local_smax;
3155 } else if (kind == 2) {
3156 tmp_local_intv->value.fval.max = local_fmax;
3157 }
3158 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003159 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003160 }
3161 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003162 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003163 }
3164
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003165 /* check min and max in correct order*/
3166 if (kind == 0) {
3167 /* current segment */
3168 if (tmp_local_intv->value.uval.min > tmp_local_intv->value.uval.max) {
3169 goto error;
3170 }
3171 if (tmp_local_intv->value.uval.min < local_umin || tmp_local_intv->value.uval.max > local_umax) {
3172 goto error;
3173 }
3174 /* segments sholud be ascending order */
Pavol Vican69f62c92016-08-30 09:06:25 +02003175 if (tmp_intv && (tmp_intv->value.uval.max >= tmp_local_intv->value.uval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003176 goto error;
3177 }
3178 } else if (kind == 1) {
3179 if (tmp_local_intv->value.sval.min > tmp_local_intv->value.sval.max) {
3180 goto error;
3181 }
3182 if (tmp_local_intv->value.sval.min < local_smin || tmp_local_intv->value.sval.max > local_smax) {
3183 goto error;
3184 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003185 if (tmp_intv && (tmp_intv->value.sval.max >= tmp_local_intv->value.sval.min)) {
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003186 goto error;
3187 }
3188 } else if (kind == 2) {
3189 if (tmp_local_intv->value.fval.min > tmp_local_intv->value.fval.max) {
3190 goto error;
3191 }
3192 if (tmp_local_intv->value.fval.min < local_fmin || tmp_local_intv->value.fval.max > local_fmax) {
3193 goto error;
3194 }
Pavol Vican69f62c92016-08-30 09:06:25 +02003195 if (tmp_intv && (tmp_intv->value.fval.max >= tmp_local_intv->value.fval.min)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003196 /* fraction-digits value is always the same (it cannot be changed in derived types) */
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003197 goto error;
3198 }
3199 }
3200
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003201 /* next segment (next OR) */
3202 seg_ptr = strchr(seg_ptr, '|');
3203 if (!seg_ptr) {
3204 break;
3205 }
3206 seg_ptr++;
Pavol Vican9e7c01d2016-08-29 09:36:17 +02003207 tmp_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003208 }
3209
3210 /* check local restrictions against superior ones */
3211 if (intv) {
3212 tmp_intv = intv;
Michal Vaskoaeb51802016-04-11 10:58:47 +02003213 tmp_local_intv = local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003214
3215 while (tmp_local_intv && tmp_intv) {
3216 /* reuse local variables */
3217 if (kind == 0) {
3218 local_umin = tmp_local_intv->value.uval.min;
3219 local_umax = tmp_local_intv->value.uval.max;
3220
3221 /* it must be in this interval */
3222 if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) {
3223 /* this interval is covered, next one */
3224 if (local_umax <= tmp_intv->value.uval.max) {
3225 tmp_local_intv = tmp_local_intv->next;
3226 continue;
3227 /* ascending order of restrictions -> fail */
3228 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003229 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003230 }
3231 }
3232 } else if (kind == 1) {
3233 local_smin = tmp_local_intv->value.sval.min;
3234 local_smax = tmp_local_intv->value.sval.max;
3235
3236 if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) {
3237 if (local_smax <= tmp_intv->value.sval.max) {
3238 tmp_local_intv = tmp_local_intv->next;
3239 continue;
3240 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003241 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003242 }
3243 }
3244 } else if (kind == 2) {
3245 local_fmin = tmp_local_intv->value.fval.min;
3246 local_fmax = tmp_local_intv->value.fval.max;
3247
Michal Vasko4d1f0482016-09-19 14:35:06 +02003248 if ((dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.min, local_fdig) > -1)
Pavol Vican3c8ee2b2016-09-29 13:18:13 +02003249 && (dec64cmp(local_fmin, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1)) {
Michal Vasko4d1f0482016-09-19 14:35:06 +02003250 if (dec64cmp(local_fmax, local_fdig, tmp_intv->value.fval.max, local_fdig) < 1) {
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003251 tmp_local_intv = tmp_local_intv->next;
3252 continue;
3253 } else {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003254 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003255 }
3256 }
3257 }
3258
3259 tmp_intv = tmp_intv->next;
3260 }
3261
3262 /* some interval left uncovered -> fail */
3263 if (tmp_local_intv) {
Michal Vaskoaeb51802016-04-11 10:58:47 +02003264 goto error;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003265 }
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003266 }
3267
Michal Vaskoaeb51802016-04-11 10:58:47 +02003268 /* append the local intervals to all the intervals of the superior types, return it all */
3269 if (intv) {
3270 for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next);
3271 tmp_intv->next = local_intv;
3272 } else {
3273 intv = local_intv;
3274 }
3275 *ret = intv;
3276
3277 return EXIT_SUCCESS;
3278
3279error:
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003280 while (intv) {
3281 tmp_intv = intv->next;
3282 free(intv);
3283 intv = tmp_intv;
3284 }
Michal Vaskoaeb51802016-04-11 10:58:47 +02003285 while (local_intv) {
3286 tmp_local_intv = local_intv->next;
3287 free(local_intv);
3288 local_intv = tmp_local_intv;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003289 }
3290
Michal Vaskoaeb51802016-04-11 10:58:47 +02003291 return -1;
Michal Vaskob2daf5b2015-08-05 16:15:37 +02003292}
3293
Michal Vasko730dfdf2015-08-11 14:48:05 +02003294/**
Michal Vasko01c6fd22016-05-20 11:43:05 +02003295 * @brief Resolve a typedef, return only resolved typedefs if derived. If leafref, it must be
3296 * resolved for this function to return it. Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003297 *
3298 * @param[in] name Typedef name.
Michal Vasko1dca6882015-10-22 14:29:42 +02003299 * @param[in] mod_name Typedef name module name.
3300 * @param[in] module Main module.
3301 * @param[in] parent Parent of the resolved type definition.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003302 * @param[out] ret Pointer to the resolved typedef. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003303 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003304 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003305 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003306int
Michal Vasko1e62a092015-12-01 12:27:20 +01003307resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module,
3308 const struct lys_node *parent, struct lys_tpdf **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003309{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003310 int i, j;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003311 struct lys_tpdf *tpdf, *match;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003312 int tpdf_size;
3313
Michal Vasko1dca6882015-10-22 14:29:42 +02003314 if (!mod_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003315 /* no prefix, try built-in types */
3316 for (i = 1; i < LY_DATA_TYPE_COUNT; i++) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003317 if (!strcmp(ly_types[i]->name, name)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003318 if (ret) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +01003319 *ret = ly_types[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003320 }
3321 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003322 }
3323 }
3324 } else {
Michal Vasko1dca6882015-10-22 14:29:42 +02003325 if (!strcmp(mod_name, module->name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003326 /* prefix refers to the current module, ignore it */
Michal Vasko1dca6882015-10-22 14:29:42 +02003327 mod_name = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003328 }
3329 }
3330
Michal Vasko1dca6882015-10-22 14:29:42 +02003331 if (!mod_name && parent) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003332 /* search in local typedefs */
3333 while (parent) {
3334 switch (parent->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02003335 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02003336 tpdf_size = ((struct lys_node_container *)parent)->tpdf_size;
3337 tpdf = ((struct lys_node_container *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003338 break;
3339
Radek Krejci76512572015-08-04 09:47:08 +02003340 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02003341 tpdf_size = ((struct lys_node_list *)parent)->tpdf_size;
3342 tpdf = ((struct lys_node_list *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003343 break;
3344
Radek Krejci76512572015-08-04 09:47:08 +02003345 case LYS_GROUPING:
Radek Krejcib8048692015-08-05 13:36:34 +02003346 tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size;
3347 tpdf = ((struct lys_node_grp *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003348 break;
3349
Radek Krejci76512572015-08-04 09:47:08 +02003350 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02003351 case LYS_ACTION:
3352 tpdf_size = ((struct lys_node_rpc_action *)parent)->tpdf_size;
3353 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003354 break;
3355
Radek Krejci76512572015-08-04 09:47:08 +02003356 case LYS_NOTIF:
Radek Krejcib8048692015-08-05 13:36:34 +02003357 tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size;
3358 tpdf = ((struct lys_node_notif *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003359 break;
3360
Radek Krejci76512572015-08-04 09:47:08 +02003361 case LYS_INPUT:
3362 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02003363 tpdf_size = ((struct lys_node_inout *)parent)->tpdf_size;
3364 tpdf = ((struct lys_node_inout *)parent)->tpdf;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003365 break;
3366
3367 default:
Michal Vaskodcf98e62016-05-05 17:53:53 +02003368 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003369 continue;
3370 }
3371
3372 for (i = 0; i < tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003373 if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003374 match = &tpdf[i];
3375 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003376 }
3377 }
3378
Michal Vaskodcf98e62016-05-05 17:53:53 +02003379 parent = lys_parent(parent);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003380 }
Radek Krejcic071c542016-01-27 14:57:51 +01003381 } else {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003382 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02003383 module = lyp_get_module(module, NULL, 0, mod_name, 0, 0);
Michal Vaskoc935fff2015-08-17 14:02:06 +02003384 if (!module) {
3385 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003386 }
3387 }
3388
3389 /* search in top level typedefs */
3390 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003391 if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base > 0) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003392 match = &module->tpdf[i];
3393 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003394 }
3395 }
3396
3397 /* search in submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01003398 for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003399 for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) {
Radek Krejcic13db382016-08-16 10:52:42 +02003400 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 +02003401 match = &module->inc[i].submodule->tpdf[j];
3402 goto check_leafref;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003403 }
3404 }
3405 }
3406
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003407 return EXIT_FAILURE;
Michal Vasko01c6fd22016-05-20 11:43:05 +02003408
3409check_leafref:
3410 if (ret) {
3411 *ret = match;
3412 }
3413 if (match->type.base == LY_TYPE_LEAFREF) {
3414 while (!match->type.info.lref.path) {
3415 match = match->type.der;
3416 assert(match);
3417 }
Michal Vasko01c6fd22016-05-20 11:43:05 +02003418 }
3419 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003420}
3421
Michal Vasko1dca6882015-10-22 14:29:42 +02003422/**
3423 * @brief Check the default \p value of the \p type. Logs directly.
3424 *
3425 * @param[in] type Type definition to use.
3426 * @param[in] value Default value to check.
Michal Vasko56826402016-03-02 11:11:37 +01003427 * @param[in] module Type module.
Michal Vasko1dca6882015-10-22 14:29:42 +02003428 *
3429 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
3430 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003431static int
Radek Krejciab08f0f2017-03-09 16:37:15 +01003432check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003433{
Radek Krejcibad2f172016-08-02 11:04:15 +02003434 struct lys_tpdf *base_tpdf = NULL;
Michal Vasko1dca6882015-10-22 14:29:42 +02003435 struct lyd_node_leaf_list node;
Radek Krejci51673202016-11-01 17:00:32 +01003436 const char *dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003437 char *s;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003438 int ret = EXIT_SUCCESS, r;
Michal Vasko53b7da02018-02-13 15:28:42 +01003439 struct ly_ctx *ctx = module->ctx;
Michal Vasko1dca6882015-10-22 14:29:42 +02003440
Radek Krejci51673202016-11-01 17:00:32 +01003441 assert(value);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003442 memset(&node, 0, sizeof node);
Radek Krejci51673202016-11-01 17:00:32 +01003443
Radek Krejcic13db382016-08-16 10:52:42 +02003444 if (type->base <= LY_TYPE_DER) {
Michal Vasko478c4652016-07-21 12:55:01 +02003445 /* the type was not resolved yet, nothing to do for now */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003446 ret = EXIT_FAILURE;
3447 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003448 } else if (!tpdf && !module->implemented) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003449 /* do not check defaults in not implemented module's data */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003450 goto cleanup;
Radek Krejci29eac3d2017-06-01 16:50:02 +02003451 } else if (tpdf && !module->implemented && type->base == LY_TYPE_IDENT) {
Radek Krejci9e6af732017-04-27 14:40:25 +02003452 /* identityrefs are checked when instantiated in data instead of typedef,
3453 * but in typedef the value has to be modified to include the prefix */
3454 if (*value) {
3455 if (strchr(*value, ':')) {
3456 dflt = transform_schema2json(module, *value);
3457 } else {
3458 /* default prefix of the module where the typedef is defined */
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003459 if (asprintf(&s, "%s:%s", lys_main_module(module)->name, *value) == -1) {
3460 LOGMEM(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003461 ret = -1;
3462 goto cleanup;
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003463 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003464 dflt = lydict_insert_zc(ctx, s);
Radek Krejci9e6af732017-04-27 14:40:25 +02003465 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003466 lydict_remove(ctx, *value);
Radek Krejci9e6af732017-04-27 14:40:25 +02003467 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003468 dflt = NULL;
Radek Krejci9e6af732017-04-27 14:40:25 +02003469 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003470 goto cleanup;
Radek Krejciab08f0f2017-03-09 16:37:15 +01003471 } else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3472 /* leafref in typedef cannot be checked */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003473 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003474 }
3475
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003476 dflt = lydict_insert(ctx, *value, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003477 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003478 /* we do not have a new default value, so is there any to check even, in some base type? */
3479 for (base_tpdf = type->der; base_tpdf->type.der; base_tpdf = base_tpdf->type.der) {
3480 if (base_tpdf->dflt) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003481 dflt = lydict_insert(ctx, base_tpdf->dflt, 0);
Michal Vasko478c4652016-07-21 12:55:01 +02003482 break;
3483 }
3484 }
3485
Radek Krejci51673202016-11-01 17:00:32 +01003486 if (!dflt) {
Michal Vasko478c4652016-07-21 12:55:01 +02003487 /* no default value, nothing to check, all is well */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003488 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003489 }
3490
3491 /* 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)? */
3492 switch (type->base) {
Michal Vasko478c4652016-07-21 12:55:01 +02003493 case LY_TYPE_IDENT:
Radek Krejci9e6af732017-04-27 14:40:25 +02003494 if (lys_main_module(base_tpdf->type.parent->module)->implemented) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003495 goto cleanup;
Radek Krejci9e6af732017-04-27 14:40:25 +02003496 } else {
3497 /* check the default value from typedef, but use also the typedef's module
3498 * due to possible searching in imported modules which is expected in
3499 * typedef's module instead of module where the typedef is used */
3500 module = base_tpdf->module;
3501 }
3502 break;
Michal Vasko478c4652016-07-21 12:55:01 +02003503 case LY_TYPE_INST:
3504 case LY_TYPE_LEAFREF:
3505 case LY_TYPE_BOOL:
3506 case LY_TYPE_EMPTY:
3507 /* 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 +01003508 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003509 case LY_TYPE_BITS:
3510 /* the default value must match the restricted list of values, if the type was restricted */
3511 if (type->info.bits.count) {
3512 break;
3513 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003514 goto cleanup;
Radek Krejcibad2f172016-08-02 11:04:15 +02003515 case LY_TYPE_ENUM:
3516 /* the default value must match the restricted list of values, if the type was restricted */
3517 if (type->info.enums.count) {
3518 break;
3519 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003520 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003521 case LY_TYPE_DEC64:
3522 if (type->info.dec64.range) {
3523 break;
3524 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003525 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003526 case LY_TYPE_BINARY:
3527 if (type->info.binary.length) {
3528 break;
3529 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003530 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003531 case LY_TYPE_INT8:
3532 case LY_TYPE_INT16:
3533 case LY_TYPE_INT32:
3534 case LY_TYPE_INT64:
3535 case LY_TYPE_UINT8:
3536 case LY_TYPE_UINT16:
3537 case LY_TYPE_UINT32:
3538 case LY_TYPE_UINT64:
3539 if (type->info.num.range) {
3540 break;
3541 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003542 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003543 case LY_TYPE_STRING:
3544 if (type->info.str.length || type->info.str.patterns) {
3545 break;
3546 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003547 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003548 case LY_TYPE_UNION:
3549 /* way too much trouble learning whether we need to check the default again, so just do it */
3550 break;
3551 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01003552 LOGINT(ctx);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003553 ret = -1;
3554 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003555 }
Radek Krejci55a161c2016-09-05 17:13:25 +02003556 } else if (type->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003557 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", type->parent->name);
3558 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "The \"empty\" data type cannot have a default value.");
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003559 ret = -1;
3560 goto cleanup;
Michal Vasko478c4652016-07-21 12:55:01 +02003561 }
3562
Michal Vasko1dca6882015-10-22 14:29:42 +02003563 /* dummy leaf */
Radek Krejci4fe36bd2016-03-17 16:47:16 +01003564 memset(&node, 0, sizeof node);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003565 node.value_str = lydict_insert(ctx, dflt, 0);
Michal Vasko1dca6882015-10-22 14:29:42 +02003566 node.value_type = type->base;
Michal Vasko31a2d322018-01-12 13:36:12 +01003567
3568 if (tpdf) {
3569 node.schema = calloc(1, sizeof (struct lys_node_leaf));
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003570 if (!node.schema) {
3571 LOGMEM(ctx);
3572 ret = -1;
3573 goto cleanup;
3574 }
Michal Vaskod1bf7c42018-02-15 08:38:49 +01003575 r = asprintf((char **)&node.schema->name, "typedef-%s-default", ((struct lys_tpdf *)type->parent)->name);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003576 if (r == -1) {
3577 LOGMEM(ctx);
3578 ret = -1;
3579 goto cleanup;
3580 }
Michal Vasko31a2d322018-01-12 13:36:12 +01003581 node.schema->module = module;
3582 memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type);
3583 } else {
3584 node.schema = (struct lys_node *)type->parent;
3585 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003586
Radek Krejci37b756f2016-01-18 10:15:03 +01003587 if (type->base == LY_TYPE_LEAFREF) {
Michal Vasko1dca6882015-10-22 14:29:42 +02003588 if (!type->info.lref.target) {
3589 ret = EXIT_FAILURE;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003590 goto cleanup;
Michal Vasko1dca6882015-10-22 14:29:42 +02003591 }
Radek Krejciab08f0f2017-03-09 16:37:15 +01003592 ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
Radek Krejci51673202016-11-01 17:00:32 +01003593 if (!ret) {
3594 /* adopt possibly changed default value to its canonical form */
3595 if (*value) {
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003596 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003597 *value = dflt;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003598 dflt = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003599 }
3600 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003601 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02003602 if (!lyp_parse_value(type, &node.value_str, NULL, &node, NULL, module, 1, 1, 0)) {
Radek Krejci5dca5932016-11-04 14:30:47 +01003603 /* possible forward reference */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003604 ret = EXIT_FAILURE;
Radek Krejcibad2f172016-08-02 11:04:15 +02003605 if (base_tpdf) {
Radek Krejci9ad23f42016-10-31 15:46:52 +01003606 /* default value is defined in some base typedef */
Radek Krejcibad2f172016-08-02 11:04:15 +02003607 if ((type->base == LY_TYPE_BITS && type->der->type.der) ||
3608 (type->base == LY_TYPE_ENUM && type->der->type.der)) {
3609 /* we have refined bits/enums */
Michal Vasko53b7da02018-02-13 15:28:42 +01003610 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL,
Radek Krejcibad2f172016-08-02 11:04:15 +02003611 "Invalid value \"%s\" of the default statement inherited to \"%s\" from \"%s\" base type.",
Radek Krejci51673202016-11-01 17:00:32 +01003612 dflt, type->parent->name, base_tpdf->name);
Radek Krejcibad2f172016-08-02 11:04:15 +02003613 }
3614 }
Radek Krejci51673202016-11-01 17:00:32 +01003615 } else {
3616 /* success - adopt canonical form from the node into the default value */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003617 if (!ly_strequal(dflt, node.value_str, 1)) {
Radek Krejci51673202016-11-01 17:00:32 +01003618 /* this can happen only if we have non-inherited default value,
3619 * inherited default values are already in canonical form */
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003620 assert(ly_strequal(dflt, *value, 1));
3621
3622 lydict_remove(ctx, *value);
Radek Krejci51673202016-11-01 17:00:32 +01003623 *value = node.value_str;
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003624 node.value_str = NULL;
Radek Krejci51673202016-11-01 17:00:32 +01003625 }
Michal Vasko3767fb22016-07-21 12:10:57 +02003626 }
Michal Vasko1dca6882015-10-22 14:29:42 +02003627 }
3628
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003629cleanup:
Michal Vasko70bf8e52018-03-26 11:32:33 +02003630 lyd_free_value(node.value, node.value_type, node.value_flags, type);
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003631 lydict_remove(ctx, node.value_str);
3632 if (tpdf && node.schema) {
Michal Vasko31a2d322018-01-12 13:36:12 +01003633 free((char *)node.schema->name);
3634 free(node.schema);
3635 }
Michal Vasko4b8eb8a2018-02-16 11:58:45 +01003636 lydict_remove(ctx, dflt);
Michal Vasko1dca6882015-10-22 14:29:42 +02003637
3638 return ret;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003639}
3640
Michal Vasko730dfdf2015-08-11 14:48:05 +02003641/**
3642 * @brief Check a key for mandatory attributes. Logs directly.
3643 *
3644 * @param[in] key The key to check.
3645 * @param[in] flags What flags to check.
3646 * @param[in] list The list of all the keys.
3647 * @param[in] index Index of the key in the key list.
3648 * @param[in] name The name of the keys.
3649 * @param[in] len The name length.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003650 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003651 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003652 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003653static int
Radek Krejci48464ed2016-03-17 15:44:09 +01003654check_key(struct lys_node_list *list, int index, const char *name, int len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003655{
Radek Krejciadb57612016-02-16 13:34:34 +01003656 struct lys_node_leaf *key = list->keys[index];
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003657 char *dup = NULL;
3658 int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01003659 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003660
3661 /* existence */
3662 if (!key) {
Michal Vaskof02e3742015-08-05 16:27:02 +02003663 if (name[len] != '\0') {
3664 dup = strdup(name);
Michal Vasko53b7da02018-02-13 15:28:42 +01003665 LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), -1);
Michal Vaskof02e3742015-08-05 16:27:02 +02003666 dup[len] = '\0';
3667 name = dup;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003668 }
Michal Vasko53b7da02018-02-13 15:28:42 +01003669 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, list, name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003670 free(dup);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003671 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003672 }
3673
3674 /* uniqueness */
3675 for (j = index - 1; j >= 0; j--) {
Radek Krejciadb57612016-02-16 13:34:34 +01003676 if (key == list->keys[j]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003677 LOGVAL(ctx, LYE_KEY_DUP, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003678 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003679 }
3680 }
3681
3682 /* key is a leaf */
Radek Krejci76512572015-08-04 09:47:08 +02003683 if (key->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003684 LOGVAL(ctx, LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003685 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003686 }
3687
3688 /* type of the leaf is not built-in empty */
Radek Krejci13fde922018-05-16 10:45:58 +02003689 if (key->type.base == LY_TYPE_EMPTY && key->module->version < LYS_VERSION_1_1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003690 LOGVAL(ctx, LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003691 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003692 }
3693
3694 /* config attribute is the same as of the list */
Radek Krejci5c08a992016-11-02 13:30:04 +01003695 if ((key->flags & LYS_CONFIG_MASK) && (list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003696 LOGVAL(ctx, LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003697 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003698 }
3699
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003700 /* key is not placed from augment */
3701 if (key->parent->nodetype == LYS_AUGMENT) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003702 LOGVAL(ctx, LYE_KEY_MISS, LY_VLOG_LYS, key, key->name);
3703 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key inserted from augment.");
Radek Krejci55e2cdc2016-03-11 13:51:09 +01003704 return -1;
3705 }
3706
Radek Krejci3f21ada2016-08-01 13:34:31 +02003707 /* key is not when/if-feature -conditional */
3708 j = 0;
3709 if (key->when || (key->iffeature_size && (j = 1))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003710 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, key, j ? "if-feature" : "when", "leaf");
3711 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Key definition cannot depend on a \"%s\" condition.",
Radek Krejci3f21ada2016-08-01 13:34:31 +02003712 j ? "if-feature" : "when");
Radek Krejci581ce772015-11-10 17:22:40 +01003713 return -1;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003714 }
3715
Michal Vasko0b85aa82016-03-07 14:37:43 +01003716 return EXIT_SUCCESS;
Michal Vasko184521f2015-09-24 13:14:26 +02003717}
Michal Vasko730dfdf2015-08-11 14:48:05 +02003718
3719/**
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003720 * @brief Resolve (test the target exists) unique. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003721 *
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003722 * @param[in] parent The parent node of the unique structure.
Michal Vasko0b85aa82016-03-07 14:37:43 +01003723 * @param[in] uniq_str_path One path from the unique string.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003724 *
3725 * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error.
3726 */
3727int
Radek Krejcid09d1a52016-08-11 14:05:45 +02003728resolve_unique(struct lys_node *parent, const char *uniq_str_path, uint8_t *trg_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003729{
Radek Krejci581ce772015-11-10 17:22:40 +01003730 int rc;
Michal Vasko1e62a092015-12-01 12:27:20 +01003731 const struct lys_node *leaf = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01003732 struct ly_ctx *ctx = parent->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003733
Michal Vaskodc300b02017-04-07 14:09:20 +02003734 rc = resolve_descendant_schema_nodeid(uniq_str_path, *lys_child(parent, LYS_LEAF), LYS_LEAF, 1, &leaf);
Michal Vasko9bb061b2016-02-12 11:00:19 +01003735 if (rc || !leaf) {
Michal Vasko0b85aa82016-03-07 14:37:43 +01003736 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003737 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003738 if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003739 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 +02003740 } else if (rc == -2) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003741 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unique argument references list.");
Radek Krejci581ce772015-11-10 17:22:40 +01003742 }
Michal Vasko0b85aa82016-03-07 14:37:43 +01003743 rc = -1;
Michal Vasko0b85aa82016-03-07 14:37:43 +01003744 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003745 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3746 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target leaf not found.");
Michal Vasko0b85aa82016-03-07 14:37:43 +01003747 rc = EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003748 }
Radek Krejci581ce772015-11-10 17:22:40 +01003749 goto error;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003750 }
Michal Vasko9bb061b2016-02-12 11:00:19 +01003751 if (leaf->nodetype != LYS_LEAF) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003752 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3753 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Target is not a leaf.");
Radek Krejcid09d1a52016-08-11 14:05:45 +02003754 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003755 }
3756
Radek Krejcicf509982015-12-15 09:22:44 +01003757 /* check status */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003758 if (parent->nodetype != LYS_EXT && lyp_check_status(parent->flags, parent->module, parent->name,
3759 leaf->flags, leaf->module, leaf->name, leaf)) {
Radek Krejcicf509982015-12-15 09:22:44 +01003760 return -1;
3761 }
3762
Radek Krejcid09d1a52016-08-11 14:05:45 +02003763 /* check that all unique's targets are of the same config type */
3764 if (*trg_type) {
3765 if (((*trg_type == 1) && (leaf->flags & LYS_CONFIG_R)) || ((*trg_type == 2) && (leaf->flags & LYS_CONFIG_W))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003766 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique");
3767 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid09d1a52016-08-11 14:05:45 +02003768 "Leaf \"%s\" referenced in unique statement is config %s, but previous referenced leaf is config %s.",
3769 uniq_str_path, *trg_type == 1 ? "false" : "true", *trg_type == 1 ? "true" : "false");
3770 return -1;
3771 }
3772 } else {
3773 /* first unique */
3774 if (leaf->flags & LYS_CONFIG_W) {
3775 *trg_type = 1;
3776 } else {
3777 *trg_type = 2;
3778 }
3779 }
3780
Radek Krejcica7efb72016-01-18 13:06:01 +01003781 /* set leaf's unique flag */
3782 ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE;
3783
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003784 return EXIT_SUCCESS;
3785
3786error:
3787
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003788 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003789}
3790
Radek Krejci0c0086a2016-03-24 15:20:28 +01003791void
Michal Vasko23b61ec2015-08-19 11:19:50 +02003792unres_data_del(struct unres_data *unres, uint32_t i)
3793{
3794 /* there are items after the one deleted */
3795 if (i+1 < unres->count) {
3796 /* we only move the data, memory is left allocated, why bother */
Michal Vaskocf024702015-10-08 15:01:42 +02003797 memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node);
Michal Vasko23b61ec2015-08-19 11:19:50 +02003798
3799 /* deleting the last item */
3800 } else if (i == 0) {
Michal Vaskocf024702015-10-08 15:01:42 +02003801 free(unres->node);
3802 unres->node = NULL;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003803 }
3804
3805 /* if there are no items after and it is not the last one, just move the counter */
3806 --unres->count;
3807}
3808
Michal Vasko0491ab32015-08-19 14:28:29 +02003809/**
3810 * @brief Resolve (find) a data node from a specific module. Does not log.
3811 *
3812 * @param[in] mod Module to search in.
3813 * @param[in] name Name of the data node.
3814 * @param[in] nam_len Length of the name.
3815 * @param[in] start Data node to start the search from.
3816 * @param[in,out] parents Resolved nodes. If there are some parents,
3817 * they are replaced (!!) with the resolvents.
3818 *
Michal Vasko2471e7f2016-04-11 11:00:15 +02003819 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko0491ab32015-08-19 14:28:29 +02003820 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003821static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003822resolve_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 +02003823{
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003824 struct lyd_node *node;
Radek Krejcic5090c32015-08-12 09:46:19 +02003825 int flag;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003826 uint32_t i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003827
Michal Vasko23b61ec2015-08-19 11:19:50 +02003828 if (!parents->count) {
3829 parents->count = 1;
Michal Vaskocf024702015-10-08 15:01:42 +02003830 parents->node = malloc(sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003831 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02003832 parents->node[0] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003833 }
Michal Vasko23b61ec2015-08-19 11:19:50 +02003834 for (i = 0; i < parents->count;) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003835 if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003836 /* skip */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003837 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003838 continue;
3839 }
3840 flag = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02003841 LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) {
Michal Vasko39608352017-05-11 10:37:10 +02003842 if (lyd_node_module(node) == mod && !strncmp(node->schema->name, name, nam_len)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003843 && node->schema->name[nam_len] == '\0') {
3844 /* matching target */
3845 if (!flag) {
Michal Vasko9a47e122015-09-03 14:26:32 +02003846 /* put node instead of the current parent */
Michal Vaskocf024702015-10-08 15:01:42 +02003847 parents->node[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003848 flag = 1;
3849 } else {
Michal Vasko9a47e122015-09-03 14:26:32 +02003850 /* multiple matching, so create a new node */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003851 ++parents->count;
Michal Vasko253035f2015-12-17 16:58:13 +01003852 parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01003853 LY_CHECK_ERR_RETURN(!parents->node, LOGMEM(mod->ctx), EXIT_FAILURE);
Michal Vaskocf024702015-10-08 15:01:42 +02003854 parents->node[parents->count-1] = node;
Michal Vasko23b61ec2015-08-19 11:19:50 +02003855 ++i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003856 }
3857 }
3858 }
Radek Krejcic5090c32015-08-12 09:46:19 +02003859
3860 if (!flag) {
3861 /* remove item from the parents list */
Michal Vasko23b61ec2015-08-19 11:19:50 +02003862 unres_data_del(parents, i);
Radek Krejcic5090c32015-08-12 09:46:19 +02003863 } else {
Michal Vasko23b61ec2015-08-19 11:19:50 +02003864 ++i;
Radek Krejcic5090c32015-08-12 09:46:19 +02003865 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003866 }
3867
Michal Vasko0491ab32015-08-19 14:28:29 +02003868 return parents->count ? EXIT_SUCCESS : EXIT_FAILURE;
Radek Krejcic5090c32015-08-12 09:46:19 +02003869}
3870
Michal Vaskoe27516a2016-10-10 17:55:31 +00003871static int
Michal Vasko1c007172017-03-10 10:20:44 +01003872resolve_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 +00003873{
3874 int dep1, dep2;
3875 const struct lys_node *node;
3876
3877 if (lys_parent(op_node)) {
3878 /* inner operation (notif/action) */
3879 if (abs_path) {
3880 return 1;
3881 } else {
3882 /* compare depth of both nodes */
3883 for (dep1 = 0, node = op_node; lys_parent(node); node = lys_parent(node));
3884 for (dep2 = 0, node = first_node; lys_parent(node); node = lys_parent(node));
3885 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
3886 return 1;
3887 }
3888 }
3889 } else {
3890 /* top-level operation (notif/rpc) */
3891 if (op_node != first_node) {
3892 return 1;
3893 }
3894 }
3895
3896 return 0;
3897}
3898
Michal Vasko730dfdf2015-08-11 14:48:05 +02003899/**
Michal Vaskof39142b2015-10-21 11:40:05 +02003900 * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003901 *
Michal Vaskobb211122015-08-19 14:03:11 +02003902 * @param[in] path Path to use.
Radek Krejciadb57612016-02-16 13:34:34 +01003903 * @param[in] context_node Predicate context node (where the predicate is placed).
3904 * @param[in] parent Path context node (where the path begins/is placed).
Michal Vaskoe27516a2016-10-10 17:55:31 +00003905 * @param[in] op_node Optional node if the leafref is in an operation (action/rpc/notif).
Michal Vasko730dfdf2015-08-11 14:48:05 +02003906 *
Michal Vasko184521f2015-09-24 13:14:26 +02003907 * @return 0 on forward reference, otherwise the number
3908 * of characters successfully parsed,
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003909 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02003910 */
Michal Vasko1f76a282015-08-04 16:16:53 +02003911static int
Michal Vasko1c007172017-03-10 10:20:44 +01003912resolve_schema_leafref_predicate(const char *path, const struct lys_node *context_node,
3913 struct lys_node *parent, const struct lys_node *op_node)
Michal Vasko1f76a282015-08-04 16:16:53 +02003914{
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003915 const struct lys_module *trg_mod;
Michal Vasko1e62a092015-12-01 12:27:20 +01003916 const struct lys_node *src_node, *dst_node;
Michal Vasko1f76a282015-08-04 16:16:53 +02003917 const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref;
Michal Vaskof9b35d92016-10-21 15:19:30 +02003918 int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, pke_parsed, parsed = 0;
3919 int has_predicate, dest_parent_times, i, rc, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01003920 struct ly_ctx *ctx = context_node->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02003921
3922 do {
Michal Vasko730dfdf2015-08-11 14:48:05 +02003923 if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr,
Michal Vasko1f76a282015-08-04 16:16:53 +02003924 &pke_len, &has_predicate)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003925 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path[-i], path-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003926 return -parsed+i;
3927 }
3928 parsed += i;
Michal Vasko730dfdf2015-08-11 14:48:05 +02003929 path += i;
Michal Vasko1f76a282015-08-04 16:16:53 +02003930
Michal Vasko58090902015-08-13 14:04:15 +02003931 /* source (must be leaf) */
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003932 if (sour_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003933 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, sour_pref, sour_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003934 } else {
3935 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003936 }
Michal Vaskobb520442017-05-23 10:55:18 +02003937 rc = lys_getnext_data(trg_mod, context_node, source, sour_len, LYS_LEAF | LYS_LEAFLIST, &src_node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003938 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003939 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path-parsed);
Michal Vasko184521f2015-09-24 13:14:26 +02003940 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003941 }
3942
3943 /* destination */
Michal Vaskof9b35d92016-10-21 15:19:30 +02003944 dest_parent_times = 0;
3945 pke_parsed = 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003946 if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len,
3947 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003948 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, path_key_expr[-i], path_key_expr-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02003949 return -parsed;
3950 }
3951 pke_parsed += i;
3952
Radek Krejciadb57612016-02-16 13:34:34 +01003953 for (i = 0, dst_node = parent; i < dest_parent_times; ++i) {
Michal Vasko3ba2d792017-07-10 15:14:43 +02003954 if (dst_node->parent && (dst_node->parent->nodetype == LYS_AUGMENT)
3955 && !((struct lys_node_augment *)dst_node->parent)->target) {
3956 /* we are in an unresolved augment, cannot evaluate */
Michal Vasko53b7da02018-02-13 15:28:42 +01003957 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, dst_node->parent,
Michal Vasko3ba2d792017-07-10 15:14:43 +02003958 "Cannot resolve leafref predicate \"%s\" because it is in an unresolved augment.", path_key_expr);
3959 return 0;
3960 }
3961
Michal Vaskofbaead72016-10-07 10:54:48 +02003962 /* path is supposed to be evaluated in data tree, so we have to skip
3963 * all schema nodes that cannot be instantiated in data tree */
3964 for (dst_node = lys_parent(dst_node);
Michal Vaskoe27516a2016-10-10 17:55:31 +00003965 dst_node && !(dst_node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
Michal Vaskofbaead72016-10-07 10:54:48 +02003966 dst_node = lys_parent(dst_node));
3967
Michal Vasko1f76a282015-08-04 16:16:53 +02003968 if (!dst_node) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003969 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003970 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003971 }
3972 }
Michal Vaskoe27516a2016-10-10 17:55:31 +00003973 first_iter = 1;
Michal Vasko1f76a282015-08-04 16:16:53 +02003974 while (1) {
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003975 if (dest_pref) {
Michal Vasko921eb6b2017-10-13 10:01:39 +02003976 trg_mod = lyp_get_module(lys_node_module(parent), NULL, 0, dest_pref, dest_pref_len, 0);
Michal Vasko0f99d3e2017-01-10 10:50:40 +01003977 } else {
3978 trg_mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +01003979 }
Michal Vaskobb520442017-05-23 10:55:18 +02003980 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 +02003981 if (rc) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003982 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path_key_expr);
Michal Vasko184521f2015-09-24 13:14:26 +02003983 return 0;
Michal Vasko1f76a282015-08-04 16:16:53 +02003984 }
3985
Michal Vaskoe27516a2016-10-10 17:55:31 +00003986 if (first_iter) {
Michal Vasko1c007172017-03-10 10:20:44 +01003987 if (resolve_schema_leafref_valid_dep_flag(op_node, dst_node, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003988 parent->flags |= LYS_LEAFREF_DEP;
Michal Vaskoe27516a2016-10-10 17:55:31 +00003989 }
3990 first_iter = 0;
3991 }
3992
Michal Vasko1f76a282015-08-04 16:16:53 +02003993 if (pke_len == pke_parsed) {
3994 break;
3995 }
3996
Michal Vaskobb520442017-05-23 10:55:18 +02003997 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 +02003998 &dest_parent_times)) < 1) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02003999 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent,
Michal Vaskobb520442017-05-23 10:55:18 +02004000 (path_key_expr + pke_parsed)[-i], (path_key_expr + pke_parsed)-i);
Michal Vasko1f76a282015-08-04 16:16:53 +02004001 return -parsed;
4002 }
4003 pke_parsed += i;
4004 }
4005
4006 /* check source - dest match */
Michal Vasko59ad4582016-09-16 13:15:41 +02004007 if (dst_node->nodetype != src_node->nodetype) {
Michal Vaskoaf8ec362018-03-28 09:08:09 +02004008 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref predicate", path - parsed);
Michal Vasko53b7da02018-02-13 15:28:42 +01004009 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Destination node is not a %s, but a %s.",
Michal Vasko59ad4582016-09-16 13:15:41 +02004010 strnodetype(src_node->nodetype), strnodetype(dst_node->nodetype));
Michal Vasko184521f2015-09-24 13:14:26 +02004011 return -parsed;
4012 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004013 } while (has_predicate);
4014
4015 return parsed;
4016}
4017
Michal Vasko74083ec2018-06-15 10:00:12 +02004018static int
4019check_leafref_features(struct lys_type *type)
4020{
4021 struct lys_node *iter;
4022 struct ly_set *src_parents, *trg_parents, *features;
4023 struct lys_node_augment *aug;
4024 struct ly_ctx *ctx = ((struct lys_tpdf *)type->parent)->module->ctx;
4025 unsigned int i, j, size, x;
4026 int ret = EXIT_SUCCESS;
4027
4028 assert(type->parent);
4029
4030 src_parents = ly_set_new();
4031 trg_parents = ly_set_new();
4032 features = ly_set_new();
4033
4034 /* get parents chain of source (leafref) */
4035 for (iter = (struct lys_node *)type->parent; iter; iter = lys_parent(iter)) {
4036 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4037 continue;
4038 }
4039 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4040 aug = (struct lys_node_augment *)iter->parent;
4041 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4042 /* unresolved augment, wait until it's resolved */
4043 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4044 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4045 ret = EXIT_FAILURE;
4046 goto cleanup;
4047 }
Michal Vaskod42871e2018-07-12 09:06:53 +02004048 /* also add this augment */
4049 ly_set_add(src_parents, aug, LY_SET_OPT_USEASLIST);
Michal Vasko74083ec2018-06-15 10:00:12 +02004050 }
4051 ly_set_add(src_parents, iter, LY_SET_OPT_USEASLIST);
4052 }
4053 /* get parents chain of target */
4054 for (iter = (struct lys_node *)type->info.lref.target; iter; iter = lys_parent(iter)) {
4055 if (iter->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
4056 continue;
4057 }
4058 if (iter->parent && (iter->parent->nodetype == LYS_AUGMENT)) {
4059 aug = (struct lys_node_augment *)iter->parent;
4060 if ((aug->module->implemented && (aug->flags & LYS_NOTAPPLIED)) || !aug->target) {
4061 /* unresolved augment, wait until it's resolved */
4062 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, aug,
4063 "Cannot check leafref \"%s\" if-feature consistency because of an unresolved augment.", type->info.lref.path);
4064 ret = EXIT_FAILURE;
4065 goto cleanup;
4066 }
4067 }
4068 ly_set_add(trg_parents, iter, LY_SET_OPT_USEASLIST);
4069 }
4070
4071 /* compare the features used in if-feature statements in the rest of both
4072 * chains of parents. The set of features used for target must be a subset
4073 * of features used for the leafref. This is not a perfect, we should compare
4074 * the truth tables but it could require too much resources, so we simplify that */
4075 for (i = 0; i < src_parents->number; i++) {
4076 iter = src_parents->set.s[i]; /* shortcut */
4077 if (!iter->iffeature_size) {
4078 continue;
4079 }
4080 for (j = 0; j < iter->iffeature_size; j++) {
4081 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4082 for (; size; size--) {
4083 if (!iter->iffeature[j].features[size - 1]) {
4084 /* not yet resolved feature, postpone this check */
4085 ret = EXIT_FAILURE;
4086 goto cleanup;
4087 }
4088 ly_set_add(features, iter->iffeature[j].features[size - 1], 0);
4089 }
4090 }
4091 }
4092 x = features->number;
4093 for (i = 0; i < trg_parents->number; i++) {
4094 iter = trg_parents->set.s[i]; /* shortcut */
4095 if (!iter->iffeature_size) {
4096 continue;
4097 }
4098 for (j = 0; j < iter->iffeature_size; j++) {
4099 resolve_iffeature_getsizes(&iter->iffeature[j], NULL, &size);
4100 for (; size; size--) {
4101 if (!iter->iffeature[j].features[size - 1]) {
4102 /* not yet resolved feature, postpone this check */
4103 ret = EXIT_FAILURE;
4104 goto cleanup;
4105 }
4106 if ((unsigned)ly_set_add(features, iter->iffeature[j].features[size - 1], 0) >= x) {
4107 /* the feature is not present in features set of target's parents chain */
4108 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, type->parent, "leafref", type->info.lref.path);
4109 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
4110 "Leafref is not conditional based on \"%s\" feature as its target.",
4111 iter->iffeature[j].features[size - 1]->name);
4112 ret = -1;
4113 goto cleanup;
4114 }
4115 }
4116 }
4117 }
4118
4119cleanup:
4120 ly_set_free(features);
4121 ly_set_free(src_parents);
4122 ly_set_free(trg_parents);
4123
4124 return ret;
4125}
4126
Michal Vasko730dfdf2015-08-11 14:48:05 +02004127/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004128 * @brief Resolve a path (leafref) in JSON schema context. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004129 *
Michal Vaskobb211122015-08-19 14:03:11 +02004130 * @param[in] path Path to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004131 * @param[in] parent_node Parent of the leafref.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004132 * @param[out] ret Pointer to the resolved schema node. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004133 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004134 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004135 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004136static int
Michal Vasko74083ec2018-06-15 10:00:12 +02004137resolve_schema_leafref(struct lys_type *type, struct lys_node *parent, struct unres_schema *unres)
Michal Vasko1f76a282015-08-04 16:16:53 +02004138{
Michal Vaskocb45f472018-02-12 10:47:42 +01004139 const struct lys_node *node, *op_node = NULL, *tmp_parent;
Michal Vaskobb520442017-05-23 10:55:18 +02004140 struct lys_node_augment *last_aug;
Michal Vasko3c60cbb2017-07-10 11:50:03 +02004141 const struct lys_module *tmp_mod, *cur_module;
Michal Vasko1f76a282015-08-04 16:16:53 +02004142 const char *id, *prefix, *name;
4143 int pref_len, nam_len, parent_times, has_predicate;
Michal Vaskocb45f472018-02-12 10:47:42 +01004144 int i, first_iter;
Michal Vasko53b7da02018-02-13 15:28:42 +01004145 struct ly_ctx *ctx = parent->module->ctx;
Michal Vasko1f76a282015-08-04 16:16:53 +02004146
Michal Vasko74083ec2018-06-15 10:00:12 +02004147 if (!type->info.lref.target) {
4148 first_iter = 1;
4149 parent_times = 0;
4150 id = type->info.lref.path;
Michal Vasko1f76a282015-08-04 16:16:53 +02004151
Michal Vasko74083ec2018-06-15 10:00:12 +02004152 /* find operation schema we are in */
4153 for (op_node = lys_parent(parent);
4154 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF | LYS_RPC));
4155 op_node = lys_parent(op_node));
Michal Vaskoe9914d12016-10-07 14:32:37 +02004156
Michal Vasko74083ec2018-06-15 10:00:12 +02004157 cur_module = lys_node_module(parent);
4158 do {
4159 if ((i = parse_path_arg(cur_module, id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) {
4160 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, parent, id[-i], &id[-i]);
Michal Vaskobb520442017-05-23 10:55:18 +02004161 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004162 }
4163 id += i;
Michal Vasko74083ec2018-06-15 10:00:12 +02004164
4165 /* get the current module */
4166 tmp_mod = prefix ? lyp_get_module(cur_module, NULL, 0, prefix, pref_len, 0) : cur_module;
4167 if (!tmp_mod) {
4168 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4169 return EXIT_FAILURE;
4170 }
4171 last_aug = NULL;
4172
4173 if (first_iter) {
4174 if (parent_times == -1) {
4175 /* use module data */
4176 node = NULL;
4177
4178 } else if (parent_times > 0) {
4179 /* we are looking for the right parent */
4180 for (i = 0, node = parent; i < parent_times; i++) {
4181 if (node->parent && (node->parent->nodetype == LYS_AUGMENT)
4182 && !((struct lys_node_augment *)node->parent)->target) {
4183 /* we are in an unresolved augment, cannot evaluate */
4184 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, node->parent,
4185 "Cannot resolve leafref \"%s\" because it is in an unresolved augment.", type->info.lref.path);
4186 return EXIT_FAILURE;
4187 }
4188
4189 /* path is supposed to be evaluated in data tree, so we have to skip
4190 * all schema nodes that cannot be instantiated in data tree */
4191 for (node = lys_parent(node);
4192 node && !(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_RPC));
4193 node = lys_parent(node));
4194
4195 if (!node) {
4196 if (i == parent_times - 1) {
4197 /* top-level */
4198 break;
4199 }
4200
4201 /* higher than top-level */
4202 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4203 return EXIT_FAILURE;
4204 }
4205 }
4206 } else {
4207 LOGINT(ctx);
4208 return -1;
4209 }
4210 }
4211
4212 /* find the next node (either in unconnected augment or as a schema sibling, node is NULL for top-level node -
4213 * - useless to search for that in augments) */
4214 if (!tmp_mod->implemented && node) {
4215 get_next_augment:
4216 last_aug = lys_getnext_target_aug(last_aug, tmp_mod, node);
4217 }
4218
4219 tmp_parent = (last_aug ? (struct lys_node *)last_aug : node);
4220 node = NULL;
4221 while ((node = lys_getnext(node, tmp_parent, tmp_mod, LYS_GETNEXT_NOSTATECHECK))) {
4222 if (lys_node_module(node) != lys_main_module(tmp_mod)) {
4223 continue;
4224 }
4225 if (strncmp(node->name, name, nam_len) || node->name[nam_len]) {
4226 continue;
4227 }
4228 /* match */
4229 break;
4230 }
4231 if (!node) {
4232 if (last_aug) {
4233 /* restore the correct augment target */
4234 node = last_aug->target;
4235 goto get_next_augment;
4236 }
4237 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4238 return EXIT_FAILURE;
4239 }
4240
4241 if (first_iter) {
4242 /* set external dependency flag, we can decide based on the first found node */
4243 if (op_node && parent_times &&
4244 resolve_schema_leafref_valid_dep_flag(op_node, node, (parent_times == -1 ? 1 : 0))) {
4245 parent->flags |= LYS_LEAFREF_DEP;
4246 }
4247 first_iter = 0;
4248 }
4249
4250 if (has_predicate) {
4251 /* we have predicate, so the current result must be list */
4252 if (node->nodetype != LYS_LIST) {
4253 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4254 return -1;
4255 }
4256
4257 i = resolve_schema_leafref_predicate(id, node, parent, op_node);
4258 if (!i) {
4259 return EXIT_FAILURE;
4260 } else if (i < 0) {
4261 return -1;
4262 }
4263 id += i;
4264 has_predicate = 0;
4265 }
4266 } while (id[0]);
4267
4268 /* the target must be leaf or leaf-list (in YANG 1.1 only) */
4269 if ((node->nodetype != LYS_LEAF) && (node->nodetype != LYS_LEAFLIST)) {
4270 LOGVAL(ctx, LYE_NORESOLV, LY_VLOG_LYS, parent, "leafref", type->info.lref.path);
4271 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Leafref target \"%s\" is not a leaf nor a leaf-list.", type->info.lref.path);
4272 return -1;
Michal Vasko1f76a282015-08-04 16:16:53 +02004273 }
Michal Vasko1f76a282015-08-04 16:16:53 +02004274
Michal Vasko74083ec2018-06-15 10:00:12 +02004275 /* check status */
4276 if (lyp_check_status(parent->flags, parent->module, parent->name,
4277 node->flags, node->module, node->name, node)) {
4278 return -1;
4279 }
4280
4281 /* assign */
4282 type->info.lref.target = (struct lys_node_leaf *)node;
Radek Krejcib1c12512015-08-11 11:22:04 +02004283 }
4284
Michal Vasko74083ec2018-06-15 10:00:12 +02004285 /* as the last thing traverse this leafref and make targets on the path implemented */
4286 if (lys_node_module(parent)->implemented) {
4287 /* make all the modules in the path implemented */
4288 for (node = (struct lys_node *)type->info.lref.target; node; node = lys_parent(node)) {
4289 if (!lys_node_module(node)->implemented) {
4290 lys_node_module(node)->implemented = 1;
4291 if (unres_schema_add_node(lys_node_module(node), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4292 return -1;
4293 }
4294 }
4295 }
4296
4297 /* store the backlink from leafref target */
4298 if (lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
4299 return -1;
4300 }
Radek Krejcicf509982015-12-15 09:22:44 +01004301 }
4302
Michal Vasko74083ec2018-06-15 10:00:12 +02004303 /* check if leafref and its target are under common if-features */
4304 return check_leafref_features(type);
Michal Vasko1f76a282015-08-04 16:16:53 +02004305}
4306
Michal Vasko730dfdf2015-08-11 14:48:05 +02004307/**
Michal Vasko718ecdd2017-10-03 14:12:39 +02004308 * @brief Compare 2 data node values.
4309 *
4310 * Comparison performed on canonical forms, the first value
4311 * is first transformed into canonical form.
4312 *
4313 * @param[in] node Leaf/leaf-list with these values.
4314 * @param[in] noncan_val Non-canonical value.
4315 * @param[in] noncan_val_len Length of \p noncal_val.
4316 * @param[in] can_val Canonical value.
4317 * @return 1 if equal, 0 if not, -1 on error (logged).
4318 */
4319static int
4320valequal(struct lys_node *node, const char *noncan_val, int noncan_val_len, const char *can_val)
4321{
4322 int ret;
4323 struct lyd_node_leaf_list leaf;
4324 struct lys_node_leaf *sleaf = (struct lys_node_leaf*)node;
4325
4326 /* dummy leaf */
4327 memset(&leaf, 0, sizeof leaf);
4328 leaf.value_str = lydict_insert(node->module->ctx, noncan_val, noncan_val_len);
4329
4330repeat:
4331 leaf.value_type = sleaf->type.base;
4332 leaf.schema = node;
4333
4334 if (leaf.value_type == LY_TYPE_LEAFREF) {
4335 if (!sleaf->type.info.lref.target) {
4336 /* it should either be unresolved leafref (leaf.value_type are ORed flags) or it will be resolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01004337 LOGINT(node->module->ctx);
Michal Vasko718ecdd2017-10-03 14:12:39 +02004338 ret = -1;
4339 goto finish;
4340 }
4341 sleaf = sleaf->type.info.lref.target;
4342 goto repeat;
4343 } else {
Michal Vasko35f46a82018-05-30 10:44:11 +02004344 if (!lyp_parse_value(&sleaf->type, &leaf.value_str, NULL, &leaf, NULL, NULL, 0, 0, 0)) {
Michal Vasko718ecdd2017-10-03 14:12:39 +02004345 ret = -1;
4346 goto finish;
4347 }
4348 }
4349
4350 if (!strcmp(leaf.value_str, can_val)) {
4351 ret = 1;
4352 } else {
4353 ret = 0;
4354 }
4355
4356finish:
4357 lydict_remove(node->module->ctx, leaf.value_str);
4358 return ret;
4359}
4360
4361/**
Michal Vaskof39142b2015-10-21 11:40:05 +02004362 * @brief Resolve instance-identifier predicate in JSON data format.
4363 * Does not log.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004364 *
Michal Vasko1b6ca962017-08-03 14:23:09 +02004365 * @param[in] prev_mod Previous module to use in case there is no prefix.
Michal Vaskobb211122015-08-19 14:03:11 +02004366 * @param[in] pred Predicate to use.
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004367 * @param[in,out] node Node matching the restriction without
4368 * the predicate. If it does not satisfy the predicate,
4369 * it is set to NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004370 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004371 * @return Number of characters successfully parsed,
4372 * positive on success, negative on failure.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004373 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004374static int
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004375resolve_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 +02004376{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004377 /* ... /node[key=value] ... */
4378 struct lyd_node_leaf_list *key;
4379 struct lys_node_leaf **list_keys = NULL;
Michal Vaskoab8adcd2017-10-02 13:32:24 +02004380 struct lys_node_list *slist = NULL;
Michal Vasko1f2cc332015-08-19 11:18:32 +02004381 const char *model, *name, *value;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004382 int mod_len, nam_len, val_len, i, has_predicate, parsed;
Michal Vasko53b7da02018-02-13 15:28:42 +01004383 struct ly_ctx *ctx = prev_mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004384
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004385 assert(pred && node && *node);
Michal Vasko1f2cc332015-08-19 11:18:32 +02004386
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004387 parsed = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004388 do {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004389 if ((i = parse_predicate(pred + parsed, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) {
4390 return -parsed + i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004391 }
4392 parsed += i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004393
Michal Vasko88850b72017-10-02 13:13:21 +02004394 if (!(*node)) {
4395 /* just parse it all */
4396 continue;
4397 }
4398
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004399 /* target */
4400 if (name[0] == '.') {
4401 /* leaf-list value */
4402 if ((*node)->schema->nodetype != LYS_LEAFLIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004403 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects leaf-list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004404 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4405 parsed = -1;
4406 goto cleanup;
4407 }
4408
4409 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004410 if (!valequal((*node)->schema, value, val_len, ((struct lyd_node_leaf_list *)*node)->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004411 *node = NULL;
4412 goto cleanup;
4413 }
4414
4415 } else if (isdigit(name[0])) {
Michal Vaskob2f40be2016-09-08 16:03:48 +02004416 assert(!value);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004417
4418 /* keyless list position */
4419 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004420 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004421 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4422 parsed = -1;
4423 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004424 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004425
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004426 if (((struct lys_node_list *)(*node)->schema)->keys) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004427 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 +02004428 (*node)->schema->name);
4429 parsed = -1;
4430 goto cleanup;
4431 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004432
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004433 /* check the index */
4434 if (atoi(name) != cur_idx) {
4435 *node = NULL;
4436 goto cleanup;
4437 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004438
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004439 } else {
4440 /* list key value */
4441 if ((*node)->schema->nodetype != LYS_LIST) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004442 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list, but have %s \"%s\".",
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004443 strnodetype((*node)->schema->nodetype), (*node)->schema->name);
4444 parsed = -1;
4445 goto cleanup;
4446 }
4447 slist = (struct lys_node_list *)(*node)->schema;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004448
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004449 /* prepare key array */
4450 if (!list_keys) {
4451 list_keys = malloc(slist->keys_size * sizeof *list_keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01004452 LY_CHECK_ERR_RETURN(!list_keys, LOGMEM(ctx), -1);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004453 for (i = 0; i < slist->keys_size; ++i) {
4454 list_keys[i] = slist->keys[i];
Michal Vaskob2f40be2016-09-08 16:03:48 +02004455 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004456 }
4457
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004458 /* find the schema key leaf */
4459 for (i = 0; i < slist->keys_size; ++i) {
4460 if (list_keys[i] && !strncmp(list_keys[i]->name, name, nam_len) && !list_keys[i]->name[nam_len]) {
4461 break;
4462 }
4463 }
4464 if (i == slist->keys_size) {
4465 /* this list has no such key */
Michal Vasko53b7da02018-02-13 15:28:42 +01004466 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Instance identifier expects list with the key \"%.*s\","
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004467 " but list \"%s\" does not define it.", nam_len, name, slist->name);
4468 parsed = -1;
4469 goto cleanup;
4470 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004471
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004472 /* check module */
4473 if (model) {
4474 if (strncmp(list_keys[i]->module->name, model, mod_len) || list_keys[i]->module->name[mod_len]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004475 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 +02004476 list_keys[i]->name, model, mod_len, list_keys[i]->module->name);
4477 parsed = -1;
4478 goto cleanup;
4479 }
4480 } else {
4481 if (list_keys[i]->module != prev_mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004482 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 +02004483 list_keys[i]->name, prev_mod->name, list_keys[i]->module->name);
4484 parsed = -1;
4485 goto cleanup;
4486 }
4487 }
4488
4489 /* find the actual data key */
4490 for (key = (struct lyd_node_leaf_list *)(*node)->child; key; key = (struct lyd_node_leaf_list *)key->next) {
4491 if (key->schema == (struct lys_node *)list_keys[i]) {
4492 break;
4493 }
4494 }
4495 if (!key) {
4496 /* list instance is missing a key? definitely should not happen */
Michal Vasko53b7da02018-02-13 15:28:42 +01004497 LOGINT(ctx);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004498 parsed = -1;
4499 goto cleanup;
4500 }
4501
4502 /* check the value */
Michal Vasko718ecdd2017-10-03 14:12:39 +02004503 if (!valequal(key->schema, value, val_len, key->value_str)) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004504 *node = NULL;
Michal Vasko88850b72017-10-02 13:13:21 +02004505 /* we still want to parse the whole predicate */
4506 continue;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004507 }
4508
4509 /* everything is fine, mark this key as resolved */
4510 list_keys[i] = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004511 }
4512 } while (has_predicate);
4513
Michal Vaskob2f40be2016-09-08 16:03:48 +02004514 /* check that all list keys were specified */
Michal Vasko88850b72017-10-02 13:13:21 +02004515 if (*node && list_keys) {
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004516 for (i = 0; i < slist->keys_size; ++i) {
4517 if (list_keys[i]) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004518 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 +02004519 parsed = -1;
4520 goto cleanup;
Michal Vaskob2f40be2016-09-08 16:03:48 +02004521 }
4522 }
Michal Vaskob2f40be2016-09-08 16:03:48 +02004523 }
4524
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02004525cleanup:
4526 free(list_keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004527 return parsed;
4528}
4529
Michal Vasko895c11f2018-03-12 11:35:58 +01004530static int
4531check_xpath(struct lys_node *node, int check_place)
Michal Vasko9e635ac2016-10-17 11:44:09 +02004532{
Michal Vasko0b963112017-08-11 12:45:36 +02004533 struct lys_node *parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004534 struct lyxp_set set;
Michal Vasko895c11f2018-03-12 11:35:58 +01004535 enum int_log_opts prev_ilo;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004536
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004537 if (check_place) {
4538 parent = node;
4539 while (parent) {
4540 if (parent->nodetype == LYS_GROUPING) {
4541 /* unresolved grouping, skip for now (will be checked later) */
Michal Vasko9e635ac2016-10-17 11:44:09 +02004542 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004543 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004544 if (parent->nodetype == LYS_AUGMENT) {
4545 if (!((struct lys_node_augment *)parent)->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004546 /* unresolved augment, skip for now (will be checked later) */
4547 return EXIT_FAILURE;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004548 } else {
4549 parent = ((struct lys_node_augment *)parent)->target;
4550 continue;
4551 }
4552 }
4553 parent = parent->parent;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004554 }
Michal Vasko9e635ac2016-10-17 11:44:09 +02004555 }
4556
Michal Vasko895c11f2018-03-12 11:35:58 +01004557 memset(&set, 0, sizeof set);
Michal Vasko9e635ac2016-10-17 11:44:09 +02004558
Michal Vasko895c11f2018-03-12 11:35:58 +01004559 /* produce just warnings */
4560 ly_ilo_change(NULL, ILO_ERR2WRN, &prev_ilo, NULL);
4561 lyxp_node_atomize(node, &set, 1);
4562 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
4563
4564 if (set.val.snodes) {
4565 free(set.val.snodes);
4566 }
4567 return EXIT_SUCCESS;
Michal Vasko9e635ac2016-10-17 11:44:09 +02004568}
4569
Radek Krejcif71f48f2016-10-25 16:37:24 +02004570static int
4571check_leafref_config(struct lys_node_leaf *leaf, struct lys_type *type)
4572{
Radek Krejcidce5f972017-09-12 15:47:49 +02004573 unsigned int i;
Radek Krejcif71f48f2016-10-25 16:37:24 +02004574
4575 if (type->base == LY_TYPE_LEAFREF) {
Radek Krejcic688ca02017-03-20 12:54:39 +01004576 if ((leaf->flags & LYS_CONFIG_W) && type->info.lref.target && type->info.lref.req != -1 &&
4577 (type->info.lref.target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004578 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 +02004579 strnodetype(leaf->nodetype), strnodetype(type->info.lref.target->nodetype));
4580 return -1;
4581 }
4582 /* we can skip the test in case the leafref is not yet resolved. In that case the test is done in the time
4583 * of leafref resolving (lys_leaf_add_leafref_target()) */
4584 } else if (type->base == LY_TYPE_UNION) {
4585 for (i = 0; i < type->info.uni.count; i++) {
4586 if (check_leafref_config(leaf, &type->info.uni.types[i])) {
4587 return -1;
4588 }
4589 }
4590 }
4591 return 0;
4592}
4593
Michal Vasko9e635ac2016-10-17 11:44:09 +02004594/**
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004595 * @brief Passes config flag down to children, skips nodes without config flags.
Michal Vasko44ab1462017-05-18 13:18:36 +02004596 * Logs.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004597 *
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004598 * @param[in] node Siblings and their children to have flags changed.
Michal Vaskoe022a562016-09-27 14:24:15 +02004599 * @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 +02004600 * @param[in] flags Flags to assign to all the nodes.
Radek Krejcib3142312016-11-09 11:04:12 +01004601 * @param[in,out] unres List of unresolved items.
Michal Vaskoa86508c2016-08-26 14:30:19 +02004602 *
4603 * @return 0 on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004604 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004605int
4606inherit_config_flag(struct lys_node *node, int flags, int clear)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004607{
Radek Krejcif71f48f2016-10-25 16:37:24 +02004608 struct lys_node_leaf *leaf;
Michal Vasko53b7da02018-02-13 15:28:42 +01004609 struct ly_ctx *ctx;
4610
4611 if (!node) {
4612 return 0;
4613 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004614
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004615 assert(!(flags ^ (flags & LYS_CONFIG_MASK)));
Michal Vasko53b7da02018-02-13 15:28:42 +01004616 ctx = node->module->ctx;
4617
Radek Krejci1d82ef62015-08-07 14:44:40 +02004618 LY_TREE_FOR(node, node) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004619 if (clear) {
4620 node->flags &= ~LYS_CONFIG_MASK;
Michal Vaskoc2a8d362016-09-29 08:50:13 +02004621 node->flags &= ~LYS_CONFIG_SET;
Michal Vaskoe022a562016-09-27 14:24:15 +02004622 } else {
4623 if (node->flags & LYS_CONFIG_SET) {
4624 /* skip nodes with an explicit config value */
4625 if ((flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004626 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, node, "true", "config");
4627 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe022a562016-09-27 14:24:15 +02004628 return -1;
4629 }
4630 continue;
Michal Vaskoa86508c2016-08-26 14:30:19 +02004631 }
Michal Vaskoe022a562016-09-27 14:24:15 +02004632
4633 if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) {
4634 node->flags = (node->flags & ~LYS_CONFIG_MASK) | flags;
4635 /* check that configuration lists have keys */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02004636 if ((node->nodetype == LYS_LIST) && (node->flags & LYS_CONFIG_W)
4637 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004638 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, node, "key", "list");
Michal Vaskoe022a562016-09-27 14:24:15 +02004639 return -1;
4640 }
4641 }
Michal Vaskoaec34ea2016-05-19 15:21:40 +02004642 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02004643 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004644 if (inherit_config_flag(node->child, flags, clear)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02004645 return -1;
4646 }
Radek Krejcif71f48f2016-10-25 16:37:24 +02004647 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4648 leaf = (struct lys_node_leaf *)node;
4649 if (check_leafref_config(leaf, &leaf->type)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02004650 return -1;
4651 }
4652 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004653 }
Michal Vaskoa86508c2016-08-26 14:30:19 +02004654
4655 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004656}
4657
Michal Vasko730dfdf2015-08-11 14:48:05 +02004658/**
Michal Vasko7178e692016-02-12 15:58:05 +01004659 * @brief Resolve augment target. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004660 *
Michal Vaskobb211122015-08-19 14:03:11 +02004661 * @param[in] aug Augment to use.
Michal Vasko97234262018-02-01 09:53:01 +01004662 * @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 +01004663 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004664 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004665 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02004666 */
Michal Vasko7178e692016-02-12 15:58:05 +01004667static int
Michal Vasko97234262018-02-01 09:53:01 +01004668resolve_augment(struct lys_node_augment *aug, struct lys_node *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004669{
Michal Vasko44ab1462017-05-18 13:18:36 +02004670 int rc;
Michal Vasko1d87a922015-08-21 12:57:16 +02004671 struct lys_node *sub;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004672 struct lys_module *mod;
Michal Vasko50576712017-07-28 12:28:33 +02004673 struct ly_set *set;
Michal Vasko53b7da02018-02-13 15:28:42 +01004674 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004675
Michal Vasko2ef7db62017-06-12 09:24:02 +02004676 assert(aug);
Radek Krejcidf46e222016-11-08 11:57:37 +01004677 mod = lys_main_module(aug->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01004678 ctx = mod->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004679
Michal Vaskobb520442017-05-23 10:55:18 +02004680 /* set it as not applied for now */
4681 aug->flags |= LYS_NOTAPPLIED;
4682
Michal Vasko2ef7db62017-06-12 09:24:02 +02004683 /* it can already be resolved in case we returned EXIT_FAILURE from if block below */
Michal Vasko44ab1462017-05-18 13:18:36 +02004684 if (!aug->target) {
Michal Vasko2ef7db62017-06-12 09:24:02 +02004685 /* resolve target node */
Michal Vasko97234262018-02-01 09:53:01 +01004686 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 +02004687 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004688 LOGVAL(ctx, LYE_PATH, LY_VLOG_LYS, aug);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004689 return -1;
4690 }
Michal Vasko50576712017-07-28 12:28:33 +02004691 if (!set) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004692 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name);
Michal Vasko2ef7db62017-06-12 09:24:02 +02004693 return EXIT_FAILURE;
4694 }
Michal Vasko50576712017-07-28 12:28:33 +02004695 aug->target = set->set.s[0];
4696 ly_set_free(set);
Michal Vasko15b36692016-08-26 15:29:54 +02004697 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004698
Michal Vasko74083ec2018-06-15 10:00:12 +02004699 /* make this module implemented if the target module is (if the target is in an unimplemented module,
4700 * it is fine because when we will be making that module implemented, its augment will be applied
4701 * and that augment target module made implemented, recursively) */
4702 if (mod->implemented && !lys_node_module(aug->target)->implemented) {
4703 lys_node_module(aug->target)->implemented = 1;
4704 if (unres_schema_add_node(lys_node_module(aug->target), unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) {
4705 return -1;
4706 }
4707 }
4708
Michal Vaskod58d5962016-03-02 14:29:41 +01004709 /* check for mandatory nodes - if the target node is in another module
4710 * the added nodes cannot be mandatory
4711 */
Michal Vasko44ab1462017-05-18 13:18:36 +02004712 if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target))
4713 && (rc = lyp_check_mandatory_augment(aug, aug->target))) {
Radek Krejcie00d2312016-08-12 15:27:49 +02004714 return rc;
Michal Vaskod58d5962016-03-02 14:29:41 +01004715 }
4716
Michal Vasko07e89ef2016-03-03 13:28:57 +01004717 /* check augment target type and then augment nodes type */
Michal Vasko44ab1462017-05-18 13:18:36 +02004718 if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST)) {
Michal Vaskodb017262017-01-24 13:10:04 +01004719 LY_TREE_FOR(aug->child, sub) {
4720 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES
4721 | LYS_CHOICE | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004722 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4723 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004724 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vaskodb017262017-01-24 13:10:04 +01004725 return -1;
4726 }
4727 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004728 } else if (aug->target->nodetype & (LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004729 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004730 if (!(sub->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004731 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4732 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004733 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004734 return -1;
4735 }
4736 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004737 } else if (aug->target->nodetype == LYS_CHOICE) {
Michal Vasko07e89ef2016-03-03 13:28:57 +01004738 LY_TREE_FOR(aug->child, sub) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02004739 if (!(sub->nodetype & (LYS_CASE | LYS_ANYDATA | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004740 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment");
4741 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Cannot augment \"%s\" with a \"%s\".",
Michal Vasko44ab1462017-05-18 13:18:36 +02004742 strnodetype(aug->target->nodetype), strnodetype(sub->nodetype));
Michal Vasko07e89ef2016-03-03 13:28:57 +01004743 return -1;
4744 }
4745 }
4746 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004747 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node");
4748 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 +01004749 return -1;
4750 }
4751
Radek Krejcic071c542016-01-27 14:57:51 +01004752 /* check identifier uniqueness as in lys_node_addchild() */
Michal Vasko1d87a922015-08-21 12:57:16 +02004753 LY_TREE_FOR(aug->child, sub) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004754 if (lys_check_id(sub, aug->target, NULL)) {
Michal Vasko3e6665f2015-08-17 14:00:38 +02004755 return -1;
Radek Krejci07911992015-08-14 15:13:31 +02004756 }
4757 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004758
Michal Vasko44ab1462017-05-18 13:18:36 +02004759 if (!aug->child) {
4760 /* empty augment, nothing to connect, but it is techincally applied */
Michal Vasko53b7da02018-02-13 15:28:42 +01004761 LOGWRN(ctx, "Augment \"%s\" without children.", aug->target_name);
Michal Vasko44ab1462017-05-18 13:18:36 +02004762 aug->flags &= ~LYS_NOTAPPLIED;
Radek Krejciaa6b2a12017-10-26 15:52:39 +02004763 } else if ((aug->parent || mod->implemented) && apply_aug(aug, unres)) {
4764 /* we try to connect the augment only in case the module is implemented or
4765 * the augment applies on the used grouping, anyway we failed here */
Michal Vasko44ab1462017-05-18 13:18:36 +02004766 return -1;
Michal Vasko15b36692016-08-26 15:29:54 +02004767 }
4768
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004769 return EXIT_SUCCESS;
4770}
4771
Radek Krejcie534c132016-11-23 13:32:31 +01004772static int
Radek Krejcia7db9702017-01-20 12:55:14 +01004773resolve_extension(struct unres_ext *info, struct lys_ext_instance **ext, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01004774{
4775 enum LY_VLOG_ELEM vlog_type;
4776 void *vlog_node;
4777 unsigned int i, j;
Radek Krejcie534c132016-11-23 13:32:31 +01004778 struct lys_ext *e;
PavolVicanc1807262017-01-31 18:00:27 +01004779 char *ext_name, *ext_prefix, *tmp;
Radek Krejcie534c132016-11-23 13:32:31 +01004780 struct lyxml_elem *next_yin, *yin;
Radek Krejcia7db9702017-01-20 12:55:14 +01004781 const struct lys_module *mod;
PavolVican22e88682017-02-14 22:38:18 +01004782 struct lys_ext_instance *tmp_ext;
Michal Vasko53b7da02018-02-13 15:28:42 +01004783 struct ly_ctx *ctx = NULL;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004784 LYEXT_TYPE etype;
Radek Krejcie534c132016-11-23 13:32:31 +01004785
4786 switch (info->parent_type) {
Radek Krejci0aa821a2016-12-08 11:21:35 +01004787 case LYEXT_PAR_NODE:
Radek Krejcie534c132016-11-23 13:32:31 +01004788 vlog_node = info->parent;
4789 vlog_type = LY_VLOG_LYS;
4790 break;
Radek Krejci0aa821a2016-12-08 11:21:35 +01004791 case LYEXT_PAR_MODULE:
4792 case LYEXT_PAR_IMPORT:
4793 case LYEXT_PAR_INCLUDE:
Radek Krejcie534c132016-11-23 13:32:31 +01004794 vlog_node = NULL;
4795 vlog_type = LY_VLOG_LYS;
4796 break;
Radek Krejci43ce4b72017-01-04 11:02:38 +01004797 default:
Radek Krejcie534c132016-11-23 13:32:31 +01004798 vlog_node = NULL;
Radek Krejci6a7fedf2017-02-10 12:38:06 +01004799 vlog_type = LY_VLOG_NONE;
Radek Krejcie534c132016-11-23 13:32:31 +01004800 break;
4801 }
4802
4803 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004804 /* YIN */
4805
Radek Krejcie534c132016-11-23 13:32:31 +01004806 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004807 mod = lyp_get_import_module_ns(info->mod, info->data.yin->ns->value);
Radek Krejcie534c132016-11-23 13:32:31 +01004808 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004809 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejci2b999ac2017-01-18 16:22:12 +01004810 return EXIT_FAILURE;
Radek Krejcie534c132016-11-23 13:32:31 +01004811 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004812 ctx = mod->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01004813
4814 /* find the extension definition */
4815 e = NULL;
4816 for (i = 0; i < mod->extensions_size; i++) {
4817 if (ly_strequal(mod->extensions[i].name, info->data.yin->name, 1)) {
4818 e = &mod->extensions[i];
4819 break;
4820 }
4821 }
4822 /* try submodules */
4823 for (j = 0; !e && j < mod->inc_size; j++) {
4824 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4825 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, info->data.yin->name, 1)) {
4826 e = &mod->inc[j].submodule->extensions[i];
4827 break;
4828 }
4829 }
4830 }
4831 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004832 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004833 return EXIT_FAILURE;
4834 }
4835
4836 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
Radek Krejcie534c132016-11-23 13:32:31 +01004837
Radek Krejci72b35992017-01-04 16:27:44 +01004838 if (e->plugin && e->plugin->check_position) {
4839 /* common part - we have plugin with position checking function, use it first */
4840 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
4841 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004842 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004843 return -1;
4844 }
4845 }
4846
Radek Krejci8d6b7422017-02-03 14:42:13 +01004847 /* extension type-specific part - allocation */
4848 if (e->plugin) {
4849 etype = e->plugin->type;
4850 } else {
4851 /* default type */
4852 etype = LYEXT_FLAG;
4853 }
4854 switch (etype) {
4855 case LYEXT_FLAG:
4856 (*ext) = calloc(1, sizeof(struct lys_ext_instance));
4857 break;
4858 case LYEXT_COMPLEX:
4859 (*ext) = calloc(1, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
4860 break;
4861 case LYEXT_ERR:
4862 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004863 LOGINT(ctx);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004864 return -1;
4865 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004866 LY_CHECK_ERR_RETURN(!*ext, LOGMEM(ctx), -1);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004867
4868 /* common part for all extension types */
4869 (*ext)->def = e;
4870 (*ext)->parent = info->parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004871 (*ext)->parent_type = info->parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01004872 (*ext)->insubstmt = info->substmt;
4873 (*ext)->insubstmt_index = info->substmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01004874 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01004875 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004876
PavolVicand86b0d62017-09-01 11:01:39 +02004877 if (e->argument) {
4878 if (!(e->flags & LYS_YINELEM)) {
4879 (*ext)->arg_value = lyxml_get_attr(info->data.yin, e->argument, NULL);
4880 if (!(*ext)->arg_value) {
PavolVicane7a1fc62018-02-19 16:50:27 +01004881 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, info->data.yin->name);
PavolVicand86b0d62017-09-01 11:01:39 +02004882 return -1;
4883 }
4884
4885 (*ext)->arg_value = lydict_insert(mod->ctx, (*ext)->arg_value, 0);
4886 } else {
4887 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4888 if (ly_strequal(yin->name, e->argument, 1)) {
4889 (*ext)->arg_value = lydict_insert(mod->ctx, yin->content, 0);
4890 lyxml_free(mod->ctx, yin);
4891 break;
4892 }
4893 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004894 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004895 }
4896
PavolVican92f23622017-12-12 13:35:56 +01004897 if ((*ext)->flags & LYEXT_OPT_VALID &&
4898 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01004899 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004900 }
4901
Radek Krejci7f1d47e2017-04-12 15:29:02 +02004902 (*ext)->nodetype = LYS_EXT;
4903 (*ext)->module = info->mod;
Radek Krejci5138e9f2017-04-12 13:10:46 +02004904
Radek Krejci8d6b7422017-02-03 14:42:13 +01004905 /* extension type-specific part - parsing content */
4906 switch (etype) {
4907 case LYEXT_FLAG:
Radek Krejci72b35992017-01-04 16:27:44 +01004908 LY_TREE_FOR_SAFE(info->data.yin->child, next_yin, yin) {
4909 if (!yin->ns) {
4910 /* garbage */
4911 lyxml_free(mod->ctx, yin);
4912 continue;
4913 } else if (!strcmp(yin->ns->value, LY_NSYIN)) {
4914 /* standard YANG statements are not expected here */
Michal Vasko53b7da02018-02-13 15:28:42 +01004915 LOGVAL(ctx, LYE_INCHILDSTMT, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejci72b35992017-01-04 16:27:44 +01004916 return -1;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004917 } else if (yin->ns == info->data.yin->ns &&
4918 (e->flags & LYS_YINELEM) && ly_strequal(yin->name, e->argument, 1)) {
Radek Krejci72b35992017-01-04 16:27:44 +01004919 /* we have the extension's argument */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004920 if ((*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004921 LOGVAL(ctx, LYE_TOOMANY, vlog_type, vlog_node, yin->name, info->data.yin->name);
Radek Krejcie534c132016-11-23 13:32:31 +01004922 return -1;
4923 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004924 (*ext)->arg_value = yin->content;
Radek Krejci72b35992017-01-04 16:27:44 +01004925 yin->content = NULL;
4926 lyxml_free(mod->ctx, yin);
4927 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004928 /* extension instance */
4929 if (lyp_yin_parse_subnode_ext(info->mod, *ext, LYEXT_PAR_EXTINST, yin,
4930 LYEXT_SUBSTMT_SELF, 0, unres)) {
4931 return -1;
4932 }
Radek Krejci72b35992017-01-04 16:27:44 +01004933
Radek Krejci72b35992017-01-04 16:27:44 +01004934 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004935 }
Radek Krejci72b35992017-01-04 16:27:44 +01004936 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004937 break;
4938 case LYEXT_COMPLEX:
Radek Krejcifebdad72017-02-06 11:35:51 +01004939 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004940 if (lyp_yin_parse_complex_ext(info->mod, (struct lys_ext_instance_complex*)(*ext), info->data.yin, unres)) {
4941 /* TODO memory cleanup */
Radek Krejci72b35992017-01-04 16:27:44 +01004942 return -1;
4943 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004944 break;
4945 default:
4946 break;
Radek Krejcie534c132016-11-23 13:32:31 +01004947 }
Radek Krejci72b35992017-01-04 16:27:44 +01004948
4949 /* TODO - lyext_check_result_clb, other than LYEXT_FLAG plugins */
4950
Radek Krejcie534c132016-11-23 13:32:31 +01004951 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004952 /* YANG */
4953
PavolVicanc1807262017-01-31 18:00:27 +01004954 ext_prefix = (char *)(*ext)->def;
4955 tmp = strchr(ext_prefix, ':');
4956 if (!tmp) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004957 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVican22e88682017-02-14 22:38:18 +01004958 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01004959 }
4960 ext_name = tmp + 1;
Radek Krejcie534c132016-11-23 13:32:31 +01004961
PavolVicanc1807262017-01-31 18:00:27 +01004962 /* get the module where the extension is supposed to be defined */
Michal Vasko921eb6b2017-10-13 10:01:39 +02004963 mod = lyp_get_module(info->mod, ext_prefix, tmp - ext_prefix, NULL, 0, 0);
PavolVicanc1807262017-01-31 18:00:27 +01004964 if (!mod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004965 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004966 return EXIT_FAILURE;
4967 }
Michal Vasko53b7da02018-02-13 15:28:42 +01004968 ctx = mod->ctx;
PavolVicanc1807262017-01-31 18:00:27 +01004969
4970 /* find the extension definition */
4971 e = NULL;
4972 for (i = 0; i < mod->extensions_size; i++) {
4973 if (ly_strequal(mod->extensions[i].name, ext_name, 0)) {
4974 e = &mod->extensions[i];
4975 break;
4976 }
4977 }
4978 /* try submodules */
4979 for (j = 0; !e && j < mod->inc_size; j++) {
4980 for (i = 0; i < mod->inc[j].submodule->extensions_size; i++) {
4981 if (ly_strequal(mod->inc[j].submodule->extensions[i].name, ext_name, 0)) {
4982 e = &mod->inc[j].submodule->extensions[i];
4983 break;
4984 }
4985 }
4986 }
4987 if (!e) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004988 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, ext_prefix);
PavolVicanc1807262017-01-31 18:00:27 +01004989 return EXIT_FAILURE;
4990 }
4991
PavolVicanfcc98762017-09-01 15:51:39 +02004992 (*ext)->flags &= ~LYEXT_OPT_YANG;
4993 (*ext)->def = NULL;
4994
PavolVicanc1807262017-01-31 18:00:27 +01004995 /* we have the extension definition, so now it cannot be forward referenced and error is always fatal */
4996
4997 if (e->plugin && e->plugin->check_position) {
4998 /* common part - we have plugin with position checking function, use it first */
4999 if ((*e->plugin->check_position)(info->parent, info->parent_type, info->substmt)) {
5000 /* extension is not allowed here */
Michal Vasko53b7da02018-02-13 15:28:42 +01005001 LOGVAL(ctx, LYE_INSTMT, vlog_type, vlog_node, e->name);
PavolVican22e88682017-02-14 22:38:18 +01005002 goto error;
PavolVicanc1807262017-01-31 18:00:27 +01005003 }
5004 }
5005
PavolVican22e88682017-02-14 22:38:18 +01005006 /* extension common part */
PavolVicanc1807262017-01-31 18:00:27 +01005007 (*ext)->def = e;
5008 (*ext)->parent = info->parent;
Radek Krejci8de8f612017-02-16 15:03:32 +01005009 (*ext)->ext_type = e->plugin ? e->plugin->type : LYEXT_FLAG;
PavolVican92f23622017-12-12 13:35:56 +01005010 (*ext)->flags |= e->plugin ? e->plugin->flags : 0;
PavolVican22e88682017-02-14 22:38:18 +01005011
PavolVicanb0d84102017-02-15 16:32:42 +01005012 if (e->argument && !(*ext)->arg_value) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005013 LOGVAL(ctx, LYE_MISSARG, LY_VLOG_NONE, NULL, e->argument, ext_name);
PavolVicanb0d84102017-02-15 16:32:42 +01005014 goto error;
5015 }
5016
PavolVican92f23622017-12-12 13:35:56 +01005017 if ((*ext)->flags & LYEXT_OPT_VALID &&
5018 (info->parent_type == LYEXT_PAR_NODE || info->parent_type == LYEXT_PAR_TPDF)) {
Michal Vasko1bdfd432018-03-09 09:30:19 +01005019 ((struct lys_node *)info->parent)->flags |= LYS_VALID_EXT;
PavolVican92f23622017-12-12 13:35:56 +01005020 }
5021
Radek Krejci7f1d47e2017-04-12 15:29:02 +02005022 (*ext)->module = info->mod;
5023 (*ext)->nodetype = LYS_EXT;
Radek Krejci5138e9f2017-04-12 13:10:46 +02005024
PavolVican22e88682017-02-14 22:38:18 +01005025 /* extension type-specific part */
5026 if (e->plugin) {
5027 etype = e->plugin->type;
5028 } else {
5029 /* default type */
5030 etype = LYEXT_FLAG;
PavolVicanc1807262017-01-31 18:00:27 +01005031 }
PavolVican22e88682017-02-14 22:38:18 +01005032 switch (etype) {
5033 case LYEXT_FLAG:
5034 /* nothing change */
5035 break;
5036 case LYEXT_COMPLEX:
5037 tmp_ext = realloc(*ext, ((struct lyext_plugin_complex*)e->plugin)->instance_size);
Michal Vasko53b7da02018-02-13 15:28:42 +01005038 LY_CHECK_ERR_GOTO(!tmp_ext, LOGMEM(ctx), error);
PavolVican22e88682017-02-14 22:38:18 +01005039 memset((char *)tmp_ext + sizeof **ext, 0, ((struct lyext_plugin_complex*)e->plugin)->instance_size - sizeof **ext);
5040 (*ext) = tmp_ext;
PavolVican22e88682017-02-14 22:38:18 +01005041 ((struct lys_ext_instance_complex*)(*ext))->substmt = ((struct lyext_plugin_complex*)e->plugin)->substmt;
PavolVicana1e291f2017-02-19 16:07:12 +01005042 if (info->data.yang) {
5043 *tmp = ':';
PavolVicandb0e8172017-02-20 00:46:09 +01005044 if (yang_parse_ext_substatement(info->mod, unres, info->data.yang->ext_substmt, ext_prefix,
5045 (struct lys_ext_instance_complex*)(*ext))) {
5046 goto error;
5047 }
5048 if (yang_fill_extcomplex_module(info->mod->ctx, (struct lys_ext_instance_complex*)(*ext), ext_prefix,
5049 info->data.yang->ext_modules, info->mod->implemented)) {
PavolVicana1e291f2017-02-19 16:07:12 +01005050 goto error;
5051 }
PavolVicana3876672017-02-21 15:49:51 +01005052 }
5053 if (lyp_mand_check_ext((struct lys_ext_instance_complex*)(*ext), ext_prefix)) {
5054 goto error;
PavolVicana1e291f2017-02-19 16:07:12 +01005055 }
PavolVican22e88682017-02-14 22:38:18 +01005056 break;
5057 case LYEXT_ERR:
5058 /* we never should be here */
Michal Vasko53b7da02018-02-13 15:28:42 +01005059 LOGINT(ctx);
PavolVican22e88682017-02-14 22:38:18 +01005060 goto error;
5061 }
5062
PavolVican22e88682017-02-14 22:38:18 +01005063 if (yang_check_ext_instance(info->mod, &(*ext)->ext, (*ext)->ext_size, *ext, unres)) {
5064 goto error;
5065 }
5066 free(ext_prefix);
Radek Krejcie534c132016-11-23 13:32:31 +01005067 }
5068
5069 return EXIT_SUCCESS;
PavolVican22e88682017-02-14 22:38:18 +01005070error:
5071 free(ext_prefix);
5072 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01005073}
5074
Michal Vasko730dfdf2015-08-11 14:48:05 +02005075/**
Pavol Vican855ca622016-09-05 13:07:54 +02005076 * @brief Resolve (find) choice default case. Does not log.
5077 *
5078 * @param[in] choic Choice to use.
5079 * @param[in] dflt Name of the default case.
5080 *
5081 * @return Pointer to the default node or NULL.
5082 */
5083static struct lys_node *
5084resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt)
5085{
5086 struct lys_node *child, *ret;
5087
5088 LY_TREE_FOR(choic->child, child) {
5089 if (child->nodetype == LYS_USES) {
5090 ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt);
5091 if (ret) {
5092 return ret;
5093 }
5094 }
5095
5096 if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYDATA | LYS_CASE
Radek Krejci2f792db2016-09-12 10:52:33 +02005097 | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Pavol Vican855ca622016-09-05 13:07:54 +02005098 return child;
5099 }
5100 }
5101
5102 return NULL;
5103}
5104
5105/**
Michal Vasko730dfdf2015-08-11 14:48:05 +02005106 * @brief Resolve uses, apply augments, refines. Logs directly.
5107 *
Michal Vaskobb211122015-08-19 14:03:11 +02005108 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005109 * @param[in,out] unres List of unresolved items.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005110 *
Michal Vaskodef0db12015-10-07 13:22:48 +02005111 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005112 */
Michal Vasko184521f2015-09-24 13:14:26 +02005113static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005114resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005115{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005116 struct ly_ctx *ctx = uses->module->ctx; /* shortcut */
Pavol Vican855ca622016-09-05 13:07:54 +02005117 struct lys_node *node = NULL, *next, *iter, **refine_nodes = NULL;
Pavol Vican55abd332016-07-12 15:54:49 +02005118 struct lys_node *node_aux, *parent, *tmp;
Radek Krejci200bf712016-08-16 17:11:04 +02005119 struct lys_node_leaflist *llist;
5120 struct lys_node_leaf *leaf;
Radek Krejci76512572015-08-04 09:47:08 +02005121 struct lys_refine *rfn;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005122 struct lys_restr *must, **old_must;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005123 struct lys_iffeature *iff, **old_iff;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005124 int i, j, k, rc;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005125 uint8_t size, *old_size;
Radek Krejci363bd4a2016-07-29 14:30:20 +02005126 unsigned int usize, usize1, usize2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005127
Michal Vasko71e1aa82015-08-12 12:17:51 +02005128 assert(uses->grp);
Radek Krejci6ff885d2017-01-03 14:06:22 +01005129
Radek Krejci93def382017-05-24 15:33:48 +02005130 /* check that the grouping is resolved (no unresolved uses inside) */
5131 assert(!uses->grp->unres_count);
Michal Vasko71e1aa82015-08-12 12:17:51 +02005132
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005133 /* copy the data nodes from grouping into the uses context */
Michal Vasko1e62a092015-12-01 12:27:20 +01005134 LY_TREE_FOR(uses->grp->child, node_aux) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01005135 if (node_aux->nodetype & LYS_GROUPING) {
5136 /* do not instantiate groupings from groupings */
5137 continue;
5138 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01005139 node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, unres, 0);
Michal Vasko1e62a092015-12-01 12:27:20 +01005140 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005141 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses");
5142 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Copying data from grouping failed.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005143 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005144 }
Pavol Vican55abd332016-07-12 15:54:49 +02005145 /* test the name of siblings */
Radek Krejcif95b6292017-02-13 15:57:37 +01005146 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 +02005147 if (!(tmp->nodetype & (LYS_USES | LYS_GROUPING | LYS_CASE)) && ly_strequal(tmp->name, node_aux->name, 1)) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005148 goto fail;
Pavol Vican55abd332016-07-12 15:54:49 +02005149 }
5150 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005151 }
Michal Vaskoe022a562016-09-27 14:24:15 +02005152
Michal Vaskodef0db12015-10-07 13:22:48 +02005153 /* we managed to copy the grouping, the rest must be possible to resolve */
5154
Pavol Vican855ca622016-09-05 13:07:54 +02005155 if (uses->refine_size) {
5156 refine_nodes = malloc(uses->refine_size * sizeof *refine_nodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01005157 LY_CHECK_ERR_GOTO(!refine_nodes, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005158 }
5159
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005160 /* apply refines */
5161 for (i = 0; i < uses->refine_size; i++) {
5162 rfn = &uses->refine[i];
Radek Krejcie2077412017-01-26 16:03:39 +01005163 rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child,
5164 LYS_NO_RPC_NOTIF_NODE | LYS_ACTION | LYS_NOTIF,
Michal Vaskodc300b02017-04-07 14:09:20 +02005165 0, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01005166 if (rc || !node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005167 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005168 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005169 }
5170
Radek Krejci1d82ef62015-08-07 14:44:40 +02005171 if (rfn->target_type && !(node->nodetype & rfn->target_type)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005172 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine");
5173 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Refine substatements not applicable to the target-node.");
Michal Vaskoa86508c2016-08-26 14:30:19 +02005174 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005175 }
Pavol Vican855ca622016-09-05 13:07:54 +02005176 refine_nodes[i] = node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005177
5178 /* description on any nodetype */
5179 if (rfn->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005180 lydict_remove(ctx, node->dsc);
5181 node->dsc = lydict_insert(ctx, rfn->dsc, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005182 }
5183
5184 /* reference on any nodetype */
5185 if (rfn->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005186 lydict_remove(ctx, node->ref);
5187 node->ref = lydict_insert(ctx, rfn->ref, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005188 }
5189
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005190 /* config on any nodetype,
5191 * in case of notification or rpc/action, the config is not applicable (there is no config status) */
5192 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005193 node->flags &= ~LYS_CONFIG_MASK;
5194 node->flags |= (rfn->flags & LYS_CONFIG_MASK);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005195 }
5196
5197 /* default value ... */
Radek Krejci200bf712016-08-16 17:11:04 +02005198 if (rfn->dflt_size) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005199 if (node->nodetype == LYS_LEAF) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005200 /* leaf */
Radek Krejci200bf712016-08-16 17:11:04 +02005201 leaf = (struct lys_node_leaf *)node;
5202
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005203 /* replace default value */
Radek Krejci200bf712016-08-16 17:11:04 +02005204 lydict_remove(ctx, leaf->dflt);
5205 leaf->dflt = lydict_insert(ctx, rfn->dflt[0], 0);
5206
5207 /* check the default value */
Radek Krejci51673202016-11-01 17:00:32 +01005208 if (unres_schema_add_node(leaf->module, unres, &leaf->type, UNRES_TYPE_DFLT,
5209 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005210 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005211 }
Radek Krejci200bf712016-08-16 17:11:04 +02005212 } else if (node->nodetype == LYS_LEAFLIST) {
5213 /* leaf-list */
5214 llist = (struct lys_node_leaflist *)node;
5215
5216 /* remove complete set of defaults in target */
Radek Krejci542ab142017-01-23 15:57:08 +01005217 for (j = 0; j < llist->dflt_size; j++) {
5218 lydict_remove(ctx, llist->dflt[j]);
Radek Krejci200bf712016-08-16 17:11:04 +02005219 }
5220 free(llist->dflt);
5221
5222 /* copy the default set from refine */
Radek Krejciaa1303c2017-05-31 13:57:37 +02005223 llist->dflt = malloc(rfn->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01005224 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), fail);
Radek Krejci200bf712016-08-16 17:11:04 +02005225 llist->dflt_size = rfn->dflt_size;
Radek Krejci542ab142017-01-23 15:57:08 +01005226 for (j = 0; j < llist->dflt_size; j++) {
5227 llist->dflt[j] = lydict_insert(ctx, rfn->dflt[j], 0);
Radek Krejci200bf712016-08-16 17:11:04 +02005228 }
5229
5230 /* check default value */
Radek Krejci542ab142017-01-23 15:57:08 +01005231 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01005232 if (unres_schema_add_node(llist->module, unres, &llist->type, UNRES_TYPE_DFLT,
Radek Krejci542ab142017-01-23 15:57:08 +01005233 (struct lys_node *)(&llist->dflt[j])) == -1) {
Pavol Vican855ca622016-09-05 13:07:54 +02005234 goto fail;
Radek Krejci200bf712016-08-16 17:11:04 +02005235 }
5236 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005237 }
5238 }
5239
5240 /* mandatory on leaf, anyxml or choice */
Radek Krejci1574a8d2015-08-03 14:16:52 +02005241 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcibf285832017-01-26 16:05:41 +01005242 /* remove current value */
5243 node->flags &= ~LYS_MAND_MASK;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005244
Radek Krejcibf285832017-01-26 16:05:41 +01005245 /* set new value */
5246 node->flags |= (rfn->flags & LYS_MAND_MASK);
5247
Pavol Vican855ca622016-09-05 13:07:54 +02005248 if (rfn->flags & LYS_MAND_TRUE) {
5249 /* check if node has default value */
5250 if ((node->nodetype & LYS_LEAF) && ((struct lys_node_leaf *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005251 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005252 "The \"mandatory\" statement is forbidden on leaf with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005253 goto fail;
5254 }
5255 if ((node->nodetype & LYS_CHOICE) && ((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005256 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses,
Radek Krejcibdcaf242017-04-19 10:29:47 +02005257 "The \"mandatory\" statement is forbidden on choices with \"default\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005258 goto fail;
5259 }
5260 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005261 }
5262
5263 /* presence on container */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005264 if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) {
5265 lydict_remove(ctx, ((struct lys_node_container *)node)->presence);
5266 ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005267 }
5268
5269 /* min/max-elements on list or leaf-list */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005270 if (node->nodetype == LYS_LIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005271 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005272 ((struct lys_node_list *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005273 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005274 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005275 ((struct lys_node_list *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005276 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005277 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005278 if (rfn->flags & LYS_RFN_MINSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005279 ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005280 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02005281 if (rfn->flags & LYS_RFN_MAXSET) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005282 ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005283 }
5284 }
5285
5286 /* must in leaf, leaf-list, list, container or anyxml */
5287 if (rfn->must_size) {
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005288 switch (node->nodetype) {
5289 case LYS_LEAF:
5290 old_size = &((struct lys_node_leaf *)node)->must_size;
5291 old_must = &((struct lys_node_leaf *)node)->must;
5292 break;
5293 case LYS_LEAFLIST:
5294 old_size = &((struct lys_node_leaflist *)node)->must_size;
5295 old_must = &((struct lys_node_leaflist *)node)->must;
5296 break;
5297 case LYS_LIST:
5298 old_size = &((struct lys_node_list *)node)->must_size;
5299 old_must = &((struct lys_node_list *)node)->must;
5300 break;
5301 case LYS_CONTAINER:
5302 old_size = &((struct lys_node_container *)node)->must_size;
5303 old_must = &((struct lys_node_container *)node)->must;
5304 break;
5305 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02005306 case LYS_ANYDATA:
5307 old_size = &((struct lys_node_anydata *)node)->must_size;
5308 old_must = &((struct lys_node_anydata *)node)->must;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005309 break;
5310 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01005311 LOGINT(ctx);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005312 goto fail;
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005313 }
5314
5315 size = *old_size + rfn->must_size;
5316 must = realloc(*old_must, size * sizeof *rfn->must);
Michal Vasko53b7da02018-02-13 15:28:42 +01005317 LY_CHECK_ERR_GOTO(!must, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005318 for (k = 0, j = *old_size; k < rfn->must_size; k++, j++) {
Radek Krejci7f0164a2017-01-25 17:04:06 +01005319 must[j].ext_size = rfn->must[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005320 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 +02005321 &must[j].ext, 0, unres);
Pavol Vican855ca622016-09-05 13:07:54 +02005322 must[j].expr = lydict_insert(ctx, rfn->must[k].expr, 0);
5323 must[j].dsc = lydict_insert(ctx, rfn->must[k].dsc, 0);
5324 must[j].ref = lydict_insert(ctx, rfn->must[k].ref, 0);
5325 must[j].eapptag = lydict_insert(ctx, rfn->must[k].eapptag, 0);
5326 must[j].emsg = lydict_insert(ctx, rfn->must[k].emsg, 0);
Radek Krejcicfcd8a52017-09-04 13:19:57 +02005327 must[j].flags = rfn->must[k].flags;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005328 }
5329
Michal Vaskoef2fdc82015-09-24 09:54:42 +02005330 *old_must = must;
5331 *old_size = size;
Michal Vasko508a50d2016-09-07 14:50:33 +02005332
5333 /* check XPath dependencies again */
5334 if (unres_schema_add_node(node->module, unres, node, UNRES_XPATH, NULL) == -1) {
5335 goto fail;
5336 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005337 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02005338
5339 /* if-feature in leaf, leaf-list, list, container or anyxml */
5340 if (rfn->iffeature_size) {
5341 old_size = &node->iffeature_size;
5342 old_iff = &node->iffeature;
5343
5344 size = *old_size + rfn->iffeature_size;
5345 iff = realloc(*old_iff, size * sizeof *rfn->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01005346 LY_CHECK_ERR_GOTO(!iff, LOGMEM(ctx), fail);
Radek Krejci3a3b2002017-09-13 16:39:02 +02005347 *old_iff = iff;
5348
Pavol Vican855ca622016-09-05 13:07:54 +02005349 for (k = 0, j = *old_size; k < rfn->iffeature_size; k++, j++) {
5350 resolve_iffeature_getsizes(&rfn->iffeature[k], &usize1, &usize2);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005351 if (usize1) {
5352 /* there is something to duplicate */
5353 /* duplicate compiled expression */
5354 usize = (usize1 / 4) + (usize1 % 4) ? 1 : 0;
5355 iff[j].expr = malloc(usize * sizeof *iff[j].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01005356 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005357 memcpy(iff[j].expr, rfn->iffeature[k].expr, usize * sizeof *iff[j].expr);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005358
5359 /* duplicate list of feature pointers */
Pavol Vican855ca622016-09-05 13:07:54 +02005360 iff[j].features = malloc(usize2 * sizeof *iff[k].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01005361 LY_CHECK_ERR_GOTO(!iff[j].expr, LOGMEM(ctx), fail);
Pavol Vican855ca622016-09-05 13:07:54 +02005362 memcpy(iff[j].features, rfn->iffeature[k].features, usize2 * sizeof *iff[j].features);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005363
Radek Krejci3a3b2002017-09-13 16:39:02 +02005364 /* duplicate extensions */
5365 iff[j].ext_size = rfn->iffeature[k].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01005366 lys_ext_dup(ctx, rfn->module, rfn->iffeature[k].ext, rfn->iffeature[k].ext_size,
Radek Krejci3a3b2002017-09-13 16:39:02 +02005367 &rfn->iffeature[k], LYEXT_PAR_IFFEATURE, &iff[j].ext, 0, unres);
5368 }
5369 (*old_size)++;
5370 }
5371 assert(*old_size == size);
Radek Krejci363bd4a2016-07-29 14:30:20 +02005372 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005373 }
5374
5375 /* apply augments */
5376 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko97234262018-02-01 09:53:01 +01005377 rc = resolve_augment(&uses->augment[i], (struct lys_node *)uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005378 if (rc) {
Michal Vaskoa86508c2016-08-26 14:30:19 +02005379 goto fail;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005380 }
5381 }
5382
Pavol Vican855ca622016-09-05 13:07:54 +02005383 /* check refines */
5384 for (i = 0; i < uses->refine_size; i++) {
5385 node = refine_nodes[i];
5386 rfn = &uses->refine[i];
5387
5388 /* config on any nodetype */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02005389 if ((rfn->flags & LYS_CONFIG_MASK) && (node->flags & LYS_CONFIG_MASK)) {
Pavol Vican855ca622016-09-05 13:07:54 +02005390 for (parent = lys_parent(node); parent && parent->nodetype == LYS_USES; parent = lys_parent(parent));
Radek Krejci5c08a992016-11-02 13:30:04 +01005391 if (parent && parent->nodetype != LYS_GROUPING && (parent->flags & LYS_CONFIG_MASK) &&
Pavol Vican855ca622016-09-05 13:07:54 +02005392 ((parent->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) &&
5393 (rfn->flags & LYS_CONFIG_W)) {
5394 /* setting config true under config false is prohibited */
Michal Vasko53b7da02018-02-13 15:28:42 +01005395 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5396 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005397 "changing config from 'false' to 'true' is prohibited while "
5398 "the target's parent is still config 'false'.");
5399 goto fail;
5400 }
5401
5402 /* inherit config change to the target children */
5403 LY_TREE_DFS_BEGIN(node->child, next, iter) {
5404 if (rfn->flags & LYS_CONFIG_W) {
5405 if (iter->flags & LYS_CONFIG_SET) {
5406 /* config is set explicitely, go to next sibling */
5407 next = NULL;
5408 goto nextsibling;
5409 }
5410 } else { /* LYS_CONFIG_R */
5411 if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) {
5412 /* error - we would have config data under status data */
Michal Vasko53b7da02018-02-13 15:28:42 +01005413 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, "config", "refine");
5414 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005415 "changing config from 'true' to 'false' is prohibited while the target "
5416 "has still a children with explicit config 'true'.");
5417 goto fail;
5418 }
5419 }
5420 /* change config */
5421 iter->flags &= ~LYS_CONFIG_MASK;
5422 iter->flags |= (rfn->flags & LYS_CONFIG_MASK);
5423
5424 /* select next iter - modified LY_TREE_DFS_END */
5425 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5426 next = NULL;
5427 } else {
5428 next = iter->child;
5429 }
5430nextsibling:
5431 if (!next) {
5432 /* try siblings */
5433 next = iter->next;
5434 }
5435 while (!next) {
5436 /* parent is already processed, go to its sibling */
5437 iter = lys_parent(iter);
5438
5439 /* no siblings, go back through parents */
5440 if (iter == node) {
5441 /* we are done, no next element to process */
5442 break;
5443 }
5444 next = iter->next;
5445 }
5446 }
5447 }
5448
5449 /* default value */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005450 if (rfn->dflt_size) {
5451 if (node->nodetype == LYS_CHOICE) {
5452 /* choice */
5453 ((struct lys_node_choice *)node)->dflt = resolve_choice_dflt((struct lys_node_choice *)node,
5454 rfn->dflt[0]);
5455 if (!((struct lys_node_choice *)node)->dflt) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005456 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, uses, rfn->dflt[0], "default");
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005457 goto fail;
5458 }
5459 if (lyp_check_mandatory_choice(node)) {
5460 goto fail;
5461 }
Pavol Vican855ca622016-09-05 13:07:54 +02005462 }
5463 }
5464
5465 /* min/max-elements on list or leaf-list */
Radek Krejci2d3c8112017-04-19 10:20:50 +02005466 if (node->nodetype == LYS_LIST && ((struct lys_node_list *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005467 if (((struct lys_node_list *)node)->min > ((struct lys_node_list *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005468 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5469 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005470 goto fail;
5471 }
Radek Krejci2d3c8112017-04-19 10:20:50 +02005472 } else if (node->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)node)->max) {
Pavol Vican855ca622016-09-05 13:07:54 +02005473 if (((struct lys_node_leaflist *)node)->min > ((struct lys_node_leaflist *)node)->max) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005474 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, uses, "Invalid value \"%d\" of \"%s\".", rfn->mod.list.min, "min-elements");
5475 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Pavol Vican855ca622016-09-05 13:07:54 +02005476 goto fail;
5477 }
5478 }
5479
5480 /* additional checks */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005481 /* default value with mandatory/min-elements */
Pavol Vican855ca622016-09-05 13:07:54 +02005482 if (node->nodetype == LYS_LEAFLIST) {
5483 llist = (struct lys_node_leaflist *)node;
5484 if (llist->dflt_size && llist->min) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005485 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "min-elements", "refine");
5486 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Pavol Vican855ca622016-09-05 13:07:54 +02005487 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
5488 goto fail;
5489 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005490 } else if (node->nodetype == LYS_LEAF) {
5491 leaf = (struct lys_node_leaf *)node;
5492 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005493 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, uses, rfn->dflt_size ? "default" : "mandatory", "refine");
5494 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005495 "The \"mandatory\" statement is forbidden on leafs with the \"default\" statement.");
5496 goto fail;
5497 }
Pavol Vican855ca622016-09-05 13:07:54 +02005498 }
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005499
Pavol Vican855ca622016-09-05 13:07:54 +02005500 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
Radek Krejci4c5fbd32016-10-25 15:14:23 +02005501 if ((rfn->flags & LYS_MAND_TRUE) || rfn->mod.list.min) {
Pavol Vican855ca622016-09-05 13:07:54 +02005502 for (parent = node->parent;
5503 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION | LYS_USES));
5504 parent = parent->parent) {
5505 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
5506 /* stop also on presence containers */
5507 break;
5508 }
5509 }
5510 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
5511 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
5512 if (lyp_check_mandatory_choice(parent)) {
5513 goto fail;
5514 }
5515 }
5516 }
5517 }
5518 free(refine_nodes);
5519
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005520 return EXIT_SUCCESS;
Michal Vaskoa86508c2016-08-26 14:30:19 +02005521
5522fail:
5523 LY_TREE_FOR_SAFE(uses->child, next, iter) {
5524 lys_node_free(iter, NULL, 0);
5525 }
Pavol Vican855ca622016-09-05 13:07:54 +02005526 free(refine_nodes);
Michal Vaskoa86508c2016-08-26 14:30:19 +02005527 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005528}
5529
Radek Krejci83a4bac2017-02-07 15:53:04 +01005530void
5531resolve_identity_backlink_update(struct lys_ident *der, struct lys_ident *base)
Radek Krejci018f1f52016-08-03 16:01:20 +02005532{
5533 int i;
5534
5535 assert(der && base);
5536
Radek Krejci018f1f52016-08-03 16:01:20 +02005537 if (!base->der) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005538 /* create a set for backlinks if it does not exist */
5539 base->der = ly_set_new();
Radek Krejci018f1f52016-08-03 16:01:20 +02005540 }
Radek Krejci85a54be2016-10-20 12:39:56 +02005541 /* store backlink */
5542 ly_set_add(base->der, der, LY_SET_OPT_USEASLIST);
Radek Krejci018f1f52016-08-03 16:01:20 +02005543
Radek Krejci85a54be2016-10-20 12:39:56 +02005544 /* do it recursively */
Radek Krejci018f1f52016-08-03 16:01:20 +02005545 for (i = 0; i < base->base_size; i++) {
Radek Krejci83a4bac2017-02-07 15:53:04 +01005546 resolve_identity_backlink_update(der, base->base[i]);
Radek Krejci018f1f52016-08-03 16:01:20 +02005547 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005548}
5549
Michal Vasko730dfdf2015-08-11 14:48:05 +02005550/**
5551 * @brief Resolve base identity recursively. Does not log.
5552 *
5553 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005554 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005555 * @param[in] basename Base name of the identity.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005556 * @param[out] ret Pointer to the resolved identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005557 *
Radek Krejci219fa612016-08-15 10:36:51 +02005558 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on crucial error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005559 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005560static int
Michal Vasko1e62a092015-12-01 12:27:20 +01005561resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename,
Radek Krejci018f1f52016-08-03 16:01:20 +02005562 struct unres_schema *unres, struct lys_ident **ret)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005563{
Michal Vaskof02e3742015-08-05 16:27:02 +02005564 uint32_t i, j;
Radek Krejci018f1f52016-08-03 16:01:20 +02005565 struct lys_ident *base = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01005566 struct ly_ctx *ctx = module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005567
Radek Krejcicf509982015-12-15 09:22:44 +01005568 assert(ret);
5569
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005570 /* search module */
5571 for (i = 0; i < module->ident_size; i++) {
5572 if (!strcmp(basename, module->ident[i].name)) {
5573
5574 if (!ident) {
5575 /* just search for type, so do not modify anything, just return
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005576 * the base identity pointer */
Radek Krejcicf509982015-12-15 09:22:44 +01005577 *ret = &module->ident[i];
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005578 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005579 }
5580
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005581 base = &module->ident[i];
5582 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005583 }
5584 }
5585
5586 /* search submodules */
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005587 for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) {
5588 for (i = 0; i < module->inc[j].submodule->ident_size; i++) {
5589 if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005590
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005591 if (!ident) {
5592 *ret = &module->inc[j].submodule->ident[i];
5593 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005594 }
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005595
5596 base = &module->inc[j].submodule->ident[i];
5597 goto matchfound;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005598 }
5599 }
5600 }
5601
Radek Krejcif2ac7ae2016-02-19 13:38:07 +01005602matchfound:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005603 /* we found it somewhere */
Radek Krejcibabbff82016-02-19 13:31:37 +01005604 if (base) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005605 /* is it already completely resolved? */
5606 for (i = 0; i < unres->count; i++) {
Radek Krejciba7b1cc2016-08-08 13:44:43 +02005607 if ((unres->item[i] == base) && (unres->type[i] == UNRES_IDENT)) {
Radek Krejci018f1f52016-08-03 16:01:20 +02005608 /* identity found, but not yet resolved, so do not return it in *res and try it again later */
5609
5610 /* simple check for circular reference,
5611 * the complete check is done as a side effect of using only completely
5612 * resolved identities (previous check of unres content) */
5613 if (ly_strequal((const char *)unres->str_snode[i], ident->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005614 LOGVAL(ctx, LYE_INARG, LY_VLOG_NONE, NULL, basename, "base");
5615 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename);
Radek Krejci219fa612016-08-15 10:36:51 +02005616 return -1;
Radek Krejci018f1f52016-08-03 16:01:20 +02005617 }
5618
Radek Krejci06f64ed2016-08-15 11:07:44 +02005619 return EXIT_FAILURE;
Radek Krejcibabbff82016-02-19 13:31:37 +01005620 }
5621 }
Radek Krejci018f1f52016-08-03 16:01:20 +02005622
Radek Krejcibabbff82016-02-19 13:31:37 +01005623 /* checks done, store the result */
Radek Krejci018f1f52016-08-03 16:01:20 +02005624 *ret = base;
Pavol Vicanfdab9f92016-09-07 15:23:27 +02005625 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005626 }
5627
Radek Krejci219fa612016-08-15 10:36:51 +02005628 /* base not found (maybe a forward reference) */
5629 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005630}
5631
Michal Vasko730dfdf2015-08-11 14:48:05 +02005632/**
5633 * @brief Resolve base identity. Logs directly.
5634 *
5635 * @param[in] module Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02005636 * @param[in] ident Identity to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005637 * @param[in] basename Base name of the identity.
Radek Krejcibabbff82016-02-19 13:31:37 +01005638 * @param[in] parent Either "type" or "identity".
Radek Krejcicf509982015-12-15 09:22:44 +01005639 * @param[in,out] type Type structure where we want to resolve identity. Can be NULL.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005640 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005641 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005642 */
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005643static int
Michal Vaskof2d43962016-09-02 11:10:16 +02005644resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char *parent,
Radek Krejci018f1f52016-08-03 16:01:20 +02005645 struct lys_type *type, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005646{
5647 const char *name;
Radek Krejci219fa612016-08-15 10:36:51 +02005648 int mod_name_len = 0, rc;
Radek Krejcicf509982015-12-15 09:22:44 +01005649 struct lys_ident *target, **ret;
Radek Krejci4372b4e2016-04-14 17:42:16 +02005650 uint16_t flags;
Radek Krejcicf509982015-12-15 09:22:44 +01005651 struct lys_module *mod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005652 struct ly_ctx *ctx = module->ctx;
Radek Krejcicf509982015-12-15 09:22:44 +01005653
5654 assert((ident && !type) || (!ident && type));
5655
5656 if (!type) {
5657 /* have ident to resolve */
5658 ret = &target;
5659 flags = ident->flags;
5660 mod = ident->module;
5661 } else {
5662 /* have type to fill */
Michal Vaskof2d43962016-09-02 11:10:16 +02005663 ++type->info.ident.count;
5664 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 +01005665 LY_CHECK_ERR_RETURN(!type->info.ident.ref, LOGMEM(ctx), -1);
Michal Vaskof2d43962016-09-02 11:10:16 +02005666
5667 ret = &type->info.ident.ref[type->info.ident.count - 1];
Radek Krejcicf509982015-12-15 09:22:44 +01005668 flags = type->parent->flags;
5669 mod = type->parent->module;
5670 }
Michal Vaskof2006002016-04-21 16:28:15 +02005671 *ret = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005672
5673 /* search for the base identity */
5674 name = strchr(basename, ':');
5675 if (name) {
5676 /* set name to correct position after colon */
Michal Vasko2d851a92015-10-20 16:16:36 +02005677 mod_name_len = name - basename;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005678 name++;
5679
Michal Vasko2d851a92015-10-20 16:16:36 +02005680 if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005681 /* prefix refers to the current module, ignore it */
Michal Vasko2d851a92015-10-20 16:16:36 +02005682 mod_name_len = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005683 }
5684 } else {
5685 name = basename;
5686 }
5687
Radek Krejcic071c542016-01-27 14:57:51 +01005688 /* get module where to search */
Michal Vasko921eb6b2017-10-13 10:01:39 +02005689 module = lyp_get_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01005690 if (!module) {
5691 /* identity refers unknown data model */
Michal Vasko53b7da02018-02-13 15:28:42 +01005692 LOGVAL(ctx, LYE_INMOD, LY_VLOG_NONE, NULL, basename);
Radek Krejcic071c542016-01-27 14:57:51 +01005693 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005694 }
5695
Radek Krejcic071c542016-01-27 14:57:51 +01005696 /* search in the identified module ... */
Radek Krejci219fa612016-08-15 10:36:51 +02005697 rc = resolve_base_ident_sub(module, ident, name, unres, ret);
5698 if (!rc) {
5699 assert(*ret);
5700
5701 /* check status */
5702 if (lyp_check_status(flags, mod, ident ? ident->name : "of type",
5703 (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) {
5704 rc = -1;
Radek Krejci83a4bac2017-02-07 15:53:04 +01005705 } else if (ident) {
5706 ident->base[ident->base_size++] = *ret;
Radek Krejci9e6af732017-04-27 14:40:25 +02005707 if (lys_main_module(mod)->implemented) {
5708 /* in case of the implemented identity, maintain backlinks to it
5709 * from the base identities to make it available when resolving
5710 * data with the identity values (not implemented identity is not
5711 * allowed as an identityref value). */
5712 resolve_identity_backlink_update(ident, *ret);
5713 }
Radek Krejci219fa612016-08-15 10:36:51 +02005714 }
5715 } else if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005716 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename);
Pavol Vican053a2882016-09-05 14:40:33 +02005717 if (type) {
5718 --type->info.ident.count;
5719 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005720 }
5721
Radek Krejci219fa612016-08-15 10:36:51 +02005722 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005723}
5724
Radek Krejci9e6af732017-04-27 14:40:25 +02005725/*
5726 * 1 - true (der is derived from base)
5727 * 0 - false (der is not derived from base)
5728 */
5729static int
5730search_base_identity(struct lys_ident *der, struct lys_ident *base)
5731{
5732 int i;
5733
5734 if (der == base) {
5735 return 1;
5736 } else {
5737 for(i = 0; i < der->base_size; i++) {
5738 if (search_base_identity(der->base[i], base) == 1) {
5739 return 1;
5740 }
5741 }
5742 }
5743
5744 return 0;
5745}
5746
Michal Vasko730dfdf2015-08-11 14:48:05 +02005747/**
Michal Vaskof39142b2015-10-21 11:40:05 +02005748 * @brief Resolve JSON data format identityref. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005749 *
Michal Vaskof2d43962016-09-02 11:10:16 +02005750 * @param[in] type Identityref type.
Michal Vaskofb0873c2015-08-21 09:00:07 +02005751 * @param[in] ident_name Identityref name.
Radek Krejciadb57612016-02-16 13:34:34 +01005752 * @param[in] node Node where the identityref is being resolved
Radek Krejci9e6af732017-04-27 14:40:25 +02005753 * @param[in] dflt flag if we are resolving default value in the schema
Michal Vasko730dfdf2015-08-11 14:48:05 +02005754 *
5755 * @return Pointer to the identity resolvent, NULL on error.
5756 */
Radek Krejcia52656e2015-08-05 13:41:50 +02005757struct lys_ident *
Radek Krejci9e6af732017-04-27 14:40:25 +02005758resolve_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 +02005759{
Radek Krejci9e6af732017-04-27 14:40:25 +02005760 const char *mod_name, *name;
Michal Vasko08767f72017-10-06 14:38:08 +02005761 char *str;
Radek Krejcidce5f972017-09-12 15:47:49 +02005762 int mod_name_len, nam_len, rc;
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005763 int need_implemented = 0;
Michal Vasko08767f72017-10-06 14:38:08 +02005764 unsigned int i, j;
Michal Vaskof2d43962016-09-02 11:10:16 +02005765 struct lys_ident *der, *cur;
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005766 struct lys_module *imod = NULL, *m, *tmod;
Michal Vasko53b7da02018-02-13 15:28:42 +01005767 struct ly_ctx *ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005768
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005769 assert(type && ident_name && mod);
Michal Vasko53b7da02018-02-13 15:28:42 +01005770 ctx = mod->ctx;
Radek Krejcibb1ce0f2016-12-05 13:24:33 +01005771
Michal Vaskof2d43962016-09-02 11:10:16 +02005772 if (!type || (!type->info.ident.count && !type->der) || !ident_name) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005773 return NULL;
5774 }
5775
Michal Vasko50576712017-07-28 12:28:33 +02005776 rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, &nam_len, NULL, 0);
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005777 if (rc < 1) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005778 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 +02005779 return NULL;
Michal Vaskob43bb3c2016-03-17 15:00:27 +01005780 } else if (rc < (signed)strlen(ident_name)) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005781 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 +02005782 return NULL;
5783 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005784
5785 m = lys_main_module(mod); /* shortcut */
5786 if (!mod_name || (!strncmp(mod_name, m->name, mod_name_len) && !m->name[mod_name_len])) {
5787 /* identity is defined in the same module as node */
5788 imod = m;
5789 } else if (dflt) {
5790 /* solving identityref in default definition in schema -
5791 * find the identity's module in the imported modules list to have a correct revision */
5792 for (i = 0; i < mod->imp_size; i++) {
5793 if (!strncmp(mod_name, mod->imp[i].module->name, mod_name_len) && !mod->imp[i].module->name[mod_name_len]) {
5794 imod = mod->imp[i].module;
5795 break;
5796 }
5797 }
Andrew Langefeld8f50a2d2018-05-23 17:16:12 -05005798
5799 /* We may need to pull it from the module that the typedef came from */
5800 if (!imod && type && type->der) {
5801 tmod = type->der->module;
5802 for (i = 0; i < tmod->imp_size; i++) {
5803 if (!strncmp(mod_name, tmod->imp[i].module->name, mod_name_len) && !tmod->imp[i].module->name[mod_name_len]) {
5804 imod = tmod->imp[i].module;
5805 break;
5806 }
5807 }
5808 }
Radek Krejci9e6af732017-04-27 14:40:25 +02005809 } else {
Michal Vasko08767f72017-10-06 14:38:08 +02005810 /* solving identityref in data - get the module from the context */
5811 for (i = 0; i < (unsigned)mod->ctx->models.used; ++i) {
5812 imod = mod->ctx->models.list[i];
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005813 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
Radek Krejci9e6af732017-04-27 14:40:25 +02005814 break;
5815 }
Michal Vasko08767f72017-10-06 14:38:08 +02005816 imod = NULL;
5817 }
Radek Krejci5ba05102017-10-26 15:02:52 +02005818 if (!imod && mod->ctx->models.parsing_sub_modules_count) {
5819 /* we are currently parsing some module and checking XPath or a default value,
5820 * so take this module into account */
5821 for (i = 0; i < mod->ctx->models.parsing_sub_modules_count; i++) {
5822 imod = mod->ctx->models.parsing_sub_modules[i];
5823 if (imod->type) {
5824 /* skip submodules */
5825 continue;
5826 }
5827 if (!strncmp(mod_name, imod->name, mod_name_len) && !imod->name[mod_name_len]) {
5828 break;
5829 }
5830 imod = NULL;
5831 }
5832 }
Michal Vasko08767f72017-10-06 14:38:08 +02005833 }
5834
Michal Vasko53b7da02018-02-13 15:28:42 +01005835 if (!dflt && (!imod || !imod->implemented) && ctx->data_clb) {
Michal Vasko08767f72017-10-06 14:38:08 +02005836 /* the needed module was not found, but it may have been expected so call the data callback */
5837 if (imod) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005838 ctx->data_clb(ctx, imod->name, imod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci58523dc2017-10-27 10:00:17 +02005839 } else if (mod_name) {
Michal Vasko08767f72017-10-06 14:38:08 +02005840 str = strndup(mod_name, mod_name_len);
Michal Vasko53b7da02018-02-13 15:28:42 +01005841 imod = (struct lys_module *)ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
Michal Vasko08767f72017-10-06 14:38:08 +02005842 free(str);
Radek Krejci9e6af732017-04-27 14:40:25 +02005843 }
5844 }
5845 if (!imod) {
5846 goto fail;
5847 }
5848
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005849 if (m != imod || lys_main_module(type->parent->module) != mod) {
Michal Vasko08767f72017-10-06 14:38:08 +02005850 /* the type is not referencing the same schema,
Radek Krejci9e6af732017-04-27 14:40:25 +02005851 * THEN, we may need to make the module with the identity implemented, but only if it really
5852 * contains the identity */
5853 if (!imod->implemented) {
5854 cur = NULL;
5855 /* get the identity in the module */
5856 for (i = 0; i < imod->ident_size; i++) {
5857 if (!strcmp(name, imod->ident[i].name)) {
5858 cur = &imod->ident[i];
5859 break;
5860 }
5861 }
5862 if (!cur) {
5863 /* go through includes */
5864 for (j = 0; j < imod->inc_size; j++) {
5865 for (i = 0; i < imod->inc[j].submodule->ident_size; i++) {
5866 if (!strcmp(name, imod->inc[j].submodule->ident[i].name)) {
5867 cur = &imod->inc[j].submodule->ident[i];
5868 break;
5869 }
5870 }
5871 }
5872 if (!cur) {
5873 goto fail;
5874 }
5875 }
5876
5877 /* check that identity is derived from one of the type's base */
5878 while (type->der) {
5879 for (i = 0; i < type->info.ident.count; i++) {
5880 if (search_base_identity(cur, type->info.ident.ref[i])) {
5881 /* cur's base matches the type's base */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005882 need_implemented = 1;
Radek Krejci9e6af732017-04-27 14:40:25 +02005883 goto match;
5884 }
5885 }
5886 type = &type->der->type;
5887 }
5888 /* matching base not found */
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005889 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 +02005890 goto fail;
5891 }
Radek Krejcif32c5f62016-12-05 09:27:38 +01005892 }
Michal Vaskofb0873c2015-08-21 09:00:07 +02005893
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005894 /* go through all the derived types of all the bases */
Michal Vaskof2d43962016-09-02 11:10:16 +02005895 while (type->der) {
5896 for (i = 0; i < type->info.ident.count; ++i) {
5897 cur = type->info.ident.ref[i];
Michal Vaskofb0873c2015-08-21 09:00:07 +02005898
Radek Krejci85a54be2016-10-20 12:39:56 +02005899 if (cur->der) {
Radek Krejci98a1e2d2017-04-26 14:34:52 +02005900 /* there are some derived identities */
Michal Vasko08767f72017-10-06 14:38:08 +02005901 for (j = 0; j < cur->der->number; j++) {
5902 der = (struct lys_ident *)cur->der->set.g[j]; /* shortcut */
Radek Krejci9e6af732017-04-27 14:40:25 +02005903 if (!strcmp(der->name, name) && lys_main_module(der->module) == imod) {
Radek Krejci85a54be2016-10-20 12:39:56 +02005904 /* we have match */
5905 cur = der;
5906 goto match;
5907 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005908 }
5909 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005910 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005911 type = &type->der->type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005912 }
5913
Radek Krejci9e6af732017-04-27 14:40:25 +02005914fail:
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005915 LOGVAL(ctx, LYE_INRESOLV, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, "identityref", ident_name);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005916 return NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +02005917
5918match:
Michal Vaskof2d43962016-09-02 11:10:16 +02005919 for (i = 0; i < cur->iffeature_size; i++) {
5920 if (!resolve_iffeature(&cur->iffeature[i])) {
Michal Vaskoae4b7d32018-07-13 12:21:01 +02005921 if (node) {
5922 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, cur->name, node->schema->name);
5923 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005924 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 +02005925 return NULL;
5926 }
5927 }
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005928 if (need_implemented) {
5929 if (dflt) {
Michal Vasko0f437062018-06-08 15:52:39 +02005930 /* later try to make the module implemented */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005931 LOGVRB("Making \"%s\" module implemented because of identityref default value \"%s\" used in the implemented \"%s\" module",
5932 imod->name, cur->name, mod->name);
Michal Vasko0f437062018-06-08 15:52:39 +02005933 /* to be more effective we should use UNRES_MOD_IMPLEMENT but that would require changing prototype of
5934 * several functions with little gain */
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005935 if (lys_set_implemented(imod)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005936 LOGERR(ctx, ly_errno, "Setting the module \"%s\" implemented because of used default identity \"%s\" failed.",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005937 imod->name, cur->name);
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005938 goto fail;
5939 }
5940 } else {
5941 /* just say that it was found, but in a non-implemented module */
Michal Vasko53b7da02018-02-13 15:28:42 +01005942 LOGVAL(ctx, LYE_SPEC, LY_VLOG_NONE, NULL, "Identity found, but in a non-implemented module \"%s\".",
Michal Vasko3f3c6a82017-10-03 10:23:23 +02005943 lys_main_module(cur->module)->name);
Radek Krejci9e6af732017-04-27 14:40:25 +02005944 goto fail;
5945 }
5946 }
Michal Vaskof2d43962016-09-02 11:10:16 +02005947 return cur;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005948}
5949
Michal Vasko730dfdf2015-08-11 14:48:05 +02005950/**
Michal Vaskobb211122015-08-19 14:03:11 +02005951 * @brief Resolve unresolved uses. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005952 *
Michal Vaskobb211122015-08-19 14:03:11 +02005953 * @param[in] uses Uses to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005954 * @param[in] unres Specific unres item.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005955 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005956 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02005957 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005958static int
Radek Krejci48464ed2016-03-17 15:44:09 +01005959resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005960{
Radek Krejci93def382017-05-24 15:33:48 +02005961 int rc;
Radek Krejci010e54b2016-03-15 09:40:34 +01005962 struct lys_node *par_grp;
Michal Vasko53b7da02018-02-13 15:28:42 +01005963 struct ly_ctx *ctx = uses->module->ctx;
Michal Vaskoe91afce2015-08-12 12:21:00 +02005964
Radek Krejci6ff885d2017-01-03 14:06:22 +01005965 /* 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 +02005966 * in some uses. When we see such a uses, the grouping's unres counter is used to store number of so far
5967 * unresolved uses. The grouping cannot be used unless this counter is decreased back to 0. To remember
5968 * that the uses already increased grouping's counter, the LYS_USESGRP flag is used. */
Michal Vaskodcf98e62016-05-05 17:53:53 +02005969 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 +02005970 if (par_grp && ly_strequal(par_grp->name, uses->name, 1)) {
5971 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
5972 return -1;
5973 }
Michal Vaskoe91afce2015-08-12 12:21:00 +02005974
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005975 if (!uses->grp) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01005976 rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp);
5977 if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005978 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005979 return -1;
5980 } else if (rc > 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005981 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005982 return -1;
5983 } else if (!uses->grp) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005984 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005985 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005986 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02005987 return -1;
5988 }
Radek Krejci010e54b2016-03-15 09:40:34 +01005989 uses->flags |= LYS_USESGRP;
Michal Vasko407f1bb2015-09-23 15:51:07 +02005990 }
Michal Vasko53b7da02018-02-13 15:28:42 +01005991 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3edeaf72016-02-11 13:17:43 +01005992 return EXIT_FAILURE;
Michal Vasko12e30842015-08-04 11:54:00 +02005993 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005994 }
5995
Radek Krejci93def382017-05-24 15:33:48 +02005996 if (uses->grp->unres_count) {
Radek Krejci010e54b2016-03-15 09:40:34 +01005997 if (par_grp && !(uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02005998 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01005999 LOGERR(ctx, LY_EINT, "Too many unresolved items (uses) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006000 return -1;
6001 }
Radek Krejci010e54b2016-03-15 09:40:34 +01006002 uses->flags |= LYS_USESGRP;
Radek Krejcie00d2312016-08-12 15:27:49 +02006003 } else {
6004 /* instantiate grouping only when it is completely resolved */
6005 uses->grp = NULL;
Michal Vasko407f1bb2015-09-23 15:51:07 +02006006 }
Michal Vaskoa9792722018-06-28 09:01:02 +02006007 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, uses, "uses", uses->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006008 return EXIT_FAILURE;
6009 }
6010
Radek Krejci48464ed2016-03-17 15:44:09 +01006011 rc = resolve_uses(uses, unres);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006012 if (!rc) {
6013 /* decrease unres count only if not first try */
Radek Krejci010e54b2016-03-15 09:40:34 +01006014 if (par_grp && (uses->flags & LYS_USESGRP)) {
Radek Krejci93def382017-05-24 15:33:48 +02006015 assert(((struct lys_node_grp *)par_grp)->unres_count);
6016 ((struct lys_node_grp *)par_grp)->unres_count--;
Radek Krejci010e54b2016-03-15 09:40:34 +01006017 uses->flags &= ~LYS_USESGRP;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006018 }
Radek Krejcicf509982015-12-15 09:22:44 +01006019
6020 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01006021 if (lyp_check_status(uses->flags, uses->module, "of uses",
Radek Krejciadb57612016-02-16 13:34:34 +01006022 uses->grp->flags, uses->grp->module, uses->grp->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006023 (struct lys_node *)uses)) {
Radek Krejcicf509982015-12-15 09:22:44 +01006024 return -1;
6025 }
6026
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006027 return EXIT_SUCCESS;
6028 }
6029
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006030 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006031}
6032
Michal Vasko730dfdf2015-08-11 14:48:05 +02006033/**
Michal Vasko9957e592015-08-17 15:04:09 +02006034 * @brief Resolve list keys. Logs directly.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006035 *
Michal Vaskobb211122015-08-19 14:03:11 +02006036 * @param[in] list List to use.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006037 * @param[in] keys_str Keys node value.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006038 *
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006039 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
Michal Vasko730dfdf2015-08-11 14:48:05 +02006040 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006041static int
Radek Krejci48464ed2016-03-17 15:44:09 +01006042resolve_list_keys(struct lys_node_list *list, const char *keys_str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006043{
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006044 int i, len, rc;
Michal Vasko4f0dad02016-02-15 14:08:23 +01006045 const char *value;
Radek Krejcia98048c2017-05-24 16:35:48 +02006046 char *s = NULL;
Michal Vasko53b7da02018-02-13 15:28:42 +01006047 struct ly_ctx *ctx = list->module->ctx;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006048
6049 for (i = 0; i < list->keys_size; ++i) {
Radek Krejcidea17dd2017-06-02 15:20:43 +02006050 assert(keys_str);
6051
Radek Krejci5c08a992016-11-02 13:30:04 +01006052 if (!list->child) {
6053 /* no child, possible forward reference */
Michal Vasko53b7da02018-02-13 15:28:42 +01006054 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str);
Radek Krejci5c08a992016-11-02 13:30:04 +01006055 return EXIT_FAILURE;
6056 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006057 /* get the key name */
6058 if ((value = strpbrk(keys_str, " \t\n"))) {
6059 len = value - keys_str;
6060 while (isspace(value[0])) {
6061 value++;
6062 }
6063 } else {
6064 len = strlen(keys_str);
6065 }
6066
Michal Vaskobb520442017-05-23 10:55:18 +02006067 rc = lys_getnext_data(lys_node_module((struct lys_node *)list), (struct lys_node *)list, keys_str, len, LYS_LEAF,
6068 (const struct lys_node **)&list->keys[i]);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006069 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006070 LOGVAL(ctx, LYE_INRESOLV, LY_VLOG_LYS, list, "list key", keys_str);
Michal Vasko7a55bea2016-05-02 14:51:20 +02006071 return EXIT_FAILURE;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006072 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006073
Radek Krejci48464ed2016-03-17 15:44:09 +01006074 if (check_key(list, i, keys_str, len)) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006075 /* check_key logs */
6076 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006077 }
6078
Radek Krejcicf509982015-12-15 09:22:44 +01006079 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +01006080 if (lyp_check_status(list->flags, list->module, list->name,
Radek Krejci48464ed2016-03-17 15:44:09 +01006081 list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name,
6082 (struct lys_node *)list->keys[i])) {
Radek Krejcicf509982015-12-15 09:22:44 +01006083 return -1;
6084 }
6085
Radek Krejcia98048c2017-05-24 16:35:48 +02006086 /* default value - is ignored, keep it but print a warning */
6087 if (list->keys[i]->dflt) {
6088 /* log is not hidden only in case this resolving fails and in such a case
6089 * we cannot get here
6090 */
Michal Vasko53b7da02018-02-13 15:28:42 +01006091 assert(log_opt == ILO_STORE);
6092 log_opt = ILO_LOG;
6093 LOGWRN(ctx, "Default value \"%s\" in the list key \"%s\" is ignored. (%s)", list->keys[i]->dflt,
Michal Vasko395b0a02018-01-22 09:36:20 +01006094 list->keys[i]->name, s = lys_path((struct lys_node*)list, LYS_PATH_FIRST_PREFIX));
Michal Vasko53b7da02018-02-13 15:28:42 +01006095 log_opt = ILO_STORE;
Radek Krejcia98048c2017-05-24 16:35:48 +02006096 free(s);
6097 }
6098
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006099 /* prepare for next iteration */
6100 while (value && isspace(value[0])) {
6101 value++;
6102 }
6103 keys_str = value;
6104 }
6105
Michal Vaskof02e3742015-08-05 16:27:02 +02006106 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006107}
6108
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006109/**
Michal Vaskobf19d252015-10-08 15:39:17 +02006110 * @brief Resolve (check) all must conditions of \p node.
6111 * Logs directly.
6112 *
6113 * @param[in] node Data node with optional must statements.
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006114 * @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 +02006115 *
6116 * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error.
6117 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006118static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006119resolve_must(struct lyd_node *node, int inout_parent, int ignore_fail)
Michal Vaskof02e3742015-08-05 16:27:02 +02006120{
Michal Vaskobf19d252015-10-08 15:39:17 +02006121 uint8_t i, must_size;
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006122 struct lys_node *schema;
Michal Vaskobf19d252015-10-08 15:39:17 +02006123 struct lys_restr *must;
6124 struct lyxp_set set;
Michal Vasko53b7da02018-02-13 15:28:42 +01006125 struct ly_ctx *ctx = node->schema->module->ctx;
Michal Vaskobf19d252015-10-08 15:39:17 +02006126
6127 assert(node);
6128 memset(&set, 0, sizeof set);
6129
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006130 if (inout_parent) {
6131 for (schema = lys_parent(node->schema);
6132 schema && (schema->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES));
6133 schema = lys_parent(schema));
6134 if (!schema || !(schema->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006135 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006136 return -1;
6137 }
6138 must_size = ((struct lys_node_inout *)schema)->must_size;
6139 must = ((struct lys_node_inout *)schema)->must;
6140
6141 /* context node is the RPC/action */
6142 node = node->parent;
6143 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006144 LOGINT(ctx);
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006145 return -1;
6146 }
6147 } else {
6148 switch (node->schema->nodetype) {
6149 case LYS_CONTAINER:
6150 must_size = ((struct lys_node_container *)node->schema)->must_size;
6151 must = ((struct lys_node_container *)node->schema)->must;
6152 break;
6153 case LYS_LEAF:
6154 must_size = ((struct lys_node_leaf *)node->schema)->must_size;
6155 must = ((struct lys_node_leaf *)node->schema)->must;
6156 break;
6157 case LYS_LEAFLIST:
6158 must_size = ((struct lys_node_leaflist *)node->schema)->must_size;
6159 must = ((struct lys_node_leaflist *)node->schema)->must;
6160 break;
6161 case LYS_LIST:
6162 must_size = ((struct lys_node_list *)node->schema)->must_size;
6163 must = ((struct lys_node_list *)node->schema)->must;
6164 break;
6165 case LYS_ANYXML:
6166 case LYS_ANYDATA:
6167 must_size = ((struct lys_node_anydata *)node->schema)->must_size;
6168 must = ((struct lys_node_anydata *)node->schema)->must;
6169 break;
6170 case LYS_NOTIF:
6171 must_size = ((struct lys_node_notif *)node->schema)->must_size;
6172 must = ((struct lys_node_notif *)node->schema)->must;
6173 break;
6174 default:
6175 must_size = 0;
6176 break;
6177 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006178 }
6179
6180 for (i = 0; i < must_size; ++i) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006181 if (lyxp_eval(must[i].expr, node, LYXP_NODE_ELEM, lyd_node_module(node), &set, LYXP_MUST)) {
Michal Vaskobf19d252015-10-08 15:39:17 +02006182 return -1;
6183 }
6184
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006185 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_MUST);
Michal Vaskobf19d252015-10-08 15:39:17 +02006186
Michal Vasko8146d4c2016-05-09 15:50:29 +02006187 if (!set.val.bool) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01006188 if ((ignore_fail == 1) || ((must[i].flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006189 LOGVRB("Must condition \"%s\" not satisfied, but it is not required.", must[i].expr);
6190 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006191 LOGVAL(ctx, LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006192 if (must[i].emsg) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006193 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, must[i].emsg);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006194 }
6195 if (must[i].eapptag) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006196 ly_err_last_set_apptag(ctx, must[i].eapptag);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006197 }
6198 return 1;
Michal Vasko6ac68282016-04-11 10:56:47 +02006199 }
Michal Vaskobf19d252015-10-08 15:39:17 +02006200 }
6201 }
6202
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006203 return EXIT_SUCCESS;
Michal Vaskof02e3742015-08-05 16:27:02 +02006204}
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006205
Michal Vaskobf19d252015-10-08 15:39:17 +02006206/**
Michal Vasko508a50d2016-09-07 14:50:33 +02006207 * @brief Resolve (find) when condition schema context node. Does not log.
6208 *
6209 * @param[in] schema Schema node with the when condition.
6210 * @param[out] ctx_snode When schema context node.
6211 * @param[out] ctx_snode_type Schema context node type.
6212 */
6213void
6214resolve_when_ctx_snode(const struct lys_node *schema, struct lys_node **ctx_snode, enum lyxp_node_type *ctx_snode_type)
6215{
6216 const struct lys_node *sparent;
6217
6218 /* find a not schema-only node */
6219 *ctx_snode_type = LYXP_NODE_ELEM;
6220 while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) {
6221 if (schema->nodetype == LYS_AUGMENT) {
6222 sparent = ((struct lys_node_augment *)schema)->target;
6223 } else {
6224 sparent = schema->parent;
6225 }
6226 if (!sparent) {
6227 /* context node is the document root (fake root in our case) */
6228 if (schema->flags & LYS_CONFIG_W) {
6229 *ctx_snode_type = LYXP_NODE_ROOT_CONFIG;
6230 } else {
Michal Vaskob94a5e42016-09-08 14:01:56 +02006231 *ctx_snode_type = LYXP_NODE_ROOT;
Michal Vasko508a50d2016-09-07 14:50:33 +02006232 }
Michal Vasko2d2aec12016-09-08 11:42:50 +02006233 /* we need the first top-level sibling, but no uses or groupings */
Michal Vaskocb45f472018-02-12 10:47:42 +01006234 schema = lys_getnext(NULL, NULL, lys_node_module(schema), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko508a50d2016-09-07 14:50:33 +02006235 break;
6236 }
6237 schema = sparent;
6238 }
6239
6240 *ctx_snode = (struct lys_node *)schema;
6241}
6242
6243/**
Michal Vaskocf024702015-10-08 15:01:42 +02006244 * @brief Resolve (find) when condition context node. Does not log.
6245 *
6246 * @param[in] node Data node, whose conditional definition is being decided.
Michal Vasko508a50d2016-09-07 14:50:33 +02006247 * @param[in] schema Schema node with the when condition.
Michal Vaskoa59495d2016-08-22 09:18:58 +02006248 * @param[out] ctx_node Context node.
6249 * @param[out] ctx_node_type Context node type.
Michal Vaskocf024702015-10-08 15:01:42 +02006250 *
Michal Vaskoa59495d2016-08-22 09:18:58 +02006251 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +02006252 */
Michal Vaskoa59495d2016-08-22 09:18:58 +02006253static int
6254resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema, struct lyd_node **ctx_node,
6255 enum lyxp_node_type *ctx_node_type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006256{
Michal Vaskocf024702015-10-08 15:01:42 +02006257 struct lyd_node *parent;
6258 struct lys_node *sparent;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006259 enum lyxp_node_type node_type;
Michal Vaskocf024702015-10-08 15:01:42 +02006260 uint16_t i, data_depth, schema_depth;
6261
Michal Vasko508a50d2016-09-07 14:50:33 +02006262 resolve_when_ctx_snode(schema, &schema, &node_type);
Michal Vaskocf024702015-10-08 15:01:42 +02006263
Michal Vaskofe989752016-09-08 08:47:26 +02006264 if (node_type == LYXP_NODE_ELEM) {
6265 /* standard element context node */
6266 for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth);
6267 for (sparent = schema, schema_depth = 0;
6268 sparent;
6269 sparent = (sparent->nodetype == LYS_AUGMENT ? ((struct lys_node_augment *)sparent)->target : sparent->parent)) {
6270 if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_NOTIF | LYS_RPC)) {
6271 ++schema_depth;
6272 }
Michal Vaskocf024702015-10-08 15:01:42 +02006273 }
Michal Vaskofe989752016-09-08 08:47:26 +02006274 if (data_depth < schema_depth) {
6275 return -1;
6276 }
Michal Vaskocf024702015-10-08 15:01:42 +02006277
Michal Vasko956e8542016-08-26 09:43:35 +02006278 /* find the corresponding data node */
6279 for (i = 0; i < data_depth - schema_depth; ++i) {
6280 node = node->parent;
6281 }
Michal Vaskoa59495d2016-08-22 09:18:58 +02006282 if (node->schema != schema) {
6283 return -1;
6284 }
Michal Vaskofe989752016-09-08 08:47:26 +02006285 } else {
6286 /* root context node */
6287 while (node->parent) {
6288 node = node->parent;
6289 }
6290 while (node->prev->next) {
6291 node = node->prev;
6292 }
Michal Vaskocf024702015-10-08 15:01:42 +02006293 }
6294
Michal Vaskoa59495d2016-08-22 09:18:58 +02006295 *ctx_node = node;
6296 *ctx_node_type = node_type;
6297 return EXIT_SUCCESS;
Michal Vaskocf024702015-10-08 15:01:42 +02006298}
6299
Michal Vasko76c3bd32016-08-24 16:02:52 +02006300/**
6301 * @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 +01006302 * The context node is adjusted if needed.
Michal Vasko76c3bd32016-08-24 16:02:52 +02006303 *
6304 * @param[in] snode Schema node, whose children instances need to be unlinked.
6305 * @param[in,out] node Data siblings where to look for the children of \p snode. If it is unlinked,
6306 * it is moved to point to another sibling still in the original tree.
6307 * @param[in,out] ctx_node When context node, adjusted if needed.
6308 * @param[in] ctx_node_type Context node type, just for information to detect invalid situations.
6309 * @param[out] unlinked_nodes Unlinked siblings. Can be safely appended to \p node afterwards.
6310 * Ordering may change, but there will be no semantic change.
6311 *
6312 * @return EXIT_SUCCESS on success, -1 on error.
6313 */
6314static int
6315resolve_when_unlink_nodes(struct lys_node *snode, struct lyd_node **node, struct lyd_node **ctx_node,
6316 enum lyxp_node_type ctx_node_type, struct lyd_node **unlinked_nodes)
6317{
6318 struct lyd_node *next, *elem;
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006319 const struct lys_node *slast;
Michal Vasko53b7da02018-02-13 15:28:42 +01006320 struct ly_ctx *ctx = snode->module->ctx;
Michal Vasko76c3bd32016-08-24 16:02:52 +02006321
6322 switch (snode->nodetype) {
6323 case LYS_AUGMENT:
6324 case LYS_USES:
6325 case LYS_CHOICE:
6326 case LYS_CASE:
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006327 slast = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +01006328 while ((slast = lys_getnext(slast, snode, NULL, LYS_GETNEXT_PARENTUSES))) {
Michal Vaskoeb81fb72017-02-06 12:11:08 +01006329 if (slast->nodetype & (LYS_ACTION | LYS_NOTIF)) {
6330 continue;
6331 }
6332
6333 if (resolve_when_unlink_nodes((struct lys_node *)slast, node, ctx_node, ctx_node_type, unlinked_nodes)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006334 return -1;
6335 }
6336 }
6337 break;
6338 case LYS_CONTAINER:
6339 case LYS_LIST:
6340 case LYS_LEAF:
6341 case LYS_LEAFLIST:
6342 case LYS_ANYXML:
6343 case LYS_ANYDATA:
6344 LY_TREE_FOR_SAFE(lyd_first_sibling(*node), next, elem) {
6345 if (elem->schema == snode) {
6346
6347 if (elem == *ctx_node) {
6348 /* We are going to unlink our context node! This normally cannot happen,
6349 * but we use normal top-level data nodes for faking a document root node,
6350 * so if this is the context node, we just use the next top-level node.
6351 * Additionally, it can even happen that there are no top-level data nodes left,
6352 * all were unlinked, so in this case we pass NULL as the context node/data tree,
6353 * lyxp_eval() can handle this special situation.
6354 */
6355 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006356 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006357 return -1;
6358 }
6359
6360 if (elem->prev == elem) {
6361 /* unlinking last top-level element, use an empty data tree */
6362 *ctx_node = NULL;
6363 } else {
6364 /* in this case just use the previous/last top-level data node */
6365 *ctx_node = elem->prev;
6366 }
6367 } else if (elem == *node) {
6368 /* We are going to unlink the currently processed node. This does not matter that
6369 * much, but we would lose access to the original data tree, so just move our
6370 * pointer somewhere still inside it.
6371 */
6372 if ((*node)->prev != *node) {
6373 *node = (*node)->prev;
6374 } else {
6375 /* the processed node with sibings were all unlinked, oh well */
6376 *node = NULL;
6377 }
6378 }
6379
6380 /* temporarily unlink the node */
Michal Vasko2bce30c2017-02-06 12:16:39 +01006381 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006382 if (*unlinked_nodes) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006383 if (lyd_insert_after((*unlinked_nodes)->prev, elem)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006384 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006385 return -1;
6386 }
6387 } else {
6388 *unlinked_nodes = elem;
6389 }
6390
6391 if (snode->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
6392 /* there can be only one instance */
6393 break;
6394 }
6395 }
6396 }
6397 break;
6398 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01006399 LOGINT(ctx);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006400 return -1;
6401 }
6402
6403 return EXIT_SUCCESS;
6404}
6405
6406/**
6407 * @brief Relink the unlinked nodes back.
6408 *
6409 * @param[in] node Data node to link the nodes back to. It can actually be the adjusted context node,
6410 * we simply need a sibling from the original data tree.
6411 * @param[in] unlinked_nodes Unlinked nodes to relink to \p node.
6412 * @param[in] ctx_node_type Context node type to distinguish between \p node being the parent
6413 * or the sibling of \p unlinked_nodes.
6414 *
6415 * @return EXIT_SUCCESS on success, -1 on error.
6416 */
6417static int
6418resolve_when_relink_nodes(struct lyd_node *node, struct lyd_node *unlinked_nodes, enum lyxp_node_type ctx_node_type)
6419{
6420 struct lyd_node *elem;
6421
6422 LY_TREE_FOR_SAFE(unlinked_nodes, unlinked_nodes, elem) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006423 lyd_unlink_internal(elem, 0);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006424 if (ctx_node_type == LYXP_NODE_ELEM) {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006425 if (lyd_insert_common(node, NULL, elem, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006426 return -1;
6427 }
6428 } else {
Michal Vasko2bce30c2017-02-06 12:16:39 +01006429 if (lyd_insert_nextto(node, elem, 0, 0)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006430 return -1;
6431 }
6432 }
6433 }
6434
6435 return EXIT_SUCCESS;
6436}
6437
Radek Krejci03b71f72016-03-16 11:10:09 +01006438int
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006439resolve_applies_must(const struct lyd_node *node)
Radek Krejci01696bf2016-03-18 13:19:36 +01006440{
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006441 int ret = 0;
6442 uint8_t must_size;
6443 struct lys_node *schema, *iter;
Radek Krejci46165822016-08-26 14:06:27 +02006444
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006445 assert(node);
6446
6447 schema = node->schema;
6448
6449 /* their own must */
Radek Krejci46165822016-08-26 14:06:27 +02006450 switch (schema->nodetype) {
Radek Krejci01696bf2016-03-18 13:19:36 +01006451 case LYS_CONTAINER:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006452 must_size = ((struct lys_node_container *)schema)->must_size;
6453 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006454 case LYS_LEAF:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006455 must_size = ((struct lys_node_leaf *)schema)->must_size;
6456 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006457 case LYS_LEAFLIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006458 must_size = ((struct lys_node_leaflist *)schema)->must_size;
6459 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006460 case LYS_LIST:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006461 must_size = ((struct lys_node_list *)schema)->must_size;
6462 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006463 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02006464 case LYS_ANYDATA:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006465 must_size = ((struct lys_node_anydata *)schema)->must_size;
6466 break;
6467 case LYS_NOTIF:
6468 must_size = ((struct lys_node_notif *)schema)->must_size;
6469 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006470 default:
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006471 must_size = 0;
6472 break;
Radek Krejci01696bf2016-03-18 13:19:36 +01006473 }
Michal Vaskoc8c810c2016-09-15 14:02:00 +02006474
6475 if (must_size) {
6476 ++ret;
6477 }
6478
6479 /* schema may be a direct data child of input/output with must (but it must be first, it needs to be evaluated only once) */
6480 if (!node->prev->next) {
6481 for (iter = lys_parent(schema); iter && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)); iter = lys_parent(iter));
6482 if (iter && (iter->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
6483 ret += 0x2;
6484 }
6485 }
6486
6487 return ret;
Radek Krejci01696bf2016-03-18 13:19:36 +01006488}
6489
6490int
Radek Krejci46165822016-08-26 14:06:27 +02006491resolve_applies_when(const struct lys_node *schema, int mode, const struct lys_node *stop)
Radek Krejci03b71f72016-03-16 11:10:09 +01006492{
Radek Krejci46165822016-08-26 14:06:27 +02006493 const struct lys_node *parent;
Radek Krejci03b71f72016-03-16 11:10:09 +01006494
Radek Krejci46165822016-08-26 14:06:27 +02006495 assert(schema);
Radek Krejci03b71f72016-03-16 11:10:09 +01006496
Radek Krejci46165822016-08-26 14:06:27 +02006497 if (!(schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)schema)->when)) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006498 return 1;
6499 }
6500
Radek Krejci46165822016-08-26 14:06:27 +02006501 parent = schema;
Radek Krejci03b71f72016-03-16 11:10:09 +01006502 goto check_augment;
6503
Radek Krejci46165822016-08-26 14:06:27 +02006504 while (parent) {
6505 /* stop conditions */
6506 if (!mode) {
6507 /* stop on node that can be instantiated in data tree */
6508 if (!(parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6509 break;
6510 }
6511 } else {
6512 /* stop on the specified node */
6513 if (parent == stop) {
6514 break;
6515 }
6516 }
6517
6518 if (((const struct lys_node_uses *)parent)->when) {
Radek Krejci03b71f72016-03-16 11:10:09 +01006519 return 1;
6520 }
6521check_augment:
6522
6523 if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) &&
Radek Krejci46165822016-08-26 14:06:27 +02006524 (((const struct lys_node_augment *)parent->parent)->when))) {
Michal Vaskoe3655562016-08-24 15:56:17 +02006525 return 1;
Radek Krejci03b71f72016-03-16 11:10:09 +01006526 }
6527 parent = lys_parent(parent);
6528 }
6529
6530 return 0;
6531}
6532
Michal Vaskocf024702015-10-08 15:01:42 +02006533/**
6534 * @brief Resolve (check) all when conditions relevant for \p node.
6535 * Logs directly.
6536 *
6537 * @param[in] node Data node, whose conditional reference, if such, is being decided.
Michal Vaskoe446b092017-08-11 10:58:09 +02006538 * @param[in] ignore_fail 1 if when does not have to be satisfied, 2 if it does not have to be satisfied
6539 * only when requiring external dependencies.
Michal Vaskocf024702015-10-08 15:01:42 +02006540 *
Radek Krejci03b71f72016-03-16 11:10:09 +01006541 * @return
6542 * -1 - error, ly_errno is set
Michal Vasko0b963112017-08-11 12:45:36 +02006543 * 0 - all "when" statements true
6544 * 0, ly_vecode = LYVE_NOWHEN - some "when" statement false, returned in failed_when
Radek Krejci03b71f72016-03-16 11:10:09 +01006545 * 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 +02006546 */
Radek Krejci46165822016-08-26 14:06:27 +02006547int
Michal Vasko0b963112017-08-11 12:45:36 +02006548resolve_when(struct lyd_node *node, int ignore_fail, struct lys_when **failed_when)
Michal Vaskocf024702015-10-08 15:01:42 +02006549{
Michal Vasko76c3bd32016-08-24 16:02:52 +02006550 struct lyd_node *ctx_node = NULL, *unlinked_nodes, *tmp_node;
Michal Vasko90fc2a32016-08-24 15:58:58 +02006551 struct lys_node *sparent;
Michal Vaskocf024702015-10-08 15:01:42 +02006552 struct lyxp_set set;
Michal Vaskoa59495d2016-08-22 09:18:58 +02006553 enum lyxp_node_type ctx_node_type;
Michal Vasko53b7da02018-02-13 15:28:42 +01006554 struct ly_ctx *ctx = node->schema->module->ctx;
Radek Krejci51093642016-03-29 10:14:59 +02006555 int rc = 0;
Michal Vaskocf024702015-10-08 15:01:42 +02006556
6557 assert(node);
6558 memset(&set, 0, sizeof set);
6559
Michal Vasko78d97e22017-02-21 09:54:38 +01006560 if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION)) && (((struct lys_node_container *)node->schema)->when)) {
Michal Vasko76c3bd32016-08-24 16:02:52 +02006561 /* make the node dummy for the evaluation */
6562 node->validity |= LYD_VAL_INUSE;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006563 rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, LYXP_NODE_ELEM, lyd_node_module(node),
6564 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006565 node->validity &= ~LYD_VAL_INUSE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006566 if (rc) {
6567 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006568 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006569 }
Radek Krejci51093642016-03-29 10:14:59 +02006570 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006571 }
6572
Radek Krejci03b71f72016-03-16 11:10:09 +01006573 /* set boolean result of the condition */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006574 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, lyd_node_module(node), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006575 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006576 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006577 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006578 || ((((struct lys_node_container *)node->schema)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6579 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006580 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6581 ((struct lys_node_container *)node->schema)->when->cond);
6582 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006583 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006584 if (failed_when) {
6585 *failed_when = ((struct lys_node_container *)node->schema)->when;
6586 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006587 goto cleanup;
6588 }
Michal Vaskocf024702015-10-08 15:01:42 +02006589 }
Radek Krejci51093642016-03-29 10:14:59 +02006590
6591 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006592 lyxp_set_cast(&set, LYXP_SET_EMPTY, node, lyd_node_module(node), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006593 }
6594
Michal Vasko90fc2a32016-08-24 15:58:58 +02006595 sparent = node->schema;
Michal Vaskocf024702015-10-08 15:01:42 +02006596 goto check_augment;
6597
6598 /* check when in every schema node that affects node */
Michal Vasko90fc2a32016-08-24 15:58:58 +02006599 while (sparent && (sparent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) {
6600 if (((struct lys_node_uses *)sparent)->when) {
Michal Vaskocf024702015-10-08 15:01:42 +02006601 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006602 rc = resolve_when_ctx_node(node, sparent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006603 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006604 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006605 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006606 }
6607 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006608
6609 unlinked_nodes = NULL;
6610 /* we do not want our node pointer to change */
6611 tmp_node = node;
6612 rc = resolve_when_unlink_nodes(sparent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6613 if (rc) {
6614 goto cleanup;
6615 }
6616
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006617 rc = lyxp_eval(((struct lys_node_uses *)sparent)->when->cond, ctx_node, ctx_node_type, lys_node_module(sparent),
6618 &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006619
6620 if (unlinked_nodes && ctx_node) {
6621 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6622 rc = -1;
6623 goto cleanup;
6624 }
6625 }
6626
Radek Krejci03b71f72016-03-16 11:10:09 +01006627 if (rc) {
6628 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006629 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006630 }
Radek Krejci51093642016-03-29 10:14:59 +02006631 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006632 }
6633
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006634 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006635 if (!set.val.bool) {
Michal Vasko0b963112017-08-11 12:45:36 +02006636 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006637 || ((((struct lys_node_uses *)sparent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6638 || (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006639 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
6640 ((struct lys_node_uses *)sparent)->when->cond);
6641 } else {
Michal Vasko2cb18e72017-03-28 14:46:33 +02006642 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko53b7da02018-02-13 15:28:42 +01006643 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)sparent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006644 if (failed_when) {
6645 *failed_when = ((struct lys_node_uses *)sparent)->when;
6646 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006647 goto cleanup;
6648 }
Michal Vaskocf024702015-10-08 15:01:42 +02006649 }
Radek Krejci51093642016-03-29 10:14:59 +02006650
6651 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006652 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006653 }
6654
6655check_augment:
Michal Vasko90fc2a32016-08-24 15:58:58 +02006656 if ((sparent->parent && (sparent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)sparent->parent)->when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02006657 if (!ctx_node) {
Michal Vasko90fc2a32016-08-24 15:58:58 +02006658 rc = resolve_when_ctx_node(node, sparent->parent, &ctx_node, &ctx_node_type);
Michal Vaskoa59495d2016-08-22 09:18:58 +02006659 if (rc) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006660 LOGINT(ctx);
Radek Krejci51093642016-03-29 10:14:59 +02006661 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006662 }
6663 }
Michal Vasko76c3bd32016-08-24 16:02:52 +02006664
6665 unlinked_nodes = NULL;
6666 tmp_node = node;
6667 rc = resolve_when_unlink_nodes(sparent->parent, &tmp_node, &ctx_node, ctx_node_type, &unlinked_nodes);
6668 if (rc) {
6669 goto cleanup;
6670 }
6671
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006672 rc = lyxp_eval(((struct lys_node_augment *)sparent->parent)->when->cond, ctx_node, ctx_node_type,
6673 lys_node_module(sparent->parent), &set, LYXP_WHEN);
Michal Vasko76c3bd32016-08-24 16:02:52 +02006674
6675 /* reconnect nodes, if ctx_node is NULL then all the nodes were unlinked, but linked together,
6676 * so the tree did not actually change and there is nothing for us to do
6677 */
6678 if (unlinked_nodes && ctx_node) {
6679 if (resolve_when_relink_nodes(ctx_node, unlinked_nodes, ctx_node_type)) {
6680 rc = -1;
6681 goto cleanup;
6682 }
6683 }
6684
Radek Krejci03b71f72016-03-16 11:10:09 +01006685 if (rc) {
6686 if (rc == 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006687 LOGVAL(ctx, LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Radek Krejci03b71f72016-03-16 11:10:09 +01006688 }
Radek Krejci51093642016-03-29 10:14:59 +02006689 goto cleanup;
Michal Vaskocf024702015-10-08 15:01:42 +02006690 }
6691
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006692 lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, lys_node_module(sparent->parent), LYXP_WHEN);
Michal Vasko8146d4c2016-05-09 15:50:29 +02006693 if (!set.val.bool) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01006694 node->when_status |= LYD_WHEN_FALSE;
Michal Vasko0b963112017-08-11 12:45:36 +02006695 if ((ignore_fail == 1)
Michal Vaskoc04173b2018-03-09 10:43:22 +01006696 || ((((struct lys_node_augment *)sparent->parent)->when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))
6697 && (ignore_fail == 2))) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006698 LOGVRB("When condition \"%s\" is not satisfied, but it is not required.",
Michal Vasko3cfa3182017-01-17 10:00:58 +01006699 ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006700 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01006701 LOGVAL(ctx, LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)sparent->parent)->when->cond);
Michal Vasko0b963112017-08-11 12:45:36 +02006702 if (failed_when) {
6703 *failed_when = ((struct lys_node_augment *)sparent->parent)->when;
6704 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006705 goto cleanup;
6706 }
Michal Vaskocf024702015-10-08 15:01:42 +02006707 }
Radek Krejci51093642016-03-29 10:14:59 +02006708
6709 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006710 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, lys_node_module(sparent->parent), 0);
Michal Vaskocf024702015-10-08 15:01:42 +02006711 }
6712
Michal Vasko90fc2a32016-08-24 15:58:58 +02006713 sparent = lys_parent(sparent);
Michal Vaskocf024702015-10-08 15:01:42 +02006714 }
6715
Radek Krejci0b7704f2016-03-18 12:16:14 +01006716 node->when_status |= LYD_WHEN_TRUE;
Radek Krejci03b71f72016-03-16 11:10:09 +01006717
Radek Krejci51093642016-03-29 10:14:59 +02006718cleanup:
Radek Krejci51093642016-03-29 10:14:59 +02006719 /* free xpath set content */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01006720 lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, NULL, 0);
Radek Krejci51093642016-03-29 10:14:59 +02006721 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006722}
6723
Radek Krejcicbb473e2016-09-16 14:48:32 +02006724static int
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006725check_type_union_leafref(struct lys_type *type)
6726{
6727 uint8_t i;
6728
6729 if ((type->base == LY_TYPE_UNION) && type->info.uni.count) {
6730 /* go through unions and look for leafref */
6731 for (i = 0; i < type->info.uni.count; ++i) {
6732 switch (type->info.uni.types[i].base) {
6733 case LY_TYPE_LEAFREF:
6734 return 1;
6735 case LY_TYPE_UNION:
6736 if (check_type_union_leafref(&type->info.uni.types[i])) {
6737 return 1;
6738 }
6739 break;
6740 default:
6741 break;
6742 }
6743 }
6744
6745 return 0;
6746 }
6747
6748 /* just inherit the flag value */
6749 return type->der->has_union_leafref;
6750}
6751
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006752/**
Michal Vaskobb211122015-08-19 14:03:11 +02006753 * @brief Resolve a single unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006754 *
6755 * @param[in] mod Main module.
6756 * @param[in] item Item to resolve. Type determined by \p type.
6757 * @param[in] type Type of the unresolved item.
6758 * @param[in] str_snode String, a schema node, or NULL.
Michal Vaskobb211122015-08-19 14:03:11 +02006759 * @param[in] unres Unres schema structure to use.
Michal Vasko769f8032017-01-24 13:11:55 +01006760 * @param[in] final_fail Whether we are just printing errors of the failed unres items.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02006761 *
6762 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
6763 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006764static int
Michal Vasko0bd29d12015-08-19 11:45:49 +02006765resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode,
Michal Vaskof96dfb62017-08-17 12:23:49 +02006766 struct unres_schema *unres)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006767{
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006768 /* has_str - whether the str_snode is a string in a dictionary that needs to be freed */
Michal Vasko53b7da02018-02-13 15:28:42 +01006769 int rc = -1, has_str = 0, parent_type = 0, i, k;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006770 unsigned int j;
Michal Vasko53b7da02018-02-13 15:28:42 +01006771 struct ly_ctx * ctx = mod->ctx;
Radek Krejci80056d52017-01-05 13:13:33 +01006772 struct lys_node *root, *next, *node, *par_grp;
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006773 const char *expr;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006774 uint8_t *u;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006775
Radek Krejcic79c6b12016-07-26 15:11:49 +02006776 struct ly_set *refs, *procs;
6777 struct lys_feature *ref, *feat;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006778 struct lys_ident *ident;
6779 struct lys_type *stype;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006780 struct lys_node_choice *choic;
Michal Vasko88c29542015-11-27 14:57:53 +01006781 struct lyxml_elem *yin;
Pavol Vicana0e4e672016-02-24 12:20:04 +01006782 struct yang_type *yang;
Radek Krejcid09d1a52016-08-11 14:05:45 +02006783 struct unres_list_uniq *unique_info;
Radek Krejcicbb473e2016-09-16 14:48:32 +02006784 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01006785 struct unres_ext *ext_data;
Radek Krejci80056d52017-01-05 13:13:33 +01006786 struct lys_ext_instance *ext, **extlist;
6787 struct lyext_plugin *eplugin;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006788
6789 switch (type) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006790 case UNRES_IDENT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006791 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006792 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006793 ident = item;
6794
Radek Krejci018f1f52016-08-03 16:01:20 +02006795 rc = resolve_base_ident(mod, ident, expr, "identity", NULL, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006796 break;
6797 case UNRES_TYPE_IDENTREF:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006798 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006799 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006800 stype = item;
6801
Radek Krejci018f1f52016-08-03 16:01:20 +02006802 rc = resolve_base_ident(mod, NULL, expr, "type", stype, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006803 break;
6804 case UNRES_TYPE_LEAFREF:
Michal Vasko563ef092015-09-04 13:17:23 +02006805 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006806 stype = item;
6807
Michal Vasko74083ec2018-06-15 10:00:12 +02006808 rc = resolve_schema_leafref(stype, node, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006809 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006810 case UNRES_TYPE_DER_EXT:
6811 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006812 /* falls through */
Radek Krejci3a5501d2016-07-18 22:03:34 +02006813 case UNRES_TYPE_DER_TPDF:
Radek Krejci8d6b7422017-02-03 14:42:13 +01006814 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006815 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006816 case UNRES_TYPE_DER:
Michal Vasko88c29542015-11-27 14:57:53 +01006817 /* parent */
6818 node = str_snode;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006819 stype = item;
6820
Michal Vasko88c29542015-11-27 14:57:53 +01006821 /* HACK type->der is temporarily unparsed type statement */
6822 yin = (struct lyxml_elem *)stype->der;
6823 stype->der = NULL;
6824
Pavol Vicana0e4e672016-02-24 12:20:04 +01006825 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
6826 yang = (struct yang_type *)yin;
Radek Krejci8d6b7422017-02-03 14:42:13 +01006827 rc = yang_check_type(mod, node, yang, stype, parent_type, unres);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006828
6829 if (rc) {
Pavol Vican8bd72e42016-08-29 09:53:05 +02006830 /* may try again later */
6831 stype->der = (struct lys_tpdf *)yang;
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006832 } else {
6833 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006834 lydict_remove(ctx, yang->name);
Pavol Vicand01d8ae2016-03-01 10:45:59 +01006835 free(yang);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006836 }
6837
Michal Vasko88c29542015-11-27 14:57:53 +01006838 } else {
Radek Krejci8d6b7422017-02-03 14:42:13 +01006839 rc = fill_yin_type(mod, node, yin, stype, parent_type, unres);
Radek Krejci63fc0962017-02-15 13:20:18 +01006840 if (!rc || rc == -1) {
Pavol Vicana0e4e672016-02-24 12:20:04 +01006841 /* we need to always be able to free this, it's safe only in this case */
Michal Vasko53b7da02018-02-13 15:28:42 +01006842 lyxml_free(ctx, yin);
Pavol Vicana0e4e672016-02-24 12:20:04 +01006843 } else {
6844 /* may try again later, put all back how it was */
6845 stype->der = (struct lys_tpdf *)yin;
6846 }
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006847 }
Radek Krejcic13db382016-08-16 10:52:42 +02006848 if (rc == EXIT_SUCCESS) {
Radek Krejci2c2fce82016-08-01 13:52:26 +02006849 /* it does not make sense to have leaf-list of empty type */
Radek Krejci8d6b7422017-02-03 14:42:13 +01006850 if (!parent_type && node->nodetype == LYS_LEAFLIST && stype->base == LY_TYPE_EMPTY) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006851 LOGWRN(ctx, "The leaf-list \"%s\" is of \"empty\" type, which does not make sense.", node->name);
Radek Krejci2c2fce82016-08-01 13:52:26 +02006852 }
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006853
6854 if ((type == UNRES_TYPE_DER_TPDF) && (stype->base == LY_TYPE_UNION)) {
6855 /* fill typedef union leafref flag */
6856 ((struct lys_tpdf *)stype->parent)->has_union_leafref = check_type_union_leafref(stype);
6857 } else if ((type == UNRES_TYPE_DER) && stype->der->has_union_leafref) {
6858 /* copy the type in case it has union leafref flag */
6859 if (lys_copy_union_leafrefs(mod, node, stype, NULL, unres)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006860 LOGERR(ctx, LY_EINT, "Failed to duplicate type.");
Michal Vaskoe1c7a822017-06-30 13:15:18 +02006861 return -1;
6862 }
6863 }
Michal Vasko101658e2018-06-05 15:05:54 +02006864 } else if (rc == EXIT_FAILURE && !(stype->value_flags & LY_VALUE_UNRESGRP)) {
Radek Krejcic13db382016-08-16 10:52:42 +02006865 /* forward reference - in case the type is in grouping, we have to make the grouping unusable
6866 * 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 +02006867 * grouping. The grouping cannot be used unless the unres counter is 0.
Michal Vasko70bf8e52018-03-26 11:32:33 +02006868 * To remember that the grouping already increased the counter, the LYTYPE_GRP is used as value
Radek Krejcic13db382016-08-16 10:52:42 +02006869 * of the type's base member. */
6870 for (par_grp = node; par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp));
6871 if (par_grp) {
Radek Krejci93def382017-05-24 15:33:48 +02006872 if (++((struct lys_node_grp *)par_grp)->unres_count == 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006873 LOGERR(ctx, LY_EINT, "Too many unresolved items (type) inside a grouping.");
Radek Krejci93def382017-05-24 15:33:48 +02006874 return -1;
6875 }
Michal Vasko101658e2018-06-05 15:05:54 +02006876 stype->value_flags |= LY_VALUE_UNRESGRP;
Radek Krejcic13db382016-08-16 10:52:42 +02006877 }
Radek Krejci2c2fce82016-08-01 13:52:26 +02006878 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006879 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006880 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02006881 iff_data = str_snode;
6882 rc = resolve_feature(iff_data->fname, strlen(iff_data->fname), iff_data->node, item);
Radek Krejci9ff0a922016-07-14 13:08:05 +02006883 if (!rc) {
6884 /* success */
Radek Krejci9de2c042016-10-19 16:53:06 +02006885 if (iff_data->infeature) {
6886 /* store backlink into the target feature to allow reverse changes in case of changing feature status */
6887 feat = *((struct lys_feature **)item);
6888 if (!feat->depfeatures) {
6889 feat->depfeatures = ly_set_new();
6890 }
Radek Krejci85a54be2016-10-20 12:39:56 +02006891 ly_set_add(feat->depfeatures, iff_data->node, LY_SET_OPT_USEASLIST);
Radek Krejci9de2c042016-10-19 16:53:06 +02006892 }
6893 /* cleanup temporary data */
Michal Vasko53b7da02018-02-13 15:28:42 +01006894 lydict_remove(ctx, iff_data->fname);
Radek Krejcicbb473e2016-09-16 14:48:32 +02006895 free(iff_data);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006896 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006897 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02006898 case UNRES_FEATURE:
6899 feat = (struct lys_feature *)item;
6900
6901 if (feat->iffeature_size) {
6902 refs = ly_set_new();
6903 procs = ly_set_new();
6904 ly_set_add(procs, feat, 0);
6905
6906 while (procs->number) {
6907 ref = procs->set.g[procs->number - 1];
6908 ly_set_rm_index(procs, procs->number - 1);
6909
6910 for (i = 0; i < ref->iffeature_size; i++) {
6911 resolve_iffeature_getsizes(&ref->iffeature[i], NULL, &j);
6912 for (; j > 0 ; j--) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02006913 if (ref->iffeature[i].features[j - 1]) {
Radek Krejcic79c6b12016-07-26 15:11:49 +02006914 if (ref->iffeature[i].features[j - 1] == feat) {
Michal Vasko53b7da02018-02-13 15:28:42 +01006915 LOGVAL(ctx, LYE_CIRC_FEATURES, LY_VLOG_NONE, NULL, feat->name);
Radek Krejcic79c6b12016-07-26 15:11:49 +02006916 goto featurecheckdone;
6917 }
6918
6919 if (ref->iffeature[i].features[j - 1]->iffeature_size) {
6920 k = refs->number;
6921 if (ly_set_add(refs, ref->iffeature[i].features[j - 1], 0) == k) {
6922 /* not yet seen feature, add it for processing */
6923 ly_set_add(procs, ref->iffeature[i].features[j - 1], 0);
6924 }
6925 }
6926 } else {
6927 /* forward reference */
6928 rc = EXIT_FAILURE;
6929 goto featurecheckdone;
6930 }
6931 }
6932
6933 }
6934 }
6935 rc = EXIT_SUCCESS;
6936
6937featurecheckdone:
6938 ly_set_free(refs);
6939 ly_set_free(procs);
6940 }
6941
6942 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006943 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01006944 rc = resolve_unres_schema_uses(item, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006945 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006946 case UNRES_TYPEDEF_DFLT:
6947 parent_type++;
Radek Krejci3d679d72017-08-01 10:44:37 +02006948 /* falls through */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006949 case UNRES_TYPE_DFLT:
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006950 stype = item;
Radek Krejciab08f0f2017-03-09 16:37:15 +01006951 rc = check_default(stype, (const char **)str_snode, mod, parent_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006952 break;
6953 case UNRES_CHOICE_DFLT:
Michal Vaskoc5c26b02016-06-29 11:10:29 +02006954 expr = str_snode;
Radek Krejci4f78b532016-02-17 13:43:00 +01006955 has_str = 1;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006956 choic = item;
6957
Radek Krejcie00d2312016-08-12 15:27:49 +02006958 if (!choic->dflt) {
6959 choic->dflt = resolve_choice_dflt(choic, expr);
6960 }
Michal Vasko7955b362015-09-04 14:18:15 +02006961 if (choic->dflt) {
Radek Krejcie00d2312016-08-12 15:27:49 +02006962 rc = lyp_check_mandatory_choice((struct lys_node *)choic);
Michal Vasko7955b362015-09-04 14:18:15 +02006963 } else {
6964 rc = EXIT_FAILURE;
Michal Vasko5fcfe7e2015-08-17 14:59:57 +02006965 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006966 break;
6967 case UNRES_LIST_KEYS:
Radek Krejci5c08a992016-11-02 13:30:04 +01006968 rc = resolve_list_keys(item, ((struct lys_node_list *)item)->keys_str);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006969 break;
6970 case UNRES_LIST_UNIQ:
Radek Krejcid09d1a52016-08-11 14:05:45 +02006971 unique_info = (struct unres_list_uniq *)item;
6972 rc = resolve_unique(unique_info->list, unique_info->expr, unique_info->trg_type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02006973 break;
Michal Vasko7178e692016-02-12 15:58:05 +01006974 case UNRES_AUGMENT:
Radek Krejcib3142312016-11-09 11:04:12 +01006975 rc = resolve_augment(item, NULL, unres);
Michal Vasko7178e692016-02-12 15:58:05 +01006976 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02006977 case UNRES_XPATH:
6978 node = (struct lys_node *)item;
Michal Vasko895c11f2018-03-12 11:35:58 +01006979 rc = check_xpath(node, 1);
Michal Vasko508a50d2016-09-07 14:50:33 +02006980 break;
Michal Vasko0f437062018-06-08 15:52:39 +02006981 case UNRES_MOD_IMPLEMENT:
6982 rc = lys_make_implemented_r(mod, unres);
6983 break;
Radek Krejcie534c132016-11-23 13:32:31 +01006984 case UNRES_EXT:
6985 ext_data = (struct unres_ext *)str_snode;
Radek Krejci2b999ac2017-01-18 16:22:12 +01006986 extlist = &(*(struct lys_ext_instance ***)item)[ext_data->ext_index];
Radek Krejcia7db9702017-01-20 12:55:14 +01006987 rc = resolve_extension(ext_data, extlist, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006988 if (!rc) {
Radek Krejci79685c92017-02-17 10:49:43 +01006989 /* success */
Radek Krejci80056d52017-01-05 13:13:33 +01006990 /* is there a callback to be done to finalize the extension? */
Radek Krejci2b999ac2017-01-18 16:22:12 +01006991 eplugin = extlist[0]->def->plugin;
Radek Krejci80056d52017-01-05 13:13:33 +01006992 if (eplugin) {
6993 if (eplugin->check_result || (eplugin->flags & LYEXT_OPT_INHERIT)) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006994 u = malloc(sizeof *u);
Michal Vasko53b7da02018-02-13 15:28:42 +01006995 LY_CHECK_ERR_RETURN(!u, LOGMEM(ctx), -1);
Radek Krejci2b999ac2017-01-18 16:22:12 +01006996 (*u) = ext_data->ext_index;
Radek Krejcib08bc172017-02-27 13:17:14 +01006997 if (unres_schema_add_node(mod, unres, item, UNRES_EXT_FINALIZE, (struct lys_node *)u) == -1) {
6998 /* something really bad happend since the extension finalization is not actually
6999 * being resolved while adding into unres, so something more serious with the unres
7000 * list itself must happened */
7001 return -1;
7002 }
Radek Krejci80056d52017-01-05 13:13:33 +01007003 }
7004 }
Radek Krejci79685c92017-02-17 10:49:43 +01007005 }
7006 if (!rc || rc == -1) {
7007 /* cleanup on success or fatal error */
7008 if (ext_data->datatype == LYS_IN_YIN) {
7009 /* YIN */
Michal Vasko53b7da02018-02-13 15:28:42 +01007010 lyxml_free(ctx, ext_data->data.yin);
Radek Krejci79685c92017-02-17 10:49:43 +01007011 } else {
PavolVicandb0e8172017-02-20 00:46:09 +01007012 /* YANG */
7013 yang_free_ext_data(ext_data->data.yang);
Radek Krejci79685c92017-02-17 10:49:43 +01007014 }
Radek Krejci2b999ac2017-01-18 16:22:12 +01007015 free(ext_data);
Radek Krejcie534c132016-11-23 13:32:31 +01007016 }
7017 break;
Radek Krejci80056d52017-01-05 13:13:33 +01007018 case UNRES_EXT_FINALIZE:
Radek Krejci2b999ac2017-01-18 16:22:12 +01007019 u = (uint8_t *)str_snode;
7020 ext = (*(struct lys_ext_instance ***)item)[*u];
7021 free(u);
7022
Radek Krejci80056d52017-01-05 13:13:33 +01007023 eplugin = ext->def->plugin;
7024
7025 /* inherit */
7026 if ((eplugin->flags & LYEXT_OPT_INHERIT) && (ext->parent_type == LYEXT_PAR_NODE)) {
7027 root = (struct lys_node *)ext->parent;
7028 if (!(root->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
7029 LY_TREE_DFS_BEGIN(root->child, next, node) {
7030 /* first, check if the node already contain instance of the same extension,
7031 * in such a case we won't inherit. In case the node was actually defined as
7032 * augment data, we are supposed to check the same way also the augment node itself */
7033 if (lys_ext_instance_presence(ext->def, node->ext, node->ext_size) != -1) {
7034 goto inherit_dfs_sibling;
7035 } else if (node->parent != root && node->parent->nodetype == LYS_AUGMENT &&
7036 lys_ext_instance_presence(ext->def, node->parent->ext, node->parent->ext_size) != -1) {
7037 goto inherit_dfs_sibling;
7038 }
7039
7040 if (eplugin->check_inherit) {
7041 /* we have a callback to check the inheritance, use it */
7042 switch ((rc = (*eplugin->check_inherit)(ext, node))) {
7043 case 0:
7044 /* yes - continue with the inheriting code */
7045 break;
7046 case 1:
7047 /* no - continue with the node's sibling */
7048 goto inherit_dfs_sibling;
7049 case 2:
7050 /* no, but continue with the children, just skip the inheriting code for this node */
7051 goto inherit_dfs_child;
7052 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007053 LOGERR(ctx, LY_EINT, "Plugin's (%s:%s) check_inherit callback returns invalid value (%d),",
Radek Krejci80056d52017-01-05 13:13:33 +01007054 ext->def->module->name, ext->def->name, rc);
7055 }
7056 }
7057
7058 /* inherit the extension */
7059 extlist = realloc(node->ext, (node->ext_size + 1) * sizeof *node->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01007060 LY_CHECK_ERR_RETURN(!extlist, LOGMEM(ctx), -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007061 extlist[node->ext_size] = malloc(sizeof **extlist);
Michal Vasko53b7da02018-02-13 15:28:42 +01007062 LY_CHECK_ERR_RETURN(!extlist[node->ext_size], LOGMEM(ctx); node->ext = extlist, -1);
Radek Krejci80056d52017-01-05 13:13:33 +01007063 memcpy(extlist[node->ext_size], ext, sizeof *ext);
7064 extlist[node->ext_size]->flags |= LYEXT_OPT_INHERIT;
7065
7066 node->ext = extlist;
7067 node->ext_size++;
7068
7069inherit_dfs_child:
7070 /* modification of - select element for the next run - children first */
7071 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
7072 next = NULL;
7073 } else {
7074 next = node->child;
7075 }
7076 if (!next) {
7077inherit_dfs_sibling:
7078 /* no children, try siblings */
7079 next = node->next;
7080 }
7081 while (!next) {
7082 /* go to the parent */
7083 node = lys_parent(node);
7084
7085 /* we are done if we are back in the root (the starter's parent */
7086 if (node == root) {
7087 break;
7088 }
7089
7090 /* parent is already processed, go to its sibling */
7091 next = node->next;
7092 }
7093 }
7094 }
7095 }
7096
7097 /* final check */
7098 if (eplugin->check_result) {
7099 if ((*eplugin->check_result)(ext)) {
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007100 LOGERR(ctx, LY_EPLUGIN, "Resolving extension failed.");
Radek Krejci80056d52017-01-05 13:13:33 +01007101 return -1;
7102 }
7103 }
7104
7105 rc = 0;
7106 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007107 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007108 LOGINT(ctx);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007109 break;
7110 }
7111
Radek Krejci54081ce2016-08-12 15:21:47 +02007112 if (has_str && !rc) {
7113 /* the string is no more needed in case of success.
7114 * In case of forward reference, we will try to resolve the string later */
Michal Vasko53b7da02018-02-13 15:28:42 +01007115 lydict_remove(ctx, str_snode);
Radek Krejci4f78b532016-02-17 13:43:00 +01007116 }
7117
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007118 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007119}
7120
Michal Vaskof02e3742015-08-05 16:27:02 +02007121/* logs directly */
7122static void
Radek Krejci48464ed2016-03-17 15:44:09 +01007123print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007124{
Michal Vaskocb34dc62016-05-20 14:38:37 +02007125 struct lyxml_elem *xml;
7126 struct lyxml_attr *attr;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007127 struct unres_iffeat_data *iff_data;
Radek Krejcie534c132016-11-23 13:32:31 +01007128 const char *name = NULL;
7129 struct unres_ext *extinfo;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007130
Michal Vaskof02e3742015-08-05 16:27:02 +02007131 switch (type) {
Michal Vaskof02e3742015-08-05 16:27:02 +02007132 case UNRES_IDENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007133 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007134 break;
7135 case UNRES_TYPE_IDENTREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007136 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007137 break;
7138 case UNRES_TYPE_LEAFREF:
Radek Krejci48464ed2016-03-17 15:44:09 +01007139 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref",
7140 ((struct lys_type *)item)->info.lref.path);
Michal Vaskof02e3742015-08-05 16:27:02 +02007141 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01007142 case UNRES_TYPE_DER_EXT:
Radek Krejci3a5501d2016-07-18 22:03:34 +02007143 case UNRES_TYPE_DER_TPDF:
Michal Vaskof02e3742015-08-05 16:27:02 +02007144 case UNRES_TYPE_DER:
Michal Vaskocb34dc62016-05-20 14:38:37 +02007145 xml = (struct lyxml_elem *)((struct lys_type *)item)->der;
7146 if (xml->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejcie534c132016-11-23 13:32:31 +01007147 name = ((struct yang_type *)xml)->name;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007148 } else {
7149 LY_TREE_FOR(xml->attr, attr) {
7150 if ((attr->type == LYXML_ATTR_STD) && !strcmp(attr->name, "name")) {
Radek Krejcie534c132016-11-23 13:32:31 +01007151 name = attr->value;
Michal Vaskocb34dc62016-05-20 14:38:37 +02007152 break;
7153 }
7154 }
7155 assert(attr);
7156 }
Radek Krejcie534c132016-11-23 13:32:31 +01007157 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007158 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007159 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007160 iff_data = str_node;
7161 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", iff_data->fname);
Michal Vaskof02e3742015-08-05 16:27:02 +02007162 break;
Radek Krejcic79c6b12016-07-26 15:11:49 +02007163 case UNRES_FEATURE:
7164 LOGVRB("There are unresolved if-features for \"%s\" feature circular dependency check, it will be attempted later",
7165 ((struct lys_feature *)item)->name);
7166 break;
Michal Vaskof02e3742015-08-05 16:27:02 +02007167 case UNRES_USES:
Radek Krejci48464ed2016-03-17 15:44:09 +01007168 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
Michal Vaskof02e3742015-08-05 16:27:02 +02007169 break;
Radek Krejciab08f0f2017-03-09 16:37:15 +01007170 case UNRES_TYPEDEF_DFLT:
Michal Vaskof02e3742015-08-05 16:27:02 +02007171 case UNRES_TYPE_DFLT:
Michal Vaskoba835cd2018-02-23 09:25:48 +01007172 if (*(char **)str_node) {
7173 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", *(char **)str_node);
Radek Krejci2e2de832016-10-13 16:12:26 +02007174 } /* else no default value in the type itself, but we are checking some restrictions against
7175 * possible default value of some base type. The failure is caused by not resolved base type,
7176 * so it was already reported */
Michal Vaskof02e3742015-08-05 16:27:02 +02007177 break;
7178 case UNRES_CHOICE_DFLT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007179 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007180 break;
7181 case UNRES_LIST_KEYS:
Radek Krejci48464ed2016-03-17 15:44:09 +01007182 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007183 break;
7184 case UNRES_LIST_UNIQ:
Radek Krejci48464ed2016-03-17 15:44:09 +01007185 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node);
Michal Vaskof02e3742015-08-05 16:27:02 +02007186 break;
Michal Vasko7178e692016-02-12 15:58:05 +01007187 case UNRES_AUGMENT:
Radek Krejci48464ed2016-03-17 15:44:09 +01007188 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target",
7189 ((struct lys_node_augment *)item)->target_name);
Michal Vasko7178e692016-02-12 15:58:05 +01007190 break;
Michal Vasko508a50d2016-09-07 14:50:33 +02007191 case UNRES_XPATH:
Michal Vasko0d198372016-11-16 11:40:03 +01007192 LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "XPath expressions of",
7193 ((struct lys_node *)item)->name);
Michal Vasko508a50d2016-09-07 14:50:33 +02007194 break;
Radek Krejcie534c132016-11-23 13:32:31 +01007195 case UNRES_EXT:
7196 extinfo = (struct unres_ext *)str_node;
7197 name = extinfo->datatype == LYS_IN_YIN ? extinfo->data.yin->name : NULL; /* TODO YANG extension */
7198 LOGVRB("Resolving extension \"%s\" failed, it will be attempted later.", name);
7199 break;
Michal Vaskocf024702015-10-08 15:01:42 +02007200 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01007201 LOGINT(NULL);
Michal Vaskof02e3742015-08-05 16:27:02 +02007202 break;
7203 }
7204}
7205
Michal Vasko0f437062018-06-08 15:52:39 +02007206static int
7207resolve_unres_schema_types(struct unres_schema *unres, enum UNRES_ITEM types, struct ly_ctx *ctx, int forward_ref,
7208 int print_all_errors, uint32_t *resolved)
7209{
7210 uint32_t i, unres_count, res_count;
7211 int ret = 0, rc;
7212 struct ly_err_item *prev_eitem;
7213 enum int_log_opts prev_ilo;
7214 LY_ERR prev_ly_errno;
7215
7216 /* if there can be no forward references, every failure is final, so we can print it directly */
7217 if (forward_ref) {
7218 prev_ly_errno = ly_errno;
7219 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
7220 }
7221
7222 do {
7223 unres_count = 0;
7224 res_count = 0;
7225
7226 for (i = 0; i < unres->count; ++i) {
7227 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7228 * if-features are resolved here to make sure that we will have all if-features for
7229 * later check of feature circular dependency */
7230 if (unres->type[i] & types) {
7231 ++unres_count;
7232 rc = resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7233 if (unres->type[i] == UNRES_EXT_FINALIZE) {
7234 /* to avoid double free */
7235 unres->type[i] = UNRES_RESOLVED;
7236 }
7237 if (!rc || (unres->type[i] == UNRES_XPATH)) {
7238 /* invalid XPath can never cause an error, only a warning */
7239 if (unres->type[i] == UNRES_LIST_UNIQ) {
7240 /* free the allocated structure */
7241 free(unres->item[i]);
7242 }
7243
7244 unres->type[i] = UNRES_RESOLVED;
7245 ++(*resolved);
7246 ++res_count;
7247 } else if ((rc == EXIT_FAILURE) && forward_ref) {
7248 /* forward reference, erase errors */
7249 ly_err_free_next(ctx, prev_eitem);
7250 } else if (print_all_errors) {
7251 /* just so that we quit the loop */
7252 ++res_count;
7253 ret = -1;
7254 } else {
7255 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
7256 return -1;
7257 }
7258 }
7259 }
7260 } while (res_count && (res_count < unres_count));
7261
7262 if (res_count < unres_count) {
7263 assert(forward_ref);
7264 /* just print the errors (but we must free the ones we have and get them again :-/ ) */
7265 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7266
7267 for (i = 0; i < unres->count; ++i) {
7268 if (unres->type[i] & types) {
7269 resolve_unres_schema_item(unres->module[i], unres->item[i], unres->type[i], unres->str_snode[i], unres);
7270 }
7271 }
7272 return -1;
7273 }
7274
7275 if (forward_ref) {
7276 /* restore log */
7277 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7278 ly_errno = prev_ly_errno;
7279 }
7280
7281 return ret;
7282}
7283
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007284/**
Michal Vaskobb211122015-08-19 14:03:11 +02007285 * @brief Resolve every unres schema item in the structure. Logs directly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007286 *
7287 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007288 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007289 *
Michal Vasko92b8a382015-08-19 14:03:49 +02007290 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007291 */
Michal Vaskof02e3742015-08-05 16:27:02 +02007292int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007293resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres)
Michal Vaskof02e3742015-08-05 16:27:02 +02007294{
Michal Vasko0f437062018-06-08 15:52:39 +02007295 uint32_t resolved = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007296
7297 assert(unres);
7298
Michal Vaskoe8734262016-09-29 14:12:06 +02007299 LOGVRB("Resolving \"%s\" unresolved schema nodes and their constraints...", mod->name);
Michal Vasko51054ca2015-08-12 12:20:00 +02007300
Michal Vasko0f437062018-06-08 15:52:39 +02007301 /* UNRES_TYPE_LEAFREF must be resolved (for storing leafref target pointers);
7302 * if-features are resolved here to make sure that we will have all if-features for
7303 * later check of feature circular dependency */
7304 if (resolve_unres_schema_types(unres, UNRES_USES | UNRES_IFFEAT | UNRES_TYPE_DER | UNRES_TYPE_DER_TPDF | UNRES_TYPE_DER_TPDF
7305 | UNRES_TYPE_LEAFREF | UNRES_MOD_IMPLEMENT | UNRES_AUGMENT | UNRES_CHOICE_DFLT | UNRES_IDENT,
7306 mod->ctx, 1, 0, &resolved)) {
Michal Vasko92b8a382015-08-19 14:03:49 +02007307 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007308 }
7309
Michal Vasko0f437062018-06-08 15:52:39 +02007310 /* another batch of resolved items */
7311 if (resolve_unres_schema_types(unres, UNRES_TYPE_IDENTREF | UNRES_FEATURE | UNRES_TYPEDEF_DFLT | UNRES_TYPE_DFLT
7312 | UNRES_LIST_KEYS | UNRES_LIST_UNIQ | UNRES_EXT, mod->ctx, 1, 0, &resolved)) {
7313 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007314 }
7315
Michal Vasko0f437062018-06-08 15:52:39 +02007316 /* print xpath warnings and finalize extensions, keep it last to provide the complete schema tree information to the plugin's checkers */
7317 if (resolve_unres_schema_types(unres, UNRES_XPATH | UNRES_EXT_FINALIZE, mod->ctx, 0, 1, &resolved)) {
7318 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007319 }
7320
Michal Vaskoe8734262016-09-29 14:12:06 +02007321 LOGVRB("All \"%s\" schema nodes and constraints resolved.", mod->name);
Radek Krejcic071c542016-01-27 14:57:51 +01007322 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007323 return EXIT_SUCCESS;
7324}
7325
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007326/**
Michal Vaskobb211122015-08-19 14:03:11 +02007327 * @brief Try to resolve an unres schema item with a string argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007328 *
7329 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007330 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007331 * @param[in] item Item to resolve. Type determined by \p type.
7332 * @param[in] type Type of the unresolved item.
7333 * @param[in] str String argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007334 *
Michal Vasko3767fb22016-07-21 12:10:57 +02007335 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007336 */
7337int
Radek Krejci48464ed2016-03-17 15:44:09 +01007338unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
7339 const char *str)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007340{
Radek Krejci54081ce2016-08-12 15:21:47 +02007341 int rc;
7342 const char *dictstr;
7343
7344 dictstr = lydict_insert(mod->ctx, str, 0);
7345 rc = unres_schema_add_node(mod, unres, item, type, (struct lys_node *)dictstr);
7346
Radek Krejcid9c0ce22017-01-20 15:20:16 +01007347 if (rc < 0) {
Radek Krejci54081ce2016-08-12 15:21:47 +02007348 lydict_remove(mod->ctx, dictstr);
7349 }
7350 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007351}
7352
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007353/**
Michal Vaskobb211122015-08-19 14:03:11 +02007354 * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007355 *
7356 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007357 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007358 * @param[in] item Item to resolve. Type determined by \p type.
Michal Vasko88c29542015-11-27 14:57:53 +01007359 * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially!
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007360 * @param[in] snode Schema node argument.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007361 *
Radek Krejci62f7aad2017-10-26 14:53:52 +02007362 * @return EXIT_SUCCESS on success, EXIT_FAILURE on storing the item in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007363 */
7364int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007365unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type,
Radek Krejci48464ed2016-03-17 15:44:09 +01007366 struct lys_node *snode)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007367{
Michal Vasko53b7da02018-02-13 15:28:42 +01007368 int rc;
Radek Krejci62f7aad2017-10-26 14:53:52 +02007369 uint32_t u;
Michal Vasko53b7da02018-02-13 15:28:42 +01007370 enum int_log_opts prev_ilo;
7371 struct ly_err_item *prev_eitem;
7372 LY_ERR prev_ly_errno;
Michal Vasko88c29542015-11-27 14:57:53 +01007373 struct lyxml_elem *yin;
Michal Vasko53b7da02018-02-13 15:28:42 +01007374 struct ly_ctx *ctx = mod->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007375
Michal Vasko0f437062018-06-08 15:52:39 +02007376 assert(unres && (item || (type == UNRES_MOD_IMPLEMENT)) && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID)
7377 && (type != UNRES_WHEN) && (type != UNRES_MUST)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007378
Radek Krejci850a5de2016-11-08 14:06:40 +01007379 /* check for duplicities in unres */
7380 for (u = 0; u < unres->count; u++) {
7381 if (unres->type[u] == type && unres->item[u] == item &&
7382 unres->str_snode[u] == snode && unres->module[u] == mod) {
Radek Krejci62f7aad2017-10-26 14:53:52 +02007383 /* duplication can happen when the node contains multiple statements of the same type to check,
7384 * this can happen for example when refinement is being applied, so we just postpone the processing
7385 * and do not duplicate the information */
7386 return EXIT_FAILURE;
Radek Krejci850a5de2016-11-08 14:06:40 +01007387 }
7388 }
7389
Michal Vasko0f437062018-06-08 15:52:39 +02007390 if ((type == UNRES_EXT_FINALIZE) || (type == UNRES_XPATH) || (type == UNRES_MOD_IMPLEMENT)) {
Michal Vaskof96dfb62017-08-17 12:23:49 +02007391 /* extension finalization is not even tried when adding the item into the inres list,
Michal Vasko0f437062018-06-08 15:52:39 +02007392 * xpath is not tried because it would hide some potential warnings,
7393 * implementing module must be deferred because some other nodes can be added that will need to be traversed
7394 * and their targets made implemented */
Radek Krejcic293bac2017-02-27 11:25:28 +01007395 rc = EXIT_FAILURE;
7396 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01007397 prev_ly_errno = ly_errno;
7398 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007399
Michal Vasko53b7da02018-02-13 15:28:42 +01007400 rc = resolve_unres_schema_item(mod, item, type, snode, unres);
Radek Krejci80056d52017-01-05 13:13:33 +01007401 if (rc != EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007402 ly_ilo_restore(ctx, prev_ilo, prev_eitem, rc == -1 ? 1 : 0);
7403 if (rc != -1) {
7404 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007405 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007406
Radek Krejci80056d52017-01-05 13:13:33 +01007407 if (type == UNRES_LIST_UNIQ) {
7408 /* free the allocated structure */
7409 free(item);
7410 } else if (rc == -1 && type == UNRES_IFFEAT) {
7411 /* free the allocated resources */
7412 free(*((char **)item));
Michal Vaskobb520442017-05-23 10:55:18 +02007413 }
Radek Krejci80056d52017-01-05 13:13:33 +01007414 return rc;
7415 } else {
7416 /* erase info about validation errors */
Michal Vasko53b7da02018-02-13 15:28:42 +01007417 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
7418 ly_errno = prev_ly_errno;
Radek Krejci80056d52017-01-05 13:13:33 +01007419 }
Michal Vaskof02e3742015-08-05 16:27:02 +02007420
Radek Krejci80056d52017-01-05 13:13:33 +01007421 print_unres_schema_item_fail(item, type, snode);
7422
7423 /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */
7424 if (type == UNRES_TYPE_DER || type == UNRES_TYPE_DER_TPDF) {
7425 yin = (struct lyxml_elem *)((struct lys_type *)item)->der;
7426 if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) {
7427 lyxml_unlink_elem(mod->ctx, yin, 1);
7428 ((struct lys_type *)item)->der = (struct lys_tpdf *)yin;
7429 }
Pavol Vicana0e4e672016-02-24 12:20:04 +01007430 }
Michal Vasko88c29542015-11-27 14:57:53 +01007431 }
7432
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007433 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01007434 unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item);
Michal Vasko53b7da02018-02-13 15:28:42 +01007435 LY_CHECK_ERR_RETURN(!unres->item, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007436 unres->item[unres->count-1] = item;
Michal Vasko253035f2015-12-17 16:58:13 +01007437 unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01007438 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(ctx), -1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007439 unres->type[unres->count-1] = type;
Michal Vasko253035f2015-12-17 16:58:13 +01007440 unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode);
Michal Vasko53b7da02018-02-13 15:28:42 +01007441 LY_CHECK_ERR_RETURN(!unres->str_snode, LOGMEM(ctx), -1);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007442 unres->str_snode[unres->count-1] = snode;
Radek Krejcic071c542016-01-27 14:57:51 +01007443 unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module);
Michal Vasko53b7da02018-02-13 15:28:42 +01007444 LY_CHECK_ERR_RETURN(!unres->module, LOGMEM(ctx), -1);
Radek Krejcic071c542016-01-27 14:57:51 +01007445 unres->module[unres->count-1] = mod;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007446
Michal Vasko3767fb22016-07-21 12:10:57 +02007447 return rc;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007448}
7449
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007450/**
Michal Vaskobb211122015-08-19 14:03:11 +02007451 * @brief Duplicate an unres schema item. Logs indirectly.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007452 *
7453 * @param[in] mod Main module.
Michal Vaskobb211122015-08-19 14:03:11 +02007454 * @param[in] unres Unres schema structure to use.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007455 * @param[in] item Old item to be resolved.
7456 * @param[in] type Type of the old unresolved item.
7457 * @param[in] new_item New item to use in the duplicate.
7458 *
Radek Krejci9ff0a922016-07-14 13:08:05 +02007459 * @return EXIT_SUCCESS on success, EXIT_FAILURE if item is not in unres, -1 on error.
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007460 */
Michal Vaskodad19402015-08-06 09:51:53 +02007461int
Michal Vasko0bd29d12015-08-19 11:45:49 +02007462unres_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 +02007463{
7464 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007465 struct unres_list_uniq aux_uniq;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007466 struct unres_iffeat_data *iff_data;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007467
Michal Vaskocf024702015-10-08 15:01:42 +02007468 assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN)));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007469
Radek Krejcid09d1a52016-08-11 14:05:45 +02007470 /* hack for UNRES_LIST_UNIQ, which stores multiple items behind its item */
7471 if (type == UNRES_LIST_UNIQ) {
7472 aux_uniq.list = item;
7473 aux_uniq.expr = ((struct unres_list_uniq *)new_item)->expr;
7474 item = &aux_uniq;
7475 }
Michal Vasko878e38d2016-09-05 12:17:53 +02007476 i = unres_schema_find(unres, -1, item, type);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007477
7478 if (i == -1) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007479 if (type == UNRES_LIST_UNIQ) {
7480 free(new_item);
7481 }
Radek Krejci9ff0a922016-07-14 13:08:05 +02007482 return EXIT_FAILURE;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007483 }
7484
Radek Krejcic79c6b12016-07-26 15:11:49 +02007485 if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) ||
Radek Krejcib69f3232016-09-16 17:41:07 +02007486 (type == UNRES_FEATURE) || (type == UNRES_LIST_UNIQ)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01007487 if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007488 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007489 return -1;
7490 }
Radek Krejcicbb473e2016-09-16 14:48:32 +02007491 } else if (type == UNRES_IFFEAT) {
7492 /* duplicate unres_iffeature_data */
7493 iff_data = malloc(sizeof *iff_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01007494 LY_CHECK_ERR_RETURN(!iff_data, LOGMEM(mod->ctx), -1);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007495 iff_data->fname = lydict_insert(mod->ctx, ((struct unres_iffeat_data *)unres->str_snode[i])->fname, 0);
7496 iff_data->node = ((struct unres_iffeat_data *)unres->str_snode[i])->node;
7497 if (unres_schema_add_node(mod, unres, new_item, type, (struct lys_node *)iff_data) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007498 LOGINT(mod->ctx);
Radek Krejcicbb473e2016-09-16 14:48:32 +02007499 return -1;
7500 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007501 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01007502 if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007503 LOGINT(mod->ctx);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02007504 return -1;
7505 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007506 }
Michal Vaskodad19402015-08-06 09:51:53 +02007507
7508 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007509}
7510
Michal Vaskof02e3742015-08-05 16:27:02 +02007511/* does not log */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007512int
Michal Vasko878e38d2016-09-05 12:17:53 +02007513unres_schema_find(struct unres_schema *unres, int start_on_backwards, void *item, enum UNRES_ITEM type)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007514{
Michal Vasko878e38d2016-09-05 12:17:53 +02007515 int i;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007516 struct unres_list_uniq *aux_uniq1, *aux_uniq2;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007517
Radek Krejciddddd0d2017-01-20 15:20:46 +01007518 if (start_on_backwards >= 0) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007519 i = start_on_backwards;
7520 } else {
7521 i = unres->count - 1;
7522 }
7523 for (; i > -1; i--) {
7524 if (unres->type[i] != type) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007525 continue;
7526 }
7527 if (type != UNRES_LIST_UNIQ) {
Michal Vasko878e38d2016-09-05 12:17:53 +02007528 if (unres->item[i] == item) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007529 break;
7530 }
7531 } else {
7532 aux_uniq1 = (struct unres_list_uniq *)unres->item[i - 1];
7533 aux_uniq2 = (struct unres_list_uniq *)item;
7534 if ((aux_uniq1->list == aux_uniq2->list) && ly_strequal(aux_uniq1->expr, aux_uniq2->expr, 0)) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02007535 break;
7536 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007537 }
7538 }
7539
Michal Vasko878e38d2016-09-05 12:17:53 +02007540 return i;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02007541}
Michal Vasko8bcdf292015-08-19 14:04:43 +02007542
Michal Vaskoede9c472016-06-07 09:38:15 +02007543static void
7544unres_schema_free_item(struct ly_ctx *ctx, struct unres_schema *unres, uint32_t i)
7545{
7546 struct lyxml_elem *yin;
7547 struct yang_type *yang;
Radek Krejcicbb473e2016-09-16 14:48:32 +02007548 struct unres_iffeat_data *iff_data;
Michal Vaskoede9c472016-06-07 09:38:15 +02007549
7550 switch (unres->type[i]) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02007551 case UNRES_TYPE_DER_TPDF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007552 case UNRES_TYPE_DER:
7553 yin = (struct lyxml_elem *)((struct lys_type *)unres->item[i])->der;
7554 if (yin->flags & LY_YANG_STRUCTURE_FLAG) {
7555 yang =(struct yang_type *)yin;
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007556 ((struct lys_type *)unres->item[i])->base = yang->base;
Michal Vaskoede9c472016-06-07 09:38:15 +02007557 lydict_remove(ctx, yang->name);
7558 free(yang);
Pavol Vicancf2af4d2016-12-21 14:13:06 +01007559 if (((struct lys_type *)unres->item[i])->base == LY_TYPE_UNION) {
7560 yang_free_type_union(ctx, (struct lys_type *)unres->item[i]);
7561 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007562 } else {
7563 lyxml_free(ctx, yin);
7564 }
7565 break;
Pavol Vican88e16c92016-09-07 15:41:50 +02007566 case UNRES_IFFEAT:
Radek Krejcicbb473e2016-09-16 14:48:32 +02007567 iff_data = (struct unres_iffeat_data *)unres->str_snode[i];
7568 lydict_remove(ctx, iff_data->fname);
7569 free(unres->str_snode[i]);
Pavol Vican88e16c92016-09-07 15:41:50 +02007570 break;
Michal Vaskoede9c472016-06-07 09:38:15 +02007571 case UNRES_IDENT:
7572 case UNRES_TYPE_IDENTREF:
Michal Vaskoede9c472016-06-07 09:38:15 +02007573 case UNRES_CHOICE_DFLT:
7574 case UNRES_LIST_KEYS:
Michal Vaskoede9c472016-06-07 09:38:15 +02007575 lydict_remove(ctx, (const char *)unres->str_snode[i]);
7576 break;
Radek Krejcid09d1a52016-08-11 14:05:45 +02007577 case UNRES_LIST_UNIQ:
7578 free(unres->item[i]);
7579 break;
PavolVicanc1807262017-01-31 18:00:27 +01007580 case UNRES_EXT:
7581 free(unres->str_snode[i]);
7582 break;
PavolVicanfcc98762017-09-01 15:51:39 +02007583 case UNRES_EXT_FINALIZE:
7584 free(unres->str_snode[i]);
Michal Vaskoede9c472016-06-07 09:38:15 +02007585 default:
7586 break;
7587 }
7588 unres->type[i] = UNRES_RESOLVED;
7589}
7590
Michal Vasko88c29542015-11-27 14:57:53 +01007591void
Michal Vasko44ab1462017-05-18 13:18:36 +02007592unres_schema_free(struct lys_module *module, struct unres_schema **unres, int all)
Michal Vasko88c29542015-11-27 14:57:53 +01007593{
7594 uint32_t i;
Radek Krejcic071c542016-01-27 14:57:51 +01007595 unsigned int unresolved = 0;
Michal Vasko88c29542015-11-27 14:57:53 +01007596
Radek Krejcic071c542016-01-27 14:57:51 +01007597 if (!unres || !(*unres)) {
7598 return;
Michal Vasko88c29542015-11-27 14:57:53 +01007599 }
7600
Michal Vasko44ab1462017-05-18 13:18:36 +02007601 assert(module || ((*unres)->count == 0));
Radek Krejcic071c542016-01-27 14:57:51 +01007602
7603 for (i = 0; i < (*unres)->count; ++i) {
Michal Vasko44ab1462017-05-18 13:18:36 +02007604 if (!all && ((*unres)->module[i] != module)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007605 if ((*unres)->type[i] != UNRES_RESOLVED) {
7606 unresolved++;
7607 }
7608 continue;
7609 }
Michal Vaskoede9c472016-06-07 09:38:15 +02007610
7611 /* free heap memory for the specific item */
7612 unres_schema_free_item(module->ctx, *unres, i);
Radek Krejcic071c542016-01-27 14:57:51 +01007613 }
7614
Michal Vaskoede9c472016-06-07 09:38:15 +02007615 /* free it all */
Michal Vasko44ab1462017-05-18 13:18:36 +02007616 if (!module || all || (!unresolved && !module->type)) {
Radek Krejcic071c542016-01-27 14:57:51 +01007617 free((*unres)->item);
7618 free((*unres)->type);
7619 free((*unres)->str_snode);
7620 free((*unres)->module);
Radek Krejcic071c542016-01-27 14:57:51 +01007621 free((*unres));
7622 (*unres) = NULL;
7623 }
Michal Vasko88c29542015-11-27 14:57:53 +01007624}
7625
Michal Vaskoff690e72017-08-03 14:25:07 +02007626/* check whether instance-identifier points outside its data subtree (for operation it is any node
7627 * outside the operation subtree, otherwise it is a node from a foreign model) */
Michal Vasko3cfa3182017-01-17 10:00:58 +01007628static int
7629check_instid_ext_dep(const struct lys_node *sleaf, const char *json_instid)
7630{
Michal Vaskoff690e72017-08-03 14:25:07 +02007631 const struct lys_node *op_node, *first_node;
Michal Vasko53b7da02018-02-13 15:28:42 +01007632 enum int_log_opts prev_ilo;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007633 char *buf;
Michal Vasko53b7da02018-02-13 15:28:42 +01007634 int ret = 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007635
Radek Krejci034cb102017-08-01 15:45:13 +02007636 if (!json_instid || !json_instid[0]) {
7637 /* no/empty value */
7638 return 0;
7639 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007640
7641 for (op_node = lys_parent(sleaf);
7642 op_node && !(op_node->nodetype & (LYS_NOTIF | LYS_RPC | LYS_ACTION));
7643 op_node = lys_parent(op_node));
7644
7645 if (op_node && lys_parent(op_node)) {
7646 /* nested operation - any absolute path is external */
7647 return 1;
7648 }
7649
7650 /* get the first node from the instid */
7651 buf = strndup(json_instid, strchr(json_instid + 1, '/') - json_instid);
7652 if (!buf) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007653 /* so that we do not have to bother with logging, say it is not external */
7654 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007655 }
7656
Michal Vaskoff690e72017-08-03 14:25:07 +02007657 /* find the first schema node, do not log */
Michal Vasko53b7da02018-02-13 15:28:42 +01007658 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoff690e72017-08-03 14:25:07 +02007659 first_node = ly_ctx_get_node(NULL, sleaf, buf, 0);
Michal Vasko53b7da02018-02-13 15:28:42 +01007660 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko3cfa3182017-01-17 10:00:58 +01007661
Michal Vasko53b7da02018-02-13 15:28:42 +01007662 free(buf);
Michal Vaskoff690e72017-08-03 14:25:07 +02007663 if (!first_node) {
7664 /* unknown path, say it is not external */
Michal Vaskoff690e72017-08-03 14:25:07 +02007665 return 0;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007666 }
Michal Vasko3cfa3182017-01-17 10:00:58 +01007667
7668 /* based on the first schema node in the path we can decide whether it points to an external tree or not */
7669
Michal Vaskoff690e72017-08-03 14:25:07 +02007670 if (op_node && (op_node != first_node)) {
7671 /* it is a top-level operation, so we're good if it points somewhere inside it */
7672 ret = 1;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007673 }
7674
7675 /* we cannot know whether it points to a tree that is going to be unlinked (application must handle
7676 * this itself), so we say it's not external */
Radek Krejci81c38b82017-06-02 15:04:16 +02007677 return ret;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007678}
7679
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007680/**
7681 * @brief Resolve instance-identifier in JSON data format. Logs directly.
7682 *
7683 * @param[in] data Data node where the path is used
7684 * @param[in] path Instance-identifier node value.
7685 * @param[in,out] ret Resolved instance or NULL.
7686 *
7687 * @return 0 on success (even if unresolved and \p ret is NULL), -1 on error.
7688 */
7689static int
7690resolve_instid(struct lyd_node *data, const char *path, int req_inst, struct lyd_node **ret)
7691{
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007692 int i = 0, j, parsed, cur_idx;
Michal Vasko1b6ca962017-08-03 14:23:09 +02007693 const struct lys_module *mod, *prev_mod = NULL;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007694 struct ly_ctx *ctx = data->schema->module->ctx;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007695 struct lyd_node *root, *node;
Radek Krejcidaa547a2017-09-22 15:56:27 +02007696 const char *model = NULL, *name;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007697 char *str;
7698 int mod_len, name_len, has_predicate;
7699 struct unres_data node_match;
7700
7701 memset(&node_match, 0, sizeof node_match);
7702 *ret = NULL;
7703
7704 /* we need root to resolve absolute path */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007705 for (root = data; root->parent; root = root->parent);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007706 /* we're still parsing it and the pointer is not correct yet */
Radek Krejci2c822ed2017-08-03 14:23:36 +02007707 if (root->prev) {
7708 for (; root->prev->next; root = root->prev);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007709 }
7710
7711 /* search for the instance node */
7712 while (path[i]) {
7713 j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate);
7714 if (j <= 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007715 LOGVAL(ctx, LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007716 goto error;
7717 }
7718 i += j;
7719
Michal Vasko1b6ca962017-08-03 14:23:09 +02007720 if (model) {
7721 str = strndup(model, mod_len);
7722 if (!str) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007723 LOGMEM(ctx);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007724 goto error;
Michal Vaskof53187d2017-01-13 13:23:14 +01007725 }
Radek Krejcidfb00d62017-09-06 09:39:35 +02007726 mod = ly_ctx_get_module(ctx, str, NULL, 1);
Michal Vasko1b6ca962017-08-03 14:23:09 +02007727 if (ctx->data_clb) {
7728 if (!mod) {
7729 mod = ctx->data_clb(ctx, str, NULL, 0, ctx->data_clb_data);
7730 } else if (!mod->implemented) {
7731 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
7732 }
7733 }
7734 free(str);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007735
Michal Vasko1b6ca962017-08-03 14:23:09 +02007736 if (!mod || !mod->implemented || mod->disabled) {
7737 break;
7738 }
7739 } else if (!prev_mod) {
7740 /* first iteration and we are missing module name */
Michal Vaskoaf8ec362018-03-28 09:08:09 +02007741 LOGVAL(ctx, LYE_INELEM_LEN, LY_VLOG_LYD, data, name_len, name);
7742 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Instance-identifier is missing prefix in the first node.");
Michal Vasko1b6ca962017-08-03 14:23:09 +02007743 goto error;
7744 } else {
7745 mod = prev_mod;
Michal Vaskof53187d2017-01-13 13:23:14 +01007746 }
7747
Radek Krejci2c822ed2017-08-03 14:23:36 +02007748 if (resolve_data(mod, name, name_len, root, &node_match)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007749 /* no instance exists */
7750 break;
7751 }
7752
7753 if (has_predicate) {
7754 /* we have predicate, so the current results must be list or leaf-list */
Radek Krejcidaa547a2017-09-22 15:56:27 +02007755 parsed = j = 0;
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007756 /* index of the current node (for lists with position predicates) */
7757 cur_idx = 1;
7758 while (j < (signed)node_match.count) {
7759 node = node_match.node[j];
7760 parsed = resolve_instid_predicate(mod, &path[i], &node, cur_idx);
7761 if (parsed < 1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007762 LOGVAL(ctx, LYE_INPRED, LY_VLOG_LYD, data, &path[i - parsed]);
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007763 goto error;
7764 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007765
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007766 if (!node) {
7767 /* current node does not satisfy the predicate */
7768 unres_data_del(&node_match, j);
7769 } else {
7770 ++j;
7771 }
7772 ++cur_idx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007773 }
Michal Vaskoa3ca4b92017-09-15 12:43:01 +02007774
7775 i += parsed;
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007776 } else if (node_match.count) {
7777 /* check that we are not addressing lists */
7778 for (j = 0; (unsigned)j < node_match.count; ++j) {
7779 if (node_match.node[j]->schema->nodetype == LYS_LIST) {
7780 unres_data_del(&node_match, j--);
7781 }
7782 }
7783 if (!node_match.count) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007784 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYD, data, "Instance identifier is missing list keys.");
Michal Vasko6f28e0f2017-04-18 15:14:13 +02007785 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007786 }
Michal Vasko1b6ca962017-08-03 14:23:09 +02007787
7788 prev_mod = mod;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007789 }
7790
7791 if (!node_match.count) {
7792 /* no instance exists */
7793 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007794 LOGVAL(ctx, LYE_NOREQINS, LY_VLOG_LYD, data, path);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007795 return EXIT_FAILURE;
7796 }
7797 LOGVRB("There is no instance of \"%s\", but it is not required.", path);
7798 return EXIT_SUCCESS;
7799 } else if (node_match.count > 1) {
7800 /* instance identifier must resolve to a single node */
Michal Vasko53b7da02018-02-13 15:28:42 +01007801 LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree");
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007802 goto error;
7803 } else {
7804 /* we have required result, remember it and cleanup */
7805 *ret = node_match.node[0];
7806 free(node_match.node);
7807 return EXIT_SUCCESS;
7808 }
7809
7810error:
7811 /* cleanup */
7812 free(node_match.node);
7813 return -1;
7814}
7815
7816static int
7817resolve_leafref(struct lyd_node_leaf_list *leaf, const char *path, int req_inst, struct lyd_node **ret)
Radek Krejci7de36cf2016-09-12 16:18:50 +02007818{
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007819 struct lyxp_set xp_set;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007820 uint32_t i;
7821
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007822 memset(&xp_set, 0, sizeof xp_set);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007823 *ret = NULL;
Radek Krejci7de36cf2016-09-12 16:18:50 +02007824
Michal Vaskoca16cb32017-07-10 11:50:33 +02007825 /* syntax was already checked, so just evaluate the path using standard XPath */
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007826 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 +02007827 return -1;
7828 }
7829
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007830 if (xp_set.type == LYXP_SET_NODE_SET) {
7831 for (i = 0; i < xp_set.used; ++i) {
7832 if ((xp_set.val.nodes[i].type != LYXP_NODE_ELEM) || !(xp_set.val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7833 continue;
7834 }
Michal Vaskoca16cb32017-07-10 11:50:33 +02007835
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007836 /* not that the value is already in canonical form since the parsers does the conversion,
7837 * so we can simply compare just the values */
7838 if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)xp_set.val.nodes[i].node)->value_str, 1)) {
7839 /* we have the match */
7840 *ret = xp_set.val.nodes[i].node;
7841 break;
7842 }
Radek Krejci7de36cf2016-09-12 16:18:50 +02007843 }
7844 }
7845
Michal Vaskob5f9ffb2018-08-06 09:01:27 +02007846 lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, (struct lyd_node *)leaf, NULL, 0);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007847
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007848 if (!*ret) {
Radek Krejci7de36cf2016-09-12 16:18:50 +02007849 /* reference not found */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007850 if (req_inst > -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01007851 LOGVAL(leaf->schema->module->ctx, LYE_NOLEAFREF, LY_VLOG_LYD, leaf, path, leaf->value_str);
Radek Krejci7de36cf2016-09-12 16:18:50 +02007852 return EXIT_FAILURE;
7853 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007854 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 +02007855 }
7856 }
7857
7858 return EXIT_SUCCESS;
7859}
7860
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007861/* 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 +01007862int
7863resolve_union(struct lyd_node_leaf_list *leaf, struct lys_type *type, int store, int ignore_fail,
7864 struct lys_type **resolved_type)
Radek Krejci9b6aad22016-09-20 15:55:51 +02007865{
Michal Vasko53b7da02018-02-13 15:28:42 +01007866 struct ly_ctx *ctx = leaf->schema->module->ctx;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007867 struct lys_type *t;
Michal Vasko3cfa3182017-01-17 10:00:58 +01007868 struct lyd_node *ret;
Michal Vasko53b7da02018-02-13 15:28:42 +01007869 enum int_log_opts prev_ilo;
7870 int found, success = 0, ext_dep, req_inst;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007871 const char *json_val = NULL;
Radek Krejci9b6aad22016-09-20 15:55:51 +02007872
7873 assert(type->base == LY_TYPE_UNION);
7874
Michal Vasko101658e2018-06-05 15:05:54 +02007875 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 +01007876 /* either NULL or instid previously converted to JSON */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01007877 json_val = lydict_insert(ctx, leaf->value.string, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007878 }
Michal Vasko1c8567a2017-01-05 13:42:27 +01007879
Michal Vaskofd6c6502017-01-06 12:15:41 +01007880 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007881 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, &((struct lys_node_leaf *)leaf->schema)->type);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007882 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vasko1c8567a2017-01-05 13:42:27 +01007883 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007884
7885 /* 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 +01007886 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007887
7888 t = NULL;
7889 found = 0;
7890 while ((t = lyp_get_next_union_type(type, t, &found))) {
7891 found = 0;
7892
7893 switch (t->base) {
7894 case LY_TYPE_LEAFREF:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007895 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
7896 req_inst = -1;
7897 } else {
7898 req_inst = t->info.lref.req;
7899 }
7900
7901 if (!resolve_leafref(leaf, t->info.lref.path, req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007902 if (store) {
7903 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
7904 /* valid resolved */
7905 leaf->value.leafref = ret;
7906 leaf->value_type = LY_TYPE_LEAFREF;
7907 } else {
7908 /* valid unresolved */
Michal Vasko53b7da02018-02-13 15:28:42 +01007909 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vasko35f46a82018-05-30 10:44:11 +02007910 if (!lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007911 return -1;
7912 }
Michal Vasko53b7da02018-02-13 15:28:42 +01007913 ly_ilo_change(NULL, ILO_IGNORE, &prev_ilo, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007914 }
7915 }
7916
7917 success = 1;
7918 }
7919 break;
7920 case LY_TYPE_INST:
Michal Vasko3cfa3182017-01-17 10:00:58 +01007921 ext_dep = check_instid_ext_dep(leaf->schema, (json_val ? json_val : leaf->value_str));
7922 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
7923 req_inst = -1;
7924 } else {
7925 req_inst = t->info.inst.req;
7926 }
7927
Michal Vaskod3a03112017-01-23 09:56:02 +01007928 if (!resolve_instid((struct lyd_node *)leaf, (json_val ? json_val : leaf->value_str), req_inst, &ret)) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007929 if (store) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01007930 if (ret && !ext_dep) {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007931 /* valid resolved */
7932 leaf->value.instance = ret;
7933 leaf->value_type = LY_TYPE_INST;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007934
Michal Vaskofd6c6502017-01-06 12:15:41 +01007935 if (json_val) {
7936 lydict_remove(leaf->schema->module->ctx, leaf->value_str);
7937 leaf->value_str = json_val;
7938 json_val = NULL;
7939 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007940 } else {
Michal Vaskofd6c6502017-01-06 12:15:41 +01007941 /* valid unresolved */
7942 if (json_val) {
7943 /* put the JSON val back */
7944 leaf->value.string = json_val;
7945 json_val = NULL;
7946 } else {
7947 leaf->value.instance = NULL;
7948 }
Michal Vasko70bf8e52018-03-26 11:32:33 +02007949 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02007950 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007951 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007952 }
7953
7954 success = 1;
7955 }
7956 break;
7957 default:
Michal Vasko35f46a82018-05-30 10:44:11 +02007958 if (lyp_parse_value(t, &leaf->value_str, NULL, leaf, NULL, NULL, store, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007959 success = 1;
7960 }
7961 break;
7962 }
7963
7964 if (success) {
7965 break;
7966 }
7967
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007968 /* erase possible present and invalid value data */
Michal Vaskofd6c6502017-01-06 12:15:41 +01007969 if (store) {
Michal Vasko70bf8e52018-03-26 11:32:33 +02007970 lyd_free_value(leaf->value, leaf->value_type, leaf->value_flags, t);
Michal Vaskofd6c6502017-01-06 12:15:41 +01007971 memset(&leaf->value, 0, sizeof leaf->value);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007972 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007973 }
7974
7975 /* turn logging back on */
Michal Vasko53b7da02018-02-13 15:28:42 +01007976 ly_ilo_restore(NULL, prev_ilo, NULL, 0);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007977
7978 if (json_val) {
7979 if (!success) {
7980 /* put the value back for now */
7981 assert(leaf->value_type == LY_TYPE_UNION);
7982 leaf->value.string = json_val;
7983 } else {
7984 /* value was ultimately useless, but we could not have known */
7985 lydict_remove(leaf->schema->module->ctx, json_val);
7986 }
7987 }
7988
Michal Vaskofd6c6502017-01-06 12:15:41 +01007989 if (success) {
7990 if (resolved_type) {
7991 *resolved_type = t;
7992 }
7993 } else if (!ignore_fail || !type->info.uni.has_ptr_type) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01007994 /* not found and it is required */
Michal Vasko53b7da02018-02-13 15:28:42 +01007995 LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, leaf, leaf->value_str ? leaf->value_str : "", leaf->schema->name);
Radek Krejci9b6aad22016-09-20 15:55:51 +02007996 return EXIT_FAILURE;
7997 }
7998
7999 return EXIT_SUCCESS;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008000
Radek Krejci9b6aad22016-09-20 15:55:51 +02008001}
8002
Michal Vasko8bcdf292015-08-19 14:04:43 +02008003/**
8004 * @brief Resolve a single unres data item. Logs directly.
8005 *
Michal Vaskocf024702015-10-08 15:01:42 +02008006 * @param[in] node Data node to resolve.
Michal Vaskocf024702015-10-08 15:01:42 +02008007 * @param[in] type Type of the unresolved item.
Michal Vasko3cfa3182017-01-17 10:00:58 +01008008 * @param[in] ignore_fail 0 - no, 1 - yes, 2 - yes, but only for external dependencies.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008009 *
8010 * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
8011 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +02008012int
Michal Vasko0b963112017-08-11 12:45:36 +02008013resolve_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 +02008014{
Michal Vasko3cfa3182017-01-17 10:00:58 +01008015 int rc, req_inst, ext_dep;
Michal Vasko83a6c462015-10-08 16:43:53 +02008016 struct lyd_node_leaf_list *leaf;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008017 struct lyd_node *ret;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008018 struct lys_node_leaf *sleaf;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008019
Michal Vasko83a6c462015-10-08 16:43:53 +02008020 leaf = (struct lyd_node_leaf_list *)node;
Michal Vaskocf024702015-10-08 15:01:42 +02008021 sleaf = (struct lys_node_leaf *)leaf->schema;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008022
Michal Vaskocf024702015-10-08 15:01:42 +02008023 switch (type) {
8024 case UNRES_LEAFREF:
8025 assert(sleaf->type.base == LY_TYPE_LEAFREF);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008026 assert(leaf->validity & LYD_VAL_LEAFREF);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008027 if ((ignore_fail == 1) || ((leaf->schema->flags & LYS_LEAFREF_DEP) && (ignore_fail == 2))) {
8028 req_inst = -1;
8029 } else {
8030 req_inst = sleaf->type.info.lref.req;
8031 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008032 rc = resolve_leafref(leaf, sleaf->type.info.lref.path, req_inst, &ret);
8033 if (!rc) {
Michal Vaskob1ac8722017-01-02 13:04:25 +01008034 if (ret && !(leaf->schema->flags & LYS_LEAFREF_DEP)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008035 /* valid resolved */
Michal Vasko70bf8e52018-03-26 11:32:33 +02008036 if (leaf->value_type == LY_TYPE_BITS) {
Michal Vasko1c8567a2017-01-05 13:42:27 +01008037 free(leaf->value.bit);
8038 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008039 leaf->value.leafref = ret;
8040 leaf->value_type = LY_TYPE_LEAFREF;
Michal Vasko101658e2018-06-05 15:05:54 +02008041 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008042 } else {
8043 /* valid unresolved */
Michal Vasko101658e2018-06-05 15:05:54 +02008044 if (!(leaf->value_flags & LY_VALUE_UNRES)) {
Michal Vasko35f46a82018-05-30 10:44:11 +02008045 if (!lyp_parse_value(&sleaf->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0, 0)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008046 return -1;
8047 }
8048 }
8049 }
8050 leaf->validity &= ~LYD_VAL_LEAFREF;
8051 } else {
8052 return rc;
8053 }
8054 break;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008055
Michal Vaskocf024702015-10-08 15:01:42 +02008056 case UNRES_INSTID:
Radek Krejci7de36cf2016-09-12 16:18:50 +02008057 assert(sleaf->type.base == LY_TYPE_INST);
Michal Vasko3cfa3182017-01-17 10:00:58 +01008058 ext_dep = check_instid_ext_dep(leaf->schema, leaf->value_str);
8059 if (ext_dep == -1) {
8060 return -1;
8061 }
8062
8063 if ((ignore_fail == 1) || (ext_dep && (ignore_fail == 2))) {
8064 req_inst = -1;
8065 } else {
8066 req_inst = sleaf->type.info.inst.req;
8067 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008068 rc = resolve_instid(node, leaf->value_str, req_inst, &ret);
8069 if (!rc) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008070 if (ret && !ext_dep) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008071 /* valid resolved */
8072 leaf->value.instance = ret;
8073 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008074 leaf->value_flags &= ~LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008075 } else {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008076 /* valid unresolved */
8077 leaf->value.instance = NULL;
Michal Vasko70bf8e52018-03-26 11:32:33 +02008078 leaf->value_type = LY_TYPE_INST;
Michal Vasko101658e2018-06-05 15:05:54 +02008079 leaf->value_flags |= LY_VALUE_UNRES;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008080 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008081 } else {
8082 return rc;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008083 }
Michal Vaskocf024702015-10-08 15:01:42 +02008084 break;
8085
Radek Krejci7de36cf2016-09-12 16:18:50 +02008086 case UNRES_UNION:
8087 assert(sleaf->type.base == LY_TYPE_UNION);
Michal Vaskofd6c6502017-01-06 12:15:41 +01008088 return resolve_union(leaf, &sleaf->type, 1, ignore_fail, NULL);
Radek Krejci7de36cf2016-09-12 16:18:50 +02008089
Michal Vaskocf024702015-10-08 15:01:42 +02008090 case UNRES_WHEN:
Michal Vasko0b963112017-08-11 12:45:36 +02008091 if ((rc = resolve_when(node, ignore_fail, failed_when))) {
Michal Vaskocf024702015-10-08 15:01:42 +02008092 return rc;
8093 }
8094 break;
8095
Michal Vaskobf19d252015-10-08 15:39:17 +02008096 case UNRES_MUST:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008097 if ((rc = resolve_must(node, 0, ignore_fail))) {
Michal Vaskoc8c810c2016-09-15 14:02:00 +02008098 return rc;
8099 }
8100 break;
8101
8102 case UNRES_MUST_INOUT:
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008103 if ((rc = resolve_must(node, 1, ignore_fail))) {
Michal Vaskobf19d252015-10-08 15:39:17 +02008104 return rc;
8105 }
8106 break;
8107
Michal Vaskocf024702015-10-08 15:01:42 +02008108 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01008109 LOGINT(NULL);
Michal Vasko8bcdf292015-08-19 14:04:43 +02008110 return -1;
8111 }
8112
8113 return EXIT_SUCCESS;
8114}
8115
8116/**
Radek Krejci0b7704f2016-03-18 12:16:14 +01008117 * @brief add data unres item
Michal Vasko8bcdf292015-08-19 14:04:43 +02008118 *
8119 * @param[in] unres Unres data structure to use.
Michal Vaskocf024702015-10-08 15:01:42 +02008120 * @param[in] node Data node to use.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008121 *
Radek Krejci0b7704f2016-03-18 12:16:14 +01008122 * @return 0 on success, -1 on error.
Michal Vasko8bcdf292015-08-19 14:04:43 +02008123 */
8124int
Radek Krejci0b7704f2016-03-18 12:16:14 +01008125unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type)
Michal Vasko8bcdf292015-08-19 14:04:43 +02008126{
Radek Krejci03b71f72016-03-16 11:10:09 +01008127 assert(unres && node);
Michal Vaskoc4280842016-04-19 16:10:42 +02008128 assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST)
Radek Krejcibacc7442016-10-27 13:39:56 +02008129 || (type == UNRES_MUST_INOUT) || (type == UNRES_UNION));
Michal Vasko8bcdf292015-08-19 14:04:43 +02008130
Radek Krejci03b71f72016-03-16 11:10:09 +01008131 unres->count++;
Michal Vasko253035f2015-12-17 16:58:13 +01008132 unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node);
Michal Vasko53b7da02018-02-13 15:28:42 +01008133 LY_CHECK_ERR_RETURN(!unres->node, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008134 unres->node[unres->count - 1] = node;
Michal Vasko253035f2015-12-17 16:58:13 +01008135 unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type);
Michal Vasko53b7da02018-02-13 15:28:42 +01008136 LY_CHECK_ERR_RETURN(!unres->type, LOGMEM(NULL), -1);
Michal Vaskocf024702015-10-08 15:01:42 +02008137 unres->type[unres->count - 1] = type;
Michal Vasko8bcdf292015-08-19 14:04:43 +02008138
Radek Krejci0b7704f2016-03-18 12:16:14 +01008139 if (type == UNRES_WHEN) {
8140 /* remove previous result */
8141 node->when_status = LYD_WHEN;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008142 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008143
Michal Vasko53b7da02018-02-13 15:28:42 +01008144 return 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008145}
8146
8147/**
8148 * @brief Resolve every unres data item in the structure. Logs directly.
8149 *
Michal Vasko660582a2018-03-19 10:10:08 +01008150 * If options include #LYD_OPT_TRUSTED, the data are considered trusted (must conditions are not expected,
8151 * unresolved leafrefs/instids are accepted, when conditions are normally resolved because at least some implicit
8152 * non-presence containers may need to be deleted).
Radek Krejci082c84f2016-10-17 16:33:06 +02008153 *
8154 * If options includes LYD_OPT_NOAUTODEL, the false resulting when condition on non-default nodes, the error is raised.
8155 *
Michal Vasko53b7da02018-02-13 15:28:42 +01008156 * @param[in] ctx Context used.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008157 * @param[in] unres Unres data structure to use.
Radek Krejci082c84f2016-10-17 16:33:06 +02008158 * @param[in,out] root Root node of the data tree, can be changed due to autodeletion.
8159 * @param[in] options Data options as described above.
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008160 *
8161 * @return EXIT_SUCCESS on success, -1 on error.
8162 */
8163int
Michal Vasko53b7da02018-02-13 15:28:42 +01008164resolve_unres_data(struct ly_ctx *ctx, struct unres_data *unres, struct lyd_node **root, int options)
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008165{
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008166 uint32_t i, j, first, resolved, del_items, stmt_count;
Michal Vasko3cfa3182017-01-17 10:00:58 +01008167 int rc, progress, ignore_fail;
Michal Vasko53b7da02018-02-13 15:28:42 +01008168 enum int_log_opts prev_ilo;
8169 struct ly_err_item *prev_eitem;
mohitarora2489837dc2018-05-01 15:09:36 +05308170 LY_ERR prev_ly_errno = ly_errno;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008171 struct lyd_node *parent;
Michal Vasko0b963112017-08-11 12:45:36 +02008172 struct lys_when *when;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008173
Radek Krejci082c84f2016-10-17 16:33:06 +02008174 assert(root);
Radek Krejci03b71f72016-03-16 11:10:09 +01008175 assert(unres);
Radek Krejci03b71f72016-03-16 11:10:09 +01008176
8177 if (!unres->count) {
8178 return EXIT_SUCCESS;
8179 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008180
Michal Vasko7fa93142018-03-19 09:59:10 +01008181 if (options & (LYD_OPT_NOTIF_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG | LYD_OPT_EDIT)) {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008182 ignore_fail = 1;
8183 } else if (options & LYD_OPT_NOEXTDEPS) {
8184 ignore_fail = 2;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008185 } else {
Michal Vasko3cfa3182017-01-17 10:00:58 +01008186 ignore_fail = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008187 }
8188
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008189 LOGVRB("Resolving unresolved data nodes and their constraints...");
Michal Vasko7fa93142018-03-19 09:59:10 +01008190 if (!ignore_fail) {
8191 /* remember logging state only if errors are generated and valid */
Michal Vasko7fa93142018-03-19 09:59:10 +01008192 ly_ilo_change(ctx, ILO_STORE, &prev_ilo, &prev_eitem);
8193 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008194
Michal Vasko7fa93142018-03-19 09:59:10 +01008195 /*
8196 * when-stmt first
8197 */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008198 first = 1;
8199 stmt_count = 0;
8200 resolved = 0;
8201 del_items = 0;
Radek Krejci010e54b2016-03-15 09:40:34 +01008202 do {
Michal Vasko7fa93142018-03-19 09:59:10 +01008203 if (!ignore_fail) {
8204 ly_err_free_next(ctx, prev_eitem);
8205 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008206 progress = 0;
Michal Vasko6df94132016-09-22 11:08:09 +02008207 for (i = 0; i < unres->count; i++) {
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008208 if (unres->type[i] != UNRES_WHEN) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008209 continue;
8210 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008211 if (first) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008212 /* count when-stmt nodes in unres list */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008213 stmt_count++;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008214 }
8215
8216 /* resolve when condition only when all parent when conditions are already resolved */
8217 for (parent = unres->node[i]->parent;
8218 parent && LYD_WHEN_DONE(parent->when_status);
8219 parent = parent->parent) {
8220 if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) {
8221 /* the parent node was already unlinked, do not resolve this node,
Michal Vaskoe446b092017-08-11 10:58:09 +02008222 * it will be removed anyway, so just mark it as resolved
Radek Krejci0b7704f2016-03-18 12:16:14 +01008223 */
8224 unres->node[i]->when_status |= LYD_WHEN_FALSE;
8225 unres->type[i] = UNRES_RESOLVED;
8226 resolved++;
8227 break;
8228 }
8229 }
8230 if (parent) {
8231 continue;
8232 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008233
Michal Vasko0b963112017-08-11 12:45:36 +02008234 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, &when);
Radek Krejci010e54b2016-03-15 09:40:34 +01008235 if (!rc) {
Michal Vasko0b963112017-08-11 12:45:36 +02008236 /* finish with error/delete the node only if when was false, an external dependency was not required,
8237 * or it was not provided (the flag would not be passed down otherwise, checked in upper functions) */
Michal Vaskoe446b092017-08-11 10:58:09 +02008238 if ((unres->node[i]->when_status & LYD_WHEN_FALSE)
Michal Vaskoc04173b2018-03-09 10:43:22 +01008239 && (!(when->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP)) || !(options & LYD_OPT_NOEXTDEPS))) {
Radek Krejci082c84f2016-10-17 16:33:06 +02008240 if ((options & LYD_OPT_NOAUTODEL) && !unres->node[i]->dflt) {
Radek Krejci03b71f72016-03-16 11:10:09 +01008241 /* false when condition */
Michal Vasko53b7da02018-02-13 15:28:42 +01008242 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008243 } /* follows else */
8244
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008245 /* auto-delete */
Michal Vasko53b7da02018-02-13 15:28:42 +01008246 LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(ctx), when->cond);
Michal Vaskoe31d34a2017-03-28 14:50:38 +02008247
Radek Krejci0c0086a2016-03-24 15:20:28 +01008248 /* only unlink now, the subtree can contain another nodes stored in the unres list */
8249 /* if it has parent non-presence containers that would be empty, we should actually
8250 * remove the container
8251 */
Radek Krejci2537fd32016-09-07 16:22:41 +02008252 for (parent = unres->node[i];
8253 parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER;
8254 parent = parent->parent) {
8255 if (((struct lys_node_container *)parent->parent->schema)->presence) {
8256 /* presence container */
8257 break;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008258 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008259 if (parent->next || parent->prev != parent) {
8260 /* non empty (the child we are in and we are going to remove is not the only child) */
8261 break;
8262 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008263 }
Radek Krejci2537fd32016-09-07 16:22:41 +02008264 unres->node[i] = parent;
Radek Krejci0c0086a2016-03-24 15:20:28 +01008265
Radek Krejci0c0086a2016-03-24 15:20:28 +01008266 if (*root && *root == unres->node[i]) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01008267 *root = (*root)->next;
Radek Krejci03b71f72016-03-16 11:10:09 +01008268 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008269
Radek Krejci0b7704f2016-03-18 12:16:14 +01008270 lyd_unlink(unres->node[i]);
8271 unres->type[i] = UNRES_DELETE;
8272 del_items++;
Radek Krejci51fd8162016-03-24 15:49:51 +01008273
8274 /* update the rest of unres items */
8275 for (j = 0; j < unres->count; j++) {
Radek Krejci3db819b2016-03-24 16:29:48 +01008276 if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) {
Radek Krejci51fd8162016-03-24 15:49:51 +01008277 continue;
8278 }
8279
8280 /* test if the node is in subtree to be deleted */
8281 for (parent = unres->node[j]; parent; parent = parent->parent) {
8282 if (parent == unres->node[i]) {
8283 /* yes, it is */
8284 unres->type[j] = UNRES_RESOLVED;
8285 resolved++;
8286 break;
8287 }
8288 }
8289 }
Radek Krejci0b7704f2016-03-18 12:16:14 +01008290 } else {
8291 unres->type[i] = UNRES_RESOLVED;
Radek Krejci03b71f72016-03-16 11:10:09 +01008292 }
Michal Vasko7fa93142018-03-19 09:59:10 +01008293 if (!ignore_fail) {
8294 ly_err_free_next(ctx, prev_eitem);
8295 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008296 resolved++;
8297 progress = 1;
8298 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008299 goto error;
Radek Krejci2467a492016-10-24 15:16:59 +02008300 } /* else forward reference */
Radek Krejci010e54b2016-03-15 09:40:34 +01008301 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008302 first = 0;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008303 } while (progress && resolved < stmt_count);
Radek Krejci010e54b2016-03-15 09:40:34 +01008304
Radek Krejci0b7704f2016-03-18 12:16:14 +01008305 /* do we have some unresolved when-stmt? */
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008306 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008307 goto error;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008308 }
8309
8310 for (i = 0; del_items && i < unres->count; i++) {
8311 /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */
8312 if (unres->type[i] != UNRES_DELETE) {
8313 continue;
8314 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01008315 if (!unres->node[i]) {
8316 unres->type[i] = UNRES_RESOLVED;
8317 del_items--;
8318 continue;
Radek Krejci0b7704f2016-03-18 12:16:14 +01008319 }
8320
8321 /* really remove the complete subtree */
8322 lyd_free(unres->node[i]);
8323 unres->type[i] = UNRES_RESOLVED;
8324 del_items--;
8325 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008326
Michal Vasko7fa93142018-03-19 09:59:10 +01008327 /*
8328 * now leafrefs
8329 */
8330 if (options & LYD_OPT_TRUSTED) {
8331 /* we want to attempt to resolve leafrefs */
8332 assert(!ignore_fail);
8333 ignore_fail = 1;
8334
8335 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8336 ly_errno = prev_ly_errno;
8337 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008338 first = 1;
8339 stmt_count = 0;
8340 resolved = 0;
8341 do {
8342 progress = 0;
8343 for (i = 0; i < unres->count; i++) {
8344 if (unres->type[i] != UNRES_LEAFREF) {
8345 continue;
8346 }
8347 if (first) {
8348 /* count leafref nodes in unres list */
8349 stmt_count++;
8350 }
8351
Michal Vasko0b963112017-08-11 12:45:36 +02008352 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008353 if (!rc) {
8354 unres->type[i] = UNRES_RESOLVED;
Michal Vasko7fa93142018-03-19 09:59:10 +01008355 if (!ignore_fail) {
8356 ly_err_free_next(ctx, prev_eitem);
8357 }
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008358 resolved++;
8359 progress = 1;
8360 } else if (rc == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008361 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008362 } /* else forward reference */
8363 }
8364 first = 0;
8365 } while (progress && resolved < stmt_count);
8366
8367 /* do we have some unresolved leafrefs? */
8368 if (stmt_count > resolved) {
Michal Vasko53b7da02018-02-13 15:28:42 +01008369 goto error;
Michal Vaskoc35d2a72017-05-09 14:21:30 +02008370 }
8371
Michal Vasko7fa93142018-03-19 09:59:10 +01008372 if (!ignore_fail) {
8373 /* log normally now, throw away irrelevant errors */
8374 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 0);
8375 ly_errno = prev_ly_errno;
8376 }
Radek Krejci010e54b2016-03-15 09:40:34 +01008377
Michal Vasko7fa93142018-03-19 09:59:10 +01008378 /*
8379 * rest
8380 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008381 for (i = 0; i < unres->count; ++i) {
Radek Krejci010e54b2016-03-15 09:40:34 +01008382 if (unres->type[i] == UNRES_RESOLVED) {
8383 continue;
8384 }
Radek Krejci082c84f2016-10-17 16:33:06 +02008385 assert(!(options & LYD_OPT_TRUSTED) || ((unres->type[i] != UNRES_MUST) && (unres->type[i] != UNRES_MUST_INOUT)));
Radek Krejci010e54b2016-03-15 09:40:34 +01008386
Michal Vasko0b963112017-08-11 12:45:36 +02008387 rc = resolve_unres_data_item(unres->node[i], unres->type[i], ignore_fail, NULL);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008388 if (rc) {
8389 /* since when was already resolved, a forward reference is an error */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008390 return -1;
8391 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01008392
8393 unres->type[i] = UNRES_RESOLVED;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008394 }
8395
Michal Vaskoa0ffcab2016-05-02 14:52:08 +02008396 LOGVRB("All data nodes and constraints resolved.");
Radek Krejci010e54b2016-03-15 09:40:34 +01008397 unres->count = 0;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008398 return EXIT_SUCCESS;
Michal Vasko53b7da02018-02-13 15:28:42 +01008399
8400error:
Michal Vasko7fa93142018-03-19 09:59:10 +01008401 if (!ignore_fail) {
8402 /* print all the new errors */
8403 ly_ilo_restore(ctx, prev_ilo, prev_eitem, 1);
8404 /* do not restore ly_errno, it was udpated properly */
8405 }
Michal Vasko53b7da02018-02-13 15:28:42 +01008406 return -1;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02008407}