blob: bc11889a8bd85ec55d27d8e833ce9ba648285dc2 [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) {
Radek Krejci950f6a52019-09-12 17:15:32 +0200111 if (!strncmp(refstr, str, str_len) && !refstr[str_len]) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200112 return 0;
113 } else {
114 return 1;
115 }
116 } else {
117 return strcmp(refstr, str);
118 }
119}
120
121API const struct lyd_node *
122lyd_search(const struct lyd_node *first, const struct lys_module *module,
123 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len)
124{
125 const struct lyd_node *node = NULL;
126 const struct lysc_node *snode;
127
128 LY_CHECK_ARG_RET(NULL, module, name, NULL);
129 if (!nodetype) {
130 nodetype = 0xffff;
131 }
132
133 LY_LIST_FOR(first, node) {
134 snode = node->schema;
135 if (!(snode->nodetype & nodetype)) {
136 continue;
137 }
138 if (snode->module != module) {
139 continue;
140 }
141
142 if (cmp_str(snode->name, name, name_len)) {
143 continue;
144 }
145
146 if (value) {
147 if (snode->nodetype == LYS_LIST) {
148 /* TODO handle value as keys of the list instance */
149 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci950f6a52019-09-12 17:15:32 +0200150 if (cmp_str(((struct lyd_node_term*)node)->value.original, value, value_len)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200151 continue;
152 }
153 } else {
154 continue;
155 }
156 }
157
158 /* all criteria passed */
159 return node;
160 }
161 return NULL;
162}
163
Radek Krejci084289f2019-07-09 17:35:30 +0200164LY_ERR
Radek Krejci3c9758d2019-07-11 16:49:10 +0200165lyd_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 +0200166 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200167{
168 LY_ERR ret = LY_SUCCESS, rc;
169 struct ly_err_item *err = NULL;
170 struct ly_ctx *ctx;
171 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +0200172 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
173 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200174 assert(node);
175
176 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +0200177
Radek Krejci73dead22019-07-11 16:46:16 +0200178 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +0200179 if (!second) {
180 node->value.realtype = type;
181 }
Radek Krejci73dead22019-07-11 16:46:16 +0200182 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
183 trees ? (void*)node : (void*)node->schema, trees,
184 &node->value, NULL, &err);
185 if (rc == LY_EINCOMPLETE) {
186 ret = rc;
187 /* continue with storing, just remember what to return if storing is ok */
188 } else if (rc) {
189 ret = rc;
190 if (err) {
191 ly_err_print(err);
192 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
193 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200194 }
Radek Krejci73dead22019-07-11 16:46:16 +0200195 goto error;
Radek Krejci084289f2019-07-09 17:35:30 +0200196 }
197
198error:
199 return ret;
200}
201
Radek Krejci38d85362019-09-05 16:26:38 +0200202LY_ERR
203lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int dynamic, int second,
204 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
205{
206 LY_ERR ret = LY_SUCCESS, rc;
207 struct ly_err_item *err = NULL;
208 struct ly_ctx *ctx;
209 struct lyext_metadata *ant;
210 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
211 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
212 assert(attr);
213
214 ctx = attr->parent->schema->module->ctx;
215 ant = attr->annotation->data;
216
217 if (!second) {
218 attr->value.realtype = ant->type;
219 }
220 rc = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
221 trees ? (void*)attr->parent : (void*)attr->parent->schema, trees,
222 &attr->value, NULL, &err);
223 if (rc == LY_EINCOMPLETE) {
224 ret = rc;
225 /* continue with storing, just remember what to return if storing is ok */
226 } else if (rc) {
227 ret = rc;
228 if (err) {
229 ly_err_print(err);
230 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
231 ly_err_free(err);
232 }
233 goto error;
234 }
235
236error:
237 return ret;
238}
239
Radek Krejci084289f2019-07-09 17:35:30 +0200240API LY_ERR
241lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
242 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
243{
244 LY_ERR rc = LY_SUCCESS;
245 struct ly_err_item *err = NULL;
246 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200247
248 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
249
250 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
251 LOGARG(ctx, node);
252 return LY_EINVAL;
253 }
254
255 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200256 /* just validate, no storing of enything */
257 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
258 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
259 if (rc == LY_EINCOMPLETE) {
260 /* actually success since we do not provide the context tree and call validation with
261 * LY_TYPE_OPTS_INCOMPLETE_DATA */
262 rc = LY_SUCCESS;
263 } else if (rc && err) {
264 if (ctx) {
265 /* log only in case the ctx was provided as input parameter */
266 ly_err_print(err);
267 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200268 }
Radek Krejci73dead22019-07-11 16:46:16 +0200269 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200270 }
271
272 return rc;
273}
274
275API LY_ERR
276lyd_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 +0200277 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 +0200278{
279 LY_ERR rc;
280 struct ly_err_item *err = NULL;
281 struct lysc_type *type;
Radek Krejci73dead22019-07-11 16:46:16 +0200282 int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200283
284 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
285
286 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200287 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
288 get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees,
289 NULL, NULL, &err);
290 if (rc == LY_EINCOMPLETE) {
291 return rc;
292 } else if (rc) {
293 if (err) {
294 if (ctx) {
295 ly_err_print(err);
296 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200297 }
Radek Krejci73dead22019-07-11 16:46:16 +0200298 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200299 }
Radek Krejci73dead22019-07-11 16:46:16 +0200300 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200301 }
302
303 return LY_SUCCESS;
304}
305
306API LY_ERR
307lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200308 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 +0200309{
310 LY_ERR ret = LY_SUCCESS, rc;
311 struct ly_err_item *err = NULL;
312 struct ly_ctx *ctx;
313 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200314 struct lyd_value data = {0};
Radek Krejci73dead22019-07-11 16:46:16 +0200315 int options = LY_TYPE_OPTS_STORE | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200316
317 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
318
319 ctx = node->schema->module->ctx;
320 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200321 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
322 trees, &data, NULL, &err);
323 if (rc == LY_EINCOMPLETE) {
324 ret = rc;
325 /* continue with comparing, just remember what to return if storing is ok */
326 } else if (rc) {
327 /* value to compare is invalid */
328 ret = LY_EINVAL;
329 if (err) {
330 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200331 }
Radek Krejci73dead22019-07-11 16:46:16 +0200332 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200333 }
334
335 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200336 if (type->plugin->compare(&node->value, &data)) {
337 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
338 ret = LY_EVALID;
339 }
Radek Krejci084289f2019-07-09 17:35:30 +0200340
341cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200342 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200343
344 return ret;
345}
346
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200347API const char *
348lyd_value2str(const struct lyd_node_term *node, int *dynamic)
349{
350 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
351
352 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
353}
354
355API const char *
356lyd_attr2str(const struct lyd_attr *attr, int *dynamic)
357{
358 LY_CHECK_ARG_RET(attr ? attr->parent->schema->module->ctx : NULL, attr, dynamic, NULL);
359
360 return attr->value.realtype->plugin->print(&attr->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
361}
362
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200363API struct lyd_node *
364lyd_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 +0200365{
Radek Krejcie7b95092019-05-15 11:03:07 +0200366 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200367#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200368 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200369#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200370
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200371 LY_CHECK_ARG_RET(ctx, ctx, NULL);
372
373 if (lyd_parse_options_check(ctx, options, __func__)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200374 return NULL;
375 }
376
377 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200378 /* first item in trees is mandatory - the RPC/action request */
379 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
380 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
381 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
382 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200383 return NULL;
384 }
385 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200386
Radek Krejcie92210c2019-05-17 15:53:35 +0200387#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200388 if (options & LYD_OPT_DATA_TEMPLATE) {
389 yang_data_name = va_arg(ap, const char *);
390 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200391#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200392
393 if (!format) {
394 /* TODO try to detect format from the content */
395 }
396
397 switch (format) {
398 case LYD_XML:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200399 lyd_parse_xml(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200400 break;
401#if 0
402 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200403 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200404 break;
405 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200406 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200407 break;
408#endif
409 case LYD_UNKNOWN:
410 LOGINT(ctx);
411 break;
412 }
413
Radek Krejcie7b95092019-05-15 11:03:07 +0200414 return result;
415}
416
417API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200418lyd_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 +0200419{
420 struct lyd_node *result;
421 size_t length;
422 char *addr;
423
424 LY_CHECK_ARG_RET(ctx, ctx, NULL);
425 if (fd < 0) {
426 LOGARG(ctx, fd);
427 return NULL;
428 }
429
430 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200431 result = lyd_parse_mem(ctx, addr ? addr : "", format, options, trees);
Radek Krejcidf3da792019-05-17 10:32:24 +0200432 if (addr) {
433 ly_munmap(addr, length);
434 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200435
436 return result;
437}
438
439API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200440lyd_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 +0200441{
442 int fd;
443 struct lyd_node *result;
444 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200445
446 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
447
448 fd = open(path, O_RDONLY);
449 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
450
451 if (!format) {
452 /* unknown format - try to detect it from filename's suffix */
453 len = strlen(path);
454
455 /* ignore trailing whitespaces */
456 for (; len > 0 && isspace(path[len - 1]); len--);
457
458 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
459 format = LYD_XML;
460#if 0
461 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
462 format = LYD_JSON;
463 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
464 format = LYD_LYB;
465#endif
466 } /* else still unknown, try later to detect it from the content */
467 }
468
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200469 result = lyd_parse_fd(ctx, fd, format, options, trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200470 close(fd);
471
472 return result;
473}
Radek Krejci084289f2019-07-09 17:35:30 +0200474
475API const struct lyd_node_term *
Radek Krejci576b23f2019-07-12 14:06:32 +0200476lyd_target(struct lyd_value_path *path, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200477{
478 unsigned int u, v, x;
479 const struct lyd_node *node = NULL, *parent = NULL, *start_search;
480 uint64_t pos = 1;
481
482 LY_CHECK_ARG_RET(NULL, path, trees, NULL);
483
484 LY_ARRAY_FOR(path, u) {
485 if (parent) {
486 start_search = lyd_node_children(parent);
487search_inner:
488 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
489 } else {
490 LY_ARRAY_FOR(trees, v) {
491 start_search = trees[v];
492search_toplevel:
493 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
494 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
495 if (node) {
496 break;
497 }
498 }
499 }
500 if (!node) {
501 return NULL;
502 }
503
504 /* check predicate if any */
505 LY_ARRAY_FOR(path[u].predicates, x) {
506 if (path[u].predicates[x].type == 0) {
507 /* position predicate */
508 if (pos != path[u].predicates[x].position) {
509 pos++;
510 goto search_repeat;
511 }
512 /* done, no more predicates are allowed here */
513 break;
514 } else if (path[u].predicates[x].type == 1) {
515 /* key-predicate */
516 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
517 const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module,
518 path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name),
519 LYS_LEAF, NULL, 0);
520 if (!key) {
521 /* probably error and we shouldn't be here due to previous checks when creating path */
522 goto search_repeat;
523 }
524 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
525 goto search_repeat;
526 }
527 } else if (path[u].predicates[x].type == 2) {
528 /* leaf-list-predicate */
529 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
530 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
531 goto search_repeat;
532 }
533 } else {
534 LOGINT(NULL);
535 }
536 }
537
538 parent = node;
539 }
540
541 return (const struct lyd_node_term*)node;
542
543search_repeat:
544 start_search = node->next;
545 if (parent) {
546 goto search_inner;
547 } else {
548 goto search_toplevel;
549 }
550}
551
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200552API LY_ERR
553lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
554{
555 const struct lyd_node *iter1, *iter2;
556 struct lyd_node_term *term1, *term2;
557 struct lyd_node_any *any1, *any2;
558 struct lysc_type *type;
559 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +0200560
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200561 if (!node1 || !node2) {
562 if (node1 == node2) {
563 return LY_SUCCESS;
564 } else {
565 return LY_ENOT;
566 }
567 }
568
569 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) {
570 return LY_ENOT;
571 }
572
573 if (node1->hash != node2->hash) {
574 return LY_ENOT;
575 }
576
577 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
578
579 switch (node1->schema->nodetype) {
580 case LYS_LEAF:
581 case LYS_LEAFLIST:
582 if (options & LYD_COMPARE_DEFAULTS) {
583 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
584 return LY_ENOT;
585 }
586 }
587
588 term1 = (struct lyd_node_term*)node1;
589 term2 = (struct lyd_node_term*)node2;
590 type = ((struct lysc_node_leaf*)node1->schema)->type;
591
592 return type->plugin->compare(&term1->value, &term2->value);
593 case LYS_CONTAINER:
594 if (options & LYD_COMPARE_DEFAULTS) {
595 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
596 return LY_ENOT;
597 }
598 }
599 if (options & LYD_COMPARE_FULL_RECURSION) {
600 iter1 = ((struct lyd_node_inner*)node1)->child;
601 iter2 = ((struct lyd_node_inner*)node2)->child;
602 goto all_children_compare;
603 }
604 return LY_SUCCESS;
605 case LYS_ACTION:
606 if (options & LYD_COMPARE_FULL_RECURSION) {
607 /* TODO action/RPC
608 goto all_children_compare;
609 */
610 }
611 return LY_SUCCESS;
612 case LYS_NOTIF:
613 if (options & LYD_COMPARE_FULL_RECURSION) {
614 /* TODO Notification
615 goto all_children_compare;
616 */
617 }
618 return LY_SUCCESS;
619 case LYS_LIST:
620 iter1 = ((struct lyd_node_inner*)node1)->child;
621 iter2 = ((struct lyd_node_inner*)node2)->child;
622
Radek Krejci0fe9b512019-07-26 17:51:05 +0200623 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200624 /* lists with keys, their equivalence is based on their keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200625 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
626 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
627 key = key->next) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200628 if (lyd_compare(iter1, iter2, options)) {
629 return LY_ENOT;
630 }
631 iter1 = iter1->next;
632 iter2 = iter2->next;
633 }
634 } else {
635 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
636
637all_children_compare:
638 if (!iter1 && !iter2) {
639 /* no children, nothing to compare */
640 return LY_SUCCESS;
641 }
642
643 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
644 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
645 return LY_ENOT;
646 }
647 }
648 if (iter1 || iter2) {
649 return LY_ENOT;
650 }
651 }
652 return LY_SUCCESS;
653 case LYS_ANYXML:
654 case LYS_ANYDATA:
655 any1 = (struct lyd_node_any*)node1;
656 any2 = (struct lyd_node_any*)node2;
657
658 if (any1->value_type != any2->value_type) {
659 return LY_ENOT;
660 }
661 switch (any1->value_type) {
662 case LYD_ANYDATA_DATATREE:
663 iter1 = any1->value.tree;
664 iter2 = any2->value.tree;
665 goto all_children_compare;
666 case LYD_ANYDATA_STRING:
667 case LYD_ANYDATA_XML:
668 case LYD_ANYDATA_JSON:
669 len1 = strlen(any1->value.str);
670 len2 = strlen(any2->value.str);
671 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
672 return LY_ENOT;
673 }
674 return LY_SUCCESS;
675#if 0 /* TODO LYB format */
676 case LYD_ANYDATA_LYB:
677 int len1 = lyd_lyb_data_length(any1->value.mem);
678 int len2 = lyd_lyb_data_length(any2->value.mem);
679 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
680 return LY_ENOT;
681 }
682 return LY_SUCCESS;
683#endif
684 }
685 }
686
687 LOGINT(node1->schema->module->ctx);
688 return LY_EINT;
689}
Radek Krejci22ebdba2019-07-25 13:59:43 +0200690
691/**
692 * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev
693 * sibling (if present).
694 *
695 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
696 */
697static struct lyd_node *
698lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options)
699{
700 struct ly_ctx *ctx;
701 struct lyd_node *dup = NULL;
702
703 LY_CHECK_ARG_RET(NULL, node, NULL);
704 ctx = node->schema->module->ctx;
705
706 switch (node->schema->nodetype) {
707 case LYS_ACTION:
708 case LYS_NOTIF:
709 case LYS_CONTAINER:
710 case LYS_LIST:
711 dup = calloc(1, sizeof(struct lyd_node_inner));
712 break;
713 case LYS_LEAF:
714 case LYS_LEAFLIST:
715 dup = calloc(1, sizeof(struct lyd_node_term));
716 break;
717 case LYS_ANYDATA:
718 case LYS_ANYXML:
719 dup = calloc(1, sizeof(struct lyd_node_any));
720 break;
721 default:
722 LOGINT(ctx);
723 goto error;
724 }
725
726 /* TODO implement LYD_DUP_WITH_WHEN */
727 dup->flags = node->flags;
728 dup->schema = node->schema;
729
730 /* interconnect the node at the end */
731 dup->parent = parent;
732 if (prev) {
733 dup->prev = prev;
734 prev->next = dup;
735 } else {
736 dup->prev = dup;
737 if (parent) {
738 parent->child = dup;
739 }
740 }
741 if (parent) {
742 parent->child->prev = dup;
743 } else if (prev) {
744 struct lyd_node *first;
745 for (first = prev; first->prev != prev; first = first->prev);
746 first->prev = dup;
747 }
748
749 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
750
751 /* nodetype-specific work */
752 if (dup->schema->nodetype & LYD_NODE_TERM) {
753 struct lyd_node_term *term = (struct lyd_node_term*)dup;
754 struct lyd_node_term *orig = (struct lyd_node_term*)node;
755
756 term->hash = orig->hash;
757 term->value.realtype = orig->value.realtype;
758 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value),
759 LOGERR(ctx, LY_EINT, "Value duplication failed."), error);
760 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
761 struct lyd_node_inner *inner = (struct lyd_node_inner*)dup;
762 struct lyd_node_inner *orig = (struct lyd_node_inner*)node;
763 struct lyd_node *child, *last = NULL;
764
765 if (options & LYD_DUP_RECURSIVE) {
766 /* duplicate all the children */
767 LY_LIST_FOR(orig->child, child) {
768 last = lyd_dup_recursive(child, inner, last, options);
769 LY_CHECK_GOTO(!last, error);
770 }
Radek Krejci0fe9b512019-07-26 17:51:05 +0200771 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200772 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200773 child = orig->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200774 for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child;
775 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
776 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200777 if (!child) {
778 /* possibly not keys are present in filtered tree */
779 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200780 } else if (child->schema != key) {
781 /* possibly not all keys are present in filtered tree,
782 * but there can be also some non-key nodes */
783 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200784 }
785 last = lyd_dup_recursive(child, inner, last, options);
786 child = child->next;
787 }
788 }
789 lyd_hash(dup);
790 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
791 struct lyd_node_any *any = (struct lyd_node_any*)dup;
792 struct lyd_node_any *orig = (struct lyd_node_any*)node;
793
794 any->hash = orig->hash;
795 any->value_type = orig->value_type;
796 switch (any->value_type) {
797 case LYD_ANYDATA_DATATREE:
798 if (orig->value.tree) {
799 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
800 LY_CHECK_GOTO(!any->value.tree, error);
801 }
802 break;
803 case LYD_ANYDATA_STRING:
804 case LYD_ANYDATA_XML:
805 case LYD_ANYDATA_JSON:
806 if (orig->value.str) {
807 any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str));
808 }
809 break;
810 }
811 }
812
813 lyd_insert_hash(dup);
814 return dup;
815
816error:
817 if (!parent && !prev) {
818 lyd_free_tree(dup);
819 }
820 return NULL;
821}
822
823API struct lyd_node *
824lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
825{
826 struct ly_ctx *ctx;
827 const struct lyd_node *orig; /* original node to be duplicated */
828 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
829 struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */
830 struct lyd_node *top = NULL; /* the most higher created node */
831 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
832 int keyless_parent_list = 0;
833
834 LY_CHECK_ARG_RET(NULL, node, NULL);
835 ctx = node->schema->module->ctx;
836
837 if (options & LYD_DUP_WITH_PARENTS) {
838 struct lyd_node_inner *orig_parent, *iter;
839 int repeat = 1;
840 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
841 if (parent && parent->schema == orig_parent->schema) {
842 /* stop creating parents, connect what we have into the provided parent */
843 iter = parent;
844 repeat = 0;
845 /* get know if there is a keyless list which we will have to rehash */
846 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200847 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200848 keyless_parent_list = 1;
849 break;
850 }
851 }
852 } else {
853 iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0);
854 LY_CHECK_GOTO(!iter, error);
855 }
856 if (!local_parent) {
857 local_parent = iter;
858 }
859 if (iter->child) {
860 /* 1) list - add after keys
861 * 2) provided parent with some children */
862 iter->child->prev->next = top;
863 if (top) {
864 top->prev = iter->child->prev;
865 iter->child->prev = top;
866 }
867 } else {
868 iter->child = top;
869 if (iter->schema->nodetype == LYS_LIST) {
870 /* keyless list - we will need to rehash it since we are going to add nodes into it */
871 keyless_parent_list = 1;
872 }
873 }
874 if (top) {
875 top->parent = iter;
876 }
877 top = (struct lyd_node*)iter;
878 }
879 if (repeat && parent) {
880 /* given parent and created parents chain actually do not interconnect */
881 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
882 goto error;
883 }
884 } else {
885 local_parent = parent;
886 }
887
888 if (local_parent && local_parent->child) {
889 last = local_parent->child->prev;
890 }
891
892 LY_LIST_FOR(node, orig) {
893 last = lyd_dup_recursive(orig, local_parent, last, options);
894 LY_CHECK_GOTO(!last, error);
895 if (!first) {
896 first = last;
897 }
898
899 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
900 break;
901 }
902 }
903 if (keyless_parent_list) {
904 /* rehash */
905 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200906 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200907 lyd_hash((struct lyd_node*)local_parent);
908 }
909 }
910 }
911 return first;
912
913error:
914 if (top) {
915 lyd_free_tree(top);
916 } else {
917 lyd_free_withsiblings(first);
918 }
919 return NULL;
920}
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200921
922static LY_ERR
923lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
924{
925 if (reqlen > *buflen) {
926 if (is_static) {
927 return LY_EINCOMPLETE;
928 }
929
930 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
931 if (!*buffer) {
932 return LY_EMEM;
933 }
934
935 *buflen = reqlen;
936 }
937
938 return LY_SUCCESS;
939}
940
941/**
942 * @brief Append all list key predicates to path.
943 *
944 * @param[in] node Node with keys to print.
945 * @param[in,out] buffer Buffer to print to.
946 * @param[in,out] buflen Current buffer length.
947 * @param[in,out] bufused Current number of characters used in @p buffer.
948 * @param[in] is_static Whether buffer is static or can be reallocated.
949 * @return LY_ERR
950 */
951static LY_ERR
952lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
953{
954 const struct lyd_node *key;
955 int dynamic = 0;
956 size_t len;
957 const char *val;
958 char quot;
959 LY_ERR rc;
960
961 for (key = lyd_node_children(node); key && (key->flags & LYS_KEY); key = key->next) {
962 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
963 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
964 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
965 if (rc != LY_SUCCESS) {
966 if (dynamic) {
967 free((char *)val);
968 }
969 return rc;
970 }
971
972 quot = '\'';
973 if (strchr(val, '\'')) {
974 quot = '"';
975 }
976 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
977
978 if (dynamic) {
979 free((char *)val);
980 }
981 }
982
983 return LY_SUCCESS;
984}
985
986/**
987 * @brief Append leaf-list value predicate to path.
988 *
989 * @param[in] node Node to print.
990 * @param[in,out] buffer Buffer to print to.
991 * @param[in,out] buflen Current buffer length.
992 * @param[in,out] bufused Current number of characters used in @p buffer.
993 * @param[in] is_static Whether buffer is static or can be reallocated.
994 * @return LY_ERR
995 */
996static LY_ERR
997lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
998{
999 int dynamic = 0;
1000 size_t len;
1001 const char *val;
1002 char quot;
1003 LY_ERR rc;
1004
1005 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
1006 len = 4 + strlen(val) + 2;
1007 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1008 if (rc != LY_SUCCESS) {
1009 goto cleanup;
1010 }
1011
1012 quot = '\'';
1013 if (strchr(val, '\'')) {
1014 quot = '"';
1015 }
1016 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
1017
1018cleanup:
1019 if (dynamic) {
1020 free((char *)val);
1021 }
1022 return rc;
1023}
1024
1025/**
1026 * @brief Append node position (relative to its other instances) predicate to path.
1027 *
1028 * @param[in] node Node to print.
1029 * @param[in,out] buffer Buffer to print to.
1030 * @param[in,out] buflen Current buffer length.
1031 * @param[in,out] bufused Current number of characters used in @p buffer.
1032 * @param[in] is_static Whether buffer is static or can be reallocated.
1033 * @return LY_ERR
1034 */
1035static LY_ERR
1036lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1037{
1038 const struct lyd_node *first, *iter;
1039 size_t len;
1040 int pos;
1041 char *val = NULL;
1042 LY_ERR rc;
1043
1044 if (node->parent) {
1045 first = node->parent->child;
1046 } else {
1047 for (first = node; node->prev->next; node = node->prev);
1048 }
1049 pos = 1;
1050 for (iter = first; iter != node; iter = iter->next) {
1051 if (iter->schema == node->schema) {
1052 ++pos;
1053 }
1054 }
1055 if (asprintf(&val, "%d", pos) == -1) {
1056 return LY_EMEM;
1057 }
1058
1059 len = 1 + strlen(val) + 1;
1060 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1061 if (rc != LY_SUCCESS) {
1062 goto cleanup;
1063 }
1064
1065 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
1066
1067cleanup:
1068 free(val);
1069 return rc;
1070}
1071
1072API char *
1073lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
1074{
1075 int is_static = 0, i, j, depth;
1076 size_t bufused = 1 /* ending zero */, len;
1077 const struct lyd_node *iter;
1078 const struct lys_module *mod;
1079 LY_ERR rc;
1080
1081 LY_CHECK_ARG_RET(NULL, node, NULL);
1082 if (buffer) {
1083 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
1084 is_static = 1;
1085 }
1086
1087 switch (pathtype) {
1088 case LYSC_PATH_LOG:
1089 depth = 0;
1090 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
1091 ++depth;
1092 }
1093
1094 i = depth + 1;
1095 goto iter_print;
1096 while (i) {
1097 /* find the right node */
1098 for (iter = node, j = 0; j < i; iter = (const struct lyd_node *)iter->parent, ++j);
1099iter_print:
1100 /* print prefix and name */
1101 mod = NULL;
1102 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
1103 mod = iter->schema->module;
1104 }
1105
1106 /* realloc string */
1107 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
1108 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
1109 if (rc != LY_SUCCESS) {
1110 break;
1111 }
1112
1113 /* print next node */
1114 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
1115
1116 switch (iter->schema->nodetype) {
1117 case LYS_LIST:
1118 if (iter->schema->flags & LYS_KEYLESS) {
1119 /* print its position */
1120 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1121 } else {
1122 /* print all list keys in predicates */
1123 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
1124 }
1125 break;
1126 case LYS_LEAFLIST:
1127 if (iter->schema->flags & LYS_CONFIG_W) {
1128 /* print leaf-list value */
1129 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
1130 } else {
1131 /* print its position */
1132 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1133 }
1134 break;
1135 default:
1136 /* nothing to print more */
1137 rc = LY_SUCCESS;
1138 break;
1139 }
1140 if (rc != LY_SUCCESS) {
1141 break;
1142 }
1143
1144 --i;
1145 }
1146 break;
1147 }
1148
1149 return buffer;
1150}