blob: 0adc86a3146ea4754030e89bd9f87b989f093072 [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 +0200106API const struct lyd_node *
107lyd_search(const struct lyd_node *first, const struct lys_module *module,
108 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len)
109{
110 const struct lyd_node *node = NULL;
111 const struct lysc_node *snode;
112
113 LY_CHECK_ARG_RET(NULL, module, name, NULL);
114 if (!nodetype) {
115 nodetype = 0xffff;
116 }
117
118 LY_LIST_FOR(first, node) {
119 snode = node->schema;
120 if (!(snode->nodetype & nodetype)) {
121 continue;
122 }
123 if (snode->module != module) {
124 continue;
125 }
126
Radek Krejci7f9b6512019-09-18 13:11:09 +0200127 if (ly_strncmp(snode->name, name, name_len)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200128 continue;
129 }
130
131 if (value) {
132 if (snode->nodetype == LYS_LIST) {
133 /* TODO handle value as keys of the list instance */
134 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200135 if (ly_strncmp(((struct lyd_node_term*)node)->value.original, value, value_len)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200136 continue;
137 }
138 } else {
139 continue;
140 }
141 }
142
143 /* all criteria passed */
144 return node;
145 }
146 return NULL;
147}
148
Radek Krejci084289f2019-07-09 17:35:30 +0200149LY_ERR
Radek Krejci3c9758d2019-07-11 16:49:10 +0200150lyd_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 +0200151 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200152{
153 LY_ERR ret = LY_SUCCESS, rc;
154 struct ly_err_item *err = NULL;
155 struct ly_ctx *ctx;
156 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +0200157 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
158 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200159 assert(node);
160
161 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +0200162
Radek Krejci73dead22019-07-11 16:46:16 +0200163 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +0200164 if (!second) {
165 node->value.realtype = type;
166 }
Radek Krejci73dead22019-07-11 16:46:16 +0200167 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
168 trees ? (void*)node : (void*)node->schema, trees,
169 &node->value, NULL, &err);
170 if (rc == LY_EINCOMPLETE) {
171 ret = rc;
172 /* continue with storing, just remember what to return if storing is ok */
173 } else if (rc) {
174 ret = rc;
175 if (err) {
176 ly_err_print(err);
177 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
178 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200179 }
Radek Krejci73dead22019-07-11 16:46:16 +0200180 goto error;
Radek Krejci084289f2019-07-09 17:35:30 +0200181 }
182
183error:
184 return ret;
185}
186
Radek Krejci38d85362019-09-05 16:26:38 +0200187LY_ERR
188lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int dynamic, int second,
189 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees)
190{
191 LY_ERR ret = LY_SUCCESS, rc;
192 struct ly_err_item *err = NULL;
193 struct ly_ctx *ctx;
194 struct lyext_metadata *ant;
195 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
196 (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
197 assert(attr);
198
199 ctx = attr->parent->schema->module->ctx;
200 ant = attr->annotation->data;
201
202 if (!second) {
203 attr->value.realtype = ant->type;
204 }
205 rc = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
206 trees ? (void*)attr->parent : (void*)attr->parent->schema, trees,
207 &attr->value, NULL, &err);
208 if (rc == LY_EINCOMPLETE) {
209 ret = rc;
210 /* continue with storing, just remember what to return if storing is ok */
211 } else if (rc) {
212 ret = rc;
213 if (err) {
214 ly_err_print(err);
215 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
216 ly_err_free(err);
217 }
218 goto error;
219 }
220
221error:
222 return ret;
223}
224
Radek Krejci084289f2019-07-09 17:35:30 +0200225API LY_ERR
226lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
227 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
228{
229 LY_ERR rc = LY_SUCCESS;
230 struct ly_err_item *err = NULL;
231 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200232
233 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
234
235 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
236 LOGARG(ctx, node);
237 return LY_EINVAL;
238 }
239
240 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200241 /* just validate, no storing of enything */
242 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
243 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
244 if (rc == LY_EINCOMPLETE) {
245 /* actually success since we do not provide the context tree and call validation with
246 * LY_TYPE_OPTS_INCOMPLETE_DATA */
247 rc = LY_SUCCESS;
248 } else if (rc && err) {
249 if (ctx) {
250 /* log only in case the ctx was provided as input parameter */
251 ly_err_print(err);
252 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200253 }
Radek Krejci73dead22019-07-11 16:46:16 +0200254 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200255 }
256
257 return rc;
258}
259
260API LY_ERR
261lyd_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 +0200262 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 +0200263{
264 LY_ERR rc;
265 struct ly_err_item *err = NULL;
266 struct lysc_type *type;
Radek Krejci73dead22019-07-11 16:46:16 +0200267 int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200268
269 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
270
271 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200272 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
273 get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees,
274 NULL, NULL, &err);
275 if (rc == LY_EINCOMPLETE) {
276 return rc;
277 } else if (rc) {
278 if (err) {
279 if (ctx) {
280 ly_err_print(err);
281 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200282 }
Radek Krejci73dead22019-07-11 16:46:16 +0200283 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200284 }
Radek Krejci73dead22019-07-11 16:46:16 +0200285 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200286 }
287
288 return LY_SUCCESS;
289}
290
291API LY_ERR
292lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200293 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 +0200294{
295 LY_ERR ret = LY_SUCCESS, rc;
296 struct ly_err_item *err = NULL;
297 struct ly_ctx *ctx;
298 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200299 struct lyd_value data = {0};
Radek Krejci73dead22019-07-11 16:46:16 +0200300 int options = LY_TYPE_OPTS_STORE | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200301
302 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
303
304 ctx = node->schema->module->ctx;
305 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200306 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
307 trees, &data, NULL, &err);
308 if (rc == LY_EINCOMPLETE) {
309 ret = rc;
310 /* continue with comparing, just remember what to return if storing is ok */
311 } else if (rc) {
312 /* value to compare is invalid */
313 ret = LY_EINVAL;
314 if (err) {
315 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200316 }
Radek Krejci73dead22019-07-11 16:46:16 +0200317 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200318 }
319
320 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200321 if (type->plugin->compare(&node->value, &data)) {
322 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
323 ret = LY_EVALID;
324 }
Radek Krejci084289f2019-07-09 17:35:30 +0200325
326cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200327 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200328
329 return ret;
330}
331
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200332API const char *
333lyd_value2str(const struct lyd_node_term *node, int *dynamic)
334{
335 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
336
337 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
338}
339
340API const char *
341lyd_attr2str(const struct lyd_attr *attr, int *dynamic)
342{
343 LY_CHECK_ARG_RET(attr ? attr->parent->schema->module->ctx : NULL, attr, dynamic, NULL);
344
345 return attr->value.realtype->plugin->print(&attr->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
346}
347
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200348API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100349lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
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
Michal Vaskoa3881362020-01-21 15:57:35 +0100358#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200359 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200360 /* first item in trees is mandatory - the RPC/action request */
361 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
362 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
363 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
364 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200365 return NULL;
366 }
367 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200368
Radek Krejcie7b95092019-05-15 11:03:07 +0200369 if (options & LYD_OPT_DATA_TEMPLATE) {
370 yang_data_name = va_arg(ap, const char *);
371 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200372#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200373
374 if (!format) {
375 /* TODO try to detect format from the content */
376 }
377
378 switch (format) {
379 case LYD_XML:
Michal Vaskoa3881362020-01-21 15:57:35 +0100380 lyd_parse_xml(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200381 break;
382#if 0
383 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200384 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200385 break;
386 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200387 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200388 break;
389#endif
390 case LYD_UNKNOWN:
391 LOGINT(ctx);
392 break;
393 }
394
Radek Krejcie7b95092019-05-15 11:03:07 +0200395 return result;
396}
397
398API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100399lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200400{
401 struct lyd_node *result;
402 size_t length;
403 char *addr;
404
405 LY_CHECK_ARG_RET(ctx, ctx, NULL);
406 if (fd < 0) {
407 LOGARG(ctx, fd);
408 return NULL;
409 }
410
411 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100412 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200413 if (addr) {
414 ly_munmap(addr, length);
415 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200416
417 return result;
418}
419
420API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100421lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200422{
423 int fd;
424 struct lyd_node *result;
425 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200426
427 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
428
429 fd = open(path, O_RDONLY);
430 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
431
432 if (!format) {
433 /* unknown format - try to detect it from filename's suffix */
434 len = strlen(path);
435
436 /* ignore trailing whitespaces */
437 for (; len > 0 && isspace(path[len - 1]); len--);
438
439 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
440 format = LYD_XML;
441#if 0
442 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
443 format = LYD_JSON;
444 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
445 format = LYD_LYB;
446#endif
447 } /* else still unknown, try later to detect it from the content */
448 }
449
Michal Vaskoa3881362020-01-21 15:57:35 +0100450 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200451 close(fd);
452
453 return result;
454}
Radek Krejci084289f2019-07-09 17:35:30 +0200455
456API const struct lyd_node_term *
Radek Krejci576b23f2019-07-12 14:06:32 +0200457lyd_target(struct lyd_value_path *path, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200458{
459 unsigned int u, v, x;
460 const struct lyd_node *node = NULL, *parent = NULL, *start_search;
461 uint64_t pos = 1;
462
463 LY_CHECK_ARG_RET(NULL, path, trees, NULL);
464
465 LY_ARRAY_FOR(path, u) {
466 if (parent) {
467 start_search = lyd_node_children(parent);
468search_inner:
469 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
470 } else {
471 LY_ARRAY_FOR(trees, v) {
472 start_search = trees[v];
473search_toplevel:
474 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
475 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
476 if (node) {
477 break;
478 }
479 }
480 }
481 if (!node) {
482 return NULL;
483 }
484
485 /* check predicate if any */
486 LY_ARRAY_FOR(path[u].predicates, x) {
487 if (path[u].predicates[x].type == 0) {
488 /* position predicate */
489 if (pos != path[u].predicates[x].position) {
490 pos++;
491 goto search_repeat;
492 }
493 /* done, no more predicates are allowed here */
494 break;
495 } else if (path[u].predicates[x].type == 1) {
496 /* key-predicate */
497 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
498 const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module,
499 path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name),
500 LYS_LEAF, NULL, 0);
501 if (!key) {
502 /* probably error and we shouldn't be here due to previous checks when creating path */
503 goto search_repeat;
504 }
505 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
506 goto search_repeat;
507 }
508 } else if (path[u].predicates[x].type == 2) {
509 /* leaf-list-predicate */
510 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
511 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
512 goto search_repeat;
513 }
514 } else {
515 LOGINT(NULL);
516 }
517 }
518
519 parent = node;
520 }
521
522 return (const struct lyd_node_term*)node;
523
524search_repeat:
525 start_search = node->next;
526 if (parent) {
527 goto search_inner;
528 } else {
529 goto search_toplevel;
530 }
531}
532
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200533API LY_ERR
534lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
535{
536 const struct lyd_node *iter1, *iter2;
537 struct lyd_node_term *term1, *term2;
538 struct lyd_node_any *any1, *any2;
539 struct lysc_type *type;
540 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +0200541
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200542 if (!node1 || !node2) {
543 if (node1 == node2) {
544 return LY_SUCCESS;
545 } else {
546 return LY_ENOT;
547 }
548 }
549
550 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) {
551 return LY_ENOT;
552 }
553
554 if (node1->hash != node2->hash) {
555 return LY_ENOT;
556 }
557
558 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
559
560 switch (node1->schema->nodetype) {
561 case LYS_LEAF:
562 case LYS_LEAFLIST:
563 if (options & LYD_COMPARE_DEFAULTS) {
564 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
565 return LY_ENOT;
566 }
567 }
568
569 term1 = (struct lyd_node_term*)node1;
570 term2 = (struct lyd_node_term*)node2;
571 type = ((struct lysc_node_leaf*)node1->schema)->type;
572
573 return type->plugin->compare(&term1->value, &term2->value);
574 case LYS_CONTAINER:
575 if (options & LYD_COMPARE_DEFAULTS) {
576 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
577 return LY_ENOT;
578 }
579 }
580 if (options & LYD_COMPARE_FULL_RECURSION) {
581 iter1 = ((struct lyd_node_inner*)node1)->child;
582 iter2 = ((struct lyd_node_inner*)node2)->child;
583 goto all_children_compare;
584 }
585 return LY_SUCCESS;
586 case LYS_ACTION:
587 if (options & LYD_COMPARE_FULL_RECURSION) {
588 /* TODO action/RPC
589 goto all_children_compare;
590 */
591 }
592 return LY_SUCCESS;
593 case LYS_NOTIF:
594 if (options & LYD_COMPARE_FULL_RECURSION) {
595 /* TODO Notification
596 goto all_children_compare;
597 */
598 }
599 return LY_SUCCESS;
600 case LYS_LIST:
601 iter1 = ((struct lyd_node_inner*)node1)->child;
602 iter2 = ((struct lyd_node_inner*)node2)->child;
603
Radek Krejci0fe9b512019-07-26 17:51:05 +0200604 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200605 /* lists with keys, their equivalence is based on their keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200606 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
607 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
608 key = key->next) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200609 if (lyd_compare(iter1, iter2, options)) {
610 return LY_ENOT;
611 }
612 iter1 = iter1->next;
613 iter2 = iter2->next;
614 }
615 } else {
616 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
617
618all_children_compare:
619 if (!iter1 && !iter2) {
620 /* no children, nothing to compare */
621 return LY_SUCCESS;
622 }
623
624 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
625 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
626 return LY_ENOT;
627 }
628 }
629 if (iter1 || iter2) {
630 return LY_ENOT;
631 }
632 }
633 return LY_SUCCESS;
634 case LYS_ANYXML:
635 case LYS_ANYDATA:
636 any1 = (struct lyd_node_any*)node1;
637 any2 = (struct lyd_node_any*)node2;
638
639 if (any1->value_type != any2->value_type) {
640 return LY_ENOT;
641 }
642 switch (any1->value_type) {
643 case LYD_ANYDATA_DATATREE:
644 iter1 = any1->value.tree;
645 iter2 = any2->value.tree;
646 goto all_children_compare;
647 case LYD_ANYDATA_STRING:
648 case LYD_ANYDATA_XML:
649 case LYD_ANYDATA_JSON:
650 len1 = strlen(any1->value.str);
651 len2 = strlen(any2->value.str);
652 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
653 return LY_ENOT;
654 }
655 return LY_SUCCESS;
656#if 0 /* TODO LYB format */
657 case LYD_ANYDATA_LYB:
658 int len1 = lyd_lyb_data_length(any1->value.mem);
659 int len2 = lyd_lyb_data_length(any2->value.mem);
660 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
661 return LY_ENOT;
662 }
663 return LY_SUCCESS;
664#endif
665 }
666 }
667
668 LOGINT(node1->schema->module->ctx);
669 return LY_EINT;
670}
Radek Krejci22ebdba2019-07-25 13:59:43 +0200671
672/**
673 * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev
674 * sibling (if present).
675 *
676 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
677 */
678static struct lyd_node *
679lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options)
680{
681 struct ly_ctx *ctx;
682 struct lyd_node *dup = NULL;
683
684 LY_CHECK_ARG_RET(NULL, node, NULL);
685 ctx = node->schema->module->ctx;
686
687 switch (node->schema->nodetype) {
688 case LYS_ACTION:
689 case LYS_NOTIF:
690 case LYS_CONTAINER:
691 case LYS_LIST:
692 dup = calloc(1, sizeof(struct lyd_node_inner));
693 break;
694 case LYS_LEAF:
695 case LYS_LEAFLIST:
696 dup = calloc(1, sizeof(struct lyd_node_term));
697 break;
698 case LYS_ANYDATA:
699 case LYS_ANYXML:
700 dup = calloc(1, sizeof(struct lyd_node_any));
701 break;
702 default:
703 LOGINT(ctx);
704 goto error;
705 }
706
707 /* TODO implement LYD_DUP_WITH_WHEN */
708 dup->flags = node->flags;
709 dup->schema = node->schema;
710
711 /* interconnect the node at the end */
712 dup->parent = parent;
713 if (prev) {
714 dup->prev = prev;
715 prev->next = dup;
716 } else {
717 dup->prev = dup;
718 if (parent) {
719 parent->child = dup;
720 }
721 }
722 if (parent) {
723 parent->child->prev = dup;
724 } else if (prev) {
725 struct lyd_node *first;
726 for (first = prev; first->prev != prev; first = first->prev);
727 first->prev = dup;
728 }
729
730 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
731
732 /* nodetype-specific work */
733 if (dup->schema->nodetype & LYD_NODE_TERM) {
734 struct lyd_node_term *term = (struct lyd_node_term*)dup;
735 struct lyd_node_term *orig = (struct lyd_node_term*)node;
736
737 term->hash = orig->hash;
738 term->value.realtype = orig->value.realtype;
739 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value),
740 LOGERR(ctx, LY_EINT, "Value duplication failed."), error);
741 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
742 struct lyd_node_inner *inner = (struct lyd_node_inner*)dup;
743 struct lyd_node_inner *orig = (struct lyd_node_inner*)node;
744 struct lyd_node *child, *last = NULL;
745
746 if (options & LYD_DUP_RECURSIVE) {
747 /* duplicate all the children */
748 LY_LIST_FOR(orig->child, child) {
749 last = lyd_dup_recursive(child, inner, last, options);
750 LY_CHECK_GOTO(!last, error);
751 }
Radek Krejci0fe9b512019-07-26 17:51:05 +0200752 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200753 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200754 child = orig->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200755 for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child;
756 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
757 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200758 if (!child) {
759 /* possibly not keys are present in filtered tree */
760 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200761 } else if (child->schema != key) {
762 /* possibly not all keys are present in filtered tree,
763 * but there can be also some non-key nodes */
764 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200765 }
766 last = lyd_dup_recursive(child, inner, last, options);
767 child = child->next;
768 }
769 }
770 lyd_hash(dup);
771 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
772 struct lyd_node_any *any = (struct lyd_node_any*)dup;
773 struct lyd_node_any *orig = (struct lyd_node_any*)node;
774
775 any->hash = orig->hash;
776 any->value_type = orig->value_type;
777 switch (any->value_type) {
778 case LYD_ANYDATA_DATATREE:
779 if (orig->value.tree) {
780 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
781 LY_CHECK_GOTO(!any->value.tree, error);
782 }
783 break;
784 case LYD_ANYDATA_STRING:
785 case LYD_ANYDATA_XML:
786 case LYD_ANYDATA_JSON:
787 if (orig->value.str) {
788 any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str));
789 }
790 break;
791 }
792 }
793
794 lyd_insert_hash(dup);
795 return dup;
796
797error:
798 if (!parent && !prev) {
799 lyd_free_tree(dup);
800 }
801 return NULL;
802}
803
804API struct lyd_node *
805lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
806{
807 struct ly_ctx *ctx;
808 const struct lyd_node *orig; /* original node to be duplicated */
809 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
810 struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */
811 struct lyd_node *top = NULL; /* the most higher created node */
812 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
813 int keyless_parent_list = 0;
814
815 LY_CHECK_ARG_RET(NULL, node, NULL);
816 ctx = node->schema->module->ctx;
817
818 if (options & LYD_DUP_WITH_PARENTS) {
819 struct lyd_node_inner *orig_parent, *iter;
820 int repeat = 1;
821 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
822 if (parent && parent->schema == orig_parent->schema) {
823 /* stop creating parents, connect what we have into the provided parent */
824 iter = parent;
825 repeat = 0;
826 /* get know if there is a keyless list which we will have to rehash */
827 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200828 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200829 keyless_parent_list = 1;
830 break;
831 }
832 }
833 } else {
834 iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0);
835 LY_CHECK_GOTO(!iter, error);
836 }
837 if (!local_parent) {
838 local_parent = iter;
839 }
840 if (iter->child) {
841 /* 1) list - add after keys
842 * 2) provided parent with some children */
843 iter->child->prev->next = top;
844 if (top) {
845 top->prev = iter->child->prev;
846 iter->child->prev = top;
847 }
848 } else {
849 iter->child = top;
850 if (iter->schema->nodetype == LYS_LIST) {
851 /* keyless list - we will need to rehash it since we are going to add nodes into it */
852 keyless_parent_list = 1;
853 }
854 }
855 if (top) {
856 top->parent = iter;
857 }
858 top = (struct lyd_node*)iter;
859 }
860 if (repeat && parent) {
861 /* given parent and created parents chain actually do not interconnect */
862 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
863 goto error;
864 }
865 } else {
866 local_parent = parent;
867 }
868
869 if (local_parent && local_parent->child) {
870 last = local_parent->child->prev;
871 }
872
873 LY_LIST_FOR(node, orig) {
874 last = lyd_dup_recursive(orig, local_parent, last, options);
875 LY_CHECK_GOTO(!last, error);
876 if (!first) {
877 first = last;
878 }
879
880 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
881 break;
882 }
883 }
884 if (keyless_parent_list) {
885 /* rehash */
886 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200887 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200888 lyd_hash((struct lyd_node*)local_parent);
889 }
890 }
891 }
892 return first;
893
894error:
895 if (top) {
896 lyd_free_tree(top);
897 } else {
898 lyd_free_withsiblings(first);
899 }
900 return NULL;
901}
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200902
903static LY_ERR
904lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
905{
906 if (reqlen > *buflen) {
907 if (is_static) {
908 return LY_EINCOMPLETE;
909 }
910
911 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
912 if (!*buffer) {
913 return LY_EMEM;
914 }
915
916 *buflen = reqlen;
917 }
918
919 return LY_SUCCESS;
920}
921
922/**
923 * @brief Append all list key predicates to path.
924 *
925 * @param[in] node Node with keys to print.
926 * @param[in,out] buffer Buffer to print to.
927 * @param[in,out] buflen Current buffer length.
928 * @param[in,out] bufused Current number of characters used in @p buffer.
929 * @param[in] is_static Whether buffer is static or can be reallocated.
930 * @return LY_ERR
931 */
932static LY_ERR
933lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
934{
935 const struct lyd_node *key;
936 int dynamic = 0;
937 size_t len;
938 const char *val;
939 char quot;
940 LY_ERR rc;
941
942 for (key = lyd_node_children(node); key && (key->flags & LYS_KEY); key = key->next) {
943 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
944 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
945 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
946 if (rc != LY_SUCCESS) {
947 if (dynamic) {
948 free((char *)val);
949 }
950 return rc;
951 }
952
953 quot = '\'';
954 if (strchr(val, '\'')) {
955 quot = '"';
956 }
957 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
958
959 if (dynamic) {
960 free((char *)val);
961 }
962 }
963
964 return LY_SUCCESS;
965}
966
967/**
968 * @brief Append leaf-list value predicate to path.
969 *
970 * @param[in] node Node to print.
971 * @param[in,out] buffer Buffer to print to.
972 * @param[in,out] buflen Current buffer length.
973 * @param[in,out] bufused Current number of characters used in @p buffer.
974 * @param[in] is_static Whether buffer is static or can be reallocated.
975 * @return LY_ERR
976 */
977static LY_ERR
978lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
979{
980 int dynamic = 0;
981 size_t len;
982 const char *val;
983 char quot;
984 LY_ERR rc;
985
986 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
987 len = 4 + strlen(val) + 2;
988 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
989 if (rc != LY_SUCCESS) {
990 goto cleanup;
991 }
992
993 quot = '\'';
994 if (strchr(val, '\'')) {
995 quot = '"';
996 }
997 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
998
999cleanup:
1000 if (dynamic) {
1001 free((char *)val);
1002 }
1003 return rc;
1004}
1005
1006/**
1007 * @brief Append node position (relative to its other instances) predicate to path.
1008 *
1009 * @param[in] node Node to print.
1010 * @param[in,out] buffer Buffer to print to.
1011 * @param[in,out] buflen Current buffer length.
1012 * @param[in,out] bufused Current number of characters used in @p buffer.
1013 * @param[in] is_static Whether buffer is static or can be reallocated.
1014 * @return LY_ERR
1015 */
1016static LY_ERR
1017lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1018{
1019 const struct lyd_node *first, *iter;
1020 size_t len;
1021 int pos;
1022 char *val = NULL;
1023 LY_ERR rc;
1024
1025 if (node->parent) {
1026 first = node->parent->child;
1027 } else {
1028 for (first = node; node->prev->next; node = node->prev);
1029 }
1030 pos = 1;
1031 for (iter = first; iter != node; iter = iter->next) {
1032 if (iter->schema == node->schema) {
1033 ++pos;
1034 }
1035 }
1036 if (asprintf(&val, "%d", pos) == -1) {
1037 return LY_EMEM;
1038 }
1039
1040 len = 1 + strlen(val) + 1;
1041 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1042 if (rc != LY_SUCCESS) {
1043 goto cleanup;
1044 }
1045
1046 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
1047
1048cleanup:
1049 free(val);
1050 return rc;
1051}
1052
1053API char *
1054lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
1055{
1056 int is_static = 0, i, j, depth;
1057 size_t bufused = 1 /* ending zero */, len;
1058 const struct lyd_node *iter;
1059 const struct lys_module *mod;
1060 LY_ERR rc;
1061
1062 LY_CHECK_ARG_RET(NULL, node, NULL);
1063 if (buffer) {
1064 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
1065 is_static = 1;
1066 }
1067
1068 switch (pathtype) {
1069 case LYSC_PATH_LOG:
1070 depth = 0;
1071 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
1072 ++depth;
1073 }
1074
1075 i = depth + 1;
1076 goto iter_print;
1077 while (i) {
1078 /* find the right node */
1079 for (iter = node, j = 0; j < i; iter = (const struct lyd_node *)iter->parent, ++j);
1080iter_print:
1081 /* print prefix and name */
1082 mod = NULL;
1083 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
1084 mod = iter->schema->module;
1085 }
1086
1087 /* realloc string */
1088 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
1089 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
1090 if (rc != LY_SUCCESS) {
1091 break;
1092 }
1093
1094 /* print next node */
1095 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
1096
1097 switch (iter->schema->nodetype) {
1098 case LYS_LIST:
1099 if (iter->schema->flags & LYS_KEYLESS) {
1100 /* print its position */
1101 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1102 } else {
1103 /* print all list keys in predicates */
1104 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
1105 }
1106 break;
1107 case LYS_LEAFLIST:
1108 if (iter->schema->flags & LYS_CONFIG_W) {
1109 /* print leaf-list value */
1110 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
1111 } else {
1112 /* print its position */
1113 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1114 }
1115 break;
1116 default:
1117 /* nothing to print more */
1118 rc = LY_SUCCESS;
1119 break;
1120 }
1121 if (rc != LY_SUCCESS) {
1122 break;
1123 }
1124
1125 --i;
1126 }
1127 break;
1128 }
1129
1130 return buffer;
1131}