blob: 115938767a584777dcd435c8166ee6d53a1fd3d1 [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);
709 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
710
711 opaq->prev = (struct lyd_node *)opaq;
712
Radek Krejci011e4aa2020-09-04 15:22:31 +0200713 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
714
Michal Vasko52927e22020-03-16 17:26:14 +0100715 opaq->format = format;
716 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 opaq->val_prefs = val_prefs;
724 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200725 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100726 *dynamic = 0;
727 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200728 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100729 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100731 opaq->ctx = ctx;
732
Radek Krejci011e4aa2020-09-04 15:22:31 +0200733finish:
734 if (ret) {
735 lyd_free_tree((struct lyd_node *)opaq);
736 } else {
737 *node = (struct lyd_node *)opaq;
738 }
739 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100740}
741
Michal Vasko3a41dff2020-07-15 14:30:28 +0200742API LY_ERR
743lyd_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 +0100744{
745 struct lyd_node *ret = NULL;
746 const struct lysc_node *schema;
747 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
748
Michal Vasko6027eb92020-07-15 16:37:30 +0200749 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100750
Michal Vaskof03ed032020-03-04 13:31:44 +0100751 if (!module) {
752 module = parent->schema->module;
753 }
754
Michal Vasko3a41dff2020-07-15 14:30:28 +0200755 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
756 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
757 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 +0100758
Michal Vasko3a41dff2020-07-15 14:30:28 +0200759 LY_CHECK_RET(lyd_create_inner(schema, &ret));
760 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100761 lyd_insert_node(parent, NULL, ret);
762 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200763
764 if (node) {
765 *node = ret;
766 }
767 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100768}
769
Michal Vasko3a41dff2020-07-15 14:30:28 +0200770API LY_ERR
771lyd_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 +0100772{
773 struct lyd_node *ret = NULL, *key;
774 const struct lysc_node *schema, *key_s;
775 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
776 va_list ap;
777 const char *key_val;
778 LY_ERR rc = LY_SUCCESS;
779
Michal Vasko6027eb92020-07-15 16:37:30 +0200780 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100781
Michal Vaskof03ed032020-03-04 13:31:44 +0100782 if (!module) {
783 module = parent->schema->module;
784 }
785
Michal Vasko013a8182020-03-03 10:46:53 +0100786 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200787 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100788
789 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200790 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100791
Michal Vasko3a41dff2020-07-15 14:30:28 +0200792 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100793
794 /* create and insert all the keys */
795 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
796 key_val = va_arg(ap, const char *);
797
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200798 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 +0200799 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
800 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100801 lyd_insert_node(ret, NULL, key);
802 }
803
Michal Vasko013a8182020-03-03 10:46:53 +0100804 if (parent) {
805 lyd_insert_node(parent, NULL, ret);
806 }
807
808cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200809 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100810 if (rc) {
811 lyd_free_tree(ret);
812 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200813 } else if (node) {
814 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100815 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200816 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100817}
818
Michal Vasko3a41dff2020-07-15 14:30:28 +0200819API LY_ERR
820lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200821 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100822{
823 struct lyd_node *ret = NULL;
824 const struct lysc_node *schema;
825 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
826
Michal Vasko6027eb92020-07-15 16:37:30 +0200827 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100828
Michal Vaskof03ed032020-03-04 13:31:44 +0100829 if (!module) {
830 module = parent->schema->module;
831 }
Michal Vasko004d3152020-06-11 19:59:22 +0200832 if (!keys) {
833 keys = "";
834 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100835
Michal Vasko004d3152020-06-11 19:59:22 +0200836 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100837 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200838 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100839
Michal Vasko004d3152020-06-11 19:59:22 +0200840 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
841 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200842 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200843 } else {
844 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200845 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200846 }
Michal Vasko004d3152020-06-11 19:59:22 +0200847 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100848 lyd_insert_node(parent, NULL, ret);
849 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200850
851 if (node) {
852 *node = ret;
853 }
854 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100855}
856
Michal Vasko3a41dff2020-07-15 14:30:28 +0200857API LY_ERR
858lyd_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 +0200859 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100860{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200861 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100862 struct lyd_node *ret = NULL;
863 const struct lysc_node *schema;
864 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
865
Michal Vasko6027eb92020-07-15 16:37:30 +0200866 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100867
Michal Vaskof03ed032020-03-04 13:31:44 +0100868 if (!module) {
869 module = parent->schema->module;
870 }
871
Michal Vasko013a8182020-03-03 10:46:53 +0100872 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200873 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100874
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200875 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 +0200876 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200877 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100878 lyd_insert_node(parent, NULL, ret);
879 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200880
881 if (node) {
882 *node = ret;
883 }
884 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100885}
886
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100888lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200889 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100890{
891 struct lyd_node *ret = NULL;
892 const struct lysc_node *schema;
893 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
894
Michal Vasko6027eb92020-07-15 16:37:30 +0200895 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100896
Michal Vaskof03ed032020-03-04 13:31:44 +0100897 if (!module) {
898 module = parent->schema->module;
899 }
900
Michal Vasko013a8182020-03-03 10:46:53 +0100901 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200902 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100903
Michal Vasko3a41dff2020-07-15 14:30:28 +0200904 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
905 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100906 lyd_insert_node(parent, NULL, ret);
907 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200908
909 if (node) {
910 *node = ret;
911 }
912 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100913}
914
Michal Vasko4490d312020-06-16 13:08:55 +0200915/**
916 * @brief Update node value.
917 *
918 * @param[in] node Node to update.
919 * @param[in] value New value to set.
920 * @param[in] value_type Type of @p value for any node.
921 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
922 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
923 * @return LY_ERR value.
924 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200925static LY_ERR
926lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200927 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200928{
929 LY_ERR ret = LY_SUCCESS;
930 struct lyd_node *new_any;
931
932 switch (node->schema->nodetype) {
933 case LYS_CONTAINER:
934 case LYS_NOTIF:
935 case LYS_RPC:
936 case LYS_ACTION:
937 case LYS_LIST:
938 case LYS_LEAFLIST:
939 /* if it exists, there is nothing to update */
940 *new_parent = NULL;
941 *new_node = NULL;
942 break;
943 case LYS_LEAF:
944 ret = lyd_change_term(node, value);
945 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
946 /* there was an actual change (at least of the default flag) */
947 *new_parent = node;
948 *new_node = node;
949 ret = LY_SUCCESS;
950 } else if (ret == LY_ENOT) {
951 /* no change */
952 *new_parent = NULL;
953 *new_node = NULL;
954 ret = LY_SUCCESS;
955 } /* else error */
956 break;
957 case LYS_ANYDATA:
958 case LYS_ANYXML:
959 /* create a new any node */
960 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
961
962 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200963 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200964 /* not equal, switch values (so that we can use generic node free) */
965 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
966 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
967 ((struct lyd_node_any *)node)->value.str = value;
968 ((struct lyd_node_any *)node)->value_type = value_type;
969
970 *new_parent = node;
971 *new_node = node;
972 } else {
973 /* they are equal */
974 *new_parent = NULL;
975 *new_node = NULL;
976 }
977 lyd_free_tree(new_any);
978 break;
979 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200980 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200981 ret = LY_EINT;
982 break;
983 }
984
985 return ret;
986}
987
Michal Vasko3a41dff2020-07-15 14:30:28 +0200988API LY_ERR
989lyd_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 +0200990 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200991{
992 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200993 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200994 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200995 size_t pref_len, name_len;
996
Michal Vasko3a41dff2020-07-15 14:30:28 +0200997 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200998
Michal Vaskob7be7a82020-08-20 09:09:04 +0200999 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +02001000
1001 /* parse the name */
1002 tmp = name;
1003 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1004 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001006 }
1007
1008 /* find the module */
1009 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001010 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001011 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 +02001012 }
1013
1014 /* set value if none */
1015 if (!val_str) {
1016 val_str = "";
1017 }
1018
Radek Krejci1798aae2020-07-14 13:26:06 +02001019 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 +02001020 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021
1022 if (meta) {
1023 *meta = ret;
1024 }
1025 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001026}
1027
Michal Vasko3a41dff2020-07-15 14:30:28 +02001028API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001029lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001030 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001031{
1032 struct lyd_node *ret = NULL;
1033
Michal Vasko6027eb92020-07-15 16:37:30 +02001034 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001035
1036 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001037 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001038 }
1039 if (!value) {
1040 value = "";
1041 }
1042
Radek Krejci1798aae2020-07-14 13:26:06 +02001043 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1044 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001045 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001046 lyd_insert_node(parent, NULL, ret);
1047 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048
1049 if (node) {
1050 *node = ret;
1051 }
1052 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001053}
1054
Michal Vasko3a41dff2020-07-15 14:30:28 +02001055API LY_ERR
1056lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001057 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001058{
Radek Krejci1798aae2020-07-14 13:26:06 +02001059 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001060 const struct ly_ctx *ctx;
1061 const char *prefix, *tmp;
1062 size_t pref_len, name_len;
1063
Michal Vasko3a41dff2020-07-15 14:30:28 +02001064 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001065
Michal Vaskob7be7a82020-08-20 09:09:04 +02001066 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001067
1068 /* parse the name */
1069 tmp = name;
1070 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1071 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001072 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001073 }
1074
1075 /* set value if none */
1076 if (!val_str) {
1077 val_str = "";
1078 }
1079
Radek Krejci1798aae2020-07-14 13:26:06 +02001080 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1081 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001082
1083 if (attr) {
1084 *attr = ret;
1085 }
1086 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001087}
1088
1089API LY_ERR
1090lyd_change_term(struct lyd_node *term, const char *val_str)
1091{
1092 LY_ERR ret = LY_SUCCESS;
1093 struct lysc_type *type;
1094 struct lyd_node_term *t;
1095 struct lyd_node *parent;
1096 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001097 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001098
1099 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1100
1101 if (!val_str) {
1102 val_str = "";
1103 }
1104 t = (struct lyd_node_term *)term;
1105 type = ((struct lysc_node_leaf *)term->schema)->type;
1106
1107 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001108 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 +02001109
1110 /* compare original and new value */
1111 if (type->plugin->compare(&t->value, &val)) {
1112 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001113 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001114 t->value = val;
1115 memset(&val, 0, sizeof val);
1116 val_change = 1;
1117 } else {
1118 val_change = 0;
1119 }
1120
1121 /* always clear the default flag */
1122 if (term->flags & LYD_DEFAULT) {
1123 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1124 parent->flags &= ~LYD_DEFAULT;
1125 }
1126 dflt_change = 1;
1127 } else {
1128 dflt_change = 0;
1129 }
1130
1131 if (val_change || dflt_change) {
1132 /* make the node non-validated */
1133 term->flags &= LYD_NEW;
1134 }
1135
1136 if (val_change) {
1137 if (term->schema->nodetype == LYS_LEAFLIST) {
1138 /* leaf-list needs to be hashed again and re-inserted into parent */
1139 lyd_unlink_hash(term);
1140 lyd_hash(term);
1141 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1142 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1143 /* list needs to be updated if its key was changed */
1144 assert(term->parent->schema->nodetype == LYS_LIST);
1145 lyd_unlink_hash((struct lyd_node *)term->parent);
1146 lyd_hash((struct lyd_node *)term->parent);
1147 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1148 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1149 }
1150
1151 /* retrun value */
1152 if (!val_change) {
1153 if (dflt_change) {
1154 /* only default flag change */
1155 ret = LY_EEXIST;
1156 } else {
1157 /* no change */
1158 ret = LY_ENOT;
1159 }
1160 } /* else value changed, LY_SUCCESS */
1161
1162cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001163 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001164 return ret;
1165}
1166
Michal Vasko41586352020-07-13 13:54:25 +02001167API LY_ERR
1168lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1169{
1170 LY_ERR ret = LY_SUCCESS;
1171 struct lyd_meta *m2;
1172 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001173 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001174
1175 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1176
1177 if (!val_str) {
1178 val_str = "";
1179 }
1180
1181 /* parse the new value into a new meta structure */
1182 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 +02001183 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001184
1185 /* compare original and new value */
1186 if (lyd_compare_meta(meta, m2)) {
1187 /* values differ, switch them */
1188 val = meta->value;
1189 meta->value = m2->value;
1190 m2->value = val;
1191 val_change = 1;
1192 } else {
1193 val_change = 0;
1194 }
1195
1196 /* retrun value */
1197 if (!val_change) {
1198 /* no change */
1199 ret = LY_ENOT;
1200 } /* else value changed, LY_SUCCESS */
1201
1202cleanup:
1203 return ret;
1204}
1205
Michal Vasko3a41dff2020-07-15 14:30:28 +02001206API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001207lyd_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 +02001208 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001209{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001210 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001211}
1212
1213API LY_ERR
1214lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001215 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 +02001216{
1217 LY_ERR ret = LY_SUCCESS, r;
1218 struct lyxp_expr *exp = NULL;
1219 struct ly_path *p = NULL;
1220 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1221 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001222 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001223 struct ly_path_predicate *pred;
1224
1225 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1226
1227 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001228 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001229 }
1230
1231 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001232 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 +02001233 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1234
1235 /* compile path */
1236 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1237 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001238 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001239
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001240 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1241 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001242 && !(options & LYD_NEWOPT_OPAQ)) {
1243 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1244 lys_nodetype2str(schema->nodetype), schema->name);
1245 ret = LY_EINVAL;
1246 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001247 } 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 +02001248 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001249 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1250 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001251
1252 if (!value) {
1253 value = "";
1254 }
1255
1256 r = LY_SUCCESS;
1257 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001258 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001259 }
1260 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001261 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 +02001262 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1263 }
1264
1265 /* try to find any existing nodes in the path */
1266 if (parent) {
1267 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1268 if (ret == LY_SUCCESS) {
1269 /* the node exists, are we supposed to update it or is it just a default? */
1270 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1271 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1272 ret = LY_EEXIST;
1273 goto cleanup;
1274 }
1275
1276 /* update the existing node */
1277 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1278 goto cleanup;
1279 } else if (ret == LY_EINCOMPLETE) {
1280 /* some nodes were found, adjust the iterator to the next segment */
1281 ++path_idx;
1282 } else if (ret == LY_ENOTFOUND) {
1283 /* 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 +02001284 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001285 node = parent;
1286 }
1287 } else {
1288 /* error */
1289 goto cleanup;
1290 }
1291 }
1292
1293 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001294 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001295 cur_parent = node;
1296 schema = p[path_idx].node;
1297
1298 switch (schema->nodetype) {
1299 case LYS_LIST:
1300 if (!(schema->flags & LYS_KEYLESS)) {
1301 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1302 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001303 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1304 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1305 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001306 } else {
1307 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1308 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1309 }
1310 break;
1311 }
Radek Krejci0f969882020-08-21 16:56:47 +02001312 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001313 case LYS_CONTAINER:
1314 case LYS_NOTIF:
1315 case LYS_RPC:
1316 case LYS_ACTION:
1317 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1318 break;
1319 case LYS_LEAFLIST:
1320 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1321 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001322 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1323 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1324 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001325 } else {
1326 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1327 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1328 }
1329 break;
1330 case LYS_LEAF:
1331 /* make there is some value */
1332 if (!value) {
1333 value = "";
1334 }
1335
1336 r = LY_SUCCESS;
1337 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001338 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001339 }
1340 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001341 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 +02001342 } else {
1343 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001344 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001345 LYD_JSON, NULL, NULL, 0, schema->module->name,
1346 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001347 }
1348 break;
1349 case LYS_ANYDATA:
1350 case LYS_ANYXML:
1351 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1352 break;
1353 default:
1354 LOGINT(ctx);
1355 ret = LY_EINT;
1356 goto cleanup;
1357 }
1358
1359 if (cur_parent) {
1360 /* connect to the parent */
1361 lyd_insert_node(cur_parent, NULL, node);
1362 } else if (parent) {
1363 /* connect to top-level siblings */
1364 lyd_insert_node(NULL, &parent, node);
1365 }
1366
1367 /* update remembered nodes */
1368 if (!nparent) {
1369 nparent = node;
1370 }
1371 nnode = node;
1372 }
1373
1374cleanup:
1375 lyxp_expr_free(ctx, exp);
1376 ly_path_free(ctx, p);
1377 if (!ret) {
1378 /* set out params only on success */
1379 if (new_parent) {
1380 *new_parent = nparent;
1381 }
1382 if (new_node) {
1383 *new_node = nnode;
1384 }
1385 }
1386 return ret;
1387}
1388
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001389LY_ERR
1390lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001391 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 +02001392 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001393{
1394 LY_ERR ret;
1395 const struct lysc_node *iter = NULL;
1396 struct lyd_node *node;
1397 struct lyd_value **dflts;
1398 LY_ARRAY_COUNT_TYPE u;
1399
1400 assert(first && (parent || sparent || mod));
1401
1402 if (!sparent && parent) {
1403 sparent = parent->schema;
1404 }
1405
1406 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1407 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1408 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001409 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1410 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001411 }
1412
1413 switch (iter->nodetype) {
1414 case LYS_CHOICE:
1415 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1416 /* create default case data */
1417 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1418 NULL, node_types, node_when, impl_opts, diff));
1419 }
1420 break;
1421 case LYS_CONTAINER:
1422 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1423 /* create default NP container */
1424 LY_CHECK_RET(lyd_create_inner(iter, &node));
1425 node->flags = LYD_DEFAULT;
1426 lyd_insert_node(parent, first, node);
1427
1428 /* cannot be a NP container with when */
1429 assert(!iter->when);
1430
1431 /* create any default children */
1432 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1433 impl_opts, diff));
1434 }
1435 break;
1436 case LYS_LEAF:
1437 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1438 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1439 /* create default leaf */
1440 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1441 if (ret == LY_EINCOMPLETE) {
1442 if (node_types) {
1443 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001444 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001445 }
1446 } else if (ret) {
1447 return ret;
1448 }
1449 node->flags = LYD_DEFAULT;
1450 lyd_insert_node(parent, first, node);
1451
1452 if (iter->when && node_when) {
1453 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001454 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001455 }
1456 if (diff) {
1457 /* add into diff */
1458 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1459 }
1460 }
1461 break;
1462 case LYS_LEAFLIST:
1463 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1464 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1465 /* create all default leaf-lists */
1466 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1467 LY_ARRAY_FOR(dflts, u) {
1468 ret = lyd_create_term2(iter, dflts[u], &node);
1469 if (ret == LY_EINCOMPLETE) {
1470 if (node_types) {
1471 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001472 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001473 }
1474 } else if (ret) {
1475 return ret;
1476 }
1477 node->flags = LYD_DEFAULT;
1478 lyd_insert_node(parent, first, node);
1479
1480 if (iter->when && node_when) {
1481 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001482 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001483 }
1484 if (diff) {
1485 /* add into diff */
1486 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1487 }
1488 }
1489 }
1490 break;
1491 default:
1492 /* without defaults */
1493 break;
1494 }
1495 }
1496
1497 return LY_SUCCESS;
1498}
1499
1500API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001501lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001502{
Michal Vasko56daf732020-08-10 10:57:18 +02001503 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001504 LY_ERR ret = LY_SUCCESS;
1505
1506 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1507 if (diff) {
1508 *diff = NULL;
1509 }
1510
Michal Vasko56daf732020-08-10 10:57:18 +02001511 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001512 /* skip added default nodes */
1513 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1514 && (node->schema->nodetype & LYD_NODE_INNER)) {
1515 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1516 NULL, implicit_options, diff), cleanup);
1517 }
1518
Michal Vasko56daf732020-08-10 10:57:18 +02001519 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001520 }
1521
1522cleanup:
1523 if (ret && diff) {
1524 lyd_free_all(*diff);
1525 *diff = NULL;
1526 }
1527 return ret;
1528}
1529
1530API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001531lyd_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 +02001532{
1533 const struct lys_module *mod;
1534 struct lyd_node *d = NULL;
1535 uint32_t i = 0;
1536 LY_ERR ret = LY_SUCCESS;
1537
1538 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1539 if (diff) {
1540 *diff = NULL;
1541 }
1542 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001543 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001544 }
1545
1546 /* add nodes for each module one-by-one */
1547 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1548 if (!mod->implemented) {
1549 continue;
1550 }
1551
1552 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1553 if (d) {
1554 /* merge into one diff */
1555 lyd_insert_sibling(*diff, d, diff);
1556
1557 d = NULL;
1558 }
1559 }
1560
1561cleanup:
1562 if (ret && diff) {
1563 lyd_free_all(*diff);
1564 *diff = NULL;
1565 }
1566 return ret;
1567}
1568
1569API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001570lyd_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 +02001571{
1572 struct lyd_node *root, *d = NULL;
1573 LY_ERR ret = LY_SUCCESS;
1574
1575 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1576 if (diff) {
1577 *diff = NULL;
1578 }
1579
1580 /* add all top-level defaults for this module */
1581 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1582
1583 /* process nested nodes */
1584 LY_LIST_FOR(*tree, root) {
1585 /* skip added default nodes */
1586 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1587 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1588
1589 if (d) {
1590 /* merge into one diff */
1591 lyd_insert_sibling(*diff, d, diff);
1592
1593 d = NULL;
1594 }
1595 }
1596 }
1597
1598cleanup:
1599 if (ret && diff) {
1600 lyd_free_all(*diff);
1601 *diff = NULL;
1602 }
1603 return ret;
1604}
1605
Michal Vasko90932a92020-02-12 14:33:03 +01001606struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001607lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001608{
Michal Vaskob104f112020-07-17 09:54:54 +02001609 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001610 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001611 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001612
Michal Vaskob104f112020-07-17 09:54:54 +02001613 assert(new_node);
1614
1615 if (!first_sibling || !new_node->schema) {
1616 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001617 return NULL;
1618 }
1619
Michal Vaskob104f112020-07-17 09:54:54 +02001620 if (first_sibling->parent && first_sibling->parent->children_ht) {
1621 /* find the anchor using hashes */
1622 sparent = first_sibling->parent->schema;
1623 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1624 while (schema) {
1625 /* keep trying to find the first existing instance of the closest following schema sibling,
1626 * otherwise return NULL - inserting at the end */
1627 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1628 break;
1629 }
1630
1631 schema = lys_getnext(schema, sparent, NULL, 0);
1632 }
1633 } else {
1634 /* find the anchor without hashes */
1635 match = (struct lyd_node *)first_sibling;
1636 if (!lysc_data_parent(new_node->schema)) {
1637 /* we are in top-level, skip all the data from preceding modules */
1638 LY_LIST_FOR(match, match) {
1639 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1640 break;
1641 }
1642 }
1643 }
1644
1645 /* get the first schema sibling */
1646 sparent = lysc_data_parent(new_node->schema);
1647 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1648
1649 found = 0;
1650 LY_LIST_FOR(match, match) {
1651 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1652 /* we have found an opaque node, which must be at the end, so use it OR
1653 * modules do not match, so we must have traversed all the data from new_node module (if any),
1654 * we have found the first node of the next module, that is what we want */
1655 break;
1656 }
1657
1658 /* skip schema nodes until we find the instantiated one */
1659 while (!found) {
1660 if (new_node->schema == schema) {
1661 /* we have found the schema of the new node, continue search to find the first
1662 * data node with a different schema (after our schema) */
1663 found = 1;
1664 break;
1665 }
1666 if (match->schema == schema) {
1667 /* current node (match) is a data node still before the new node, continue search in data */
1668 break;
1669 }
1670 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1671 assert(schema);
1672 }
1673
1674 if (found && (match->schema != new_node->schema)) {
1675 /* find the next node after we have found our node schema data instance */
1676 break;
1677 }
1678 }
Michal Vasko90932a92020-02-12 14:33:03 +01001679 }
1680
1681 return match;
1682}
1683
1684/**
1685 * @brief Insert node after a sibling.
1686 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001687 * Handles inserting into NP containers and key-less lists.
1688 *
Michal Vasko90932a92020-02-12 14:33:03 +01001689 * @param[in] sibling Sibling to insert after.
1690 * @param[in] node Node to insert.
1691 */
1692static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001693lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001694{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001695 struct lyd_node_inner *par;
1696
Michal Vasko90932a92020-02-12 14:33:03 +01001697 assert(!node->next && (node->prev == node));
1698
1699 node->next = sibling->next;
1700 node->prev = sibling;
1701 sibling->next = node;
1702 if (node->next) {
1703 /* sibling had a succeeding node */
1704 node->next->prev = node;
1705 } else {
1706 /* sibling was last, find first sibling and change its prev */
1707 if (sibling->parent) {
1708 sibling = sibling->parent->child;
1709 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001710 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001711 }
1712 sibling->prev = node;
1713 }
1714 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001715
Michal Vasko9f96a052020-03-10 09:41:45 +01001716 for (par = node->parent; par; par = par->parent) {
1717 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1718 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001719 par->flags &= ~LYD_DEFAULT;
1720 }
Michal Vaskob104f112020-07-17 09:54:54 +02001721 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001722 /* rehash key-less list */
1723 lyd_hash((struct lyd_node *)par);
1724 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001725 }
Michal Vasko90932a92020-02-12 14:33:03 +01001726}
1727
1728/**
1729 * @brief Insert node before a sibling.
1730 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001731 * Handles inserting into NP containers and key-less lists.
1732 *
Michal Vasko90932a92020-02-12 14:33:03 +01001733 * @param[in] sibling Sibling to insert before.
1734 * @param[in] node Node to insert.
1735 */
1736static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001737lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001738{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001739 struct lyd_node_inner *par;
1740
Michal Vasko90932a92020-02-12 14:33:03 +01001741 assert(!node->next && (node->prev == node));
1742
1743 node->next = sibling;
1744 /* covers situation of sibling being first */
1745 node->prev = sibling->prev;
1746 sibling->prev = node;
1747 if (node->prev->next) {
1748 /* sibling had a preceding node */
1749 node->prev->next = node;
1750 } else if (sibling->parent) {
1751 /* sibling was first and we must also change parent child pointer */
1752 sibling->parent->child = node;
1753 }
1754 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001755
Michal Vasko9f96a052020-03-10 09:41:45 +01001756 for (par = node->parent; par; par = par->parent) {
1757 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1758 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001759 par->flags &= ~LYD_DEFAULT;
1760 }
Michal Vaskob104f112020-07-17 09:54:54 +02001761 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001762 /* rehash key-less list */
1763 lyd_hash((struct lyd_node *)par);
1764 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001765 }
Michal Vasko90932a92020-02-12 14:33:03 +01001766}
1767
1768/**
Michal Vaskob104f112020-07-17 09:54:54 +02001769 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001770 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001771 * Handles inserting into NP containers and key-less lists.
1772 *
Michal Vasko90932a92020-02-12 14:33:03 +01001773 * @param[in] parent Parent to insert into.
1774 * @param[in] node Node to insert.
1775 */
1776static void
Michal Vaskob104f112020-07-17 09:54:54 +02001777lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001778{
1779 struct lyd_node_inner *par;
1780
Michal Vaskob104f112020-07-17 09:54:54 +02001781 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001782 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001783
1784 par = (struct lyd_node_inner *)parent;
1785
Michal Vaskob104f112020-07-17 09:54:54 +02001786 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001787 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001788
Michal Vaskod989ba02020-08-24 10:59:24 +02001789 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001790 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1791 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001792 par->flags &= ~LYD_DEFAULT;
1793 }
Michal Vasko52927e22020-03-16 17:26:14 +01001794 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001795 /* rehash key-less list */
1796 lyd_hash((struct lyd_node *)par);
1797 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001798 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001799}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001800
Michal Vasko751cb4d2020-07-14 12:25:28 +02001801/**
1802 * @brief Learn whether a list instance has all the keys.
1803 *
1804 * @param[in] list List instance to check.
1805 * @return non-zero if all the keys were found,
1806 * @return 0 otherwise.
1807 */
1808static int
1809lyd_insert_has_keys(const struct lyd_node *list)
1810{
1811 const struct lyd_node *key;
1812 const struct lysc_node *skey = NULL;
1813
1814 assert(list->schema->nodetype == LYS_LIST);
1815 key = lyd_node_children(list, 0);
1816 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1817 if (!key || (key->schema != skey)) {
1818 /* key missing */
1819 return 0;
1820 }
1821
1822 key = key->next;
1823 }
1824
1825 /* all keys found */
1826 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001827}
1828
1829void
Michal Vaskob104f112020-07-17 09:54:54 +02001830lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001831{
Michal Vaskob104f112020-07-17 09:54:54 +02001832 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001833
Michal Vaskob104f112020-07-17 09:54:54 +02001834 /* inserting list without its keys is not supported */
1835 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001836
Michal Vaskob104f112020-07-17 09:54:54 +02001837 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1838 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001839 }
Michal Vasko90932a92020-02-12 14:33:03 +01001840
Michal Vaskob104f112020-07-17 09:54:54 +02001841 /* get first sibling */
1842 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001843
Michal Vaskob104f112020-07-17 09:54:54 +02001844 /* find the anchor, our next node, so we can insert before it */
1845 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1846 if (anchor) {
1847 lyd_insert_before_node(anchor, node);
1848 } else if (first_sibling) {
1849 lyd_insert_after_node(first_sibling->prev, node);
1850 } else if (parent) {
1851 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001852 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001853 *first_sibling_p = node;
1854 }
1855
1856 /* insert into parent HT */
1857 lyd_insert_hash(node);
1858
1859 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001860 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001861 lyd_hash(parent);
1862
1863 /* now we can insert even the list into its parent HT */
1864 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001865 }
Michal Vasko90932a92020-02-12 14:33:03 +01001866}
1867
Michal Vaskof03ed032020-03-04 13:31:44 +01001868static LY_ERR
1869lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1870{
1871 const struct lysc_node *par2;
1872
1873 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001874 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001875
1876 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001877 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001878
1879 if (parent) {
1880 /* inner node */
1881 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001882 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1883 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001884 return LY_EINVAL;
1885 }
1886 } else {
1887 /* top-level node */
1888 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001889 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001890 return LY_EINVAL;
1891 }
1892 }
1893
1894 return LY_SUCCESS;
1895}
1896
1897API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001898lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001899{
1900 struct lyd_node *iter;
1901
Michal Vasko654bc852020-06-23 13:28:06 +02001902 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001903
1904 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1905
1906 if (node->schema->flags & LYS_KEY) {
1907 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1908 return LY_EINVAL;
1909 }
1910
1911 if (node->parent || node->prev->next) {
1912 lyd_unlink_tree(node);
1913 }
1914
1915 while (node) {
1916 iter = node->next;
1917 lyd_unlink_tree(node);
1918 lyd_insert_node(parent, NULL, node);
1919 node = iter;
1920 }
1921 return LY_SUCCESS;
1922}
1923
1924API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001925lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001926{
1927 struct lyd_node *iter;
1928
Michal Vaskob104f112020-07-17 09:54:54 +02001929 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001930
Michal Vaskob104f112020-07-17 09:54:54 +02001931 if (sibling) {
1932 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001933 }
1934
1935 if (node->parent || node->prev->next) {
1936 lyd_unlink_tree(node);
1937 }
1938
1939 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001940 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001941 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001942 return LY_EINVAL;
1943 }
1944
Michal Vaskob1b5c262020-03-05 14:29:47 +01001945 iter = node->next;
1946 lyd_unlink_tree(node);
1947 lyd_insert_node(NULL, &sibling, node);
1948 node = iter;
1949 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001950
Michal Vaskob104f112020-07-17 09:54:54 +02001951 if (first) {
1952 /* find the first sibling */
1953 *first = sibling;
1954 while ((*first)->prev->next) {
1955 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001956 }
1957 }
1958
1959 return LY_SUCCESS;
1960}
1961
Michal Vaskob1b5c262020-03-05 14:29:47 +01001962API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001963lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1964{
1965 struct lyd_node *iter;
1966
1967 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1968
Michal Vasko62ed12d2020-05-21 10:08:25 +02001969 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001970
Michal Vaskob104f112020-07-17 09:54:54 +02001971 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001972 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001973 return LY_EINVAL;
1974 }
1975
1976 if (node->parent || node->prev->next) {
1977 lyd_unlink_tree(node);
1978 }
1979
1980 /* insert in reverse order to get the original order */
1981 node = node->prev;
1982 while (node) {
1983 iter = node->prev;
1984 lyd_unlink_tree(node);
1985
1986 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001987 lyd_insert_hash(node);
1988
Michal Vaskof03ed032020-03-04 13:31:44 +01001989 /* move the anchor accordingly */
1990 sibling = node;
1991
1992 node = (iter == node) ? NULL : iter;
1993 }
1994 return LY_SUCCESS;
1995}
1996
1997API LY_ERR
1998lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1999{
2000 struct lyd_node *iter;
2001
2002 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2003
Michal Vasko62ed12d2020-05-21 10:08:25 +02002004 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002005
Michal Vaskob104f112020-07-17 09:54:54 +02002006 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002007 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002008 return LY_EINVAL;
2009 }
2010
2011 if (node->parent || node->prev->next) {
2012 lyd_unlink_tree(node);
2013 }
2014
2015 while (node) {
2016 iter = node->next;
2017 lyd_unlink_tree(node);
2018
2019 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002020 lyd_insert_hash(node);
2021
Michal Vaskof03ed032020-03-04 13:31:44 +01002022 /* move the anchor accordingly */
2023 sibling = node;
2024
2025 node = iter;
2026 }
2027 return LY_SUCCESS;
2028}
2029
2030API void
2031lyd_unlink_tree(struct lyd_node *node)
2032{
2033 struct lyd_node *iter;
2034
2035 if (!node) {
2036 return;
2037 }
2038
Michal Vaskob104f112020-07-17 09:54:54 +02002039 /* update hashes while still linked into the tree */
2040 lyd_unlink_hash(node);
2041
Michal Vaskof03ed032020-03-04 13:31:44 +01002042 /* unlink from siblings */
2043 if (node->prev->next) {
2044 node->prev->next = node->next;
2045 }
2046 if (node->next) {
2047 node->next->prev = node->prev;
2048 } else {
2049 /* unlinking the last node */
2050 if (node->parent) {
2051 iter = node->parent->child;
2052 } else {
2053 iter = node->prev;
2054 while (iter->prev != node) {
2055 iter = iter->prev;
2056 }
2057 }
2058 /* update the "last" pointer from the first node */
2059 iter->prev = node->prev;
2060 }
2061
2062 /* unlink from parent */
2063 if (node->parent) {
2064 if (node->parent->child == node) {
2065 /* the node is the first child */
2066 node->parent->child = node->next;
2067 }
2068
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002069 /* check for NP container whether its last non-default node is not being unlinked */
2070 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2071 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2072 LY_LIST_FOR(node->parent->child, iter) {
2073 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2074 break;
2075 }
2076 }
2077 if (!iter) {
2078 node->parent->flags |= LYD_DEFAULT;
2079 }
2080 }
2081
Michal Vaskof03ed032020-03-04 13:31:44 +01002082 /* check for keyless list and update its hash */
2083 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002084 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002085 lyd_hash(iter);
2086 }
2087 }
2088
2089 node->parent = NULL;
2090 }
2091
2092 node->next = NULL;
2093 node->prev = node;
2094}
2095
Michal Vaskoa5da3292020-08-12 13:10:50 +02002096void
Radek Krejci1798aae2020-07-14 13:26:06 +02002097lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2098{
2099 struct lyd_meta *last, *iter;
2100
2101 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002102
2103 if (!meta) {
2104 return;
2105 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002106
2107 for (iter = meta; iter; iter = iter->next) {
2108 iter->parent = parent;
2109 }
2110
2111 /* insert as the last attribute */
2112 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002113 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002114 last->next = meta;
2115 } else {
2116 parent->meta = meta;
2117 }
2118
2119 /* remove default flags from NP containers */
2120 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2121 parent->flags &= ~LYD_DEFAULT;
2122 parent = (struct lyd_node *)parent->parent;
2123 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002124}
2125
2126LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002127lyd_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 +02002128 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 +02002129 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002130{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002131 LY_ERR ret, rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002132 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002133 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002134 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002135
Michal Vasko9f96a052020-03-10 09:41:45 +01002136 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002137
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002138 LY_ARRAY_FOR(mod->compiled->exts, u) {
2139 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2140 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002141 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002142 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002143 break;
2144 }
2145 }
2146 if (!ant) {
2147 /* attribute is not defined as a metadata annotation (RFC 7952) */
2148 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2149 mod->name, name_len, name);
2150 return LY_EINVAL;
2151 }
2152
Michal Vasko9f96a052020-03-10 09:41:45 +01002153 mt = calloc(1, sizeof *mt);
2154 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2155 mt->parent = parent;
2156 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002157 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 +01002158 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002159 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002160 return ret;
2161 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002162 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2163 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002164
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002165 /* insert as the last attribute */
2166 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002167 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002168 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002169 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002170 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002171 }
2172
Michal Vasko9f96a052020-03-10 09:41:45 +01002173 if (meta) {
2174 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002175 }
2176 return ret;
2177}
2178
Michal Vaskoa5da3292020-08-12 13:10:50 +02002179void
2180lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2181{
2182 struct lyd_attr *last, *iter;
2183 struct lyd_node_opaq *opaq;
2184
2185 assert(parent && !parent->schema);
2186
2187 if (!attr) {
2188 return;
2189 }
2190
2191 opaq = (struct lyd_node_opaq *)parent;
2192 for (iter = attr; iter; iter = iter->next) {
2193 iter->parent = opaq;
2194 }
2195
2196 /* insert as the last attribute */
2197 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002198 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002199 last->next = attr;
2200 } else {
2201 opaq->attr = attr;
2202 }
2203}
2204
Michal Vasko52927e22020-03-16 17:26:14 +01002205LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002206lyd_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 +02002207 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 +02002208 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 +01002209{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002210 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002211 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002212
2213 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2214 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002215
2216 if (!value_len) {
2217 value = "";
2218 }
2219
2220 at = calloc(1, sizeof *at);
2221 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002222 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002223 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002224 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2225 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002226 *dynamic = 0;
2227 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002228 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002229 }
2230
Radek Krejci1798aae2020-07-14 13:26:06 +02002231 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002232 at->format = format;
2233 at->val_prefs = val_prefs;
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}