blob: 6514520398cbd07ba188edddd278a0b8b9c98836 [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 Krejci19611252020-10-04 13:54:53 +0200276API ly_bool
277lyd_is_default(const struct lyd_node *node)
278{
279 const struct lysc_node_leaf *leaf;
280 const struct lysc_node_leaflist *llist;
281 const struct lyd_node_term *term;
282 LY_ARRAY_COUNT_TYPE u;
283
284 assert(node->schema->nodetype & LYD_NODE_TERM);
285 term = (const struct lyd_node_term *)node;
286
287 if (node->schema->nodetype == LYS_LEAF) {
288 leaf = (const struct lysc_node_leaf *)node->schema;
289 if (!leaf->dflt) {
290 return 0;
291 }
292
293 /* compare with the default value */
294 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
295 return 0;
296 }
297 } else {
298 llist = (const struct lysc_node_leaflist *)node->schema;
299 if (!llist->dflts) {
300 return 0;
301 }
302
303 LY_ARRAY_FOR(llist->dflts, u) {
304 /* compare with each possible default value */
305 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
306 return 0;
307 }
308 }
309 }
310
311 return 1;
312}
313
Radek Krejci7931b192020-06-25 17:05:03 +0200314static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200315lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200316{
Radek Krejcie7b95092019-05-15 11:03:07 +0200317
Radek Krejci7931b192020-06-25 17:05:03 +0200318 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200320 const char *path = in->method.fpath.filepath;
321 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200322
323 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200324 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200325
326 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
327 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
329 format = LYD_JSON;
330 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
331 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200332 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200333 }
334
Radek Krejci7931b192020-06-25 17:05:03 +0200335 return format;
336}
Radek Krejcie7b95092019-05-15 11:03:07 +0200337
Radek Krejci7931b192020-06-25 17:05:03 +0200338API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200339lyd_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 +0200340 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200341{
Radek Krejci1798aae2020-07-14 13:26:06 +0200342 LY_ERR ret = LY_SUCCESS;
343 struct lyd_ctx *lydctx = NULL;
344
Radek Krejci7931b192020-06-25 17:05:03 +0200345 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
346 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
347 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
348
349 format = lyd_parse_get_format(in, format);
350 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
351
Radek Krejci1798aae2020-07-14 13:26:06 +0200352 /* init */
353 *tree = NULL;
354
Michal Vasko63f3d842020-07-08 10:10:14 +0200355 /* remember input position */
356 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200357
358 switch (format) {
359 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200360 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
361 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200362 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200363 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
364 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200365 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
367 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200368 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200369 LOGINT_RET(ctx);
370 }
371
Radek Krejci1798aae2020-07-14 13:26:06 +0200372 if (!(parse_options & LYD_PARSE_ONLY)) {
373 uint32_t i = 0;
374 const struct lys_module *mod;
375 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200376
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 next = *tree;
378 while (1) {
379 if (validate_options & LYD_VALIDATE_PRESENT) {
380 mod = lyd_data_next_module(&next, &first);
381 } else {
382 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
383 }
384 if (!mod) {
385 break;
386 }
387 if (first == *tree) {
388 /* make sure first2 changes are carried to tree */
389 first2 = tree;
390 } else {
391 first2 = &first;
392 }
393
394 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
395 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
396
397 /* add all top-level defaults for this module */
398 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
Radek Krejci0f969882020-08-21 16:56:47 +0200399 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 LY_CHECK_GOTO(ret, cleanup);
401
402 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200403 switch (format) {
404 case LYD_XML:
405 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
406 LY_PREF_XML, &((struct lyxml_ctx *)lydctx->data_ctx)->ns, NULL);
407 break;
408 case LYD_JSON:
409 case LYD_LYB:
410 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
411 LY_PREF_JSON, NULL, NULL);
412 break;
413 case LYD_UNKNOWN:
414 LOGINT_RET(ctx);
415 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200416 LY_CHECK_GOTO(ret, cleanup);
417
418 /* perform final validation that assumes the data tree is final */
419 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
420 }
421 }
422
423cleanup:
424 lydctx->free((struct lyd_ctx *)lydctx);
425 if (ret) {
426 lyd_free_all(*tree);
427 *tree = NULL;
428 }
429 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200430}
431
432API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200433lyd_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 +0200434 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200435{
436 LY_ERR ret;
437 struct ly_in *in;
438
439 LY_CHECK_RET(ly_in_new_memory(data, &in));
440 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
441
442 ly_in_free(in, 0);
443 return ret;
444}
445
446API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200447lyd_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 +0200448 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200449{
450 LY_ERR ret;
451 struct ly_in *in;
452
453 LY_CHECK_RET(ly_in_new_fd(fd, &in));
454 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
455
456 ly_in_free(in, 0);
457 return ret;
458}
459
460API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200461lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
462 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200463{
464 LY_ERR ret;
465 struct ly_in *in;
466
467 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
468 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
469
470 ly_in_free(in, 0);
471 return ret;
472}
473
Radek Krejci7931b192020-06-25 17:05:03 +0200474API LY_ERR
475lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
476{
477 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
478
479 format = lyd_parse_get_format(in, format);
480 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
481
Radek Krejci1798aae2020-07-14 13:26:06 +0200482 /* init */
483 *tree = NULL;
484 if (op) {
485 *op = NULL;
486 }
487
Michal Vasko63f3d842020-07-08 10:10:14 +0200488 /* remember input position */
489 in->func_start = in->current;
490
Radek Krejci7931b192020-06-25 17:05:03 +0200491 switch (format) {
492 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200494 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200496 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200497 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200498 case LYD_UNKNOWN:
499 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200500 }
501
502 LOGINT_RET(ctx);
503}
504
505API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200506lyd_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 +0200507 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200508{
509 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200510 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200511
512 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200513 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200514
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 /* init */
516 if (tree) {
517 *tree = NULL;
518 }
519 if (op) {
520 *op = NULL;
521 }
522
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 /* remember input position */
524 in->func_start = in->current;
525
Radek Krejci7931b192020-06-25 17:05:03 +0200526 switch (format) {
527 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200529 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200530 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200531 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200532 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200533 case LYD_UNKNOWN:
534 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200535 }
536
Michal Vaskob7be7a82020-08-20 09:09:04 +0200537 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200538}
539
540API LY_ERR
541lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
542{
Radek Krejci1798aae2020-07-14 13:26:06 +0200543 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200544
545 format = lyd_parse_get_format(in, format);
546 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
547
Radek Krejci1798aae2020-07-14 13:26:06 +0200548 /* init */
549 if (tree) {
550 *tree = NULL;
551 }
552 if (ntf) {
553 *ntf = NULL;
554 }
555
Michal Vasko63f3d842020-07-08 10:10:14 +0200556 /* remember input position */
557 in->func_start = in->current;
558
Radek Krejci7931b192020-06-25 17:05:03 +0200559 switch (format) {
560 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200561 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200562 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200564 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200565 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200566 case LYD_UNKNOWN:
567 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200568 }
569
570 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200571}
Radek Krejci084289f2019-07-09 17:35:30 +0200572
Michal Vasko90932a92020-02-12 14:33:03 +0100573LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200574lyd_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 +0200575 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100576{
577 LY_ERR ret;
578 struct lyd_node_term *term;
579
Michal Vasko9b368d32020-02-14 13:53:31 +0100580 assert(schema->nodetype & LYD_NODE_TERM);
581
Michal Vasko90932a92020-02-12 14:33:03 +0100582 term = calloc(1, sizeof *term);
583 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
584
585 term->schema = schema;
586 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100587 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100588
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200589 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, format, prefix_data, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100590 if (ret && (ret != LY_EINCOMPLETE)) {
591 free(term);
592 return ret;
593 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100594 lyd_hash((struct lyd_node *)term);
595
596 *node = (struct lyd_node *)term;
597 return ret;
598}
599
600LY_ERR
601lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
602{
603 LY_ERR ret;
604 struct lyd_node_term *term;
605 struct lysc_type *type;
606
607 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200608 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100609
610 term = calloc(1, sizeof *term);
611 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
612
613 term->schema = schema;
614 term->prev = (struct lyd_node *)term;
615 term->flags = LYD_NEW;
616
617 type = ((struct lysc_node_leaf *)schema)->type;
618 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
619 if (ret) {
620 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
621 free(term);
622 return ret;
623 }
624 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100625
626 *node = (struct lyd_node *)term;
627 return ret;
628}
629
630LY_ERR
631lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
632{
633 struct lyd_node_inner *in;
634
Michal Vasko9b368d32020-02-14 13:53:31 +0100635 assert(schema->nodetype & LYD_NODE_INNER);
636
Michal Vasko90932a92020-02-12 14:33:03 +0100637 in = calloc(1, sizeof *in);
638 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
639
640 in->schema = schema;
641 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100642 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100643
Michal Vasko9b368d32020-02-14 13:53:31 +0100644 /* do not hash list with keys, we need them for the hash */
645 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
646 lyd_hash((struct lyd_node *)in);
647 }
Michal Vasko90932a92020-02-12 14:33:03 +0100648
649 *node = (struct lyd_node *)in;
650 return LY_SUCCESS;
651}
652
Michal Vasko90932a92020-02-12 14:33:03 +0100653LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200654lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100655{
656 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100657 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200658 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100659
Michal Vasko004d3152020-06-11 19:59:22 +0200660 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100661
662 /* create list */
663 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
664
Michal Vasko90932a92020-02-12 14:33:03 +0100665 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200666 LY_ARRAY_FOR(predicates, u) {
667 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100668 lyd_insert_node(list, NULL, key);
669 }
670
Michal Vasko9b368d32020-02-14 13:53:31 +0100671 /* hash having all the keys */
672 lyd_hash(list);
673
Michal Vasko90932a92020-02-12 14:33:03 +0100674 /* success */
675 *node = list;
676 list = NULL;
677
678cleanup:
679 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200680 return ret;
681}
682
683static LY_ERR
684lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
685{
686 LY_ERR ret = LY_SUCCESS;
687 struct lyxp_expr *expr = NULL;
688 uint16_t exp_idx = 0;
689 enum ly_path_pred_type pred_type = 0;
690 struct ly_path_predicate *predicates = NULL;
691
692 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200693 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 +0200694 LY_PATH_PRED_KEYS, &expr), cleanup);
695
696 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200697 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200698 LY_PREF_JSON, NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200699
700 /* create the list node */
701 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
702
703cleanup:
704 lyxp_expr_free(schema->module->ctx, expr);
705 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100706 return ret;
707}
708
709LY_ERR
710lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
711{
712 struct lyd_node_any *any;
713
Michal Vasko9b368d32020-02-14 13:53:31 +0100714 assert(schema->nodetype & LYD_NODE_ANY);
715
Michal Vasko90932a92020-02-12 14:33:03 +0100716 any = calloc(1, sizeof *any);
717 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
718
719 any->schema = schema;
720 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100721 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100722
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 /* TODO: convert XML/JSON strings into a opaq data tree */
724 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100725 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100726 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100727
728 *node = (struct lyd_node *)any;
729 return LY_SUCCESS;
730}
731
Michal Vasko52927e22020-03-16 17:26:14 +0100732LY_ERR
733lyd_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 +0200734 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 +0200735 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100736{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200737 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100738 struct lyd_node_opaq *opaq;
739
Radek Krejci1798aae2020-07-14 13:26:06 +0200740 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100741
742 if (!value_len) {
743 value = "";
744 }
745
746 opaq = calloc(1, sizeof *opaq);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200747 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vasko52927e22020-03-16 17:26:14 +0100748
749 opaq->prev = (struct lyd_node *)opaq;
Radek Krejcid46e46a2020-09-15 14:22:42 +0200750 opaq->val_prefs = val_prefs;
751 opaq->format = format;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200752 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
753
Michal Vasko52927e22020-03-16 17:26:14 +0100754 if (pref_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200755 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100756 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200758 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 }
760
Michal Vasko52927e22020-03-16 17:26:14 +0100761 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200762 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100763 *dynamic = 0;
764 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200765 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100766 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200767 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100768 opaq->ctx = ctx;
769
Radek Krejci011e4aa2020-09-04 15:22:31 +0200770finish:
771 if (ret) {
772 lyd_free_tree((struct lyd_node *)opaq);
773 } else {
774 *node = (struct lyd_node *)opaq;
775 }
776 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100777}
778
Michal Vasko3a41dff2020-07-15 14:30:28 +0200779API LY_ERR
780lyd_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 +0100781{
782 struct lyd_node *ret = NULL;
783 const struct lysc_node *schema;
784 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
785
Michal Vasko6027eb92020-07-15 16:37:30 +0200786 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100787
Michal Vaskof03ed032020-03-04 13:31:44 +0100788 if (!module) {
789 module = parent->schema->module;
790 }
791
Michal Vasko3a41dff2020-07-15 14:30:28 +0200792 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
793 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
794 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 +0100795
Michal Vasko3a41dff2020-07-15 14:30:28 +0200796 LY_CHECK_RET(lyd_create_inner(schema, &ret));
797 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100798 lyd_insert_node(parent, NULL, ret);
799 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200800
801 if (node) {
802 *node = ret;
803 }
804 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100805}
806
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807API LY_ERR
808lyd_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 +0100809{
810 struct lyd_node *ret = NULL, *key;
811 const struct lysc_node *schema, *key_s;
812 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
813 va_list ap;
814 const char *key_val;
815 LY_ERR rc = LY_SUCCESS;
816
Michal Vasko6027eb92020-07-15 16:37:30 +0200817 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100818
Michal Vaskof03ed032020-03-04 13:31:44 +0100819 if (!module) {
820 module = parent->schema->module;
821 }
822
Michal Vasko013a8182020-03-03 10:46:53 +0100823 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200824 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100825
826 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200827 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100828
Michal Vasko3a41dff2020-07-15 14:30:28 +0200829 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100830
831 /* create and insert all the keys */
832 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
833 key_val = va_arg(ap, const char *);
834
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200835 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 +0200836 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
837 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100838 lyd_insert_node(ret, NULL, key);
839 }
840
Michal Vasko013a8182020-03-03 10:46:53 +0100841 if (parent) {
842 lyd_insert_node(parent, NULL, ret);
843 }
844
845cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200846 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100847 if (rc) {
848 lyd_free_tree(ret);
849 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200850 } else if (node) {
851 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100852 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200853 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100854}
855
Michal Vasko3a41dff2020-07-15 14:30:28 +0200856API LY_ERR
857lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200858 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100859{
860 struct lyd_node *ret = NULL;
861 const struct lysc_node *schema;
862 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
863
Michal Vasko6027eb92020-07-15 16:37:30 +0200864 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100865
Michal Vaskof03ed032020-03-04 13:31:44 +0100866 if (!module) {
867 module = parent->schema->module;
868 }
Michal Vasko004d3152020-06-11 19:59:22 +0200869 if (!keys) {
870 keys = "";
871 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100872
Michal Vasko004d3152020-06-11 19:59:22 +0200873 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100874 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200875 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100876
Michal Vasko004d3152020-06-11 19:59:22 +0200877 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
878 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200879 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200880 } else {
881 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200883 }
Michal Vasko004d3152020-06-11 19:59:22 +0200884 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100885 lyd_insert_node(parent, NULL, ret);
886 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887
888 if (node) {
889 *node = ret;
890 }
891 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100892}
893
Michal Vasko3a41dff2020-07-15 14:30:28 +0200894API LY_ERR
895lyd_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 +0200896 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100897{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200898 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100899 struct lyd_node *ret = NULL;
900 const struct lysc_node *schema;
901 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
902
Michal Vasko6027eb92020-07-15 16:37:30 +0200903 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100904
Michal Vaskof03ed032020-03-04 13:31:44 +0100905 if (!module) {
906 module = parent->schema->module;
907 }
908
Michal Vasko013a8182020-03-03 10:46:53 +0100909 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200910 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100911
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200912 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 +0200913 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200914 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100915 lyd_insert_node(parent, NULL, ret);
916 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200917
918 if (node) {
919 *node = ret;
920 }
921 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100922}
923
Michal Vasko3a41dff2020-07-15 14:30:28 +0200924API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100925lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200926 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100927{
928 struct lyd_node *ret = NULL;
929 const struct lysc_node *schema;
930 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
931
Michal Vasko6027eb92020-07-15 16:37:30 +0200932 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100933
Michal Vaskof03ed032020-03-04 13:31:44 +0100934 if (!module) {
935 module = parent->schema->module;
936 }
937
Michal Vasko013a8182020-03-03 10:46:53 +0100938 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200939 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100940
Michal Vasko3a41dff2020-07-15 14:30:28 +0200941 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
942 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100943 lyd_insert_node(parent, NULL, ret);
944 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945
946 if (node) {
947 *node = ret;
948 }
949 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100950}
951
Michal Vasko4490d312020-06-16 13:08:55 +0200952/**
953 * @brief Update node value.
954 *
955 * @param[in] node Node to update.
956 * @param[in] value New value to set.
957 * @param[in] value_type Type of @p value for any node.
958 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
959 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
960 * @return LY_ERR value.
961 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200962static LY_ERR
963lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200964 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200965{
966 LY_ERR ret = LY_SUCCESS;
967 struct lyd_node *new_any;
968
969 switch (node->schema->nodetype) {
970 case LYS_CONTAINER:
971 case LYS_NOTIF:
972 case LYS_RPC:
973 case LYS_ACTION:
974 case LYS_LIST:
975 case LYS_LEAFLIST:
976 /* if it exists, there is nothing to update */
977 *new_parent = NULL;
978 *new_node = NULL;
979 break;
980 case LYS_LEAF:
981 ret = lyd_change_term(node, value);
982 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
983 /* there was an actual change (at least of the default flag) */
984 *new_parent = node;
985 *new_node = node;
986 ret = LY_SUCCESS;
987 } else if (ret == LY_ENOT) {
988 /* no change */
989 *new_parent = NULL;
990 *new_node = NULL;
991 ret = LY_SUCCESS;
992 } /* else error */
993 break;
994 case LYS_ANYDATA:
995 case LYS_ANYXML:
996 /* create a new any node */
997 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
998
999 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001000 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001001 /* not equal, switch values (so that we can use generic node free) */
1002 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1003 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1004 ((struct lyd_node_any *)node)->value.str = value;
1005 ((struct lyd_node_any *)node)->value_type = value_type;
1006
1007 *new_parent = node;
1008 *new_node = node;
1009 } else {
1010 /* they are equal */
1011 *new_parent = NULL;
1012 *new_node = NULL;
1013 }
1014 lyd_free_tree(new_any);
1015 break;
1016 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001017 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001018 ret = LY_EINT;
1019 break;
1020 }
1021
1022 return ret;
1023}
1024
Michal Vasko3a41dff2020-07-15 14:30:28 +02001025API LY_ERR
1026lyd_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 +02001027 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001028{
1029 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001030 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +02001031 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001032 size_t pref_len, name_len;
1033
Michal Vasko3a41dff2020-07-15 14:30:28 +02001034 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001035
Michal Vaskob7be7a82020-08-20 09:09:04 +02001036 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +02001037
1038 /* parse the name */
1039 tmp = name;
1040 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1041 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001042 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001043 }
1044
1045 /* find the module */
1046 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001047 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048 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 +02001049 }
1050
1051 /* set value if none */
1052 if (!val_str) {
1053 val_str = "";
1054 }
1055
Radek Krejci1798aae2020-07-14 13:26:06 +02001056 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 +02001057 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001058
1059 if (meta) {
1060 *meta = ret;
1061 }
1062 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001063}
1064
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001066lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001067 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001068{
1069 struct lyd_node *ret = NULL;
1070
Michal Vasko6027eb92020-07-15 16:37:30 +02001071 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001072
1073 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001074 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001075 }
1076 if (!value) {
1077 value = "";
1078 }
1079
Radek Krejci1798aae2020-07-14 13:26:06 +02001080 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1081 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001082 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001083 lyd_insert_node(parent, NULL, ret);
1084 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001085
1086 if (node) {
1087 *node = ret;
1088 }
1089 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001090}
1091
Michal Vasko3a41dff2020-07-15 14:30:28 +02001092API LY_ERR
1093lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001094 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001095{
Radek Krejci1798aae2020-07-14 13:26:06 +02001096 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001097 const struct ly_ctx *ctx;
1098 const char *prefix, *tmp;
1099 size_t pref_len, name_len;
1100
Michal Vasko3a41dff2020-07-15 14:30:28 +02001101 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001102
Michal Vaskob7be7a82020-08-20 09:09:04 +02001103 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001104
1105 /* parse the name */
1106 tmp = name;
1107 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1108 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001109 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001110 }
1111
1112 /* set value if none */
1113 if (!val_str) {
1114 val_str = "";
1115 }
1116
Radek Krejci1798aae2020-07-14 13:26:06 +02001117 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1118 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001119
1120 if (attr) {
1121 *attr = ret;
1122 }
1123 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001124}
1125
1126API LY_ERR
1127lyd_change_term(struct lyd_node *term, const char *val_str)
1128{
1129 LY_ERR ret = LY_SUCCESS;
1130 struct lysc_type *type;
1131 struct lyd_node_term *t;
1132 struct lyd_node *parent;
1133 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001134 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001135
1136 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1137
1138 if (!val_str) {
1139 val_str = "";
1140 }
1141 t = (struct lyd_node_term *)term;
1142 type = ((struct lysc_node_leaf *)term->schema)->type;
1143
1144 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001145 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 +02001146
1147 /* compare original and new value */
1148 if (type->plugin->compare(&t->value, &val)) {
1149 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001150 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001151 t->value = val;
1152 memset(&val, 0, sizeof val);
1153 val_change = 1;
1154 } else {
1155 val_change = 0;
1156 }
1157
1158 /* always clear the default flag */
1159 if (term->flags & LYD_DEFAULT) {
1160 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1161 parent->flags &= ~LYD_DEFAULT;
1162 }
1163 dflt_change = 1;
1164 } else {
1165 dflt_change = 0;
1166 }
1167
1168 if (val_change || dflt_change) {
1169 /* make the node non-validated */
1170 term->flags &= LYD_NEW;
1171 }
1172
1173 if (val_change) {
1174 if (term->schema->nodetype == LYS_LEAFLIST) {
1175 /* leaf-list needs to be hashed again and re-inserted into parent */
1176 lyd_unlink_hash(term);
1177 lyd_hash(term);
1178 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1179 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1180 /* list needs to be updated if its key was changed */
1181 assert(term->parent->schema->nodetype == LYS_LIST);
1182 lyd_unlink_hash((struct lyd_node *)term->parent);
1183 lyd_hash((struct lyd_node *)term->parent);
1184 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1185 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1186 }
1187
1188 /* retrun value */
1189 if (!val_change) {
1190 if (dflt_change) {
1191 /* only default flag change */
1192 ret = LY_EEXIST;
1193 } else {
1194 /* no change */
1195 ret = LY_ENOT;
1196 }
1197 } /* else value changed, LY_SUCCESS */
1198
1199cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001200 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001201 return ret;
1202}
1203
Michal Vasko41586352020-07-13 13:54:25 +02001204API LY_ERR
1205lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1206{
1207 LY_ERR ret = LY_SUCCESS;
1208 struct lyd_meta *m2;
1209 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001210 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001211
1212 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1213
1214 if (!val_str) {
1215 val_str = "";
1216 }
1217
1218 /* parse the new value into a new meta structure */
1219 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 +02001220 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001221
1222 /* compare original and new value */
1223 if (lyd_compare_meta(meta, m2)) {
1224 /* values differ, switch them */
1225 val = meta->value;
1226 meta->value = m2->value;
1227 m2->value = val;
1228 val_change = 1;
1229 } else {
1230 val_change = 0;
1231 }
1232
1233 /* retrun value */
1234 if (!val_change) {
1235 /* no change */
1236 ret = LY_ENOT;
1237 } /* else value changed, LY_SUCCESS */
1238
1239cleanup:
1240 return ret;
1241}
1242
Michal Vasko3a41dff2020-07-15 14:30:28 +02001243API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001244lyd_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 +02001245 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001246{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001247 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001248}
1249
1250API LY_ERR
1251lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001252 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 +02001253{
1254 LY_ERR ret = LY_SUCCESS, r;
1255 struct lyxp_expr *exp = NULL;
1256 struct ly_path *p = NULL;
1257 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1258 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001259 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001260 struct ly_path_predicate *pred;
1261
1262 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1263
1264 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001265 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001266 }
1267
1268 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001269 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 +02001270 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1271
1272 /* compile path */
1273 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1274 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001275 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001276
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001277 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1278 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001279 && !(options & LYD_NEWOPT_OPAQ)) {
1280 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1281 lys_nodetype2str(schema->nodetype), schema->name);
1282 ret = LY_EINVAL;
1283 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001284 } 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 +02001285 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001286 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1287 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001288
1289 if (!value) {
1290 value = "";
1291 }
1292
1293 r = LY_SUCCESS;
1294 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001295 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001296 }
1297 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001298 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 +02001299 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1300 }
1301
1302 /* try to find any existing nodes in the path */
1303 if (parent) {
1304 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1305 if (ret == LY_SUCCESS) {
1306 /* the node exists, are we supposed to update it or is it just a default? */
1307 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1308 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1309 ret = LY_EEXIST;
1310 goto cleanup;
1311 }
1312
1313 /* update the existing node */
1314 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1315 goto cleanup;
1316 } else if (ret == LY_EINCOMPLETE) {
1317 /* some nodes were found, adjust the iterator to the next segment */
1318 ++path_idx;
1319 } else if (ret == LY_ENOTFOUND) {
1320 /* 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 +02001321 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001322 node = parent;
1323 }
1324 } else {
1325 /* error */
1326 goto cleanup;
1327 }
1328 }
1329
1330 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001331 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001332 cur_parent = node;
1333 schema = p[path_idx].node;
1334
1335 switch (schema->nodetype) {
1336 case LYS_LIST:
1337 if (!(schema->flags & LYS_KEYLESS)) {
1338 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1339 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001340 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1341 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1342 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001343 } else {
1344 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1345 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1346 }
1347 break;
1348 }
Radek Krejci0f969882020-08-21 16:56:47 +02001349 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001350 case LYS_CONTAINER:
1351 case LYS_NOTIF:
1352 case LYS_RPC:
1353 case LYS_ACTION:
1354 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1355 break;
1356 case LYS_LEAFLIST:
1357 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1358 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001359 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1360 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1361 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001362 } else {
1363 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1364 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1365 }
1366 break;
1367 case LYS_LEAF:
1368 /* make there is some value */
1369 if (!value) {
1370 value = "";
1371 }
1372
1373 r = LY_SUCCESS;
1374 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001375 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001376 }
1377 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001378 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 +02001379 } else {
1380 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001381 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001382 LYD_JSON, NULL, NULL, 0, schema->module->name,
1383 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001384 }
1385 break;
1386 case LYS_ANYDATA:
1387 case LYS_ANYXML:
1388 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1389 break;
1390 default:
1391 LOGINT(ctx);
1392 ret = LY_EINT;
1393 goto cleanup;
1394 }
1395
1396 if (cur_parent) {
1397 /* connect to the parent */
1398 lyd_insert_node(cur_parent, NULL, node);
1399 } else if (parent) {
1400 /* connect to top-level siblings */
1401 lyd_insert_node(NULL, &parent, node);
1402 }
1403
1404 /* update remembered nodes */
1405 if (!nparent) {
1406 nparent = node;
1407 }
1408 nnode = node;
1409 }
1410
1411cleanup:
1412 lyxp_expr_free(ctx, exp);
1413 ly_path_free(ctx, p);
1414 if (!ret) {
1415 /* set out params only on success */
1416 if (new_parent) {
1417 *new_parent = nparent;
1418 }
1419 if (new_node) {
1420 *new_node = nnode;
1421 }
1422 }
1423 return ret;
1424}
1425
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001426LY_ERR
1427lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001428 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 +02001429 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001430{
1431 LY_ERR ret;
1432 const struct lysc_node *iter = NULL;
1433 struct lyd_node *node;
1434 struct lyd_value **dflts;
1435 LY_ARRAY_COUNT_TYPE u;
1436
1437 assert(first && (parent || sparent || mod));
1438
1439 if (!sparent && parent) {
1440 sparent = parent->schema;
1441 }
1442
1443 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1444 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1445 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001446 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1447 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001448 }
1449
1450 switch (iter->nodetype) {
1451 case LYS_CHOICE:
1452 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1453 /* create default case data */
1454 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1455 NULL, node_types, node_when, impl_opts, diff));
1456 }
1457 break;
1458 case LYS_CONTAINER:
1459 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1460 /* create default NP container */
1461 LY_CHECK_RET(lyd_create_inner(iter, &node));
1462 node->flags = LYD_DEFAULT;
1463 lyd_insert_node(parent, first, node);
1464
1465 /* cannot be a NP container with when */
1466 assert(!iter->when);
1467
1468 /* create any default children */
1469 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1470 impl_opts, diff));
1471 }
1472 break;
1473 case LYS_LEAF:
1474 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1475 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1476 /* create default leaf */
1477 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1478 if (ret == LY_EINCOMPLETE) {
1479 if (node_types) {
1480 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001481 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001482 }
1483 } else if (ret) {
1484 return ret;
1485 }
1486 node->flags = LYD_DEFAULT;
1487 lyd_insert_node(parent, first, node);
1488
1489 if (iter->when && node_when) {
1490 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001491 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001492 }
1493 if (diff) {
1494 /* add into diff */
1495 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1496 }
1497 }
1498 break;
1499 case LYS_LEAFLIST:
1500 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1501 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1502 /* create all default leaf-lists */
1503 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1504 LY_ARRAY_FOR(dflts, u) {
1505 ret = lyd_create_term2(iter, dflts[u], &node);
1506 if (ret == LY_EINCOMPLETE) {
1507 if (node_types) {
1508 /* remember to resolve type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001509 LY_CHECK_RET(ly_set_add(node_types, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001510 }
1511 } else if (ret) {
1512 return ret;
1513 }
1514 node->flags = LYD_DEFAULT;
1515 lyd_insert_node(parent, first, node);
1516
1517 if (iter->when && node_when) {
1518 /* remember to resolve when */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001519 LY_CHECK_RET(ly_set_add(node_when, node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001520 }
1521 if (diff) {
1522 /* add into diff */
1523 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1524 }
1525 }
1526 }
1527 break;
1528 default:
1529 /* without defaults */
1530 break;
1531 }
1532 }
1533
1534 return LY_SUCCESS;
1535}
1536
1537API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001538lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001539{
Michal Vasko56daf732020-08-10 10:57:18 +02001540 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001541 LY_ERR ret = LY_SUCCESS;
1542
1543 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1544 if (diff) {
1545 *diff = NULL;
1546 }
1547
Michal Vasko56daf732020-08-10 10:57:18 +02001548 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001549 /* skip added default nodes */
1550 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1551 && (node->schema->nodetype & LYD_NODE_INNER)) {
1552 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1553 NULL, implicit_options, diff), cleanup);
1554 }
1555
Michal Vasko56daf732020-08-10 10:57:18 +02001556 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001557 }
1558
1559cleanup:
1560 if (ret && diff) {
1561 lyd_free_all(*diff);
1562 *diff = NULL;
1563 }
1564 return ret;
1565}
1566
1567API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001568lyd_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 +02001569{
1570 const struct lys_module *mod;
1571 struct lyd_node *d = NULL;
1572 uint32_t i = 0;
1573 LY_ERR ret = LY_SUCCESS;
1574
1575 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1576 if (diff) {
1577 *diff = NULL;
1578 }
1579 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001580 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001581 }
1582
1583 /* add nodes for each module one-by-one */
1584 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1585 if (!mod->implemented) {
1586 continue;
1587 }
1588
1589 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1590 if (d) {
1591 /* merge into one diff */
1592 lyd_insert_sibling(*diff, d, diff);
1593
1594 d = NULL;
1595 }
1596 }
1597
1598cleanup:
1599 if (ret && diff) {
1600 lyd_free_all(*diff);
1601 *diff = NULL;
1602 }
1603 return ret;
1604}
1605
1606API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001607lyd_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 +02001608{
1609 struct lyd_node *root, *d = NULL;
1610 LY_ERR ret = LY_SUCCESS;
1611
1612 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1613 if (diff) {
1614 *diff = NULL;
1615 }
1616
1617 /* add all top-level defaults for this module */
1618 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1619
1620 /* process nested nodes */
1621 LY_LIST_FOR(*tree, root) {
1622 /* skip added default nodes */
1623 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1624 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1625
1626 if (d) {
1627 /* merge into one diff */
1628 lyd_insert_sibling(*diff, d, diff);
1629
1630 d = NULL;
1631 }
1632 }
1633 }
1634
1635cleanup:
1636 if (ret && diff) {
1637 lyd_free_all(*diff);
1638 *diff = NULL;
1639 }
1640 return ret;
1641}
1642
Michal Vasko90932a92020-02-12 14:33:03 +01001643struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001644lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001645{
Michal Vaskob104f112020-07-17 09:54:54 +02001646 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001647 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001648 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001649
Michal Vaskob104f112020-07-17 09:54:54 +02001650 assert(new_node);
1651
1652 if (!first_sibling || !new_node->schema) {
1653 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001654 return NULL;
1655 }
1656
Michal Vaskob104f112020-07-17 09:54:54 +02001657 if (first_sibling->parent && first_sibling->parent->children_ht) {
1658 /* find the anchor using hashes */
1659 sparent = first_sibling->parent->schema;
1660 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1661 while (schema) {
1662 /* keep trying to find the first existing instance of the closest following schema sibling,
1663 * otherwise return NULL - inserting at the end */
1664 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1665 break;
1666 }
1667
1668 schema = lys_getnext(schema, sparent, NULL, 0);
1669 }
1670 } else {
1671 /* find the anchor without hashes */
1672 match = (struct lyd_node *)first_sibling;
1673 if (!lysc_data_parent(new_node->schema)) {
1674 /* we are in top-level, skip all the data from preceding modules */
1675 LY_LIST_FOR(match, match) {
1676 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1677 break;
1678 }
1679 }
1680 }
1681
1682 /* get the first schema sibling */
1683 sparent = lysc_data_parent(new_node->schema);
1684 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1685
1686 found = 0;
1687 LY_LIST_FOR(match, match) {
1688 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1689 /* we have found an opaque node, which must be at the end, so use it OR
1690 * modules do not match, so we must have traversed all the data from new_node module (if any),
1691 * we have found the first node of the next module, that is what we want */
1692 break;
1693 }
1694
1695 /* skip schema nodes until we find the instantiated one */
1696 while (!found) {
1697 if (new_node->schema == schema) {
1698 /* we have found the schema of the new node, continue search to find the first
1699 * data node with a different schema (after our schema) */
1700 found = 1;
1701 break;
1702 }
1703 if (match->schema == schema) {
1704 /* current node (match) is a data node still before the new node, continue search in data */
1705 break;
1706 }
1707 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1708 assert(schema);
1709 }
1710
1711 if (found && (match->schema != new_node->schema)) {
1712 /* find the next node after we have found our node schema data instance */
1713 break;
1714 }
1715 }
Michal Vasko90932a92020-02-12 14:33:03 +01001716 }
1717
1718 return match;
1719}
1720
1721/**
1722 * @brief Insert node after a sibling.
1723 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001724 * Handles inserting into NP containers and key-less lists.
1725 *
Michal Vasko90932a92020-02-12 14:33:03 +01001726 * @param[in] sibling Sibling to insert after.
1727 * @param[in] node Node to insert.
1728 */
1729static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001730lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001731{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001732 struct lyd_node_inner *par;
1733
Michal Vasko90932a92020-02-12 14:33:03 +01001734 assert(!node->next && (node->prev == node));
1735
1736 node->next = sibling->next;
1737 node->prev = sibling;
1738 sibling->next = node;
1739 if (node->next) {
1740 /* sibling had a succeeding node */
1741 node->next->prev = node;
1742 } else {
1743 /* sibling was last, find first sibling and change its prev */
1744 if (sibling->parent) {
1745 sibling = sibling->parent->child;
1746 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001747 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001748 }
1749 sibling->prev = node;
1750 }
1751 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001752
Michal Vasko9f96a052020-03-10 09:41:45 +01001753 for (par = node->parent; par; par = par->parent) {
1754 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1755 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001756 par->flags &= ~LYD_DEFAULT;
1757 }
Michal Vaskob104f112020-07-17 09:54:54 +02001758 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001759 /* rehash key-less list */
1760 lyd_hash((struct lyd_node *)par);
1761 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001762 }
Michal Vasko90932a92020-02-12 14:33:03 +01001763}
1764
1765/**
1766 * @brief Insert node before a sibling.
1767 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001768 * Handles inserting into NP containers and key-less lists.
1769 *
Michal Vasko90932a92020-02-12 14:33:03 +01001770 * @param[in] sibling Sibling to insert before.
1771 * @param[in] node Node to insert.
1772 */
1773static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001774lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001775{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001776 struct lyd_node_inner *par;
1777
Michal Vasko90932a92020-02-12 14:33:03 +01001778 assert(!node->next && (node->prev == node));
1779
1780 node->next = sibling;
1781 /* covers situation of sibling being first */
1782 node->prev = sibling->prev;
1783 sibling->prev = node;
1784 if (node->prev->next) {
1785 /* sibling had a preceding node */
1786 node->prev->next = node;
1787 } else if (sibling->parent) {
1788 /* sibling was first and we must also change parent child pointer */
1789 sibling->parent->child = node;
1790 }
1791 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001792
Michal Vasko9f96a052020-03-10 09:41:45 +01001793 for (par = node->parent; par; par = par->parent) {
1794 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1795 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001796 par->flags &= ~LYD_DEFAULT;
1797 }
Michal Vaskob104f112020-07-17 09:54:54 +02001798 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001799 /* rehash key-less list */
1800 lyd_hash((struct lyd_node *)par);
1801 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001802 }
Michal Vasko90932a92020-02-12 14:33:03 +01001803}
1804
1805/**
Michal Vaskob104f112020-07-17 09:54:54 +02001806 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001807 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001808 * Handles inserting into NP containers and key-less lists.
1809 *
Michal Vasko90932a92020-02-12 14:33:03 +01001810 * @param[in] parent Parent to insert into.
1811 * @param[in] node Node to insert.
1812 */
1813static void
Michal Vaskob104f112020-07-17 09:54:54 +02001814lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001815{
1816 struct lyd_node_inner *par;
1817
Radek Krejcia1c1e542020-09-29 16:06:52 +02001818 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001819 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001820
1821 par = (struct lyd_node_inner *)parent;
1822
Michal Vaskob104f112020-07-17 09:54:54 +02001823 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001824 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001825
Michal Vaskod989ba02020-08-24 10:59:24 +02001826 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001827 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1828 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001829 par->flags &= ~LYD_DEFAULT;
1830 }
Michal Vasko52927e22020-03-16 17:26:14 +01001831 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001832 /* rehash key-less list */
1833 lyd_hash((struct lyd_node *)par);
1834 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001835 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001836}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001837
Michal Vasko751cb4d2020-07-14 12:25:28 +02001838/**
1839 * @brief Learn whether a list instance has all the keys.
1840 *
1841 * @param[in] list List instance to check.
1842 * @return non-zero if all the keys were found,
1843 * @return 0 otherwise.
1844 */
1845static int
1846lyd_insert_has_keys(const struct lyd_node *list)
1847{
1848 const struct lyd_node *key;
1849 const struct lysc_node *skey = NULL;
1850
1851 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02001852 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001853 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1854 if (!key || (key->schema != skey)) {
1855 /* key missing */
1856 return 0;
1857 }
1858
1859 key = key->next;
1860 }
1861
1862 /* all keys found */
1863 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001864}
1865
1866void
Michal Vaskob104f112020-07-17 09:54:54 +02001867lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001868{
Michal Vaskob104f112020-07-17 09:54:54 +02001869 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001870
Michal Vaskob104f112020-07-17 09:54:54 +02001871 /* inserting list without its keys is not supported */
1872 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001873
Michal Vaskob104f112020-07-17 09:54:54 +02001874 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1875 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001876 }
Michal Vasko90932a92020-02-12 14:33:03 +01001877
Michal Vaskob104f112020-07-17 09:54:54 +02001878 /* get first sibling */
1879 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001880
Michal Vaskob104f112020-07-17 09:54:54 +02001881 /* find the anchor, our next node, so we can insert before it */
1882 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1883 if (anchor) {
1884 lyd_insert_before_node(anchor, node);
1885 } else if (first_sibling) {
1886 lyd_insert_after_node(first_sibling->prev, node);
1887 } else if (parent) {
1888 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001889 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001890 *first_sibling_p = node;
1891 }
1892
1893 /* insert into parent HT */
1894 lyd_insert_hash(node);
1895
1896 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001897 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001898 lyd_hash(parent);
1899
1900 /* now we can insert even the list into its parent HT */
1901 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001902 }
Michal Vasko90932a92020-02-12 14:33:03 +01001903}
1904
Michal Vaskof03ed032020-03-04 13:31:44 +01001905static LY_ERR
1906lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1907{
1908 const struct lysc_node *par2;
1909
1910 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001911 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001912
1913 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001914 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001915
1916 if (parent) {
1917 /* inner node */
1918 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001919 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1920 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001921 return LY_EINVAL;
1922 }
1923 } else {
1924 /* top-level node */
1925 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001926 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001927 return LY_EINVAL;
1928 }
1929 }
1930
1931 return LY_SUCCESS;
1932}
1933
1934API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001935lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001936{
1937 struct lyd_node *iter;
1938
Michal Vasko654bc852020-06-23 13:28:06 +02001939 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001940
1941 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1942
1943 if (node->schema->flags & LYS_KEY) {
1944 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1945 return LY_EINVAL;
1946 }
1947
1948 if (node->parent || node->prev->next) {
1949 lyd_unlink_tree(node);
1950 }
1951
1952 while (node) {
1953 iter = node->next;
1954 lyd_unlink_tree(node);
1955 lyd_insert_node(parent, NULL, node);
1956 node = iter;
1957 }
1958 return LY_SUCCESS;
1959}
1960
1961API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001962lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001963{
1964 struct lyd_node *iter;
1965
Michal Vaskob104f112020-07-17 09:54:54 +02001966 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001967
Michal Vaskob104f112020-07-17 09:54:54 +02001968 if (sibling) {
1969 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001970 }
1971
1972 if (node->parent || node->prev->next) {
1973 lyd_unlink_tree(node);
1974 }
1975
1976 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001977 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001978 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001979 return LY_EINVAL;
1980 }
1981
Michal Vaskob1b5c262020-03-05 14:29:47 +01001982 iter = node->next;
1983 lyd_unlink_tree(node);
1984 lyd_insert_node(NULL, &sibling, node);
1985 node = iter;
1986 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001987
Michal Vaskob104f112020-07-17 09:54:54 +02001988 if (first) {
1989 /* find the first sibling */
1990 *first = sibling;
1991 while ((*first)->prev->next) {
1992 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001993 }
1994 }
1995
1996 return LY_SUCCESS;
1997}
1998
Michal Vaskob1b5c262020-03-05 14:29:47 +01001999API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002000lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2001{
2002 struct lyd_node *iter;
2003
2004 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2005
Michal Vasko62ed12d2020-05-21 10:08:25 +02002006 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002007
Michal Vaskob104f112020-07-17 09:54:54 +02002008 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002009 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002010 return LY_EINVAL;
2011 }
2012
2013 if (node->parent || node->prev->next) {
2014 lyd_unlink_tree(node);
2015 }
2016
2017 /* insert in reverse order to get the original order */
2018 node = node->prev;
2019 while (node) {
2020 iter = node->prev;
2021 lyd_unlink_tree(node);
2022
2023 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002024 lyd_insert_hash(node);
2025
Michal Vaskof03ed032020-03-04 13:31:44 +01002026 /* move the anchor accordingly */
2027 sibling = node;
2028
2029 node = (iter == node) ? NULL : iter;
2030 }
2031 return LY_SUCCESS;
2032}
2033
2034API LY_ERR
2035lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2036{
2037 struct lyd_node *iter;
2038
2039 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2040
Michal Vasko62ed12d2020-05-21 10:08:25 +02002041 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002042
Michal Vaskob104f112020-07-17 09:54:54 +02002043 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002044 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002045 return LY_EINVAL;
2046 }
2047
2048 if (node->parent || node->prev->next) {
2049 lyd_unlink_tree(node);
2050 }
2051
2052 while (node) {
2053 iter = node->next;
2054 lyd_unlink_tree(node);
2055
2056 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002057 lyd_insert_hash(node);
2058
Michal Vaskof03ed032020-03-04 13:31:44 +01002059 /* move the anchor accordingly */
2060 sibling = node;
2061
2062 node = iter;
2063 }
2064 return LY_SUCCESS;
2065}
2066
2067API void
2068lyd_unlink_tree(struct lyd_node *node)
2069{
2070 struct lyd_node *iter;
2071
2072 if (!node) {
2073 return;
2074 }
2075
Michal Vaskob104f112020-07-17 09:54:54 +02002076 /* update hashes while still linked into the tree */
2077 lyd_unlink_hash(node);
2078
Michal Vaskof03ed032020-03-04 13:31:44 +01002079 /* unlink from siblings */
2080 if (node->prev->next) {
2081 node->prev->next = node->next;
2082 }
2083 if (node->next) {
2084 node->next->prev = node->prev;
2085 } else {
2086 /* unlinking the last node */
2087 if (node->parent) {
2088 iter = node->parent->child;
2089 } else {
2090 iter = node->prev;
2091 while (iter->prev != node) {
2092 iter = iter->prev;
2093 }
2094 }
2095 /* update the "last" pointer from the first node */
2096 iter->prev = node->prev;
2097 }
2098
2099 /* unlink from parent */
2100 if (node->parent) {
2101 if (node->parent->child == node) {
2102 /* the node is the first child */
2103 node->parent->child = node->next;
2104 }
2105
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002106 /* check for NP container whether its last non-default node is not being unlinked */
2107 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2108 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2109 LY_LIST_FOR(node->parent->child, iter) {
2110 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2111 break;
2112 }
2113 }
2114 if (!iter) {
2115 node->parent->flags |= LYD_DEFAULT;
2116 }
2117 }
2118
Michal Vaskof03ed032020-03-04 13:31:44 +01002119 /* check for keyless list and update its hash */
2120 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002121 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002122 lyd_hash(iter);
2123 }
2124 }
2125
2126 node->parent = NULL;
2127 }
2128
2129 node->next = NULL;
2130 node->prev = node;
2131}
2132
Michal Vaskoa5da3292020-08-12 13:10:50 +02002133void
Radek Krejci1798aae2020-07-14 13:26:06 +02002134lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2135{
2136 struct lyd_meta *last, *iter;
2137
2138 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002139
2140 if (!meta) {
2141 return;
2142 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002143
2144 for (iter = meta; iter; iter = iter->next) {
2145 iter->parent = parent;
2146 }
2147
2148 /* insert as the last attribute */
2149 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002150 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002151 last->next = meta;
2152 } else {
2153 parent->meta = meta;
2154 }
2155
2156 /* remove default flags from NP containers */
2157 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2158 parent->flags &= ~LYD_DEFAULT;
2159 parent = (struct lyd_node *)parent->parent;
2160 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002161}
2162
2163LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002164lyd_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 +02002165 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 +02002166 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002167{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002168 LY_ERR ret, rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002169 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002170 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002171 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002172
Michal Vasko9f96a052020-03-10 09:41:45 +01002173 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002174
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002175 LY_ARRAY_FOR(mod->compiled->exts, u) {
2176 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2177 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002178 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002179 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002180 break;
2181 }
2182 }
2183 if (!ant) {
2184 /* attribute is not defined as a metadata annotation (RFC 7952) */
2185 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2186 mod->name, name_len, name);
2187 return LY_EINVAL;
2188 }
2189
Michal Vasko9f96a052020-03-10 09:41:45 +01002190 mt = calloc(1, sizeof *mt);
2191 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2192 mt->parent = parent;
2193 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002194 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 +01002195 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002196 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002197 return ret;
2198 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002199 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2200 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002201
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002202 /* insert as the last attribute */
2203 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002204 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002205 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002206 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002207 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002208 }
2209
Michal Vasko9f96a052020-03-10 09:41:45 +01002210 if (meta) {
2211 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002212 }
2213 return ret;
2214}
2215
Michal Vaskoa5da3292020-08-12 13:10:50 +02002216void
2217lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2218{
2219 struct lyd_attr *last, *iter;
2220 struct lyd_node_opaq *opaq;
2221
2222 assert(parent && !parent->schema);
2223
2224 if (!attr) {
2225 return;
2226 }
2227
2228 opaq = (struct lyd_node_opaq *)parent;
2229 for (iter = attr; iter; iter = iter->next) {
2230 iter->parent = opaq;
2231 }
2232
2233 /* insert as the last attribute */
2234 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002235 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002236 last->next = attr;
2237 } else {
2238 opaq->attr = attr;
2239 }
2240}
2241
Michal Vasko52927e22020-03-16 17:26:14 +01002242LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002243lyd_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 +02002244 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 +02002245 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 +01002246{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002247 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002248 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002249
2250 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2251 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002252
2253 if (!value_len) {
2254 value = "";
2255 }
2256
2257 at = calloc(1, sizeof *at);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002258 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
2259 at->hint = value_hint;
2260 at->format = format;
2261 at->val_prefs = val_prefs;
2262
Radek Krejci011e4aa2020-09-04 15:22:31 +02002263 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002264 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002265 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2266 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002267 *dynamic = 0;
2268 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002269 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002270 }
2271
Radek Krejci1798aae2020-07-14 13:26:06 +02002272 if (prefix_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002273 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +02002274 }
2275 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002276 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002277 }
2278
2279 /* insert as the last attribute */
2280 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002281 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002282 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002283 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002284 last->next = at;
2285 }
2286
Radek Krejci011e4aa2020-09-04 15:22:31 +02002287finish:
2288 if (ret) {
2289 lyd_free_attr_single(ctx, at);
2290 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002291 *attr = at;
2292 }
2293 return LY_SUCCESS;
2294}
2295
Radek Krejci084289f2019-07-09 17:35:30 +02002296API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002297lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002298{
Michal Vasko004d3152020-06-11 19:59:22 +02002299 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002300
Michal Vasko004d3152020-06-11 19:59:22 +02002301 if (ly_path_eval(path, tree, &target)) {
2302 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002303 }
2304
Michal Vasko004d3152020-06-11 19:59:22 +02002305 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002306}
2307
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002308API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002309lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002310{
2311 const struct lyd_node *iter1, *iter2;
2312 struct lyd_node_term *term1, *term2;
2313 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002314 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002315 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002316
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002317 if (!node1 || !node2) {
2318 if (node1 == node2) {
2319 return LY_SUCCESS;
2320 } else {
2321 return LY_ENOT;
2322 }
2323 }
2324
Michal Vaskob7be7a82020-08-20 09:09:04 +02002325 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002326 return LY_ENOT;
2327 }
2328
2329 if (node1->hash != node2->hash) {
2330 return LY_ENOT;
2331 }
Michal Vasko52927e22020-03-16 17:26:14 +01002332 /* 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 +02002333
Michal Vasko52927e22020-03-16 17:26:14 +01002334 if (!node1->schema) {
2335 opaq1 = (struct lyd_node_opaq *)node1;
2336 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002337 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002338 return LY_ENOT;
2339 }
Michal Vasko52927e22020-03-16 17:26:14 +01002340 switch (opaq1->format) {
2341 case LYD_XML:
2342 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2343 return LY_ENOT;
2344 }
2345 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002346 case LYD_JSON:
2347 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2348 if (strcmp(opaq1->value, opaq2->value)) {
2349 return LY_ENOT;
2350 }
2351 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002352 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002353 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002354 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002355 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002356 return LY_EINT;
2357 }
2358 if (options & LYD_COMPARE_FULL_RECURSION) {
2359 iter1 = opaq1->child;
2360 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002361 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002362 }
2363 return LY_SUCCESS;
2364 } else {
2365 switch (node1->schema->nodetype) {
2366 case LYS_LEAF:
2367 case LYS_LEAFLIST:
2368 if (options & LYD_COMPARE_DEFAULTS) {
2369 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2370 return LY_ENOT;
2371 }
2372 }
2373
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002374 term1 = (struct lyd_node_term *)node1;
2375 term2 = (struct lyd_node_term *)node2;
2376 if (term1->value.realtype != term2->value.realtype) {
2377 return LY_ENOT;
2378 }
Michal Vasko52927e22020-03-16 17:26:14 +01002379
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002380 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002381 case LYS_CONTAINER:
2382 if (options & LYD_COMPARE_DEFAULTS) {
2383 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2384 return LY_ENOT;
2385 }
2386 }
2387 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002388 iter1 = ((struct lyd_node_inner *)node1)->child;
2389 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002390 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002391 }
2392 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002393 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002394 case LYS_ACTION:
2395 if (options & LYD_COMPARE_FULL_RECURSION) {
2396 /* TODO action/RPC
2397 goto all_children_compare;
2398 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002399 }
2400 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002401 case LYS_NOTIF:
2402 if (options & LYD_COMPARE_FULL_RECURSION) {
2403 /* TODO Notification
2404 goto all_children_compare;
2405 */
2406 }
2407 return LY_SUCCESS;
2408 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002409 iter1 = ((struct lyd_node_inner *)node1)->child;
2410 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002411
2412 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2413 /* lists with keys, their equivalence is based on their keys */
Michal Vasko22df3f02020-08-24 13:29:22 +02002414 for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002415 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002416 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002417 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002418 return LY_ENOT;
2419 }
2420 iter1 = iter1->next;
2421 iter2 = iter2->next;
2422 }
2423 } else {
2424 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2425
Radek Krejci0f969882020-08-21 16:56:47 +02002426all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002427 if (!iter1 && !iter2) {
2428 /* no children, nothing to compare */
2429 return LY_SUCCESS;
2430 }
2431
Michal Vaskod989ba02020-08-24 10:59:24 +02002432 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002433 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002434 return LY_ENOT;
2435 }
2436 }
2437 if (iter1 || iter2) {
2438 return LY_ENOT;
2439 }
2440 }
2441 return LY_SUCCESS;
2442 case LYS_ANYXML:
2443 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002444 any1 = (struct lyd_node_any *)node1;
2445 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002446
2447 if (any1->value_type != any2->value_type) {
2448 return LY_ENOT;
2449 }
2450 switch (any1->value_type) {
2451 case LYD_ANYDATA_DATATREE:
2452 iter1 = any1->value.tree;
2453 iter2 = any2->value.tree;
2454 goto all_children_compare;
2455 case LYD_ANYDATA_STRING:
2456 case LYD_ANYDATA_XML:
2457 case LYD_ANYDATA_JSON:
2458 len1 = strlen(any1->value.str);
2459 len2 = strlen(any2->value.str);
2460 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2461 return LY_ENOT;
2462 }
2463 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002464 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002465 len1 = lyd_lyb_data_length(any1->value.mem);
2466 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002467 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2468 return LY_ENOT;
2469 }
2470 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002471 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002472 }
2473 }
2474
Michal Vaskob7be7a82020-08-20 09:09:04 +02002475 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002476 return LY_EINT;
2477}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002478
Michal Vasko21725742020-06-29 11:49:25 +02002479API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002480lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002481{
Michal Vaskod989ba02020-08-24 10:59:24 +02002482 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002483 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2484 }
2485
Michal Vasko11a81432020-07-28 16:31:29 +02002486 if (node1 == node2) {
2487 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002488 }
Michal Vasko11a81432020-07-28 16:31:29 +02002489 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002490}
2491
2492API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002493lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2494{
2495 if (!meta1 || !meta2) {
2496 if (meta1 == meta2) {
2497 return LY_SUCCESS;
2498 } else {
2499 return LY_ENOT;
2500 }
2501 }
2502
Michal Vaskob7be7a82020-08-20 09:09:04 +02002503 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002504 return LY_ENOT;
2505 }
2506
2507 if (meta1->value.realtype != meta2->value.realtype) {
2508 return LY_ENOT;
2509 }
2510
2511 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2512}
2513
Radek Krejci22ebdba2019-07-25 13:59:43 +02002514/**
Michal Vasko52927e22020-03-16 17:26:14 +01002515 * @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 +02002516 *
2517 * 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 +02002518 *
2519 * @param[in] node Original node to duplicate
2520 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2521 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2522 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2523 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2524 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002525 */
Michal Vasko52927e22020-03-16 17:26:14 +01002526static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002527lyd_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 +02002528 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002529{
Michal Vasko52927e22020-03-16 17:26:14 +01002530 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002531 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002532 struct lyd_meta *meta;
2533 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002534 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002535
Michal Vasko52927e22020-03-16 17:26:14 +01002536 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002537
Michal Vasko52927e22020-03-16 17:26:14 +01002538 if (!node->schema) {
2539 dup = calloc(1, sizeof(struct lyd_node_opaq));
2540 } else {
2541 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002542 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002543 case LYS_ACTION:
2544 case LYS_NOTIF:
2545 case LYS_CONTAINER:
2546 case LYS_LIST:
2547 dup = calloc(1, sizeof(struct lyd_node_inner));
2548 break;
2549 case LYS_LEAF:
2550 case LYS_LEAFLIST:
2551 dup = calloc(1, sizeof(struct lyd_node_term));
2552 break;
2553 case LYS_ANYDATA:
2554 case LYS_ANYXML:
2555 dup = calloc(1, sizeof(struct lyd_node_any));
2556 break;
2557 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002558 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002559 ret = LY_EINT;
2560 goto error;
2561 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002562 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002563 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002564
Michal Vaskof6df0a02020-06-16 13:08:34 +02002565 if (options & LYD_DUP_WITH_FLAGS) {
2566 dup->flags = node->flags;
2567 } else {
2568 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2569 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002570 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002571 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002572
Michal Vasko25a32822020-07-09 15:48:22 +02002573 /* duplicate metadata */
2574 if (!(options & LYD_DUP_NO_META)) {
2575 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002576 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002577 }
2578 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002579
2580 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002581 if (!dup->schema) {
2582 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2583 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2584 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002585
2586 if (options & LYD_DUP_RECURSIVE) {
2587 /* duplicate all the children */
2588 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002589 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002590 }
2591 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002592 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002593 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002594 if (orig->prefix.id) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002595 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002596 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002597 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 +01002598 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002599 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 +01002600 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002601 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
2602 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 +01002603 LY_ARRAY_INCREMENT(opaq->val_prefs);
2604 }
2605 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002606 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002607 opaq->ctx = orig->ctx;
2608 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2609 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2610 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2611
2612 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002613 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
2614 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002615 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2616 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2617 struct lyd_node *child;
2618
2619 if (options & LYD_DUP_RECURSIVE) {
2620 /* duplicate all the children */
2621 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002622 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002623 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002624 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002625 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002626 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002627 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002628 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002629 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002630 if (!child) {
2631 /* possibly not keys are present in filtered tree */
2632 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002633 } else if (child->schema != key) {
2634 /* possibly not all keys are present in filtered tree,
2635 * but there can be also some non-key nodes */
2636 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002637 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002638 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002639 child = child->next;
2640 }
2641 }
2642 lyd_hash(dup);
2643 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002644 dup->hash = node->hash;
2645 any = (struct lyd_node_any *)node;
2646 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002647 }
2648
Michal Vasko52927e22020-03-16 17:26:14 +01002649 /* insert */
2650 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002651
2652 if (dup_p) {
2653 *dup_p = dup;
2654 }
2655 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002656
2657error:
Michal Vasko52927e22020-03-16 17:26:14 +01002658 lyd_free_tree(dup);
2659 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002660}
2661
Michal Vasko3a41dff2020-07-15 14:30:28 +02002662static LY_ERR
2663lyd_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 +02002664 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002665{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002666 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002667 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002668
2669 *dup_parent = NULL;
2670 *local_parent = NULL;
2671
2672 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2673 if (parent && (parent->schema == orig_parent->schema)) {
2674 /* stop creating parents, connect what we have into the provided parent */
2675 iter = parent;
2676 repeat = 0;
2677 } else {
2678 iter = NULL;
2679 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002680 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002681 }
2682 if (!*local_parent) {
2683 *local_parent = (struct lyd_node_inner *)iter;
2684 }
2685 if (iter->child) {
2686 /* 1) list - add after keys
2687 * 2) provided parent with some children */
2688 iter->child->prev->next = *dup_parent;
2689 if (*dup_parent) {
2690 (*dup_parent)->prev = iter->child->prev;
2691 iter->child->prev = *dup_parent;
2692 }
2693 } else {
2694 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2695 }
2696 if (*dup_parent) {
2697 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2698 }
2699 *dup_parent = (struct lyd_node *)iter;
2700 }
2701
2702 if (repeat && parent) {
2703 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002704 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko3a41dff2020-07-15 14:30:28 +02002705 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2706 return LY_EINVAL;
2707 }
2708
2709 return LY_SUCCESS;
2710}
2711
2712static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002713lyd_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 +02002714{
2715 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002716 const struct lyd_node *orig; /* original node to be duplicated */
2717 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002718 struct lyd_node *top = NULL; /* the most higher created node */
2719 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002720
Michal Vasko3a41dff2020-07-15 14:30:28 +02002721 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002722
2723 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002724 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002725 } else {
2726 local_parent = parent;
2727 }
2728
Radek Krejci22ebdba2019-07-25 13:59:43 +02002729 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002730 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002731 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2732 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002733 break;
2734 }
2735 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002736
2737 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02002738 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002739 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2740 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002741 }
2742 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002743
2744 if (dup) {
2745 *dup = first;
2746 }
2747 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002748
2749error:
2750 if (top) {
2751 lyd_free_tree(top);
2752 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002753 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002754 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002755 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002756}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002757
Michal Vasko3a41dff2020-07-15 14:30:28 +02002758API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002759lyd_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 +02002760{
2761 return lyd_dup(node, parent, options, 1, dup);
2762}
2763
2764API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002765lyd_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 +02002766{
2767 return lyd_dup(node, parent, options, 0, dup);
2768}
2769
2770API LY_ERR
2771lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002772{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002773 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002774 struct lyd_meta *mt, *last;
2775
Michal Vasko3a41dff2020-07-15 14:30:28 +02002776 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002777
2778 /* create a copy */
2779 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002780 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002781 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002782 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002783 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
2784 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002785
2786 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002787 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002788 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002789 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002790 last->next = mt;
2791 } else {
2792 node->meta = mt;
2793 }
2794
Radek Krejci011e4aa2020-09-04 15:22:31 +02002795finish:
2796 if (ret) {
2797 lyd_free_meta_single(mt);
2798 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002799 *dup = mt;
2800 }
2801 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002802}
2803
Michal Vasko4490d312020-06-16 13:08:55 +02002804/**
2805 * @brief Merge a source sibling into target siblings.
2806 *
2807 * @param[in,out] first_trg First target sibling, is updated if top-level.
2808 * @param[in] parent_trg Target parent.
2809 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2810 * @param[in] options Merge options.
2811 * @return LY_ERR value.
2812 */
2813static LY_ERR
2814lyd_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 +02002815 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02002816{
2817 LY_ERR ret;
2818 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002819 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002820 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002821
2822 sibling_src = *sibling_src_p;
2823 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2824 /* try to find the exact instance */
2825 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2826 } else {
2827 /* try to simply find the node, there cannot be more instances */
2828 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2829 }
2830
2831 if (!ret) {
2832 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002833 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002834 /* since they are different, they cannot both be default */
2835 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2836
Michal Vasko3a41dff2020-07-15 14:30:28 +02002837 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2838 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002839 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002840 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2841 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko4490d312020-06-16 13:08:55 +02002842 &((struct lyd_node_term *)match_trg)->value));
2843
2844 /* copy flags and add LYD_NEW */
2845 match_trg->flags = sibling_src->flags | LYD_NEW;
2846 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002847 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002848 /* update value */
2849 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002850 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002851
2852 /* copy flags and add LYD_NEW */
2853 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002854 } else {
2855 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02002856 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002857 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2858 }
2859 }
2860 } else {
2861 /* node not found, merge it */
2862 if (options & LYD_MERGE_DESTRUCT) {
2863 dup_src = (struct lyd_node *)sibling_src;
2864 lyd_unlink_tree(dup_src);
2865 /* spend it */
2866 *sibling_src_p = NULL;
2867 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002868 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 +02002869 }
2870
2871 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002872 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002873 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002874 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002875 }
2876
2877 lyd_insert_node(parent_trg, first_trg, dup_src);
2878 }
2879
2880 return LY_SUCCESS;
2881}
2882
Michal Vasko3a41dff2020-07-15 14:30:28 +02002883static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002884lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002885{
2886 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02002887 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02002888
2889 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2890
2891 if (!source) {
2892 /* nothing to merge */
2893 return LY_SUCCESS;
2894 }
2895
2896 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002897 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002898 return LY_EINVAL;
2899 }
2900
2901 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02002902 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002903 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2904 if (first && !sibling_src) {
2905 /* source was spent (unlinked), move to the next node */
2906 source = tmp;
2907 }
2908
Michal Vasko3a41dff2020-07-15 14:30:28 +02002909 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002910 break;
2911 }
2912 }
2913
2914 if (options & LYD_MERGE_DESTRUCT) {
2915 /* free any leftover source data that were not merged */
2916 lyd_free_siblings((struct lyd_node *)source);
2917 }
2918
2919 return LY_SUCCESS;
2920}
2921
Michal Vasko3a41dff2020-07-15 14:30:28 +02002922API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002923lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002924{
2925 return lyd_merge(target, source, options, 1);
2926}
2927
2928API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002929lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002930{
2931 return lyd_merge(target, source, options, 0);
2932}
2933
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002934static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002935lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002936{
Michal Vasko14654712020-02-06 08:35:21 +01002937 /* ending \0 */
2938 ++reqlen;
2939
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002940 if (reqlen > *buflen) {
2941 if (is_static) {
2942 return LY_EINCOMPLETE;
2943 }
2944
2945 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2946 if (!*buffer) {
2947 return LY_EMEM;
2948 }
2949
2950 *buflen = reqlen;
2951 }
2952
2953 return LY_SUCCESS;
2954}
2955
Michal Vaskod59035b2020-07-08 12:00:06 +02002956LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002957lyd_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 +02002958{
2959 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002960 size_t len;
2961 const char *val;
2962 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002963
Radek Krejcia1c1e542020-09-29 16:06:52 +02002964 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002965 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002966 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002967 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002968
2969 quot = '\'';
2970 if (strchr(val, '\'')) {
2971 quot = '"';
2972 }
2973 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002974 }
2975
2976 return LY_SUCCESS;
2977}
2978
2979/**
2980 * @brief Append leaf-list value predicate to path.
2981 *
2982 * @param[in] node Node to print.
2983 * @param[in,out] buffer Buffer to print to.
2984 * @param[in,out] buflen Current buffer length.
2985 * @param[in,out] bufused Current number of characters used in @p buffer.
2986 * @param[in] is_static Whether buffer is static or can be reallocated.
2987 * @return LY_ERR
2988 */
2989static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002990lyd_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 +02002991{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002992 size_t len;
2993 const char *val;
2994 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002995
Michal Vaskob7be7a82020-08-20 09:09:04 +02002996 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002997 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002998 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002999
3000 quot = '\'';
3001 if (strchr(val, '\'')) {
3002 quot = '"';
3003 }
3004 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3005
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003006 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003007}
3008
3009/**
3010 * @brief Append node position (relative to its other instances) predicate to path.
3011 *
3012 * @param[in] node Node to print.
3013 * @param[in,out] buffer Buffer to print to.
3014 * @param[in,out] buflen Current buffer length.
3015 * @param[in,out] bufused Current number of characters used in @p buffer.
3016 * @param[in] is_static Whether buffer is static or can be reallocated.
3017 * @return LY_ERR
3018 */
3019static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003020lyd_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 +02003021{
3022 const struct lyd_node *first, *iter;
3023 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003024 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003025 char *val = NULL;
3026 LY_ERR rc;
3027
3028 if (node->parent) {
3029 first = node->parent->child;
3030 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003031 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003032 }
3033 pos = 1;
3034 for (iter = first; iter != node; iter = iter->next) {
3035 if (iter->schema == node->schema) {
3036 ++pos;
3037 }
3038 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003039 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003040 return LY_EMEM;
3041 }
3042
3043 len = 1 + strlen(val) + 1;
3044 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3045 if (rc != LY_SUCCESS) {
3046 goto cleanup;
3047 }
3048
3049 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3050
3051cleanup:
3052 free(val);
3053 return rc;
3054}
3055
3056API char *
3057lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3058{
Radek Krejci857189e2020-09-01 13:26:36 +02003059 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003060 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003061 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003062 const struct lyd_node *iter;
3063 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003064 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003065
3066 LY_CHECK_ARG_RET(NULL, node, NULL);
3067 if (buffer) {
3068 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3069 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003070 } else {
3071 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003072 }
3073
3074 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003075 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003076 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003077 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003078 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3079 ++depth;
3080 }
3081
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003082 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003083 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003084 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003085 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003086iter_print:
3087 /* print prefix and name */
3088 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003089 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003090 mod = iter->schema->module;
3091 }
3092
3093 /* realloc string */
Michal Vasko22df3f02020-08-24 13:29:22 +02003094 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 +02003095 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3096 if (rc != LY_SUCCESS) {
3097 break;
3098 }
3099
3100 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003101 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vasko22df3f02020-08-24 13:29:22 +02003102 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003103
Michal Vasko790b2bc2020-08-03 13:35:06 +02003104 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003105 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003106 switch (iter->schema->nodetype) {
3107 case LYS_LIST:
3108 if (iter->schema->flags & LYS_KEYLESS) {
3109 /* print its position */
3110 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3111 } else {
3112 /* print all list keys in predicates */
3113 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3114 }
3115 break;
3116 case LYS_LEAFLIST:
3117 if (iter->schema->flags & LYS_CONFIG_W) {
3118 /* print leaf-list value */
3119 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3120 } else {
3121 /* print its position */
3122 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3123 }
3124 break;
3125 default:
3126 /* nothing to print more */
3127 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003128 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003129 }
3130 if (rc != LY_SUCCESS) {
3131 break;
3132 }
3133
Michal Vasko14654712020-02-06 08:35:21 +01003134 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003135 }
3136 break;
3137 }
3138
3139 return buffer;
3140}
Michal Vaskoe444f752020-02-10 12:20:06 +01003141
Michal Vasko25a32822020-07-09 15:48:22 +02003142API struct lyd_meta *
3143lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3144{
3145 struct lyd_meta *ret = NULL;
3146 const struct ly_ctx *ctx;
3147 const char *prefix, *tmp;
3148 char *str;
3149 size_t pref_len, name_len;
3150
3151 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3152
3153 if (!first) {
3154 return NULL;
3155 }
3156
3157 ctx = first->annotation->module->ctx;
3158
3159 /* parse the name */
3160 tmp = name;
3161 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3162 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3163 return NULL;
3164 }
3165
3166 /* find the module */
3167 if (prefix) {
3168 str = strndup(prefix, pref_len);
3169 module = ly_ctx_get_module_latest(ctx, str);
3170 free(str);
3171 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3172 }
3173
3174 /* find the metadata */
3175 LY_LIST_FOR(first, first) {
3176 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3177 ret = (struct lyd_meta *)first;
3178 break;
3179 }
3180 }
3181
3182 return ret;
3183}
3184
Michal Vasko9b368d32020-02-14 13:53:31 +01003185API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003186lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3187{
3188 struct lyd_node **match_p;
3189 struct lyd_node_inner *parent;
3190
Michal Vaskof03ed032020-03-04 13:31:44 +01003191 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003192
Michal Vasko62ed12d2020-05-21 10:08:25 +02003193 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3194 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003195 if (match) {
3196 *match = NULL;
3197 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003198 return LY_ENOTFOUND;
3199 }
3200
3201 /* find first sibling */
3202 if (siblings->parent) {
3203 siblings = siblings->parent->child;
3204 } else {
3205 while (siblings->prev->next) {
3206 siblings = siblings->prev;
3207 }
3208 }
3209
3210 parent = (struct lyd_node_inner *)siblings->parent;
3211 if (parent && parent->children_ht) {
3212 assert(target->hash);
3213
3214 /* find by hash */
3215 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003216 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003217 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003218 siblings = *match_p;
3219 } else {
3220 siblings = NULL;
3221 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003222 } else {
3223 /* not found */
3224 siblings = NULL;
3225 }
3226 } else {
3227 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003228 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003229 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003230 break;
3231 }
3232 }
3233 }
3234
3235 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003236 if (match) {
3237 *match = NULL;
3238 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003239 return LY_ENOTFOUND;
3240 }
3241
Michal Vasko9b368d32020-02-14 13:53:31 +01003242 if (match) {
3243 *match = (struct lyd_node *)siblings;
3244 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003245 return LY_SUCCESS;
3246}
3247
Radek Krejci857189e2020-09-01 13:26:36 +02003248/**
3249 * @brief Comparison callback to match schema node with a schema of a data node.
3250 *
3251 * @param[in] val1_p Pointer to the schema node
3252 * @param[in] val2_p Pointer to the data node
3253 * Implementation of ::values_equal_cb.
3254 */
3255static ly_bool
3256lyd_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 +01003257{
3258 struct lysc_node *val1;
3259 struct lyd_node *val2;
3260
3261 val1 = *((struct lysc_node **)val1_p);
3262 val2 = *((struct lyd_node **)val2_p);
3263
Michal Vasko90932a92020-02-12 14:33:03 +01003264 if (val1 == val2->schema) {
3265 /* schema match is enough */
3266 return 1;
3267 } else {
3268 return 0;
3269 }
3270}
3271
3272static LY_ERR
3273lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3274{
3275 struct lyd_node **match_p;
3276 struct lyd_node_inner *parent;
3277 uint32_t hash;
3278 values_equal_cb ht_cb;
3279
Michal Vaskob104f112020-07-17 09:54:54 +02003280 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003281
3282 parent = (struct lyd_node_inner *)siblings->parent;
3283 if (parent && parent->children_ht) {
3284 /* calculate our hash */
3285 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3286 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3287 hash = dict_hash_multi(hash, NULL, 0);
3288
3289 /* use special hash table function */
3290 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3291
3292 /* find by hash */
3293 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3294 siblings = *match_p;
3295 } else {
3296 /* not found */
3297 siblings = NULL;
3298 }
3299
3300 /* set the original hash table compare function back */
3301 lyht_set_cb(parent->children_ht, ht_cb);
3302 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003303 /* find first sibling */
3304 if (siblings->parent) {
3305 siblings = siblings->parent->child;
3306 } else {
3307 while (siblings->prev->next) {
3308 siblings = siblings->prev;
3309 }
3310 }
3311
3312 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003313 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003314 if (siblings->schema == schema) {
3315 /* schema match is enough */
3316 break;
3317 }
3318 }
3319 }
3320
3321 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003322 if (match) {
3323 *match = NULL;
3324 }
Michal Vasko90932a92020-02-12 14:33:03 +01003325 return LY_ENOTFOUND;
3326 }
3327
Michal Vasko9b368d32020-02-14 13:53:31 +01003328 if (match) {
3329 *match = (struct lyd_node *)siblings;
3330 }
Michal Vasko90932a92020-02-12 14:33:03 +01003331 return LY_SUCCESS;
3332}
3333
Michal Vaskoe444f752020-02-10 12:20:06 +01003334API LY_ERR
3335lyd_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 +02003336 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003337{
3338 LY_ERR rc;
3339 struct lyd_node *target = NULL;
3340
Michal Vasko4c583e82020-07-17 12:16:14 +02003341 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003342
Michal Vasko62ed12d2020-05-21 10:08:25 +02003343 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3344 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003345 if (match) {
3346 *match = NULL;
3347 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003348 return LY_ENOTFOUND;
3349 }
3350
Michal Vaskof03ed032020-03-04 13:31:44 +01003351 if (key_or_value && !val_len) {
3352 val_len = strlen(key_or_value);
3353 }
3354
Michal Vaskob104f112020-07-17 09:54:54 +02003355 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3356 /* create a data node and find the instance */
3357 if (schema->nodetype == LYS_LEAFLIST) {
3358 /* target used attributes: schema, hash, value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003359 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, LY_PREF_JSON, NULL, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003360 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3361 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003362 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003363 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003364 }
3365
3366 /* find it */
3367 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003368 } else {
3369 /* find the first schema node instance */
3370 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003371 }
3372
Michal Vaskoe444f752020-02-10 12:20:06 +01003373 lyd_free_tree(target);
3374 return rc;
3375}
Michal Vaskoccc02342020-05-21 10:09:21 +02003376
3377API LY_ERR
3378lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3379{
3380 LY_ERR ret = LY_SUCCESS;
3381 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003382 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003383 uint32_t i;
3384
3385 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3386
3387 memset(&xp_set, 0, sizeof xp_set);
3388
3389 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003390 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3391 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003392
3393 /* evaluate expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003394 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 +02003395 LY_CHECK_GOTO(ret, cleanup);
3396
3397 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003398 ret = ly_set_new(set);
3399 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003400
3401 /* transform into ly_set */
3402 if (xp_set.type == LYXP_SET_NODE_SET) {
3403 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3404 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003405 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003406 (*set)->size = xp_set.used;
3407
3408 for (i = 0; i < xp_set.used; ++i) {
3409 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003410 ret = ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST, NULL);
3411 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003412 }
3413 }
3414 }
3415
3416cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003417 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003418 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003419 return ret;
3420}