blob: 1b5949385df0eb4739bcb5599318718701d587c8 [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 "context.h"
33#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020034#include "diff.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"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020049#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "xml.h"
51#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020052
Michal Vaskob104f112020-07-17 09:54:54 +020053static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020054 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020055
Radek Krejci084289f2019-07-09 17:35:30 +020056LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +020057lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, ly_bool *dynamic, ly_bool second,
Radek Krejci1deb5be2020-08-26 16:43:36 +020058 uint32_t value_hint, LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020059{
Michal Vasko90932a92020-02-12 14:33:03 +010060 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020061 struct ly_err_item *err = NULL;
62 struct ly_ctx *ctx;
63 struct lysc_type *type;
Radek Krejci1deb5be2020-08-26 16:43:36 +020064 uint32_t options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010065 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020066 assert(node);
67
68 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020069
Michal Vasko22df3f02020-08-24 13:29:22 +020070 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020071 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +020072 tree ? (void *)node : (void *)node->schema, tree, &node->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010073 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020074 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020075 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020076 if (!node->parent && lysc_data_parent(node->schema)) {
77 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
78 } else {
79 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
80 }
Radek Krejci73dead22019-07-11 16:46:16 +020081 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020082 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010084 } else if (dynamic) {
85 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020086 }
87
88error:
89 return ret;
90}
91
Michal Vasko00cbf532020-06-15 13:58:47 +020092/* 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 +020093LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +020094lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci0f969882020-08-21 16:56:47 +020095 LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko90932a92020-02-12 14:33:03 +010096{
97 LY_ERR ret = LY_SUCCESS;
98 struct ly_err_item *err = NULL;
99 struct ly_ctx *ctx;
100 struct lysc_type *type;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200101 uint32_t options = LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100102
103 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
104
105 ctx = schema->module->ctx;
106 type = ((struct lysc_node_leaf *)schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200107 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, (void *)schema, NULL,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200108 val, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100109 if (ret == LY_EINCOMPLETE) {
110 /* this is fine, we do not need it resolved */
111 ret = LY_SUCCESS;
112 } else if (ret && err) {
113 ly_err_print(err);
114 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
115 ly_err_free(err);
116 }
117 if (!ret && dynamic) {
118 *dynamic = 0;
119 }
120
121 return ret;
122}
123
Radek Krejci38d85362019-09-05 16:26:38 +0200124LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200125lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, ly_bool *dynamic,
126 ly_bool second, uint32_t value_hint, LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_snode,
Radek Krejci0f969882020-08-21 16:56:47 +0200127 const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200128{
Michal Vasko90932a92020-02-12 14:33:03 +0100129 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200130 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 struct lyext_metadata *ant;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132 uint32_t options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100133 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200134
Michal Vasko9f96a052020-03-10 09:41:45 +0100135 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100136
Michal Vasko9f96a052020-03-10 09:41:45 +0100137 ant = meta->annotation->data;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200138 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200139 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100140 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200141 if (err) {
142 ly_err_print(err);
143 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
144 ly_err_free(err);
145 }
146 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100147 } else if (dynamic) {
148 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200149 }
150
151error:
152 return ret;
153}
154
Michal Vaskof937cfe2020-08-03 16:07:12 +0200155LY_ERR
156_lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200157 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200158{
159 LY_ERR rc = LY_SUCCESS;
160 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200161 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200162 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200163
164 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
165
166 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
167 LOGARG(ctx, node);
168 return LY_EINVAL;
169 }
170
Michal Vasko22df3f02020-08-24 13:29:22 +0200171 type = ((struct lysc_node_leaf *)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200172 /* just validate, no storing of enything */
173 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200174 format, prefix_data, node, NULL, &storage, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200175 if (rc == LY_EINCOMPLETE) {
176 /* actually success since we do not provide the context tree and call validation with
177 * LY_TYPE_OPTS_INCOMPLETE_DATA */
178 rc = LY_SUCCESS;
179 } else if (rc && err) {
180 if (ctx) {
181 /* log only in case the ctx was provided as input parameter */
182 ly_err_print(err);
183 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200184 }
Radek Krejci73dead22019-07-11 16:46:16 +0200185 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200186 }
187
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200188 if (!rc) {
189 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
190 }
Radek Krejci084289f2019-07-09 17:35:30 +0200191 return rc;
192}
193
194API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200195lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
196{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200197 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200198}
199
200API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100201lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200202 const struct lyd_node *tree, struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200203{
204 LY_ERR rc;
205 struct ly_err_item *err = NULL;
206 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200207 struct lyd_value val = {0};
Radek Krejci1deb5be2020-08-26 16:43:36 +0200208 uint32_t options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200209
210 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
211
Michal Vasko22df3f02020-08-24 13:29:22 +0200212 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200213 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, LY_PREF_JSON, NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +0200214 tree ? (void *)node : (void *)node->schema, tree, &val, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200215 if (rc == LY_EINCOMPLETE) {
216 return rc;
217 } else if (rc) {
218 if (err) {
219 if (ctx) {
220 ly_err_print(err);
221 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200222 }
Radek Krejci73dead22019-07-11 16:46:16 +0200223 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200224 }
Radek Krejci73dead22019-07-11 16:46:16 +0200225 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200226 }
227
Michal Vasko3701af52020-08-03 14:29:38 +0200228 if (realtype) {
229 *realtype = val.realtype;
230 }
231
232 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200233 return LY_SUCCESS;
234}
235
236API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200237lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200238{
239 LY_ERR ret = LY_SUCCESS, rc;
240 struct ly_err_item *err = NULL;
241 struct ly_ctx *ctx;
242 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200243 struct lyd_value data = {0};
Radek Krejci1deb5be2020-08-26 16:43:36 +0200244 uint32_t options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200245
246 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
247
248 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200249 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200250 rc = type->plugin->store(ctx, type, value, value_len, options, LY_PREF_JSON, NULL, (struct lyd_node *)node, tree, &data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200251 &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200252 if (rc == LY_EINCOMPLETE) {
253 ret = rc;
254 /* continue with comparing, just remember what to return if storing is ok */
255 } else if (rc) {
256 /* value to compare is invalid */
257 ret = LY_EINVAL;
258 if (err) {
259 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200260 }
Radek Krejci73dead22019-07-11 16:46:16 +0200261 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200262 }
263
264 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200265 if (type->plugin->compare(&node->value, &data)) {
266 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200267 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200268 }
Radek Krejci084289f2019-07-09 17:35:30 +0200269
270cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200271 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200272
273 return ret;
274}
275
Radek Krejci7931b192020-06-25 17:05:03 +0200276static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200277lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200278{
Radek Krejcie7b95092019-05-15 11:03:07 +0200279
Radek Krejci7931b192020-06-25 17:05:03 +0200280 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200282 const char *path = in->method.fpath.filepath;
283 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200284
285 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200286 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200287
288 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
289 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
291 format = LYD_JSON;
292 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
293 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200294 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200295 }
296
Radek Krejci7931b192020-06-25 17:05:03 +0200297 return format;
298}
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejci7931b192020-06-25 17:05:03 +0200300API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200301lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200302 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200303{
Radek Krejci1798aae2020-07-14 13:26:06 +0200304 LY_ERR ret = LY_SUCCESS;
305 struct lyd_ctx *lydctx = NULL;
306
Radek Krejci7931b192020-06-25 17:05:03 +0200307 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
308 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
309 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
310
311 format = lyd_parse_get_format(in, format);
312 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
313
Radek Krejci1798aae2020-07-14 13:26:06 +0200314 /* init */
315 *tree = NULL;
316
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 /* remember input position */
318 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200319
320 switch (format) {
321 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
323 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200324 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200325 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
326 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200327 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200328 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
329 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200330 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200331 LOGINT_RET(ctx);
332 }
333
Radek Krejci1798aae2020-07-14 13:26:06 +0200334 if (!(parse_options & LYD_PARSE_ONLY)) {
335 uint32_t i = 0;
336 const struct lys_module *mod;
337 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200338
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 next = *tree;
340 while (1) {
341 if (validate_options & LYD_VALIDATE_PRESENT) {
342 mod = lyd_data_next_module(&next, &first);
343 } else {
344 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
345 }
346 if (!mod) {
347 break;
348 }
349 if (first == *tree) {
350 /* make sure first2 changes are carried to tree */
351 first2 = tree;
352 } else {
353 first2 = &first;
354 }
355
356 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
357 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
358
359 /* add all top-level defaults for this module */
360 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
Radek Krejci0f969882020-08-21 16:56:47 +0200361 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 LY_CHECK_GOTO(ret, cleanup);
363
364 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200365 switch (format) {
366 case LYD_XML:
367 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
368 LY_PREF_XML, &((struct lyxml_ctx *)lydctx->data_ctx)->ns, NULL);
369 break;
370 case LYD_JSON:
371 case LYD_LYB:
372 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
373 LY_PREF_JSON, NULL, NULL);
374 break;
375 case LYD_UNKNOWN:
376 LOGINT_RET(ctx);
377 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200378 LY_CHECK_GOTO(ret, cleanup);
379
380 /* perform final validation that assumes the data tree is final */
381 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
382 }
383 }
384
385cleanup:
386 lydctx->free((struct lyd_ctx *)lydctx);
387 if (ret) {
388 lyd_free_all(*tree);
389 *tree = NULL;
390 }
391 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200392}
393
394API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200395lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200396 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200397{
398 LY_ERR ret;
399 struct ly_in *in;
400
401 LY_CHECK_RET(ly_in_new_memory(data, &in));
402 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
403
404 ly_in_free(in, 0);
405 return ret;
406}
407
408API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200409lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200410 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200411{
412 LY_ERR ret;
413 struct ly_in *in;
414
415 LY_CHECK_RET(ly_in_new_fd(fd, &in));
416 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
417
418 ly_in_free(in, 0);
419 return ret;
420}
421
422API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200423lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
424 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200425{
426 LY_ERR ret;
427 struct ly_in *in;
428
429 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
430 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
431
432 ly_in_free(in, 0);
433 return ret;
434}
435
Radek Krejci7931b192020-06-25 17:05:03 +0200436API LY_ERR
437lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
438{
439 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
440
441 format = lyd_parse_get_format(in, format);
442 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
443
Radek Krejci1798aae2020-07-14 13:26:06 +0200444 /* init */
445 *tree = NULL;
446 if (op) {
447 *op = NULL;
448 }
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_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200456 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200458 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200460 case LYD_UNKNOWN:
461 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200462 }
463
464 LOGINT_RET(ctx);
465}
466
467API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200468lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
Radek Krejci0f969882020-08-21 16:56:47 +0200469 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200470{
471 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200472 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200473
474 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200476
Radek Krejci1798aae2020-07-14 13:26:06 +0200477 /* init */
478 if (tree) {
479 *tree = NULL;
480 }
481 if (op) {
482 *op = NULL;
483 }
484
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 /* remember input position */
486 in->func_start = in->current;
487
Radek Krejci7931b192020-06-25 17:05:03 +0200488 switch (format) {
489 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200491 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200493 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200495 case LYD_UNKNOWN:
496 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200497 }
498
Michal Vaskob7be7a82020-08-20 09:09:04 +0200499 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200500}
501
502API LY_ERR
503lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
504{
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200506
507 format = lyd_parse_get_format(in, format);
508 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
509
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 /* init */
511 if (tree) {
512 *tree = NULL;
513 }
514 if (ntf) {
515 *ntf = NULL;
516 }
517
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 /* remember input position */
519 in->func_start = in->current;
520
Radek Krejci7931b192020-06-25 17:05:03 +0200521 switch (format) {
522 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200524 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200525 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200526 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200528 case LYD_UNKNOWN:
529 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200530 }
531
532 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200533}
Radek Krejci084289f2019-07-09 17:35:30 +0200534
Michal Vasko90932a92020-02-12 14:33:03 +0100535LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200536lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, uint32_t value_hint,
Radek Krejci0f969882020-08-21 16:56:47 +0200537 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100538{
539 LY_ERR ret;
540 struct lyd_node_term *term;
541
Michal Vasko9b368d32020-02-14 13:53:31 +0100542 assert(schema->nodetype & LYD_NODE_TERM);
543
Michal Vasko90932a92020-02-12 14:33:03 +0100544 term = calloc(1, sizeof *term);
545 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
546
547 term->schema = schema;
548 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100549 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100550
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200551 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, format, prefix_data, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100552 if (ret && (ret != LY_EINCOMPLETE)) {
553 free(term);
554 return ret;
555 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100556 lyd_hash((struct lyd_node *)term);
557
558 *node = (struct lyd_node *)term;
559 return ret;
560}
561
562LY_ERR
563lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
564{
565 LY_ERR ret;
566 struct lyd_node_term *term;
567 struct lysc_type *type;
568
569 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200570 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100571
572 term = calloc(1, sizeof *term);
573 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
574
575 term->schema = schema;
576 term->prev = (struct lyd_node *)term;
577 term->flags = LYD_NEW;
578
579 type = ((struct lysc_node_leaf *)schema)->type;
580 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
581 if (ret) {
582 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
583 free(term);
584 return ret;
585 }
586 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100587
588 *node = (struct lyd_node *)term;
589 return ret;
590}
591
592LY_ERR
593lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
594{
595 struct lyd_node_inner *in;
596
Michal Vasko9b368d32020-02-14 13:53:31 +0100597 assert(schema->nodetype & LYD_NODE_INNER);
598
Michal Vasko90932a92020-02-12 14:33:03 +0100599 in = calloc(1, sizeof *in);
600 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
601
602 in->schema = schema;
603 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100604 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100605
Michal Vasko9b368d32020-02-14 13:53:31 +0100606 /* do not hash list with keys, we need them for the hash */
607 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
608 lyd_hash((struct lyd_node *)in);
609 }
Michal Vasko90932a92020-02-12 14:33:03 +0100610
611 *node = (struct lyd_node *)in;
612 return LY_SUCCESS;
613}
614
Michal Vasko90932a92020-02-12 14:33:03 +0100615LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200616lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100617{
618 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100619 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200620 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100621
Michal Vasko004d3152020-06-11 19:59:22 +0200622 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100623
624 /* create list */
625 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
626
Michal Vasko90932a92020-02-12 14:33:03 +0100627 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200628 LY_ARRAY_FOR(predicates, u) {
629 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100630 lyd_insert_node(list, NULL, key);
631 }
632
Michal Vasko9b368d32020-02-14 13:53:31 +0100633 /* hash having all the keys */
634 lyd_hash(list);
635
Michal Vasko90932a92020-02-12 14:33:03 +0100636 /* success */
637 *node = list;
638 list = NULL;
639
640cleanup:
641 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200642 return ret;
643}
644
645static LY_ERR
646lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
647{
648 LY_ERR ret = LY_SUCCESS;
649 struct lyxp_expr *expr = NULL;
650 uint16_t exp_idx = 0;
651 enum ly_path_pred_type pred_type = 0;
652 struct ly_path_predicate *predicates = NULL;
653
654 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200655 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko004d3152020-06-11 19:59:22 +0200656 LY_PATH_PRED_KEYS, &expr), cleanup);
657
658 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200659 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200660 LY_PREF_JSON, NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200661
662 /* create the list node */
663 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
664
665cleanup:
666 lyxp_expr_free(schema->module->ctx, expr);
667 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100668 return ret;
669}
670
671LY_ERR
672lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
673{
674 struct lyd_node_any *any;
675
Michal Vasko9b368d32020-02-14 13:53:31 +0100676 assert(schema->nodetype & LYD_NODE_ANY);
677
Michal Vasko90932a92020-02-12 14:33:03 +0100678 any = calloc(1, sizeof *any);
679 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
680
681 any->schema = schema;
682 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100683 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100684
Radek Krejci1798aae2020-07-14 13:26:06 +0200685 /* TODO: convert XML/JSON strings into a opaq data tree */
686 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100687 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100688 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100689
690 *node = (struct lyd_node *)any;
691 return LY_SUCCESS;
692}
693
Michal Vasko52927e22020-03-16 17:26:14 +0100694LY_ERR
695lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200696 ly_bool *dynamic, uint32_t value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200697 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100698{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200699 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100700 struct lyd_node_opaq *opaq;
701
Radek Krejci1798aae2020-07-14 13:26:06 +0200702 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100703
704 if (!value_len) {
705 value = "";
706 }
707
708 opaq = calloc(1, sizeof *opaq);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200709 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vasko52927e22020-03-16 17:26:14 +0100710
711 opaq->prev = (struct lyd_node *)opaq;
Radek Krejcid46e46a2020-09-15 14:22:42 +0200712 opaq->val_prefs = val_prefs;
713 opaq->format = format;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200714 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
715
Michal Vasko52927e22020-03-16 17:26:14 +0100716 if (pref_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200717 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100718 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200719 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200720 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200721 }
722
Michal Vasko52927e22020-03-16 17:26:14 +0100723 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200724 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100725 *dynamic = 0;
726 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200727 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100728 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100730 opaq->ctx = ctx;
731
Radek Krejci011e4aa2020-09-04 15:22:31 +0200732finish:
733 if (ret) {
734 lyd_free_tree((struct lyd_node *)opaq);
735 } else {
736 *node = (struct lyd_node *)opaq;
737 }
738 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100739}
740
Michal Vasko3a41dff2020-07-15 14:30:28 +0200741API LY_ERR
742lyd_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 +0100743{
744 struct lyd_node *ret = NULL;
745 const struct lysc_node *schema;
746 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
747
Michal Vasko6027eb92020-07-15 16:37:30 +0200748 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100749
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 if (!module) {
751 module = parent->schema->module;
752 }
753
Michal Vasko3a41dff2020-07-15 14:30:28 +0200754 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
755 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
756 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 +0100757
Michal Vasko3a41dff2020-07-15 14:30:28 +0200758 LY_CHECK_RET(lyd_create_inner(schema, &ret));
759 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100760 lyd_insert_node(parent, NULL, ret);
761 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200762
763 if (node) {
764 *node = ret;
765 }
766 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100767}
768
Michal Vasko3a41dff2020-07-15 14:30:28 +0200769API LY_ERR
770lyd_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 +0100771{
772 struct lyd_node *ret = NULL, *key;
773 const struct lysc_node *schema, *key_s;
774 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
775 va_list ap;
776 const char *key_val;
777 LY_ERR rc = LY_SUCCESS;
778
Michal Vasko6027eb92020-07-15 16:37:30 +0200779 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100780
Michal Vaskof03ed032020-03-04 13:31:44 +0100781 if (!module) {
782 module = parent->schema->module;
783 }
784
Michal Vasko013a8182020-03-03 10:46:53 +0100785 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100787
788 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100790
Michal Vasko3a41dff2020-07-15 14:30:28 +0200791 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100792
793 /* create and insert all the keys */
794 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
795 key_val = va_arg(ap, const char *);
796
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200797 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, 0, LY_PREF_JSON, NULL, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200798 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
799 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100800 lyd_insert_node(ret, NULL, key);
801 }
802
Michal Vasko013a8182020-03-03 10:46:53 +0100803 if (parent) {
804 lyd_insert_node(parent, NULL, ret);
805 }
806
807cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200808 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100809 if (rc) {
810 lyd_free_tree(ret);
811 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812 } else if (node) {
813 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100814 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200815 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100816}
817
Michal Vasko3a41dff2020-07-15 14:30:28 +0200818API LY_ERR
819lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200820 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100821{
822 struct lyd_node *ret = NULL;
823 const struct lysc_node *schema;
824 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
825
Michal Vasko6027eb92020-07-15 16:37:30 +0200826 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100827
Michal Vaskof03ed032020-03-04 13:31:44 +0100828 if (!module) {
829 module = parent->schema->module;
830 }
Michal Vasko004d3152020-06-11 19:59:22 +0200831 if (!keys) {
832 keys = "";
833 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100834
Michal Vasko004d3152020-06-11 19:59:22 +0200835 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100836 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200837 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100838
Michal Vasko004d3152020-06-11 19:59:22 +0200839 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
840 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200841 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200842 } else {
843 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200844 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200845 }
Michal Vasko004d3152020-06-11 19:59:22 +0200846 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100847 lyd_insert_node(parent, NULL, ret);
848 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200849
850 if (node) {
851 *node = ret;
852 }
853 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100854}
855
Michal Vasko3a41dff2020-07-15 14:30:28 +0200856API LY_ERR
857lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200858 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100859{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200860 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100861 struct lyd_node *ret = NULL;
862 const struct lysc_node *schema;
863 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
864
Michal Vasko6027eb92020-07-15 16:37:30 +0200865 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100866
Michal Vaskof03ed032020-03-04 13:31:44 +0100867 if (!module) {
868 module = parent->schema->module;
869 }
870
Michal Vasko013a8182020-03-03 10:46:53 +0100871 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200872 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100873
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200874 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, 0, LY_PREF_JSON, NULL, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200875 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200876 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100877 lyd_insert_node(parent, NULL, ret);
878 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200879
880 if (node) {
881 *node = ret;
882 }
883 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100884}
885
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100887lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200888 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100889{
890 struct lyd_node *ret = NULL;
891 const struct lysc_node *schema;
892 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
893
Michal Vasko6027eb92020-07-15 16:37:30 +0200894 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100895
Michal Vaskof03ed032020-03-04 13:31:44 +0100896 if (!module) {
897 module = parent->schema->module;
898 }
899
Michal Vasko013a8182020-03-03 10:46:53 +0100900 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200901 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100902
Michal Vasko3a41dff2020-07-15 14:30:28 +0200903 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
904 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100905 lyd_insert_node(parent, NULL, ret);
906 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200907
908 if (node) {
909 *node = ret;
910 }
911 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100912}
913
Michal Vasko4490d312020-06-16 13:08:55 +0200914/**
915 * @brief Update node value.
916 *
917 * @param[in] node Node to update.
918 * @param[in] value New value to set.
919 * @param[in] value_type Type of @p value for any node.
920 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
921 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
922 * @return LY_ERR value.
923 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200924static LY_ERR
925lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200926 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200927{
928 LY_ERR ret = LY_SUCCESS;
929 struct lyd_node *new_any;
930
931 switch (node->schema->nodetype) {
932 case LYS_CONTAINER:
933 case LYS_NOTIF:
934 case LYS_RPC:
935 case LYS_ACTION:
936 case LYS_LIST:
937 case LYS_LEAFLIST:
938 /* if it exists, there is nothing to update */
939 *new_parent = NULL;
940 *new_node = NULL;
941 break;
942 case LYS_LEAF:
943 ret = lyd_change_term(node, value);
944 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
945 /* there was an actual change (at least of the default flag) */
946 *new_parent = node;
947 *new_node = node;
948 ret = LY_SUCCESS;
949 } else if (ret == LY_ENOT) {
950 /* no change */
951 *new_parent = NULL;
952 *new_node = NULL;
953 ret = LY_SUCCESS;
954 } /* else error */
955 break;
956 case LYS_ANYDATA:
957 case LYS_ANYXML:
958 /* create a new any node */
959 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
960
961 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200962 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200963 /* not equal, switch values (so that we can use generic node free) */
964 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
965 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
966 ((struct lyd_node_any *)node)->value.str = value;
967 ((struct lyd_node_any *)node)->value_type = value_type;
968
969 *new_parent = node;
970 *new_node = node;
971 } else {
972 /* they are equal */
973 *new_parent = NULL;
974 *new_node = NULL;
975 }
976 lyd_free_tree(new_any);
977 break;
978 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200979 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200980 ret = LY_EINT;
981 break;
982 }
983
984 return ret;
985}
986
Michal Vasko3a41dff2020-07-15 14:30:28 +0200987API LY_ERR
988lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200989 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200990{
991 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200992 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200993 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200994 size_t pref_len, name_len;
995
Michal Vasko3a41dff2020-07-15 14:30:28 +0200996 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200997
Michal Vaskob7be7a82020-08-20 09:09:04 +0200998 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200999
1000 /* parse the name */
1001 tmp = name;
1002 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1003 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001005 }
1006
1007 /* find the module */
1008 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001009 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001010 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 +02001011 }
1012
1013 /* set value if none */
1014 if (!val_str) {
1015 val_str = "";
1016 }
1017
Radek Krejci1798aae2020-07-14 13:26:06 +02001018 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001019 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001020
1021 if (meta) {
1022 *meta = ret;
1023 }
1024 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001025}
1026
Michal Vasko3a41dff2020-07-15 14:30:28 +02001027API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001028lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001029 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001030{
1031 struct lyd_node *ret = NULL;
1032
Michal Vasko6027eb92020-07-15 16:37:30 +02001033 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001034
1035 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001036 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001037 }
1038 if (!value) {
1039 value = "";
1040 }
1041
Radek Krejci1798aae2020-07-14 13:26:06 +02001042 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1043 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001044 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001045 lyd_insert_node(parent, NULL, ret);
1046 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001047
1048 if (node) {
1049 *node = ret;
1050 }
1051 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001052}
1053
Michal Vasko3a41dff2020-07-15 14:30:28 +02001054API LY_ERR
1055lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001056 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001057{
Radek Krejci1798aae2020-07-14 13:26:06 +02001058 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001059 const struct ly_ctx *ctx;
1060 const char *prefix, *tmp;
1061 size_t pref_len, name_len;
1062
Michal Vasko3a41dff2020-07-15 14:30:28 +02001063 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001064
Michal Vaskob7be7a82020-08-20 09:09:04 +02001065 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001066
1067 /* parse the name */
1068 tmp = name;
1069 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1070 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001071 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001072 }
1073
1074 /* set value if none */
1075 if (!val_str) {
1076 val_str = "";
1077 }
1078
Radek Krejci1798aae2020-07-14 13:26:06 +02001079 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1080 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001081
1082 if (attr) {
1083 *attr = ret;
1084 }
1085 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001086}
1087
1088API LY_ERR
1089lyd_change_term(struct lyd_node *term, const char *val_str)
1090{
1091 LY_ERR ret = LY_SUCCESS;
1092 struct lysc_type *type;
1093 struct lyd_node_term *t;
1094 struct lyd_node *parent;
1095 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001096 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001097
1098 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1099
1100 if (!val_str) {
1101 val_str = "";
1102 }
1103 t = (struct lyd_node_term *)term;
1104 type = ((struct lysc_node_leaf *)term->schema)->type;
1105
1106 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001107 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001108
1109 /* compare original and new value */
1110 if (type->plugin->compare(&t->value, &val)) {
1111 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001112 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001113 t->value = val;
1114 memset(&val, 0, sizeof val);
1115 val_change = 1;
1116 } else {
1117 val_change = 0;
1118 }
1119
1120 /* always clear the default flag */
1121 if (term->flags & LYD_DEFAULT) {
1122 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1123 parent->flags &= ~LYD_DEFAULT;
1124 }
1125 dflt_change = 1;
1126 } else {
1127 dflt_change = 0;
1128 }
1129
1130 if (val_change || dflt_change) {
1131 /* make the node non-validated */
1132 term->flags &= LYD_NEW;
1133 }
1134
1135 if (val_change) {
1136 if (term->schema->nodetype == LYS_LEAFLIST) {
1137 /* leaf-list needs to be hashed again and re-inserted into parent */
1138 lyd_unlink_hash(term);
1139 lyd_hash(term);
1140 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1141 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1142 /* list needs to be updated if its key was changed */
1143 assert(term->parent->schema->nodetype == LYS_LIST);
1144 lyd_unlink_hash((struct lyd_node *)term->parent);
1145 lyd_hash((struct lyd_node *)term->parent);
1146 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1147 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1148 }
1149
1150 /* retrun value */
1151 if (!val_change) {
1152 if (dflt_change) {
1153 /* only default flag change */
1154 ret = LY_EEXIST;
1155 } else {
1156 /* no change */
1157 ret = LY_ENOT;
1158 }
1159 } /* else value changed, LY_SUCCESS */
1160
1161cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001162 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001163 return ret;
1164}
1165
Michal Vasko41586352020-07-13 13:54:25 +02001166API LY_ERR
1167lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1168{
1169 LY_ERR ret = LY_SUCCESS;
1170 struct lyd_meta *m2;
1171 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001172 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001173
1174 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1175
1176 if (!val_str) {
1177 val_str = "";
1178 }
1179
1180 /* parse the new value into a new meta structure */
1181 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001182 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001183
1184 /* compare original and new value */
1185 if (lyd_compare_meta(meta, m2)) {
1186 /* values differ, switch them */
1187 val = meta->value;
1188 meta->value = m2->value;
1189 m2->value = val;
1190 val_change = 1;
1191 } else {
1192 val_change = 0;
1193 }
1194
1195 /* retrun value */
1196 if (!val_change) {
1197 /* no change */
1198 ret = LY_ENOT;
1199 } /* else value changed, LY_SUCCESS */
1200
1201cleanup:
1202 return ret;
1203}
1204
Michal Vasko3a41dff2020-07-15 14:30:28 +02001205API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001206lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02001207 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001208{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001209 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001210}
1211
1212API LY_ERR
1213lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001214 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001215{
1216 LY_ERR ret = LY_SUCCESS, r;
1217 struct lyxp_expr *exp = NULL;
1218 struct ly_path *p = NULL;
1219 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1220 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001221 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001222 struct ly_path_predicate *pred;
1223
1224 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1225
1226 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001227 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001228 }
1229
1230 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001231 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko00cbf532020-06-15 13:58:47 +02001232 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1233
1234 /* compile path */
1235 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1236 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001237 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001238
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001239 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1240 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001241 && !(options & LYD_NEWOPT_OPAQ)) {
1242 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1243 lys_nodetype2str(schema->nodetype), schema->name);
1244 ret = LY_EINVAL;
1245 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 } 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 +02001247 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001248 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1249 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001250
1251 if (!value) {
1252 value = "";
1253 }
1254
1255 r = LY_SUCCESS;
1256 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001257 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001258 }
1259 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001260 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001261 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1262 }
1263
1264 /* try to find any existing nodes in the path */
1265 if (parent) {
1266 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1267 if (ret == LY_SUCCESS) {
1268 /* the node exists, are we supposed to update it or is it just a default? */
1269 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1270 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1271 ret = LY_EEXIST;
1272 goto cleanup;
1273 }
1274
1275 /* update the existing node */
1276 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1277 goto cleanup;
1278 } else if (ret == LY_EINCOMPLETE) {
1279 /* some nodes were found, adjust the iterator to the next segment */
1280 ++path_idx;
1281 } else if (ret == LY_ENOTFOUND) {
1282 /* 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 +02001283 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001284 node = parent;
1285 }
1286 } else {
1287 /* error */
1288 goto cleanup;
1289 }
1290 }
1291
1292 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001293 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001294 cur_parent = node;
1295 schema = p[path_idx].node;
1296
1297 switch (schema->nodetype) {
1298 case LYS_LIST:
1299 if (!(schema->flags & LYS_KEYLESS)) {
1300 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1301 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001302 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1303 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1304 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001305 } else {
1306 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1307 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1308 }
1309 break;
1310 }
Radek Krejci0f969882020-08-21 16:56:47 +02001311 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001312 case LYS_CONTAINER:
1313 case LYS_NOTIF:
1314 case LYS_RPC:
1315 case LYS_ACTION:
1316 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1317 break;
1318 case LYS_LEAFLIST:
1319 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1320 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001321 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1322 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1323 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001324 } else {
1325 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1326 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1327 }
1328 break;
1329 case LYS_LEAF:
1330 /* make there is some value */
1331 if (!value) {
1332 value = "";
1333 }
1334
1335 r = LY_SUCCESS;
1336 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001337 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001338 }
1339 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001340 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, 0, LY_PREF_JSON, NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001341 } else {
1342 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001343 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001344 LYD_JSON, NULL, NULL, 0, schema->module->name,
1345 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001346 }
1347 break;
1348 case LYS_ANYDATA:
1349 case LYS_ANYXML:
1350 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1351 break;
1352 default:
1353 LOGINT(ctx);
1354 ret = LY_EINT;
1355 goto cleanup;
1356 }
1357
1358 if (cur_parent) {
1359 /* connect to the parent */
1360 lyd_insert_node(cur_parent, NULL, node);
1361 } else if (parent) {
1362 /* connect to top-level siblings */
1363 lyd_insert_node(NULL, &parent, node);
1364 }
1365
1366 /* update remembered nodes */
1367 if (!nparent) {
1368 nparent = node;
1369 }
1370 nnode = node;
1371 }
1372
1373cleanup:
1374 lyxp_expr_free(ctx, exp);
1375 ly_path_free(ctx, p);
1376 if (!ret) {
1377 /* set out params only on success */
1378 if (new_parent) {
1379 *new_parent = nparent;
1380 }
1381 if (new_node) {
1382 *new_node = nnode;
1383 }
1384 }
1385 return ret;
1386}
1387
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001388LY_ERR
1389lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001390 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, uint32_t impl_opts,
Radek Krejci0f969882020-08-21 16:56:47 +02001391 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001392{
1393 LY_ERR ret;
1394 const struct lysc_node *iter = NULL;
1395 struct lyd_node *node;
1396 struct lyd_value **dflts;
1397 LY_ARRAY_COUNT_TYPE u;
1398
1399 assert(first && (parent || sparent || mod));
1400
1401 if (!sparent && parent) {
1402 sparent = parent->schema;
1403 }
1404
1405 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1406 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1407 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001408 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1409 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001410 }
1411
1412 switch (iter->nodetype) {
1413 case LYS_CHOICE:
1414 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1415 /* create default case data */
1416 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1417 NULL, node_types, node_when, impl_opts, diff));
1418 }
1419 break;
1420 case LYS_CONTAINER:
1421 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1422 /* create default NP container */
1423 LY_CHECK_RET(lyd_create_inner(iter, &node));
1424 node->flags = LYD_DEFAULT;
1425 lyd_insert_node(parent, first, node);
1426
1427 /* cannot be a NP container with when */
1428 assert(!iter->when);
1429
1430 /* create any default children */
1431 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1432 impl_opts, diff));
1433 }
1434 break;
1435 case LYS_LEAF:
1436 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1437 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1438 /* create default leaf */
1439 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1440 if (ret == LY_EINCOMPLETE) {
1441 if (node_types) {
1442 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001443 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001444 }
1445 } else if (ret) {
1446 return ret;
1447 }
1448 node->flags = LYD_DEFAULT;
1449 lyd_insert_node(parent, first, node);
1450
1451 if (iter->when && node_when) {
1452 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001453 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001454 }
1455 if (diff) {
1456 /* add into diff */
1457 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1458 }
1459 }
1460 break;
1461 case LYS_LEAFLIST:
1462 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1463 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1464 /* create all default leaf-lists */
1465 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1466 LY_ARRAY_FOR(dflts, u) {
1467 ret = lyd_create_term2(iter, dflts[u], &node);
1468 if (ret == LY_EINCOMPLETE) {
1469 if (node_types) {
1470 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001471 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001472 }
1473 } else if (ret) {
1474 return ret;
1475 }
1476 node->flags = LYD_DEFAULT;
1477 lyd_insert_node(parent, first, node);
1478
1479 if (iter->when && node_when) {
1480 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001481 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001482 }
1483 if (diff) {
1484 /* add into diff */
1485 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1486 }
1487 }
1488 }
1489 break;
1490 default:
1491 /* without defaults */
1492 break;
1493 }
1494 }
1495
1496 return LY_SUCCESS;
1497}
1498
1499API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001500lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001501{
Michal Vasko56daf732020-08-10 10:57:18 +02001502 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001503 LY_ERR ret = LY_SUCCESS;
1504
1505 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1506 if (diff) {
1507 *diff = NULL;
1508 }
1509
Michal Vasko56daf732020-08-10 10:57:18 +02001510 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001511 /* skip added default nodes */
1512 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1513 && (node->schema->nodetype & LYD_NODE_INNER)) {
1514 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1515 NULL, implicit_options, diff), cleanup);
1516 }
1517
Michal Vasko56daf732020-08-10 10:57:18 +02001518 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001519 }
1520
1521cleanup:
1522 if (ret && diff) {
1523 lyd_free_all(*diff);
1524 *diff = NULL;
1525 }
1526 return ret;
1527}
1528
1529API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001530lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001531{
1532 const struct lys_module *mod;
1533 struct lyd_node *d = NULL;
1534 uint32_t i = 0;
1535 LY_ERR ret = LY_SUCCESS;
1536
1537 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1538 if (diff) {
1539 *diff = NULL;
1540 }
1541 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001542 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001543 }
1544
1545 /* add nodes for each module one-by-one */
1546 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1547 if (!mod->implemented) {
1548 continue;
1549 }
1550
1551 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1552 if (d) {
1553 /* merge into one diff */
1554 lyd_insert_sibling(*diff, d, diff);
1555
1556 d = NULL;
1557 }
1558 }
1559
1560cleanup:
1561 if (ret && diff) {
1562 lyd_free_all(*diff);
1563 *diff = NULL;
1564 }
1565 return ret;
1566}
1567
1568API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001569lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001570{
1571 struct lyd_node *root, *d = NULL;
1572 LY_ERR ret = LY_SUCCESS;
1573
1574 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1575 if (diff) {
1576 *diff = NULL;
1577 }
1578
1579 /* add all top-level defaults for this module */
1580 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1581
1582 /* process nested nodes */
1583 LY_LIST_FOR(*tree, root) {
1584 /* skip added default nodes */
1585 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1586 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1587
1588 if (d) {
1589 /* merge into one diff */
1590 lyd_insert_sibling(*diff, d, diff);
1591
1592 d = NULL;
1593 }
1594 }
1595 }
1596
1597cleanup:
1598 if (ret && diff) {
1599 lyd_free_all(*diff);
1600 *diff = NULL;
1601 }
1602 return ret;
1603}
1604
Michal Vasko90932a92020-02-12 14:33:03 +01001605struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001606lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001607{
Michal Vaskob104f112020-07-17 09:54:54 +02001608 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001609 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001610 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001611
Michal Vaskob104f112020-07-17 09:54:54 +02001612 assert(new_node);
1613
1614 if (!first_sibling || !new_node->schema) {
1615 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001616 return NULL;
1617 }
1618
Michal Vaskob104f112020-07-17 09:54:54 +02001619 if (first_sibling->parent && first_sibling->parent->children_ht) {
1620 /* find the anchor using hashes */
1621 sparent = first_sibling->parent->schema;
1622 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1623 while (schema) {
1624 /* keep trying to find the first existing instance of the closest following schema sibling,
1625 * otherwise return NULL - inserting at the end */
1626 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1627 break;
1628 }
1629
1630 schema = lys_getnext(schema, sparent, NULL, 0);
1631 }
1632 } else {
1633 /* find the anchor without hashes */
1634 match = (struct lyd_node *)first_sibling;
1635 if (!lysc_data_parent(new_node->schema)) {
1636 /* we are in top-level, skip all the data from preceding modules */
1637 LY_LIST_FOR(match, match) {
1638 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1639 break;
1640 }
1641 }
1642 }
1643
1644 /* get the first schema sibling */
1645 sparent = lysc_data_parent(new_node->schema);
1646 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1647
1648 found = 0;
1649 LY_LIST_FOR(match, match) {
1650 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1651 /* we have found an opaque node, which must be at the end, so use it OR
1652 * modules do not match, so we must have traversed all the data from new_node module (if any),
1653 * we have found the first node of the next module, that is what we want */
1654 break;
1655 }
1656
1657 /* skip schema nodes until we find the instantiated one */
1658 while (!found) {
1659 if (new_node->schema == schema) {
1660 /* we have found the schema of the new node, continue search to find the first
1661 * data node with a different schema (after our schema) */
1662 found = 1;
1663 break;
1664 }
1665 if (match->schema == schema) {
1666 /* current node (match) is a data node still before the new node, continue search in data */
1667 break;
1668 }
1669 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1670 assert(schema);
1671 }
1672
1673 if (found && (match->schema != new_node->schema)) {
1674 /* find the next node after we have found our node schema data instance */
1675 break;
1676 }
1677 }
Michal Vasko90932a92020-02-12 14:33:03 +01001678 }
1679
1680 return match;
1681}
1682
1683/**
1684 * @brief Insert node after a sibling.
1685 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001686 * Handles inserting into NP containers and key-less lists.
1687 *
Michal Vasko90932a92020-02-12 14:33:03 +01001688 * @param[in] sibling Sibling to insert after.
1689 * @param[in] node Node to insert.
1690 */
1691static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001692lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001693{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001694 struct lyd_node_inner *par;
1695
Michal Vasko90932a92020-02-12 14:33:03 +01001696 assert(!node->next && (node->prev == node));
1697
1698 node->next = sibling->next;
1699 node->prev = sibling;
1700 sibling->next = node;
1701 if (node->next) {
1702 /* sibling had a succeeding node */
1703 node->next->prev = node;
1704 } else {
1705 /* sibling was last, find first sibling and change its prev */
1706 if (sibling->parent) {
1707 sibling = sibling->parent->child;
1708 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001709 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001710 }
1711 sibling->prev = node;
1712 }
1713 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001714
Michal Vasko9f96a052020-03-10 09:41:45 +01001715 for (par = node->parent; par; par = par->parent) {
1716 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1717 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001718 par->flags &= ~LYD_DEFAULT;
1719 }
Michal Vaskob104f112020-07-17 09:54:54 +02001720 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001721 /* rehash key-less list */
1722 lyd_hash((struct lyd_node *)par);
1723 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001724 }
Michal Vasko90932a92020-02-12 14:33:03 +01001725}
1726
1727/**
1728 * @brief Insert node before a sibling.
1729 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001730 * Handles inserting into NP containers and key-less lists.
1731 *
Michal Vasko90932a92020-02-12 14:33:03 +01001732 * @param[in] sibling Sibling to insert before.
1733 * @param[in] node Node to insert.
1734 */
1735static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001736lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001737{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001738 struct lyd_node_inner *par;
1739
Michal Vasko90932a92020-02-12 14:33:03 +01001740 assert(!node->next && (node->prev == node));
1741
1742 node->next = sibling;
1743 /* covers situation of sibling being first */
1744 node->prev = sibling->prev;
1745 sibling->prev = node;
1746 if (node->prev->next) {
1747 /* sibling had a preceding node */
1748 node->prev->next = node;
1749 } else if (sibling->parent) {
1750 /* sibling was first and we must also change parent child pointer */
1751 sibling->parent->child = node;
1752 }
1753 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001754
Michal Vasko9f96a052020-03-10 09:41:45 +01001755 for (par = node->parent; par; par = par->parent) {
1756 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1757 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001758 par->flags &= ~LYD_DEFAULT;
1759 }
Michal Vaskob104f112020-07-17 09:54:54 +02001760 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001761 /* rehash key-less list */
1762 lyd_hash((struct lyd_node *)par);
1763 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001764 }
Michal Vasko90932a92020-02-12 14:33:03 +01001765}
1766
1767/**
Michal Vaskob104f112020-07-17 09:54:54 +02001768 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001769 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001770 * Handles inserting into NP containers and key-less lists.
1771 *
Michal Vasko90932a92020-02-12 14:33:03 +01001772 * @param[in] parent Parent to insert into.
1773 * @param[in] node Node to insert.
1774 */
1775static void
Michal Vaskob104f112020-07-17 09:54:54 +02001776lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001777{
1778 struct lyd_node_inner *par;
1779
Michal Vaskob104f112020-07-17 09:54:54 +02001780 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001781 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001782
1783 par = (struct lyd_node_inner *)parent;
1784
Michal Vaskob104f112020-07-17 09:54:54 +02001785 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001786 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001787
Michal Vaskod989ba02020-08-24 10:59:24 +02001788 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001789 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1790 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001791 par->flags &= ~LYD_DEFAULT;
1792 }
Michal Vasko52927e22020-03-16 17:26:14 +01001793 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001794 /* rehash key-less list */
1795 lyd_hash((struct lyd_node *)par);
1796 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001797 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001798}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001799
Michal Vasko751cb4d2020-07-14 12:25:28 +02001800/**
1801 * @brief Learn whether a list instance has all the keys.
1802 *
1803 * @param[in] list List instance to check.
1804 * @return non-zero if all the keys were found,
1805 * @return 0 otherwise.
1806 */
1807static int
1808lyd_insert_has_keys(const struct lyd_node *list)
1809{
1810 const struct lyd_node *key;
1811 const struct lysc_node *skey = NULL;
1812
1813 assert(list->schema->nodetype == LYS_LIST);
1814 key = lyd_node_children(list, 0);
1815 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1816 if (!key || (key->schema != skey)) {
1817 /* key missing */
1818 return 0;
1819 }
1820
1821 key = key->next;
1822 }
1823
1824 /* all keys found */
1825 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001826}
1827
1828void
Michal Vaskob104f112020-07-17 09:54:54 +02001829lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001830{
Michal Vaskob104f112020-07-17 09:54:54 +02001831 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001832
Michal Vaskob104f112020-07-17 09:54:54 +02001833 /* inserting list without its keys is not supported */
1834 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001835
Michal Vaskob104f112020-07-17 09:54:54 +02001836 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1837 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001838 }
Michal Vasko90932a92020-02-12 14:33:03 +01001839
Michal Vaskob104f112020-07-17 09:54:54 +02001840 /* get first sibling */
1841 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001842
Michal Vaskob104f112020-07-17 09:54:54 +02001843 /* find the anchor, our next node, so we can insert before it */
1844 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1845 if (anchor) {
1846 lyd_insert_before_node(anchor, node);
1847 } else if (first_sibling) {
1848 lyd_insert_after_node(first_sibling->prev, node);
1849 } else if (parent) {
1850 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001851 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001852 *first_sibling_p = node;
1853 }
1854
1855 /* insert into parent HT */
1856 lyd_insert_hash(node);
1857
1858 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001859 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001860 lyd_hash(parent);
1861
1862 /* now we can insert even the list into its parent HT */
1863 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001864 }
Michal Vasko90932a92020-02-12 14:33:03 +01001865}
1866
Michal Vaskof03ed032020-03-04 13:31:44 +01001867static LY_ERR
1868lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1869{
1870 const struct lysc_node *par2;
1871
1872 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001873 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001874
1875 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001876 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001877
1878 if (parent) {
1879 /* inner node */
1880 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001881 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1882 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001883 return LY_EINVAL;
1884 }
1885 } else {
1886 /* top-level node */
1887 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001888 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001889 return LY_EINVAL;
1890 }
1891 }
1892
1893 return LY_SUCCESS;
1894}
1895
1896API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001897lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001898{
1899 struct lyd_node *iter;
1900
Michal Vasko654bc852020-06-23 13:28:06 +02001901 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001902
1903 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1904
1905 if (node->schema->flags & LYS_KEY) {
1906 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1907 return LY_EINVAL;
1908 }
1909
1910 if (node->parent || node->prev->next) {
1911 lyd_unlink_tree(node);
1912 }
1913
1914 while (node) {
1915 iter = node->next;
1916 lyd_unlink_tree(node);
1917 lyd_insert_node(parent, NULL, node);
1918 node = iter;
1919 }
1920 return LY_SUCCESS;
1921}
1922
1923API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001924lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001925{
1926 struct lyd_node *iter;
1927
Michal Vaskob104f112020-07-17 09:54:54 +02001928 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001929
Michal Vaskob104f112020-07-17 09:54:54 +02001930 if (sibling) {
1931 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001932 }
1933
1934 if (node->parent || node->prev->next) {
1935 lyd_unlink_tree(node);
1936 }
1937
1938 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001939 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001940 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001941 return LY_EINVAL;
1942 }
1943
Michal Vaskob1b5c262020-03-05 14:29:47 +01001944 iter = node->next;
1945 lyd_unlink_tree(node);
1946 lyd_insert_node(NULL, &sibling, node);
1947 node = iter;
1948 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001949
Michal Vaskob104f112020-07-17 09:54:54 +02001950 if (first) {
1951 /* find the first sibling */
1952 *first = sibling;
1953 while ((*first)->prev->next) {
1954 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001955 }
1956 }
1957
1958 return LY_SUCCESS;
1959}
1960
Michal Vaskob1b5c262020-03-05 14:29:47 +01001961API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001962lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1963{
1964 struct lyd_node *iter;
1965
1966 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1967
Michal Vasko62ed12d2020-05-21 10:08:25 +02001968 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001969
Michal Vaskob104f112020-07-17 09:54:54 +02001970 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001971 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001972 return LY_EINVAL;
1973 }
1974
1975 if (node->parent || node->prev->next) {
1976 lyd_unlink_tree(node);
1977 }
1978
1979 /* insert in reverse order to get the original order */
1980 node = node->prev;
1981 while (node) {
1982 iter = node->prev;
1983 lyd_unlink_tree(node);
1984
1985 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001986 lyd_insert_hash(node);
1987
Michal Vaskof03ed032020-03-04 13:31:44 +01001988 /* move the anchor accordingly */
1989 sibling = node;
1990
1991 node = (iter == node) ? NULL : iter;
1992 }
1993 return LY_SUCCESS;
1994}
1995
1996API LY_ERR
1997lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1998{
1999 struct lyd_node *iter;
2000
2001 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2002
Michal Vasko62ed12d2020-05-21 10:08:25 +02002003 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002004
Michal Vaskob104f112020-07-17 09:54:54 +02002005 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002006 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002007 return LY_EINVAL;
2008 }
2009
2010 if (node->parent || node->prev->next) {
2011 lyd_unlink_tree(node);
2012 }
2013
2014 while (node) {
2015 iter = node->next;
2016 lyd_unlink_tree(node);
2017
2018 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002019 lyd_insert_hash(node);
2020
Michal Vaskof03ed032020-03-04 13:31:44 +01002021 /* move the anchor accordingly */
2022 sibling = node;
2023
2024 node = iter;
2025 }
2026 return LY_SUCCESS;
2027}
2028
2029API void
2030lyd_unlink_tree(struct lyd_node *node)
2031{
2032 struct lyd_node *iter;
2033
2034 if (!node) {
2035 return;
2036 }
2037
Michal Vaskob104f112020-07-17 09:54:54 +02002038 /* update hashes while still linked into the tree */
2039 lyd_unlink_hash(node);
2040
Michal Vaskof03ed032020-03-04 13:31:44 +01002041 /* unlink from siblings */
2042 if (node->prev->next) {
2043 node->prev->next = node->next;
2044 }
2045 if (node->next) {
2046 node->next->prev = node->prev;
2047 } else {
2048 /* unlinking the last node */
2049 if (node->parent) {
2050 iter = node->parent->child;
2051 } else {
2052 iter = node->prev;
2053 while (iter->prev != node) {
2054 iter = iter->prev;
2055 }
2056 }
2057 /* update the "last" pointer from the first node */
2058 iter->prev = node->prev;
2059 }
2060
2061 /* unlink from parent */
2062 if (node->parent) {
2063 if (node->parent->child == node) {
2064 /* the node is the first child */
2065 node->parent->child = node->next;
2066 }
2067
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002068 /* check for NP container whether its last non-default node is not being unlinked */
2069 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2070 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2071 LY_LIST_FOR(node->parent->child, iter) {
2072 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2073 break;
2074 }
2075 }
2076 if (!iter) {
2077 node->parent->flags |= LYD_DEFAULT;
2078 }
2079 }
2080
Michal Vaskof03ed032020-03-04 13:31:44 +01002081 /* check for keyless list and update its hash */
2082 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002083 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002084 lyd_hash(iter);
2085 }
2086 }
2087
2088 node->parent = NULL;
2089 }
2090
2091 node->next = NULL;
2092 node->prev = node;
2093}
2094
Michal Vaskoa5da3292020-08-12 13:10:50 +02002095void
Radek Krejci1798aae2020-07-14 13:26:06 +02002096lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2097{
2098 struct lyd_meta *last, *iter;
2099
2100 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002101
2102 if (!meta) {
2103 return;
2104 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002105
2106 for (iter = meta; iter; iter = iter->next) {
2107 iter->parent = parent;
2108 }
2109
2110 /* insert as the last attribute */
2111 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002112 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002113 last->next = meta;
2114 } else {
2115 parent->meta = meta;
2116 }
2117
2118 /* remove default flags from NP containers */
2119 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2120 parent->flags &= ~LYD_DEFAULT;
2121 parent = (struct lyd_node *)parent->parent;
2122 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002123}
2124
2125LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002126lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci857189e2020-09-01 13:26:36 +02002127 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, uint32_t value_hint, LY_PREFIX_FORMAT format,
Radek Krejci0f969882020-08-21 16:56:47 +02002128 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002129{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002130 LY_ERR ret, rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002131 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002132 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002133 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002134
Michal Vasko9f96a052020-03-10 09:41:45 +01002135 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002136
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002137 LY_ARRAY_FOR(mod->compiled->exts, u) {
2138 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2139 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002140 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002141 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002142 break;
2143 }
2144 }
2145 if (!ant) {
2146 /* attribute is not defined as a metadata annotation (RFC 7952) */
2147 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2148 mod->name, name_len, name);
2149 return LY_EINVAL;
2150 }
2151
Michal Vasko9f96a052020-03-10 09:41:45 +01002152 mt = calloc(1, sizeof *mt);
2153 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2154 mt->parent = parent;
2155 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002156 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, value_hint, format, prefix_data, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01002157 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002158 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002159 return ret;
2160 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002161 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2162 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002163
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002164 /* insert as the last attribute */
2165 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002166 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002167 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002168 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002169 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002170 }
2171
Michal Vasko9f96a052020-03-10 09:41:45 +01002172 if (meta) {
2173 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002174 }
2175 return ret;
2176}
2177
Michal Vaskoa5da3292020-08-12 13:10:50 +02002178void
2179lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2180{
2181 struct lyd_attr *last, *iter;
2182 struct lyd_node_opaq *opaq;
2183
2184 assert(parent && !parent->schema);
2185
2186 if (!attr) {
2187 return;
2188 }
2189
2190 opaq = (struct lyd_node_opaq *)parent;
2191 for (iter = attr; iter; iter = iter->next) {
2192 iter->parent = opaq;
2193 }
2194
2195 /* insert as the last attribute */
2196 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002197 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002198 last->next = attr;
2199 } else {
2200 opaq->attr = attr;
2201 }
2202}
2203
Michal Vasko52927e22020-03-16 17:26:14 +01002204LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002205lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
Radek Krejci857189e2020-09-01 13:26:36 +02002206 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, uint32_t value_hint, LYD_FORMAT format,
Radek Krejci0f969882020-08-21 16:56:47 +02002207 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len)
Michal Vasko52927e22020-03-16 17:26:14 +01002208{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002209 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002210 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002211
2212 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2213 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002214
2215 if (!value_len) {
2216 value = "";
2217 }
2218
2219 at = calloc(1, sizeof *at);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002220 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
2221 at->hint = value_hint;
2222 at->format = format;
2223 at->val_prefs = val_prefs;
2224
Radek Krejci011e4aa2020-09-04 15:22:31 +02002225 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002226 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002227 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2228 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002229 *dynamic = 0;
2230 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002231 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002232 }
2233
Radek Krejci1798aae2020-07-14 13:26:06 +02002234 if (prefix_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002235 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +02002236 }
2237 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002238 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002239 }
2240
2241 /* insert as the last attribute */
2242 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002243 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002244 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002245 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002246 last->next = at;
2247 }
2248
Radek Krejci011e4aa2020-09-04 15:22:31 +02002249finish:
2250 if (ret) {
2251 lyd_free_attr_single(ctx, at);
2252 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002253 *attr = at;
2254 }
2255 return LY_SUCCESS;
2256}
2257
Radek Krejci084289f2019-07-09 17:35:30 +02002258API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002259lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002260{
Michal Vasko004d3152020-06-11 19:59:22 +02002261 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002262
Michal Vasko004d3152020-06-11 19:59:22 +02002263 if (ly_path_eval(path, tree, &target)) {
2264 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002265 }
2266
Michal Vasko004d3152020-06-11 19:59:22 +02002267 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002268}
2269
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002270API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002271lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002272{
2273 const struct lyd_node *iter1, *iter2;
2274 struct lyd_node_term *term1, *term2;
2275 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002276 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002277 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002278
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002279 if (!node1 || !node2) {
2280 if (node1 == node2) {
2281 return LY_SUCCESS;
2282 } else {
2283 return LY_ENOT;
2284 }
2285 }
2286
Michal Vaskob7be7a82020-08-20 09:09:04 +02002287 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002288 return LY_ENOT;
2289 }
2290
2291 if (node1->hash != node2->hash) {
2292 return LY_ENOT;
2293 }
Michal Vasko52927e22020-03-16 17:26:14 +01002294 /* 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 +02002295
Michal Vasko52927e22020-03-16 17:26:14 +01002296 if (!node1->schema) {
2297 opaq1 = (struct lyd_node_opaq *)node1;
2298 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002299 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002300 return LY_ENOT;
2301 }
Michal Vasko52927e22020-03-16 17:26:14 +01002302 switch (opaq1->format) {
2303 case LYD_XML:
2304 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2305 return LY_ENOT;
2306 }
2307 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002308 case LYD_JSON:
2309 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2310 if (strcmp(opaq1->value, opaq2->value)) {
2311 return LY_ENOT;
2312 }
2313 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002314 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002315 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002316 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002317 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002318 return LY_EINT;
2319 }
2320 if (options & LYD_COMPARE_FULL_RECURSION) {
2321 iter1 = opaq1->child;
2322 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002323 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002324 }
2325 return LY_SUCCESS;
2326 } else {
2327 switch (node1->schema->nodetype) {
2328 case LYS_LEAF:
2329 case LYS_LEAFLIST:
2330 if (options & LYD_COMPARE_DEFAULTS) {
2331 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2332 return LY_ENOT;
2333 }
2334 }
2335
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002336 term1 = (struct lyd_node_term *)node1;
2337 term2 = (struct lyd_node_term *)node2;
2338 if (term1->value.realtype != term2->value.realtype) {
2339 return LY_ENOT;
2340 }
Michal Vasko52927e22020-03-16 17:26:14 +01002341
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002342 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002343 case LYS_CONTAINER:
2344 if (options & LYD_COMPARE_DEFAULTS) {
2345 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2346 return LY_ENOT;
2347 }
2348 }
2349 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002350 iter1 = ((struct lyd_node_inner *)node1)->child;
2351 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002352 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002353 }
2354 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002355 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002356 case LYS_ACTION:
2357 if (options & LYD_COMPARE_FULL_RECURSION) {
2358 /* TODO action/RPC
2359 goto all_children_compare;
2360 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002361 }
2362 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002363 case LYS_NOTIF:
2364 if (options & LYD_COMPARE_FULL_RECURSION) {
2365 /* TODO Notification
2366 goto all_children_compare;
2367 */
2368 }
2369 return LY_SUCCESS;
2370 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002371 iter1 = ((struct lyd_node_inner *)node1)->child;
2372 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002373
2374 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2375 /* lists with keys, their equivalence is based on their keys */
Michal Vasko22df3f02020-08-24 13:29:22 +02002376 for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002377 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002378 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002379 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002380 return LY_ENOT;
2381 }
2382 iter1 = iter1->next;
2383 iter2 = iter2->next;
2384 }
2385 } else {
2386 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2387
Radek Krejci0f969882020-08-21 16:56:47 +02002388all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002389 if (!iter1 && !iter2) {
2390 /* no children, nothing to compare */
2391 return LY_SUCCESS;
2392 }
2393
Michal Vaskod989ba02020-08-24 10:59:24 +02002394 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002395 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002396 return LY_ENOT;
2397 }
2398 }
2399 if (iter1 || iter2) {
2400 return LY_ENOT;
2401 }
2402 }
2403 return LY_SUCCESS;
2404 case LYS_ANYXML:
2405 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002406 any1 = (struct lyd_node_any *)node1;
2407 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002408
2409 if (any1->value_type != any2->value_type) {
2410 return LY_ENOT;
2411 }
2412 switch (any1->value_type) {
2413 case LYD_ANYDATA_DATATREE:
2414 iter1 = any1->value.tree;
2415 iter2 = any2->value.tree;
2416 goto all_children_compare;
2417 case LYD_ANYDATA_STRING:
2418 case LYD_ANYDATA_XML:
2419 case LYD_ANYDATA_JSON:
2420 len1 = strlen(any1->value.str);
2421 len2 = strlen(any2->value.str);
2422 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2423 return LY_ENOT;
2424 }
2425 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002426 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002427 len1 = lyd_lyb_data_length(any1->value.mem);
2428 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002429 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2430 return LY_ENOT;
2431 }
2432 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002433 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002434 }
2435 }
2436
Michal Vaskob7be7a82020-08-20 09:09:04 +02002437 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002438 return LY_EINT;
2439}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002440
Michal Vasko21725742020-06-29 11:49:25 +02002441API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002442lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002443{
Michal Vaskod989ba02020-08-24 10:59:24 +02002444 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002445 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2446 }
2447
Michal Vasko11a81432020-07-28 16:31:29 +02002448 if (node1 == node2) {
2449 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002450 }
Michal Vasko11a81432020-07-28 16:31:29 +02002451 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002452}
2453
2454API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002455lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2456{
2457 if (!meta1 || !meta2) {
2458 if (meta1 == meta2) {
2459 return LY_SUCCESS;
2460 } else {
2461 return LY_ENOT;
2462 }
2463 }
2464
Michal Vaskob7be7a82020-08-20 09:09:04 +02002465 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002466 return LY_ENOT;
2467 }
2468
2469 if (meta1->value.realtype != meta2->value.realtype) {
2470 return LY_ENOT;
2471 }
2472
2473 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2474}
2475
Radek Krejci22ebdba2019-07-25 13:59:43 +02002476/**
Michal Vasko52927e22020-03-16 17:26:14 +01002477 * @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 +02002478 *
2479 * 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 +02002480 *
2481 * @param[in] node Original node to duplicate
2482 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2483 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2484 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2485 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2486 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002487 */
Michal Vasko52927e22020-03-16 17:26:14 +01002488static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002489lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02002490 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002491{
Michal Vasko52927e22020-03-16 17:26:14 +01002492 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002493 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002494 struct lyd_meta *meta;
2495 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002496 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002497
Michal Vasko52927e22020-03-16 17:26:14 +01002498 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002499
Michal Vasko52927e22020-03-16 17:26:14 +01002500 if (!node->schema) {
2501 dup = calloc(1, sizeof(struct lyd_node_opaq));
2502 } else {
2503 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002504 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002505 case LYS_ACTION:
2506 case LYS_NOTIF:
2507 case LYS_CONTAINER:
2508 case LYS_LIST:
2509 dup = calloc(1, sizeof(struct lyd_node_inner));
2510 break;
2511 case LYS_LEAF:
2512 case LYS_LEAFLIST:
2513 dup = calloc(1, sizeof(struct lyd_node_term));
2514 break;
2515 case LYS_ANYDATA:
2516 case LYS_ANYXML:
2517 dup = calloc(1, sizeof(struct lyd_node_any));
2518 break;
2519 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002520 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002521 ret = LY_EINT;
2522 goto error;
2523 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002524 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002525 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002526
Michal Vaskof6df0a02020-06-16 13:08:34 +02002527 if (options & LYD_DUP_WITH_FLAGS) {
2528 dup->flags = node->flags;
2529 } else {
2530 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2531 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002532 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002533 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002534
Michal Vasko25a32822020-07-09 15:48:22 +02002535 /* duplicate metadata */
2536 if (!(options & LYD_DUP_NO_META)) {
2537 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002538 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002539 }
2540 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002541
2542 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002543 if (!dup->schema) {
2544 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2545 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2546 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002547
2548 if (options & LYD_DUP_RECURSIVE) {
2549 /* duplicate all the children */
2550 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002551 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002552 }
2553 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002554 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002555 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002556 if (orig->prefix.id) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002557 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002558 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002559 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0, &opaq->prefix.module_ns), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002560 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002561 LY_ARRAY_CREATE_GOTO(LYD_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002562 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002563 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
2564 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0, &opaq->val_prefs[u].module_ns), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002565 LY_ARRAY_INCREMENT(opaq->val_prefs);
2566 }
2567 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002568 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002569 opaq->ctx = orig->ctx;
2570 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2571 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2572 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2573
2574 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002575 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
2576 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002577 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2578 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2579 struct lyd_node *child;
2580
2581 if (options & LYD_DUP_RECURSIVE) {
2582 /* duplicate all the children */
2583 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002584 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002585 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002586 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002587 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002588 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002589 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002590 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002591 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002592 if (!child) {
2593 /* possibly not keys are present in filtered tree */
2594 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002595 } else if (child->schema != key) {
2596 /* possibly not all keys are present in filtered tree,
2597 * but there can be also some non-key nodes */
2598 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002599 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002600 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002601 child = child->next;
2602 }
2603 }
2604 lyd_hash(dup);
2605 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002606 dup->hash = node->hash;
2607 any = (struct lyd_node_any *)node;
2608 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002609 }
2610
Michal Vasko52927e22020-03-16 17:26:14 +01002611 /* insert */
2612 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002613
2614 if (dup_p) {
2615 *dup_p = dup;
2616 }
2617 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002618
2619error:
Michal Vasko52927e22020-03-16 17:26:14 +01002620 lyd_free_tree(dup);
2621 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002622}
2623
Michal Vasko3a41dff2020-07-15 14:30:28 +02002624static LY_ERR
2625lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
Radek Krejci0f969882020-08-21 16:56:47 +02002626 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002627{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002628 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002629 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002630
2631 *dup_parent = NULL;
2632 *local_parent = NULL;
2633
2634 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2635 if (parent && (parent->schema == orig_parent->schema)) {
2636 /* stop creating parents, connect what we have into the provided parent */
2637 iter = parent;
2638 repeat = 0;
2639 } else {
2640 iter = NULL;
2641 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002642 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002643 }
2644 if (!*local_parent) {
2645 *local_parent = (struct lyd_node_inner *)iter;
2646 }
2647 if (iter->child) {
2648 /* 1) list - add after keys
2649 * 2) provided parent with some children */
2650 iter->child->prev->next = *dup_parent;
2651 if (*dup_parent) {
2652 (*dup_parent)->prev = iter->child->prev;
2653 iter->child->prev = *dup_parent;
2654 }
2655 } else {
2656 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2657 }
2658 if (*dup_parent) {
2659 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2660 }
2661 *dup_parent = (struct lyd_node *)iter;
2662 }
2663
2664 if (repeat && parent) {
2665 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002666 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko3a41dff2020-07-15 14:30:28 +02002667 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2668 return LY_EINVAL;
2669 }
2670
2671 return LY_SUCCESS;
2672}
2673
2674static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002675lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002676{
2677 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002678 const struct lyd_node *orig; /* original node to be duplicated */
2679 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002680 struct lyd_node *top = NULL; /* the most higher created node */
2681 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002682
Michal Vasko3a41dff2020-07-15 14:30:28 +02002683 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002684
2685 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002686 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002687 } else {
2688 local_parent = parent;
2689 }
2690
Radek Krejci22ebdba2019-07-25 13:59:43 +02002691 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002692 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002693 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2694 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002695 break;
2696 }
2697 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002698
2699 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02002700 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002701 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2702 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002703 }
2704 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002705
2706 if (dup) {
2707 *dup = first;
2708 }
2709 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002710
2711error:
2712 if (top) {
2713 lyd_free_tree(top);
2714 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002715 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002716 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002717 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002718}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002719
Michal Vasko3a41dff2020-07-15 14:30:28 +02002720API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002721lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002722{
2723 return lyd_dup(node, parent, options, 1, dup);
2724}
2725
2726API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002727lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002728{
2729 return lyd_dup(node, parent, options, 0, dup);
2730}
2731
2732API LY_ERR
2733lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002734{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002735 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002736 struct lyd_meta *mt, *last;
2737
Michal Vasko3a41dff2020-07-15 14:30:28 +02002738 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002739
2740 /* create a copy */
2741 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002742 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002743 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002744 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002745 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
2746 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002747
2748 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002749 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002750 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002751 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002752 last->next = mt;
2753 } else {
2754 node->meta = mt;
2755 }
2756
Radek Krejci011e4aa2020-09-04 15:22:31 +02002757finish:
2758 if (ret) {
2759 lyd_free_meta_single(mt);
2760 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002761 *dup = mt;
2762 }
2763 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002764}
2765
Michal Vasko4490d312020-06-16 13:08:55 +02002766/**
2767 * @brief Merge a source sibling into target siblings.
2768 *
2769 * @param[in,out] first_trg First target sibling, is updated if top-level.
2770 * @param[in] parent_trg Target parent.
2771 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2772 * @param[in] options Merge options.
2773 * @return LY_ERR value.
2774 */
2775static LY_ERR
2776lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Radek Krejci1deb5be2020-08-26 16:43:36 +02002777 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02002778{
2779 LY_ERR ret;
2780 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002781 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002782 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002783
2784 sibling_src = *sibling_src_p;
2785 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2786 /* try to find the exact instance */
2787 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2788 } else {
2789 /* try to simply find the node, there cannot be more instances */
2790 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2791 }
2792
2793 if (!ret) {
2794 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002795 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002796 /* since they are different, they cannot both be default */
2797 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2798
Michal Vasko3a41dff2020-07-15 14:30:28 +02002799 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2800 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002801 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002802 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2803 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko4490d312020-06-16 13:08:55 +02002804 &((struct lyd_node_term *)match_trg)->value));
2805
2806 /* copy flags and add LYD_NEW */
2807 match_trg->flags = sibling_src->flags | LYD_NEW;
2808 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002809 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002810 /* update value */
2811 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002812 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002813
2814 /* copy flags and add LYD_NEW */
2815 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002816 } else {
2817 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002818 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002819 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2820 }
2821 }
2822 } else {
2823 /* node not found, merge it */
2824 if (options & LYD_MERGE_DESTRUCT) {
2825 dup_src = (struct lyd_node *)sibling_src;
2826 lyd_unlink_tree(dup_src);
2827 /* spend it */
2828 *sibling_src_p = NULL;
2829 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002830 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 +02002831 }
2832
2833 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002834 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002835 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002836 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002837 }
2838
2839 lyd_insert_node(parent_trg, first_trg, dup_src);
2840 }
2841
2842 return LY_SUCCESS;
2843}
2844
Michal Vasko3a41dff2020-07-15 14:30:28 +02002845static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002846lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002847{
2848 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02002849 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02002850
2851 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2852
2853 if (!source) {
2854 /* nothing to merge */
2855 return LY_SUCCESS;
2856 }
2857
2858 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002859 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002860 return LY_EINVAL;
2861 }
2862
2863 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02002864 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002865 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2866 if (first && !sibling_src) {
2867 /* source was spent (unlinked), move to the next node */
2868 source = tmp;
2869 }
2870
Michal Vasko3a41dff2020-07-15 14:30:28 +02002871 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002872 break;
2873 }
2874 }
2875
2876 if (options & LYD_MERGE_DESTRUCT) {
2877 /* free any leftover source data that were not merged */
2878 lyd_free_siblings((struct lyd_node *)source);
2879 }
2880
2881 return LY_SUCCESS;
2882}
2883
Michal Vasko3a41dff2020-07-15 14:30:28 +02002884API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002885lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002886{
2887 return lyd_merge(target, source, options, 1);
2888}
2889
2890API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002891lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002892{
2893 return lyd_merge(target, source, options, 0);
2894}
2895
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002896static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002897lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002898{
Michal Vasko14654712020-02-06 08:35:21 +01002899 /* ending \0 */
2900 ++reqlen;
2901
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002902 if (reqlen > *buflen) {
2903 if (is_static) {
2904 return LY_EINCOMPLETE;
2905 }
2906
2907 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2908 if (!*buffer) {
2909 return LY_EMEM;
2910 }
2911
2912 *buflen = reqlen;
2913 }
2914
2915 return LY_SUCCESS;
2916}
2917
Michal Vaskod59035b2020-07-08 12:00:06 +02002918LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002919lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002920{
2921 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002922 size_t len;
2923 const char *val;
2924 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002925
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002926 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002927 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002928 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002929 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002930
2931 quot = '\'';
2932 if (strchr(val, '\'')) {
2933 quot = '"';
2934 }
2935 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002936 }
2937
2938 return LY_SUCCESS;
2939}
2940
2941/**
2942 * @brief Append leaf-list value predicate to path.
2943 *
2944 * @param[in] node Node to print.
2945 * @param[in,out] buffer Buffer to print to.
2946 * @param[in,out] buflen Current buffer length.
2947 * @param[in,out] bufused Current number of characters used in @p buffer.
2948 * @param[in] is_static Whether buffer is static or can be reallocated.
2949 * @return LY_ERR
2950 */
2951static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002952lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002953{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002954 size_t len;
2955 const char *val;
2956 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002957
Michal Vaskob7be7a82020-08-20 09:09:04 +02002958 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002959 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002960 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002961
2962 quot = '\'';
2963 if (strchr(val, '\'')) {
2964 quot = '"';
2965 }
2966 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2967
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002968 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002969}
2970
2971/**
2972 * @brief Append node position (relative to its other instances) predicate to path.
2973 *
2974 * @param[in] node Node to print.
2975 * @param[in,out] buffer Buffer to print to.
2976 * @param[in,out] buflen Current buffer length.
2977 * @param[in,out] bufused Current number of characters used in @p buffer.
2978 * @param[in] is_static Whether buffer is static or can be reallocated.
2979 * @return LY_ERR
2980 */
2981static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002982lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002983{
2984 const struct lyd_node *first, *iter;
2985 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002986 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002987 char *val = NULL;
2988 LY_ERR rc;
2989
2990 if (node->parent) {
2991 first = node->parent->child;
2992 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02002993 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002994 }
2995 pos = 1;
2996 for (iter = first; iter != node; iter = iter->next) {
2997 if (iter->schema == node->schema) {
2998 ++pos;
2999 }
3000 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003001 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003002 return LY_EMEM;
3003 }
3004
3005 len = 1 + strlen(val) + 1;
3006 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3007 if (rc != LY_SUCCESS) {
3008 goto cleanup;
3009 }
3010
3011 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3012
3013cleanup:
3014 free(val);
3015 return rc;
3016}
3017
3018API char *
3019lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3020{
Radek Krejci857189e2020-09-01 13:26:36 +02003021 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003022 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003023 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003024 const struct lyd_node *iter;
3025 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003026 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003027
3028 LY_CHECK_ARG_RET(NULL, node, NULL);
3029 if (buffer) {
3030 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3031 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003032 } else {
3033 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003034 }
3035
3036 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003037 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003038 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003039 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003040 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3041 ++depth;
3042 }
3043
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003044 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003045 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003046 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003047 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003048iter_print:
3049 /* print prefix and name */
3050 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003051 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003052 mod = iter->schema->module;
3053 }
3054
3055 /* realloc string */
Michal Vasko22df3f02020-08-24 13:29:22 +02003056 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : strlen(((struct lyd_node_opaq *)iter)->name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003057 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3058 if (rc != LY_SUCCESS) {
3059 break;
3060 }
3061
3062 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003063 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vasko22df3f02020-08-24 13:29:22 +02003064 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003065
Michal Vasko790b2bc2020-08-03 13:35:06 +02003066 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003067 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003068 switch (iter->schema->nodetype) {
3069 case LYS_LIST:
3070 if (iter->schema->flags & LYS_KEYLESS) {
3071 /* print its position */
3072 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3073 } else {
3074 /* print all list keys in predicates */
3075 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3076 }
3077 break;
3078 case LYS_LEAFLIST:
3079 if (iter->schema->flags & LYS_CONFIG_W) {
3080 /* print leaf-list value */
3081 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3082 } else {
3083 /* print its position */
3084 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3085 }
3086 break;
3087 default:
3088 /* nothing to print more */
3089 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003090 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003091 }
3092 if (rc != LY_SUCCESS) {
3093 break;
3094 }
3095
Michal Vasko14654712020-02-06 08:35:21 +01003096 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003097 }
3098 break;
3099 }
3100
3101 return buffer;
3102}
Michal Vaskoe444f752020-02-10 12:20:06 +01003103
Michal Vasko25a32822020-07-09 15:48:22 +02003104API struct lyd_meta *
3105lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3106{
3107 struct lyd_meta *ret = NULL;
3108 const struct ly_ctx *ctx;
3109 const char *prefix, *tmp;
3110 char *str;
3111 size_t pref_len, name_len;
3112
3113 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3114
3115 if (!first) {
3116 return NULL;
3117 }
3118
3119 ctx = first->annotation->module->ctx;
3120
3121 /* parse the name */
3122 tmp = name;
3123 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3124 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3125 return NULL;
3126 }
3127
3128 /* find the module */
3129 if (prefix) {
3130 str = strndup(prefix, pref_len);
3131 module = ly_ctx_get_module_latest(ctx, str);
3132 free(str);
3133 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3134 }
3135
3136 /* find the metadata */
3137 LY_LIST_FOR(first, first) {
3138 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3139 ret = (struct lyd_meta *)first;
3140 break;
3141 }
3142 }
3143
3144 return ret;
3145}
3146
Michal Vasko9b368d32020-02-14 13:53:31 +01003147API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003148lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3149{
3150 struct lyd_node **match_p;
3151 struct lyd_node_inner *parent;
3152
Michal Vaskof03ed032020-03-04 13:31:44 +01003153 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003154
Michal Vasko62ed12d2020-05-21 10:08:25 +02003155 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3156 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003157 if (match) {
3158 *match = NULL;
3159 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003160 return LY_ENOTFOUND;
3161 }
3162
3163 /* find first sibling */
3164 if (siblings->parent) {
3165 siblings = siblings->parent->child;
3166 } else {
3167 while (siblings->prev->next) {
3168 siblings = siblings->prev;
3169 }
3170 }
3171
3172 parent = (struct lyd_node_inner *)siblings->parent;
3173 if (parent && parent->children_ht) {
3174 assert(target->hash);
3175
3176 /* find by hash */
3177 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003178 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003179 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003180 siblings = *match_p;
3181 } else {
3182 siblings = NULL;
3183 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003184 } else {
3185 /* not found */
3186 siblings = NULL;
3187 }
3188 } else {
3189 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003190 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003191 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003192 break;
3193 }
3194 }
3195 }
3196
3197 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003198 if (match) {
3199 *match = NULL;
3200 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003201 return LY_ENOTFOUND;
3202 }
3203
Michal Vasko9b368d32020-02-14 13:53:31 +01003204 if (match) {
3205 *match = (struct lyd_node *)siblings;
3206 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003207 return LY_SUCCESS;
3208}
3209
Radek Krejci857189e2020-09-01 13:26:36 +02003210/**
3211 * @brief Comparison callback to match schema node with a schema of a data node.
3212 *
3213 * @param[in] val1_p Pointer to the schema node
3214 * @param[in] val2_p Pointer to the data node
3215 * Implementation of ::values_equal_cb.
3216 */
3217static ly_bool
3218lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko90932a92020-02-12 14:33:03 +01003219{
3220 struct lysc_node *val1;
3221 struct lyd_node *val2;
3222
3223 val1 = *((struct lysc_node **)val1_p);
3224 val2 = *((struct lyd_node **)val2_p);
3225
Michal Vasko90932a92020-02-12 14:33:03 +01003226 if (val1 == val2->schema) {
3227 /* schema match is enough */
3228 return 1;
3229 } else {
3230 return 0;
3231 }
3232}
3233
3234static LY_ERR
3235lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3236{
3237 struct lyd_node **match_p;
3238 struct lyd_node_inner *parent;
3239 uint32_t hash;
3240 values_equal_cb ht_cb;
3241
Michal Vaskob104f112020-07-17 09:54:54 +02003242 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003243
3244 parent = (struct lyd_node_inner *)siblings->parent;
3245 if (parent && parent->children_ht) {
3246 /* calculate our hash */
3247 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3248 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3249 hash = dict_hash_multi(hash, NULL, 0);
3250
3251 /* use special hash table function */
3252 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3253
3254 /* find by hash */
3255 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3256 siblings = *match_p;
3257 } else {
3258 /* not found */
3259 siblings = NULL;
3260 }
3261
3262 /* set the original hash table compare function back */
3263 lyht_set_cb(parent->children_ht, ht_cb);
3264 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003265 /* find first sibling */
3266 if (siblings->parent) {
3267 siblings = siblings->parent->child;
3268 } else {
3269 while (siblings->prev->next) {
3270 siblings = siblings->prev;
3271 }
3272 }
3273
3274 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003275 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003276 if (siblings->schema == schema) {
3277 /* schema match is enough */
3278 break;
3279 }
3280 }
3281 }
3282
3283 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003284 if (match) {
3285 *match = NULL;
3286 }
Michal Vasko90932a92020-02-12 14:33:03 +01003287 return LY_ENOTFOUND;
3288 }
3289
Michal Vasko9b368d32020-02-14 13:53:31 +01003290 if (match) {
3291 *match = (struct lyd_node *)siblings;
3292 }
Michal Vasko90932a92020-02-12 14:33:03 +01003293 return LY_SUCCESS;
3294}
3295
Michal Vaskoe444f752020-02-10 12:20:06 +01003296API LY_ERR
3297lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
Radek Krejci0f969882020-08-21 16:56:47 +02003298 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003299{
3300 LY_ERR rc;
3301 struct lyd_node *target = NULL;
3302
Michal Vasko4c583e82020-07-17 12:16:14 +02003303 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003304
Michal Vasko62ed12d2020-05-21 10:08:25 +02003305 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3306 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003307 if (match) {
3308 *match = NULL;
3309 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003310 return LY_ENOTFOUND;
3311 }
3312
Michal Vaskof03ed032020-03-04 13:31:44 +01003313 if (key_or_value && !val_len) {
3314 val_len = strlen(key_or_value);
3315 }
3316
Michal Vaskob104f112020-07-17 09:54:54 +02003317 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3318 /* create a data node and find the instance */
3319 if (schema->nodetype == LYS_LEAFLIST) {
3320 /* target used attributes: schema, hash, value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003321 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, LY_PREF_JSON, NULL, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003322 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3323 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003324 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003325 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003326 }
3327
3328 /* find it */
3329 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003330 } else {
3331 /* find the first schema node instance */
3332 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003333 }
3334
Michal Vaskoe444f752020-02-10 12:20:06 +01003335 lyd_free_tree(target);
3336 return rc;
3337}
Michal Vaskoccc02342020-05-21 10:09:21 +02003338
3339API LY_ERR
3340lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3341{
3342 LY_ERR ret = LY_SUCCESS;
3343 struct lyxp_set xp_set;
3344 struct lyxp_expr *exp;
3345 uint32_t i;
3346
3347 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3348
3349 memset(&xp_set, 0, sizeof xp_set);
3350
3351 /* compile expression */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003352 exp = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003353 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3354
3355 /* evaluate expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003356 ret = lyxp_eval(exp, LY_PREF_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003357 LY_CHECK_GOTO(ret, cleanup);
3358
3359 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003360 ret = ly_set_new(set);
3361 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003362
3363 /* transform into ly_set */
3364 if (xp_set.type == LYXP_SET_NODE_SET) {
3365 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3366 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003367 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003368 (*set)->size = xp_set.used;
3369
3370 for (i = 0; i < xp_set.used; ++i) {
3371 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003372 ret = ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST, NULL);
3373 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003374 }
3375 }
3376 }
3377
3378cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003379 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003380 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003381 return ret;
3382}