blob: 72e8c7b2ee282563012894f1b483e18b87e41f8f [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020041#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
49#include "xml.h"
50#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020051
Michal Vaskob104f112020-07-17 09:54:54 +020052static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
53 struct lyd_node **match);
54
Radek Krejci084289f2019-07-09 17:35:30 +020055LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010056lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010057 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020058{
Michal Vasko90932a92020-02-12 14:33:03 +010059 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020060 struct ly_err_item *err = NULL;
61 struct ly_ctx *ctx;
62 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020063 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010064 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020065 assert(node);
66
67 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020068
Radek Krejci73dead22019-07-11 16:46:16 +020069 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020070 if (!second) {
71 node->value.realtype = type;
72 }
Michal Vasko90932a92020-02-12 14:33:03 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020074 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010075 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020076 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020077 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020078 if (!node->parent && lysc_data_parent(node->schema)) {
79 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
80 } else {
81 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
82 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020084 }
Radek Krejci73dead22019-07-11 16:46:16 +020085 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010086 } else if (dynamic) {
87 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020088 }
89
90error:
91 return ret;
92}
93
Michal Vasko00cbf532020-06-15 13:58:47 +020094/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020095LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010096lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
97 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
98{
99 LY_ERR ret = LY_SUCCESS;
100 struct ly_err_item *err = NULL;
101 struct ly_ctx *ctx;
102 struct lysc_type *type;
103 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
104
105 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
106
107 ctx = schema->module->ctx;
108 type = ((struct lysc_node_leaf *)schema)->type;
109 val->realtype = type;
110 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
111 val, NULL, &err);
112 if (ret == LY_EINCOMPLETE) {
113 /* this is fine, we do not need it resolved */
114 ret = LY_SUCCESS;
115 } else if (ret && err) {
116 ly_err_print(err);
117 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
118 ly_err_free(err);
119 }
120 if (!ret && dynamic) {
121 *dynamic = 0;
122 }
123
124 return ret;
125}
126
Radek Krejci38d85362019-09-05 16:26:38 +0200127LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200128lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100129 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100130 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200131{
Michal Vasko90932a92020-02-12 14:33:03 +0100132 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200133 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200134 struct lyext_metadata *ant;
135 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100136 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200137
Michal Vasko9f96a052020-03-10 09:41:45 +0100138 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100139
Michal Vasko9f96a052020-03-10 09:41:45 +0100140 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200141
142 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100143 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200144 }
Michal Vasko90932a92020-02-12 14:33:03 +0100145 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100146 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100147 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200148 if (err) {
149 ly_err_print(err);
150 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
151 ly_err_free(err);
152 }
153 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100154 } else if (dynamic) {
155 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200156 }
157
158error:
159 return ret;
160}
161
Radek Krejci084289f2019-07-09 17:35:30 +0200162API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100163lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci084289f2019-07-09 17:35:30 +0200164 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
165{
166 LY_ERR rc = LY_SUCCESS;
167 struct ly_err_item *err = NULL;
168 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200169
170 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
171
172 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
173 LOGARG(ctx, node);
174 return LY_EINVAL;
175 }
176
177 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200178 /* just validate, no storing of enything */
179 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
180 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
181 if (rc == LY_EINCOMPLETE) {
182 /* actually success since we do not provide the context tree and call validation with
183 * LY_TYPE_OPTS_INCOMPLETE_DATA */
184 rc = LY_SUCCESS;
185 } else if (rc && err) {
186 if (ctx) {
187 /* log only in case the ctx was provided as input parameter */
188 ly_err_print(err);
189 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200190 }
Radek Krejci73dead22019-07-11 16:46:16 +0200191 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200192 }
193
194 return rc;
195}
196
197API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100198lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100199 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200200{
201 LY_ERR rc;
202 struct ly_err_item *err = NULL;
203 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100204 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200205
206 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
207
208 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200209 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100210 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200211 NULL, NULL, &err);
212 if (rc == LY_EINCOMPLETE) {
213 return rc;
214 } else if (rc) {
215 if (err) {
216 if (ctx) {
217 ly_err_print(err);
218 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200219 }
Radek Krejci73dead22019-07-11 16:46:16 +0200220 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200221 }
Radek Krejci73dead22019-07-11 16:46:16 +0200222 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200223 }
224
225 return LY_SUCCESS;
226}
227
228API LY_ERR
229lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100230 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200231{
232 LY_ERR ret = LY_SUCCESS, rc;
233 struct ly_err_item *err = NULL;
234 struct ly_ctx *ctx;
235 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200236 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100237 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200238
239 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
240
241 ctx = node->schema->module->ctx;
242 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200243 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
Michal Vaskof03ed032020-03-04 13:31:44 +0100244 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200245 if (rc == LY_EINCOMPLETE) {
246 ret = rc;
247 /* continue with comparing, just remember what to return if storing is ok */
248 } else if (rc) {
249 /* value to compare is invalid */
250 ret = LY_EINVAL;
251 if (err) {
252 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200253 }
Radek Krejci73dead22019-07-11 16:46:16 +0200254 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200255 }
256
257 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200258 if (type->plugin->compare(&node->value, &data)) {
259 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200260 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200261 }
Radek Krejci084289f2019-07-09 17:35:30 +0200262
263cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200264 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200265
266 return ret;
267}
268
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200269API const char *
270lyd_value2str(const struct lyd_node_term *node, int *dynamic)
271{
272 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
273
274 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
275}
276
277API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100278lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200279{
Michal Vasko9f96a052020-03-10 09:41:45 +0100280 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200281
Michal Vasko9f96a052020-03-10 09:41:45 +0100282 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200283}
284
Radek Krejci7931b192020-06-25 17:05:03 +0200285static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200286lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200287{
Radek Krejcie7b95092019-05-15 11:03:07 +0200288
Radek Krejci7931b192020-06-25 17:05:03 +0200289 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200291 const char *path = in->method.fpath.filepath;
292 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200293
294 /* ignore trailing whitespaces */
295 for (; len > 0 && isspace(path[len - 1]); len--);
296
297 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
298 format = LYD_XML;
299#if 0
300 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
301 format = LYD_JSON;
Radek Krejci7931b192020-06-25 17:05:03 +0200302#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
304 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200305 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200306 }
307
Radek Krejci7931b192020-06-25 17:05:03 +0200308 return format;
309}
Radek Krejcie7b95092019-05-15 11:03:07 +0200310
Radek Krejci7931b192020-06-25 17:05:03 +0200311API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200312lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
313 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200314{
315 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
316 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
317 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
318
319 format = lyd_parse_get_format(in, format);
320 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
321
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 /* remember input position */
323 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200324
325 switch (format) {
326 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 return lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200328#if 0
329 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 return lyd_parse_json_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200331#endif
332 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200333 return lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200334 case LYD_SCHEMA:
335 LOGINT_RET(ctx);
336 }
337
338 /* TODO move here the top-level validation from parser_xml.c's lyd_parse_xml_data() and make
339 * it common for all the lyd_parse_*_data() functions */
340
341 LOGINT_RET(ctx);
342}
343
344API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200345lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
346 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200347{
348 LY_ERR ret;
349 struct ly_in *in;
350
351 LY_CHECK_RET(ly_in_new_memory(data, &in));
352 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
353
354 ly_in_free(in, 0);
355 return ret;
356}
357
358API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200359lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
360 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200361{
362 LY_ERR ret;
363 struct ly_in *in;
364
365 LY_CHECK_RET(ly_in_new_fd(fd, &in));
366 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
367
368 ly_in_free(in, 0);
369 return ret;
370}
371
372API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200373lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
374 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200375{
376 LY_ERR ret;
377 struct ly_in *in;
378
379 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
380 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
381
382 ly_in_free(in, 0);
383 return ret;
384}
385
Radek Krejci7931b192020-06-25 17:05:03 +0200386API LY_ERR
387lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
388{
389 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
390
391 format = lyd_parse_get_format(in, format);
392 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
393
Michal Vasko63f3d842020-07-08 10:10:14 +0200394 /* remember input position */
395 in->func_start = in->current;
396
Radek Krejci7931b192020-06-25 17:05:03 +0200397 switch (format) {
398 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200400#if 0
401 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200402 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200403#endif
404 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200405 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200406 case LYD_SCHEMA:
407 LOGINT_RET(ctx);
408 }
409
410 LOGINT_RET(ctx);
411}
412
413API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200414lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
415 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200416{
417 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
418 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
419
420 format = lyd_parse_get_format(in, format);
421 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
422
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 /* remember input position */
424 in->func_start = in->current;
425
Radek Krejci7931b192020-06-25 17:05:03 +0200426 switch (format) {
427 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200428 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200429#if 0
430 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200432#endif
433 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200435 case LYD_SCHEMA:
436 LOGINT_RET(LYD_NODE_CTX(request));
437 }
438
439 LOGINT_RET(LYD_NODE_CTX(request));
440}
441
442API LY_ERR
443lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
444{
445 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
446
447 format = lyd_parse_get_format(in, format);
448 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
449
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 /* remember input position */
451 in->func_start = in->current;
452
Radek Krejci7931b192020-06-25 17:05:03 +0200453 switch (format) {
454 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200456#if 0
457 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200459#endif
460 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200462 case LYD_SCHEMA:
463 LOGINT_RET(ctx);
464 }
465
466 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200467}
Radek Krejci084289f2019-07-09 17:35:30 +0200468
Michal Vasko90932a92020-02-12 14:33:03 +0100469LY_ERR
470lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
471 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
472{
473 LY_ERR ret;
474 struct lyd_node_term *term;
475
Michal Vasko9b368d32020-02-14 13:53:31 +0100476 assert(schema->nodetype & LYD_NODE_TERM);
477
Michal Vasko90932a92020-02-12 14:33:03 +0100478 term = calloc(1, sizeof *term);
479 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
480
481 term->schema = schema;
482 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100483 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100484
485 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
486 if (ret && (ret != LY_EINCOMPLETE)) {
487 free(term);
488 return ret;
489 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100490 lyd_hash((struct lyd_node *)term);
491
492 *node = (struct lyd_node *)term;
493 return ret;
494}
495
496LY_ERR
497lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
498{
499 LY_ERR ret;
500 struct lyd_node_term *term;
501 struct lysc_type *type;
502
503 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200504 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100505
506 term = calloc(1, sizeof *term);
507 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
508
509 term->schema = schema;
510 term->prev = (struct lyd_node *)term;
511 term->flags = LYD_NEW;
512
513 type = ((struct lysc_node_leaf *)schema)->type;
514 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
515 if (ret) {
516 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
517 free(term);
518 return ret;
519 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200520 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100521 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100522
523 *node = (struct lyd_node *)term;
524 return ret;
525}
526
527LY_ERR
528lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
529{
530 struct lyd_node_inner *in;
531
Michal Vasko9b368d32020-02-14 13:53:31 +0100532 assert(schema->nodetype & LYD_NODE_INNER);
533
Michal Vasko90932a92020-02-12 14:33:03 +0100534 in = calloc(1, sizeof *in);
535 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
536
537 in->schema = schema;
538 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100539 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100540
Michal Vasko9b368d32020-02-14 13:53:31 +0100541 /* do not hash list with keys, we need them for the hash */
542 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
543 lyd_hash((struct lyd_node *)in);
544 }
Michal Vasko90932a92020-02-12 14:33:03 +0100545
546 *node = (struct lyd_node *)in;
547 return LY_SUCCESS;
548}
549
Michal Vasko90932a92020-02-12 14:33:03 +0100550LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200551lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100552{
553 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100554 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200555 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100556
Michal Vasko004d3152020-06-11 19:59:22 +0200557 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100558
559 /* create list */
560 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
561
Michal Vasko90932a92020-02-12 14:33:03 +0100562 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200563 LY_ARRAY_FOR(predicates, u) {
564 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100565 lyd_insert_node(list, NULL, key);
566 }
567
Michal Vasko9b368d32020-02-14 13:53:31 +0100568 /* hash having all the keys */
569 lyd_hash(list);
570
Michal Vasko90932a92020-02-12 14:33:03 +0100571 /* success */
572 *node = list;
573 list = NULL;
574
575cleanup:
576 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200577 return ret;
578}
579
580static LY_ERR
581lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
582{
583 LY_ERR ret = LY_SUCCESS;
584 struct lyxp_expr *expr = NULL;
585 uint16_t exp_idx = 0;
586 enum ly_path_pred_type pred_type = 0;
587 struct ly_path_predicate *predicates = NULL;
588
589 /* parse keys */
590 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
591 LY_PATH_PRED_KEYS, &expr), cleanup);
592
593 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200594 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
595 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200596
597 /* create the list node */
598 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
599
600cleanup:
601 lyxp_expr_free(schema->module->ctx, expr);
602 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100603 return ret;
604}
605
606LY_ERR
607lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
608{
609 struct lyd_node_any *any;
610
Michal Vasko9b368d32020-02-14 13:53:31 +0100611 assert(schema->nodetype & LYD_NODE_ANY);
612
Michal Vasko90932a92020-02-12 14:33:03 +0100613 any = calloc(1, sizeof *any);
614 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
615
616 any->schema = schema;
617 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100618 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100619
620 any->value.xml = value;
621 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100622 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100623
624 *node = (struct lyd_node *)any;
625 return LY_SUCCESS;
626}
627
Michal Vasko52927e22020-03-16 17:26:14 +0100628LY_ERR
629lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
630 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
631 const char *ns, struct lyd_node **node)
632{
633 struct lyd_node_opaq *opaq;
634
635 assert(ctx && name && name_len && ns);
636
637 if (!value_len) {
638 value = "";
639 }
640
641 opaq = calloc(1, sizeof *opaq);
642 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
643
644 opaq->prev = (struct lyd_node *)opaq;
645
646 opaq->name = lydict_insert(ctx, name, name_len);
647 opaq->format = format;
648 if (pref_len) {
649 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
650 }
651 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
652 opaq->val_prefs = val_prefs;
653 if (dynamic && *dynamic) {
654 opaq->value = lydict_insert_zc(ctx, (char *)value);
655 *dynamic = 0;
656 } else {
657 opaq->value = lydict_insert(ctx, value, value_len);
658 }
659 opaq->ctx = ctx;
660
661 *node = (struct lyd_node *)opaq;
662 return LY_SUCCESS;
663}
664
Michal Vasko3a41dff2020-07-15 14:30:28 +0200665API LY_ERR
666lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100667{
668 struct lyd_node *ret = NULL;
669 const struct lysc_node *schema;
670 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
671
Michal Vasko6027eb92020-07-15 16:37:30 +0200672 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100673
Michal Vaskof03ed032020-03-04 13:31:44 +0100674 if (!module) {
675 module = parent->schema->module;
676 }
677
Michal Vasko3a41dff2020-07-15 14:30:28 +0200678 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
679 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
680 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100681
Michal Vasko3a41dff2020-07-15 14:30:28 +0200682 LY_CHECK_RET(lyd_create_inner(schema, &ret));
683 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100684 lyd_insert_node(parent, NULL, ret);
685 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200686
687 if (node) {
688 *node = ret;
689 }
690 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100691}
692
Michal Vasko3a41dff2020-07-15 14:30:28 +0200693API LY_ERR
694lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100695{
696 struct lyd_node *ret = NULL, *key;
697 const struct lysc_node *schema, *key_s;
698 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
699 va_list ap;
700 const char *key_val;
701 LY_ERR rc = LY_SUCCESS;
702
Michal Vasko6027eb92020-07-15 16:37:30 +0200703 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100704
Michal Vaskof03ed032020-03-04 13:31:44 +0100705 if (!module) {
706 module = parent->schema->module;
707 }
708
Michal Vasko013a8182020-03-03 10:46:53 +0100709 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200710 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100711
712 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200713 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100714
Michal Vasko3a41dff2020-07-15 14:30:28 +0200715 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100716
717 /* create and insert all the keys */
718 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
719 key_val = va_arg(ap, const char *);
720
Michal Vaskof03ed032020-03-04 13:31:44 +0100721 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200722 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
723 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100724 lyd_insert_node(ret, NULL, key);
725 }
726
Michal Vasko013a8182020-03-03 10:46:53 +0100727 if (parent) {
728 lyd_insert_node(parent, NULL, ret);
729 }
730
731cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200732 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100733 if (rc) {
734 lyd_free_tree(ret);
735 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200736 } else if (node) {
737 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100738 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200739 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100740}
741
Michal Vasko3a41dff2020-07-15 14:30:28 +0200742API LY_ERR
743lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
744 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100745{
746 struct lyd_node *ret = NULL;
747 const struct lysc_node *schema;
748 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
749
Michal Vasko6027eb92020-07-15 16:37:30 +0200750 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100751
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 if (!module) {
753 module = parent->schema->module;
754 }
Michal Vasko004d3152020-06-11 19:59:22 +0200755 if (!keys) {
756 keys = "";
757 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100758
Michal Vasko004d3152020-06-11 19:59:22 +0200759 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100760 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200761 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100762
Michal Vasko004d3152020-06-11 19:59:22 +0200763 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
764 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200765 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200766 } else {
767 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200768 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200769 }
Michal Vasko004d3152020-06-11 19:59:22 +0200770 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100771 lyd_insert_node(parent, NULL, ret);
772 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200773
774 if (node) {
775 *node = ret;
776 }
777 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100778}
779
Michal Vasko3a41dff2020-07-15 14:30:28 +0200780API LY_ERR
781lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
782 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100783{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200784 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100785 struct lyd_node *ret = NULL;
786 const struct lysc_node *schema;
787 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
788
Michal Vasko6027eb92020-07-15 16:37:30 +0200789 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100790
Michal Vaskof03ed032020-03-04 13:31:44 +0100791 if (!module) {
792 module = parent->schema->module;
793 }
794
Michal Vasko013a8182020-03-03 10:46:53 +0100795 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200796 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100797
Michal Vaskocbff3e92020-05-27 12:56:41 +0200798 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200799 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200800 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100801 lyd_insert_node(parent, NULL, ret);
802 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200803
804 if (node) {
805 *node = ret;
806 }
807 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100808}
809
Michal Vasko3a41dff2020-07-15 14:30:28 +0200810API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100811lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100813{
814 struct lyd_node *ret = NULL;
815 const struct lysc_node *schema;
816 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
817
Michal Vasko6027eb92020-07-15 16:37:30 +0200818 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100819
Michal Vaskof03ed032020-03-04 13:31:44 +0100820 if (!module) {
821 module = parent->schema->module;
822 }
823
Michal Vasko013a8182020-03-03 10:46:53 +0100824 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200825 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100826
Michal Vasko3a41dff2020-07-15 14:30:28 +0200827 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
828 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100829 lyd_insert_node(parent, NULL, ret);
830 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831
832 if (node) {
833 *node = ret;
834 }
835 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100836}
837
Michal Vasko4490d312020-06-16 13:08:55 +0200838/**
839 * @brief Update node value.
840 *
841 * @param[in] node Node to update.
842 * @param[in] value New value to set.
843 * @param[in] value_type Type of @p value for any node.
844 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
845 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
846 * @return LY_ERR value.
847 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200848static LY_ERR
849lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
850 struct lyd_node **new_parent, struct lyd_node **new_node)
851{
852 LY_ERR ret = LY_SUCCESS;
853 struct lyd_node *new_any;
854
855 switch (node->schema->nodetype) {
856 case LYS_CONTAINER:
857 case LYS_NOTIF:
858 case LYS_RPC:
859 case LYS_ACTION:
860 case LYS_LIST:
861 case LYS_LEAFLIST:
862 /* if it exists, there is nothing to update */
863 *new_parent = NULL;
864 *new_node = NULL;
865 break;
866 case LYS_LEAF:
867 ret = lyd_change_term(node, value);
868 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
869 /* there was an actual change (at least of the default flag) */
870 *new_parent = node;
871 *new_node = node;
872 ret = LY_SUCCESS;
873 } else if (ret == LY_ENOT) {
874 /* no change */
875 *new_parent = NULL;
876 *new_node = NULL;
877 ret = LY_SUCCESS;
878 } /* else error */
879 break;
880 case LYS_ANYDATA:
881 case LYS_ANYXML:
882 /* create a new any node */
883 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
884
885 /* compare with the existing one */
886 if (lyd_compare(node, new_any, 0)) {
887 /* not equal, switch values (so that we can use generic node free) */
888 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
889 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
890 ((struct lyd_node_any *)node)->value.str = value;
891 ((struct lyd_node_any *)node)->value_type = value_type;
892
893 *new_parent = node;
894 *new_node = node;
895 } else {
896 /* they are equal */
897 *new_parent = NULL;
898 *new_node = NULL;
899 }
900 lyd_free_tree(new_any);
901 break;
902 default:
903 LOGINT(LYD_NODE_CTX(node));
904 ret = LY_EINT;
905 break;
906 }
907
908 return ret;
909}
910
Michal Vasko3a41dff2020-07-15 14:30:28 +0200911API LY_ERR
912lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
913 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200914{
915 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200916 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200917 const char *prefix, *tmp;
918 char *str;
919 size_t pref_len, name_len;
920
Michal Vasko3a41dff2020-07-15 14:30:28 +0200921 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200922
923 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200924
925 /* parse the name */
926 tmp = name;
927 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
928 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200929 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200930 }
931
932 /* find the module */
933 if (prefix) {
Michal Vasko0b245382020-07-09 15:43:52 +0200934 str = strndup(prefix, pref_len);
Michal Vaskod86997b2020-05-26 15:19:54 +0200935 module = ly_ctx_get_module_implemented(ctx, str);
936 free(str);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200937 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +0200938 }
939
940 /* set value if none */
941 if (!val_str) {
942 val_str = "";
943 }
944
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL,
946 lydjson_resolve_prefix, NULL, LYD_JSON, parent->schema));
947
948 if (meta) {
949 *meta = ret;
950 }
951 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +0200952}
953
Michal Vasko3a41dff2020-07-15 14:30:28 +0200954API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200955lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200956 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200957{
958 struct lyd_node *ret = NULL;
959
Michal Vasko6027eb92020-07-15 16:37:30 +0200960 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200961
962 if (!ctx) {
963 ctx = LYD_NODE_CTX(parent);
964 }
965 if (!value) {
966 value = "";
967 }
968
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0,
970 module_name, &ret));
971 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200972 lyd_insert_node(parent, NULL, ret);
973 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974
975 if (node) {
976 *node = ret;
977 }
978 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200979}
980
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981API LY_ERR
982lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
983 struct ly_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +0200984{
985 struct ly_attr *ret = NULL;
986 const struct ly_ctx *ctx;
987 const char *prefix, *tmp;
988 size_t pref_len, name_len;
989
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200991
992 ctx = LYD_NODE_CTX(parent);
993
994 /* parse the name */
995 tmp = name;
996 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
997 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200998 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200999 }
1000
1001 /* set value if none */
1002 if (!val_str) {
1003 val_str = "";
1004 }
1005
Michal Vasko3a41dff2020-07-15 14:30:28 +02001006 LY_CHECK_RET(ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL,
1007 prefix, pref_len, module_name));
1008
1009 if (attr) {
1010 *attr = ret;
1011 }
1012 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001013}
1014
1015API LY_ERR
1016lyd_change_term(struct lyd_node *term, const char *val_str)
1017{
1018 LY_ERR ret = LY_SUCCESS;
1019 struct lysc_type *type;
1020 struct lyd_node_term *t;
1021 struct lyd_node *parent;
1022 struct lyd_value val = {0};
1023 int dflt_change, val_change;
1024
1025 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1026
1027 if (!val_str) {
1028 val_str = "";
1029 }
1030 t = (struct lyd_node_term *)term;
1031 type = ((struct lysc_node_leaf *)term->schema)->type;
1032
1033 /* parse the new value */
1034 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1035 LYD_JSON), cleanup);
1036
1037 /* compare original and new value */
1038 if (type->plugin->compare(&t->value, &val)) {
1039 /* values differ, switch them */
1040 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1041 t->value = val;
1042 memset(&val, 0, sizeof val);
1043 val_change = 1;
1044 } else {
1045 val_change = 0;
1046 }
1047
1048 /* always clear the default flag */
1049 if (term->flags & LYD_DEFAULT) {
1050 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1051 parent->flags &= ~LYD_DEFAULT;
1052 }
1053 dflt_change = 1;
1054 } else {
1055 dflt_change = 0;
1056 }
1057
1058 if (val_change || dflt_change) {
1059 /* make the node non-validated */
1060 term->flags &= LYD_NEW;
1061 }
1062
1063 if (val_change) {
1064 if (term->schema->nodetype == LYS_LEAFLIST) {
1065 /* leaf-list needs to be hashed again and re-inserted into parent */
1066 lyd_unlink_hash(term);
1067 lyd_hash(term);
1068 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1069 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1070 /* list needs to be updated if its key was changed */
1071 assert(term->parent->schema->nodetype == LYS_LIST);
1072 lyd_unlink_hash((struct lyd_node *)term->parent);
1073 lyd_hash((struct lyd_node *)term->parent);
1074 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1075 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1076 }
1077
1078 /* retrun value */
1079 if (!val_change) {
1080 if (dflt_change) {
1081 /* only default flag change */
1082 ret = LY_EEXIST;
1083 } else {
1084 /* no change */
1085 ret = LY_ENOT;
1086 }
1087 } /* else value changed, LY_SUCCESS */
1088
1089cleanup:
1090 type->plugin->free(LYD_NODE_CTX(term), &val);
1091 return ret;
1092}
1093
Michal Vasko41586352020-07-13 13:54:25 +02001094API LY_ERR
1095lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1096{
1097 LY_ERR ret = LY_SUCCESS;
1098 struct lyd_meta *m2;
1099 struct lyd_value val;
1100 int val_change;
1101
1102 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1103
1104 if (!val_str) {
1105 val_str = "";
1106 }
1107
1108 /* parse the new value into a new meta structure */
1109 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
1110 strlen(val_str), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, NULL), cleanup);
1111
1112 /* compare original and new value */
1113 if (lyd_compare_meta(meta, m2)) {
1114 /* values differ, switch them */
1115 val = meta->value;
1116 meta->value = m2->value;
1117 m2->value = val;
1118 val_change = 1;
1119 } else {
1120 val_change = 0;
1121 }
1122
1123 /* retrun value */
1124 if (!val_change) {
1125 /* no change */
1126 ret = LY_ENOT;
1127 } /* else value changed, LY_SUCCESS */
1128
1129cleanup:
1130 return ret;
1131}
1132
Michal Vasko3a41dff2020-07-15 14:30:28 +02001133API LY_ERR
1134lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1135 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001136{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001137 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001138}
1139
1140API LY_ERR
1141lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1142 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1143{
1144 LY_ERR ret = LY_SUCCESS, r;
1145 struct lyxp_expr *exp = NULL;
1146 struct ly_path *p = NULL;
1147 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1148 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001149 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001150 struct ly_path_predicate *pred;
1151
1152 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1153
1154 if (!ctx) {
1155 ctx = LYD_NODE_CTX(parent);
1156 }
1157
1158 /* parse path */
1159 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1160 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1161
1162 /* compile path */
1163 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1164 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1165 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1166
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001167 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1168 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001169 && !(options & LYD_NEWOPT_OPAQ)) {
1170 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1171 lys_nodetype2str(schema->nodetype), schema->name);
1172 ret = LY_EINVAL;
1173 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001174 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001175 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001176 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1177 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001178
1179 if (!value) {
1180 value = "";
1181 }
1182
1183 r = LY_SUCCESS;
1184 if (options & LYD_NEWOPT_OPAQ) {
1185 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1186 }
1187 if (!r) {
1188 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1189 NULL, LYD_JSON), cleanup);
1190 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1191 }
1192
1193 /* try to find any existing nodes in the path */
1194 if (parent) {
1195 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1196 if (ret == LY_SUCCESS) {
1197 /* the node exists, are we supposed to update it or is it just a default? */
1198 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1199 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1200 ret = LY_EEXIST;
1201 goto cleanup;
1202 }
1203
1204 /* update the existing node */
1205 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1206 goto cleanup;
1207 } else if (ret == LY_EINCOMPLETE) {
1208 /* some nodes were found, adjust the iterator to the next segment */
1209 ++path_idx;
1210 } else if (ret == LY_ENOTFOUND) {
1211 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001212 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001213 node = parent;
1214 }
1215 } else {
1216 /* error */
1217 goto cleanup;
1218 }
1219 }
1220
1221 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001222 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001223 cur_parent = node;
1224 schema = p[path_idx].node;
1225
1226 switch (schema->nodetype) {
1227 case LYS_LIST:
1228 if (!(schema->flags & LYS_KEYLESS)) {
1229 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1230 /* creating opaque list without keys */
1231 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1232 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1233 } else {
1234 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1235 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1236 }
1237 break;
1238 }
1239 /* fallthrough */
1240 case LYS_CONTAINER:
1241 case LYS_NOTIF:
1242 case LYS_RPC:
1243 case LYS_ACTION:
1244 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1245 break;
1246 case LYS_LEAFLIST:
1247 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1248 /* creating opaque leaf-list without value */
1249 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1250 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1251 } else {
1252 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1253 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1254 }
1255 break;
1256 case LYS_LEAF:
1257 /* make there is some value */
1258 if (!value) {
1259 value = "";
1260 }
1261
1262 r = LY_SUCCESS;
1263 if (options & LYD_NEWOPT_OPAQ) {
1264 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1265 }
1266 if (!r) {
1267 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1268 LYD_JSON, &node), cleanup);
1269 } else {
1270 /* creating opaque leaf without value */
1271 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1272 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1273 }
1274 break;
1275 case LYS_ANYDATA:
1276 case LYS_ANYXML:
1277 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1278 break;
1279 default:
1280 LOGINT(ctx);
1281 ret = LY_EINT;
1282 goto cleanup;
1283 }
1284
1285 if (cur_parent) {
1286 /* connect to the parent */
1287 lyd_insert_node(cur_parent, NULL, node);
1288 } else if (parent) {
1289 /* connect to top-level siblings */
1290 lyd_insert_node(NULL, &parent, node);
1291 }
1292
1293 /* update remembered nodes */
1294 if (!nparent) {
1295 nparent = node;
1296 }
1297 nnode = node;
1298 }
1299
1300cleanup:
1301 lyxp_expr_free(ctx, exp);
1302 ly_path_free(ctx, p);
1303 if (!ret) {
1304 /* set out params only on success */
1305 if (new_parent) {
1306 *new_parent = nparent;
1307 }
1308 if (new_node) {
1309 *new_node = nnode;
1310 }
1311 }
1312 return ret;
1313}
1314
Michal Vasko90932a92020-02-12 14:33:03 +01001315struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001316lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001317{
Michal Vaskob104f112020-07-17 09:54:54 +02001318 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001319 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001320 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001321
Michal Vaskob104f112020-07-17 09:54:54 +02001322 assert(new_node);
1323
1324 if (!first_sibling || !new_node->schema) {
1325 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001326 return NULL;
1327 }
1328
Michal Vaskob104f112020-07-17 09:54:54 +02001329 if (first_sibling->parent && first_sibling->parent->children_ht) {
1330 /* find the anchor using hashes */
1331 sparent = first_sibling->parent->schema;
1332 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1333 while (schema) {
1334 /* keep trying to find the first existing instance of the closest following schema sibling,
1335 * otherwise return NULL - inserting at the end */
1336 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1337 break;
1338 }
1339
1340 schema = lys_getnext(schema, sparent, NULL, 0);
1341 }
1342 } else {
1343 /* find the anchor without hashes */
1344 match = (struct lyd_node *)first_sibling;
1345 if (!lysc_data_parent(new_node->schema)) {
1346 /* we are in top-level, skip all the data from preceding modules */
1347 LY_LIST_FOR(match, match) {
1348 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1349 break;
1350 }
1351 }
1352 }
1353
1354 /* get the first schema sibling */
1355 sparent = lysc_data_parent(new_node->schema);
1356 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1357
1358 found = 0;
1359 LY_LIST_FOR(match, match) {
1360 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1361 /* we have found an opaque node, which must be at the end, so use it OR
1362 * modules do not match, so we must have traversed all the data from new_node module (if any),
1363 * we have found the first node of the next module, that is what we want */
1364 break;
1365 }
1366
1367 /* skip schema nodes until we find the instantiated one */
1368 while (!found) {
1369 if (new_node->schema == schema) {
1370 /* we have found the schema of the new node, continue search to find the first
1371 * data node with a different schema (after our schema) */
1372 found = 1;
1373 break;
1374 }
1375 if (match->schema == schema) {
1376 /* current node (match) is a data node still before the new node, continue search in data */
1377 break;
1378 }
1379 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1380 assert(schema);
1381 }
1382
1383 if (found && (match->schema != new_node->schema)) {
1384 /* find the next node after we have found our node schema data instance */
1385 break;
1386 }
1387 }
Michal Vasko90932a92020-02-12 14:33:03 +01001388 }
1389
1390 return match;
1391}
1392
1393/**
1394 * @brief Insert node after a sibling.
1395 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 * Handles inserting into NP containers and key-less lists.
1397 *
Michal Vasko90932a92020-02-12 14:33:03 +01001398 * @param[in] sibling Sibling to insert after.
1399 * @param[in] node Node to insert.
1400 */
1401static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001402lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001403{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001404 struct lyd_node_inner *par;
1405
Michal Vasko90932a92020-02-12 14:33:03 +01001406 assert(!node->next && (node->prev == node));
1407
1408 node->next = sibling->next;
1409 node->prev = sibling;
1410 sibling->next = node;
1411 if (node->next) {
1412 /* sibling had a succeeding node */
1413 node->next->prev = node;
1414 } else {
1415 /* sibling was last, find first sibling and change its prev */
1416 if (sibling->parent) {
1417 sibling = sibling->parent->child;
1418 } else {
1419 for (; sibling->prev->next != node; sibling = sibling->prev);
1420 }
1421 sibling->prev = node;
1422 }
1423 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001424
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 for (par = node->parent; par; par = par->parent) {
1426 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1427 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001428 par->flags &= ~LYD_DEFAULT;
1429 }
Michal Vaskob104f112020-07-17 09:54:54 +02001430 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 /* rehash key-less list */
1432 lyd_hash((struct lyd_node *)par);
1433 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001434 }
Michal Vasko90932a92020-02-12 14:33:03 +01001435}
1436
1437/**
1438 * @brief Insert node before a sibling.
1439 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 * Handles inserting into NP containers and key-less lists.
1441 *
Michal Vasko90932a92020-02-12 14:33:03 +01001442 * @param[in] sibling Sibling to insert before.
1443 * @param[in] node Node to insert.
1444 */
1445static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001446lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001447{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001448 struct lyd_node_inner *par;
1449
Michal Vasko90932a92020-02-12 14:33:03 +01001450 assert(!node->next && (node->prev == node));
1451
1452 node->next = sibling;
1453 /* covers situation of sibling being first */
1454 node->prev = sibling->prev;
1455 sibling->prev = node;
1456 if (node->prev->next) {
1457 /* sibling had a preceding node */
1458 node->prev->next = node;
1459 } else if (sibling->parent) {
1460 /* sibling was first and we must also change parent child pointer */
1461 sibling->parent->child = node;
1462 }
1463 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001464
Michal Vasko9f96a052020-03-10 09:41:45 +01001465 for (par = node->parent; par; par = par->parent) {
1466 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1467 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001468 par->flags &= ~LYD_DEFAULT;
1469 }
Michal Vaskob104f112020-07-17 09:54:54 +02001470 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 /* rehash key-less list */
1472 lyd_hash((struct lyd_node *)par);
1473 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001474 }
Michal Vasko90932a92020-02-12 14:33:03 +01001475}
1476
1477/**
Michal Vaskob104f112020-07-17 09:54:54 +02001478 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001479 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001480 * Handles inserting into NP containers and key-less lists.
1481 *
Michal Vasko90932a92020-02-12 14:33:03 +01001482 * @param[in] parent Parent to insert into.
1483 * @param[in] node Node to insert.
1484 */
1485static void
Michal Vaskob104f112020-07-17 09:54:54 +02001486lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001487{
1488 struct lyd_node_inner *par;
1489
Michal Vaskob104f112020-07-17 09:54:54 +02001490 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001491 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001492
1493 par = (struct lyd_node_inner *)parent;
1494
Michal Vaskob104f112020-07-17 09:54:54 +02001495 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001496 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001497
Michal Vasko9f96a052020-03-10 09:41:45 +01001498 for (; par; par = par->parent) {
1499 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1500 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001501 par->flags &= ~LYD_DEFAULT;
1502 }
Michal Vasko52927e22020-03-16 17:26:14 +01001503 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001504 /* rehash key-less list */
1505 lyd_hash((struct lyd_node *)par);
1506 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001507 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001508}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001509
Michal Vasko751cb4d2020-07-14 12:25:28 +02001510/**
1511 * @brief Learn whether a list instance has all the keys.
1512 *
1513 * @param[in] list List instance to check.
1514 * @return non-zero if all the keys were found,
1515 * @return 0 otherwise.
1516 */
1517static int
1518lyd_insert_has_keys(const struct lyd_node *list)
1519{
1520 const struct lyd_node *key;
1521 const struct lysc_node *skey = NULL;
1522
1523 assert(list->schema->nodetype == LYS_LIST);
1524 key = lyd_node_children(list, 0);
1525 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1526 if (!key || (key->schema != skey)) {
1527 /* key missing */
1528 return 0;
1529 }
1530
1531 key = key->next;
1532 }
1533
1534 /* all keys found */
1535 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001536}
1537
1538void
Michal Vaskob104f112020-07-17 09:54:54 +02001539lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001540{
Michal Vaskob104f112020-07-17 09:54:54 +02001541 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001542
Michal Vaskob104f112020-07-17 09:54:54 +02001543 /* inserting list without its keys is not supported */
1544 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001545
Michal Vaskob104f112020-07-17 09:54:54 +02001546 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1547 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001548 }
Michal Vasko90932a92020-02-12 14:33:03 +01001549
Michal Vaskob104f112020-07-17 09:54:54 +02001550 /* get first sibling */
1551 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001552
Michal Vaskob104f112020-07-17 09:54:54 +02001553 /* find the anchor, our next node, so we can insert before it */
1554 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1555 if (anchor) {
1556 lyd_insert_before_node(anchor, node);
1557 } else if (first_sibling) {
1558 lyd_insert_after_node(first_sibling->prev, node);
1559 } else if (parent) {
1560 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001561 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001562 *first_sibling_p = node;
1563 }
1564
1565 /* insert into parent HT */
1566 lyd_insert_hash(node);
1567
1568 /* finish hashes for our parent, if needed and possible */
1569 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1570 lyd_hash(parent);
1571
1572 /* now we can insert even the list into its parent HT */
1573 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001574 }
Michal Vasko90932a92020-02-12 14:33:03 +01001575}
1576
Michal Vaskof03ed032020-03-04 13:31:44 +01001577static LY_ERR
1578lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1579{
1580 const struct lysc_node *par2;
1581
1582 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001583 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001584
1585 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001586 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001587
1588 if (parent) {
1589 /* inner node */
1590 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001591 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1592 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001593 return LY_EINVAL;
1594 }
1595 } else {
1596 /* top-level node */
1597 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001598 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001599 return LY_EINVAL;
1600 }
1601 }
1602
1603 return LY_SUCCESS;
1604}
1605
1606API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001607lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001608{
1609 struct lyd_node *iter;
1610
Michal Vasko654bc852020-06-23 13:28:06 +02001611 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001612
1613 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1614
1615 if (node->schema->flags & LYS_KEY) {
1616 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1617 return LY_EINVAL;
1618 }
1619
1620 if (node->parent || node->prev->next) {
1621 lyd_unlink_tree(node);
1622 }
1623
1624 while (node) {
1625 iter = node->next;
1626 lyd_unlink_tree(node);
1627 lyd_insert_node(parent, NULL, node);
1628 node = iter;
1629 }
1630 return LY_SUCCESS;
1631}
1632
1633API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001634lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001635{
1636 struct lyd_node *iter;
1637
Michal Vaskob104f112020-07-17 09:54:54 +02001638 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001639
Michal Vaskob104f112020-07-17 09:54:54 +02001640 if (sibling) {
1641 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001642 }
1643
1644 if (node->parent || node->prev->next) {
1645 lyd_unlink_tree(node);
1646 }
1647
1648 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001649 if (node->schema->flags & LYS_KEY) {
1650 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1651 return LY_EINVAL;
1652 }
1653
Michal Vaskob1b5c262020-03-05 14:29:47 +01001654 iter = node->next;
1655 lyd_unlink_tree(node);
1656 lyd_insert_node(NULL, &sibling, node);
1657 node = iter;
1658 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001659
Michal Vaskob104f112020-07-17 09:54:54 +02001660 if (first) {
1661 /* find the first sibling */
1662 *first = sibling;
1663 while ((*first)->prev->next) {
1664 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001665 }
1666 }
1667
1668 return LY_SUCCESS;
1669}
1670
Michal Vaskob1b5c262020-03-05 14:29:47 +01001671API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001672lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1673{
1674 struct lyd_node *iter;
1675
1676 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1677
Michal Vasko62ed12d2020-05-21 10:08:25 +02001678 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001679
Michal Vaskob104f112020-07-17 09:54:54 +02001680 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1681 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001682 return LY_EINVAL;
1683 }
1684
1685 if (node->parent || node->prev->next) {
1686 lyd_unlink_tree(node);
1687 }
1688
1689 /* insert in reverse order to get the original order */
1690 node = node->prev;
1691 while (node) {
1692 iter = node->prev;
1693 lyd_unlink_tree(node);
1694
1695 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001696 lyd_insert_hash(node);
1697
Michal Vaskof03ed032020-03-04 13:31:44 +01001698 /* move the anchor accordingly */
1699 sibling = node;
1700
1701 node = (iter == node) ? NULL : iter;
1702 }
1703 return LY_SUCCESS;
1704}
1705
1706API LY_ERR
1707lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1708{
1709 struct lyd_node *iter;
1710
1711 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1712
Michal Vasko62ed12d2020-05-21 10:08:25 +02001713 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001714
Michal Vaskob104f112020-07-17 09:54:54 +02001715 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1716 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001717 return LY_EINVAL;
1718 }
1719
1720 if (node->parent || node->prev->next) {
1721 lyd_unlink_tree(node);
1722 }
1723
1724 while (node) {
1725 iter = node->next;
1726 lyd_unlink_tree(node);
1727
1728 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001729 lyd_insert_hash(node);
1730
Michal Vaskof03ed032020-03-04 13:31:44 +01001731 /* move the anchor accordingly */
1732 sibling = node;
1733
1734 node = iter;
1735 }
1736 return LY_SUCCESS;
1737}
1738
1739API void
1740lyd_unlink_tree(struct lyd_node *node)
1741{
1742 struct lyd_node *iter;
1743
1744 if (!node) {
1745 return;
1746 }
1747
Michal Vaskob104f112020-07-17 09:54:54 +02001748 /* update hashes while still linked into the tree */
1749 lyd_unlink_hash(node);
1750
Michal Vaskof03ed032020-03-04 13:31:44 +01001751 /* unlink from siblings */
1752 if (node->prev->next) {
1753 node->prev->next = node->next;
1754 }
1755 if (node->next) {
1756 node->next->prev = node->prev;
1757 } else {
1758 /* unlinking the last node */
1759 if (node->parent) {
1760 iter = node->parent->child;
1761 } else {
1762 iter = node->prev;
1763 while (iter->prev != node) {
1764 iter = iter->prev;
1765 }
1766 }
1767 /* update the "last" pointer from the first node */
1768 iter->prev = node->prev;
1769 }
1770
1771 /* unlink from parent */
1772 if (node->parent) {
1773 if (node->parent->child == node) {
1774 /* the node is the first child */
1775 node->parent->child = node->next;
1776 }
1777
Michal Vaskof03ed032020-03-04 13:31:44 +01001778 /* check for keyless list and update its hash */
1779 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001780 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001781 lyd_hash(iter);
1782 }
1783 }
1784
1785 node->parent = NULL;
1786 }
1787
1788 node->next = NULL;
1789 node->prev = node;
1790}
1791
Michal Vasko90932a92020-02-12 14:33:03 +01001792LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001793lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko52927e22020-03-16 17:26:14 +01001794 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix resolve_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01001795 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001796{
1797 LY_ERR ret;
1798 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001799 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001800 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001801
Michal Vasko9f96a052020-03-10 09:41:45 +01001802 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001803
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001804 LY_ARRAY_FOR(mod->compiled->exts, u) {
1805 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1806 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001807 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001808 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001809 break;
1810 }
1811 }
1812 if (!ant) {
1813 /* attribute is not defined as a metadata annotation (RFC 7952) */
1814 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1815 mod->name, name_len, name);
1816 return LY_EINVAL;
1817 }
1818
Michal Vasko9f96a052020-03-10 09:41:45 +01001819 mt = calloc(1, sizeof *mt);
1820 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1821 mt->parent = parent;
1822 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001823 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, resolve_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01001824 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001825 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001826 return ret;
1827 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001828 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001829
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001830 /* insert as the last attribute */
1831 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001832 if (parent->meta) {
1833 for (last = parent->meta; last->next; last = last->next);
1834 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001835 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001836 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001837 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001838 } else if (*meta) {
1839 for (last = *meta; last->next; last = last->next);
1840 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001841 }
1842
1843 /* remove default flags from NP containers */
Michal Vasko4ba6aef2020-07-09 15:44:23 +02001844 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001845 parent->flags &= ~LYD_DEFAULT;
1846 parent = (struct lyd_node *)parent->parent;
1847 }
1848
Michal Vasko9f96a052020-03-10 09:41:45 +01001849 if (meta) {
1850 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001851 }
1852 return ret;
1853}
1854
Michal Vasko52927e22020-03-16 17:26:14 +01001855LY_ERR
1856ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1857 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1858 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1859{
1860 struct ly_attr *at, *last;
1861 struct lyd_node_opaq *opaq;
1862
1863 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1864 assert(name && name_len);
1865 assert((prefix_len && ns) || (!prefix_len && !ns));
1866
1867 if (!value_len) {
1868 value = "";
1869 }
1870
1871 at = calloc(1, sizeof *at);
1872 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1873 at->parent = (struct lyd_node_opaq *)parent;
1874 at->name = lydict_insert(ctx, name, name_len);
1875 if (dynamic && *dynamic) {
1876 at->value = lydict_insert_zc(ctx, (char *)value);
1877 *dynamic = 0;
1878 } else {
1879 at->value = lydict_insert(ctx, value, value_len);
1880 }
1881
1882 at->format = format;
1883 at->val_prefs = val_prefs;
1884 if (ns) {
1885 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1886 at->prefix.ns = lydict_insert(ctx, ns, 0);
1887 }
1888
1889 /* insert as the last attribute */
1890 if (parent) {
1891 opaq = (struct lyd_node_opaq *)parent;
1892 if (opaq->attr) {
1893 for (last = opaq->attr; last->next; last = last->next);
1894 last->next = at;
1895 } else {
1896 opaq->attr = at;
1897 }
1898 } else if (*attr) {
1899 for (last = *attr; last->next; last = last->next);
1900 last->next = at;
1901 }
1902
1903 if (attr) {
1904 *attr = at;
1905 }
1906 return LY_SUCCESS;
1907}
1908
Radek Krejci084289f2019-07-09 17:35:30 +02001909API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001910lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001911{
Michal Vasko004d3152020-06-11 19:59:22 +02001912 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001913
Michal Vasko004d3152020-06-11 19:59:22 +02001914 if (ly_path_eval(path, tree, &target)) {
1915 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001916 }
1917
Michal Vasko004d3152020-06-11 19:59:22 +02001918 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001919}
1920
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001921API LY_ERR
1922lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1923{
1924 const struct lyd_node *iter1, *iter2;
1925 struct lyd_node_term *term1, *term2;
1926 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001927 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001928 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001929
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001930 if (!node1 || !node2) {
1931 if (node1 == node2) {
1932 return LY_SUCCESS;
1933 } else {
1934 return LY_ENOT;
1935 }
1936 }
1937
Michal Vasko52927e22020-03-16 17:26:14 +01001938 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001939 return LY_ENOT;
1940 }
1941
1942 if (node1->hash != node2->hash) {
1943 return LY_ENOT;
1944 }
Michal Vasko52927e22020-03-16 17:26:14 +01001945 /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001946
Michal Vasko52927e22020-03-16 17:26:14 +01001947 if (!node1->schema) {
1948 opaq1 = (struct lyd_node_opaq *)node1;
1949 opaq2 = (struct lyd_node_opaq *)node2;
1950 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001951 return LY_ENOT;
1952 }
Michal Vasko52927e22020-03-16 17:26:14 +01001953 switch (opaq1->format) {
1954 case LYD_XML:
1955 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1956 return LY_ENOT;
1957 }
1958 break;
1959 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001960 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001961 /* not allowed */
1962 LOGINT(LYD_NODE_CTX(node1));
1963 return LY_EINT;
1964 }
1965 if (options & LYD_COMPARE_FULL_RECURSION) {
1966 iter1 = opaq1->child;
1967 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001968 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001969 }
1970 return LY_SUCCESS;
1971 } else {
1972 switch (node1->schema->nodetype) {
1973 case LYS_LEAF:
1974 case LYS_LEAFLIST:
1975 if (options & LYD_COMPARE_DEFAULTS) {
1976 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1977 return LY_ENOT;
1978 }
1979 }
1980
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001981 term1 = (struct lyd_node_term *)node1;
1982 term2 = (struct lyd_node_term *)node2;
1983 if (term1->value.realtype != term2->value.realtype) {
1984 return LY_ENOT;
1985 }
Michal Vasko52927e22020-03-16 17:26:14 +01001986
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001987 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001988 case LYS_CONTAINER:
1989 if (options & LYD_COMPARE_DEFAULTS) {
1990 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1991 return LY_ENOT;
1992 }
1993 }
1994 if (options & LYD_COMPARE_FULL_RECURSION) {
1995 iter1 = ((struct lyd_node_inner*)node1)->child;
1996 iter2 = ((struct lyd_node_inner*)node2)->child;
1997 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001998 }
1999 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002000 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002001 case LYS_ACTION:
2002 if (options & LYD_COMPARE_FULL_RECURSION) {
2003 /* TODO action/RPC
2004 goto all_children_compare;
2005 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002006 }
2007 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002008 case LYS_NOTIF:
2009 if (options & LYD_COMPARE_FULL_RECURSION) {
2010 /* TODO Notification
2011 goto all_children_compare;
2012 */
2013 }
2014 return LY_SUCCESS;
2015 case LYS_LIST:
2016 iter1 = ((struct lyd_node_inner*)node1)->child;
2017 iter2 = ((struct lyd_node_inner*)node2)->child;
2018
2019 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2020 /* lists with keys, their equivalence is based on their keys */
2021 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002022 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002023 key = key->next) {
2024 if (lyd_compare(iter1, iter2, options)) {
2025 return LY_ENOT;
2026 }
2027 iter1 = iter1->next;
2028 iter2 = iter2->next;
2029 }
2030 } else {
2031 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2032
2033 all_children_compare:
2034 if (!iter1 && !iter2) {
2035 /* no children, nothing to compare */
2036 return LY_SUCCESS;
2037 }
2038
2039 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
2040 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
2041 return LY_ENOT;
2042 }
2043 }
2044 if (iter1 || iter2) {
2045 return LY_ENOT;
2046 }
2047 }
2048 return LY_SUCCESS;
2049 case LYS_ANYXML:
2050 case LYS_ANYDATA:
2051 any1 = (struct lyd_node_any*)node1;
2052 any2 = (struct lyd_node_any*)node2;
2053
2054 if (any1->value_type != any2->value_type) {
2055 return LY_ENOT;
2056 }
2057 switch (any1->value_type) {
2058 case LYD_ANYDATA_DATATREE:
2059 iter1 = any1->value.tree;
2060 iter2 = any2->value.tree;
2061 goto all_children_compare;
2062 case LYD_ANYDATA_STRING:
2063 case LYD_ANYDATA_XML:
2064 case LYD_ANYDATA_JSON:
2065 len1 = strlen(any1->value.str);
2066 len2 = strlen(any2->value.str);
2067 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2068 return LY_ENOT;
2069 }
2070 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002071 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002072 len1 = lyd_lyb_data_length(any1->value.mem);
2073 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002074 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2075 return LY_ENOT;
2076 }
2077 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002078 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002079 }
2080 }
2081
Michal Vasko52927e22020-03-16 17:26:14 +01002082 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002083 return LY_EINT;
2084}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002085
Michal Vasko21725742020-06-29 11:49:25 +02002086API LY_ERR
2087lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2088{
2089 if (!meta1 || !meta2) {
2090 if (meta1 == meta2) {
2091 return LY_SUCCESS;
2092 } else {
2093 return LY_ENOT;
2094 }
2095 }
2096
2097 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2098 return LY_ENOT;
2099 }
2100
2101 if (meta1->value.realtype != meta2->value.realtype) {
2102 return LY_ENOT;
2103 }
2104
2105 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2106}
2107
Radek Krejci22ebdba2019-07-25 13:59:43 +02002108/**
Michal Vasko52927e22020-03-16 17:26:14 +01002109 * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings.
Radek Krejci22ebdba2019-07-25 13:59:43 +02002110 *
2111 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02002112 *
2113 * @param[in] node Original node to duplicate
2114 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2115 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2116 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2117 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2118 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002119 */
Michal Vasko52927e22020-03-16 17:26:14 +01002120static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002121lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2122 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002123{
Michal Vasko52927e22020-03-16 17:26:14 +01002124 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002125 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002126 struct lyd_meta *meta;
2127 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002128 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002129
Michal Vasko52927e22020-03-16 17:26:14 +01002130 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002131
Michal Vasko52927e22020-03-16 17:26:14 +01002132 if (!node->schema) {
2133 dup = calloc(1, sizeof(struct lyd_node_opaq));
2134 } else {
2135 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002136 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002137 case LYS_ACTION:
2138 case LYS_NOTIF:
2139 case LYS_CONTAINER:
2140 case LYS_LIST:
2141 dup = calloc(1, sizeof(struct lyd_node_inner));
2142 break;
2143 case LYS_LEAF:
2144 case LYS_LEAFLIST:
2145 dup = calloc(1, sizeof(struct lyd_node_term));
2146 break;
2147 case LYS_ANYDATA:
2148 case LYS_ANYXML:
2149 dup = calloc(1, sizeof(struct lyd_node_any));
2150 break;
2151 default:
2152 LOGINT(LYD_NODE_CTX(node));
2153 ret = LY_EINT;
2154 goto error;
2155 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002156 }
Michal Vasko52927e22020-03-16 17:26:14 +01002157 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002158
Michal Vaskof6df0a02020-06-16 13:08:34 +02002159 if (options & LYD_DUP_WITH_FLAGS) {
2160 dup->flags = node->flags;
2161 } else {
2162 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2163 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002164 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002165 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002166
Michal Vasko25a32822020-07-09 15:48:22 +02002167 /* duplicate metadata */
2168 if (!(options & LYD_DUP_NO_META)) {
2169 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002170 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002171 }
2172 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002173
2174 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002175 if (!dup->schema) {
2176 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2177 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2178 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002179
2180 if (options & LYD_DUP_RECURSIVE) {
2181 /* duplicate all the children */
2182 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002183 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002184 }
2185 }
2186 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2187 opaq->format = orig->format;
2188 if (orig->prefix.pref) {
2189 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2190 }
2191 if (orig->prefix.ns) {
2192 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2193 }
2194 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002195 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002196 LY_ARRAY_FOR(orig->val_prefs, u) {
2197 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2198 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2199 LY_ARRAY_INCREMENT(opaq->val_prefs);
2200 }
2201 }
2202 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2203 opaq->ctx = orig->ctx;
2204 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2205 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2206 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2207
2208 term->hash = orig->hash;
2209 term->value.realtype = orig->value.realtype;
2210 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2211 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2212 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2213 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2214 struct lyd_node *child;
2215
2216 if (options & LYD_DUP_RECURSIVE) {
2217 /* duplicate all the children */
2218 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002219 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002220 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002221 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002222 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002223 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002224 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002225 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002226 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002227 if (!child) {
2228 /* possibly not keys are present in filtered tree */
2229 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002230 } else if (child->schema != key) {
2231 /* possibly not all keys are present in filtered tree,
2232 * but there can be also some non-key nodes */
2233 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002234 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002235 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002236 child = child->next;
2237 }
2238 }
2239 lyd_hash(dup);
2240 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002241 dup->hash = node->hash;
2242 any = (struct lyd_node_any *)node;
2243 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002244 }
2245
Michal Vasko52927e22020-03-16 17:26:14 +01002246 /* insert */
2247 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002248
2249 if (dup_p) {
2250 *dup_p = dup;
2251 }
2252 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002253
2254error:
Michal Vasko52927e22020-03-16 17:26:14 +01002255 lyd_free_tree(dup);
2256 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002257}
2258
Michal Vasko3a41dff2020-07-15 14:30:28 +02002259static LY_ERR
2260lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2261 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002262{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002263 const struct lyd_node_inner *orig_parent, *iter;
2264 int repeat = 1;
2265
2266 *dup_parent = NULL;
2267 *local_parent = NULL;
2268
2269 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2270 if (parent && (parent->schema == orig_parent->schema)) {
2271 /* stop creating parents, connect what we have into the provided parent */
2272 iter = parent;
2273 repeat = 0;
2274 } else {
2275 iter = NULL;
2276 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2277 (struct lyd_node **)&iter));
2278 }
2279 if (!*local_parent) {
2280 *local_parent = (struct lyd_node_inner *)iter;
2281 }
2282 if (iter->child) {
2283 /* 1) list - add after keys
2284 * 2) provided parent with some children */
2285 iter->child->prev->next = *dup_parent;
2286 if (*dup_parent) {
2287 (*dup_parent)->prev = iter->child->prev;
2288 iter->child->prev = *dup_parent;
2289 }
2290 } else {
2291 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2292 }
2293 if (*dup_parent) {
2294 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2295 }
2296 *dup_parent = (struct lyd_node *)iter;
2297 }
2298
2299 if (repeat && parent) {
2300 /* given parent and created parents chain actually do not interconnect */
2301 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2302 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2303 return LY_EINVAL;
2304 }
2305
2306 return LY_SUCCESS;
2307}
2308
2309static LY_ERR
2310lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2311{
2312 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002313 const struct lyd_node *orig; /* original node to be duplicated */
2314 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002315 struct lyd_node *top = NULL; /* the most higher created node */
2316 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002317
Michal Vasko3a41dff2020-07-15 14:30:28 +02002318 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002319
2320 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002321 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002322 } else {
2323 local_parent = parent;
2324 }
2325
Radek Krejci22ebdba2019-07-25 13:59:43 +02002326 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002327 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002328 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2329 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002330 break;
2331 }
2332 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002333
2334 /* rehash if needed */
2335 for (; local_parent; local_parent = local_parent->parent) {
2336 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2337 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002338 }
2339 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002340
2341 if (dup) {
2342 *dup = first;
2343 }
2344 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002345
2346error:
2347 if (top) {
2348 lyd_free_tree(top);
2349 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002350 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002351 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002352 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002353}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002354
Michal Vasko3a41dff2020-07-15 14:30:28 +02002355API LY_ERR
2356lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2357{
2358 return lyd_dup(node, parent, options, 1, dup);
2359}
2360
2361API LY_ERR
2362lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2363{
2364 return lyd_dup(node, parent, options, 0, dup);
2365}
2366
2367API LY_ERR
2368lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002369{
2370 LY_ERR ret;
2371 struct lyd_meta *mt, *last;
2372
Michal Vasko3a41dff2020-07-15 14:30:28 +02002373 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002374
2375 /* create a copy */
2376 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002377 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002378 mt->parent = node;
2379 mt->annotation = meta->annotation;
2380 mt->value.realtype = meta->value.realtype;
2381 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002382 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002383 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2384
2385 /* insert as the last attribute */
2386 if (node->meta) {
2387 for (last = node->meta; last->next; last = last->next);
2388 last->next = mt;
2389 } else {
2390 node->meta = mt;
2391 }
2392
Michal Vasko3a41dff2020-07-15 14:30:28 +02002393 if (dup) {
2394 *dup = mt;
2395 }
2396 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002397}
2398
Michal Vasko4490d312020-06-16 13:08:55 +02002399/**
2400 * @brief Merge a source sibling into target siblings.
2401 *
2402 * @param[in,out] first_trg First target sibling, is updated if top-level.
2403 * @param[in] parent_trg Target parent.
2404 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2405 * @param[in] options Merge options.
2406 * @return LY_ERR value.
2407 */
2408static LY_ERR
2409lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2410 int options)
2411{
2412 LY_ERR ret;
2413 const struct lyd_node *child_src, *tmp, *sibling_src;
2414 struct lyd_node *match_trg, *dup_src, *next, *elem;
2415 struct lysc_type *type;
2416 LYD_ANYDATA_VALUETYPE tmp_val_type;
2417 union lyd_any_value tmp_val;
2418
2419 sibling_src = *sibling_src_p;
2420 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2421 /* try to find the exact instance */
2422 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2423 } else {
2424 /* try to simply find the node, there cannot be more instances */
2425 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2426 }
2427
2428 if (!ret) {
2429 /* node found, make sure even value matches for all node types */
2430 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2431 /* since they are different, they cannot both be default */
2432 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2433
Michal Vasko3a41dff2020-07-15 14:30:28 +02002434 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2435 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002436 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2437 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2438 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2439 &((struct lyd_node_term *)match_trg)->value));
2440
2441 /* copy flags and add LYD_NEW */
2442 match_trg->flags = sibling_src->flags | LYD_NEW;
2443 }
2444 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2445 if (options & LYD_MERGE_DESTRUCT) {
2446 dup_src = (struct lyd_node *)sibling_src;
2447 lyd_unlink_tree(dup_src);
2448 /* spend it */
2449 *sibling_src_p = NULL;
2450 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002451 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, 0, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002452 }
2453 /* just switch values */
2454 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2455 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2456 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2457 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2458 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2459 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2460
2461 /* copy flags and add LYD_NEW */
2462 match_trg->flags = sibling_src->flags | LYD_NEW;
2463
2464 /* dup_src is not needed, actually */
2465 lyd_free_tree(dup_src);
2466 } else {
2467 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002468 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002469 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2470 }
2471 }
2472 } else {
2473 /* node not found, merge it */
2474 if (options & LYD_MERGE_DESTRUCT) {
2475 dup_src = (struct lyd_node *)sibling_src;
2476 lyd_unlink_tree(dup_src);
2477 /* spend it */
2478 *sibling_src_p = NULL;
2479 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002480 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002481 }
2482
2483 /* set LYD_NEW for all the new nodes, required for validation */
2484 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2485 elem->flags |= LYD_NEW;
2486 LYD_TREE_DFS_END(dup_src, next, elem);
2487 }
2488
2489 lyd_insert_node(parent_trg, first_trg, dup_src);
2490 }
2491
2492 return LY_SUCCESS;
2493}
2494
Michal Vasko3a41dff2020-07-15 14:30:28 +02002495static LY_ERR
2496lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002497{
2498 const struct lyd_node *sibling_src, *tmp;
2499 int first;
2500
2501 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2502
2503 if (!source) {
2504 /* nothing to merge */
2505 return LY_SUCCESS;
2506 }
2507
2508 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2509 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2510 return LY_EINVAL;
2511 }
2512
2513 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2514 first = sibling_src == source ? 1 : 0;
2515 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2516 if (first && !sibling_src) {
2517 /* source was spent (unlinked), move to the next node */
2518 source = tmp;
2519 }
2520
Michal Vasko3a41dff2020-07-15 14:30:28 +02002521 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002522 break;
2523 }
2524 }
2525
2526 if (options & LYD_MERGE_DESTRUCT) {
2527 /* free any leftover source data that were not merged */
2528 lyd_free_siblings((struct lyd_node *)source);
2529 }
2530
2531 return LY_SUCCESS;
2532}
2533
Michal Vasko3a41dff2020-07-15 14:30:28 +02002534API LY_ERR
2535lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2536{
2537 return lyd_merge(target, source, options, 1);
2538}
2539
2540API LY_ERR
2541lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2542{
2543 return lyd_merge(target, source, options, 0);
2544}
2545
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002546static LY_ERR
2547lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2548{
Michal Vasko14654712020-02-06 08:35:21 +01002549 /* ending \0 */
2550 ++reqlen;
2551
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002552 if (reqlen > *buflen) {
2553 if (is_static) {
2554 return LY_EINCOMPLETE;
2555 }
2556
2557 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2558 if (!*buffer) {
2559 return LY_EMEM;
2560 }
2561
2562 *buflen = reqlen;
2563 }
2564
2565 return LY_SUCCESS;
2566}
2567
Michal Vaskod59035b2020-07-08 12:00:06 +02002568LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002569lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2570{
2571 const struct lyd_node *key;
2572 int dynamic = 0;
2573 size_t len;
2574 const char *val;
2575 char quot;
2576 LY_ERR rc;
2577
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002578 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002579 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2580 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2581 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2582 if (rc != LY_SUCCESS) {
2583 if (dynamic) {
2584 free((char *)val);
2585 }
2586 return rc;
2587 }
2588
2589 quot = '\'';
2590 if (strchr(val, '\'')) {
2591 quot = '"';
2592 }
2593 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2594
2595 if (dynamic) {
2596 free((char *)val);
2597 }
2598 }
2599
2600 return LY_SUCCESS;
2601}
2602
2603/**
2604 * @brief Append leaf-list value predicate to path.
2605 *
2606 * @param[in] node Node to print.
2607 * @param[in,out] buffer Buffer to print to.
2608 * @param[in,out] buflen Current buffer length.
2609 * @param[in,out] bufused Current number of characters used in @p buffer.
2610 * @param[in] is_static Whether buffer is static or can be reallocated.
2611 * @return LY_ERR
2612 */
2613static LY_ERR
2614lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2615{
2616 int dynamic = 0;
2617 size_t len;
2618 const char *val;
2619 char quot;
2620 LY_ERR rc;
2621
2622 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2623 len = 4 + strlen(val) + 2;
2624 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2625 if (rc != LY_SUCCESS) {
2626 goto cleanup;
2627 }
2628
2629 quot = '\'';
2630 if (strchr(val, '\'')) {
2631 quot = '"';
2632 }
2633 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2634
2635cleanup:
2636 if (dynamic) {
2637 free((char *)val);
2638 }
2639 return rc;
2640}
2641
2642/**
2643 * @brief Append node position (relative to its other instances) predicate to path.
2644 *
2645 * @param[in] node Node to print.
2646 * @param[in,out] buffer Buffer to print to.
2647 * @param[in,out] buflen Current buffer length.
2648 * @param[in,out] bufused Current number of characters used in @p buffer.
2649 * @param[in] is_static Whether buffer is static or can be reallocated.
2650 * @return LY_ERR
2651 */
2652static LY_ERR
2653lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2654{
2655 const struct lyd_node *first, *iter;
2656 size_t len;
2657 int pos;
2658 char *val = NULL;
2659 LY_ERR rc;
2660
2661 if (node->parent) {
2662 first = node->parent->child;
2663 } else {
2664 for (first = node; node->prev->next; node = node->prev);
2665 }
2666 pos = 1;
2667 for (iter = first; iter != node; iter = iter->next) {
2668 if (iter->schema == node->schema) {
2669 ++pos;
2670 }
2671 }
2672 if (asprintf(&val, "%d", pos) == -1) {
2673 return LY_EMEM;
2674 }
2675
2676 len = 1 + strlen(val) + 1;
2677 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2678 if (rc != LY_SUCCESS) {
2679 goto cleanup;
2680 }
2681
2682 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2683
2684cleanup:
2685 free(val);
2686 return rc;
2687}
2688
2689API char *
2690lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2691{
Michal Vasko14654712020-02-06 08:35:21 +01002692 int is_static = 0, i, depth;
2693 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002694 const struct lyd_node *iter;
2695 const struct lys_module *mod;
2696 LY_ERR rc;
2697
2698 LY_CHECK_ARG_RET(NULL, node, NULL);
2699 if (buffer) {
2700 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2701 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002702 } else {
2703 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002704 }
2705
2706 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002707 case LYD_PATH_LOG:
2708 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002709 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2710 ++depth;
2711 }
2712
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002713 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002714 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002715 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002716 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002717iter_print:
2718 /* print prefix and name */
2719 mod = NULL;
2720 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2721 mod = iter->schema->module;
2722 }
2723
2724 /* realloc string */
2725 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2726 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2727 if (rc != LY_SUCCESS) {
2728 break;
2729 }
2730
2731 /* print next node */
2732 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2733
2734 switch (iter->schema->nodetype) {
2735 case LYS_LIST:
2736 if (iter->schema->flags & LYS_KEYLESS) {
2737 /* print its position */
2738 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2739 } else {
2740 /* print all list keys in predicates */
2741 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2742 }
2743 break;
2744 case LYS_LEAFLIST:
2745 if (iter->schema->flags & LYS_CONFIG_W) {
2746 /* print leaf-list value */
2747 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2748 } else {
2749 /* print its position */
2750 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2751 }
2752 break;
2753 default:
2754 /* nothing to print more */
2755 rc = LY_SUCCESS;
2756 break;
2757 }
2758 if (rc != LY_SUCCESS) {
2759 break;
2760 }
2761
Michal Vasko14654712020-02-06 08:35:21 +01002762 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002763 }
2764 break;
2765 }
2766
2767 return buffer;
2768}
Michal Vaskoe444f752020-02-10 12:20:06 +01002769
Michal Vasko25a32822020-07-09 15:48:22 +02002770API struct lyd_meta *
2771lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2772{
2773 struct lyd_meta *ret = NULL;
2774 const struct ly_ctx *ctx;
2775 const char *prefix, *tmp;
2776 char *str;
2777 size_t pref_len, name_len;
2778
2779 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2780
2781 if (!first) {
2782 return NULL;
2783 }
2784
2785 ctx = first->annotation->module->ctx;
2786
2787 /* parse the name */
2788 tmp = name;
2789 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2790 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2791 return NULL;
2792 }
2793
2794 /* find the module */
2795 if (prefix) {
2796 str = strndup(prefix, pref_len);
2797 module = ly_ctx_get_module_latest(ctx, str);
2798 free(str);
2799 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2800 }
2801
2802 /* find the metadata */
2803 LY_LIST_FOR(first, first) {
2804 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2805 ret = (struct lyd_meta *)first;
2806 break;
2807 }
2808 }
2809
2810 return ret;
2811}
2812
Michal Vasko9b368d32020-02-14 13:53:31 +01002813LY_ERR
2814lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2815 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002816{
2817 LY_ERR rc;
2818 const struct lyd_node *node = NULL;
2819 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02002820 struct lyxp_expr *expr = NULL;
2821 uint16_t exp_idx = 0;
2822 struct ly_path_predicate *predicates = NULL;
2823 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01002824 struct lyd_value val = {0};
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002825 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01002826
Michal Vasko9b368d32020-02-14 13:53:31 +01002827 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002828
2829 if (!first) {
2830 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002831 if (match) {
2832 *match = NULL;
2833 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002834 return LY_ENOTFOUND;
2835 }
2836
Michal Vaskoe444f752020-02-10 12:20:06 +01002837 if (key_or_value && !val_len) {
2838 val_len = strlen(key_or_value);
2839 }
2840
2841 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002842 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002843 LY_CHECK_GOTO(rc = lyd_value_store(&val, schema, key_or_value, val_len, 0, lydjson_resolve_prefix, NULL, LYD_JSON), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002844 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02002845 /* parse keys */
2846 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
2847 LY_PATH_PRED_KEYS, &expr), cleanup);
2848
2849 /* compile them */
2850 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
2851 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002852 }
2853
2854 /* find first matching value */
2855 LY_LIST_FOR(first, node) {
2856 if (node->schema != schema) {
2857 continue;
2858 }
2859
Michal Vasko00cbf532020-06-15 13:58:47 +02002860 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002861 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02002862 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002863 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002864 rc = lyd_find_sibling_val(lyd_node_children(node, 0), predicates[u].key, NULL, 0, (struct lyd_node **)&term);
Michal Vaskoe444f752020-02-10 12:20:06 +01002865 if (rc == LY_ENOTFOUND) {
2866 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002867 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002868 }
2869 LY_CHECK_GOTO(rc, cleanup);
2870
2871 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02002872 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002873 break;
2874 }
2875 }
2876
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002877 if (u < LY_ARRAY_COUNT(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002878 /* not a match */
2879 continue;
2880 }
Michal Vasko90932a92020-02-12 14:33:03 +01002881 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002882 term = (struct lyd_node_term *)node;
2883
2884 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002885 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002886 /* not a match */
2887 continue;
2888 }
2889 }
2890
2891 /* all criteria passed */
2892 break;
2893 }
2894
2895 if (!node) {
2896 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002897 if (match) {
2898 *match = NULL;
2899 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002900 goto cleanup;
2901 }
2902
2903 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002904 if (match) {
2905 *match = (struct lyd_node *)node;
2906 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002907 rc = LY_SUCCESS;
2908
2909cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02002910 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
2911 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01002912 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002913 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002914 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002915 return rc;
2916}
2917
2918API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002919lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2920 const char *key_or_value, size_t val_len, struct lyd_node **match)
2921{
2922 const struct lysc_node *schema;
2923
2924 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2925
2926 if (!first) {
2927 /* no data */
2928 *match = NULL;
2929 return LY_ENOTFOUND;
2930 }
2931
2932 /* find schema */
2933 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2934 if (!schema) {
2935 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2936 return LY_EINVAL;
2937 }
2938
2939 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2940}
2941
2942API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002943lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2944{
2945 struct lyd_node **match_p;
2946 struct lyd_node_inner *parent;
2947
Michal Vaskof03ed032020-03-04 13:31:44 +01002948 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002949
Michal Vasko62ed12d2020-05-21 10:08:25 +02002950 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2951 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002952 if (match) {
2953 *match = NULL;
2954 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002955 return LY_ENOTFOUND;
2956 }
2957
2958 /* find first sibling */
2959 if (siblings->parent) {
2960 siblings = siblings->parent->child;
2961 } else {
2962 while (siblings->prev->next) {
2963 siblings = siblings->prev;
2964 }
2965 }
2966
2967 parent = (struct lyd_node_inner *)siblings->parent;
2968 if (parent && parent->children_ht) {
2969 assert(target->hash);
2970
2971 /* find by hash */
2972 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002973 /* check even value when needed */
2974 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(target, *match_p, 0)) {
2975 siblings = *match_p;
2976 } else {
2977 siblings = NULL;
2978 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002979 } else {
2980 /* not found */
2981 siblings = NULL;
2982 }
2983 } else {
2984 /* no children hash table */
2985 for (; siblings; siblings = siblings->next) {
2986 if (!lyd_compare(siblings, target, 0)) {
2987 break;
2988 }
2989 }
2990 }
2991
2992 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002993 if (match) {
2994 *match = NULL;
2995 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002996 return LY_ENOTFOUND;
2997 }
2998
Michal Vasko9b368d32020-02-14 13:53:31 +01002999 if (match) {
3000 *match = (struct lyd_node *)siblings;
3001 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003002 return LY_SUCCESS;
3003}
3004
3005API LY_ERR
3006lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3007{
3008 struct lyd_node_inner *parent;
3009 struct lyd_node *match;
3010 struct lyd_node **match_p;
3011 struct ly_set *ret;
3012
3013 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3014
Michal Vasko62ed12d2020-05-21 10:08:25 +02003015 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3016 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003017 return LY_ENOTFOUND;
3018 }
3019
3020 ret = ly_set_new();
3021 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3022
3023 /* find first sibling */
3024 if (siblings->parent) {
3025 siblings = siblings->parent->child;
3026 } else {
3027 while (siblings->prev->next) {
3028 siblings = siblings->prev;
3029 }
3030 }
3031
3032 parent = (struct lyd_node_inner *)siblings->parent;
3033 if (parent && parent->children_ht) {
3034 assert(target->hash);
3035
3036 /* find by hash */
3037 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003038 /* check even value when needed */
3039 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(target, *match_p, 0)) {
3040 match = *match_p;
3041 } else {
3042 match = NULL;
3043 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003044 } else {
3045 /* not found */
3046 match = NULL;
3047 }
3048 while (match) {
3049 /* add all found nodes into the return set */
3050 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3051 goto error;
3052 }
3053
3054 /* find next instance */
3055 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3056 match = NULL;
3057 } else {
Michal Vaskoda859032020-07-14 12:20:14 +02003058 /* check even value when needed */
3059 if (!(match->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(match, *match_p, 0)) {
3060 match = *match_p;
3061 } else {
3062 match = NULL;
3063 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003064 }
3065 }
3066 } else {
3067 /* no children hash table */
3068 for (; siblings; siblings = siblings->next) {
3069 if (!lyd_compare(siblings, target, 0)) {
3070 /* a match */
3071 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3072 goto error;
3073 }
3074 }
3075 }
3076 }
3077
3078 if (!ret->count) {
3079 ly_set_free(ret, NULL);
3080 return LY_ENOTFOUND;
3081 }
3082
3083 *set = ret;
3084 return LY_SUCCESS;
3085
3086error:
3087 ly_set_free(ret, NULL);
3088 return LY_EMEM;
3089}
3090
Michal Vasko90932a92020-02-12 14:33:03 +01003091static int
3092lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3093{
3094 struct lysc_node *val1;
3095 struct lyd_node *val2;
3096
3097 val1 = *((struct lysc_node **)val1_p);
3098 val2 = *((struct lyd_node **)val2_p);
3099
Michal Vasko90932a92020-02-12 14:33:03 +01003100 if (val1 == val2->schema) {
3101 /* schema match is enough */
3102 return 1;
3103 } else {
3104 return 0;
3105 }
3106}
3107
3108static LY_ERR
3109lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3110{
3111 struct lyd_node **match_p;
3112 struct lyd_node_inner *parent;
3113 uint32_t hash;
3114 values_equal_cb ht_cb;
3115
Michal Vaskob104f112020-07-17 09:54:54 +02003116 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003117
3118 parent = (struct lyd_node_inner *)siblings->parent;
3119 if (parent && parent->children_ht) {
3120 /* calculate our hash */
3121 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3122 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3123 hash = dict_hash_multi(hash, NULL, 0);
3124
3125 /* use special hash table function */
3126 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3127
3128 /* find by hash */
3129 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3130 siblings = *match_p;
3131 } else {
3132 /* not found */
3133 siblings = NULL;
3134 }
3135
3136 /* set the original hash table compare function back */
3137 lyht_set_cb(parent->children_ht, ht_cb);
3138 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003139 /* find first sibling */
3140 if (siblings->parent) {
3141 siblings = siblings->parent->child;
3142 } else {
3143 while (siblings->prev->next) {
3144 siblings = siblings->prev;
3145 }
3146 }
3147
3148 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003149 for (; siblings; siblings = siblings->next) {
3150 if (siblings->schema == schema) {
3151 /* schema match is enough */
3152 break;
3153 }
3154 }
3155 }
3156
3157 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003158 if (match) {
3159 *match = NULL;
3160 }
Michal Vasko90932a92020-02-12 14:33:03 +01003161 return LY_ENOTFOUND;
3162 }
3163
Michal Vasko9b368d32020-02-14 13:53:31 +01003164 if (match) {
3165 *match = (struct lyd_node *)siblings;
3166 }
Michal Vasko90932a92020-02-12 14:33:03 +01003167 return LY_SUCCESS;
3168}
3169
Michal Vaskoe444f752020-02-10 12:20:06 +01003170API LY_ERR
3171lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3172 size_t val_len, struct lyd_node **match)
3173{
3174 LY_ERR rc;
3175 struct lyd_node *target = NULL;
3176
3177 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003178 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003179 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3180 return LY_EINVAL;
Michal Vaskoe444f752020-02-10 12:20:06 +01003181 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3182 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3183 lys_nodetype2str(schema->nodetype), __func__);
3184 return LY_EINVAL;
3185 }
3186
Michal Vasko62ed12d2020-05-21 10:08:25 +02003187 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3188 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003189 if (match) {
3190 *match = NULL;
3191 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003192 return LY_ENOTFOUND;
3193 }
3194
Michal Vaskof03ed032020-03-04 13:31:44 +01003195 if (key_or_value && !val_len) {
3196 val_len = strlen(key_or_value);
3197 }
3198
Michal Vaskob104f112020-07-17 09:54:54 +02003199 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3200 /* create a data node and find the instance */
3201 if (schema->nodetype == LYS_LEAFLIST) {
3202 /* target used attributes: schema, hash, value */
3203 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
3204 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3205 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003206 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003207 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003208 }
3209
3210 /* find it */
3211 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003212 } else {
3213 /* find the first schema node instance */
3214 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003215 }
3216
Michal Vaskoe444f752020-02-10 12:20:06 +01003217 lyd_free_tree(target);
3218 return rc;
3219}
Michal Vaskoccc02342020-05-21 10:09:21 +02003220
3221API LY_ERR
3222lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3223{
3224 LY_ERR ret = LY_SUCCESS;
3225 struct lyxp_set xp_set;
3226 struct lyxp_expr *exp;
3227 uint32_t i;
3228
3229 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3230
3231 memset(&xp_set, 0, sizeof xp_set);
3232
3233 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003234 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003235 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3236
3237 /* evaluate expression */
3238 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3239 LY_CHECK_GOTO(ret, cleanup);
3240
3241 /* allocate return set */
3242 *set = ly_set_new();
3243 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3244
3245 /* transform into ly_set */
3246 if (xp_set.type == LYXP_SET_NODE_SET) {
3247 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3248 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3249 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3250 (*set)->size = xp_set.used;
3251
3252 for (i = 0; i < xp_set.used; ++i) {
3253 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3254 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3255 }
3256 }
3257 }
3258
3259cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003260 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003261 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3262 return ret;
3263}