blob: 6c98e7d277aa908222f4b53dc7f1cb82cda6b4fa [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"
Radek Krejci38d85362019-09-05 16:26:38 +020031#include "plugins_exts_metadata.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032
Radek Krejci576b23f2019-07-12 14:06:32 +020033API void
34lyd_trees_free(const struct lyd_node **trees, int free_data)
35{
36 if (!trees) {
37 return;
38 }
39
40 if (free_data) {
41 unsigned int u;
42 LY_ARRAY_FOR(trees, u) {
43 lyd_free_all((struct lyd_node *)trees[u]);
44 }
45 }
46 LY_ARRAY_FREE(trees);
47}
48
49static const struct lyd_node *
50lyd_trees_getstart(const struct lyd_node *tree)
51{
52 if (!tree) {
53 return NULL;
54 }
55 while (tree->prev->next) {
56 tree = tree->prev;
57 }
58 return tree;
59}
60
61API const struct lyd_node **
62lyd_trees_new(size_t count, const struct lyd_node *tree, ...)
63{
64 LY_ERR ret;
65 const struct lyd_node **trees = NULL;
66 va_list ap;
67
68 LY_CHECK_ARG_RET(NULL, tree, count > 0, NULL);
69
70 va_start(ap, tree);
71
72 LY_ARRAY_CREATE_GOTO(tree->schema->module->ctx, trees, count, ret, error);
73 /* first, mandatory, tree to insert */
74 trees[0] = lyd_trees_getstart(tree);
75 LY_ARRAY_INCREMENT(trees);
76
77 /* variable arguments */
78 for (unsigned int u = 1; u < count; ++u) {
79 trees[u] = lyd_trees_getstart(va_arg(ap, const struct lyd_node *));
80 LY_ARRAY_INCREMENT(trees);
81 }
82
83 va_end(ap);
84 return trees;
85
86error:
87 (void)ret; /* unused */
88 lyd_trees_free(trees, 1);
89 va_end(ap);
90 return NULL;
91}
92
Radek Krejcif3b6fec2019-07-24 15:53:11 +020093API const struct lyd_node **
94lyd_trees_add(const struct lyd_node **trees, const struct lyd_node *tree)
95{
96 const struct lyd_node **t = NULL;
97
98 LY_CHECK_ARG_RET(NULL, tree, trees, trees);
99
100 LY_ARRAY_NEW_RET(tree->schema->module->ctx, trees, t, NULL);
101 *t = lyd_trees_getstart(tree);
102
103 return trees;
104}
105
Radek Krejcie7b95092019-05-15 11:03:07 +0200106static int
Radek Krejci357398c2019-05-27 14:15:01 +0200107cmp_str(const char *refstr, const char *str, size_t str_len)
Radek Krejcie7b95092019-05-15 11:03:07 +0200108{
109
Radek Krejci357398c2019-05-27 14:15:01 +0200110 if (str_len) {
111 int r = strncmp(refstr, str, str_len);
112 if (!r && !refstr[str_len]) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200113 return 0;
114 } else {
115 return 1;
116 }
117 } else {
118 return strcmp(refstr, str);
119 }
120}
121
122API const struct lyd_node *
123lyd_search(const struct lyd_node *first, const struct lys_module *module,
124 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len)
125{
126 const struct lyd_node *node = NULL;
127 const struct lysc_node *snode;
128
129 LY_CHECK_ARG_RET(NULL, module, name, NULL);
130 if (!nodetype) {
131 nodetype = 0xffff;
132 }
133
134 LY_LIST_FOR(first, node) {
135 snode = node->schema;
136 if (!(snode->nodetype & nodetype)) {
137 continue;
138 }
139 if (snode->module != module) {
140 continue;
141 }
142
143 if (cmp_str(snode->name, name, name_len)) {
144 continue;
145 }
146
147 if (value) {
148 if (snode->nodetype == LYS_LIST) {
149 /* TODO handle value as keys of the list instance */
150 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
151 if (cmp_str(((struct lyd_node_term*)node)->value.canonized, value, value_len)) {
152 continue;
153 }
154 } else {
155 continue;
156 }
157 }
158
159 /* all criteria passed */
160 return node;
161 }
162 return NULL;
163}
164
Radek Krejci084289f2019-07-09 17:35:30 +0200165LY_ERR
Radek Krejci3c9758d2019-07-11 16:49:10 +0200166lyd_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 +0200167 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200168{
169 LY_ERR ret = LY_SUCCESS, rc;
170 struct ly_err_item *err = NULL;
171 struct ly_ctx *ctx;
172 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +0200173 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
174 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200175 assert(node);
176
177 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +0200178
Radek Krejci73dead22019-07-11 16:46:16 +0200179 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +0200180 if (!second) {
181 node->value.realtype = type;
182 }
Radek Krejci73dead22019-07-11 16:46:16 +0200183 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
184 trees ? (void*)node : (void*)node->schema, trees,
185 &node->value, NULL, &err);
186 if (rc == LY_EINCOMPLETE) {
187 ret = rc;
188 /* continue with storing, just remember what to return if storing is ok */
189 } else if (rc) {
190 ret = rc;
191 if (err) {
192 ly_err_print(err);
193 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
194 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200195 }
Radek Krejci73dead22019-07-11 16:46:16 +0200196 goto error;
Radek Krejci084289f2019-07-09 17:35:30 +0200197 }
198
199error:
200 return ret;
201}
202
Radek Krejci38d85362019-09-05 16:26:38 +0200203LY_ERR
204lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int dynamic, int second,
205 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
206{
207 LY_ERR ret = LY_SUCCESS, rc;
208 struct ly_err_item *err = NULL;
209 struct ly_ctx *ctx;
210 struct lyext_metadata *ant;
211 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
212 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
213 assert(attr);
214
215 ctx = attr->parent->schema->module->ctx;
216 ant = attr->annotation->data;
217
218 if (!second) {
219 attr->value.realtype = ant->type;
220 }
221 rc = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
222 trees ? (void*)attr->parent : (void*)attr->parent->schema, trees,
223 &attr->value, NULL, &err);
224 if (rc == LY_EINCOMPLETE) {
225 ret = rc;
226 /* continue with storing, just remember what to return if storing is ok */
227 } else if (rc) {
228 ret = rc;
229 if (err) {
230 ly_err_print(err);
231 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
232 ly_err_free(err);
233 }
234 goto error;
235 }
236
237error:
238 return ret;
239}
240
Radek Krejci084289f2019-07-09 17:35:30 +0200241API LY_ERR
242lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
243 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
244{
245 LY_ERR rc = LY_SUCCESS;
246 struct ly_err_item *err = NULL;
247 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200248
249 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
250
251 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
252 LOGARG(ctx, node);
253 return LY_EINVAL;
254 }
255
256 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200257 /* just validate, no storing of enything */
258 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
259 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
260 if (rc == LY_EINCOMPLETE) {
261 /* actually success since we do not provide the context tree and call validation with
262 * LY_TYPE_OPTS_INCOMPLETE_DATA */
263 rc = LY_SUCCESS;
264 } else if (rc && err) {
265 if (ctx) {
266 /* log only in case the ctx was provided as input parameter */
267 ly_err_print(err);
268 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200269 }
Radek Krejci73dead22019-07-11 16:46:16 +0200270 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200271 }
272
273 return rc;
274}
275
276API LY_ERR
277lyd_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 +0200278 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 +0200279{
280 LY_ERR rc;
281 struct ly_err_item *err = NULL;
282 struct lysc_type *type;
Radek Krejci73dead22019-07-11 16:46:16 +0200283 int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200284
285 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
286
287 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200288 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
289 get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees,
290 NULL, NULL, &err);
291 if (rc == LY_EINCOMPLETE) {
292 return rc;
293 } else if (rc) {
294 if (err) {
295 if (ctx) {
296 ly_err_print(err);
297 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200298 }
Radek Krejci73dead22019-07-11 16:46:16 +0200299 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200300 }
Radek Krejci73dead22019-07-11 16:46:16 +0200301 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200302 }
303
304 return LY_SUCCESS;
305}
306
307API LY_ERR
308lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200309 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 +0200310{
311 LY_ERR ret = LY_SUCCESS, rc;
312 struct ly_err_item *err = NULL;
313 struct ly_ctx *ctx;
314 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200315 struct lyd_value data = {0};
Radek Krejci73dead22019-07-11 16:46:16 +0200316 int options = LY_TYPE_OPTS_STORE | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200317
318 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
319
320 ctx = node->schema->module->ctx;
321 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200322 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
323 trees, &data, NULL, &err);
324 if (rc == LY_EINCOMPLETE) {
325 ret = rc;
326 /* continue with comparing, just remember what to return if storing is ok */
327 } else if (rc) {
328 /* value to compare is invalid */
329 ret = LY_EINVAL;
330 if (err) {
331 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200332 }
Radek Krejci73dead22019-07-11 16:46:16 +0200333 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200334 }
335
336 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200337 if (type->plugin->compare(&node->value, &data)) {
338 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
339 ret = LY_EVALID;
340 }
Radek Krejci084289f2019-07-09 17:35:30 +0200341
342cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200343 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200344
345 return ret;
346}
347
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200348API struct lyd_node *
349lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, const struct lyd_node **trees)
Radek Krejcie7b95092019-05-15 11:03:07 +0200350{
Radek Krejcie7b95092019-05-15 11:03:07 +0200351 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200352#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200353 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200354#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200355
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200356 LY_CHECK_ARG_RET(ctx, ctx, NULL);
357
358 if (lyd_parse_options_check(ctx, options, __func__)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200359 return NULL;
360 }
361
362 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200363 /* first item in trees is mandatory - the RPC/action request */
364 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
365 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
366 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
367 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200368 return NULL;
369 }
370 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200371
Radek Krejcie92210c2019-05-17 15:53:35 +0200372#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200373 if (options & LYD_OPT_DATA_TEMPLATE) {
374 yang_data_name = va_arg(ap, const char *);
375 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200376#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200377
378 if (!format) {
379 /* TODO try to detect format from the content */
380 }
381
382 switch (format) {
383 case LYD_XML:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200384 lyd_parse_xml(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200385 break;
386#if 0
387 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200388 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200389 break;
390 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200391 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200392 break;
393#endif
394 case LYD_UNKNOWN:
395 LOGINT(ctx);
396 break;
397 }
398
Radek Krejcie7b95092019-05-15 11:03:07 +0200399 return result;
400}
401
402API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200403lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, const struct lyd_node **trees)
Radek Krejcie7b95092019-05-15 11:03:07 +0200404{
405 struct lyd_node *result;
406 size_t length;
407 char *addr;
408
409 LY_CHECK_ARG_RET(ctx, ctx, NULL);
410 if (fd < 0) {
411 LOGARG(ctx, fd);
412 return NULL;
413 }
414
415 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200416 result = lyd_parse_mem(ctx, addr ? addr : "", format, options, trees);
Radek Krejcidf3da792019-05-17 10:32:24 +0200417 if (addr) {
418 ly_munmap(addr, length);
419 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200420
421 return result;
422}
423
424API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200425lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, const struct lyd_node **trees)
Radek Krejcie7b95092019-05-15 11:03:07 +0200426{
427 int fd;
428 struct lyd_node *result;
429 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200430
431 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
432
433 fd = open(path, O_RDONLY);
434 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
435
436 if (!format) {
437 /* unknown format - try to detect it from filename's suffix */
438 len = strlen(path);
439
440 /* ignore trailing whitespaces */
441 for (; len > 0 && isspace(path[len - 1]); len--);
442
443 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
444 format = LYD_XML;
445#if 0
446 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
447 format = LYD_JSON;
448 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
449 format = LYD_LYB;
450#endif
451 } /* else still unknown, try later to detect it from the content */
452 }
453
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200454 result = lyd_parse_fd(ctx, fd, format, options, trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200455 close(fd);
456
457 return result;
458}
Radek Krejci084289f2019-07-09 17:35:30 +0200459
460API const struct lyd_node_term *
Radek Krejci576b23f2019-07-12 14:06:32 +0200461lyd_target(struct lyd_value_path *path, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200462{
463 unsigned int u, v, x;
464 const struct lyd_node *node = NULL, *parent = NULL, *start_search;
465 uint64_t pos = 1;
466
467 LY_CHECK_ARG_RET(NULL, path, trees, NULL);
468
469 LY_ARRAY_FOR(path, u) {
470 if (parent) {
471 start_search = lyd_node_children(parent);
472search_inner:
473 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
474 } else {
475 LY_ARRAY_FOR(trees, v) {
476 start_search = trees[v];
477search_toplevel:
478 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
479 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
480 if (node) {
481 break;
482 }
483 }
484 }
485 if (!node) {
486 return NULL;
487 }
488
489 /* check predicate if any */
490 LY_ARRAY_FOR(path[u].predicates, x) {
491 if (path[u].predicates[x].type == 0) {
492 /* position predicate */
493 if (pos != path[u].predicates[x].position) {
494 pos++;
495 goto search_repeat;
496 }
497 /* done, no more predicates are allowed here */
498 break;
499 } else if (path[u].predicates[x].type == 1) {
500 /* key-predicate */
501 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
502 const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module,
503 path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name),
504 LYS_LEAF, NULL, 0);
505 if (!key) {
506 /* probably error and we shouldn't be here due to previous checks when creating path */
507 goto search_repeat;
508 }
509 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
510 goto search_repeat;
511 }
512 } else if (path[u].predicates[x].type == 2) {
513 /* leaf-list-predicate */
514 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
515 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
516 goto search_repeat;
517 }
518 } else {
519 LOGINT(NULL);
520 }
521 }
522
523 parent = node;
524 }
525
526 return (const struct lyd_node_term*)node;
527
528search_repeat:
529 start_search = node->next;
530 if (parent) {
531 goto search_inner;
532 } else {
533 goto search_toplevel;
534 }
535}
536
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200537API LY_ERR
538lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
539{
540 const struct lyd_node *iter1, *iter2;
541 struct lyd_node_term *term1, *term2;
542 struct lyd_node_any *any1, *any2;
543 struct lysc_type *type;
544 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +0200545
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200546 if (!node1 || !node2) {
547 if (node1 == node2) {
548 return LY_SUCCESS;
549 } else {
550 return LY_ENOT;
551 }
552 }
553
554 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) {
555 return LY_ENOT;
556 }
557
558 if (node1->hash != node2->hash) {
559 return LY_ENOT;
560 }
561
562 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
563
564 switch (node1->schema->nodetype) {
565 case LYS_LEAF:
566 case LYS_LEAFLIST:
567 if (options & LYD_COMPARE_DEFAULTS) {
568 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
569 return LY_ENOT;
570 }
571 }
572
573 term1 = (struct lyd_node_term*)node1;
574 term2 = (struct lyd_node_term*)node2;
575 type = ((struct lysc_node_leaf*)node1->schema)->type;
576
577 return type->plugin->compare(&term1->value, &term2->value);
578 case LYS_CONTAINER:
579 if (options & LYD_COMPARE_DEFAULTS) {
580 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
581 return LY_ENOT;
582 }
583 }
584 if (options & LYD_COMPARE_FULL_RECURSION) {
585 iter1 = ((struct lyd_node_inner*)node1)->child;
586 iter2 = ((struct lyd_node_inner*)node2)->child;
587 goto all_children_compare;
588 }
589 return LY_SUCCESS;
590 case LYS_ACTION:
591 if (options & LYD_COMPARE_FULL_RECURSION) {
592 /* TODO action/RPC
593 goto all_children_compare;
594 */
595 }
596 return LY_SUCCESS;
597 case LYS_NOTIF:
598 if (options & LYD_COMPARE_FULL_RECURSION) {
599 /* TODO Notification
600 goto all_children_compare;
601 */
602 }
603 return LY_SUCCESS;
604 case LYS_LIST:
605 iter1 = ((struct lyd_node_inner*)node1)->child;
606 iter2 = ((struct lyd_node_inner*)node2)->child;
607
Radek Krejci0fe9b512019-07-26 17:51:05 +0200608 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200609 /* lists with keys, their equivalence is based on their keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200610 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
611 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
612 key = key->next) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200613 if (lyd_compare(iter1, iter2, options)) {
614 return LY_ENOT;
615 }
616 iter1 = iter1->next;
617 iter2 = iter2->next;
618 }
619 } else {
620 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
621
622all_children_compare:
623 if (!iter1 && !iter2) {
624 /* no children, nothing to compare */
625 return LY_SUCCESS;
626 }
627
628 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
629 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
630 return LY_ENOT;
631 }
632 }
633 if (iter1 || iter2) {
634 return LY_ENOT;
635 }
636 }
637 return LY_SUCCESS;
638 case LYS_ANYXML:
639 case LYS_ANYDATA:
640 any1 = (struct lyd_node_any*)node1;
641 any2 = (struct lyd_node_any*)node2;
642
643 if (any1->value_type != any2->value_type) {
644 return LY_ENOT;
645 }
646 switch (any1->value_type) {
647 case LYD_ANYDATA_DATATREE:
648 iter1 = any1->value.tree;
649 iter2 = any2->value.tree;
650 goto all_children_compare;
651 case LYD_ANYDATA_STRING:
652 case LYD_ANYDATA_XML:
653 case LYD_ANYDATA_JSON:
654 len1 = strlen(any1->value.str);
655 len2 = strlen(any2->value.str);
656 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
657 return LY_ENOT;
658 }
659 return LY_SUCCESS;
660#if 0 /* TODO LYB format */
661 case LYD_ANYDATA_LYB:
662 int len1 = lyd_lyb_data_length(any1->value.mem);
663 int len2 = lyd_lyb_data_length(any2->value.mem);
664 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
665 return LY_ENOT;
666 }
667 return LY_SUCCESS;
668#endif
669 }
670 }
671
672 LOGINT(node1->schema->module->ctx);
673 return LY_EINT;
674}
Radek Krejci22ebdba2019-07-25 13:59:43 +0200675
676/**
677 * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev
678 * sibling (if present).
679 *
680 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
681 */
682static struct lyd_node *
683lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options)
684{
685 struct ly_ctx *ctx;
686 struct lyd_node *dup = NULL;
687
688 LY_CHECK_ARG_RET(NULL, node, NULL);
689 ctx = node->schema->module->ctx;
690
691 switch (node->schema->nodetype) {
692 case LYS_ACTION:
693 case LYS_NOTIF:
694 case LYS_CONTAINER:
695 case LYS_LIST:
696 dup = calloc(1, sizeof(struct lyd_node_inner));
697 break;
698 case LYS_LEAF:
699 case LYS_LEAFLIST:
700 dup = calloc(1, sizeof(struct lyd_node_term));
701 break;
702 case LYS_ANYDATA:
703 case LYS_ANYXML:
704 dup = calloc(1, sizeof(struct lyd_node_any));
705 break;
706 default:
707 LOGINT(ctx);
708 goto error;
709 }
710
711 /* TODO implement LYD_DUP_WITH_WHEN */
712 dup->flags = node->flags;
713 dup->schema = node->schema;
714
715 /* interconnect the node at the end */
716 dup->parent = parent;
717 if (prev) {
718 dup->prev = prev;
719 prev->next = dup;
720 } else {
721 dup->prev = dup;
722 if (parent) {
723 parent->child = dup;
724 }
725 }
726 if (parent) {
727 parent->child->prev = dup;
728 } else if (prev) {
729 struct lyd_node *first;
730 for (first = prev; first->prev != prev; first = first->prev);
731 first->prev = dup;
732 }
733
734 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
735
736 /* nodetype-specific work */
737 if (dup->schema->nodetype & LYD_NODE_TERM) {
738 struct lyd_node_term *term = (struct lyd_node_term*)dup;
739 struct lyd_node_term *orig = (struct lyd_node_term*)node;
740
741 term->hash = orig->hash;
742 term->value.realtype = orig->value.realtype;
743 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value),
744 LOGERR(ctx, LY_EINT, "Value duplication failed."), error);
745 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
746 struct lyd_node_inner *inner = (struct lyd_node_inner*)dup;
747 struct lyd_node_inner *orig = (struct lyd_node_inner*)node;
748 struct lyd_node *child, *last = NULL;
749
750 if (options & LYD_DUP_RECURSIVE) {
751 /* duplicate all the children */
752 LY_LIST_FOR(orig->child, child) {
753 last = lyd_dup_recursive(child, inner, last, options);
754 LY_CHECK_GOTO(!last, error);
755 }
Radek Krejci0fe9b512019-07-26 17:51:05 +0200756 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200757 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200758 child = orig->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200759 for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child;
760 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
761 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200762 if (!child) {
763 /* possibly not keys are present in filtered tree */
764 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200765 } else if (child->schema != key) {
766 /* possibly not all keys are present in filtered tree,
767 * but there can be also some non-key nodes */
768 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200769 }
770 last = lyd_dup_recursive(child, inner, last, options);
771 child = child->next;
772 }
773 }
774 lyd_hash(dup);
775 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
776 struct lyd_node_any *any = (struct lyd_node_any*)dup;
777 struct lyd_node_any *orig = (struct lyd_node_any*)node;
778
779 any->hash = orig->hash;
780 any->value_type = orig->value_type;
781 switch (any->value_type) {
782 case LYD_ANYDATA_DATATREE:
783 if (orig->value.tree) {
784 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
785 LY_CHECK_GOTO(!any->value.tree, error);
786 }
787 break;
788 case LYD_ANYDATA_STRING:
789 case LYD_ANYDATA_XML:
790 case LYD_ANYDATA_JSON:
791 if (orig->value.str) {
792 any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str));
793 }
794 break;
795 }
796 }
797
798 lyd_insert_hash(dup);
799 return dup;
800
801error:
802 if (!parent && !prev) {
803 lyd_free_tree(dup);
804 }
805 return NULL;
806}
807
808API struct lyd_node *
809lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
810{
811 struct ly_ctx *ctx;
812 const struct lyd_node *orig; /* original node to be duplicated */
813 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
814 struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */
815 struct lyd_node *top = NULL; /* the most higher created node */
816 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
817 int keyless_parent_list = 0;
818
819 LY_CHECK_ARG_RET(NULL, node, NULL);
820 ctx = node->schema->module->ctx;
821
822 if (options & LYD_DUP_WITH_PARENTS) {
823 struct lyd_node_inner *orig_parent, *iter;
824 int repeat = 1;
825 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
826 if (parent && parent->schema == orig_parent->schema) {
827 /* stop creating parents, connect what we have into the provided parent */
828 iter = parent;
829 repeat = 0;
830 /* get know if there is a keyless list which we will have to rehash */
831 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200832 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200833 keyless_parent_list = 1;
834 break;
835 }
836 }
837 } else {
838 iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0);
839 LY_CHECK_GOTO(!iter, error);
840 }
841 if (!local_parent) {
842 local_parent = iter;
843 }
844 if (iter->child) {
845 /* 1) list - add after keys
846 * 2) provided parent with some children */
847 iter->child->prev->next = top;
848 if (top) {
849 top->prev = iter->child->prev;
850 iter->child->prev = top;
851 }
852 } else {
853 iter->child = top;
854 if (iter->schema->nodetype == LYS_LIST) {
855 /* keyless list - we will need to rehash it since we are going to add nodes into it */
856 keyless_parent_list = 1;
857 }
858 }
859 if (top) {
860 top->parent = iter;
861 }
862 top = (struct lyd_node*)iter;
863 }
864 if (repeat && parent) {
865 /* given parent and created parents chain actually do not interconnect */
866 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
867 goto error;
868 }
869 } else {
870 local_parent = parent;
871 }
872
873 if (local_parent && local_parent->child) {
874 last = local_parent->child->prev;
875 }
876
877 LY_LIST_FOR(node, orig) {
878 last = lyd_dup_recursive(orig, local_parent, last, options);
879 LY_CHECK_GOTO(!last, error);
880 if (!first) {
881 first = last;
882 }
883
884 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
885 break;
886 }
887 }
888 if (keyless_parent_list) {
889 /* rehash */
890 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200891 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200892 lyd_hash((struct lyd_node*)local_parent);
893 }
894 }
895 }
896 return first;
897
898error:
899 if (top) {
900 lyd_free_tree(top);
901 } else {
902 lyd_free_withsiblings(first);
903 }
904 return NULL;
905}