blob: e3f2b2917185f856ed8377da356dacabe409033f [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
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200348API const char *
349lyd_value2str(const struct lyd_node_term *node, int *dynamic)
350{
351 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
352
353 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
354}
355
356API const char *
357lyd_attr2str(const struct lyd_attr *attr, int *dynamic)
358{
359 LY_CHECK_ARG_RET(attr ? attr->parent->schema->module->ctx : NULL, attr, dynamic, NULL);
360
361 return attr->value.realtype->plugin->print(&attr->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
362}
363
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200364API struct lyd_node *
365lyd_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 +0200366{
Radek Krejcie7b95092019-05-15 11:03:07 +0200367 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200368#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200369 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200370#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200371
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200372 LY_CHECK_ARG_RET(ctx, ctx, NULL);
373
374 if (lyd_parse_options_check(ctx, options, __func__)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200375 return NULL;
376 }
377
378 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200379 /* first item in trees is mandatory - the RPC/action request */
380 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
381 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
382 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
383 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200384 return NULL;
385 }
386 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200387
Radek Krejcie92210c2019-05-17 15:53:35 +0200388#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200389 if (options & LYD_OPT_DATA_TEMPLATE) {
390 yang_data_name = va_arg(ap, const char *);
391 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200392#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200393
394 if (!format) {
395 /* TODO try to detect format from the content */
396 }
397
398 switch (format) {
399 case LYD_XML:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200400 lyd_parse_xml(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200401 break;
402#if 0
403 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200404 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200405 break;
406 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200407 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200408 break;
409#endif
410 case LYD_UNKNOWN:
411 LOGINT(ctx);
412 break;
413 }
414
Radek Krejcie7b95092019-05-15 11:03:07 +0200415 return result;
416}
417
418API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200419lyd_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 +0200420{
421 struct lyd_node *result;
422 size_t length;
423 char *addr;
424
425 LY_CHECK_ARG_RET(ctx, ctx, NULL);
426 if (fd < 0) {
427 LOGARG(ctx, fd);
428 return NULL;
429 }
430
431 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200432 result = lyd_parse_mem(ctx, addr ? addr : "", format, options, trees);
Radek Krejcidf3da792019-05-17 10:32:24 +0200433 if (addr) {
434 ly_munmap(addr, length);
435 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200436
437 return result;
438}
439
440API struct lyd_node *
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200441lyd_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 +0200442{
443 int fd;
444 struct lyd_node *result;
445 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200446
447 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
448
449 fd = open(path, O_RDONLY);
450 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
451
452 if (!format) {
453 /* unknown format - try to detect it from filename's suffix */
454 len = strlen(path);
455
456 /* ignore trailing whitespaces */
457 for (; len > 0 && isspace(path[len - 1]); len--);
458
459 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
460 format = LYD_XML;
461#if 0
462 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
463 format = LYD_JSON;
464 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
465 format = LYD_LYB;
466#endif
467 } /* else still unknown, try later to detect it from the content */
468 }
469
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200470 result = lyd_parse_fd(ctx, fd, format, options, trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200471 close(fd);
472
473 return result;
474}
Radek Krejci084289f2019-07-09 17:35:30 +0200475
476API const struct lyd_node_term *
Radek Krejci576b23f2019-07-12 14:06:32 +0200477lyd_target(struct lyd_value_path *path, const struct lyd_node **trees)
Radek Krejci084289f2019-07-09 17:35:30 +0200478{
479 unsigned int u, v, x;
480 const struct lyd_node *node = NULL, *parent = NULL, *start_search;
481 uint64_t pos = 1;
482
483 LY_CHECK_ARG_RET(NULL, path, trees, NULL);
484
485 LY_ARRAY_FOR(path, u) {
486 if (parent) {
487 start_search = lyd_node_children(parent);
488search_inner:
489 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
490 } else {
491 LY_ARRAY_FOR(trees, v) {
492 start_search = trees[v];
493search_toplevel:
494 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
495 node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0);
496 if (node) {
497 break;
498 }
499 }
500 }
501 if (!node) {
502 return NULL;
503 }
504
505 /* check predicate if any */
506 LY_ARRAY_FOR(path[u].predicates, x) {
507 if (path[u].predicates[x].type == 0) {
508 /* position predicate */
509 if (pos != path[u].predicates[x].position) {
510 pos++;
511 goto search_repeat;
512 }
513 /* done, no more predicates are allowed here */
514 break;
515 } else if (path[u].predicates[x].type == 1) {
516 /* key-predicate */
517 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
518 const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module,
519 path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name),
520 LYS_LEAF, NULL, 0);
521 if (!key) {
522 /* probably error and we shouldn't be here due to previous checks when creating path */
523 goto search_repeat;
524 }
525 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
526 goto search_repeat;
527 }
528 } else if (path[u].predicates[x].type == 2) {
529 /* leaf-list-predicate */
530 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
531 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
532 goto search_repeat;
533 }
534 } else {
535 LOGINT(NULL);
536 }
537 }
538
539 parent = node;
540 }
541
542 return (const struct lyd_node_term*)node;
543
544search_repeat:
545 start_search = node->next;
546 if (parent) {
547 goto search_inner;
548 } else {
549 goto search_toplevel;
550 }
551}
552
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200553API LY_ERR
554lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
555{
556 const struct lyd_node *iter1, *iter2;
557 struct lyd_node_term *term1, *term2;
558 struct lyd_node_any *any1, *any2;
559 struct lysc_type *type;
560 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +0200561
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200562 if (!node1 || !node2) {
563 if (node1 == node2) {
564 return LY_SUCCESS;
565 } else {
566 return LY_ENOT;
567 }
568 }
569
570 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) {
571 return LY_ENOT;
572 }
573
574 if (node1->hash != node2->hash) {
575 return LY_ENOT;
576 }
577
578 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
579
580 switch (node1->schema->nodetype) {
581 case LYS_LEAF:
582 case LYS_LEAFLIST:
583 if (options & LYD_COMPARE_DEFAULTS) {
584 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
585 return LY_ENOT;
586 }
587 }
588
589 term1 = (struct lyd_node_term*)node1;
590 term2 = (struct lyd_node_term*)node2;
591 type = ((struct lysc_node_leaf*)node1->schema)->type;
592
593 return type->plugin->compare(&term1->value, &term2->value);
594 case LYS_CONTAINER:
595 if (options & LYD_COMPARE_DEFAULTS) {
596 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
597 return LY_ENOT;
598 }
599 }
600 if (options & LYD_COMPARE_FULL_RECURSION) {
601 iter1 = ((struct lyd_node_inner*)node1)->child;
602 iter2 = ((struct lyd_node_inner*)node2)->child;
603 goto all_children_compare;
604 }
605 return LY_SUCCESS;
606 case LYS_ACTION:
607 if (options & LYD_COMPARE_FULL_RECURSION) {
608 /* TODO action/RPC
609 goto all_children_compare;
610 */
611 }
612 return LY_SUCCESS;
613 case LYS_NOTIF:
614 if (options & LYD_COMPARE_FULL_RECURSION) {
615 /* TODO Notification
616 goto all_children_compare;
617 */
618 }
619 return LY_SUCCESS;
620 case LYS_LIST:
621 iter1 = ((struct lyd_node_inner*)node1)->child;
622 iter2 = ((struct lyd_node_inner*)node2)->child;
623
Radek Krejci0fe9b512019-07-26 17:51:05 +0200624 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200625 /* lists with keys, their equivalence is based on their keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200626 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
627 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
628 key = key->next) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200629 if (lyd_compare(iter1, iter2, options)) {
630 return LY_ENOT;
631 }
632 iter1 = iter1->next;
633 iter2 = iter2->next;
634 }
635 } else {
636 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
637
638all_children_compare:
639 if (!iter1 && !iter2) {
640 /* no children, nothing to compare */
641 return LY_SUCCESS;
642 }
643
644 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
645 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
646 return LY_ENOT;
647 }
648 }
649 if (iter1 || iter2) {
650 return LY_ENOT;
651 }
652 }
653 return LY_SUCCESS;
654 case LYS_ANYXML:
655 case LYS_ANYDATA:
656 any1 = (struct lyd_node_any*)node1;
657 any2 = (struct lyd_node_any*)node2;
658
659 if (any1->value_type != any2->value_type) {
660 return LY_ENOT;
661 }
662 switch (any1->value_type) {
663 case LYD_ANYDATA_DATATREE:
664 iter1 = any1->value.tree;
665 iter2 = any2->value.tree;
666 goto all_children_compare;
667 case LYD_ANYDATA_STRING:
668 case LYD_ANYDATA_XML:
669 case LYD_ANYDATA_JSON:
670 len1 = strlen(any1->value.str);
671 len2 = strlen(any2->value.str);
672 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
673 return LY_ENOT;
674 }
675 return LY_SUCCESS;
676#if 0 /* TODO LYB format */
677 case LYD_ANYDATA_LYB:
678 int len1 = lyd_lyb_data_length(any1->value.mem);
679 int len2 = lyd_lyb_data_length(any2->value.mem);
680 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
681 return LY_ENOT;
682 }
683 return LY_SUCCESS;
684#endif
685 }
686 }
687
688 LOGINT(node1->schema->module->ctx);
689 return LY_EINT;
690}
Radek Krejci22ebdba2019-07-25 13:59:43 +0200691
692/**
693 * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev
694 * sibling (if present).
695 *
696 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
697 */
698static struct lyd_node *
699lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options)
700{
701 struct ly_ctx *ctx;
702 struct lyd_node *dup = NULL;
703
704 LY_CHECK_ARG_RET(NULL, node, NULL);
705 ctx = node->schema->module->ctx;
706
707 switch (node->schema->nodetype) {
708 case LYS_ACTION:
709 case LYS_NOTIF:
710 case LYS_CONTAINER:
711 case LYS_LIST:
712 dup = calloc(1, sizeof(struct lyd_node_inner));
713 break;
714 case LYS_LEAF:
715 case LYS_LEAFLIST:
716 dup = calloc(1, sizeof(struct lyd_node_term));
717 break;
718 case LYS_ANYDATA:
719 case LYS_ANYXML:
720 dup = calloc(1, sizeof(struct lyd_node_any));
721 break;
722 default:
723 LOGINT(ctx);
724 goto error;
725 }
726
727 /* TODO implement LYD_DUP_WITH_WHEN */
728 dup->flags = node->flags;
729 dup->schema = node->schema;
730
731 /* interconnect the node at the end */
732 dup->parent = parent;
733 if (prev) {
734 dup->prev = prev;
735 prev->next = dup;
736 } else {
737 dup->prev = dup;
738 if (parent) {
739 parent->child = dup;
740 }
741 }
742 if (parent) {
743 parent->child->prev = dup;
744 } else if (prev) {
745 struct lyd_node *first;
746 for (first = prev; first->prev != prev; first = first->prev);
747 first->prev = dup;
748 }
749
750 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
751
752 /* nodetype-specific work */
753 if (dup->schema->nodetype & LYD_NODE_TERM) {
754 struct lyd_node_term *term = (struct lyd_node_term*)dup;
755 struct lyd_node_term *orig = (struct lyd_node_term*)node;
756
757 term->hash = orig->hash;
758 term->value.realtype = orig->value.realtype;
759 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value),
760 LOGERR(ctx, LY_EINT, "Value duplication failed."), error);
761 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
762 struct lyd_node_inner *inner = (struct lyd_node_inner*)dup;
763 struct lyd_node_inner *orig = (struct lyd_node_inner*)node;
764 struct lyd_node *child, *last = NULL;
765
766 if (options & LYD_DUP_RECURSIVE) {
767 /* duplicate all the children */
768 LY_LIST_FOR(orig->child, child) {
769 last = lyd_dup_recursive(child, inner, last, options);
770 LY_CHECK_GOTO(!last, error);
771 }
Radek Krejci0fe9b512019-07-26 17:51:05 +0200772 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200773 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200774 child = orig->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200775 for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child;
776 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
777 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200778 if (!child) {
779 /* possibly not keys are present in filtered tree */
780 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200781 } else if (child->schema != key) {
782 /* possibly not all keys are present in filtered tree,
783 * but there can be also some non-key nodes */
784 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200785 }
786 last = lyd_dup_recursive(child, inner, last, options);
787 child = child->next;
788 }
789 }
790 lyd_hash(dup);
791 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
792 struct lyd_node_any *any = (struct lyd_node_any*)dup;
793 struct lyd_node_any *orig = (struct lyd_node_any*)node;
794
795 any->hash = orig->hash;
796 any->value_type = orig->value_type;
797 switch (any->value_type) {
798 case LYD_ANYDATA_DATATREE:
799 if (orig->value.tree) {
800 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
801 LY_CHECK_GOTO(!any->value.tree, error);
802 }
803 break;
804 case LYD_ANYDATA_STRING:
805 case LYD_ANYDATA_XML:
806 case LYD_ANYDATA_JSON:
807 if (orig->value.str) {
808 any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str));
809 }
810 break;
811 }
812 }
813
814 lyd_insert_hash(dup);
815 return dup;
816
817error:
818 if (!parent && !prev) {
819 lyd_free_tree(dup);
820 }
821 return NULL;
822}
823
824API struct lyd_node *
825lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
826{
827 struct ly_ctx *ctx;
828 const struct lyd_node *orig; /* original node to be duplicated */
829 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
830 struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */
831 struct lyd_node *top = NULL; /* the most higher created node */
832 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
833 int keyless_parent_list = 0;
834
835 LY_CHECK_ARG_RET(NULL, node, NULL);
836 ctx = node->schema->module->ctx;
837
838 if (options & LYD_DUP_WITH_PARENTS) {
839 struct lyd_node_inner *orig_parent, *iter;
840 int repeat = 1;
841 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
842 if (parent && parent->schema == orig_parent->schema) {
843 /* stop creating parents, connect what we have into the provided parent */
844 iter = parent;
845 repeat = 0;
846 /* get know if there is a keyless list which we will have to rehash */
847 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200848 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200849 keyless_parent_list = 1;
850 break;
851 }
852 }
853 } else {
854 iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0);
855 LY_CHECK_GOTO(!iter, error);
856 }
857 if (!local_parent) {
858 local_parent = iter;
859 }
860 if (iter->child) {
861 /* 1) list - add after keys
862 * 2) provided parent with some children */
863 iter->child->prev->next = top;
864 if (top) {
865 top->prev = iter->child->prev;
866 iter->child->prev = top;
867 }
868 } else {
869 iter->child = top;
870 if (iter->schema->nodetype == LYS_LIST) {
871 /* keyless list - we will need to rehash it since we are going to add nodes into it */
872 keyless_parent_list = 1;
873 }
874 }
875 if (top) {
876 top->parent = iter;
877 }
878 top = (struct lyd_node*)iter;
879 }
880 if (repeat && parent) {
881 /* given parent and created parents chain actually do not interconnect */
882 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
883 goto error;
884 }
885 } else {
886 local_parent = parent;
887 }
888
889 if (local_parent && local_parent->child) {
890 last = local_parent->child->prev;
891 }
892
893 LY_LIST_FOR(node, orig) {
894 last = lyd_dup_recursive(orig, local_parent, last, options);
895 LY_CHECK_GOTO(!last, error);
896 if (!first) {
897 first = last;
898 }
899
900 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
901 break;
902 }
903 }
904 if (keyless_parent_list) {
905 /* rehash */
906 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200907 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200908 lyd_hash((struct lyd_node*)local_parent);
909 }
910 }
911 }
912 return first;
913
914error:
915 if (top) {
916 lyd_free_tree(top);
917 } else {
918 lyd_free_withsiblings(first);
919 }
920 return NULL;
921}
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200922
923static LY_ERR
924lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
925{
926 if (reqlen > *buflen) {
927 if (is_static) {
928 return LY_EINCOMPLETE;
929 }
930
931 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
932 if (!*buffer) {
933 return LY_EMEM;
934 }
935
936 *buflen = reqlen;
937 }
938
939 return LY_SUCCESS;
940}
941
942/**
943 * @brief Append all list key predicates to path.
944 *
945 * @param[in] node Node with keys to print.
946 * @param[in,out] buffer Buffer to print to.
947 * @param[in,out] buflen Current buffer length.
948 * @param[in,out] bufused Current number of characters used in @p buffer.
949 * @param[in] is_static Whether buffer is static or can be reallocated.
950 * @return LY_ERR
951 */
952static LY_ERR
953lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
954{
955 const struct lyd_node *key;
956 int dynamic = 0;
957 size_t len;
958 const char *val;
959 char quot;
960 LY_ERR rc;
961
962 for (key = lyd_node_children(node); key && (key->flags & LYS_KEY); key = key->next) {
963 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
964 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
965 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
966 if (rc != LY_SUCCESS) {
967 if (dynamic) {
968 free((char *)val);
969 }
970 return rc;
971 }
972
973 quot = '\'';
974 if (strchr(val, '\'')) {
975 quot = '"';
976 }
977 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
978
979 if (dynamic) {
980 free((char *)val);
981 }
982 }
983
984 return LY_SUCCESS;
985}
986
987/**
988 * @brief Append leaf-list value predicate to path.
989 *
990 * @param[in] node Node to print.
991 * @param[in,out] buffer Buffer to print to.
992 * @param[in,out] buflen Current buffer length.
993 * @param[in,out] bufused Current number of characters used in @p buffer.
994 * @param[in] is_static Whether buffer is static or can be reallocated.
995 * @return LY_ERR
996 */
997static LY_ERR
998lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
999{
1000 int dynamic = 0;
1001 size_t len;
1002 const char *val;
1003 char quot;
1004 LY_ERR rc;
1005
1006 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
1007 len = 4 + strlen(val) + 2;
1008 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1009 if (rc != LY_SUCCESS) {
1010 goto cleanup;
1011 }
1012
1013 quot = '\'';
1014 if (strchr(val, '\'')) {
1015 quot = '"';
1016 }
1017 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
1018
1019cleanup:
1020 if (dynamic) {
1021 free((char *)val);
1022 }
1023 return rc;
1024}
1025
1026/**
1027 * @brief Append node position (relative to its other instances) predicate to path.
1028 *
1029 * @param[in] node Node to print.
1030 * @param[in,out] buffer Buffer to print to.
1031 * @param[in,out] buflen Current buffer length.
1032 * @param[in,out] bufused Current number of characters used in @p buffer.
1033 * @param[in] is_static Whether buffer is static or can be reallocated.
1034 * @return LY_ERR
1035 */
1036static LY_ERR
1037lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1038{
1039 const struct lyd_node *first, *iter;
1040 size_t len;
1041 int pos;
1042 char *val = NULL;
1043 LY_ERR rc;
1044
1045 if (node->parent) {
1046 first = node->parent->child;
1047 } else {
1048 for (first = node; node->prev->next; node = node->prev);
1049 }
1050 pos = 1;
1051 for (iter = first; iter != node; iter = iter->next) {
1052 if (iter->schema == node->schema) {
1053 ++pos;
1054 }
1055 }
1056 if (asprintf(&val, "%d", pos) == -1) {
1057 return LY_EMEM;
1058 }
1059
1060 len = 1 + strlen(val) + 1;
1061 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1062 if (rc != LY_SUCCESS) {
1063 goto cleanup;
1064 }
1065
1066 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
1067
1068cleanup:
1069 free(val);
1070 return rc;
1071}
1072
1073API char *
1074lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
1075{
1076 int is_static = 0, i, j, depth;
1077 size_t bufused = 1 /* ending zero */, len;
1078 const struct lyd_node *iter;
1079 const struct lys_module *mod;
1080 LY_ERR rc;
1081
1082 LY_CHECK_ARG_RET(NULL, node, NULL);
1083 if (buffer) {
1084 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
1085 is_static = 1;
1086 }
1087
1088 switch (pathtype) {
1089 case LYSC_PATH_LOG:
1090 depth = 0;
1091 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
1092 ++depth;
1093 }
1094
1095 i = depth + 1;
1096 goto iter_print;
1097 while (i) {
1098 /* find the right node */
1099 for (iter = node, j = 0; j < i; iter = (const struct lyd_node *)iter->parent, ++j);
1100iter_print:
1101 /* print prefix and name */
1102 mod = NULL;
1103 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
1104 mod = iter->schema->module;
1105 }
1106
1107 /* realloc string */
1108 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
1109 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
1110 if (rc != LY_SUCCESS) {
1111 break;
1112 }
1113
1114 /* print next node */
1115 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
1116
1117 switch (iter->schema->nodetype) {
1118 case LYS_LIST:
1119 if (iter->schema->flags & LYS_KEYLESS) {
1120 /* print its position */
1121 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1122 } else {
1123 /* print all list keys in predicates */
1124 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
1125 }
1126 break;
1127 case LYS_LEAFLIST:
1128 if (iter->schema->flags & LYS_CONFIG_W) {
1129 /* print leaf-list value */
1130 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
1131 } else {
1132 /* print its position */
1133 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1134 }
1135 break;
1136 default:
1137 /* nothing to print more */
1138 rc = LY_SUCCESS;
1139 break;
1140 }
1141 if (rc != LY_SUCCESS) {
1142 break;
1143 }
1144
1145 --i;
1146 }
1147 break;
1148 }
1149
1150 return buffer;
1151}