blob: 8e5fed4881146a5e48c413e4f7d4aa38dc19be47 [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"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "plugins_exts.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Michal Vasko69730152020-10-09 16:30:07 +020043#include "plugins_exts_metadata.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
48#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
59 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
60 const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Michal Vasko25d6ad02020-10-22 12:20:22 +020064 uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vaskofeca4fb2020-10-05 08:58:40 +020066 if (incomplete) {
67 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020068 }
69
Michal Vaskofeca4fb2020-10-05 08:58:40 +020070 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010071 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020072 if (incomplete) {
73 *incomplete = 1;
74 }
75 } else if (ret) {
76 if (err) {
77 LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg);
78 ly_err_free(err);
79 } else {
80 LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
81 }
82 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010083 }
84
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 if (dynamic) {
86 *dynamic = 0;
87 }
88 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010089}
90
Radek Krejci38d85362019-09-05 16:26:38 +020091LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
93 const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
Radek Krejci38d85362019-09-05 16:26:38 +020094{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020095 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +020096 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +020097
Michal Vaskofeca4fb2020-10-05 08:58:40 +020098 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +010099
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200100 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
101 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200102 if (err) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200103 LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg);
Radek Krejci38d85362019-09-05 16:26:38 +0200104 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200105 } else {
106 LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
Radek Krejci38d85362019-09-05 16:26:38 +0200107 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200108 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200109 }
110
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200111 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200112}
113
Michal Vaskof937cfe2020-08-03 16:07:12 +0200114LY_ERR
115_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 +0200116 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200117{
118 LY_ERR rc = LY_SUCCESS;
119 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200120 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200121 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200122
123 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
124
125 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
126 LOGARG(ctx, node);
127 return LY_EINVAL;
128 }
129
Michal Vasko22df3f02020-08-24 13:29:22 +0200130 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200131 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
132 LYD_HINT_SCHEMA, node, &storage, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200133 if (rc == LY_EINCOMPLETE) {
134 /* actually success since we do not provide the context tree and call validation with
135 * LY_TYPE_OPTS_INCOMPLETE_DATA */
136 rc = LY_SUCCESS;
137 } else if (rc && err) {
138 if (ctx) {
139 /* log only in case the ctx was provided as input parameter */
140 ly_err_print(err);
141 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200142 }
Radek Krejci73dead22019-07-11 16:46:16 +0200143 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200144 }
145
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200146 if (!rc) {
147 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
148 }
Radek Krejci084289f2019-07-09 17:35:30 +0200149 return rc;
150}
151
152API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200153lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
154{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200155 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200156}
157
158API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100159lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200160 const struct lyd_node *tree, const struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200161{
162 LY_ERR rc;
163 struct ly_err_item *err = NULL;
164 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200165 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200166 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200167
168 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
169
Michal Vasko22df3f02020-08-24 13:29:22 +0200170 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200171 /* store */
172 rc = type->plugin->store(ctx ? ctx : LYD_CTX(node), type, value, value_len, 0, LY_PREF_JSON, NULL,
173 LYD_HINT_DATA, node->schema, &val, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200174 if (rc == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200175 stored = 1;
176
177 /* resolve */
178 rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, (struct lyd_node *)node, tree, &val, &err);
179 }
180
181 if (rc) {
Radek Krejci73dead22019-07-11 16:46:16 +0200182 if (err) {
183 if (ctx) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200184 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200185 }
Radek Krejci73dead22019-07-11 16:46:16 +0200186 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200187 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200188 if (stored) {
189 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
190 }
Radek Krejci73dead22019-07-11 16:46:16 +0200191 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200192 }
193
Michal Vasko3701af52020-08-03 14:29:38 +0200194 if (realtype) {
195 *realtype = val.realtype;
196 }
197
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200198 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200199 return LY_SUCCESS;
200}
201
202API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200203lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200204{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200205 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200206 struct ly_ctx *ctx;
207 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200208 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200209
210 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
211
212 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200213 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200214
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200215 /* store the value */
216 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema,
217 NULL, LY_VLOG_LYSC, node->schema);
218 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200219
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200220 /* compare values */
221 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200222
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200223 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200224 return ret;
225}
226
Radek Krejci19611252020-10-04 13:54:53 +0200227API ly_bool
228lyd_is_default(const struct lyd_node *node)
229{
230 const struct lysc_node_leaf *leaf;
231 const struct lysc_node_leaflist *llist;
232 const struct lyd_node_term *term;
233 LY_ARRAY_COUNT_TYPE u;
234
235 assert(node->schema->nodetype & LYD_NODE_TERM);
236 term = (const struct lyd_node_term *)node;
237
238 if (node->schema->nodetype == LYS_LEAF) {
239 leaf = (const struct lysc_node_leaf *)node->schema;
240 if (!leaf->dflt) {
241 return 0;
242 }
243
244 /* compare with the default value */
245 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
246 return 0;
247 }
248 } else {
249 llist = (const struct lysc_node_leaflist *)node->schema;
250 if (!llist->dflts) {
251 return 0;
252 }
253
254 LY_ARRAY_FOR(llist->dflts, u) {
255 /* compare with each possible default value */
256 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
257 return 0;
258 }
259 }
260 }
261
262 return 1;
263}
264
Radek Krejci7931b192020-06-25 17:05:03 +0200265static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200266lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200267{
Michal Vasko69730152020-10-09 16:30:07 +0200268 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200269 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200270 const char *path = in->method.fpath.filepath;
271 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200272
273 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200274 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200275
Michal Vasko69730152020-10-09 16:30:07 +0200276 if ((len >= 5) && !strncmp(&path[len - 4], ".xml", 4)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200277 format = LYD_XML;
Michal Vasko69730152020-10-09 16:30:07 +0200278 } else if ((len >= 6) && !strncmp(&path[len - 5], ".json", 5)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 format = LYD_JSON;
Michal Vasko69730152020-10-09 16:30:07 +0200280 } else if ((len >= 5) && !strncmp(&path[len - 4], ".lyb", 4)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200282 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200283 }
284
Radek Krejci7931b192020-06-25 17:05:03 +0200285 return format;
286}
Radek Krejcie7b95092019-05-15 11:03:07 +0200287
Radek Krejci7931b192020-06-25 17:05:03 +0200288API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200289lyd_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 +0200290 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200291{
Radek Krejci1798aae2020-07-14 13:26:06 +0200292 LY_ERR ret = LY_SUCCESS;
293 struct lyd_ctx *lydctx = NULL;
294
Radek Krejci7931b192020-06-25 17:05:03 +0200295 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
296 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
297 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
298
299 format = lyd_parse_get_format(in, format);
300 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
301
Radek Krejci1798aae2020-07-14 13:26:06 +0200302 /* init */
303 *tree = NULL;
304
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 /* remember input position */
306 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200307
308 switch (format) {
309 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200310 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
311 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200312 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200313 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
314 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200315 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200316 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
317 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200318 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200319 LOGINT_RET(ctx);
320 }
321
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 if (!(parse_options & LYD_PARSE_ONLY)) {
323 uint32_t i = 0;
324 const struct lys_module *mod;
325 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200326
Radek Krejci1798aae2020-07-14 13:26:06 +0200327 next = *tree;
328 while (1) {
329 if (validate_options & LYD_VALIDATE_PRESENT) {
330 mod = lyd_data_next_module(&next, &first);
331 } else {
332 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
333 }
334 if (!mod) {
335 break;
336 }
337 if (first == *tree) {
338 /* make sure first2 changes are carried to tree */
339 first2 = tree;
340 } else {
341 first2 = &first;
342 }
343
344 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
345 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
346
347 /* add all top-level defaults for this module */
348 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
Michal Vasko69730152020-10-09 16:30:07 +0200349 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200350 LY_CHECK_GOTO(ret, cleanup);
351
352 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200353 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200354 LY_CHECK_GOTO(ret, cleanup);
355
356 /* perform final validation that assumes the data tree is final */
357 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
358 }
359 }
360
361cleanup:
Michal Vaskoafac7822020-10-20 14:22:26 +0200362 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200363 if (ret) {
364 lyd_free_all(*tree);
365 *tree = NULL;
366 }
367 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200368}
369
370API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200371lyd_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 +0200372 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200373{
374 LY_ERR ret;
375 struct ly_in *in;
376
377 LY_CHECK_RET(ly_in_new_memory(data, &in));
378 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
379
380 ly_in_free(in, 0);
381 return ret;
382}
383
384API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200385lyd_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 +0200386 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200387{
388 LY_ERR ret;
389 struct ly_in *in;
390
391 LY_CHECK_RET(ly_in_new_fd(fd, &in));
392 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
393
394 ly_in_free(in, 0);
395 return ret;
396}
397
398API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200399lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
400 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200401{
402 LY_ERR ret;
403 struct ly_in *in;
404
405 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
406 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
407
408 ly_in_free(in, 0);
409 return ret;
410}
411
Radek Krejci7931b192020-06-25 17:05:03 +0200412API LY_ERR
413lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
414{
415 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
416
417 format = lyd_parse_get_format(in, format);
418 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
419
Radek Krejci1798aae2020-07-14 13:26:06 +0200420 /* init */
421 *tree = NULL;
422 if (op) {
423 *op = NULL;
424 }
425
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 /* remember input position */
427 in->func_start = in->current;
428
Radek Krejci7931b192020-06-25 17:05:03 +0200429 switch (format) {
430 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200432 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200433 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200434 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200435 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200436 case LYD_UNKNOWN:
437 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200438 }
439
440 LOGINT_RET(ctx);
441}
442
443API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200444lyd_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 +0200445 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200446{
447 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200448 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200449
450 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200451 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200452
Radek Krejci1798aae2020-07-14 13:26:06 +0200453 /* init */
454 if (tree) {
455 *tree = NULL;
456 }
457 if (op) {
458 *op = NULL;
459 }
460
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 /* remember input position */
462 in->func_start = in->current;
463
Radek Krejci7931b192020-06-25 17:05:03 +0200464 switch (format) {
465 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200467 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200469 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200471 case LYD_UNKNOWN:
472 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200473 }
474
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200476}
477
478API LY_ERR
479lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
480{
Radek Krejci1798aae2020-07-14 13:26:06 +0200481 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200482
483 format = lyd_parse_get_format(in, format);
484 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
485
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 /* init */
487 if (tree) {
488 *tree = NULL;
489 }
490 if (ntf) {
491 *ntf = NULL;
492 }
493
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 /* remember input position */
495 in->func_start = in->current;
496
Radek Krejci7931b192020-06-25 17:05:03 +0200497 switch (format) {
498 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200499 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200500 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200502 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200503 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200504 case LYD_UNKNOWN:
505 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200506 }
507
508 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200509}
Radek Krejci084289f2019-07-09 17:35:30 +0200510
Michal Vasko90932a92020-02-12 14:33:03 +0100511LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200512lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
513 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100514{
515 LY_ERR ret;
516 struct lyd_node_term *term;
517
Michal Vasko9b368d32020-02-14 13:53:31 +0100518 assert(schema->nodetype & LYD_NODE_TERM);
519
Michal Vasko90932a92020-02-12 14:33:03 +0100520 term = calloc(1, sizeof *term);
521 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
522
523 term->schema = schema;
524 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100525 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100526
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200527 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
528 value_len, dynamic, format, prefix_data, hints, schema, incomplete, LY_VLOG_LYSC, schema);
529 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9b368d32020-02-14 13:53:31 +0100530 lyd_hash((struct lyd_node *)term);
531
532 *node = (struct lyd_node *)term;
533 return ret;
534}
535
536LY_ERR
537lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
538{
539 LY_ERR ret;
540 struct lyd_node_term *term;
541 struct lysc_type *type;
542
543 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200544 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100545
546 term = calloc(1, sizeof *term);
547 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
548
549 term->schema = schema;
550 term->prev = (struct lyd_node *)term;
551 term->flags = LYD_NEW;
552
553 type = ((struct lysc_node_leaf *)schema)->type;
554 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
555 if (ret) {
556 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
557 free(term);
558 return ret;
559 }
560 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100561
562 *node = (struct lyd_node *)term;
563 return ret;
564}
565
566LY_ERR
567lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
568{
569 struct lyd_node_inner *in;
570
Michal Vasko9b368d32020-02-14 13:53:31 +0100571 assert(schema->nodetype & LYD_NODE_INNER);
572
Michal Vasko90932a92020-02-12 14:33:03 +0100573 in = calloc(1, sizeof *in);
574 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
575
576 in->schema = schema;
577 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100578 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100579
Michal Vasko9b368d32020-02-14 13:53:31 +0100580 /* do not hash list with keys, we need them for the hash */
581 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
582 lyd_hash((struct lyd_node *)in);
583 }
Michal Vasko90932a92020-02-12 14:33:03 +0100584
585 *node = (struct lyd_node *)in;
586 return LY_SUCCESS;
587}
588
Michal Vasko90932a92020-02-12 14:33:03 +0100589LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200590lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100591{
592 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100593 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200594 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100595
Michal Vasko004d3152020-06-11 19:59:22 +0200596 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100597
598 /* create list */
599 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
600
Michal Vasko90932a92020-02-12 14:33:03 +0100601 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200602 LY_ARRAY_FOR(predicates, u) {
603 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100604 lyd_insert_node(list, NULL, key);
605 }
606
Michal Vasko9b368d32020-02-14 13:53:31 +0100607 /* hash having all the keys */
608 lyd_hash(list);
609
Michal Vasko90932a92020-02-12 14:33:03 +0100610 /* success */
611 *node = list;
612 list = NULL;
613
614cleanup:
615 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200616 return ret;
617}
618
619static LY_ERR
620lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
621{
622 LY_ERR ret = LY_SUCCESS;
623 struct lyxp_expr *expr = NULL;
624 uint16_t exp_idx = 0;
625 enum ly_path_pred_type pred_type = 0;
626 struct ly_path_predicate *predicates = NULL;
627
628 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200629 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko69730152020-10-09 16:30:07 +0200630 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200631
632 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200633 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
634 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200635
636 /* create the list node */
637 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
638
639cleanup:
640 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200641 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100642 return ret;
643}
644
645LY_ERR
646lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
647{
648 struct lyd_node_any *any;
649
Michal Vasko9b368d32020-02-14 13:53:31 +0100650 assert(schema->nodetype & LYD_NODE_ANY);
651
Michal Vasko90932a92020-02-12 14:33:03 +0100652 any = calloc(1, sizeof *any);
653 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
654
655 any->schema = schema;
656 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100657 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100658
Radek Krejci1798aae2020-07-14 13:26:06 +0200659 /* TODO: convert XML/JSON strings into a opaq data tree */
660 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100661 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100662 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100663
664 *node = (struct lyd_node *)any;
665 return LY_SUCCESS;
666}
667
Michal Vasko52927e22020-03-16 17:26:14 +0100668LY_ERR
669lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200670 ly_bool *dynamic, LYD_FORMAT format, uint32_t hints, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200671 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100672{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200673 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100674 struct lyd_node_opaq *opaq;
675
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200676 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100677
678 if (!value_len) {
679 value = "";
680 }
681
682 opaq = calloc(1, sizeof *opaq);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200683 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vasko52927e22020-03-16 17:26:14 +0100684
685 opaq->prev = (struct lyd_node *)opaq;
Radek Krejcid46e46a2020-09-15 14:22:42 +0200686 opaq->val_prefs = val_prefs;
687 opaq->format = format;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200688 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
689
Michal Vasko52927e22020-03-16 17:26:14 +0100690 if (pref_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200691 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100692 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200694 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200695 }
696
Michal Vasko52927e22020-03-16 17:26:14 +0100697 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200698 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100699 *dynamic = 0;
700 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200701 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100702 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200703 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100704 opaq->ctx = ctx;
705
Radek Krejci011e4aa2020-09-04 15:22:31 +0200706finish:
707 if (ret) {
708 lyd_free_tree((struct lyd_node *)opaq);
709 } else {
710 *node = (struct lyd_node *)opaq;
711 }
712 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100713}
714
Michal Vasko3a41dff2020-07-15 14:30:28 +0200715API LY_ERR
716lyd_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 +0100717{
718 struct lyd_node *ret = NULL;
719 const struct lysc_node *schema;
720 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
721
Michal Vasko6027eb92020-07-15 16:37:30 +0200722 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100723
Michal Vaskof03ed032020-03-04 13:31:44 +0100724 if (!module) {
725 module = parent->schema->module;
726 }
727
Michal Vasko3a41dff2020-07-15 14:30:28 +0200728 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Michal Vasko69730152020-10-09 16:30:07 +0200729 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200730 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 +0100731
Michal Vasko3a41dff2020-07-15 14:30:28 +0200732 LY_CHECK_RET(lyd_create_inner(schema, &ret));
733 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100734 lyd_insert_node(parent, NULL, ret);
735 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200736
737 if (node) {
738 *node = ret;
739 }
740 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100741}
742
Michal Vasko3a41dff2020-07-15 14:30:28 +0200743API LY_ERR
744lyd_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 +0100745{
746 struct lyd_node *ret = NULL, *key;
747 const struct lysc_node *schema, *key_s;
748 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
749 va_list ap;
750 const char *key_val;
751 LY_ERR rc = LY_SUCCESS;
752
Michal Vasko6027eb92020-07-15 16:37:30 +0200753 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100754
Michal Vaskof03ed032020-03-04 13:31:44 +0100755 if (!module) {
756 module = parent->schema->module;
757 }
758
Michal Vasko013a8182020-03-03 10:46:53 +0100759 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200760 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100761
762 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200763 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100764
Michal Vasko3a41dff2020-07-15 14:30:28 +0200765 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100766
767 /* create and insert all the keys */
768 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
769 key_val = va_arg(ap, const char *);
770
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200771 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
772 NULL, &key);
773 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100774 lyd_insert_node(ret, NULL, key);
775 }
776
Michal Vasko013a8182020-03-03 10:46:53 +0100777 if (parent) {
778 lyd_insert_node(parent, NULL, ret);
779 }
780
781cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200782 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100783 if (rc) {
784 lyd_free_tree(ret);
785 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 } else if (node) {
787 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100788 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100790}
791
Michal Vasko3a41dff2020-07-15 14:30:28 +0200792API LY_ERR
793lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200794 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100795{
796 struct lyd_node *ret = NULL;
797 const struct lysc_node *schema;
798 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
799
Michal Vasko6027eb92020-07-15 16:37:30 +0200800 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100801
Michal Vaskof03ed032020-03-04 13:31:44 +0100802 if (!module) {
803 module = parent->schema->module;
804 }
Michal Vasko004d3152020-06-11 19:59:22 +0200805 if (!keys) {
806 keys = "";
807 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100808
Michal Vasko004d3152020-06-11 19:59:22 +0200809 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100810 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200811 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100812
Michal Vasko004d3152020-06-11 19:59:22 +0200813 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
814 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200815 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200816 } else {
817 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200818 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200819 }
Michal Vasko004d3152020-06-11 19:59:22 +0200820 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100821 lyd_insert_node(parent, NULL, ret);
822 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200823
824 if (node) {
825 *node = ret;
826 }
827 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100828}
829
Michal Vasko3a41dff2020-07-15 14:30:28 +0200830API LY_ERR
831lyd_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 +0200832 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100833{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200834 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100835 struct lyd_node *ret = NULL;
836 const struct lysc_node *schema;
837 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
838
Michal Vasko6027eb92020-07-15 16:37:30 +0200839 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100840
Michal Vaskof03ed032020-03-04 13:31:44 +0100841 if (!module) {
842 module = parent->schema->module;
843 }
844
Michal Vasko013a8182020-03-03 10:46:53 +0100845 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200846 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100847
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200848 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL,
849 &ret);
850 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200851 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100852 lyd_insert_node(parent, NULL, ret);
853 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200854
855 if (node) {
856 *node = ret;
857 }
858 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100859}
860
Michal Vasko3a41dff2020-07-15 14:30:28 +0200861API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100862lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200863 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100864{
865 struct lyd_node *ret = NULL;
866 const struct lysc_node *schema;
867 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
868
Michal Vasko6027eb92020-07-15 16:37:30 +0200869 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100870
Michal Vaskof03ed032020-03-04 13:31:44 +0100871 if (!module) {
872 module = parent->schema->module;
873 }
874
Michal Vasko013a8182020-03-03 10:46:53 +0100875 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200876 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100877
Michal Vasko3a41dff2020-07-15 14:30:28 +0200878 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
879 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100880 lyd_insert_node(parent, NULL, ret);
881 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882
883 if (node) {
884 *node = ret;
885 }
886 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100887}
888
Michal Vasko4490d312020-06-16 13:08:55 +0200889/**
890 * @brief Update node value.
891 *
892 * @param[in] node Node to update.
893 * @param[in] value New value to set.
894 * @param[in] value_type Type of @p value for any node.
895 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
896 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
897 * @return LY_ERR value.
898 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200899static LY_ERR
900lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200901 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200902{
903 LY_ERR ret = LY_SUCCESS;
904 struct lyd_node *new_any;
905
906 switch (node->schema->nodetype) {
907 case LYS_CONTAINER:
908 case LYS_NOTIF:
909 case LYS_RPC:
910 case LYS_ACTION:
911 case LYS_LIST:
912 case LYS_LEAFLIST:
913 /* if it exists, there is nothing to update */
914 *new_parent = NULL;
915 *new_node = NULL;
916 break;
917 case LYS_LEAF:
918 ret = lyd_change_term(node, value);
919 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
920 /* there was an actual change (at least of the default flag) */
921 *new_parent = node;
922 *new_node = node;
923 ret = LY_SUCCESS;
924 } else if (ret == LY_ENOT) {
925 /* no change */
926 *new_parent = NULL;
927 *new_node = NULL;
928 ret = LY_SUCCESS;
929 } /* else error */
930 break;
931 case LYS_ANYDATA:
932 case LYS_ANYXML:
933 /* create a new any node */
934 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
935
936 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200937 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200938 /* not equal, switch values (so that we can use generic node free) */
939 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
940 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
941 ((struct lyd_node_any *)node)->value.str = value;
942 ((struct lyd_node_any *)node)->value_type = value_type;
943
944 *new_parent = node;
945 *new_node = node;
946 } else {
947 /* they are equal */
948 *new_parent = NULL;
949 *new_node = NULL;
950 }
951 lyd_free_tree(new_any);
952 break;
953 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200954 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200955 ret = LY_EINT;
956 break;
957 }
958
959 return ret;
960}
961
Michal Vasko3a41dff2020-07-15 14:30:28 +0200962API LY_ERR
963lyd_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 +0200964 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200965{
966 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200967 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200968 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200969 size_t pref_len, name_len;
970
Michal Vasko3a41dff2020-07-15 14:30:28 +0200971 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200972
Michal Vaskob7be7a82020-08-20 09:09:04 +0200973 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200974
975 /* parse the name */
976 tmp = name;
977 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
978 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200979 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200980 }
981
982 /* find the module */
983 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +0200984 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200985 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 +0200986 }
987
988 /* set value if none */
989 if (!val_str) {
990 val_str = "";
991 }
992
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200993 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
994 NULL, LYD_HINT_DATA, NULL));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200995
996 if (meta) {
997 *meta = ret;
998 }
999 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001000}
1001
Michal Vasko3a41dff2020-07-15 14:30:28 +02001002API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001003lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001004 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001005{
1006 struct lyd_node *ret = NULL;
1007
Michal Vasko6027eb92020-07-15 16:37:30 +02001008 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001009
1010 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001011 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001012 }
1013 if (!value) {
1014 value = "";
1015 }
1016
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001017 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, 0, NULL, NULL, 0,
1018 module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001019 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001020 lyd_insert_node(parent, NULL, ret);
1021 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022
1023 if (node) {
1024 *node = ret;
1025 }
1026 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001027}
1028
Michal Vasko3a41dff2020-07-15 14:30:28 +02001029API LY_ERR
1030lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001031 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001032{
Radek Krejci1798aae2020-07-14 13:26:06 +02001033 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001034 const struct ly_ctx *ctx;
1035 const char *prefix, *tmp;
1036 size_t pref_len, name_len;
1037
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001039
Michal Vaskob7be7a82020-08-20 09:09:04 +02001040 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001041
1042 /* parse the name */
1043 tmp = name;
1044 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1045 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001046 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001047 }
1048
1049 /* set value if none */
1050 if (!val_str) {
1051 val_str = "";
1052 }
1053
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001054 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, 0, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001055 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001056
1057 if (attr) {
1058 *attr = ret;
1059 }
1060 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001061}
1062
1063API LY_ERR
1064lyd_change_term(struct lyd_node *term, const char *val_str)
1065{
1066 LY_ERR ret = LY_SUCCESS;
1067 struct lysc_type *type;
1068 struct lyd_node_term *t;
1069 struct lyd_node *parent;
1070 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001071 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001072
1073 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1074
1075 if (!val_str) {
1076 val_str = "";
1077 }
1078 t = (struct lyd_node_term *)term;
1079 type = ((struct lysc_node_leaf *)term->schema)->type;
1080
1081 /* parse the new value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001082 ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
1083 term->schema, NULL, LY_VLOG_LYD, term);
1084 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001085
1086 /* compare original and new value */
1087 if (type->plugin->compare(&t->value, &val)) {
1088 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001089 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001090 t->value = val;
1091 memset(&val, 0, sizeof val);
1092 val_change = 1;
1093 } else {
1094 val_change = 0;
1095 }
1096
1097 /* always clear the default flag */
1098 if (term->flags & LYD_DEFAULT) {
1099 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1100 parent->flags &= ~LYD_DEFAULT;
1101 }
1102 dflt_change = 1;
1103 } else {
1104 dflt_change = 0;
1105 }
1106
1107 if (val_change || dflt_change) {
1108 /* make the node non-validated */
1109 term->flags &= LYD_NEW;
1110 }
1111
1112 if (val_change) {
1113 if (term->schema->nodetype == LYS_LEAFLIST) {
1114 /* leaf-list needs to be hashed again and re-inserted into parent */
1115 lyd_unlink_hash(term);
1116 lyd_hash(term);
1117 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1118 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1119 /* list needs to be updated if its key was changed */
1120 assert(term->parent->schema->nodetype == LYS_LIST);
1121 lyd_unlink_hash((struct lyd_node *)term->parent);
1122 lyd_hash((struct lyd_node *)term->parent);
1123 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1124 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1125 }
1126
1127 /* retrun value */
1128 if (!val_change) {
1129 if (dflt_change) {
1130 /* only default flag change */
1131 ret = LY_EEXIST;
1132 } else {
1133 /* no change */
1134 ret = LY_ENOT;
1135 }
1136 } /* else value changed, LY_SUCCESS */
1137
1138cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001139 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001140 return ret;
1141}
1142
Michal Vasko41586352020-07-13 13:54:25 +02001143API LY_ERR
1144lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1145{
1146 LY_ERR ret = LY_SUCCESS;
1147 struct lyd_meta *m2;
1148 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001149 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001150
1151 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1152
1153 if (!val_str) {
1154 val_str = "";
1155 }
1156
1157 /* parse the new value into a new meta structure */
1158 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001159 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001160
1161 /* compare original and new value */
1162 if (lyd_compare_meta(meta, m2)) {
1163 /* values differ, switch them */
1164 val = meta->value;
1165 meta->value = m2->value;
1166 m2->value = val;
1167 val_change = 1;
1168 } else {
1169 val_change = 0;
1170 }
1171
1172 /* retrun value */
1173 if (!val_change) {
1174 /* no change */
1175 ret = LY_ENOT;
1176 } /* else value changed, LY_SUCCESS */
1177
1178cleanup:
1179 return ret;
1180}
1181
Michal Vasko3a41dff2020-07-15 14:30:28 +02001182API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001183lyd_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 +02001184 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001185{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001186 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001187}
1188
1189API LY_ERR
1190lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001191 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 +02001192{
1193 LY_ERR ret = LY_SUCCESS, r;
1194 struct lyxp_expr *exp = NULL;
1195 struct ly_path *p = NULL;
1196 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1197 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001198 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001199 struct ly_path_predicate *pred;
1200
1201 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1202
1203 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001204 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001205 }
1206
1207 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001208 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko69730152020-10-09 16:30:07 +02001209 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001210
1211 /* compile path */
1212 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001213 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON,
1214 NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001215
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001216 schema = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko69730152020-10-09 16:30:07 +02001217 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE) &&
Radek Krejci092e33c2020-10-12 15:33:10 +02001218 !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001219 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +02001220 lys_nodetype2str(schema->nodetype), schema->name);
Michal Vasko00cbf532020-06-15 13:58:47 +02001221 ret = LY_EINVAL;
1222 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001223 } 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 +02001224 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001225 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1226 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001227
1228 if (!value) {
1229 value = "";
1230 }
1231
1232 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001233 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001234 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001235 }
1236 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001237 ret = lyd_value_store(ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value, strlen(value),
1238 NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL, LY_VLOG_LYSC, schema);
1239 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae875662020-10-21 10:33:17 +02001240 ++((struct lysc_type *)pred->value.realtype)->refcount;
Michal Vasko00cbf532020-06-15 13:58:47 +02001241 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1242 }
1243
1244 /* try to find any existing nodes in the path */
1245 if (parent) {
1246 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1247 if (ret == LY_SUCCESS) {
1248 /* the node exists, are we supposed to update it or is it just a default? */
Radek Krejci092e33c2020-10-12 15:33:10 +02001249 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001250 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1251 ret = LY_EEXIST;
1252 goto cleanup;
1253 }
1254
1255 /* update the existing node */
1256 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1257 goto cleanup;
1258 } else if (ret == LY_EINCOMPLETE) {
1259 /* some nodes were found, adjust the iterator to the next segment */
1260 ++path_idx;
1261 } else if (ret == LY_ENOTFOUND) {
1262 /* 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 +02001263 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001264 node = parent;
1265 }
1266 } else {
1267 /* error */
1268 goto cleanup;
1269 }
1270 }
1271
1272 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001273 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001274 cur_parent = node;
1275 schema = p[path_idx].node;
1276
1277 switch (schema->nodetype) {
1278 case LYS_LIST:
1279 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001280 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001281 /* creating opaque list without keys */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001282 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1283 LYD_NODEHINT_LIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
Michal Vasko69730152020-10-09 16:30:07 +02001284 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001285 } else {
1286 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1287 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1288 }
1289 break;
1290 }
Radek Krejci0f969882020-08-21 16:56:47 +02001291 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001292 case LYS_CONTAINER:
1293 case LYS_NOTIF:
1294 case LYS_RPC:
1295 case LYS_ACTION:
1296 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1297 break;
1298 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001299 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001300 /* creating opaque leaf-list without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001301 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1302 LYD_NODEHINT_LEAFLIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1303 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001304 } else {
1305 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1306 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1307 }
1308 break;
1309 case LYS_LEAF:
1310 /* make there is some value */
1311 if (!value) {
1312 value = "";
1313 }
1314
1315 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001316 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001317 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001318 }
1319 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001320 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1321 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001322 } else {
1323 /* creating opaque leaf without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001324 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001325 0, NULL, NULL, 0, schema->module->name,
1326 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001327 }
1328 break;
1329 case LYS_ANYDATA:
1330 case LYS_ANYXML:
1331 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1332 break;
1333 default:
1334 LOGINT(ctx);
1335 ret = LY_EINT;
1336 goto cleanup;
1337 }
1338
1339 if (cur_parent) {
1340 /* connect to the parent */
1341 lyd_insert_node(cur_parent, NULL, node);
1342 } else if (parent) {
1343 /* connect to top-level siblings */
1344 lyd_insert_node(NULL, &parent, node);
1345 }
1346
1347 /* update remembered nodes */
1348 if (!nparent) {
1349 nparent = node;
1350 }
1351 nnode = node;
1352 }
1353
1354cleanup:
1355 lyxp_expr_free(ctx, exp);
1356 ly_path_free(ctx, p);
1357 if (!ret) {
1358 /* set out params only on success */
1359 if (new_parent) {
1360 *new_parent = nparent;
1361 }
1362 if (new_node) {
1363 *new_node = nnode;
1364 }
1365 }
1366 return ret;
1367}
1368
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001369LY_ERR
1370lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001371 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 +02001372 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001373{
1374 LY_ERR ret;
1375 const struct lysc_node *iter = NULL;
1376 struct lyd_node *node;
1377 struct lyd_value **dflts;
1378 LY_ARRAY_COUNT_TYPE u;
1379
1380 assert(first && (parent || sparent || mod));
1381
1382 if (!sparent && parent) {
1383 sparent = parent->schema;
1384 }
1385
1386 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1387 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1388 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001389 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1390 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001391 }
1392
1393 switch (iter->nodetype) {
1394 case LYS_CHOICE:
1395 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1396 /* create default case data */
1397 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
Michal Vasko69730152020-10-09 16:30:07 +02001398 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001399 }
1400 break;
1401 case LYS_CONTAINER:
1402 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1403 /* create default NP container */
1404 LY_CHECK_RET(lyd_create_inner(iter, &node));
1405 node->flags = LYD_DEFAULT;
1406 lyd_insert_node(parent, first, node);
1407
1408 /* cannot be a NP container with when */
1409 assert(!iter->when);
1410
1411 /* create any default children */
1412 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
Michal Vasko69730152020-10-09 16:30:07 +02001413 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001414 }
1415 break;
1416 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001417 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1418 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001419 /* create default leaf */
1420 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1421 if (ret == LY_EINCOMPLETE) {
1422 if (node_types) {
1423 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001424 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001425 }
1426 } else if (ret) {
1427 return ret;
1428 }
1429 node->flags = LYD_DEFAULT;
1430 lyd_insert_node(parent, first, node);
1431
1432 if (iter->when && node_when) {
1433 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001434 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001435 }
1436 if (diff) {
1437 /* add into diff */
1438 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1439 }
1440 }
1441 break;
1442 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001443 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1444 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001445 /* create all default leaf-lists */
1446 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1447 LY_ARRAY_FOR(dflts, u) {
1448 ret = lyd_create_term2(iter, dflts[u], &node);
1449 if (ret == LY_EINCOMPLETE) {
1450 if (node_types) {
1451 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001452 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001453 }
1454 } else if (ret) {
1455 return ret;
1456 }
1457 node->flags = LYD_DEFAULT;
1458 lyd_insert_node(parent, first, node);
1459
1460 if (iter->when && node_when) {
1461 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001462 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001463 }
1464 if (diff) {
1465 /* add into diff */
1466 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1467 }
1468 }
1469 }
1470 break;
1471 default:
1472 /* without defaults */
1473 break;
1474 }
1475 }
1476
1477 return LY_SUCCESS;
1478}
1479
1480API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001481lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001482{
Michal Vasko56daf732020-08-10 10:57:18 +02001483 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001484 LY_ERR ret = LY_SUCCESS;
1485
1486 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1487 if (diff) {
1488 *diff = NULL;
1489 }
1490
Michal Vasko56daf732020-08-10 10:57:18 +02001491 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001492 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001493 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1494 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001495 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001496 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001497 }
1498
Michal Vasko56daf732020-08-10 10:57:18 +02001499 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001500 }
1501
1502cleanup:
1503 if (ret && diff) {
1504 lyd_free_all(*diff);
1505 *diff = NULL;
1506 }
1507 return ret;
1508}
1509
1510API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001511lyd_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 +02001512{
1513 const struct lys_module *mod;
1514 struct lyd_node *d = NULL;
1515 uint32_t i = 0;
1516 LY_ERR ret = LY_SUCCESS;
1517
1518 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1519 if (diff) {
1520 *diff = NULL;
1521 }
1522 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001523 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001524 }
1525
1526 /* add nodes for each module one-by-one */
1527 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1528 if (!mod->implemented) {
1529 continue;
1530 }
1531
1532 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1533 if (d) {
1534 /* merge into one diff */
1535 lyd_insert_sibling(*diff, d, diff);
1536
1537 d = NULL;
1538 }
1539 }
1540
1541cleanup:
1542 if (ret && diff) {
1543 lyd_free_all(*diff);
1544 *diff = NULL;
1545 }
1546 return ret;
1547}
1548
1549API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001550lyd_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 +02001551{
1552 struct lyd_node *root, *d = NULL;
1553 LY_ERR ret = LY_SUCCESS;
1554
1555 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1556 if (diff) {
1557 *diff = NULL;
1558 }
1559
1560 /* add all top-level defaults for this module */
1561 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1562
1563 /* process nested nodes */
1564 LY_LIST_FOR(*tree, root) {
1565 /* skip added default nodes */
1566 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1567 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1568
1569 if (d) {
1570 /* merge into one diff */
1571 lyd_insert_sibling(*diff, d, diff);
1572
1573 d = NULL;
1574 }
1575 }
1576 }
1577
1578cleanup:
1579 if (ret && diff) {
1580 lyd_free_all(*diff);
1581 *diff = NULL;
1582 }
1583 return ret;
1584}
1585
Michal Vasko90932a92020-02-12 14:33:03 +01001586struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001587lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001588{
Michal Vaskob104f112020-07-17 09:54:54 +02001589 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001590 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001591 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001592
Michal Vaskob104f112020-07-17 09:54:54 +02001593 assert(new_node);
1594
1595 if (!first_sibling || !new_node->schema) {
1596 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001597 return NULL;
1598 }
1599
Michal Vaskob104f112020-07-17 09:54:54 +02001600 if (first_sibling->parent && first_sibling->parent->children_ht) {
1601 /* find the anchor using hashes */
1602 sparent = first_sibling->parent->schema;
1603 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1604 while (schema) {
1605 /* keep trying to find the first existing instance of the closest following schema sibling,
1606 * otherwise return NULL - inserting at the end */
1607 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1608 break;
1609 }
1610
1611 schema = lys_getnext(schema, sparent, NULL, 0);
1612 }
1613 } else {
1614 /* find the anchor without hashes */
1615 match = (struct lyd_node *)first_sibling;
1616 if (!lysc_data_parent(new_node->schema)) {
1617 /* we are in top-level, skip all the data from preceding modules */
1618 LY_LIST_FOR(match, match) {
1619 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1620 break;
1621 }
1622 }
1623 }
1624
1625 /* get the first schema sibling */
1626 sparent = lysc_data_parent(new_node->schema);
1627 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1628
1629 found = 0;
1630 LY_LIST_FOR(match, match) {
1631 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1632 /* we have found an opaque node, which must be at the end, so use it OR
1633 * modules do not match, so we must have traversed all the data from new_node module (if any),
1634 * we have found the first node of the next module, that is what we want */
1635 break;
1636 }
1637
1638 /* skip schema nodes until we find the instantiated one */
1639 while (!found) {
1640 if (new_node->schema == schema) {
1641 /* we have found the schema of the new node, continue search to find the first
1642 * data node with a different schema (after our schema) */
1643 found = 1;
1644 break;
1645 }
1646 if (match->schema == schema) {
1647 /* current node (match) is a data node still before the new node, continue search in data */
1648 break;
1649 }
1650 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1651 assert(schema);
1652 }
1653
1654 if (found && (match->schema != new_node->schema)) {
1655 /* find the next node after we have found our node schema data instance */
1656 break;
1657 }
1658 }
Michal Vasko90932a92020-02-12 14:33:03 +01001659 }
1660
1661 return match;
1662}
1663
1664/**
1665 * @brief Insert node after a sibling.
1666 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001667 * Handles inserting into NP containers and key-less lists.
1668 *
Michal Vasko90932a92020-02-12 14:33:03 +01001669 * @param[in] sibling Sibling to insert after.
1670 * @param[in] node Node to insert.
1671 */
1672static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001673lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001674{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001675 struct lyd_node_inner *par;
1676
Michal Vasko90932a92020-02-12 14:33:03 +01001677 assert(!node->next && (node->prev == node));
1678
1679 node->next = sibling->next;
1680 node->prev = sibling;
1681 sibling->next = node;
1682 if (node->next) {
1683 /* sibling had a succeeding node */
1684 node->next->prev = node;
1685 } else {
1686 /* sibling was last, find first sibling and change its prev */
1687 if (sibling->parent) {
1688 sibling = sibling->parent->child;
1689 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001690 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001691 }
1692 sibling->prev = node;
1693 }
1694 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001695
Michal Vasko9f96a052020-03-10 09:41:45 +01001696 for (par = node->parent; par; par = par->parent) {
1697 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1698 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001699 par->flags &= ~LYD_DEFAULT;
1700 }
Michal Vaskob104f112020-07-17 09:54:54 +02001701 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001702 /* rehash key-less list */
1703 lyd_hash((struct lyd_node *)par);
1704 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001705 }
Michal Vasko90932a92020-02-12 14:33:03 +01001706}
1707
1708/**
1709 * @brief Insert node before a sibling.
1710 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001711 * Handles inserting into NP containers and key-less lists.
1712 *
Michal Vasko90932a92020-02-12 14:33:03 +01001713 * @param[in] sibling Sibling to insert before.
1714 * @param[in] node Node to insert.
1715 */
1716static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001717lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001718{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001719 struct lyd_node_inner *par;
1720
Michal Vasko90932a92020-02-12 14:33:03 +01001721 assert(!node->next && (node->prev == node));
1722
1723 node->next = sibling;
1724 /* covers situation of sibling being first */
1725 node->prev = sibling->prev;
1726 sibling->prev = node;
1727 if (node->prev->next) {
1728 /* sibling had a preceding node */
1729 node->prev->next = node;
1730 } else if (sibling->parent) {
1731 /* sibling was first and we must also change parent child pointer */
1732 sibling->parent->child = node;
1733 }
1734 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001735
Michal Vasko9f96a052020-03-10 09:41:45 +01001736 for (par = node->parent; par; par = par->parent) {
1737 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1738 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001739 par->flags &= ~LYD_DEFAULT;
1740 }
Michal Vaskob104f112020-07-17 09:54:54 +02001741 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001742 /* rehash key-less list */
1743 lyd_hash((struct lyd_node *)par);
1744 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001745 }
Michal Vasko90932a92020-02-12 14:33:03 +01001746}
1747
1748/**
Michal Vaskob104f112020-07-17 09:54:54 +02001749 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001750 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001751 * Handles inserting into NP containers and key-less lists.
1752 *
Michal Vasko90932a92020-02-12 14:33:03 +01001753 * @param[in] parent Parent to insert into.
1754 * @param[in] node Node to insert.
1755 */
1756static void
Michal Vaskob104f112020-07-17 09:54:54 +02001757lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001758{
1759 struct lyd_node_inner *par;
1760
Radek Krejcia1c1e542020-09-29 16:06:52 +02001761 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001762 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001763
1764 par = (struct lyd_node_inner *)parent;
1765
Michal Vaskob104f112020-07-17 09:54:54 +02001766 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001767 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001768
Michal Vaskod989ba02020-08-24 10:59:24 +02001769 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001770 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1771 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001772 par->flags &= ~LYD_DEFAULT;
1773 }
Michal Vasko52927e22020-03-16 17:26:14 +01001774 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001775 /* rehash key-less list */
1776 lyd_hash((struct lyd_node *)par);
1777 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001778 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001779}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001780
Michal Vasko751cb4d2020-07-14 12:25:28 +02001781/**
1782 * @brief Learn whether a list instance has all the keys.
1783 *
1784 * @param[in] list List instance to check.
1785 * @return non-zero if all the keys were found,
1786 * @return 0 otherwise.
1787 */
1788static int
1789lyd_insert_has_keys(const struct lyd_node *list)
1790{
1791 const struct lyd_node *key;
1792 const struct lysc_node *skey = NULL;
1793
1794 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02001795 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001796 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1797 if (!key || (key->schema != skey)) {
1798 /* key missing */
1799 return 0;
1800 }
1801
1802 key = key->next;
1803 }
1804
1805 /* all keys found */
1806 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001807}
1808
1809void
Michal Vaskob104f112020-07-17 09:54:54 +02001810lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001811{
Michal Vaskob104f112020-07-17 09:54:54 +02001812 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001813
Michal Vaskob104f112020-07-17 09:54:54 +02001814 /* inserting list without its keys is not supported */
1815 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001816
Michal Vaskob104f112020-07-17 09:54:54 +02001817 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1818 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001819 }
Michal Vasko90932a92020-02-12 14:33:03 +01001820
Michal Vaskob104f112020-07-17 09:54:54 +02001821 /* get first sibling */
1822 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001823
Michal Vaskob104f112020-07-17 09:54:54 +02001824 /* find the anchor, our next node, so we can insert before it */
1825 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1826 if (anchor) {
1827 lyd_insert_before_node(anchor, node);
1828 } else if (first_sibling) {
1829 lyd_insert_after_node(first_sibling->prev, node);
1830 } else if (parent) {
1831 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001832 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001833 *first_sibling_p = node;
1834 }
1835
1836 /* insert into parent HT */
1837 lyd_insert_hash(node);
1838
1839 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001840 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001841 lyd_hash(parent);
1842
1843 /* now we can insert even the list into its parent HT */
1844 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001845 }
Michal Vasko90932a92020-02-12 14:33:03 +01001846}
1847
Michal Vaskof03ed032020-03-04 13:31:44 +01001848static LY_ERR
1849lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1850{
1851 const struct lysc_node *par2;
1852
1853 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001854 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001855
1856 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001857 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001858
1859 if (parent) {
1860 /* inner node */
1861 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001862 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02001863 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001864 return LY_EINVAL;
1865 }
1866 } else {
1867 /* top-level node */
1868 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001869 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001870 return LY_EINVAL;
1871 }
1872 }
1873
1874 return LY_SUCCESS;
1875}
1876
1877API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001878lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001879{
1880 struct lyd_node *iter;
1881
Michal Vasko654bc852020-06-23 13:28:06 +02001882 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001883
1884 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1885
1886 if (node->schema->flags & LYS_KEY) {
1887 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1888 return LY_EINVAL;
1889 }
1890
1891 if (node->parent || node->prev->next) {
1892 lyd_unlink_tree(node);
1893 }
1894
1895 while (node) {
1896 iter = node->next;
1897 lyd_unlink_tree(node);
1898 lyd_insert_node(parent, NULL, node);
1899 node = iter;
1900 }
1901 return LY_SUCCESS;
1902}
1903
1904API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001905lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001906{
1907 struct lyd_node *iter;
1908
Michal Vaskob104f112020-07-17 09:54:54 +02001909 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001910
Michal Vaskob104f112020-07-17 09:54:54 +02001911 if (sibling) {
1912 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001913 }
1914
1915 if (node->parent || node->prev->next) {
1916 lyd_unlink_tree(node);
1917 }
1918
1919 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001920 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001921 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001922 return LY_EINVAL;
1923 }
1924
Michal Vaskob1b5c262020-03-05 14:29:47 +01001925 iter = node->next;
1926 lyd_unlink_tree(node);
1927 lyd_insert_node(NULL, &sibling, node);
1928 node = iter;
1929 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001930
Michal Vaskob104f112020-07-17 09:54:54 +02001931 if (first) {
1932 /* find the first sibling */
1933 *first = sibling;
1934 while ((*first)->prev->next) {
1935 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001936 }
1937 }
1938
1939 return LY_SUCCESS;
1940}
1941
Michal Vaskob1b5c262020-03-05 14:29:47 +01001942API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001943lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1944{
1945 struct lyd_node *iter;
1946
1947 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1948
Michal Vasko62ed12d2020-05-21 10:08:25 +02001949 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001950
Michal Vaskob104f112020-07-17 09:54:54 +02001951 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001952 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001953 return LY_EINVAL;
1954 }
1955
1956 if (node->parent || node->prev->next) {
1957 lyd_unlink_tree(node);
1958 }
1959
1960 /* insert in reverse order to get the original order */
1961 node = node->prev;
1962 while (node) {
1963 iter = node->prev;
1964 lyd_unlink_tree(node);
1965
1966 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001967 lyd_insert_hash(node);
1968
Michal Vaskof03ed032020-03-04 13:31:44 +01001969 /* move the anchor accordingly */
1970 sibling = node;
1971
1972 node = (iter == node) ? NULL : iter;
1973 }
1974 return LY_SUCCESS;
1975}
1976
1977API LY_ERR
1978lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1979{
1980 struct lyd_node *iter;
1981
1982 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1983
Michal Vasko62ed12d2020-05-21 10:08:25 +02001984 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001985
Michal Vaskob104f112020-07-17 09:54:54 +02001986 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001987 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001988 return LY_EINVAL;
1989 }
1990
1991 if (node->parent || node->prev->next) {
1992 lyd_unlink_tree(node);
1993 }
1994
1995 while (node) {
1996 iter = node->next;
1997 lyd_unlink_tree(node);
1998
1999 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002000 lyd_insert_hash(node);
2001
Michal Vaskof03ed032020-03-04 13:31:44 +01002002 /* move the anchor accordingly */
2003 sibling = node;
2004
2005 node = iter;
2006 }
2007 return LY_SUCCESS;
2008}
2009
2010API void
2011lyd_unlink_tree(struct lyd_node *node)
2012{
2013 struct lyd_node *iter;
2014
2015 if (!node) {
2016 return;
2017 }
2018
Michal Vaskob104f112020-07-17 09:54:54 +02002019 /* update hashes while still linked into the tree */
2020 lyd_unlink_hash(node);
2021
Michal Vaskof03ed032020-03-04 13:31:44 +01002022 /* unlink from siblings */
2023 if (node->prev->next) {
2024 node->prev->next = node->next;
2025 }
2026 if (node->next) {
2027 node->next->prev = node->prev;
2028 } else {
2029 /* unlinking the last node */
2030 if (node->parent) {
2031 iter = node->parent->child;
2032 } else {
2033 iter = node->prev;
2034 while (iter->prev != node) {
2035 iter = iter->prev;
2036 }
2037 }
2038 /* update the "last" pointer from the first node */
2039 iter->prev = node->prev;
2040 }
2041
2042 /* unlink from parent */
2043 if (node->parent) {
2044 if (node->parent->child == node) {
2045 /* the node is the first child */
2046 node->parent->child = node->next;
2047 }
2048
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002049 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002050 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2051 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002052 LY_LIST_FOR(node->parent->child, iter) {
2053 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2054 break;
2055 }
2056 }
2057 if (!iter) {
2058 node->parent->flags |= LYD_DEFAULT;
2059 }
2060 }
2061
Michal Vaskof03ed032020-03-04 13:31:44 +01002062 /* check for keyless list and update its hash */
2063 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002064 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002065 lyd_hash(iter);
2066 }
2067 }
2068
2069 node->parent = NULL;
2070 }
2071
2072 node->next = NULL;
2073 node->prev = node;
2074}
2075
Michal Vaskoa5da3292020-08-12 13:10:50 +02002076void
Radek Krejci1798aae2020-07-14 13:26:06 +02002077lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2078{
2079 struct lyd_meta *last, *iter;
2080
2081 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002082
2083 if (!meta) {
2084 return;
2085 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002086
2087 for (iter = meta; iter; iter = iter->next) {
2088 iter->parent = parent;
2089 }
2090
2091 /* insert as the last attribute */
2092 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002093 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002094 last->next = meta;
2095 } else {
2096 parent->meta = meta;
2097 }
2098
2099 /* remove default flags from NP containers */
2100 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2101 parent->flags &= ~LYD_DEFAULT;
2102 parent = (struct lyd_node *)parent->parent;
2103 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002104}
2105
2106LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002107lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002108 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
2109 void *prefix_data, uint32_t hints, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002110{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002111 LY_ERR rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002112 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002113 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002114 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002115
Michal Vasko9f96a052020-03-10 09:41:45 +01002116 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002117
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002118 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002119 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002120 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002121 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002122 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002123 break;
2124 }
2125 }
2126 if (!ant) {
2127 /* attribute is not defined as a metadata annotation (RFC 7952) */
2128 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2129 mod->name, name_len, name);
2130 return LY_EINVAL;
2131 }
2132
Michal Vasko9f96a052020-03-10 09:41:45 +01002133 mt = calloc(1, sizeof *mt);
2134 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2135 mt->parent = parent;
2136 mt->annotation = ant;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002137 rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic,
2138 format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL);
2139 LY_CHECK_ERR_RET(rc, free(mt), rc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002140 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2141 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002142
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002143 /* insert as the last attribute */
2144 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002145 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002146 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002147 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002148 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002149 }
2150
Michal Vasko9f96a052020-03-10 09:41:45 +01002151 if (meta) {
2152 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002153 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002154 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002155}
2156
Michal Vaskoa5da3292020-08-12 13:10:50 +02002157void
2158lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2159{
2160 struct lyd_attr *last, *iter;
2161 struct lyd_node_opaq *opaq;
2162
2163 assert(parent && !parent->schema);
2164
2165 if (!attr) {
2166 return;
2167 }
2168
2169 opaq = (struct lyd_node_opaq *)parent;
2170 for (iter = attr; iter; iter = iter->next) {
2171 iter->parent = opaq;
2172 }
2173
2174 /* insert as the last attribute */
2175 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002176 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002177 last->next = attr;
2178 } else {
2179 opaq->attr = attr;
2180 }
2181}
2182
Michal Vasko52927e22020-03-16 17:26:14 +01002183LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002184lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len,
2185 const char *value, size_t value_len, ly_bool *dynamic, LYD_FORMAT format, uint32_t hints,
Radek Krejci0f969882020-08-21 16:56:47 +02002186 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 +01002187{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002188 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002189 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002190
2191 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002192 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002193
2194 if (!value_len) {
2195 value = "";
2196 }
2197
2198 at = calloc(1, sizeof *at);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002199 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002200 at->hints = hints;
Radek Krejcid46e46a2020-09-15 14:22:42 +02002201 at->format = format;
2202 at->val_prefs = val_prefs;
2203
Radek Krejci011e4aa2020-09-04 15:22:31 +02002204 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002205 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002206 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2207 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002208 *dynamic = 0;
2209 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002210 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002211 }
2212
Radek Krejci1798aae2020-07-14 13:26:06 +02002213 if (prefix_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002214 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +02002215 }
2216 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002217 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002218 }
2219
2220 /* insert as the last attribute */
2221 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002222 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002223 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002224 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002225 last->next = at;
2226 }
2227
Radek Krejci011e4aa2020-09-04 15:22:31 +02002228finish:
2229 if (ret) {
2230 lyd_free_attr_single(ctx, at);
2231 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002232 *attr = at;
2233 }
2234 return LY_SUCCESS;
2235}
2236
Radek Krejci084289f2019-07-09 17:35:30 +02002237API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002238lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002239{
Michal Vasko004d3152020-06-11 19:59:22 +02002240 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002241
Michal Vasko004d3152020-06-11 19:59:22 +02002242 if (ly_path_eval(path, tree, &target)) {
2243 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002244 }
2245
Michal Vasko004d3152020-06-11 19:59:22 +02002246 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002247}
2248
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002249API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002250lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002251{
2252 const struct lyd_node *iter1, *iter2;
2253 struct lyd_node_term *term1, *term2;
2254 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002255 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002256 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002257
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002258 if (!node1 || !node2) {
2259 if (node1 == node2) {
2260 return LY_SUCCESS;
2261 } else {
2262 return LY_ENOT;
2263 }
2264 }
2265
Michal Vaskob7be7a82020-08-20 09:09:04 +02002266 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002267 return LY_ENOT;
2268 }
2269
2270 if (node1->hash != node2->hash) {
2271 return LY_ENOT;
2272 }
Michal Vasko52927e22020-03-16 17:26:14 +01002273 /* 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 +02002274
Michal Vasko52927e22020-03-16 17:26:14 +01002275 if (!node1->schema) {
2276 opaq1 = (struct lyd_node_opaq *)node1;
2277 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002278 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002279 return LY_ENOT;
2280 }
Michal Vasko52927e22020-03-16 17:26:14 +01002281 switch (opaq1->format) {
2282 case LYD_XML:
2283 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2284 return LY_ENOT;
2285 }
2286 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002287 case LYD_JSON:
2288 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2289 if (strcmp(opaq1->value, opaq2->value)) {
2290 return LY_ENOT;
2291 }
2292 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002293 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002294 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002295 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002296 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002297 return LY_EINT;
2298 }
2299 if (options & LYD_COMPARE_FULL_RECURSION) {
2300 iter1 = opaq1->child;
2301 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002302 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002303 }
2304 return LY_SUCCESS;
2305 } else {
2306 switch (node1->schema->nodetype) {
2307 case LYS_LEAF:
2308 case LYS_LEAFLIST:
2309 if (options & LYD_COMPARE_DEFAULTS) {
2310 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2311 return LY_ENOT;
2312 }
2313 }
2314
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002315 term1 = (struct lyd_node_term *)node1;
2316 term2 = (struct lyd_node_term *)node2;
2317 if (term1->value.realtype != term2->value.realtype) {
2318 return LY_ENOT;
2319 }
Michal Vasko52927e22020-03-16 17:26:14 +01002320
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002321 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002322 case LYS_CONTAINER:
2323 if (options & LYD_COMPARE_DEFAULTS) {
2324 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2325 return LY_ENOT;
2326 }
2327 }
2328 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002329 iter1 = ((struct lyd_node_inner *)node1)->child;
2330 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002331 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002332 }
2333 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002334 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002335 case LYS_ACTION:
2336 if (options & LYD_COMPARE_FULL_RECURSION) {
2337 /* TODO action/RPC
2338 goto all_children_compare;
2339 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002340 }
2341 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002342 case LYS_NOTIF:
2343 if (options & LYD_COMPARE_FULL_RECURSION) {
2344 /* TODO Notification
2345 goto all_children_compare;
2346 */
2347 }
2348 return LY_SUCCESS;
2349 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002350 iter1 = ((struct lyd_node_inner *)node1)->child;
2351 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002352
2353 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2354 /* lists with keys, their equivalence is based on their keys */
Michal Vasko22df3f02020-08-24 13:29:22 +02002355 for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002356 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002357 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002358 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002359 return LY_ENOT;
2360 }
2361 iter1 = iter1->next;
2362 iter2 = iter2->next;
2363 }
2364 } else {
2365 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2366
Radek Krejci0f969882020-08-21 16:56:47 +02002367all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002368 if (!iter1 && !iter2) {
2369 /* no children, nothing to compare */
2370 return LY_SUCCESS;
2371 }
2372
Michal Vaskod989ba02020-08-24 10:59:24 +02002373 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002374 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002375 return LY_ENOT;
2376 }
2377 }
2378 if (iter1 || iter2) {
2379 return LY_ENOT;
2380 }
2381 }
2382 return LY_SUCCESS;
2383 case LYS_ANYXML:
2384 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002385 any1 = (struct lyd_node_any *)node1;
2386 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002387
2388 if (any1->value_type != any2->value_type) {
2389 return LY_ENOT;
2390 }
2391 switch (any1->value_type) {
2392 case LYD_ANYDATA_DATATREE:
2393 iter1 = any1->value.tree;
2394 iter2 = any2->value.tree;
2395 goto all_children_compare;
2396 case LYD_ANYDATA_STRING:
2397 case LYD_ANYDATA_XML:
2398 case LYD_ANYDATA_JSON:
2399 len1 = strlen(any1->value.str);
2400 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002401 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002402 return LY_ENOT;
2403 }
2404 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002405 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002406 len1 = lyd_lyb_data_length(any1->value.mem);
2407 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002408 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002409 return LY_ENOT;
2410 }
2411 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002412 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002413 }
2414 }
2415
Michal Vaskob7be7a82020-08-20 09:09:04 +02002416 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002417 return LY_EINT;
2418}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002419
Michal Vasko21725742020-06-29 11:49:25 +02002420API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002421lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002422{
Michal Vaskod989ba02020-08-24 10:59:24 +02002423 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002424 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2425 }
2426
Michal Vasko11a81432020-07-28 16:31:29 +02002427 if (node1 == node2) {
2428 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002429 }
Michal Vasko11a81432020-07-28 16:31:29 +02002430 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002431}
2432
2433API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002434lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2435{
2436 if (!meta1 || !meta2) {
2437 if (meta1 == meta2) {
2438 return LY_SUCCESS;
2439 } else {
2440 return LY_ENOT;
2441 }
2442 }
2443
Michal Vaskob7be7a82020-08-20 09:09:04 +02002444 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002445 return LY_ENOT;
2446 }
2447
2448 if (meta1->value.realtype != meta2->value.realtype) {
2449 return LY_ENOT;
2450 }
2451
2452 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2453}
2454
Radek Krejci22ebdba2019-07-25 13:59:43 +02002455/**
Michal Vasko52927e22020-03-16 17:26:14 +01002456 * @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 +02002457 *
2458 * 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 +02002459 *
2460 * @param[in] node Original node to duplicate
2461 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2462 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2463 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2464 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2465 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002466 */
Michal Vasko52927e22020-03-16 17:26:14 +01002467static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002468lyd_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 +02002469 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002470{
Michal Vasko52927e22020-03-16 17:26:14 +01002471 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002472 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002473 struct lyd_meta *meta;
2474 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002475 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002476
Michal Vasko52927e22020-03-16 17:26:14 +01002477 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002478
Michal Vasko52927e22020-03-16 17:26:14 +01002479 if (!node->schema) {
2480 dup = calloc(1, sizeof(struct lyd_node_opaq));
2481 } else {
2482 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002483 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002484 case LYS_ACTION:
2485 case LYS_NOTIF:
2486 case LYS_CONTAINER:
2487 case LYS_LIST:
2488 dup = calloc(1, sizeof(struct lyd_node_inner));
2489 break;
2490 case LYS_LEAF:
2491 case LYS_LEAFLIST:
2492 dup = calloc(1, sizeof(struct lyd_node_term));
2493 break;
2494 case LYS_ANYDATA:
2495 case LYS_ANYXML:
2496 dup = calloc(1, sizeof(struct lyd_node_any));
2497 break;
2498 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002499 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002500 ret = LY_EINT;
2501 goto error;
2502 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002503 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002504 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002505
Michal Vaskof6df0a02020-06-16 13:08:34 +02002506 if (options & LYD_DUP_WITH_FLAGS) {
2507 dup->flags = node->flags;
2508 } else {
2509 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2510 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002511 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002512 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002513
Michal Vasko25a32822020-07-09 15:48:22 +02002514 /* duplicate metadata */
2515 if (!(options & LYD_DUP_NO_META)) {
2516 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002517 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002518 }
2519 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002520
2521 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002522 if (!dup->schema) {
2523 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2524 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2525 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002526
2527 if (options & LYD_DUP_RECURSIVE) {
2528 /* duplicate all the children */
2529 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002530 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002531 }
2532 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002533 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002534 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002535 if (orig->prefix.id) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002536 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002537 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002538 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 +01002539 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002540 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 +01002541 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002542 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
2543 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 +01002544 LY_ARRAY_INCREMENT(opaq->val_prefs);
2545 }
2546 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002547 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002548 opaq->ctx = orig->ctx;
2549 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2550 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2551 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2552
2553 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002554 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002555 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002556 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2557 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2558 struct lyd_node *child;
2559
2560 if (options & LYD_DUP_RECURSIVE) {
2561 /* duplicate all the children */
2562 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002563 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002564 }
Michal Vasko69730152020-10-09 16:30:07 +02002565 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002566 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002567 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002568 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002569 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002570 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002571 if (!child) {
2572 /* possibly not keys are present in filtered tree */
2573 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002574 } else if (child->schema != key) {
2575 /* possibly not all keys are present in filtered tree,
2576 * but there can be also some non-key nodes */
2577 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002578 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002579 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002580 child = child->next;
2581 }
2582 }
2583 lyd_hash(dup);
2584 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002585 dup->hash = node->hash;
2586 any = (struct lyd_node_any *)node;
2587 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002588 }
2589
Michal Vasko52927e22020-03-16 17:26:14 +01002590 /* insert */
2591 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002592
2593 if (dup_p) {
2594 *dup_p = dup;
2595 }
2596 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002597
2598error:
Michal Vasko52927e22020-03-16 17:26:14 +01002599 lyd_free_tree(dup);
2600 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002601}
2602
Michal Vasko3a41dff2020-07-15 14:30:28 +02002603static LY_ERR
2604lyd_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 +02002605 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002606{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002607 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002608 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002609
2610 *dup_parent = NULL;
2611 *local_parent = NULL;
2612
2613 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2614 if (parent && (parent->schema == orig_parent->schema)) {
2615 /* stop creating parents, connect what we have into the provided parent */
2616 iter = parent;
2617 repeat = 0;
2618 } else {
2619 iter = NULL;
2620 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002621 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002622 }
2623 if (!*local_parent) {
2624 *local_parent = (struct lyd_node_inner *)iter;
2625 }
2626 if (iter->child) {
2627 /* 1) list - add after keys
2628 * 2) provided parent with some children */
2629 iter->child->prev->next = *dup_parent;
2630 if (*dup_parent) {
2631 (*dup_parent)->prev = iter->child->prev;
2632 iter->child->prev = *dup_parent;
2633 }
2634 } else {
2635 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2636 }
2637 if (*dup_parent) {
2638 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2639 }
2640 *dup_parent = (struct lyd_node *)iter;
2641 }
2642
2643 if (repeat && parent) {
2644 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002645 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002646 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002647 return LY_EINVAL;
2648 }
2649
2650 return LY_SUCCESS;
2651}
2652
2653static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002654lyd_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 +02002655{
2656 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002657 const struct lyd_node *orig; /* original node to be duplicated */
2658 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002659 struct lyd_node *top = NULL; /* the most higher created node */
2660 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002661
Michal Vasko3a41dff2020-07-15 14:30:28 +02002662 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002663
2664 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002665 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002666 } else {
2667 local_parent = parent;
2668 }
2669
Radek Krejci22ebdba2019-07-25 13:59:43 +02002670 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002671 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002672 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2673 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002674 break;
2675 }
2676 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002677
2678 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02002679 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02002680 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002681 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002682 }
2683 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002684
2685 if (dup) {
2686 *dup = first;
2687 }
2688 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002689
2690error:
2691 if (top) {
2692 lyd_free_tree(top);
2693 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002694 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002695 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002696 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002697}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002698
Michal Vasko3a41dff2020-07-15 14:30:28 +02002699API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002700lyd_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 +02002701{
2702 return lyd_dup(node, parent, options, 1, dup);
2703}
2704
2705API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002706lyd_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 +02002707{
2708 return lyd_dup(node, parent, options, 0, dup);
2709}
2710
2711API LY_ERR
2712lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002713{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002714 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002715 struct lyd_meta *mt, *last;
2716
Michal Vasko3a41dff2020-07-15 14:30:28 +02002717 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002718
2719 /* create a copy */
2720 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002721 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002722 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002723 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002724 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
2725 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002726
2727 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002728 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002729 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002730 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002731 last->next = mt;
2732 } else {
2733 node->meta = mt;
2734 }
2735
Radek Krejci011e4aa2020-09-04 15:22:31 +02002736finish:
2737 if (ret) {
2738 lyd_free_meta_single(mt);
2739 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002740 *dup = mt;
2741 }
2742 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002743}
2744
Michal Vasko4490d312020-06-16 13:08:55 +02002745/**
2746 * @brief Merge a source sibling into target siblings.
2747 *
2748 * @param[in,out] first_trg First target sibling, is updated if top-level.
2749 * @param[in] parent_trg Target parent.
2750 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2751 * @param[in] options Merge options.
2752 * @return LY_ERR value.
2753 */
2754static LY_ERR
2755lyd_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 +02002756 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02002757{
2758 LY_ERR ret;
2759 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002760 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002761 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002762
2763 sibling_src = *sibling_src_p;
2764 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2765 /* try to find the exact instance */
2766 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2767 } else {
2768 /* try to simply find the node, there cannot be more instances */
2769 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2770 }
2771
2772 if (!ret) {
2773 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002774 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002775 /* since they are different, they cannot both be default */
2776 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2777
Michal Vasko3a41dff2020-07-15 14:30:28 +02002778 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2779 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002780 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002781 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2782 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002783 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002784
2785 /* copy flags and add LYD_NEW */
2786 match_trg->flags = sibling_src->flags | LYD_NEW;
2787 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002788 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002789 /* update value */
2790 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002791 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002792
2793 /* copy flags and add LYD_NEW */
2794 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002795 } else {
2796 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02002797 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002798 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2799 }
2800 }
2801 } else {
2802 /* node not found, merge it */
2803 if (options & LYD_MERGE_DESTRUCT) {
2804 dup_src = (struct lyd_node *)sibling_src;
2805 lyd_unlink_tree(dup_src);
2806 /* spend it */
2807 *sibling_src_p = NULL;
2808 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002809 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 +02002810 }
2811
2812 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002813 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002814 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002815 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002816 }
2817
2818 lyd_insert_node(parent_trg, first_trg, dup_src);
2819 }
2820
2821 return LY_SUCCESS;
2822}
2823
Michal Vasko3a41dff2020-07-15 14:30:28 +02002824static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002825lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002826{
2827 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02002828 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02002829
2830 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2831
2832 if (!source) {
2833 /* nothing to merge */
2834 return LY_SUCCESS;
2835 }
2836
2837 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002838 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002839 return LY_EINVAL;
2840 }
2841
2842 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02002843 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002844 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2845 if (first && !sibling_src) {
2846 /* source was spent (unlinked), move to the next node */
2847 source = tmp;
2848 }
2849
Michal Vasko3a41dff2020-07-15 14:30:28 +02002850 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002851 break;
2852 }
2853 }
2854
2855 if (options & LYD_MERGE_DESTRUCT) {
2856 /* free any leftover source data that were not merged */
2857 lyd_free_siblings((struct lyd_node *)source);
2858 }
2859
2860 return LY_SUCCESS;
2861}
2862
Michal Vasko3a41dff2020-07-15 14:30:28 +02002863API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002864lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002865{
2866 return lyd_merge(target, source, options, 1);
2867}
2868
2869API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002870lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002871{
2872 return lyd_merge(target, source, options, 0);
2873}
2874
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002875static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002876lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002877{
Michal Vasko14654712020-02-06 08:35:21 +01002878 /* ending \0 */
2879 ++reqlen;
2880
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002881 if (reqlen > *buflen) {
2882 if (is_static) {
2883 return LY_EINCOMPLETE;
2884 }
2885
2886 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2887 if (!*buffer) {
2888 return LY_EMEM;
2889 }
2890
2891 *buflen = reqlen;
2892 }
2893
2894 return LY_SUCCESS;
2895}
2896
Michal Vaskod59035b2020-07-08 12:00:06 +02002897LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002898lyd_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 +02002899{
2900 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002901 size_t len;
2902 const char *val;
2903 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002904
Radek Krejcia1c1e542020-09-29 16:06:52 +02002905 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002906 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002907 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002908 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002909
2910 quot = '\'';
2911 if (strchr(val, '\'')) {
2912 quot = '"';
2913 }
2914 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002915 }
2916
2917 return LY_SUCCESS;
2918}
2919
2920/**
2921 * @brief Append leaf-list value predicate to path.
2922 *
2923 * @param[in] node Node to print.
2924 * @param[in,out] buffer Buffer to print to.
2925 * @param[in,out] buflen Current buffer length.
2926 * @param[in,out] bufused Current number of characters used in @p buffer.
2927 * @param[in] is_static Whether buffer is static or can be reallocated.
2928 * @return LY_ERR
2929 */
2930static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002931lyd_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 +02002932{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002933 size_t len;
2934 const char *val;
2935 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002936
Michal Vaskob7be7a82020-08-20 09:09:04 +02002937 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002938 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002939 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002940
2941 quot = '\'';
2942 if (strchr(val, '\'')) {
2943 quot = '"';
2944 }
2945 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2946
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002947 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002948}
2949
2950/**
2951 * @brief Append node position (relative to its other instances) predicate to path.
2952 *
2953 * @param[in] node Node to print.
2954 * @param[in,out] buffer Buffer to print to.
2955 * @param[in,out] buflen Current buffer length.
2956 * @param[in,out] bufused Current number of characters used in @p buffer.
2957 * @param[in] is_static Whether buffer is static or can be reallocated.
2958 * @return LY_ERR
2959 */
2960static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002961lyd_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 +02002962{
2963 const struct lyd_node *first, *iter;
2964 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002965 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002966 char *val = NULL;
2967 LY_ERR rc;
2968
2969 if (node->parent) {
2970 first = node->parent->child;
2971 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02002972 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002973 }
2974 pos = 1;
2975 for (iter = first; iter != node; iter = iter->next) {
2976 if (iter->schema == node->schema) {
2977 ++pos;
2978 }
2979 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002980 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002981 return LY_EMEM;
2982 }
2983
2984 len = 1 + strlen(val) + 1;
2985 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2986 if (rc != LY_SUCCESS) {
2987 goto cleanup;
2988 }
2989
2990 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2991
2992cleanup:
2993 free(val);
2994 return rc;
2995}
2996
2997API char *
2998lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2999{
Radek Krejci857189e2020-09-01 13:26:36 +02003000 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003001 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003002 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003003 const struct lyd_node *iter;
3004 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003005 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003006
3007 LY_CHECK_ARG_RET(NULL, node, NULL);
3008 if (buffer) {
3009 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3010 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003011 } else {
3012 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003013 }
3014
3015 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003016 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003017 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003018 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003019 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3020 ++depth;
3021 }
3022
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003023 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003024 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003025 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003026 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003027iter_print:
3028 /* print prefix and name */
3029 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003030 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003031 mod = iter->schema->module;
3032 }
3033
3034 /* realloc string */
Michal Vasko22df3f02020-08-24 13:29:22 +02003035 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 +02003036 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3037 if (rc != LY_SUCCESS) {
3038 break;
3039 }
3040
3041 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003042 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vasko69730152020-10-09 16:30:07 +02003043 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003044
Michal Vasko790b2bc2020-08-03 13:35:06 +02003045 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003046 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003047 switch (iter->schema->nodetype) {
3048 case LYS_LIST:
3049 if (iter->schema->flags & LYS_KEYLESS) {
3050 /* print its position */
3051 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3052 } else {
3053 /* print all list keys in predicates */
3054 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3055 }
3056 break;
3057 case LYS_LEAFLIST:
3058 if (iter->schema->flags & LYS_CONFIG_W) {
3059 /* print leaf-list value */
3060 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3061 } else {
3062 /* print its position */
3063 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3064 }
3065 break;
3066 default:
3067 /* nothing to print more */
3068 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003069 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003070 }
3071 if (rc != LY_SUCCESS) {
3072 break;
3073 }
3074
Michal Vasko14654712020-02-06 08:35:21 +01003075 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003076 }
3077 break;
3078 }
3079
3080 return buffer;
3081}
Michal Vaskoe444f752020-02-10 12:20:06 +01003082
Michal Vasko25a32822020-07-09 15:48:22 +02003083API struct lyd_meta *
3084lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3085{
3086 struct lyd_meta *ret = NULL;
3087 const struct ly_ctx *ctx;
3088 const char *prefix, *tmp;
3089 char *str;
3090 size_t pref_len, name_len;
3091
3092 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3093
3094 if (!first) {
3095 return NULL;
3096 }
3097
3098 ctx = first->annotation->module->ctx;
3099
3100 /* parse the name */
3101 tmp = name;
3102 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3103 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3104 return NULL;
3105 }
3106
3107 /* find the module */
3108 if (prefix) {
3109 str = strndup(prefix, pref_len);
3110 module = ly_ctx_get_module_latest(ctx, str);
3111 free(str);
3112 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3113 }
3114
3115 /* find the metadata */
3116 LY_LIST_FOR(first, first) {
3117 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3118 ret = (struct lyd_meta *)first;
3119 break;
3120 }
3121 }
3122
3123 return ret;
3124}
3125
Michal Vasko9b368d32020-02-14 13:53:31 +01003126API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003127lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3128{
3129 struct lyd_node **match_p;
3130 struct lyd_node_inner *parent;
3131
Michal Vaskof03ed032020-03-04 13:31:44 +01003132 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003133
Michal Vasko62ed12d2020-05-21 10:08:25 +02003134 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3135 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003136 if (match) {
3137 *match = NULL;
3138 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003139 return LY_ENOTFOUND;
3140 }
3141
3142 /* find first sibling */
3143 if (siblings->parent) {
3144 siblings = siblings->parent->child;
3145 } else {
3146 while (siblings->prev->next) {
3147 siblings = siblings->prev;
3148 }
3149 }
3150
3151 parent = (struct lyd_node_inner *)siblings->parent;
3152 if (parent && parent->children_ht) {
3153 assert(target->hash);
3154
3155 /* find by hash */
3156 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003157 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003158 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003159 siblings = *match_p;
3160 } else {
3161 siblings = NULL;
3162 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003163 } else {
3164 /* not found */
3165 siblings = NULL;
3166 }
3167 } else {
3168 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003169 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003170 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003171 break;
3172 }
3173 }
3174 }
3175
3176 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003177 if (match) {
3178 *match = NULL;
3179 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003180 return LY_ENOTFOUND;
3181 }
3182
Michal Vasko9b368d32020-02-14 13:53:31 +01003183 if (match) {
3184 *match = (struct lyd_node *)siblings;
3185 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003186 return LY_SUCCESS;
3187}
3188
Radek Krejci857189e2020-09-01 13:26:36 +02003189/**
3190 * @brief Comparison callback to match schema node with a schema of a data node.
3191 *
3192 * @param[in] val1_p Pointer to the schema node
3193 * @param[in] val2_p Pointer to the data node
3194 * Implementation of ::values_equal_cb.
3195 */
3196static ly_bool
3197lyd_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 +01003198{
3199 struct lysc_node *val1;
3200 struct lyd_node *val2;
3201
3202 val1 = *((struct lysc_node **)val1_p);
3203 val2 = *((struct lyd_node **)val2_p);
3204
Michal Vasko90932a92020-02-12 14:33:03 +01003205 if (val1 == val2->schema) {
3206 /* schema match is enough */
3207 return 1;
3208 } else {
3209 return 0;
3210 }
3211}
3212
3213static LY_ERR
3214lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3215{
3216 struct lyd_node **match_p;
3217 struct lyd_node_inner *parent;
3218 uint32_t hash;
3219 values_equal_cb ht_cb;
3220
Michal Vaskob104f112020-07-17 09:54:54 +02003221 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003222
3223 parent = (struct lyd_node_inner *)siblings->parent;
3224 if (parent && parent->children_ht) {
3225 /* calculate our hash */
3226 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3227 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3228 hash = dict_hash_multi(hash, NULL, 0);
3229
3230 /* use special hash table function */
3231 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3232
3233 /* find by hash */
3234 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3235 siblings = *match_p;
3236 } else {
3237 /* not found */
3238 siblings = NULL;
3239 }
3240
3241 /* set the original hash table compare function back */
3242 lyht_set_cb(parent->children_ht, ht_cb);
3243 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003244 /* find first sibling */
3245 if (siblings->parent) {
3246 siblings = siblings->parent->child;
3247 } else {
3248 while (siblings->prev->next) {
3249 siblings = siblings->prev;
3250 }
3251 }
3252
3253 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003254 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003255 if (siblings->schema == schema) {
3256 /* schema match is enough */
3257 break;
3258 }
3259 }
3260 }
3261
3262 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003263 if (match) {
3264 *match = NULL;
3265 }
Michal Vasko90932a92020-02-12 14:33:03 +01003266 return LY_ENOTFOUND;
3267 }
3268
Michal Vasko9b368d32020-02-14 13:53:31 +01003269 if (match) {
3270 *match = (struct lyd_node *)siblings;
3271 }
Michal Vasko90932a92020-02-12 14:33:03 +01003272 return LY_SUCCESS;
3273}
3274
Michal Vaskoe444f752020-02-10 12:20:06 +01003275API LY_ERR
3276lyd_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 +02003277 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003278{
3279 LY_ERR rc;
3280 struct lyd_node *target = NULL;
3281
Michal Vasko4c583e82020-07-17 12:16:14 +02003282 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003283
Michal Vasko62ed12d2020-05-21 10:08:25 +02003284 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3285 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003286 if (match) {
3287 *match = NULL;
3288 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003289 return LY_ENOTFOUND;
3290 }
3291
Michal Vaskof03ed032020-03-04 13:31:44 +01003292 if (key_or_value && !val_len) {
3293 val_len = strlen(key_or_value);
3294 }
3295
Michal Vaskob104f112020-07-17 09:54:54 +02003296 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3297 /* create a data node and find the instance */
3298 if (schema->nodetype == LYS_LEAFLIST) {
3299 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003300 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3301 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003302 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003303 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003304 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003305 }
3306
3307 /* find it */
3308 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003309 } else {
3310 /* find the first schema node instance */
3311 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003312 }
3313
Michal Vaskoe444f752020-02-10 12:20:06 +01003314 lyd_free_tree(target);
3315 return rc;
3316}
Michal Vaskoccc02342020-05-21 10:09:21 +02003317
3318API LY_ERR
3319lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3320{
3321 LY_ERR ret = LY_SUCCESS;
3322 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003323 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003324 uint32_t i;
3325
3326 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3327
3328 memset(&xp_set, 0, sizeof xp_set);
3329
3330 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003331 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3332 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003333
3334 /* evaluate expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003335 ret = lyxp_eval(exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003336 LY_CHECK_GOTO(ret, cleanup);
3337
3338 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003339 ret = ly_set_new(set);
3340 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003341
3342 /* transform into ly_set */
3343 if (xp_set.type == LYXP_SET_NODE_SET) {
3344 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3345 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003346 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003347 (*set)->size = xp_set.used;
3348
3349 for (i = 0; i < xp_set.used; ++i) {
3350 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003351 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003352 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003353 }
3354 }
3355 }
3356
3357cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003358 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003359 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003360 return ret;
3361}