blob: bcb72ca3535747f98ff8366aaaf1be738cb5288f [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * 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
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejci084289f2019-07-09 17:35:30 +020017#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdarg.h>
22#include <stdint.h>
23#include <string.h>
24#include <unistd.h>
25
26#include "log.h"
27#include "tree.h"
28#include "tree_data.h"
29#include "tree_data_internal.h"
30#include "tree_schema.h"
31
Radek Krejci576b23f2019-07-12 14:06:32 +020032API void
33lyd_trees_free(const struct lyd_node **trees, int free_data)
34{
35 if (!trees) {
36 return;
37 }
38
39 if (free_data) {
40 unsigned int u;
41 LY_ARRAY_FOR(trees, u) {
42 lyd_free_all((struct lyd_node *)trees[u]);
43 }
44 }
45 LY_ARRAY_FREE(trees);
46}
47
48static const struct lyd_node *
49lyd_trees_getstart(const struct lyd_node *tree)
50{
51 if (!tree) {
52 return NULL;
53 }
54 while (tree->prev->next) {
55 tree = tree->prev;
56 }
57 return tree;
58}
59
60API const struct lyd_node **
61lyd_trees_new(size_t count, const struct lyd_node *tree, ...)
62{
63 LY_ERR ret;
64 const struct lyd_node **trees = NULL;
65 va_list ap;
66
67 LY_CHECK_ARG_RET(NULL, tree, count > 0, NULL);
68
69 va_start(ap, tree);
70
71 LY_ARRAY_CREATE_GOTO(tree->schema->module->ctx, trees, count, ret, error);
72 /* first, mandatory, tree to insert */
73 trees[0] = lyd_trees_getstart(tree);
74 LY_ARRAY_INCREMENT(trees);
75
76 /* variable arguments */
77 for (unsigned int u = 1; u < count; ++u) {
78 trees[u] = lyd_trees_getstart(va_arg(ap, const struct lyd_node *));
79 LY_ARRAY_INCREMENT(trees);
80 }
81
82 va_end(ap);
83 return trees;
84
85error:
86 (void)ret; /* unused */
87 lyd_trees_free(trees, 1);
88 va_end(ap);
89 return NULL;
90}
91
Radek Krejcie7b95092019-05-15 11:03:07 +020092static int
Radek Krejci357398c2019-05-27 14:15:01 +020093cmp_str(const char *refstr, const char *str, size_t str_len)
Radek Krejcie7b95092019-05-15 11:03:07 +020094{
95
Radek Krejci357398c2019-05-27 14:15:01 +020096 if (str_len) {
97 int r = strncmp(refstr, str, str_len);
98 if (!r && !refstr[str_len]) {
Radek Krejcie7b95092019-05-15 11:03:07 +020099 return 0;
100 } else {
101 return 1;
102 }
103 } else {
104 return strcmp(refstr, str);
105 }
106}
107
108API const struct lyd_node *
109lyd_search(const struct lyd_node *first, const struct lys_module *module,
110 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len)
111{
112 const struct lyd_node *node = NULL;
113 const struct lysc_node *snode;
114
115 LY_CHECK_ARG_RET(NULL, module, name, NULL);
116 if (!nodetype) {
117 nodetype = 0xffff;
118 }
119
120 LY_LIST_FOR(first, node) {
121 snode = node->schema;
122 if (!(snode->nodetype & nodetype)) {
123 continue;
124 }
125 if (snode->module != module) {
126 continue;
127 }
128
129 if (cmp_str(snode->name, name, name_len)) {
130 continue;
131 }
132
133 if (value) {
134 if (snode->nodetype == LYS_LIST) {
135 /* TODO handle value as keys of the list instance */
136 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
137 if (cmp_str(((struct lyd_node_term*)node)->value.canonized, value, value_len)) {
138 continue;
139 }
140 } else {
141 continue;
142 }
143 }
144
145 /* all criteria passed */
146 return node;
147 }
148 return NULL;
149}
150
Radek Krejci084289f2019-07-09 17:35:30 +0200151LY_ERR
Radek Krejci3c9758d2019-07-11 16:49:10 +0200152lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int dynamic, int second,
Radek Krejci576b23f2019-07-12 14:06:32 +0200153 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200154{
155 LY_ERR ret = LY_SUCCESS, rc;
156 struct ly_err_item *err = NULL;
157 struct ly_ctx *ctx;
158 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +0200159 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
160 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200161 assert(node);
162
163 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +0200164
Radek Krejci73dead22019-07-11 16:46:16 +0200165 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +0200166 if (!second) {
167 node->value.realtype = type;
168 }
Radek Krejci73dead22019-07-11 16:46:16 +0200169 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
170 trees ? (void*)node : (void*)node->schema, trees,
171 &node->value, NULL, &err);
172 if (rc == LY_EINCOMPLETE) {
173 ret = rc;
174 /* continue with storing, just remember what to return if storing is ok */
175 } else if (rc) {
176 ret = rc;
177 if (err) {
178 ly_err_print(err);
179 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
180 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200181 }
Radek Krejci73dead22019-07-11 16:46:16 +0200182 goto error;
Radek Krejci084289f2019-07-09 17:35:30 +0200183 }
184
185error:
186 return ret;
187}
188
189API LY_ERR
190lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
191 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
192{
193 LY_ERR rc = LY_SUCCESS;
194 struct ly_err_item *err = NULL;
195 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200196
197 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
198
199 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
200 LOGARG(ctx, node);
201 return LY_EINVAL;
202 }
203
204 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200205 /* just validate, no storing of enything */
206 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
207 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
208 if (rc == LY_EINCOMPLETE) {
209 /* actually success since we do not provide the context tree and call validation with
210 * LY_TYPE_OPTS_INCOMPLETE_DATA */
211 rc = LY_SUCCESS;
212 } else if (rc && err) {
213 if (ctx) {
214 /* log only in case the ctx was provided as input parameter */
215 ly_err_print(err);
216 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200217 }
Radek Krejci73dead22019-07-11 16:46:16 +0200218 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200219 }
220
221 return rc;
222}
223
224API LY_ERR
225lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200226 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200227{
228 LY_ERR rc;
229 struct ly_err_item *err = NULL;
230 struct lysc_type *type;
Radek Krejci73dead22019-07-11 16:46:16 +0200231 int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200232
233 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
234
235 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200236 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
237 get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees,
238 NULL, NULL, &err);
239 if (rc == LY_EINCOMPLETE) {
240 return rc;
241 } else if (rc) {
242 if (err) {
243 if (ctx) {
244 ly_err_print(err);
245 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200246 }
Radek Krejci73dead22019-07-11 16:46:16 +0200247 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200248 }
Radek Krejci73dead22019-07-11 16:46:16 +0200249 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200250 }
251
252 return LY_SUCCESS;
253}
254
255API LY_ERR
256lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200257 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200258{
259 LY_ERR ret = LY_SUCCESS, rc;
260 struct ly_err_item *err = NULL;
261 struct ly_ctx *ctx;
262 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200263 struct lyd_value data = {0};
Radek Krejci73dead22019-07-11 16:46:16 +0200264 int options = LY_TYPE_OPTS_STORE | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200265
266 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
267
268 ctx = node->schema->module->ctx;
269 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200270 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
271 trees, &data, NULL, &err);
272 if (rc == LY_EINCOMPLETE) {
273 ret = rc;
274 /* continue with comparing, just remember what to return if storing is ok */
275 } else if (rc) {
276 /* value to compare is invalid */
277 ret = LY_EINVAL;
278 if (err) {
279 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200280 }
Radek Krejci73dead22019-07-11 16:46:16 +0200281 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200282 }
283
284 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200285 if (type->plugin->compare(&node->value, &data)) {
286 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
287 ret = LY_EVALID;
288 }
Radek Krejci084289f2019-07-09 17:35:30 +0200289
290cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200291 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200292
293 return ret;
294}
295
Radek Krejcie7b95092019-05-15 11:03:07 +0200296static struct lyd_node *
297lyd_parse_mem_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap)
298{
Radek Krejcie7b95092019-05-15 11:03:07 +0200299 struct lyd_node *result = NULL;
300 const struct lyd_node *rpc_act = NULL, *data_tree = NULL, *iter;
Radek Krejcie92210c2019-05-17 15:53:35 +0200301#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200302 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200303#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200304
305 if (lyd_parse_check_options(ctx, options, __func__)) {
306 return NULL;
307 }
308
309 if (options & LYD_OPT_RPCREPLY) {
310 rpc_act = va_arg(ap, const struct lyd_node *);
311 if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
312 LOGERR(ctx, LY_EINVAL, "Data parser invalid variable parameter (const struct lyd_node *rpc_act).");
313 return NULL;
314 }
315 }
316 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) {
317 data_tree = va_arg(ap, const struct lyd_node *);
318 if (data_tree) {
319 if (options & LYD_OPT_NOEXTDEPS) {
320 LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree and LYD_OPT_NOEXTDEPS set).",
321 __func__);
322 return NULL;
323 }
324
325 LY_LIST_FOR(data_tree, iter) {
326 if (iter->parent) {
327 /* a sibling is not top-level */
328 LOGERR(ctx, LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__);
329 return NULL;
330 }
331 }
332
333 /* move it to the beginning */
334 for (; data_tree->prev->next; data_tree = data_tree->prev);
335
336 /* LYD_OPT_NOSIBLINGS cannot be set in this case */
337 if (options & LYD_OPT_NOSIBLINGS) {
338 LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__);
339 return NULL;
340 }
341 }
342 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200343#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200344 if (options & LYD_OPT_DATA_TEMPLATE) {
345 yang_data_name = va_arg(ap, const char *);
346 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200347#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200348
349 if (!format) {
350 /* TODO try to detect format from the content */
351 }
352
353 switch (format) {
354 case LYD_XML:
Radek Krejcie92210c2019-05-17 15:53:35 +0200355 lyd_parse_xml(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200356 break;
357#if 0
358 case LYD_JSON:
Radek Krejcie92210c2019-05-17 15:53:35 +0200359 lyd_parse_json(ctx, data, options, rpc_act, data_tree, yang_data_name);
Radek Krejcie7b95092019-05-15 11:03:07 +0200360 break;
361 case LYD_LYB:
Radek Krejcie92210c2019-05-17 15:53:35 +0200362 lyd_parse_lyb(ctx, data, options, data_tree, yang_data_name, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200363 break;
364#endif
365 case LYD_UNKNOWN:
366 LOGINT(ctx);
367 break;
368 }
369
Radek Krejcie7b95092019-05-15 11:03:07 +0200370 return result;
371}
372
373API struct lyd_node *
374lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...)
375{
376 va_list ap;
377 struct lyd_node *result;
378
379 va_start(ap, options);
380 result = lyd_parse_mem_(ctx, data, format, options, ap);
381 va_end(ap);
382
383 return result;
384}
385
386static struct lyd_node *
387lyd_parse_fd_(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, va_list ap)
388{
389 struct lyd_node *result;
390 size_t length;
391 char *addr;
392
393 LY_CHECK_ARG_RET(ctx, ctx, NULL);
394 if (fd < 0) {
395 LOGARG(ctx, fd);
396 return NULL;
397 }
398
399 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
400 result = lyd_parse_mem_(ctx, addr ? addr : "", format, options, ap);
Radek Krejcidf3da792019-05-17 10:32:24 +0200401 if (addr) {
402 ly_munmap(addr, length);
403 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200404
405 return result;
406}
407
408API struct lyd_node *
409lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...)
410{
411 struct lyd_node *ret;
412 va_list ap;
413
414 va_start(ap, options);
415 ret = lyd_parse_fd_(ctx, fd, format, options, ap);
416 va_end(ap);
417
418 return ret;
419}
420
421API struct lyd_node *
422lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...)
423{
424 int fd;
425 struct lyd_node *result;
426 size_t len;
427 va_list ap;
428
429 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
430
431 fd = open(path, O_RDONLY);
432 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
433
434 if (!format) {
435 /* unknown format - try to detect it from filename's suffix */
436 len = strlen(path);
437
438 /* ignore trailing whitespaces */
439 for (; len > 0 && isspace(path[len - 1]); len--);
440
441 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
442 format = LYD_XML;
443#if 0
444 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
445 format = LYD_JSON;
446 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
447 format = LYD_LYB;
448#endif
449 } /* else still unknown, try later to detect it from the content */
450 }
451
452 va_start(ap, options);
453 result = lyd_parse_fd_(ctx, fd, format, options, ap);
454
455 va_end(ap);
456 close(fd);
457
458 return result;
459}
Radek Krejci084289f2019-07-09 17:35:30 +0200460
461API const struct lyd_node_term *
Radek Krejci576b23f2019-07-12 14:06:32 +0200462lyd_target(struct lyd_value_path *path, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200463{
464 unsigned int u, v, x;
465 const struct lyd_node *node = NULL, *parent = NULL, *start_search;
466 uint64_t pos = 1;
467
468 LY_CHECK_ARG_RET(NULL, path, trees, NULL);
469
470 LY_ARRAY_FOR(path, u) {
471 if (parent) {
472 start_search = lyd_node_children(parent);
473search_inner:
474 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
475 } else {
476 LY_ARRAY_FOR(trees, v) {
477 start_search = trees[v];
478search_toplevel:
479 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
480 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
481 if (node) {
482 break;
483 }
484 }
485 }
486 if (!node) {
487 return NULL;
488 }
489
490 /* check predicate if any */
491 LY_ARRAY_FOR(path[u].predicates, x) {
492 if (path[u].predicates[x].type == 0) {
493 /* position predicate */
494 if (pos != path[u].predicates[x].position) {
495 pos++;
496 goto search_repeat;
497 }
498 /* done, no more predicates are allowed here */
499 break;
500 } else if (path[u].predicates[x].type == 1) {
501 /* key-predicate */
502 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
503 const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module,
504 path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name),
505 LYS_LEAF, NULL, 0);
506 if (!key) {
507 /* probably error and we shouldn't be here due to previous checks when creating path */
508 goto search_repeat;
509 }
510 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
511 goto search_repeat;
512 }
513 } else if (path[u].predicates[x].type == 2) {
514 /* leaf-list-predicate */
515 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
516 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
517 goto search_repeat;
518 }
519 } else {
520 LOGINT(NULL);
521 }
522 }
523
524 parent = node;
525 }
526
527 return (const struct lyd_node_term*)node;
528
529search_repeat:
530 start_search = node->next;
531 if (parent) {
532 goto search_inner;
533 } else {
534 goto search_toplevel;
535 }
536}
537
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200538API LY_ERR
539lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
540{
541 const struct lyd_node *iter1, *iter2;
542 struct lyd_node_term *term1, *term2;
543 struct lyd_node_any *any1, *any2;
544 struct lysc_type *type;
545 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +0200546
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200547 if (!node1 || !node2) {
548 if (node1 == node2) {
549 return LY_SUCCESS;
550 } else {
551 return LY_ENOT;
552 }
553 }
554
555 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) {
556 return LY_ENOT;
557 }
558
559 if (node1->hash != node2->hash) {
560 return LY_ENOT;
561 }
562
563 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
564
565 switch (node1->schema->nodetype) {
566 case LYS_LEAF:
567 case LYS_LEAFLIST:
568 if (options & LYD_COMPARE_DEFAULTS) {
569 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
570 return LY_ENOT;
571 }
572 }
573
574 term1 = (struct lyd_node_term*)node1;
575 term2 = (struct lyd_node_term*)node2;
576 type = ((struct lysc_node_leaf*)node1->schema)->type;
577
578 return type->plugin->compare(&term1->value, &term2->value);
579 case LYS_CONTAINER:
580 if (options & LYD_COMPARE_DEFAULTS) {
581 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
582 return LY_ENOT;
583 }
584 }
585 if (options & LYD_COMPARE_FULL_RECURSION) {
586 iter1 = ((struct lyd_node_inner*)node1)->child;
587 iter2 = ((struct lyd_node_inner*)node2)->child;
588 goto all_children_compare;
589 }
590 return LY_SUCCESS;
591 case LYS_ACTION:
592 if (options & LYD_COMPARE_FULL_RECURSION) {
593 /* TODO action/RPC
594 goto all_children_compare;
595 */
596 }
597 return LY_SUCCESS;
598 case LYS_NOTIF:
599 if (options & LYD_COMPARE_FULL_RECURSION) {
600 /* TODO Notification
601 goto all_children_compare;
602 */
603 }
604 return LY_SUCCESS;
605 case LYS_LIST:
606 iter1 = ((struct lyd_node_inner*)node1)->child;
607 iter2 = ((struct lyd_node_inner*)node2)->child;
608
Radek Krejcif6aa45c2019-07-23 16:59:14 +0200609 if (((struct lysc_node_list*)node1->schema)->keys && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200610 /* lists with keys, their equivalence is based on their keys */
611 unsigned int u;
612
613 LY_ARRAY_FOR(((struct lysc_node_list*)node1->schema)->keys, u) {
614 if (lyd_compare(iter1, iter2, options)) {
615 return LY_ENOT;
616 }
617 iter1 = iter1->next;
618 iter2 = iter2->next;
619 }
620 } else {
621 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
622
623all_children_compare:
624 if (!iter1 && !iter2) {
625 /* no children, nothing to compare */
626 return LY_SUCCESS;
627 }
628
629 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
630 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
631 return LY_ENOT;
632 }
633 }
634 if (iter1 || iter2) {
635 return LY_ENOT;
636 }
637 }
638 return LY_SUCCESS;
639 case LYS_ANYXML:
640 case LYS_ANYDATA:
641 any1 = (struct lyd_node_any*)node1;
642 any2 = (struct lyd_node_any*)node2;
643
644 if (any1->value_type != any2->value_type) {
645 return LY_ENOT;
646 }
647 switch (any1->value_type) {
648 case LYD_ANYDATA_DATATREE:
649 iter1 = any1->value.tree;
650 iter2 = any2->value.tree;
651 goto all_children_compare;
652 case LYD_ANYDATA_STRING:
653 case LYD_ANYDATA_XML:
654 case LYD_ANYDATA_JSON:
655 len1 = strlen(any1->value.str);
656 len2 = strlen(any2->value.str);
657 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
658 return LY_ENOT;
659 }
660 return LY_SUCCESS;
661#if 0 /* TODO LYB format */
662 case LYD_ANYDATA_LYB:
663 int len1 = lyd_lyb_data_length(any1->value.mem);
664 int len2 = lyd_lyb_data_length(any2->value.mem);
665 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
666 return LY_ENOT;
667 }
668 return LY_SUCCESS;
669#endif
670 }
671 }
672
673 LOGINT(node1->schema->module->ctx);
674 return LY_EINT;
675}